home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / elf32-mips.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  183KB  |  6,296 lines

  1. /* MIPS-specific support for 32-bit ELF
  2.    Copyright 1993, 1994, 1995, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  22.  
  23. /* This file handles MIPS ELF targets.  SGI Irix 5 uses a slightly
  24.    different MIPS ELF from other targets.  This matters when linking.
  25.    This file supports both, switching at runtime.  */
  26.  
  27. #include "bfd.h"
  28. #include "sysdep.h"
  29. #include "libbfd.h"
  30. #include "bfdlink.h"
  31. #include "genlink.h"
  32. #include "elf-bfd.h"
  33. #include "elf/mips.h"
  34.  
  35. /* Get the ECOFF swapping routines.  */
  36. #include "coff/sym.h"
  37. #include "coff/symconst.h"
  38. #include "coff/internal.h"
  39. #include "coff/ecoff.h"
  40. #include "coff/mips.h"
  41. #define ECOFF_32
  42. #include "ecoffswap.h"
  43.  
  44. static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
  45.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  46. static void mips_info_to_howto_rel
  47.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
  48. static void bfd_mips_elf32_swap_gptab_in
  49.   PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
  50. static void bfd_mips_elf32_swap_gptab_out
  51.   PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
  52. static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
  53. static boolean mips_elf32_object_p PARAMS ((bfd *));
  54. static boolean mips_elf_create_procedure_table
  55.   PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
  56.        struct ecoff_debug_info *));
  57. static int mips_elf_additional_program_headers PARAMS ((bfd *));
  58. static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
  59. static boolean mips_elf32_section_from_shdr
  60.   PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
  61. static boolean mips_elf32_section_processing
  62.   PARAMS ((bfd *, Elf32_Internal_Shdr *));
  63. static boolean mips_elf_is_local_label
  64.   PARAMS ((bfd *, asymbol *));
  65. static struct bfd_hash_entry *mips_elf_link_hash_newfunc
  66.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  67. static struct bfd_link_hash_table *mips_elf_link_hash_table_create
  68.   PARAMS ((bfd *));
  69. static int gptab_compare PARAMS ((const void *, const void *));
  70. static boolean mips_elf_final_link
  71.   PARAMS ((bfd *, struct bfd_link_info *));
  72. static void mips_elf_relocate_hi16
  73.   PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
  74.        bfd_vma));
  75. static void mips_elf_relocate_got_local
  76.   PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
  77.        Elf_Internal_Rela *, bfd_byte *, bfd_vma));
  78. static void mips_elf_relocate_global_got
  79.    PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
  80. static boolean mips_elf_adjust_dynindx
  81.   PARAMS ((struct elf_link_hash_entry *, PTR));
  82. static boolean mips_elf_relocate_section
  83.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  84.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
  85. static boolean mips_elf_create_dynamic_sections
  86.   PARAMS ((bfd *, struct bfd_link_info *));
  87. static boolean mips_elf_create_compact_rel_section
  88.   PARAMS ((bfd *, struct bfd_link_info *));
  89. static boolean mips_elf_create_got_section
  90.   PARAMS ((bfd *, struct bfd_link_info *));
  91. static boolean mips_elf_check_relocs
  92.   PARAMS ((bfd *, struct bfd_link_info *, asection *,
  93.        const Elf_Internal_Rela *));
  94. static boolean mips_elf_adjust_dynamic_symbol
  95.   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
  96. static boolean mips_elf_size_dynamic_sections
  97.   PARAMS ((bfd *, struct bfd_link_info *));
  98. static boolean mips_elf_finish_dynamic_symbol
  99.   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
  100.        Elf_Internal_Sym *));
  101. static boolean mips_elf_finish_dynamic_sections
  102.   PARAMS ((bfd *, struct bfd_link_info *));
  103. static boolean mips_elf_add_symbol_hook
  104.   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
  105.        const char **, flagword *, asection **, bfd_vma *));
  106. static bfd_reloc_status_type mips_elf_final_gp
  107.   PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
  108. static bfd_byte *elf32_mips_get_relocated_section_contents
  109.   PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
  110.        bfd_byte *, boolean, asymbol **));
  111.  
  112. /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
  113.    executables.  FIXME: At the moment, we default to always generating
  114.    Irix 5 executables.  */
  115.  
  116. #define SGI_COMPAT(abfd) (1)
  117.  
  118. /* This structure is used to hold .got information when linking.  It
  119.    is stored in the tdata field of the bfd_elf_section_data structure.  */
  120.  
  121. struct mips_got_info
  122. {
  123.   /* The symbol index of the first global .got symbol.  */
  124.   unsigned long global_gotsym;
  125.   /* The number of local .got entries.  */
  126.   unsigned int local_gotno;
  127. };
  128.  
  129. /* The number of local .got entries we reserve.  */
  130. #define MIPS_RESERVED_GOTNO (2)
  131.  
  132. /* Instructions which appear in a stub.  For some reason the stub is
  133.    slightly different on an SGI system.  */
  134. #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
  135. #define STUB_LW(abfd)                    \
  136.   (SGI_COMPAT (abfd)                    \
  137.    ? 0x8f998010            /* lw t9,0x8010(gp) */    \
  138.    : 0x8f998000)        /* lw t9,0x8000(gp) */
  139. #define STUB_MOVE 0x03e07825    /* move t7,ra */
  140. #define STUB_JALR 0x0320f809    /* jal t9 */
  141. #define STUB_LI16 0x34180000    /* ori t8,zero,0 */
  142. #define MIPS_FUNCTION_STUB_SIZE (16)
  143.  
  144. /* Names of sections which appear in the .dynsym section in an Irix 5
  145.    executable.  */
  146.  
  147. static const char * const mips_elf_dynsym_sec_names[] =
  148. {
  149.   ".text",
  150.   ".init",
  151.   ".fini",
  152.   ".data",
  153.   ".rodata",
  154.   ".sdata",
  155.   ".sbss",
  156.   ".bss",
  157.   NULL
  158. };
  159.  
  160. #define SIZEOF_MIPS_DYNSYM_SECNAMES \
  161.   (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
  162.  
  163. /* The number of entries in mips_elf_dynsym_sec_names which go in the
  164.    text segment.  */
  165.  
  166. #define MIPS_TEXT_DYNSYM_SECNO (3)
  167.  
  168. /* The names of the runtime procedure table symbols used on Irix 5.  */
  169.  
  170. static const char * const mips_elf_dynsym_rtproc_names[] =
  171. {
  172.   "_procedure_table",
  173.   "_procedure_string_table",
  174.   "_procedure_table_size",
  175.   NULL
  176. };
  177.  
  178. /* These structures are used to generate the .compact_rel section on
  179.    Irix 5.  */
  180.  
  181. typedef struct
  182. {
  183.   unsigned long id1;        /* Always one?  */
  184.   unsigned long num;        /* Number of compact relocation entries.  */
  185.   unsigned long id2;        /* Always two?  */
  186.   unsigned long offset;        /* The file offset of the first relocation.  */
  187.   unsigned long reserved0;    /* Zero?  */
  188.   unsigned long reserved1;    /* Zero?  */
  189. } Elf32_compact_rel;
  190.  
  191. typedef struct
  192. {
  193.   bfd_byte id1[4];
  194.   bfd_byte num[4];
  195.   bfd_byte id2[4];
  196.   bfd_byte offset[4];
  197.   bfd_byte reserved0[4];
  198.   bfd_byte reserved1[4];
  199. } Elf32_External_compact_rel;
  200.  
  201. typedef struct
  202. {
  203.   unsigned int ctype : 1;    /* 1: long 0: short format. See below.  */
  204.   unsigned int rtype : 4;    /* Relocation types. See below. */
  205.   unsigned int dist2to : 8;
  206.   unsigned int relvaddr : 19;    /* (VADDR - vaddr of the previous entry)/ 4 */
  207.   unsigned long konst;        /* KONST field. See below.  */
  208.   unsigned long vaddr;        /* VADDR to be relocated.  */
  209. } Elf32_crinfo;
  210.  
  211. typedef struct
  212. {
  213.   unsigned int ctype : 1;    /* 1: long 0: short format. See below.  */
  214.   unsigned int rtype : 4;    /* Relocation types. See below. */
  215.   unsigned int dist2to : 8;
  216.   unsigned int relvaddr : 19;    /* (VADDR - vaddr of the previous entry)/ 4 */
  217.   unsigned long konst;        /* KONST field. See below.  */
  218. } Elf32_crinfo2;
  219.  
  220. typedef struct
  221. {
  222.   bfd_byte info[4];
  223.   bfd_byte konst[4];
  224.   bfd_byte vaddr[4];
  225. } Elf32_External_crinfo;
  226.  
  227. typedef struct
  228. {
  229.   bfd_byte info[4];
  230.   bfd_byte konst[4];
  231. } Elf32_External_crinfo2;
  232.  
  233. /* These are the constants used to swap the bitfields in a crinfo.  */
  234.  
  235. #define CRINFO_CTYPE (0x1)
  236. #define CRINFO_CTYPE_SH (31)
  237. #define CRINFO_RTYPE (0xf)
  238. #define CRINFO_RTYPE_SH (27)
  239. #define CRINFO_DIST2TO (0xff)
  240. #define CRINFO_DIST2TO_SH (19)
  241. #define CRINFO_RELVADDR (0x7ffff)
  242. #define CRINFO_RELVADDR_SH (0)
  243.  
  244. /* A compact relocation info has long (3 words) or short (2 words)
  245.    formats.  A short format doesn't have VADDR field and relvaddr
  246.    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
  247. #define CRF_MIPS_LONG            1
  248. #define CRF_MIPS_SHORT            0
  249.  
  250. /* There are 4 types of compact relocation at least. The value KONST
  251.    has different meaning for each type:
  252.  
  253.    (type)        (konst)
  254.    CT_MIPS_REL32    Address in data
  255.    CT_MIPS_WORD        Address in word (XXX)
  256.    CT_MIPS_GPHI_LO    GP - vaddr
  257.    CT_MIPS_JMPAD    Address to jump
  258.    */
  259.  
  260. #define CRT_MIPS_REL32            0xa
  261. #define CRT_MIPS_WORD            0xb
  262. #define CRT_MIPS_GPHI_LO        0xc
  263. #define CRT_MIPS_JMPAD            0xd
  264.  
  265. #define mips_elf_set_cr_format(x,format)    ((x).ctype = (format))
  266. #define mips_elf_set_cr_type(x,type)        ((x).rtype = (type))
  267. #define mips_elf_set_cr_dist2to(x,v)        ((x).dist2to = (v))
  268. #define mips_elf_set_cr_relvaddr(x,d)        ((x).relvaddr = (d)<<2)
  269.  
  270. static void bfd_elf32_swap_compact_rel_out
  271.   PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
  272. static void bfd_elf32_swap_crinfo_out
  273.   PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
  274.  
  275. #define USE_REL    1        /* MIPS uses REL relocations instead of RELA */
  276.  
  277. enum reloc_type
  278. {
  279.   R_MIPS_NONE = 0,
  280.   R_MIPS_16,        R_MIPS_32,
  281.   R_MIPS_REL32,        R_MIPS_26,
  282.   R_MIPS_HI16,        R_MIPS_LO16,
  283.   R_MIPS_GPREL16,    R_MIPS_LITERAL,
  284.   R_MIPS_GOT16,        R_MIPS_PC16,
  285.   R_MIPS_CALL16,    R_MIPS_GPREL32,
  286.   /* The remaining relocs are defined on Irix, although they are not
  287.      in the MIPS ELF ABI.  */
  288.   R_MIPS_UNUSED1,    R_MIPS_UNUSED2,
  289.   R_MIPS_UNUSED3,
  290.   R_MIPS_SHIFT5,    R_MIPS_SHIFT6,
  291.   R_MIPS_64,        R_MIPS_GOT_DISP,
  292.   R_MIPS_GOT_PAGE,    R_MIPS_GOT_OFST,
  293.   R_MIPS_GOT_HI16,    R_MIPS_GOT_LO16,
  294.   R_MIPS_SUB,        R_MIPS_INSERT_A,
  295.   R_MIPS_INSERT_B,    R_MIPS_DELETE,
  296.   R_MIPS_HIGHER,    R_MIPS_HIGHEST,
  297.   R_MIPS_CALL_HI16,    R_MIPS_CALL_LO16,
  298.   R_MIPS_max
  299. };
  300.  
  301. static reloc_howto_type elf_mips_howto_table[] =
  302. {
  303.   /* No relocation.  */
  304.   HOWTO (R_MIPS_NONE,        /* type */
  305.      0,            /* rightshift */
  306.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  307.      0,            /* bitsize */
  308.      false,            /* pc_relative */
  309.      0,            /* bitpos */
  310.      complain_overflow_dont, /* complain_on_overflow */
  311.      bfd_elf_generic_reloc,    /* special_function */
  312.      "R_MIPS_NONE",        /* name */
  313.      false,            /* partial_inplace */
  314.      0,            /* src_mask */
  315.      0,            /* dst_mask */
  316.      false),        /* pcrel_offset */
  317.  
  318.   /* 16 bit relocation.  */
  319.   HOWTO (R_MIPS_16,        /* type */
  320.      0,            /* rightshift */
  321.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  322.      16,            /* 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_16",        /* name */
  328.      true,            /* partial_inplace */
  329.      0xffff,        /* src_mask */
  330.      0xffff,        /* dst_mask */
  331.      false),        /* pcrel_offset */
  332.  
  333.   /* 32 bit relocation.  */
  334.   HOWTO (R_MIPS_32,        /* type */
  335.      0,            /* rightshift */
  336.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  337.      32,            /* bitsize */
  338.      false,            /* pc_relative */
  339.      0,            /* bitpos */
  340.      complain_overflow_bitfield, /* complain_on_overflow */
  341.      bfd_elf_generic_reloc,    /* special_function */
  342.      "R_MIPS_32",        /* name */
  343.      true,            /* partial_inplace */
  344.      0xffffffff,        /* src_mask */
  345.      0xffffffff,        /* dst_mask */
  346.      false),        /* pcrel_offset */
  347.  
  348.   /* 32 bit symbol relative relocation.  */
  349.   HOWTO (R_MIPS_REL32,        /* type */
  350.      0,            /* rightshift */
  351.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  352.      32,            /* bitsize */
  353.      false,            /* pc_relative */
  354.      0,            /* bitpos */
  355.      complain_overflow_bitfield, /* complain_on_overflow */
  356.      bfd_elf_generic_reloc,    /* special_function */
  357.      "R_MIPS_REL32",    /* name */
  358.      true,            /* partial_inplace */
  359.      0xffffffff,        /* src_mask */
  360.      0xffffffff,        /* dst_mask */
  361.      false),        /* pcrel_offset */
  362.  
  363.   /* 26 bit branch address.  */
  364.   HOWTO (R_MIPS_26,        /* type */
  365.      2,            /* rightshift */
  366.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  367.      26,            /* bitsize */
  368.      false,            /* pc_relative */
  369.      0,            /* bitpos */
  370.      complain_overflow_dont, /* complain_on_overflow */
  371.                  /* This needs complex overflow
  372.                    detection, because the upper four
  373.                    bits must match the PC.  */
  374.      bfd_elf_generic_reloc,    /* special_function */
  375.      "R_MIPS_26",        /* name */
  376.      true,            /* partial_inplace */
  377.      0x3ffffff,        /* src_mask */
  378.      0x3ffffff,        /* dst_mask */
  379.      false),        /* pcrel_offset */
  380.  
  381.   /* High 16 bits of symbol value.  */
  382.   HOWTO (R_MIPS_HI16,        /* type */
  383.      0,            /* rightshift */
  384.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  385.      16,            /* bitsize */
  386.      false,            /* pc_relative */
  387.      0,            /* bitpos */
  388.      complain_overflow_dont, /* complain_on_overflow */
  389.      _bfd_mips_elf_hi16_reloc,    /* special_function */
  390.      "R_MIPS_HI16",        /* name */
  391.      true,            /* partial_inplace */
  392.      0xffff,        /* src_mask */
  393.      0xffff,        /* dst_mask */
  394.      false),        /* pcrel_offset */
  395.  
  396.   /* Low 16 bits of symbol value.  */
  397.   HOWTO (R_MIPS_LO16,        /* type */
  398.      0,            /* rightshift */
  399.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  400.      16,            /* bitsize */
  401.      false,            /* pc_relative */
  402.      0,            /* bitpos */
  403.      complain_overflow_dont, /* complain_on_overflow */
  404.      _bfd_mips_elf_lo16_reloc,    /* special_function */
  405.      "R_MIPS_LO16",        /* name */
  406.      true,            /* partial_inplace */
  407.      0xffff,        /* src_mask */
  408.      0xffff,        /* dst_mask */
  409.      false),        /* pcrel_offset */
  410.  
  411.   /* GP relative reference.  */
  412.   HOWTO (R_MIPS_GPREL16,    /* type */
  413.      0,            /* rightshift */
  414.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  415.      16,            /* bitsize */
  416.      false,            /* pc_relative */
  417.      0,            /* bitpos */
  418.      complain_overflow_signed, /* complain_on_overflow */
  419.      _bfd_mips_elf_gprel16_reloc, /* special_function */
  420.      "R_MIPS_GPREL16",    /* name */
  421.      true,            /* partial_inplace */
  422.      0xffff,        /* src_mask */
  423.      0xffff,        /* dst_mask */
  424.      false),        /* pcrel_offset */
  425.  
  426.   /* Reference to literal section.  */
  427.   HOWTO (R_MIPS_LITERAL,    /* type */
  428.      0,            /* rightshift */
  429.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  430.      16,            /* bitsize */
  431.      false,            /* pc_relative */
  432.      0,            /* bitpos */
  433.      complain_overflow_signed, /* complain_on_overflow */
  434.      _bfd_mips_elf_gprel16_reloc, /* special_function */
  435.      "R_MIPS_LITERAL",    /* name */
  436.      true,            /* partial_inplace */
  437.      0xffff,        /* src_mask */
  438.      0xffff,        /* dst_mask */
  439.      false),        /* pcrel_offset */
  440.  
  441.   /* Reference to global offset table.  */
  442.   HOWTO (R_MIPS_GOT16,        /* type */
  443.      0,            /* rightshift */
  444.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  445.      16,            /* bitsize */
  446.      false,            /* pc_relative */
  447.      0,            /* bitpos */
  448.      complain_overflow_signed, /* complain_on_overflow */
  449.      _bfd_mips_elf_got16_reloc,    /* special_function */
  450.      "R_MIPS_GOT16",    /* name */
  451.      false,            /* partial_inplace */
  452.      0,            /* src_mask */
  453.      0xffff,        /* dst_mask */
  454.      false),        /* pcrel_offset */
  455.  
  456.   /* 16 bit PC relative reference.  */
  457.   HOWTO (R_MIPS_PC16,        /* type */
  458.      0,            /* rightshift */
  459.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  460.      16,            /* bitsize */
  461.      true,            /* pc_relative */
  462.      0,            /* bitpos */
  463.      complain_overflow_signed, /* complain_on_overflow */
  464.      bfd_elf_generic_reloc,    /* special_function */
  465.      "R_MIPS_PC16",        /* name */
  466.      true,            /* partial_inplace */
  467.      0xffff,        /* src_mask */
  468.      0xffff,        /* dst_mask */
  469.      false),        /* pcrel_offset */
  470.  
  471.   /* 16 bit call through global offset table.  */
  472.   /* FIXME: This is not handled correctly.  */
  473.   HOWTO (R_MIPS_CALL16,        /* type */
  474.      0,            /* rightshift */
  475.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  476.      16,            /* bitsize */
  477.      false,            /* pc_relative */
  478.      0,            /* bitpos */
  479.      complain_overflow_signed, /* complain_on_overflow */
  480.      bfd_elf_generic_reloc,    /* special_function */
  481.      "R_MIPS_CALL16",    /* name */
  482.      false,            /* partial_inplace */
  483.      0,            /* src_mask */
  484.      0xffff,        /* dst_mask */
  485.      false),        /* pcrel_offset */
  486.  
  487.   /* 32 bit GP relative reference.  */
  488.   HOWTO (R_MIPS_GPREL32,    /* type */
  489.      0,            /* rightshift */
  490.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  491.      32,            /* bitsize */
  492.      false,            /* pc_relative */
  493.      0,            /* bitpos */
  494.      complain_overflow_bitfield, /* complain_on_overflow */
  495.      _bfd_mips_elf_gprel32_reloc, /* special_function */
  496.      "R_MIPS_GPREL32",    /* name */
  497.      true,            /* partial_inplace */
  498.      0xffffffff,        /* src_mask */
  499.      0xffffffff,        /* dst_mask */
  500.      false),        /* pcrel_offset */
  501.  
  502.     /* The remaining relocs are defined on Irix 5, although they are
  503.        not defined by the ABI.  */
  504.     { 13 },
  505.     { 14 },
  506.     { 15 },
  507.  
  508.   /* A 5 bit shift field.  */
  509.   HOWTO (R_MIPS_SHIFT5,        /* type */
  510.      0,            /* rightshift */
  511.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  512.      5,            /* bitsize */
  513.      false,            /* pc_relative */
  514.      6,            /* bitpos */
  515.      complain_overflow_bitfield, /* complain_on_overflow */
  516.      bfd_elf_generic_reloc,    /* special_function */
  517.      "R_MIPS_SHIFT5",    /* name */
  518.      true,            /* partial_inplace */
  519.      0x000007c0,        /* src_mask */
  520.      0x000007c0,        /* dst_mask */
  521.      false),        /* pcrel_offset */
  522.  
  523.   /* A 6 bit shift field.  */
  524.   /* FIXME: This is not handled correctly; a special function is
  525.      needed to put the most significant bit in the right place.  */
  526.   HOWTO (R_MIPS_SHIFT6,        /* type */
  527.      0,            /* rightshift */
  528.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  529.      6,            /* bitsize */
  530.      false,            /* pc_relative */
  531.      6,            /* bitpos */
  532.      complain_overflow_bitfield, /* complain_on_overflow */
  533.      bfd_elf_generic_reloc,    /* special_function */
  534.      "R_MIPS_SHIFT6",    /* name */
  535.      true,            /* partial_inplace */
  536.      0x000007c4,        /* src_mask */
  537.      0x000007c4,        /* dst_mask */
  538.      false),        /* pcrel_offset */
  539.  
  540.   /* A 64 bit relocation.  Presumably not used in 32 bit ELF.  */
  541.   { R_MIPS_64 },
  542.  
  543.   /* Displacement in the global offset table.  */
  544.   /* FIXME: Not handled correctly.  */
  545.   HOWTO (R_MIPS_GOT_DISP,    /* type */
  546.      0,            /* rightshift */
  547.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  548.      16,            /* bitsize */
  549.      false,            /* pc_relative */
  550.      0,            /* bitpos */
  551.      complain_overflow_bitfield, /* complain_on_overflow */
  552.      bfd_elf_generic_reloc,    /* special_function */
  553.      "R_MIPS_GOT_DISP",    /* name */
  554.      true,            /* partial_inplace */
  555.      0x0000ffff,        /* src_mask */
  556.      0x0000ffff,        /* dst_mask */
  557.      false),        /* pcrel_offset */
  558.  
  559.   /* Displacement to page pointer in the global offset table.  */
  560.   /* FIXME: Not handled correctly.  */
  561.   HOWTO (R_MIPS_GOT_PAGE,    /* type */
  562.      0,            /* rightshift */
  563.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  564.      16,            /* bitsize */
  565.      false,            /* pc_relative */
  566.      0,            /* bitpos */
  567.      complain_overflow_bitfield, /* complain_on_overflow */
  568.      bfd_elf_generic_reloc,    /* special_function */
  569.      "R_MIPS_GOT_PAGE",    /* name */
  570.      true,            /* partial_inplace */
  571.      0x0000ffff,        /* src_mask */
  572.      0x0000ffff,        /* dst_mask */
  573.      false),        /* pcrel_offset */
  574.  
  575.   /* Offset from page pointer in the global offset table.  */
  576.   /* FIXME: Not handled correctly.  */
  577.   HOWTO (R_MIPS_GOT_OFST,    /* type */
  578.      0,            /* rightshift */
  579.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  580.      16,            /* bitsize */
  581.      false,            /* pc_relative */
  582.      0,            /* bitpos */
  583.      complain_overflow_bitfield, /* complain_on_overflow */
  584.      bfd_elf_generic_reloc,    /* special_function */
  585.      "R_MIPS_GOT_OFST",    /* name */
  586.      true,            /* partial_inplace */
  587.      0x0000ffff,        /* src_mask */
  588.      0x0000ffff,        /* dst_mask */
  589.      false),        /* pcrel_offset */
  590.  
  591.   /* High 16 bits of displacement in global offset table.  */
  592.   /* FIXME: Not handled correctly.  */
  593.   HOWTO (R_MIPS_GOT_HI16,    /* type */
  594.      0,            /* rightshift */
  595.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  596.      16,            /* bitsize */
  597.      false,            /* pc_relative */
  598.      0,            /* bitpos */
  599.      complain_overflow_dont, /* complain_on_overflow */
  600.      bfd_elf_generic_reloc,    /* special_function */
  601.      "R_MIPS_GOT_HI16",    /* name */
  602.      true,            /* partial_inplace */
  603.      0x0000ffff,        /* src_mask */
  604.      0x0000ffff,        /* dst_mask */
  605.      false),        /* pcrel_offset */
  606.  
  607.   /* Low 16 bits of displacement in global offset table.  */
  608.   /* FIXME: Not handled correctly.  */
  609.   HOWTO (R_MIPS_GOT_LO16,    /* type */
  610.      0,            /* rightshift */
  611.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  612.      16,            /* bitsize */
  613.      false,            /* pc_relative */
  614.      0,            /* bitpos */
  615.      complain_overflow_dont, /* complain_on_overflow */
  616.      bfd_elf_generic_reloc,    /* special_function */
  617.      "R_MIPS_GOT_LO16",    /* name */
  618.      true,            /* partial_inplace */
  619.      0x0000ffff,        /* src_mask */
  620.      0x0000ffff,        /* dst_mask */
  621.      false),        /* pcrel_offset */
  622.  
  623.   /* 64 bit subtraction.  Presumably not used in 32 bit ELF.  */
  624.   { R_MIPS_SUB },
  625.  
  626.   /* Used to cause the linker to insert and delete instructions?  */
  627.   { R_MIPS_INSERT_A },
  628.   { R_MIPS_INSERT_B },
  629.   { R_MIPS_DELETE },
  630.  
  631.   /* Get the higher values of a 64 bit addend.  Presumably not used in
  632.      32 bit ELF.  */
  633.   { R_MIPS_HIGHER },
  634.   { R_MIPS_HIGHEST },
  635.  
  636.   /* High 16 bits of displacement in global offset table.  */
  637.   /* FIXME: Not handled correctly.  */
  638.   HOWTO (R_MIPS_CALL_HI16,    /* type */
  639.      0,            /* rightshift */
  640.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  641.      16,            /* bitsize */
  642.      false,            /* pc_relative */
  643.      0,            /* bitpos */
  644.      complain_overflow_dont, /* complain_on_overflow */
  645.      bfd_elf_generic_reloc,    /* special_function */
  646.      "R_MIPS_CALL_HI16",    /* name */
  647.      true,            /* partial_inplace */
  648.      0x0000ffff,        /* src_mask */
  649.      0x0000ffff,        /* dst_mask */
  650.      false),        /* pcrel_offset */
  651.  
  652.   /* Low 16 bits of displacement in global offset table.  */
  653.   /* FIXME: Not handled correctly.  */
  654.   HOWTO (R_MIPS_CALL_LO16,    /* type */
  655.      0,            /* rightshift */
  656.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  657.      16,            /* bitsize */
  658.      false,            /* pc_relative */
  659.      0,            /* bitpos */
  660.      complain_overflow_dont, /* complain_on_overflow */
  661.      bfd_elf_generic_reloc,    /* special_function */
  662.      "R_MIPS_CALL_LO16",    /* name */
  663.      true,            /* partial_inplace */
  664.      0x0000ffff,        /* src_mask */
  665.      0x0000ffff,        /* dst_mask */
  666.      false)            /* pcrel_offset */
  667. };
  668.  
  669. /* Do a R_MIPS_HI16 relocation.  This has to be done in combination
  670.    with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
  671.    the HI16.  Here we just save the information we need; we do the
  672.    actual relocation when we see the LO16.  MIPS ELF requires that the
  673.    LO16 immediately follow the HI16.  As a GNU extension, we permit an
  674.    arbitrary number of HI16 relocs to be associated with a single LO16
  675.    reloc.  This extension permits gcc to output the HI and LO relocs
  676.    itself.  */
  677.  
  678. struct mips_hi16
  679. {
  680.   struct mips_hi16 *next;
  681.   bfd_byte *addr;
  682.   bfd_vma addend;
  683. };
  684.  
  685. /* FIXME: This should not be a static variable.  */
  686.  
  687. static struct mips_hi16 *mips_hi16_list;
  688.  
  689. bfd_reloc_status_type
  690. _bfd_mips_elf_hi16_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.   bfd_reloc_status_type ret;
  706.   bfd_vma relocation;
  707.   struct mips_hi16 *n;
  708.  
  709.   /* If we're relocating, and this an external symbol, we don't want
  710.      to change anything.  */
  711.   if (output_bfd != (bfd *) NULL
  712.       && (symbol->flags & BSF_SECTION_SYM) == 0
  713.       && reloc_entry->addend == 0)
  714.     {
  715.       reloc_entry->address += input_section->output_offset;
  716.       return bfd_reloc_ok;
  717.     }
  718.  
  719.   ret = bfd_reloc_ok;
  720.  
  721.   if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
  722.     {
  723.       boolean relocateable;
  724.       bfd_vma gp;
  725.  
  726.       if (ret == bfd_reloc_undefined)
  727.     abort ();
  728.  
  729.       if (output_bfd != NULL)
  730.     relocateable = true;
  731.       else
  732.     {
  733.       relocateable = false;
  734.       output_bfd = symbol->section->output_section->owner;
  735.     }
  736.  
  737.       ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
  738.                    error_message, &gp);
  739.       if (ret != bfd_reloc_ok)
  740.     return ret;
  741.  
  742.       relocation = gp - reloc_entry->address;
  743.     }
  744.   else
  745.     {
  746.       if (bfd_is_und_section (symbol->section)
  747.       && output_bfd == (bfd *) NULL)
  748.     ret = bfd_reloc_undefined;
  749.  
  750.       if (bfd_is_com_section (symbol->section))
  751.     relocation = 0;
  752.       else
  753.     relocation = symbol->value;
  754.     }
  755.  
  756.   relocation += symbol->section->output_section->vma;
  757.   relocation += symbol->section->output_offset;
  758.   relocation += reloc_entry->addend;
  759.  
  760.   if (reloc_entry->address > input_section->_cooked_size)
  761.     return bfd_reloc_outofrange;
  762.  
  763.   /* Save the information, and let LO16 do the actual relocation.  */
  764.   n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
  765.   if (n == NULL)
  766.     return bfd_reloc_outofrange;
  767.   n->addr = (bfd_byte *) data + reloc_entry->address;
  768.   n->addend = relocation;
  769.   n->next = mips_hi16_list;
  770.   mips_hi16_list = n;
  771.  
  772.   if (output_bfd != (bfd *) NULL)
  773.     reloc_entry->address += input_section->output_offset;
  774.  
  775.   return ret;
  776. }
  777.  
  778. /* Do a R_MIPS_LO16 relocation.  This is a straightforward 16 bit
  779.    inplace relocation; this function exists in order to do the
  780.    R_MIPS_HI16 relocation described above.  */
  781.  
  782. bfd_reloc_status_type
  783. _bfd_mips_elf_lo16_reloc (abfd,
  784.              reloc_entry,
  785.              symbol,
  786.              data,
  787.              input_section,
  788.              output_bfd,
  789.              error_message)
  790.      bfd *abfd;
  791.      arelent *reloc_entry;
  792.      asymbol *symbol;
  793.      PTR data;
  794.      asection *input_section;
  795.      bfd *output_bfd;
  796.      char **error_message;
  797. {
  798.   arelent gp_disp_relent;
  799.  
  800.   if (mips_hi16_list != NULL)
  801.     {
  802.       struct mips_hi16 *l;
  803.  
  804.       l = mips_hi16_list;
  805.       while (l != NULL)
  806.     {
  807.       unsigned long insn;
  808.       unsigned long val;
  809.       unsigned long vallo;
  810.       struct mips_hi16 *next;
  811.  
  812.       /* Do the HI16 relocation.  Note that we actually don't need
  813.          to know anything about the LO16 itself, except where to
  814.          find the low 16 bits of the addend needed by the LO16.  */
  815.       insn = bfd_get_32 (abfd, l->addr);
  816.       vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
  817.            & 0xffff);
  818.       val = ((insn & 0xffff) << 16) + vallo;
  819.       val += l->addend;
  820.  
  821.       /* The low order 16 bits are always treated as a signed
  822.          value.  Therefore, a negative value in the low order bits
  823.          requires an adjustment in the high order bits.  We need
  824.          to make this adjustment in two ways: once for the bits we
  825.          took from the data, and once for the bits we are putting
  826.          back in to the data.  */
  827.       if ((vallo & 0x8000) != 0)
  828.         val -= 0x10000;
  829.       if ((val & 0x8000) != 0)
  830.         val += 0x10000;
  831.  
  832.       insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
  833.       bfd_put_32 (abfd, insn, l->addr);
  834.  
  835.       if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
  836.         {
  837.           gp_disp_relent = *reloc_entry;
  838.           reloc_entry = &gp_disp_relent;
  839.           reloc_entry->addend = l->addend;
  840.         }
  841.  
  842.       next = l->next;
  843.       free (l);
  844.       l = next;
  845.     }
  846.  
  847.       mips_hi16_list = NULL;
  848.     }
  849.   else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
  850.     {
  851.       bfd_reloc_status_type ret;
  852.       bfd_vma gp, relocation;
  853.  
  854.       /* FIXME: Does this case ever occur?  */
  855.  
  856.       ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
  857.       if (ret != bfd_reloc_ok)
  858.     return ret;
  859.  
  860.       relocation = gp - reloc_entry->address;
  861.       relocation += symbol->section->output_section->vma;
  862.       relocation += symbol->section->output_offset;
  863.       relocation += reloc_entry->addend;
  864.  
  865.       if (reloc_entry->address > input_section->_cooked_size)
  866.     return bfd_reloc_outofrange;
  867.  
  868.       gp_disp_relent = *reloc_entry;
  869.       reloc_entry = &gp_disp_relent;
  870.       reloc_entry->addend = relocation - 4;
  871.     }
  872.  
  873.   /* Now do the LO16 reloc in the usual way.  */
  874.   return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  875.                 input_section, output_bfd, error_message);
  876. }
  877.  
  878. /* Do a R_MIPS_GOT16 reloc.  This is a reloc against the global offset
  879.    table used for PIC code.  If the symbol is an external symbol, the
  880.    instruction is modified to contain the offset of the appropriate
  881.    entry in the global offset table.  If the symbol is a section
  882.    symbol, the next reloc is a R_MIPS_LO16 reloc.  The two 16 bit
  883.    addends are combined to form the real addend against the section
  884.    symbol; the GOT16 is modified to contain the offset of an entry in
  885.    the global offset table, and the LO16 is modified to offset it
  886.    appropriately.  Thus an offset larger than 16 bits requires a
  887.    modified value in the global offset table.
  888.  
  889.    This implementation suffices for the assembler, but the linker does
  890.    not yet know how to create global offset tables.  */
  891.  
  892. bfd_reloc_status_type
  893. _bfd_mips_elf_got16_reloc (abfd,
  894.               reloc_entry,
  895.               symbol,
  896.               data,
  897.               input_section,
  898.               output_bfd,
  899.               error_message)
  900.      bfd *abfd;
  901.      arelent *reloc_entry;
  902.      asymbol *symbol;
  903.      PTR data;
  904.      asection *input_section;
  905.      bfd *output_bfd;
  906.      char **error_message;
  907. {
  908.   /* If we're relocating, and this an external symbol, we don't want
  909.      to change anything.  */
  910.   if (output_bfd != (bfd *) NULL
  911.       && (symbol->flags & BSF_SECTION_SYM) == 0
  912.       && reloc_entry->addend == 0)
  913.     {
  914.       reloc_entry->address += input_section->output_offset;
  915.       return bfd_reloc_ok;
  916.     }
  917.  
  918.   /* If we're relocating, and this is a local symbol, we can handle it
  919.      just like HI16.  */
  920.   if (output_bfd != (bfd *) NULL
  921.       && (symbol->flags & BSF_SECTION_SYM) != 0)
  922.     return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
  923.                      input_section, output_bfd, error_message);
  924.  
  925.   abort ();
  926. }
  927.  
  928. /* We have to figure out the gp value, so that we can adjust the
  929.    symbol value correctly.  We look up the symbol _gp in the output
  930.    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
  931.    target data.  We don't need to adjust the symbol value for an
  932.    external symbol if we are producing relocateable output.  */
  933.  
  934. static bfd_reloc_status_type
  935. mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
  936.      bfd *output_bfd;
  937.      asymbol *symbol;
  938.      boolean relocateable;
  939.      char **error_message;
  940.      bfd_vma *pgp;
  941. {
  942.   if (bfd_is_und_section (symbol->section)
  943.       && ! relocateable)
  944.     {
  945.       *pgp = 0;
  946.       return bfd_reloc_undefined;
  947.     }
  948.  
  949.   *pgp = _bfd_get_gp_value (output_bfd);
  950.   if (*pgp == 0
  951.       && (! relocateable
  952.       || (symbol->flags & BSF_SECTION_SYM) != 0))
  953.     {
  954.       if (relocateable)
  955.     {
  956.       /* Make up a value.  */
  957.       *pgp = symbol->section->output_section->vma + 0x4000;
  958.       _bfd_set_gp_value (output_bfd, *pgp);
  959.     }
  960.       else
  961.     {
  962.       unsigned int count;
  963.       asymbol **sym;
  964.       unsigned int i;
  965.  
  966.       count = bfd_get_symcount (output_bfd);
  967.       sym = bfd_get_outsymbols (output_bfd);
  968.  
  969.       if (sym == (asymbol **) NULL)
  970.         i = count;
  971.       else
  972.         {
  973.           for (i = 0; i < count; i++, sym++)
  974.         {
  975.           register CONST char *name;
  976.  
  977.           name = bfd_asymbol_name (*sym);
  978.           if (*name == '_' && strcmp (name, "_gp") == 0)
  979.             {
  980.               *pgp = bfd_asymbol_value (*sym);
  981.               _bfd_set_gp_value (output_bfd, *pgp);
  982.               break;
  983.             }
  984.         }
  985.         }
  986.  
  987.       if (i >= count)
  988.         {
  989.           /* Only get the error once.  */
  990.           *pgp = 4;
  991.           _bfd_set_gp_value (output_bfd, *pgp);
  992.           *error_message =
  993.         (char *) "GP relative relocation when _gp not defined";
  994.           return bfd_reloc_dangerous;
  995.         }
  996.     }
  997.     }
  998.  
  999.   return bfd_reloc_ok;
  1000. }
  1001.  
  1002. /* Do a R_MIPS_GPREL16 relocation.  This is a 16 bit value which must
  1003.    become the offset from the gp register.  This function also handles
  1004.    R_MIPS_LITERAL relocations, although those can be handled more
  1005.    cleverly because the entries in the .lit8 and .lit4 sections can be
  1006.    merged.  */
  1007.  
  1008. static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
  1009.                               arelent *, asection *,
  1010.                               boolean, PTR, bfd_vma));
  1011.  
  1012. bfd_reloc_status_type
  1013. _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
  1014.                  output_bfd, error_message)
  1015.      bfd *abfd;
  1016.      arelent *reloc_entry;
  1017.      asymbol *symbol;
  1018.      PTR data;
  1019.      asection *input_section;
  1020.      bfd *output_bfd;
  1021.      char **error_message;
  1022. {
  1023.   boolean relocateable;
  1024.   bfd_reloc_status_type ret;
  1025.   bfd_vma gp;
  1026.  
  1027.   /* If we're relocating, and this is an external symbol with no
  1028.      addend, we don't want to change anything.  We will only have an
  1029.      addend if this is a newly created reloc, not read from an ELF
  1030.      file.  */
  1031.   if (output_bfd != (bfd *) NULL
  1032.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1033.       && reloc_entry->addend == 0)
  1034.     {
  1035.       reloc_entry->address += input_section->output_offset;
  1036.       return bfd_reloc_ok;
  1037.     }
  1038.  
  1039.   if (output_bfd != (bfd *) NULL)
  1040.     relocateable = true;
  1041.   else
  1042.     {
  1043.       relocateable = false;
  1044.       output_bfd = symbol->section->output_section->owner;
  1045.     }
  1046.  
  1047.   ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
  1048.                &gp);
  1049.   if (ret != bfd_reloc_ok)
  1050.     return ret;
  1051.  
  1052.   return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
  1053.               relocateable, data, gp);
  1054. }
  1055.  
  1056. static bfd_reloc_status_type
  1057. gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
  1058.          gp)
  1059.      bfd *abfd;
  1060.      asymbol *symbol;
  1061.      arelent *reloc_entry;
  1062.      asection *input_section;
  1063.      boolean relocateable;
  1064.      PTR data;
  1065.      bfd_vma gp;
  1066. {
  1067.   bfd_vma relocation;
  1068.   unsigned long insn;
  1069.   unsigned long val;
  1070.  
  1071.   if (bfd_is_com_section (symbol->section))
  1072.     relocation = 0;
  1073.   else
  1074.     relocation = symbol->value;
  1075.  
  1076.   relocation += symbol->section->output_section->vma;
  1077.   relocation += symbol->section->output_offset;
  1078.  
  1079.   if (reloc_entry->address > input_section->_cooked_size)
  1080.     return bfd_reloc_outofrange;
  1081.  
  1082.   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  1083.  
  1084.   /* Set val to the offset into the section or symbol.  */
  1085.   if (reloc_entry->howto->src_mask == 0)
  1086.     {
  1087.       /* This case occurs with the 64-bit MIPS ELF ABI.  */
  1088.       val = reloc_entry->addend;
  1089.     }
  1090.   else
  1091.     {
  1092.       val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
  1093.       if (val & 0x8000)
  1094.     val -= 0x10000;
  1095.     }
  1096.  
  1097.   /* Adjust val for the final section location and GP value.  If we
  1098.      are producing relocateable output, we don't want to do this for
  1099.      an external symbol.  */
  1100.   if (! relocateable
  1101.       || (symbol->flags & BSF_SECTION_SYM) != 0)
  1102.     val += relocation - gp;
  1103.  
  1104.   insn = (insn &~ 0xffff) | (val & 0xffff);
  1105.   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  1106.  
  1107.   if (relocateable)
  1108.     reloc_entry->address += input_section->output_offset;
  1109.  
  1110.   /* Make sure it fit in 16 bits.  */
  1111.   if (val >= 0x8000 && val < 0xffff8000)
  1112.     return bfd_reloc_overflow;
  1113.  
  1114.   return bfd_reloc_ok;
  1115. }
  1116.  
  1117. /* Do a R_MIPS_GPREL32 relocation.  Is this 32 bit value the offset
  1118.    from the gp register? XXX */
  1119.  
  1120. static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
  1121.                               arelent *, asection *,
  1122.                               boolean, PTR, bfd_vma));
  1123.  
  1124. bfd_reloc_status_type
  1125. _bfd_mips_elf_gprel32_reloc (abfd,
  1126.             reloc_entry,
  1127.             symbol,
  1128.             data,
  1129.             input_section,
  1130.             output_bfd,
  1131.             error_message)
  1132.      bfd *abfd;
  1133.      arelent *reloc_entry;
  1134.      asymbol *symbol;
  1135.      PTR data;
  1136.      asection *input_section;
  1137.      bfd *output_bfd;
  1138.      char **error_message;
  1139. {
  1140.   boolean relocateable;
  1141.   bfd_reloc_status_type ret;
  1142.   bfd_vma gp;
  1143.  
  1144.   /* If we're relocating, and this is an external symbol with no
  1145.      addend, we don't want to change anything.  We will only have an
  1146.      addend if this is a newly created reloc, not read from an ELF
  1147.      file.  */
  1148.   if (output_bfd != (bfd *) NULL
  1149.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1150.       && reloc_entry->addend == 0)
  1151.     {
  1152.       *error_message = (char *)
  1153.     "32bits gp relative relocation occurs for an external symbol";
  1154.       return bfd_reloc_outofrange;
  1155.     }
  1156.  
  1157.   if (output_bfd != (bfd *) NULL)
  1158.     {
  1159.       relocateable = true;
  1160.       gp = _bfd_get_gp_value (output_bfd);
  1161.     }
  1162.   else
  1163.     {
  1164.       relocateable = false;
  1165.       output_bfd = symbol->section->output_section->owner;
  1166.  
  1167.       ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
  1168.                    error_message, &gp);
  1169.       if (ret != bfd_reloc_ok)
  1170.     return ret;
  1171.     }
  1172.  
  1173.   return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
  1174.               relocateable, data, gp);
  1175. }
  1176.  
  1177. static bfd_reloc_status_type
  1178. gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
  1179.          gp)
  1180.      bfd *abfd;
  1181.      asymbol *symbol;
  1182.      arelent *reloc_entry;
  1183.      asection *input_section;
  1184.      boolean relocateable;
  1185.      PTR data;
  1186.      bfd_vma gp;
  1187. {
  1188.   bfd_vma relocation;
  1189.   unsigned long val;
  1190.  
  1191.   if (bfd_is_com_section (symbol->section))
  1192.     relocation = 0;
  1193.   else
  1194.     relocation = symbol->value;
  1195.  
  1196.   relocation += symbol->section->output_section->vma;
  1197.   relocation += symbol->section->output_offset;
  1198.  
  1199.   if (reloc_entry->address > input_section->_cooked_size)
  1200.     return bfd_reloc_outofrange;
  1201.  
  1202.   if (reloc_entry->howto->src_mask == 0)
  1203.     {
  1204.       /* This case arises with the 64-bit MIPS ELF ABI.  */
  1205.       val = 0;
  1206.     }
  1207.   else
  1208.     val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  1209.  
  1210.   /* Set val to the offset into the section or symbol.  */
  1211.   val += reloc_entry->addend;
  1212.  
  1213.   /* Adjust val for the final section location and GP value.  If we
  1214.      are producing relocateable output, we don't want to do this for
  1215.      an external symbol.  */
  1216.   if (! relocateable
  1217.       || (symbol->flags & BSF_SECTION_SYM) != 0)
  1218.     val += relocation - gp;
  1219.  
  1220.   bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
  1221.  
  1222.   if (relocateable)
  1223.     reloc_entry->address += input_section->output_offset;
  1224.  
  1225.   return bfd_reloc_ok;
  1226. }
  1227.  
  1228. /* A mapping from BFD reloc types to MIPS ELF reloc types.  */
  1229.  
  1230. struct elf_reloc_map {
  1231.   bfd_reloc_code_real_type bfd_reloc_val;
  1232.   enum reloc_type elf_reloc_val;
  1233. };
  1234.  
  1235. static CONST struct elf_reloc_map mips_reloc_map[] =
  1236. {
  1237.   { BFD_RELOC_NONE, R_MIPS_NONE, },
  1238.   { BFD_RELOC_16, R_MIPS_16 },
  1239.   { BFD_RELOC_32, R_MIPS_32 },
  1240.   { BFD_RELOC_CTOR, R_MIPS_32 },
  1241.   { BFD_RELOC_32_PCREL, R_MIPS_REL32 },
  1242.   { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
  1243.   { BFD_RELOC_HI16_S, R_MIPS_HI16 },
  1244.   { BFD_RELOC_LO16, R_MIPS_LO16 },
  1245.   { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
  1246.   { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
  1247.   { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
  1248.   { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
  1249.   { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
  1250.   { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
  1251.   { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
  1252.   { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
  1253.   { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
  1254.   { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 }
  1255. };
  1256.  
  1257. /* Given a BFD reloc type, return a howto structure.  */
  1258.  
  1259. static reloc_howto_type *
  1260. bfd_elf32_bfd_reloc_type_lookup (abfd, code)
  1261.      bfd *abfd;
  1262.      bfd_reloc_code_real_type code;
  1263. {
  1264.   unsigned int i;
  1265.  
  1266.   for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
  1267.     {
  1268.       if (mips_reloc_map[i].bfd_reloc_val == code)
  1269.     return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
  1270.     }
  1271.   return NULL;
  1272. }
  1273.  
  1274. /* Given a MIPS reloc type, fill in an arelent structure.  */
  1275.  
  1276. static void
  1277. mips_info_to_howto_rel (abfd, cache_ptr, dst)
  1278.      bfd *abfd;
  1279.      arelent *cache_ptr;
  1280.      Elf32_Internal_Rel *dst;
  1281. {
  1282.   unsigned int r_type;
  1283.  
  1284.   r_type = ELF32_R_TYPE (dst->r_info);
  1285.   BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
  1286.   cache_ptr->howto = &elf_mips_howto_table[r_type];
  1287.  
  1288.   /* The addend for a GPREL16 or LITERAL relocation comes from the GP
  1289.      value for the object file.  We get the addend now, rather than
  1290.      when we do the relocation, because the symbol manipulations done
  1291.      by the linker may cause us to lose track of the input BFD.  */
  1292.   if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
  1293.       && (r_type == (unsigned int) R_MIPS_GPREL16
  1294.       || r_type == (unsigned int) R_MIPS_LITERAL))
  1295.     cache_ptr->addend = elf_gp (abfd);
  1296. }
  1297.  
  1298. /* A .reginfo section holds a single Elf32_RegInfo structure.  These
  1299.    routines swap this structure in and out.  They are used outside of
  1300.    BFD, so they are globally visible.  */
  1301.  
  1302. void
  1303. bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
  1304.      bfd *abfd;
  1305.      const Elf32_External_RegInfo *ex;
  1306.      Elf32_RegInfo *in;
  1307. {
  1308.   in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
  1309.   in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
  1310.   in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
  1311.   in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
  1312.   in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
  1313.   in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
  1314. }
  1315.  
  1316. void
  1317. bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
  1318.      bfd *abfd;
  1319.      const Elf32_RegInfo *in;
  1320.      Elf32_External_RegInfo *ex;
  1321. {
  1322.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
  1323.         (bfd_byte *) ex->ri_gprmask);
  1324.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
  1325.         (bfd_byte *) ex->ri_cprmask[0]);
  1326.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
  1327.         (bfd_byte *) ex->ri_cprmask[1]);
  1328.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
  1329.         (bfd_byte *) ex->ri_cprmask[2]);
  1330.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
  1331.         (bfd_byte *) ex->ri_cprmask[3]);
  1332.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
  1333.         (bfd_byte *) ex->ri_gp_value);
  1334. }
  1335.  
  1336. /* In the 64 bit ABI, the .MIPS.options section holds register
  1337.    information in an Elf64_Reginfo structure.  These routines swap
  1338.    them in and out.  They are globally visible because they are used
  1339.    outside of BFD.  These routines are here so that gas can call them
  1340.    without worrying about whether the 64 bit ABI has been included.  */
  1341.  
  1342. void
  1343. bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
  1344.      bfd *abfd;
  1345.      const Elf64_External_RegInfo *ex;
  1346.      Elf64_Internal_RegInfo *in;
  1347. {
  1348.   in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
  1349.   in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
  1350.   in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
  1351.   in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
  1352.   in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
  1353.   in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
  1354.   in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
  1355. }
  1356.  
  1357. void
  1358. bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
  1359.      bfd *abfd;
  1360.      const Elf64_Internal_RegInfo *in;
  1361.      Elf64_External_RegInfo *ex;
  1362. {
  1363.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
  1364.         (bfd_byte *) ex->ri_gprmask);
  1365.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
  1366.         (bfd_byte *) ex->ri_pad);
  1367.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
  1368.         (bfd_byte *) ex->ri_cprmask[0]);
  1369.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
  1370.         (bfd_byte *) ex->ri_cprmask[1]);
  1371.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
  1372.         (bfd_byte *) ex->ri_cprmask[2]);
  1373.   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
  1374.         (bfd_byte *) ex->ri_cprmask[3]);
  1375.   bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
  1376.         (bfd_byte *) ex->ri_gp_value);
  1377. }
  1378.  
  1379. /* Swap an entry in a .gptab section.  Note that these routines rely
  1380.    on the equivalence of the two elements of the union.  */
  1381.  
  1382. static void
  1383. bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
  1384.      bfd *abfd;
  1385.      const Elf32_External_gptab *ex;
  1386.      Elf32_gptab *in;
  1387. {
  1388.   in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
  1389.   in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
  1390. }
  1391.  
  1392. static void
  1393. bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
  1394.      bfd *abfd;
  1395.      const Elf32_gptab *in;
  1396.      Elf32_External_gptab *ex;
  1397. {
  1398.   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
  1399.         ex->gt_entry.gt_g_value);
  1400.   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
  1401.         ex->gt_entry.gt_bytes);
  1402. }
  1403.  
  1404. static void
  1405. bfd_elf32_swap_compact_rel_out (abfd, in, ex)
  1406.      bfd *abfd;
  1407.      const Elf32_compact_rel *in;
  1408.      Elf32_External_compact_rel *ex;
  1409. {
  1410.   bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
  1411.   bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
  1412.   bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
  1413.   bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
  1414.   bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
  1415.   bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
  1416. }
  1417.  
  1418. static void
  1419. bfd_elf32_swap_crinfo_out (abfd, in, ex)
  1420.      bfd *abfd;
  1421.      const Elf32_crinfo *in;
  1422.      Elf32_External_crinfo *ex;
  1423. {
  1424.   unsigned long l;
  1425.  
  1426.   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
  1427.        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
  1428.        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
  1429.        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
  1430.   bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
  1431.   bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
  1432.   bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
  1433. }
  1434.  
  1435. /* Swap in an options header.  */
  1436.  
  1437. void
  1438. bfd_mips_elf_swap_options_in (abfd, ex, in)
  1439.      bfd *abfd;
  1440.      const Elf_External_Options *ex;
  1441.      Elf_Internal_Options *in;
  1442. {
  1443.   in->kind = bfd_h_get_8 (abfd, ex->kind);
  1444.   in->size = bfd_h_get_8 (abfd, ex->size);
  1445.   in->section = bfd_h_get_16 (abfd, ex->section);
  1446.   in->info = bfd_h_get_32 (abfd, ex->info);
  1447. }
  1448.  
  1449. /* Swap out an options header.  */
  1450.  
  1451. void
  1452. bfd_mips_elf_swap_options_out (abfd, in, ex)
  1453.      bfd *abfd;
  1454.      const Elf_Internal_Options *in;
  1455.      Elf_External_Options *ex;
  1456. {
  1457.   bfd_h_put_8 (abfd, in->kind, ex->kind);
  1458.   bfd_h_put_8 (abfd, in->size, ex->size);
  1459.   bfd_h_put_16 (abfd, in->section, ex->section);
  1460.   bfd_h_put_32 (abfd, in->info, ex->info);
  1461. }
  1462.  
  1463. /* Determine whether a symbol is global for the purposes of splitting
  1464.    the symbol table into global symbols and local symbols.  At least
  1465.    on Irix 5, this split must be between section symbols and all other
  1466.    symbols.  On most ELF targets the split is between static symbols
  1467.    and externally visible symbols.  */
  1468.  
  1469. /*ARGSUSED*/
  1470. static boolean
  1471. mips_elf_sym_is_global (abfd, sym)
  1472.      bfd *abfd;
  1473.      asymbol *sym;
  1474. {
  1475.   return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
  1476. }
  1477.  
  1478. /* Set the right machine number for a MIPS ELF file.  This is used for
  1479.    both the 32-bit and the 64-bit ABI.  */
  1480.  
  1481. boolean
  1482. _bfd_mips_elf_object_p (abfd)
  1483.      bfd *abfd;
  1484. {
  1485.   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
  1486.     {
  1487.     default:
  1488.     case E_MIPS_ARCH_1:
  1489.       /* Just use the default, which was set in elfcode.h.  */
  1490.       break;
  1491.  
  1492.     case E_MIPS_ARCH_2:
  1493.       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
  1494.       break;
  1495.  
  1496.     case E_MIPS_ARCH_3:
  1497.       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
  1498.       break;
  1499.  
  1500.     case E_MIPS_ARCH_4:
  1501.       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 8000);
  1502.       break;
  1503.     }
  1504.  
  1505.   return true;
  1506. }
  1507.  
  1508. /* Set the right machine number for a 32-bit MIPS ELF file.  */
  1509.  
  1510. static boolean
  1511. mips_elf32_object_p (abfd)
  1512.      bfd *abfd;
  1513. {
  1514.   /* Irix 5 is broken.  Object file symbol tables are not always
  1515.      sorted correctly such that local symbols precede global symbols,
  1516.      and the sh_info field in the symbol table is not always right.  */
  1517.   elf_bad_symtab (abfd) = true;
  1518.  
  1519.   return _bfd_mips_elf_object_p (abfd);
  1520. }
  1521.  
  1522. /* The final processing done just before writing out a MIPS ELF object
  1523.    file.  This gets the MIPS architecture right based on the machine
  1524.    number.  This is used by both the 32-bit and the 64-bit ABI.  */
  1525.  
  1526. /*ARGSUSED*/
  1527. void
  1528. _bfd_mips_elf_final_write_processing (abfd, linker)
  1529.      bfd *abfd;
  1530.      boolean linker;
  1531. {
  1532.   unsigned long val;
  1533.   unsigned int i;
  1534.   Elf_Internal_Shdr **hdrpp;
  1535.  
  1536.   switch (bfd_get_mach (abfd))
  1537.     {
  1538.     case 3000:
  1539.       val = E_MIPS_ARCH_1;
  1540.       break;
  1541.  
  1542.     case 6000:
  1543.       val = E_MIPS_ARCH_2;
  1544.       break;
  1545.  
  1546.     case 4000:
  1547.       val = E_MIPS_ARCH_3;
  1548.       break;
  1549.  
  1550.     case 8000:
  1551.       val = E_MIPS_ARCH_4;
  1552.       break;
  1553.  
  1554.     default:
  1555.       val = 0;
  1556.       break;
  1557.     }
  1558.  
  1559.   elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
  1560.   elf_elfheader (abfd)->e_flags |= val;
  1561.  
  1562.   /* Set the sh_info field for .gptab sections.  */
  1563.   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
  1564.        i < elf_elfheader (abfd)->e_shnum;
  1565.        i++, hdrpp++)
  1566.     {
  1567.       if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB)
  1568.     {
  1569.       const char *name;
  1570.       asection *sec;
  1571.  
  1572.       BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
  1573.       name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
  1574.       BFD_ASSERT (name != NULL
  1575.               && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
  1576.       sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
  1577.       BFD_ASSERT (sec != NULL);
  1578.       (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
  1579.     }
  1580.     }
  1581. }
  1582.  
  1583. /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
  1584.  
  1585. boolean
  1586. _bfd_mips_elf_set_private_flags (abfd, flags)
  1587.      bfd *abfd;
  1588.      flagword flags;
  1589. {
  1590.   BFD_ASSERT (!elf_flags_init (abfd)
  1591.           || elf_elfheader (abfd)->e_flags == flags);
  1592.  
  1593.   elf_elfheader (abfd)->e_flags = flags;
  1594.   elf_flags_init (abfd) = true;
  1595.   return true;
  1596. }
  1597.  
  1598. /* Copy backend specific data from one object module to another */
  1599.  
  1600. boolean
  1601. _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
  1602.      bfd *ibfd;
  1603.      bfd *obfd;
  1604. {
  1605.   /* This function is selected based on the input vector.  We only
  1606.      want to copy information over if the output BFD also uses Elf
  1607.      format.  */
  1608.   if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1609.     return true;
  1610.  
  1611.   BFD_ASSERT (!elf_flags_init (obfd)
  1612.           || (elf_elfheader (obfd)->e_flags
  1613.           == elf_elfheader (ibfd)->e_flags));
  1614.  
  1615.   elf_gp (obfd) = elf_gp (ibfd);
  1616.   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  1617.   elf_flags_init (obfd) = true;
  1618.   return true;
  1619. }
  1620.  
  1621. /* Merge backend specific data from an object file to the output
  1622.    object file when linking.  */
  1623.  
  1624. boolean
  1625. _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
  1626.      bfd *ibfd;
  1627.      bfd *obfd;
  1628. {
  1629.   flagword old_flags;
  1630.   flagword new_flags;
  1631.  
  1632.   /* Check if we have the same endianess */
  1633.   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
  1634.       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
  1635.     {
  1636.       (*_bfd_error_handler)
  1637.     ("%s: compiled for a %s endian system and target is %s endian",
  1638.      bfd_get_filename (ibfd),
  1639.      bfd_big_endian (ibfd) ? "big" : "little",
  1640.      bfd_big_endian (obfd) ? "big" : "little");
  1641.  
  1642.       bfd_set_error (bfd_error_wrong_format);
  1643.       return false;
  1644.     }
  1645.  
  1646.   /* This function is selected based on the input vector.  We only
  1647.      want to copy information over if the output BFD also uses Elf
  1648.      format.  */
  1649.   if (bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1650.     return true;
  1651.  
  1652.   new_flags = elf_elfheader (ibfd)->e_flags;
  1653.   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
  1654.   old_flags = elf_elfheader (obfd)->e_flags;
  1655.  
  1656.   if (!elf_flags_init (obfd))    /* First call, no flags set */
  1657.     {
  1658.       elf_flags_init (obfd) = true;
  1659.       elf_elfheader (obfd)->e_flags = new_flags;
  1660.     }
  1661.   else if (((new_flags ^ old_flags) & ~EF_MIPS_NOREORDER)
  1662.        == 0)            /* Compatible flags are ok */
  1663.     ;
  1664.   else                    /* Incompatible flags */
  1665.     {
  1666.       /* Warn about -fPIC mismatch */
  1667.       if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
  1668.     {
  1669.       new_flags &= ~EF_MIPS_PIC;
  1670.       (*_bfd_error_handler)
  1671.         ("%s: needs all files compiled with -fPIC",
  1672.          bfd_get_filename (ibfd));
  1673.     }
  1674.  
  1675.       if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
  1676.     {
  1677.       new_flags &= ~EF_MIPS_CPIC;
  1678.       (*_bfd_error_handler)
  1679.         ("%s: needs all files compiled with -mabicalls",
  1680.          bfd_get_filename (ibfd));
  1681.     }
  1682.  
  1683.       /* Warn about any other mismatches */
  1684.       if (new_flags != old_flags)
  1685.     (*_bfd_error_handler)
  1686.       ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
  1687.        bfd_get_filename (ibfd), (unsigned long) new_flags,
  1688.        (unsigned long) old_flags);
  1689.  
  1690.       bfd_set_error (bfd_error_bad_value);
  1691.       return false;
  1692.     }
  1693.  
  1694.   return true;
  1695. }
  1696.  
  1697. /* Handle a MIPS specific section when reading an object file.  This
  1698.    is called when elfcode.h finds a section with an unknown type.
  1699.    This routine supports both the 32-bit and 64-bit ELF ABI.
  1700.  
  1701.    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
  1702.    how to.  */
  1703.  
  1704. boolean
  1705. _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
  1706.      bfd *abfd;
  1707.      Elf_Internal_Shdr *hdr;
  1708.      const char *name;
  1709. {
  1710.   /* There ought to be a place to keep ELF backend specific flags, but
  1711.      at the moment there isn't one.  We just keep track of the
  1712.      sections by their name, instead.  Fortunately, the ABI gives
  1713.      suggested names for all the MIPS specific sections, so we will
  1714.      probably get away with this.  */
  1715.   switch (hdr->sh_type)
  1716.     {
  1717.     case SHT_MIPS_LIBLIST:
  1718.       if (strcmp (name, ".liblist") != 0)
  1719.     return false;
  1720.       break;
  1721.     case SHT_MIPS_MSYM:
  1722.       if (strcmp (name, ".msym") != 0)
  1723.     return false;
  1724.       break;
  1725.     case SHT_MIPS_CONFLICT:
  1726.       if (strcmp (name, ".conflict") != 0)
  1727.     return false;
  1728.       break;
  1729.     case SHT_MIPS_GPTAB:
  1730.       if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
  1731.     return false;
  1732.       break;
  1733.     case SHT_MIPS_UCODE:
  1734.       if (strcmp (name, ".ucode") != 0)
  1735.     return false;
  1736.       break;
  1737.     case SHT_MIPS_DEBUG:
  1738.       if (strcmp (name, ".mdebug") != 0)
  1739.     return false;
  1740.       break;
  1741.     case SHT_MIPS_REGINFO:
  1742.       if (strcmp (name, ".reginfo") != 0
  1743.       || hdr->sh_size != sizeof (Elf32_External_RegInfo))
  1744.     return false;
  1745.       break;
  1746.     case SHT_MIPS_OPTIONS:
  1747.       if (strcmp (name, ".options") != 0
  1748.       && strcmp (name, ".MIPS.options") != 0)
  1749.     return false;
  1750.       break;
  1751.     case SHT_MIPS_DWARF:
  1752.       if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
  1753.     return false;
  1754.       break;
  1755.     case SHT_MIPS_EVENTS:
  1756.       if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
  1757.     return false;
  1758.       break;
  1759.     default:
  1760.       return false;
  1761.     }
  1762.  
  1763.   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
  1764.     return false;
  1765.  
  1766.   if (hdr->sh_type == SHT_MIPS_DEBUG)
  1767.     {
  1768.       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
  1769.                    (bfd_get_section_flags (abfd,
  1770.                                hdr->bfd_section)
  1771.                     | SEC_DEBUGGING)))
  1772.     return false;
  1773.     }
  1774.  
  1775.   return true;
  1776. }
  1777.  
  1778. /* Handle a 32-bit MIPS ELF specific section.  */
  1779.  
  1780. static boolean
  1781. mips_elf32_section_from_shdr (abfd, hdr, name)
  1782.      bfd *abfd;
  1783.      Elf_Internal_Shdr *hdr;
  1784.      char *name;
  1785. {
  1786.   if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
  1787.     return false;
  1788.  
  1789.   /* FIXME: We should record sh_info for a .gptab section.  */
  1790.  
  1791.   /* For a .reginfo section, set the gp value in the tdata information
  1792.      from the contents of this section.  We need the gp value while
  1793.      processing relocs, so we just get it now.  The .reginfo section
  1794.      is not used in the 64-bit MIPS ELF ABI.  */
  1795.   if (hdr->sh_type == SHT_MIPS_REGINFO)
  1796.     {
  1797.       Elf32_External_RegInfo ext;
  1798.       Elf32_RegInfo s;
  1799.  
  1800.       if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
  1801.                       (file_ptr) 0, sizeof ext))
  1802.     return false;
  1803.       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
  1804.       elf_gp (abfd) = s.ri_gp_value;
  1805.     }
  1806.  
  1807.   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
  1808.      set the gp value based on what we find.  We may see both
  1809.      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
  1810.      they should agree.  */
  1811.   if (hdr->sh_type == SHT_MIPS_OPTIONS)
  1812.     {
  1813.       bfd_byte *contents, *l, *lend;
  1814.  
  1815.       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
  1816.       if (contents == NULL)
  1817.     return false;
  1818.       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
  1819.                       (file_ptr) 0, hdr->sh_size))
  1820.     {
  1821.       free (contents);
  1822.       return false;
  1823.     }
  1824.       l = contents;
  1825.       lend = contents + hdr->sh_size;
  1826.       while (l + sizeof (Elf_External_Options) <= lend)
  1827.     {
  1828.       Elf_Internal_Options intopt;
  1829.  
  1830.       bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
  1831.                     &intopt);
  1832.       if (intopt.kind == ODK_REGINFO)
  1833.         {
  1834.           Elf32_RegInfo intreg;
  1835.  
  1836.           bfd_mips_elf32_swap_reginfo_in
  1837.         (abfd,
  1838.          ((Elf32_External_RegInfo *)
  1839.           (l + sizeof (Elf_External_Options))),
  1840.          &intreg);
  1841.           elf_gp (abfd) = intreg.ri_gp_value;
  1842.         }
  1843.       l += intopt.size;
  1844.     }
  1845.       free (contents);
  1846.     }
  1847.  
  1848.   return true;
  1849. }
  1850.  
  1851. /* Set the correct type for a MIPS ELF section.  We do this by the
  1852.    section name, which is a hack, but ought to work.  This routine is
  1853.    used by both the 32-bit and the 64-bit ABI.  */
  1854.  
  1855. boolean
  1856. _bfd_mips_elf_fake_sections (abfd, hdr, sec)
  1857.      bfd *abfd;
  1858.      Elf32_Internal_Shdr *hdr;
  1859.      asection *sec;
  1860. {
  1861.   register const char *name;
  1862.  
  1863.   name = bfd_get_section_name (abfd, sec);
  1864.  
  1865.   if (strcmp (name, ".liblist") == 0)
  1866.     {
  1867.       hdr->sh_type = SHT_MIPS_LIBLIST;
  1868.       hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
  1869.       /* FIXME: Set the sh_link field.  */
  1870.     }
  1871.   else if (strcmp (name, ".msym") == 0)
  1872.     {
  1873.       hdr->sh_type = SHT_MIPS_MSYM;
  1874.       hdr->sh_entsize = 8;
  1875.       /* FIXME: Set the sh_info field.  */
  1876.     }
  1877.   else if (strcmp (name, ".conflict") == 0)
  1878.     hdr->sh_type = SHT_MIPS_CONFLICT;
  1879.   else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
  1880.     {
  1881.       hdr->sh_type = SHT_MIPS_GPTAB;
  1882.       hdr->sh_entsize = sizeof (Elf32_External_gptab);
  1883.       /* The sh_info field is set in final_write_processing.  */
  1884.     }
  1885.   else if (strcmp (name, ".ucode") == 0)
  1886.     hdr->sh_type = SHT_MIPS_UCODE;
  1887.   else if (strcmp (name, ".mdebug") == 0)
  1888.     {
  1889.       hdr->sh_type = SHT_MIPS_DEBUG;
  1890.       /* In a shared object on Irix 5.3, the .mdebug section has an
  1891.          entsize of 0.  FIXME: Does this matter?  */
  1892.       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
  1893.     hdr->sh_entsize = 0;
  1894.       else
  1895.     hdr->sh_entsize = 1;
  1896.     }
  1897.   else if (strcmp (name, ".reginfo") == 0)
  1898.     {
  1899.       hdr->sh_type = SHT_MIPS_REGINFO;
  1900.       /* In a shared object on Irix 5.3, the .reginfo section has an
  1901.          entsize of 0x18.  FIXME: Does this matter?  */
  1902.       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
  1903.     hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
  1904.       else
  1905.     hdr->sh_entsize = 1;
  1906.  
  1907.       /* Force the section size to the correct value, even if the
  1908.      linker thinks it is larger.  The link routine below will only
  1909.      write out this much data for .reginfo.  */
  1910.       hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
  1911.     }
  1912.   else if (SGI_COMPAT (abfd)
  1913.        && (strcmp (name, ".hash") == 0
  1914.            || strcmp (name, ".dynamic") == 0
  1915.            || strcmp (name, ".dynstr") == 0))
  1916.     {
  1917.       hdr->sh_entsize = 0;
  1918.       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
  1919.     }
  1920.   else if (strcmp (name, ".got") == 0
  1921.        || strcmp (name, ".sdata") == 0
  1922.        || strcmp (name, ".sbss") == 0
  1923.        || strcmp (name, ".lit4") == 0
  1924.        || strcmp (name, ".lit8") == 0)
  1925.     hdr->sh_flags |= SHF_MIPS_GPREL;
  1926.   else if (strcmp (name, ".options") == 0
  1927.        || strcmp (name, ".MIPS.options") == 0)
  1928.     {
  1929.       hdr->sh_type = SHT_MIPS_OPTIONS;
  1930.       hdr->sh_entsize = 1;
  1931.     }
  1932.   else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
  1933.     hdr->sh_type = SHT_MIPS_DWARF;
  1934.   else if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
  1935.     hdr->sh_type = SHT_MIPS_EVENTS;
  1936.  
  1937.   return true;
  1938. }
  1939.  
  1940. /* Given a BFD section, try to locate the corresponding ELF section
  1941.    index.  This is used by both the 32-bit and the 64-bit ABI.
  1942.    Actually, it's not clear to me that the 64-bit ABI supports these,
  1943.    but for non-PIC objects we will certainly want support for at least
  1944.    the .scommon section.  */
  1945.  
  1946. boolean
  1947. _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
  1948.      bfd *abfd;
  1949.      Elf32_Internal_Shdr *hdr;
  1950.      asection *sec;
  1951.      int *retval;
  1952. {
  1953.   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
  1954.     {
  1955.       *retval = SHN_MIPS_SCOMMON;
  1956.       return true;
  1957.     }
  1958.   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
  1959.     {
  1960.       *retval = SHN_MIPS_ACOMMON;
  1961.       return true;
  1962.     }
  1963.   return false;
  1964. }
  1965.  
  1966. /* When are writing out the .options or .MIPS.options section,
  1967.    remember the bytes we are writing out, so that we can install the
  1968.    GP value in the section_processing routine.  */
  1969.  
  1970. boolean
  1971. _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
  1972.      bfd *abfd;
  1973.      sec_ptr section;
  1974.      PTR location;
  1975.      file_ptr offset;
  1976.      bfd_size_type count;
  1977. {
  1978.   if (strcmp (section->name, ".options") == 0
  1979.       || strcmp (section->name, ".MIPS.options") == 0)
  1980.     {
  1981.       bfd_byte *c;
  1982.  
  1983.       if (elf_section_data (section) == NULL)
  1984.     {
  1985.       section->used_by_bfd =
  1986.         (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
  1987.       if (elf_section_data (section) == NULL)
  1988.         return false;
  1989.     }
  1990.       c = (bfd_byte *) elf_section_data (section)->tdata;
  1991.       if (c == NULL)
  1992.     {
  1993.       bfd_size_type size;
  1994.  
  1995.       if (section->_cooked_size != 0)
  1996.         size = section->_cooked_size;
  1997.       else
  1998.         size = section->_raw_size;
  1999.       c = (PTR) bfd_zalloc (abfd, size);
  2000.       if (c == NULL)
  2001.         return false;
  2002.       elf_section_data (section)->tdata = (PTR) c;
  2003.     }
  2004.  
  2005.       memcpy (c + offset, location, count);
  2006.     }
  2007.  
  2008.   return _bfd_elf_set_section_contents (abfd, section, location, offset,
  2009.                     count);
  2010. }
  2011.  
  2012. /* Work over a section just before writing it out.  This routine is
  2013.    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
  2014.    sections that need the SHF_MIPS_GPREL flag by name; there has to be
  2015.    a better way.  */
  2016.  
  2017. boolean
  2018. _bfd_mips_elf_section_processing (abfd, hdr)
  2019.      bfd *abfd;
  2020.      Elf_Internal_Shdr *hdr;
  2021. {
  2022.   if (hdr->bfd_section != NULL)
  2023.     {
  2024.       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
  2025.  
  2026.       if (strcmp (name, ".sdata") == 0)
  2027.     {
  2028.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  2029.       hdr->sh_type = SHT_PROGBITS;
  2030.     }
  2031.       else if (strcmp (name, ".sbss") == 0)
  2032.     {
  2033.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  2034.       hdr->sh_type = SHT_NOBITS;
  2035.     }
  2036.       else if (strcmp (name, ".lit8") == 0
  2037.            || strcmp (name, ".lit4") == 0)
  2038.     {
  2039.       hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
  2040.       hdr->sh_type = SHT_PROGBITS;
  2041.     }
  2042.       else if (strcmp (name, ".compact_rel") == 0)
  2043.     {
  2044.       hdr->sh_flags = 0;
  2045.       hdr->sh_type = SHT_PROGBITS;
  2046.     }
  2047.       else if (strcmp (name, ".rtproc") == 0)
  2048.     {
  2049.       if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
  2050.         {
  2051.           unsigned int adjust;
  2052.  
  2053.           adjust = hdr->sh_size % hdr->sh_addralign;
  2054.           if (adjust != 0)
  2055.         hdr->sh_size += hdr->sh_addralign - adjust;
  2056.         }
  2057.     }
  2058.     }
  2059.  
  2060.   return true;
  2061. }
  2062.  
  2063. /* Work over a section just before writing it out.  We update the GP
  2064.    value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
  2065.    on the value we are using.  */
  2066.  
  2067. static boolean
  2068. mips_elf32_section_processing (abfd, hdr)
  2069.      bfd *abfd;
  2070.      Elf32_Internal_Shdr *hdr;
  2071. {
  2072.   if (hdr->sh_type == SHT_MIPS_REGINFO)
  2073.     {
  2074.       bfd_byte buf[4];
  2075.  
  2076.       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
  2077.       BFD_ASSERT (hdr->contents == NULL);
  2078.  
  2079.       if (bfd_seek (abfd,
  2080.             hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
  2081.             SEEK_SET) == -1)
  2082.     return false;
  2083.       bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
  2084.       if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
  2085.     return false;
  2086.     }
  2087.  
  2088.   if (hdr->sh_type == SHT_MIPS_OPTIONS
  2089.       && hdr->bfd_section != NULL
  2090.       && elf_section_data (hdr->bfd_section) != NULL
  2091.       && elf_section_data (hdr->bfd_section)->tdata != NULL)
  2092.     {
  2093.       bfd_byte *contents, *l, *lend;
  2094.  
  2095.       /* We stored the section contents in the elf_section_data tdata
  2096.      field in the set_section_contents routine.  We save the
  2097.      section contents so that we don't have to read them again.
  2098.      At this point we know that elf_gp is set, so we can look
  2099.      through the section contents to see if there is an
  2100.      ODK_REGINFO structure.  */
  2101.  
  2102.       contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
  2103.       l = contents;
  2104.       lend = contents + hdr->sh_size;
  2105.       while (l + sizeof (Elf_External_Options) <= lend)
  2106.     {
  2107.       Elf_Internal_Options intopt;
  2108.  
  2109.       bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
  2110.                     &intopt);
  2111.       if (intopt.kind == ODK_REGINFO)
  2112.         {
  2113.           bfd_byte buf[4];
  2114.  
  2115.           if (bfd_seek (abfd,
  2116.                 (hdr->sh_offset
  2117.                  + (l - contents)
  2118.                  + sizeof (Elf_External_Options)
  2119.                  + (sizeof (Elf32_External_RegInfo) - 4)),
  2120.                  SEEK_SET) == -1)
  2121.         return false;
  2122.           bfd_h_put_32 (abfd, elf_gp (abfd), buf);
  2123.           if (bfd_write (buf, 1, 4, abfd) != 4)
  2124.         return false;
  2125.         }
  2126.       l += intopt.size;
  2127.     }
  2128.     }
  2129.  
  2130.   return _bfd_mips_elf_section_processing (abfd, hdr);
  2131. }
  2132.  
  2133. /* MIPS ELF uses two common sections.  One is the usual one, and the
  2134.    other is for small objects.  All the small objects are kept
  2135.    together, and then referenced via the gp pointer, which yields
  2136.    faster assembler code.  This is what we use for the small common
  2137.    section.  This approach is copied from ecoff.c.  */
  2138. static asection mips_elf_scom_section;
  2139. static asymbol mips_elf_scom_symbol;
  2140. static asymbol *mips_elf_scom_symbol_ptr;
  2141.  
  2142. /* MIPS ELF also uses an acommon section, which represents an
  2143.    allocated common symbol which may be overridden by a     
  2144.    definition in a shared library.  */
  2145. static asection mips_elf_acom_section;
  2146. static asymbol mips_elf_acom_symbol;
  2147. static asymbol *mips_elf_acom_symbol_ptr;
  2148.  
  2149. /* The Irix 5 support uses two virtual sections, which represent
  2150.    text/data symbols defined in dynamic objects.  */
  2151. static asection mips_elf_text_section;
  2152. static asection *mips_elf_text_section_ptr;
  2153. static asymbol mips_elf_text_symbol;
  2154. static asymbol *mips_elf_text_symbol_ptr;
  2155.  
  2156. static asection mips_elf_data_section;
  2157. static asection *mips_elf_data_section_ptr;
  2158. static asymbol mips_elf_data_symbol;
  2159. static asymbol *mips_elf_data_symbol_ptr;
  2160.  
  2161. /* Handle the special MIPS section numbers that a symbol may use.
  2162.    This is used for both the 32-bit and the 64-bit ABI.  */
  2163.  
  2164. void
  2165. _bfd_mips_elf_symbol_processing (abfd, asym)
  2166.      bfd *abfd;
  2167.      asymbol *asym;
  2168. {
  2169.   elf_symbol_type *elfsym;
  2170.  
  2171.   elfsym = (elf_symbol_type *) asym;
  2172.   switch (elfsym->internal_elf_sym.st_shndx)
  2173.     {
  2174.     case SHN_MIPS_ACOMMON:
  2175.       /* This section is used in a dynamically linked executable file.
  2176.      It is an allocated common section.  The dynamic linker can
  2177.      either resolve these symbols to something in a shared
  2178.      library, or it can just leave them here.  For our purposes,
  2179.      we can consider these symbols to be in a new section.  */
  2180.       if (mips_elf_acom_section.name == NULL)
  2181.     {
  2182.       /* Initialize the acommon section.  */
  2183.       mips_elf_acom_section.name = ".acommon";
  2184.       mips_elf_acom_section.flags = SEC_ALLOC;
  2185.       mips_elf_acom_section.output_section = &mips_elf_acom_section;
  2186.       mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
  2187.       mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
  2188.       mips_elf_acom_symbol.name = ".acommon";
  2189.       mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
  2190.       mips_elf_acom_symbol.section = &mips_elf_acom_section;
  2191.       mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
  2192.     }
  2193.       asym->section = &mips_elf_acom_section;
  2194.       break;
  2195.  
  2196.     case SHN_COMMON:
  2197.       /* Common symbols less than the GP size are automatically
  2198.      treated as SHN_MIPS_SCOMMON symbols.  */
  2199.       if (asym->value > elf_gp_size (abfd))
  2200.     break;
  2201.       /* Fall through.  */
  2202.     case SHN_MIPS_SCOMMON:
  2203.       if (mips_elf_scom_section.name == NULL)
  2204.     {
  2205.       /* Initialize the small common section.  */
  2206.       mips_elf_scom_section.name = ".scommon";
  2207.       mips_elf_scom_section.flags = SEC_IS_COMMON;
  2208.       mips_elf_scom_section.output_section = &mips_elf_scom_section;
  2209.       mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
  2210.       mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
  2211.       mips_elf_scom_symbol.name = ".scommon";
  2212.       mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
  2213.       mips_elf_scom_symbol.section = &mips_elf_scom_section;
  2214.       mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
  2215.     }
  2216.       asym->section = &mips_elf_scom_section;
  2217.       asym->value = elfsym->internal_elf_sym.st_size;
  2218.       break;
  2219.  
  2220.     case SHN_MIPS_SUNDEFINED:
  2221.       asym->section = bfd_und_section_ptr;
  2222.       break;
  2223.  
  2224. #if 0 /* for SGI_COMPAT */
  2225.     case SHN_MIPS_TEXT:
  2226.       asym->section = mips_elf_text_section_ptr;
  2227.       break;
  2228.  
  2229.     case SHN_MIPS_DATA:
  2230.       asym->section = mips_elf_data_section_ptr;
  2231.       break;
  2232. #endif
  2233.     }
  2234. }
  2235.  
  2236. /* When creating an Irix 5 executable, we need REGINFO and RTPROC
  2237.    segments.  */
  2238.  
  2239. static int
  2240. mips_elf_additional_program_headers (abfd)
  2241.      bfd *abfd;
  2242. {
  2243.   asection *s;
  2244.   int ret;
  2245.  
  2246.   ret = 0;
  2247.  
  2248.   if (! SGI_COMPAT (abfd))
  2249.     return ret;
  2250.  
  2251.   s = bfd_get_section_by_name (abfd, ".reginfo");
  2252.   if (s != NULL && (s->flags & SEC_LOAD) != 0)
  2253.     {
  2254.       /* We need a PT_MIPS_REGINFO segment.  */
  2255.       ++ret;
  2256.     }
  2257.  
  2258.   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
  2259.       && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
  2260.     {
  2261.       /* We need a PT_MIPS_RTPROC segment.  */
  2262.       ++ret;
  2263.     }
  2264.  
  2265.   return ret;
  2266. }
  2267.  
  2268. /* Modify the segment map for an Irix 5 executable.  */
  2269.  
  2270. static boolean
  2271. mips_elf_modify_segment_map (abfd)
  2272.      bfd *abfd;
  2273. {
  2274.   asection *s;
  2275.   struct elf_segment_map *m, **pm;
  2276.  
  2277.   if (! SGI_COMPAT (abfd))
  2278.     return true;
  2279.  
  2280.   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
  2281.      segment.  */
  2282.   s = bfd_get_section_by_name (abfd, ".reginfo");
  2283.   if (s != NULL && (s->flags & SEC_LOAD) != 0)
  2284.     {
  2285.       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
  2286.     if (m->p_type == PT_MIPS_REGINFO)
  2287.       break;
  2288.       if (m == NULL)
  2289.     {
  2290.       m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
  2291.       if (m == NULL)
  2292.         return false;
  2293.  
  2294.       m->p_type = PT_MIPS_REGINFO;
  2295.       m->count = 1;
  2296.       m->sections[0] = s;
  2297.  
  2298.       /* We want to put it after the PHDR and INTERP segments.  */
  2299.       pm = &elf_tdata (abfd)->segment_map;
  2300.       while (*pm != NULL
  2301.          && ((*pm)->p_type == PT_PHDR
  2302.              || (*pm)->p_type == PT_INTERP))
  2303.         pm = &(*pm)->next;
  2304.  
  2305.       m->next = *pm;
  2306.       *pm = m;
  2307.     }
  2308.     }
  2309.       
  2310.   /* If there are .dynamic and .mdebug sections, we make a room for
  2311.      the RTPROC header.  FIXME: Rewrite without section names.  */
  2312.   if (bfd_get_section_by_name (abfd, ".interp") == NULL
  2313.       && bfd_get_section_by_name (abfd, ".dynamic") != NULL
  2314.       && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
  2315.     {
  2316.       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
  2317.     if (m->p_type == PT_MIPS_RTPROC)
  2318.       break;
  2319.       if (m == NULL)
  2320.     {
  2321.       m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
  2322.       if (m == NULL)
  2323.         return false;
  2324.  
  2325.       m->p_type = PT_MIPS_RTPROC;
  2326.  
  2327.       s = bfd_get_section_by_name (abfd, ".rtproc");
  2328.       if (s == NULL)
  2329.         {
  2330.           m->count = 0;
  2331.           m->p_flags = 0;
  2332.           m->p_flags_valid = 1;
  2333.         }
  2334.       else
  2335.         {
  2336.           m->count = 1;
  2337.           m->sections[0] = s;
  2338.         }
  2339.  
  2340.       /* We want to put it after the DYNAMIC segment.  */
  2341.       pm = &elf_tdata (abfd)->segment_map;
  2342.       while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
  2343.         pm = &(*pm)->next;
  2344.       if (*pm != NULL)
  2345.         pm = &(*pm)->next;
  2346.  
  2347.       m->next = *pm;
  2348.       *pm = m;
  2349.     }
  2350.     }
  2351.  
  2352.   /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
  2353.      .dynsym, and .hash sections, and everything in between.  */
  2354.   for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
  2355.     if ((*pm)->p_type == PT_DYNAMIC)
  2356.       break;
  2357.   m = *pm;
  2358.   if (m != NULL
  2359.       && m->count == 1
  2360.       && strcmp (m->sections[0]->name, ".dynamic") == 0)
  2361.     {
  2362.       static const char *sec_names[] =
  2363.     { ".dynamic", ".dynstr", ".dynsym", ".hash" };
  2364.       bfd_vma low, high;
  2365.       unsigned int i, c;
  2366.       struct elf_segment_map *n;
  2367.  
  2368.       low = 0xffffffff;
  2369.       high = 0;
  2370.       for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
  2371.     {
  2372.       s = bfd_get_section_by_name (abfd, sec_names[i]);
  2373.       if (s != NULL && (s->flags & SEC_LOAD) != 0)
  2374.         {
  2375.           bfd_size_type sz;
  2376.  
  2377.           if (low > s->vma)
  2378.         low = s->vma;
  2379.           sz = s->_cooked_size;
  2380.           if (sz == 0)
  2381.         sz = s->_raw_size;
  2382.           if (high < s->vma + sz)
  2383.         high = s->vma + sz;
  2384.         }
  2385.     }
  2386.  
  2387.       c = 0;
  2388.       for (s = abfd->sections; s != NULL; s = s->next)
  2389.     if ((s->flags & SEC_LOAD) != 0
  2390.         && s->vma >= low
  2391.         && ((s->vma
  2392.          + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
  2393.         <= high))
  2394.       ++c;
  2395.  
  2396.       n = ((struct elf_segment_map *)
  2397.        bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
  2398.       if (n == NULL)
  2399.     return false;
  2400.       *n = *m;
  2401.       n->count = c;
  2402.  
  2403.       i = 0;
  2404.       for (s = abfd->sections; s != NULL; s = s->next)
  2405.     {
  2406.       if ((s->flags & SEC_LOAD) != 0
  2407.           && s->vma >= low
  2408.           && ((s->vma
  2409.            + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
  2410.           <= high))
  2411.         {
  2412.           n->sections[i] = s;
  2413.           ++i;
  2414.         }
  2415.     }
  2416.  
  2417.       *pm = n;
  2418.     }
  2419.  
  2420.   return true;
  2421. }
  2422.  
  2423. /* The structure of the runtime procedure descriptor created by the
  2424.    loader for use by the static exception system.  */
  2425.  
  2426. typedef struct runtime_pdr {
  2427.     bfd_vma    adr;        /* memory address of start of procedure */
  2428.     long    regmask;    /* save register mask */
  2429.     long    regoffset;    /* save register offset */
  2430.     long    fregmask;    /* save floating point register mask */
  2431.     long    fregoffset;    /* save floating point register offset */
  2432.     long    frameoffset;    /* frame size */
  2433.     short    framereg;    /* frame pointer register */
  2434.     short    pcreg;        /* offset or reg of return pc */
  2435.     long    irpss;        /* index into the runtime string table */
  2436.     long    reserved;
  2437.     struct exception_info *exception_info;/* pointer to exception array */
  2438. } RPDR, *pRPDR;
  2439. #define cbRPDR sizeof(RPDR)
  2440. #define rpdNil ((pRPDR) 0)
  2441.  
  2442. /* Swap RPDR (runtime procedure table entry) for output.  */
  2443.  
  2444. static void ecoff_swap_rpdr_out
  2445.   PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
  2446.  
  2447. static void
  2448. ecoff_swap_rpdr_out (abfd, in, ex)
  2449.      bfd *abfd;
  2450.      const RPDR *in;
  2451.      struct rpdr_ext *ex;
  2452. {
  2453.   /* ecoff_put_off was defined in ecoffswap.h.  */
  2454.   ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
  2455.   bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
  2456.   bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
  2457.   bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
  2458.   bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
  2459.   bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
  2460.  
  2461.   bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
  2462.   bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
  2463.  
  2464.   bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
  2465. #if 0 /* FIXME */
  2466.   ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
  2467. #endif
  2468. }
  2469.  
  2470. /* Read ECOFF debugging information from a .mdebug section into a
  2471.    ecoff_debug_info structure.  */
  2472.  
  2473. boolean
  2474. _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
  2475.      bfd *abfd;
  2476.      asection *section;
  2477.      struct ecoff_debug_info *debug;
  2478. {
  2479.   HDRR *symhdr;
  2480.   const struct ecoff_debug_swap *swap;
  2481.   char *ext_hdr = NULL;
  2482.  
  2483.   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  2484.  
  2485.   ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
  2486.   if (ext_hdr == NULL && swap->external_hdr_size != 0)
  2487.     goto error_return;
  2488.  
  2489.   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
  2490.                 swap->external_hdr_size)
  2491.       == false)
  2492.     goto error_return;
  2493.  
  2494.   symhdr = &debug->symbolic_header;
  2495.   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
  2496.  
  2497.   /* The symbolic header contains absolute file offsets and sizes to
  2498.      read.  */
  2499. #define READ(ptr, offset, count, size, type)                \
  2500.   if (symhdr->count == 0)                        \
  2501.     debug->ptr = NULL;                            \
  2502.   else                                    \
  2503.     {                                    \
  2504.       debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count));    \
  2505.       if (debug->ptr == NULL)                        \
  2506.     goto error_return;                        \
  2507.       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0    \
  2508.       || (bfd_read (debug->ptr, size, symhdr->count,        \
  2509.             abfd) != size * symhdr->count))            \
  2510.     goto error_return;                        \
  2511.     }
  2512.  
  2513.   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
  2514.   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
  2515.   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
  2516.   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
  2517.   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
  2518.   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
  2519.     union aux_ext *);
  2520.   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
  2521.   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
  2522.   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
  2523.   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
  2524.   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
  2525. #undef READ
  2526.  
  2527.   debug->fdr = NULL;
  2528.   debug->adjust = NULL;
  2529.  
  2530.   return true;
  2531.  
  2532.  error_return:
  2533.   if (ext_hdr != NULL)
  2534.     free (ext_hdr);
  2535.   if (debug->line != NULL)
  2536.     free (debug->line);
  2537.   if (debug->external_dnr != NULL)
  2538.     free (debug->external_dnr);
  2539.   if (debug->external_pdr != NULL)
  2540.     free (debug->external_pdr);
  2541.   if (debug->external_sym != NULL)
  2542.     free (debug->external_sym);
  2543.   if (debug->external_opt != NULL)
  2544.     free (debug->external_opt);
  2545.   if (debug->external_aux != NULL)
  2546.     free (debug->external_aux);
  2547.   if (debug->ss != NULL)
  2548.     free (debug->ss);
  2549.   if (debug->ssext != NULL)
  2550.     free (debug->ssext);
  2551.   if (debug->external_fdr != NULL)
  2552.     free (debug->external_fdr);
  2553.   if (debug->external_rfd != NULL)
  2554.     free (debug->external_rfd);
  2555.   if (debug->external_ext != NULL)
  2556.     free (debug->external_ext);
  2557.   return false;
  2558. }
  2559.  
  2560. /* MIPS ELF local labels start with '$', not 'L'.  */
  2561.  
  2562. /*ARGSUSED*/
  2563. static boolean
  2564. mips_elf_is_local_label (abfd, symbol)
  2565.      bfd *abfd;
  2566.      asymbol *symbol;
  2567. {
  2568.   return symbol->name[0] == '$';
  2569. }
  2570.  
  2571. /* MIPS ELF uses a special find_nearest_line routine in order the
  2572.    handle the ECOFF debugging information.  */
  2573.  
  2574. struct mips_elf_find_line
  2575. {
  2576.   struct ecoff_debug_info d;
  2577.   struct ecoff_find_line i;
  2578. };
  2579.  
  2580. boolean
  2581. _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
  2582.                  functionname_ptr, line_ptr)
  2583.      bfd *abfd;
  2584.      asection *section;
  2585.      asymbol **symbols;
  2586.      bfd_vma offset;
  2587.      const char **filename_ptr;
  2588.      const char **functionname_ptr;
  2589.      unsigned int *line_ptr;
  2590. {
  2591.   asection *msec;
  2592.  
  2593.   msec = bfd_get_section_by_name (abfd, ".mdebug");
  2594.   if (msec != NULL)
  2595.     {
  2596.       flagword origflags;
  2597.       struct mips_elf_find_line *fi;
  2598.       const struct ecoff_debug_swap * const swap =
  2599.     get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  2600.  
  2601.       /* If we are called during a link, mips_elf_final_link may have
  2602.      cleared the SEC_HAS_CONTENTS field.  We force it back on here
  2603.      if appropriate (which it normally will be).  */
  2604.       origflags = msec->flags;
  2605.       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
  2606.     msec->flags |= SEC_HAS_CONTENTS;
  2607.  
  2608.       fi = elf_tdata (abfd)->find_line_info;
  2609.       if (fi == NULL)
  2610.     {
  2611.       bfd_size_type external_fdr_size;
  2612.       char *fraw_src;
  2613.       char *fraw_end;
  2614.       struct fdr *fdr_ptr;
  2615.  
  2616.       fi = ((struct mips_elf_find_line *)
  2617.         bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
  2618.       if (fi == NULL)
  2619.         {
  2620.           msec->flags = origflags;
  2621.           return false;
  2622.         }
  2623.  
  2624.       if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
  2625.         {
  2626.           msec->flags = origflags;
  2627.           return false;
  2628.         }
  2629.  
  2630.       /* Swap in the FDR information.  */
  2631.       fi->d.fdr = ((struct fdr *)
  2632.                bfd_alloc (abfd,
  2633.                   (fi->d.symbolic_header.ifdMax *
  2634.                    sizeof (struct fdr))));
  2635.       if (fi->d.fdr == NULL)
  2636.         {
  2637.           msec->flags = origflags;
  2638.           return false;
  2639.         }
  2640.       external_fdr_size = swap->external_fdr_size;
  2641.       fdr_ptr = fi->d.fdr;
  2642.       fraw_src = (char *) fi->d.external_fdr;
  2643.       fraw_end = (fraw_src
  2644.               + fi->d.symbolic_header.ifdMax * external_fdr_size);
  2645.       for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
  2646.         (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
  2647.  
  2648.       elf_tdata (abfd)->find_line_info = fi;
  2649.  
  2650.       /* Note that we don't bother to ever free this information.
  2651.              find_nearest_line is either called all the time, as in
  2652.              objdump -l, so the information should be saved, or it is
  2653.              rarely called, as in ld error messages, so the memory
  2654.              wasted is unimportant.  Still, it would probably be a
  2655.              good idea for free_cached_info to throw it away.  */
  2656.     }
  2657.  
  2658.       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
  2659.                   &fi->i, filename_ptr, functionname_ptr,
  2660.                   line_ptr))
  2661.     {
  2662.       msec->flags = origflags;
  2663.       return true;
  2664.     }
  2665.  
  2666.       msec->flags = origflags;
  2667.     }
  2668.  
  2669.   /* Fall back on the generic ELF find_nearest_line routine.  */
  2670.  
  2671.   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
  2672.                      filename_ptr, functionname_ptr,
  2673.                      line_ptr);
  2674. }
  2675.  
  2676. /* The MIPS ELF linker needs additional information for each symbol in
  2677.    the global hash table.  */
  2678.  
  2679. struct mips_elf_link_hash_entry
  2680. {
  2681.   struct elf_link_hash_entry root;
  2682.  
  2683.   /* External symbol information.  */
  2684.   EXTR esym;
  2685.  
  2686.   /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol.  */
  2687.   unsigned int mips_32_relocs;
  2688. };
  2689.  
  2690. /* MIPS ELF linker hash table.  */
  2691.  
  2692. struct mips_elf_link_hash_table
  2693. {
  2694.   struct elf_link_hash_table root;
  2695.   /* String section indices for the dynamic section symbols.  */
  2696.   bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
  2697.   /* The number of .rtproc entries.  */
  2698.   bfd_size_type procedure_count;
  2699.   /* The size of the .compact_rel section (if SGI_COMPAT).  */
  2700.   bfd_size_type compact_rel_size;
  2701.   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
  2702.      entry is set to the address of __rld_obj_head as in Irix 5. */
  2703.   boolean use_rld_obj_head;
  2704.   /* This is the value of the __rld_map or __rld_obj_head symbol.  */
  2705.   bfd_vma rld_value;
  2706. };
  2707.  
  2708. /* Look up an entry in a MIPS ELF linker hash table.  */
  2709.  
  2710. #define mips_elf_link_hash_lookup(table, string, create, copy, follow)    \
  2711.   ((struct mips_elf_link_hash_entry *)                    \
  2712.    elf_link_hash_lookup (&(table)->root, (string), (create),        \
  2713.              (copy), (follow)))
  2714.  
  2715. /* Traverse a MIPS ELF linker hash table.  */
  2716.  
  2717. #define mips_elf_link_hash_traverse(table, func, info)            \
  2718.   (elf_link_hash_traverse                        \
  2719.    (&(table)->root,                            \
  2720.     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),    \
  2721.     (info)))
  2722.  
  2723. /* Get the MIPS ELF linker hash table from a link_info structure.  */
  2724.  
  2725. #define mips_elf_hash_table(p) \
  2726.   ((struct mips_elf_link_hash_table *) ((p)->hash))
  2727.  
  2728. static boolean mips_elf_output_extsym
  2729.   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
  2730.  
  2731. /* Create an entry in a MIPS ELF linker hash table.  */
  2732.  
  2733. static struct bfd_hash_entry *
  2734. mips_elf_link_hash_newfunc (entry, table, string)
  2735.      struct bfd_hash_entry *entry;
  2736.      struct bfd_hash_table *table;
  2737.      const char *string;
  2738. {
  2739.   struct mips_elf_link_hash_entry *ret =
  2740.     (struct mips_elf_link_hash_entry *) entry;
  2741.  
  2742.   /* Allocate the structure if it has not already been allocated by a
  2743.      subclass.  */
  2744.   if (ret == (struct mips_elf_link_hash_entry *) NULL)
  2745.     ret = ((struct mips_elf_link_hash_entry *)
  2746.        bfd_hash_allocate (table,
  2747.                   sizeof (struct mips_elf_link_hash_entry)));
  2748.   if (ret == (struct mips_elf_link_hash_entry *) NULL)
  2749.     return (struct bfd_hash_entry *) ret;
  2750.  
  2751.   /* Call the allocation method of the superclass.  */
  2752.   ret = ((struct mips_elf_link_hash_entry *)
  2753.      _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  2754.                      table, string));
  2755.   if (ret != (struct mips_elf_link_hash_entry *) NULL)
  2756.     {
  2757.       /* Set local fields.  */
  2758.       memset (&ret->esym, 0, sizeof (EXTR));
  2759.       /* We use -2 as a marker to indicate that the information has
  2760.      not been set.  -1 means there is no associated ifd.  */
  2761.       ret->esym.ifd = -2;
  2762.       ret->mips_32_relocs = 0;
  2763.     }
  2764.  
  2765.   return (struct bfd_hash_entry *) ret;
  2766. }
  2767.  
  2768. /* Create a MIPS ELF linker hash table.  */
  2769.  
  2770. static struct bfd_link_hash_table *
  2771. mips_elf_link_hash_table_create (abfd)
  2772.      bfd *abfd;
  2773. {
  2774.   struct mips_elf_link_hash_table *ret;
  2775.   unsigned int i;
  2776.  
  2777.   ret = ((struct mips_elf_link_hash_table *)
  2778.      bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
  2779.   if (ret == (struct mips_elf_link_hash_table *) NULL)
  2780.     return NULL;
  2781.  
  2782.   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
  2783.                        mips_elf_link_hash_newfunc))
  2784.     {
  2785.       bfd_release (abfd, ret);
  2786.       return NULL;
  2787.     }
  2788.  
  2789.   for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
  2790.     ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
  2791.   ret->procedure_count = 0;
  2792.   ret->compact_rel_size = 0;
  2793.   ret->use_rld_obj_head = false;
  2794.   ret->rld_value = 0;
  2795.  
  2796.   return &ret->root.root;
  2797. }
  2798.  
  2799. /* Hook called by the linker routine which adds symbols from an object
  2800.    file.  We must handle the special MIPS section numbers here.  */
  2801.  
  2802. /*ARGSUSED*/
  2803. static boolean
  2804. mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
  2805.      bfd *abfd;
  2806.      struct bfd_link_info *info;
  2807.      const Elf_Internal_Sym *sym;
  2808.      const char **namep;
  2809.      flagword *flagsp;
  2810.      asection **secp;
  2811.      bfd_vma *valp;
  2812. {
  2813.   if (SGI_COMPAT (abfd)
  2814.       && (abfd->flags & DYNAMIC) != 0
  2815.       && strcmp (*namep, "_rld_new_interface") == 0)
  2816.     {
  2817.       /* Skip Irix 5 rld entry name.  */
  2818.       *namep = NULL;
  2819.       return true;
  2820.     }
  2821.  
  2822.   switch (sym->st_shndx)
  2823.     {
  2824.     case SHN_COMMON:
  2825.       /* Common symbols less than the GP size are automatically
  2826.      treated as SHN_MIPS_SCOMMON symbols.  */
  2827.       if (sym->st_size > elf_gp_size (abfd))
  2828.     break;
  2829.       /* Fall through.  */
  2830.     case SHN_MIPS_SCOMMON:
  2831.       *secp = bfd_make_section_old_way (abfd, ".scommon");
  2832.       (*secp)->flags |= SEC_IS_COMMON;
  2833.       *valp = sym->st_size;
  2834.       break;
  2835.  
  2836.     case SHN_MIPS_TEXT:
  2837.       /* This section is used in a shared object.  */
  2838.       if (mips_elf_text_section_ptr == NULL)
  2839.     {
  2840.       /* Initialize the section.  */
  2841.       mips_elf_text_section.name = ".text";
  2842.       mips_elf_text_section.flags = SEC_NO_FLAGS;
  2843.       mips_elf_text_section.output_section = NULL;
  2844.       mips_elf_text_section.owner = abfd;
  2845.       mips_elf_text_section.symbol = &mips_elf_text_symbol;
  2846.       mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
  2847.       mips_elf_text_symbol.name = ".text";
  2848.       mips_elf_text_symbol.flags = BSF_SECTION_SYM;
  2849.       mips_elf_text_symbol.section = &mips_elf_text_section;
  2850.       mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
  2851.       mips_elf_text_section_ptr = &mips_elf_text_section;
  2852.     }
  2853.       if (info->shared)
  2854.     *secp = bfd_und_section_ptr;
  2855.       else
  2856.     *secp = mips_elf_text_section_ptr;
  2857.       break;
  2858.  
  2859.     case SHN_MIPS_ACOMMON:
  2860.       /* Fall through. XXX Can we treat this as allocated data?  */
  2861.     case SHN_MIPS_DATA:
  2862.       /* This section is used in a shared object.  */
  2863.       if (mips_elf_data_section_ptr == NULL)
  2864.     {
  2865.       /* Initialize the section.  */
  2866.       mips_elf_data_section.name = ".data";
  2867.       mips_elf_data_section.flags = SEC_NO_FLAGS;
  2868.       mips_elf_data_section.output_section = NULL;
  2869.       mips_elf_data_section.owner = abfd;
  2870.       mips_elf_data_section.symbol = &mips_elf_data_symbol;
  2871.       mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
  2872.       mips_elf_data_symbol.name = ".data";
  2873.       mips_elf_data_symbol.flags = BSF_SECTION_SYM;
  2874.       mips_elf_data_symbol.section = &mips_elf_data_section;
  2875.       mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
  2876.       mips_elf_data_section_ptr = &mips_elf_data_section;
  2877.     }
  2878.       if (info->shared)
  2879.     *secp = bfd_und_section_ptr;
  2880.       else
  2881.     *secp = mips_elf_data_section_ptr;
  2882.       break;
  2883.  
  2884.     case SHN_MIPS_SUNDEFINED:
  2885.       *secp = bfd_und_section_ptr;
  2886.       break;
  2887.     }
  2888.  
  2889.   if (SGI_COMPAT (abfd)
  2890.       && ! info->shared
  2891.       && info->hash->creator == abfd->xvec
  2892.       && strcmp (*namep, "__rld_obj_head") == 0)
  2893.     {
  2894.       struct elf_link_hash_entry *h;
  2895.  
  2896.       /* Mark __rld_obj_head as dynamic.  */
  2897.       h = NULL;
  2898.       if (! (_bfd_generic_link_add_one_symbol
  2899.          (info, abfd, *namep, BSF_GLOBAL, *secp,
  2900.           (bfd_vma) *valp, (const char *) NULL, false,
  2901.           get_elf_backend_data (abfd)->collect,
  2902.           (struct bfd_link_hash_entry **) &h)))
  2903.     return false;
  2904.       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
  2905.       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  2906.       h->type = STT_OBJECT;
  2907.  
  2908.       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  2909.     return false;
  2910.  
  2911.       mips_elf_hash_table (info)->use_rld_obj_head = true;
  2912.     }
  2913.  
  2914.   return true;
  2915. }
  2916.  
  2917. /* Structure used to pass information to mips_elf_output_extsym.  */
  2918.  
  2919. struct extsym_info
  2920. {
  2921.   bfd *abfd;
  2922.   struct bfd_link_info *info;
  2923.   struct ecoff_debug_info *debug;
  2924.   const struct ecoff_debug_swap *swap;
  2925.   boolean failed;
  2926. };
  2927.  
  2928. /* This routine is used to write out ECOFF debugging external symbol
  2929.    information.  It is called via mips_elf_link_hash_traverse.  The
  2930.    ECOFF external symbol information must match the ELF external
  2931.    symbol information.  Unfortunately, at this point we don't know
  2932.    whether a symbol is required by reloc information, so the two
  2933.    tables may wind up being different.  We must sort out the external
  2934.    symbol information before we can set the final size of the .mdebug
  2935.    section, and we must set the size of the .mdebug section before we
  2936.    can relocate any sections, and we can't know which symbols are
  2937.    required by relocation until we relocate the sections.
  2938.    Fortunately, it is relatively unlikely that any symbol will be
  2939.    stripped but required by a reloc.  In particular, it can not happen
  2940.    when generating a final executable.  */
  2941.  
  2942. static boolean
  2943. mips_elf_output_extsym (h, data)
  2944.      struct mips_elf_link_hash_entry *h;
  2945.      PTR data;
  2946. {
  2947.   struct extsym_info *einfo = (struct extsym_info *) data;
  2948.   boolean strip;
  2949.   asection *sec, *output_section;
  2950.  
  2951.   if (h->root.indx == -2)
  2952.     strip = false;
  2953.   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  2954.         || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
  2955.        && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
  2956.        && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
  2957.     strip = true;
  2958.   else if (einfo->info->strip == strip_all
  2959.        || (einfo->info->strip == strip_some
  2960.            && bfd_hash_lookup (einfo->info->keep_hash,
  2961.                    h->root.root.root.string,
  2962.                    false, false) == NULL))
  2963.     strip = true;
  2964.   else
  2965.     strip = false;
  2966.  
  2967.   if (strip)
  2968.     return true;
  2969.  
  2970.   if (h->esym.ifd == -2)
  2971.     {
  2972.       h->esym.jmptbl = 0;
  2973.       h->esym.cobol_main = 0;
  2974.       h->esym.weakext = 0;
  2975.       h->esym.reserved = 0;
  2976.       h->esym.ifd = ifdNil;
  2977.       h->esym.asym.value = 0;
  2978.       h->esym.asym.st = stGlobal;
  2979.  
  2980.       if (SGI_COMPAT (einfo->abfd)
  2981.       && (h->root.root.type == bfd_link_hash_undefined
  2982.           || h->root.root.type == bfd_link_hash_undefweak))
  2983.     {
  2984.       const char *name;
  2985.  
  2986.       /* Use undefined class.  Also, set class and type for some
  2987.              special symbols.  */
  2988.       name = h->root.root.root.string;
  2989.       if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
  2990.           || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
  2991.         {
  2992.           h->esym.asym.sc = scData;
  2993.           h->esym.asym.st = stLabel;
  2994.           h->esym.asym.value = 0;
  2995.         }
  2996.       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
  2997.         {
  2998.           h->esym.asym.sc = scAbs;
  2999.           h->esym.asym.st = stLabel;
  3000.           h->esym.asym.value =
  3001.         mips_elf_hash_table (einfo->info)->procedure_count;
  3002.         }
  3003.       else if (strcmp (name, "_gp_disp") == 0)
  3004.         {
  3005.           h->esym.asym.sc = scAbs;
  3006.           h->esym.asym.st = stLabel;
  3007.           h->esym.asym.value = elf_gp (einfo->abfd);
  3008.         }
  3009.       else
  3010.         h->esym.asym.sc = scUndefined;
  3011.     }
  3012.       else if (h->root.root.type != bfd_link_hash_defined
  3013.       && h->root.root.type != bfd_link_hash_defweak)
  3014.     h->esym.asym.sc = scAbs;
  3015.       else
  3016.     {
  3017.       const char *name;
  3018.  
  3019.       sec = h->root.root.u.def.section;
  3020.       output_section = sec->output_section;
  3021.  
  3022.       /* When making a shared library and symbol h is the one from
  3023.          the another shared library, OUTPUT_SECTION may be null.  */
  3024.       if (output_section == NULL)
  3025.         h->esym.asym.sc = scUndefined;
  3026.       else
  3027.         {
  3028.           name = bfd_section_name (output_section->owner, output_section);
  3029.     
  3030.           if (strcmp (name, ".text") == 0)
  3031.         h->esym.asym.sc = scText;
  3032.           else if (strcmp (name, ".data") == 0)
  3033.         h->esym.asym.sc = scData;
  3034.           else if (strcmp (name, ".sdata") == 0)
  3035.         h->esym.asym.sc = scSData;
  3036.           else if (strcmp (name, ".rodata") == 0
  3037.                || strcmp (name, ".rdata") == 0)
  3038.         h->esym.asym.sc = scRData;
  3039.           else if (strcmp (name, ".bss") == 0)
  3040.         h->esym.asym.sc = scBss;
  3041.           else if (strcmp (name, ".sbss") == 0)
  3042.         h->esym.asym.sc = scSBss;
  3043.           else if (strcmp (name, ".init") == 0)
  3044.         h->esym.asym.sc = scInit;
  3045.           else if (strcmp (name, ".fini") == 0)
  3046.         h->esym.asym.sc = scFini;
  3047.           else
  3048.         h->esym.asym.sc = scAbs;
  3049.         }
  3050.     }
  3051.  
  3052.       h->esym.asym.reserved = 0;
  3053.       h->esym.asym.index = indexNil;
  3054.     }
  3055.  
  3056.   if (h->root.root.type == bfd_link_hash_common)
  3057.     h->esym.asym.value = h->root.root.u.c.size;
  3058.   else if (h->root.root.type == bfd_link_hash_defined
  3059.        || h->root.root.type == bfd_link_hash_defweak)
  3060.     {
  3061.       if (h->esym.asym.sc == scCommon)
  3062.     h->esym.asym.sc = scBss;
  3063.       else if (h->esym.asym.sc == scSCommon)
  3064.     h->esym.asym.sc = scSBss;
  3065.  
  3066.       sec = h->root.root.u.def.section;
  3067.       output_section = sec->output_section;
  3068.       if (output_section != NULL)
  3069.     h->esym.asym.value = (h->root.root.u.def.value
  3070.                   + sec->output_offset
  3071.                   + output_section->vma);
  3072.       else
  3073.     h->esym.asym.value = 0;
  3074.     }
  3075.   else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
  3076.     {
  3077.       /* Set type and value for a symbol with a function stub.  */
  3078.       h->esym.asym.st = stProc;
  3079.       sec = h->root.root.u.def.section;
  3080.       if (sec == NULL)
  3081.     h->esym.asym.value = 0;
  3082.       else
  3083.     {
  3084.       output_section = sec->output_section;
  3085.       if (output_section != NULL)
  3086.         h->esym.asym.value = (h->root.plt_offset
  3087.                   + sec->output_offset
  3088.                   + output_section->vma);
  3089.       else
  3090.         h->esym.asym.value = 0;
  3091.     }
  3092. #if 0 /* FIXME?  */
  3093.       h->esym.ifd = 0;
  3094. #endif
  3095.     }      
  3096.  
  3097.   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
  3098.                       h->root.root.root.string,
  3099.                       &h->esym))
  3100.     {
  3101.       einfo->failed = true;
  3102.       return false;
  3103.     }
  3104.  
  3105.   return true;
  3106. }
  3107.  
  3108. /* Create a runtime procedure table from the .mdebug section.  */
  3109.  
  3110. static boolean
  3111. mips_elf_create_procedure_table (handle, abfd, info, s, debug)
  3112.      PTR handle;
  3113.      bfd *abfd;
  3114.      struct bfd_link_info *info;
  3115.      asection *s;
  3116.      struct ecoff_debug_info *debug;
  3117. {
  3118.   const struct ecoff_debug_swap *swap;
  3119.   HDRR *hdr = &debug->symbolic_header;
  3120.   RPDR *rpdr, *rp;
  3121.   struct rpdr_ext *erp;
  3122.   PTR rtproc;
  3123.   struct pdr_ext *epdr;
  3124.   struct sym_ext *esym;
  3125.   char *ss, **sv;
  3126.   char *str;
  3127.   unsigned long size, count;
  3128.   unsigned long sindex;
  3129.   unsigned long i;
  3130.   PDR pdr;
  3131.   SYMR sym;
  3132.   const char *no_name_func = "static procedure (no name)";
  3133.  
  3134.   epdr = NULL;
  3135.   rpdr = NULL;
  3136.   esym = NULL;
  3137.   ss = NULL;
  3138.   sv = NULL;
  3139.  
  3140.   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  3141.  
  3142.   sindex = strlen (no_name_func) + 1;
  3143.   count = hdr->ipdMax;
  3144.   if (count > 0)
  3145.     {
  3146.       size = swap->external_pdr_size;
  3147.  
  3148.       epdr = (struct pdr_ext *) bfd_malloc (size * count);
  3149.       if (epdr == NULL)
  3150.     goto error_return;
  3151.  
  3152.       if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
  3153.     goto error_return;
  3154.  
  3155.       size = sizeof (RPDR);
  3156.       rp = rpdr = (RPDR *) bfd_malloc (size * count);
  3157.       if (rpdr == NULL)
  3158.     goto error_return;
  3159.  
  3160.       sv = (char **) bfd_malloc (sizeof (char *) * count);
  3161.       if (sv == NULL)
  3162.     goto error_return;
  3163.  
  3164.       count = hdr->isymMax;
  3165.       size = swap->external_sym_size;
  3166.       esym = (struct sym_ext *) bfd_malloc (size * count);
  3167.       if (esym == NULL)
  3168.     goto error_return;
  3169.  
  3170.       if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
  3171.     goto error_return;
  3172.  
  3173.       count = hdr->issMax;
  3174.       ss = (char *) bfd_malloc (count);
  3175.       if (ss == NULL)
  3176.     goto error_return;
  3177.       if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
  3178.     goto error_return;
  3179.  
  3180.       count = hdr->ipdMax;
  3181.       for (i = 0; i < count; i++, rp++)
  3182.     {
  3183.       (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
  3184.       (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
  3185.       rp->adr = sym.value;
  3186.       rp->regmask = pdr.regmask;
  3187.       rp->regoffset = pdr.regoffset;
  3188.       rp->fregmask = pdr.fregmask;
  3189.       rp->fregoffset = pdr.fregoffset;
  3190.       rp->frameoffset = pdr.frameoffset;
  3191.       rp->framereg = pdr.framereg;
  3192.       rp->pcreg = pdr.pcreg;
  3193.       rp->irpss = sindex;
  3194.       sv[i] = ss + sym.iss;
  3195.       sindex += strlen (sv[i]) + 1;
  3196.     }
  3197.     }
  3198.  
  3199.   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
  3200.   size = BFD_ALIGN (size, 16);
  3201.   rtproc = (PTR) bfd_alloc (abfd, size);
  3202.   if (rtproc == NULL)
  3203.     {
  3204.       mips_elf_hash_table (info)->procedure_count = 0;
  3205.       goto error_return;
  3206.     }
  3207.  
  3208.   mips_elf_hash_table (info)->procedure_count = count + 2;
  3209.  
  3210.   erp = (struct rpdr_ext *) rtproc;
  3211.   memset (erp, 0, sizeof (struct rpdr_ext));
  3212.   erp++;
  3213.   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
  3214.   strcpy (str, no_name_func);
  3215.   str += strlen (no_name_func) + 1;
  3216.   for (i = 0; i < count; i++)
  3217.     {
  3218.       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
  3219.       strcpy (str, sv[i]);
  3220.       str += strlen (sv[i]) + 1;
  3221.     }
  3222.   ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
  3223.  
  3224.   /* Set the size and contents of .rtproc section.  */
  3225.   s->_raw_size = size;
  3226.   s->contents = rtproc;
  3227.  
  3228.   /* Skip this section later on (I don't think this currently
  3229.      matters, but someday it might).  */
  3230.   s->link_order_head = (struct bfd_link_order *) NULL;
  3231.  
  3232.   if (epdr != NULL)
  3233.     free (epdr);
  3234.   if (rpdr != NULL)
  3235.     free (rpdr);
  3236.   if (esym != NULL)
  3237.     free (esym);
  3238.   if (ss != NULL)
  3239.     free (ss);
  3240.   if (sv != NULL)
  3241.     free (sv);
  3242.  
  3243.   return true;
  3244.  
  3245.  error_return:
  3246.   if (epdr != NULL)
  3247.     free (epdr);
  3248.   if (rpdr != NULL)
  3249.     free (rpdr);
  3250.   if (esym != NULL)
  3251.     free (esym);
  3252.   if (ss != NULL)
  3253.     free (ss);
  3254.   if (sv != NULL)
  3255.     free (sv);
  3256.   return false;
  3257. }
  3258.  
  3259. /* A comparison routine used to sort .gptab entries.  */
  3260.  
  3261. static int
  3262. gptab_compare (p1, p2)
  3263.      const PTR p1;
  3264.      const PTR p2;
  3265. {
  3266.   const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
  3267.   const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
  3268.  
  3269.   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
  3270. }
  3271.  
  3272. /* We need to use a special link routine to handle the .reginfo and
  3273.    the .mdebug sections.  We need to merge all instances of these
  3274.    sections together, not write them all out sequentially.  */
  3275.  
  3276. static boolean
  3277. mips_elf_final_link (abfd, info)
  3278.      bfd *abfd;
  3279.      struct bfd_link_info *info;
  3280. {
  3281.   asection **secpp;
  3282.   asection *o;
  3283.   struct bfd_link_order *p;
  3284.   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
  3285.   asection *rtproc_sec;
  3286.   Elf32_RegInfo reginfo;
  3287.   struct ecoff_debug_info debug;
  3288.   const struct ecoff_debug_swap *swap
  3289.     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  3290.   HDRR *symhdr = &debug.symbolic_header;
  3291.   PTR mdebug_handle = NULL;
  3292.  
  3293.   /* Drop the .options section, since it has special semantics which I
  3294.      haven't bothered to figure out.  */
  3295.   for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
  3296.     {
  3297.       if (strcmp ((*secpp)->name, ".options") == 0)
  3298.     {
  3299.       for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
  3300.         if (p->type == bfd_indirect_link_order)
  3301.           p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
  3302.       (*secpp)->link_order_head = NULL;
  3303.       *secpp = (*secpp)->next;
  3304.       --abfd->section_count;
  3305.       break;
  3306.     }
  3307.     }
  3308.  
  3309.   /* Get a value for the GP register.  */
  3310.   if (elf_gp (abfd) == 0)
  3311.     {
  3312.       struct bfd_link_hash_entry *h;
  3313.  
  3314.       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
  3315.       if (h != (struct bfd_link_hash_entry *) NULL
  3316.       && h->type == bfd_link_hash_defined)
  3317.     elf_gp (abfd) = (h->u.def.value
  3318.              + h->u.def.section->output_section->vma
  3319.              + h->u.def.section->output_offset);
  3320.       else if (info->relocateable)
  3321.     {
  3322.       bfd_vma lo;
  3323.  
  3324.       /* Make up a value.  */
  3325.       lo = (bfd_vma) -1;
  3326.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  3327.         {
  3328.           if (o->vma < lo
  3329.           && (strcmp (o->name, ".sbss") == 0
  3330.               || strcmp (o->name, ".sdata") == 0
  3331.               || strcmp (o->name, ".lit4") == 0
  3332.               || strcmp (o->name, ".lit8") == 0))
  3333.         lo = o->vma;
  3334.         }
  3335.       elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
  3336.     }
  3337.       else
  3338.     {
  3339.       /* If the relocate_section function needs to do a reloc
  3340.          involving the GP value, it should make a reloc_dangerous
  3341.          callback to warn that GP is not defined.  */
  3342.     }
  3343.     }
  3344.  
  3345.   /* Go through the sections and collect the .reginfo and .mdebug
  3346.      information.  */
  3347.   reginfo_sec = NULL;
  3348.   mdebug_sec = NULL;
  3349.   gptab_data_sec = NULL;
  3350.   gptab_bss_sec = NULL;
  3351.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  3352.     {
  3353.       if (strcmp (o->name, ".reginfo") == 0)
  3354.     {
  3355.       memset (®info, 0, sizeof reginfo);
  3356.  
  3357.       /* We have found the .reginfo section in the output file.
  3358.          Look through all the link_orders comprising it and merge
  3359.          the information together.  */
  3360.       for (p = o->link_order_head;
  3361.            p != (struct bfd_link_order *) NULL;
  3362.            p = p->next)
  3363.         {
  3364.           asection *input_section;
  3365.           bfd *input_bfd;
  3366.           Elf32_External_RegInfo ext;
  3367.           Elf32_RegInfo sub;
  3368.  
  3369.           if (p->type != bfd_indirect_link_order)
  3370.         {
  3371.           if (p->type == bfd_fill_link_order)
  3372.             continue;
  3373.           abort ();
  3374.         }
  3375.  
  3376.           input_section = p->u.indirect.section;
  3377.           input_bfd = input_section->owner;
  3378.  
  3379.           /* The linker emulation code has probably clobbered the
  3380.                  size to be zero bytes.  */
  3381.           if (input_section->_raw_size == 0)
  3382.         input_section->_raw_size = sizeof (Elf32_External_RegInfo);
  3383.  
  3384.           if (! bfd_get_section_contents (input_bfd, input_section,
  3385.                           (PTR) &ext,
  3386.                           (file_ptr) 0,
  3387.                           sizeof ext))
  3388.         return false;
  3389.  
  3390.           bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
  3391.  
  3392.           reginfo.ri_gprmask |= sub.ri_gprmask;
  3393.           reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
  3394.           reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
  3395.           reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
  3396.           reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
  3397.  
  3398.           /* ri_gp_value is set by the function
  3399.          mips_elf32_section_processing when the section is
  3400.          finally written out.  */
  3401.  
  3402.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  3403.          elf_link_input_bfd ignores this section.  */
  3404.           input_section->flags &=~ SEC_HAS_CONTENTS;
  3405.         }
  3406.  
  3407.       /* Force the section size to the value we want.  */
  3408.       o->_raw_size = sizeof (Elf32_External_RegInfo);
  3409.  
  3410.       /* Skip this section later on (I don't think this currently
  3411.          matters, but someday it might).  */
  3412.       o->link_order_head = (struct bfd_link_order *) NULL;
  3413.  
  3414.       reginfo_sec = o;
  3415.     }
  3416.  
  3417.       if (strcmp (o->name, ".mdebug") == 0)
  3418.     {
  3419.       struct extsym_info einfo;
  3420.  
  3421.       /* We have found the .mdebug section in the output file.
  3422.          Look through all the link_orders comprising it and merge
  3423.          the information together.  */
  3424.       symhdr->magic = swap->sym_magic;
  3425.       /* FIXME: What should the version stamp be?  */
  3426.       symhdr->vstamp = 0;
  3427.       symhdr->ilineMax = 0;
  3428.       symhdr->cbLine = 0;
  3429.       symhdr->idnMax = 0;
  3430.       symhdr->ipdMax = 0;
  3431.       symhdr->isymMax = 0;
  3432.       symhdr->ioptMax = 0;
  3433.       symhdr->iauxMax = 0;
  3434.       symhdr->issMax = 0;
  3435.       symhdr->issExtMax = 0;
  3436.       symhdr->ifdMax = 0;
  3437.       symhdr->crfd = 0;
  3438.       symhdr->iextMax = 0;
  3439.  
  3440.       /* We accumulate the debugging information itself in the
  3441.          debug_info structure.  */
  3442.       debug.line = NULL;
  3443.       debug.external_dnr = NULL;
  3444.       debug.external_pdr = NULL;
  3445.       debug.external_sym = NULL;
  3446.       debug.external_opt = NULL;
  3447.       debug.external_aux = NULL;
  3448.       debug.ss = NULL;
  3449.       debug.ssext = debug.ssext_end = NULL;
  3450.       debug.external_fdr = NULL;
  3451.       debug.external_rfd = NULL;
  3452.       debug.external_ext = debug.external_ext_end = NULL;
  3453.  
  3454.       mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
  3455.       if (mdebug_handle == (PTR) NULL)
  3456.         return false;
  3457.  
  3458.       if (SGI_COMPAT (abfd))
  3459.         {
  3460.           asection *s;
  3461.           EXTR esym;
  3462.           bfd_vma last;
  3463.           unsigned int i;
  3464.           static const char * const name[] =
  3465.         { ".text", ".init", ".fini", ".data",
  3466.             ".rodata", ".sdata", ".sbss", ".bss" };
  3467.           static const int sc[] = { scText, scInit, scFini, scData,
  3468.                       scRData, scSData, scSBss, scBss };
  3469.  
  3470.           esym.jmptbl = 0;
  3471.           esym.cobol_main = 0;
  3472.           esym.weakext = 0;
  3473.           esym.reserved = 0;
  3474.           esym.ifd = ifdNil;
  3475.           esym.asym.iss = issNil;
  3476.           esym.asym.st = stLocal;
  3477.           esym.asym.reserved = 0;
  3478.           esym.asym.index = indexNil;
  3479.           for (i = 0; i < 8; i++)
  3480.         {
  3481.           esym.asym.sc = sc[i];
  3482.           s = bfd_get_section_by_name (abfd, name[i]);
  3483.           if (s != NULL)
  3484.             {
  3485.               esym.asym.value = s->vma;
  3486.               last = s->vma + s->_raw_size;
  3487.             }
  3488.           else
  3489.             esym.asym.value = last;
  3490.         
  3491.           if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
  3492.                               name[i], &esym))
  3493.             return false;
  3494.         }
  3495.         }
  3496.  
  3497.       for (p = o->link_order_head;
  3498.            p != (struct bfd_link_order *) NULL;
  3499.            p = p->next)
  3500.         {
  3501.           asection *input_section;
  3502.           bfd *input_bfd;
  3503.           const struct ecoff_debug_swap *input_swap;
  3504.           struct ecoff_debug_info input_debug;
  3505.           char *eraw_src;
  3506.           char *eraw_end;
  3507.  
  3508.           if (p->type != bfd_indirect_link_order)
  3509.         {
  3510.           if (p->type == bfd_fill_link_order)
  3511.             continue;
  3512.           abort ();
  3513.         }
  3514.  
  3515.           input_section = p->u.indirect.section;
  3516.           input_bfd = input_section->owner;
  3517.  
  3518.           if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
  3519.           || (get_elf_backend_data (input_bfd)
  3520.               ->elf_backend_ecoff_debug_swap) == NULL)
  3521.         {
  3522.           /* I don't know what a non MIPS ELF bfd would be
  3523.              doing with a .mdebug section, but I don't really
  3524.              want to deal with it.  */
  3525.           continue;
  3526.         }
  3527.  
  3528.           input_swap = (get_elf_backend_data (input_bfd)
  3529.                 ->elf_backend_ecoff_debug_swap);
  3530.  
  3531.           BFD_ASSERT (p->size == input_section->_raw_size);
  3532.  
  3533.           /* The ECOFF linking code expects that we have already
  3534.          read in the debugging information and set up an
  3535.          ecoff_debug_info structure, so we do that now.  */
  3536.           if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
  3537.                            &input_debug))
  3538.         return false;
  3539.  
  3540.           if (! (bfd_ecoff_debug_accumulate
  3541.              (mdebug_handle, abfd, &debug, swap, input_bfd,
  3542.               &input_debug, input_swap, info)))
  3543.         return false;
  3544.  
  3545.           /* Loop through the external symbols.  For each one with
  3546.          interesting information, try to find the symbol in
  3547.          the linker global hash table and save the information
  3548.          for the output external symbols.  */
  3549.           eraw_src = input_debug.external_ext;
  3550.           eraw_end = (eraw_src
  3551.               + (input_debug.symbolic_header.iextMax
  3552.                  * input_swap->external_ext_size));
  3553.           for (;
  3554.            eraw_src < eraw_end;
  3555.            eraw_src += input_swap->external_ext_size)
  3556.         {
  3557.           EXTR ext;
  3558.           const char *name;
  3559.           struct mips_elf_link_hash_entry *h;
  3560.  
  3561.           (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
  3562.           if (ext.asym.sc == scNil
  3563.               || ext.asym.sc == scUndefined
  3564.               || ext.asym.sc == scSUndefined)
  3565.             continue;
  3566.  
  3567.           name = input_debug.ssext + ext.asym.iss;
  3568.           h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
  3569.                          name, false, false, true);
  3570.           if (h == NULL || h->esym.ifd != -2)
  3571.             continue;
  3572.  
  3573.           if (ext.ifd != -1)
  3574.             {
  3575.               BFD_ASSERT (ext.ifd
  3576.                   < input_debug.symbolic_header.ifdMax);
  3577.               ext.ifd = input_debug.ifdmap[ext.ifd];
  3578.             }
  3579.  
  3580.           h->esym = ext;
  3581.         }
  3582.  
  3583.           /* Free up the information we just read.  */
  3584.           free (input_debug.line);
  3585.           free (input_debug.external_dnr);
  3586.           free (input_debug.external_pdr);
  3587.           free (input_debug.external_sym);
  3588.           free (input_debug.external_opt);
  3589.           free (input_debug.external_aux);
  3590.           free (input_debug.ss);
  3591.           free (input_debug.ssext);
  3592.           free (input_debug.external_fdr);
  3593.           free (input_debug.external_rfd);
  3594.           free (input_debug.external_ext);
  3595.  
  3596.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  3597.          elf_link_input_bfd ignores this section.  */
  3598.           input_section->flags &=~ SEC_HAS_CONTENTS;
  3599.         }
  3600.  
  3601.       if (SGI_COMPAT (abfd) && info->shared)
  3602.         {
  3603.           /* Create .rtproc section.  */
  3604.           rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
  3605.           if (rtproc_sec == NULL)
  3606.         {
  3607.           flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
  3608.                     | SEC_READONLY);
  3609.  
  3610.           rtproc_sec = bfd_make_section (abfd, ".rtproc");
  3611.           if (rtproc_sec == NULL
  3612.               || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
  3613.               || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
  3614.             return false;
  3615.         }
  3616.  
  3617.           if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
  3618.                              info, rtproc_sec, &debug))
  3619.         return false;
  3620.         }
  3621.  
  3622.       /* Build the external symbol information.  */
  3623.       einfo.abfd = abfd;
  3624.       einfo.info = info;
  3625.       einfo.debug = &debug;
  3626.       einfo.swap = swap;
  3627.       einfo.failed = false;
  3628.       mips_elf_link_hash_traverse (mips_elf_hash_table (info),
  3629.                        mips_elf_output_extsym,
  3630.                        (PTR) &einfo);
  3631.       if (einfo.failed)
  3632.         return false;
  3633.  
  3634.       /* Set the size of the .mdebug section.  */
  3635.       o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
  3636.  
  3637.       /* Skip this section later on (I don't think this currently
  3638.          matters, but someday it might).  */
  3639.       o->link_order_head = (struct bfd_link_order *) NULL;
  3640.  
  3641.       mdebug_sec = o;
  3642.     }
  3643.  
  3644.       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
  3645.     {
  3646.       const char *subname;
  3647.       unsigned int c;
  3648.       Elf32_gptab *tab;
  3649.       Elf32_External_gptab *ext_tab;
  3650.       unsigned int i;
  3651.  
  3652.       /* The .gptab.sdata and .gptab.sbss sections hold
  3653.          information describing how the small data area would
  3654.          change depending upon the -G switch.  These sections
  3655.          not used in executables files.  */
  3656.       if (! info->relocateable)
  3657.         {
  3658.           asection **secpp;
  3659.  
  3660.           for (p = o->link_order_head;
  3661.            p != (struct bfd_link_order *) NULL;
  3662.            p = p->next)
  3663.         {
  3664.           asection *input_section;
  3665.  
  3666.           if (p->type != bfd_indirect_link_order)
  3667.             {
  3668.               if (p->type == bfd_fill_link_order)
  3669.             continue;
  3670.               abort ();
  3671.             }
  3672.  
  3673.           input_section = p->u.indirect.section;
  3674.  
  3675.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  3676.              elf_link_input_bfd ignores this section.  */
  3677.           input_section->flags &=~ SEC_HAS_CONTENTS;
  3678.         }
  3679.  
  3680.           /* Skip this section later on (I don't think this
  3681.          currently matters, but someday it might).  */
  3682.           o->link_order_head = (struct bfd_link_order *) NULL;
  3683.  
  3684.           /* Really remove the section.  */
  3685.           for (secpp = &abfd->sections;
  3686.            *secpp != o;
  3687.            secpp = &(*secpp)->next)
  3688.         ;
  3689.           *secpp = (*secpp)->next;
  3690.           --abfd->section_count;
  3691.  
  3692.           continue;
  3693.         }
  3694.  
  3695.       /* There is one gptab for initialized data, and one for
  3696.          uninitialized data.  */
  3697.       if (strcmp (o->name, ".gptab.sdata") == 0)
  3698.         gptab_data_sec = o;
  3699.       else if (strcmp (o->name, ".gptab.sbss") == 0)
  3700.         gptab_bss_sec = o;
  3701.       else
  3702.         {
  3703.           (*_bfd_error_handler)
  3704.         ("%s: illegal section name `%s'",
  3705.          bfd_get_filename (abfd), o->name);
  3706.           bfd_set_error (bfd_error_nonrepresentable_section);
  3707.           return false;
  3708.         }
  3709.  
  3710.       /* The linker script always combines .gptab.data and
  3711.          .gptab.sdata into .gptab.sdata, and likewise for
  3712.          .gptab.bss and .gptab.sbss.  It is possible that there is
  3713.          no .sdata or .sbss section in the output file, in which
  3714.          case we must change the name of the output section.  */
  3715.       subname = o->name + sizeof ".gptab" - 1;
  3716.       if (bfd_get_section_by_name (abfd, subname) == NULL)
  3717.         {
  3718.           if (o == gptab_data_sec)
  3719.         o->name = ".gptab.data";
  3720.           else
  3721.         o->name = ".gptab.bss";
  3722.           subname = o->name + sizeof ".gptab" - 1;
  3723.           BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
  3724.         }
  3725.  
  3726.       /* Set up the first entry.  */
  3727.       c = 1;
  3728.       tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
  3729.       if (tab == NULL)
  3730.         return false;
  3731.       tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
  3732.       tab[0].gt_header.gt_unused = 0;
  3733.  
  3734.       /* Combine the input sections.  */
  3735.       for (p = o->link_order_head;
  3736.            p != (struct bfd_link_order *) NULL;
  3737.            p = p->next)
  3738.         {
  3739.           asection *input_section;
  3740.           bfd *input_bfd;
  3741.           bfd_size_type size;
  3742.           unsigned long last;
  3743.           bfd_size_type gpentry;
  3744.  
  3745.           if (p->type != bfd_indirect_link_order)
  3746.         {
  3747.           if (p->type == bfd_fill_link_order)
  3748.             continue;
  3749.           abort ();
  3750.         }
  3751.  
  3752.           input_section = p->u.indirect.section;
  3753.           input_bfd = input_section->owner;
  3754.  
  3755.           /* Combine the gptab entries for this input section one
  3756.          by one.  We know that the input gptab entries are
  3757.          sorted by ascending -G value.  */
  3758.           size = bfd_section_size (input_bfd, input_section);
  3759.           last = 0;
  3760.           for (gpentry = sizeof (Elf32_External_gptab);
  3761.            gpentry < size;
  3762.            gpentry += sizeof (Elf32_External_gptab))
  3763.         {
  3764.           Elf32_External_gptab ext_gptab;
  3765.           Elf32_gptab int_gptab;
  3766.           unsigned long val;
  3767.           unsigned long add;
  3768.           boolean exact;
  3769.           unsigned int look;
  3770.  
  3771.           if (! (bfd_get_section_contents
  3772.              (input_bfd, input_section, (PTR) &ext_gptab,
  3773.               gpentry, sizeof (Elf32_External_gptab))))
  3774.             {
  3775.               free (tab);
  3776.               return false;
  3777.             }
  3778.  
  3779.           bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
  3780.                         &int_gptab);
  3781.           val = int_gptab.gt_entry.gt_g_value;
  3782.           add = int_gptab.gt_entry.gt_bytes - last;
  3783.  
  3784.           exact = false;
  3785.           for (look = 1; look < c; look++)
  3786.             {
  3787.               if (tab[look].gt_entry.gt_g_value >= val)
  3788.             tab[look].gt_entry.gt_bytes += add;
  3789.  
  3790.               if (tab[look].gt_entry.gt_g_value == val)
  3791.             exact = true;
  3792.             }
  3793.  
  3794.           if (! exact)
  3795.             {
  3796.               Elf32_gptab *new_tab;
  3797.               unsigned int max;
  3798.  
  3799.               /* We need a new table entry.  */
  3800.               new_tab = ((Elf32_gptab *)
  3801.                  bfd_realloc ((PTR) tab,
  3802.                           (c + 1) * sizeof (Elf32_gptab)));
  3803.               if (new_tab == NULL)
  3804.             {
  3805.               free (tab);
  3806.               return false;
  3807.             }
  3808.               tab = new_tab;
  3809.               tab[c].gt_entry.gt_g_value = val;
  3810.               tab[c].gt_entry.gt_bytes = add;
  3811.  
  3812.               /* Merge in the size for the next smallest -G
  3813.              value, since that will be implied by this new
  3814.              value.  */
  3815.               max = 0;
  3816.               for (look = 1; look < c; look++)
  3817.             {
  3818.               if (tab[look].gt_entry.gt_g_value < val
  3819.                   && (max == 0
  3820.                   || (tab[look].gt_entry.gt_g_value
  3821.                       > tab[max].gt_entry.gt_g_value)))
  3822.                 max = look;
  3823.             }
  3824.               if (max != 0)
  3825.             tab[c].gt_entry.gt_bytes +=
  3826.               tab[max].gt_entry.gt_bytes;
  3827.  
  3828.               ++c;
  3829.             }
  3830.  
  3831.           last = int_gptab.gt_entry.gt_bytes;
  3832.         }
  3833.  
  3834.           /* Hack: reset the SEC_HAS_CONTENTS flag so that
  3835.          elf_link_input_bfd ignores this section.  */
  3836.           input_section->flags &=~ SEC_HAS_CONTENTS;
  3837.         }
  3838.  
  3839.       /* The table must be sorted by -G value.  */
  3840.       if (c > 2)
  3841.         qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
  3842.  
  3843.       /* Swap out the table.  */
  3844.       ext_tab = ((Elf32_External_gptab *)
  3845.              bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
  3846.       if (ext_tab == NULL)
  3847.         {
  3848.           free (tab);
  3849.           return false;
  3850.         }
  3851.  
  3852.       for (i = 0; i < c; i++)
  3853.         bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
  3854.       free (tab);
  3855.  
  3856.       o->_raw_size = c * sizeof (Elf32_External_gptab);
  3857.       o->contents = (bfd_byte *) ext_tab;
  3858.  
  3859.       /* Skip this section later on (I don't think this currently
  3860.          matters, but someday it might).  */
  3861.       o->link_order_head = (struct bfd_link_order *) NULL;
  3862.     }
  3863.     }
  3864.  
  3865.   /* Invoke the regular ELF backend linker to do all the work.  */
  3866.   if (! bfd_elf32_bfd_final_link (abfd, info))
  3867.     return false;
  3868.  
  3869.   /* Now write out the computed sections.  */
  3870.  
  3871.   if (reginfo_sec != (asection *) NULL)
  3872.     {
  3873.       Elf32_External_RegInfo ext;
  3874.  
  3875.       bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
  3876.       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
  3877.                       (file_ptr) 0, sizeof ext))
  3878.     return false;
  3879.     }
  3880.  
  3881.   if (mdebug_sec != (asection *) NULL)
  3882.     {
  3883.       BFD_ASSERT (abfd->output_has_begun);
  3884.       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
  3885.                            swap, info,
  3886.                            mdebug_sec->filepos))
  3887.     return false;
  3888.  
  3889.       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
  3890.     }
  3891.  
  3892.   if (gptab_data_sec != (asection *) NULL)
  3893.     {
  3894.       if (! bfd_set_section_contents (abfd, gptab_data_sec,
  3895.                       gptab_data_sec->contents,
  3896.                       (file_ptr) 0,
  3897.                       gptab_data_sec->_raw_size))
  3898.     return false;
  3899.     }
  3900.  
  3901.   if (gptab_bss_sec != (asection *) NULL)
  3902.     {
  3903.       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
  3904.                       gptab_bss_sec->contents,
  3905.                       (file_ptr) 0,
  3906.                       gptab_bss_sec->_raw_size))
  3907.     return false;
  3908.     }
  3909.  
  3910.   if (SGI_COMPAT (abfd))
  3911.     {
  3912.       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
  3913.       if (rtproc_sec != NULL)
  3914.     {
  3915.       if (! bfd_set_section_contents (abfd, rtproc_sec,
  3916.                       rtproc_sec->contents,
  3917.                       (file_ptr) 0,
  3918.                       rtproc_sec->_raw_size))
  3919.         return false;
  3920.     }
  3921.     }
  3922.  
  3923.   return true;
  3924. }
  3925.  
  3926. /* Handle a MIPS ELF HI16 reloc.  */
  3927.  
  3928. static void
  3929. mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
  3930.      bfd *input_bfd;
  3931.      Elf_Internal_Rela *relhi;
  3932.      Elf_Internal_Rela *rello;
  3933.      bfd_byte *contents;
  3934.      bfd_vma addend;
  3935. {
  3936.   bfd_vma insn;
  3937.   bfd_vma addlo;
  3938.  
  3939.   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
  3940.  
  3941.   addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
  3942.   addlo &= 0xffff;
  3943.  
  3944.   addend += ((insn & 0xffff) << 16) + addlo;
  3945.  
  3946.   if ((addlo & 0x8000) != 0)
  3947.     addend -= 0x10000;
  3948.   if ((addend & 0x8000) != 0)
  3949.     addend += 0x10000;
  3950.  
  3951.   bfd_put_32 (input_bfd,
  3952.           (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
  3953.           contents + relhi->r_offset);
  3954. }
  3955.  
  3956. /* Handle a MIPS ELF local GOT16 reloc.  */
  3957.  
  3958. static void
  3959. mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
  3960.                  contents, addend)
  3961.      bfd *output_bfd;
  3962.      bfd *input_bfd;
  3963.      asection *sgot;
  3964.      Elf_Internal_Rela *relhi;
  3965.      Elf_Internal_Rela *rello;
  3966.      bfd_byte *contents;
  3967.      bfd_vma addend;
  3968. {
  3969.   int local_gotno;
  3970.   int i;
  3971.   bfd_vma insn;
  3972.   bfd_vma addlo;
  3973.   bfd_vma address;
  3974.   bfd_vma hipage;
  3975.   bfd_byte *got_contents;
  3976.   struct mips_got_info *g;
  3977.  
  3978.   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
  3979.  
  3980.   addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
  3981.   addlo &= 0xffff;
  3982.  
  3983.   addend += ((insn & 0xffff) << 16) + addlo;
  3984.  
  3985.   if ((addlo & 0x8000) != 0)
  3986.     addend -= 0x10000;
  3987.   if ((addend & 0x8000) != 0)
  3988.     addend += 0x10000;
  3989.  
  3990.   /* Get a got entry representing requested hipage.  */
  3991.   BFD_ASSERT (elf_section_data (sgot) != NULL);
  3992.   g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
  3993.   BFD_ASSERT (g != NULL);
  3994.  
  3995.   local_gotno = g->local_gotno;
  3996.   got_contents = sgot->contents;
  3997.   hipage = addend & 0xffff0000;
  3998.  
  3999.   for (i = MIPS_RESERVED_GOTNO; i < local_gotno; i++)
  4000.     {
  4001.       address = bfd_get_32 (input_bfd, got_contents + i * 4);
  4002.       if (hipage == (address & 0xffff0000))
  4003.     break;
  4004.       if (address == (bfd_vma) 0)
  4005.     {
  4006.       bfd_put_32 (input_bfd, hipage, got_contents + i * 4);
  4007.       break;
  4008.     }
  4009.     }
  4010.  
  4011.   BFD_ASSERT (i < local_gotno);
  4012. #if 1
  4013.   if (i == local_gotno)
  4014.     (*_bfd_error_handler)
  4015.       ("ELF MIPS linker: more got entries are needed for hipage: %x",
  4016.        hipage);
  4017. #endif
  4018.  
  4019.   i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
  4020.   bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
  4021.           contents + relhi->r_offset);
  4022. }
  4023.  
  4024. /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc.  */
  4025.  
  4026. static void
  4027. mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
  4028.      bfd *input_bfd;
  4029.      Elf_Internal_Rela *rel;
  4030.      bfd_byte *contents;
  4031.      bfd_vma offset;
  4032. {
  4033.   bfd_vma insn;
  4034.  
  4035.   insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  4036.   bfd_put_32 (input_bfd,
  4037.           (insn & 0xffff0000) | (offset & 0xffff),
  4038.           contents + rel->r_offset);
  4039. }
  4040.  
  4041. /* Relocate a MIPS ELF section.  */
  4042.  
  4043. static boolean
  4044. mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
  4045.                contents, relocs, local_syms, local_sections)
  4046.      bfd *output_bfd;
  4047.      struct bfd_link_info *info;
  4048.      bfd *input_bfd;
  4049.      asection *input_section;
  4050.      bfd_byte *contents;
  4051.      Elf_Internal_Rela *relocs;
  4052.      Elf_Internal_Sym *local_syms;
  4053.      asection **local_sections;
  4054. {
  4055.   Elf_Internal_Shdr *symtab_hdr;
  4056.   size_t locsymcount;
  4057.   size_t extsymoff;
  4058.   asection *sgot, *sreloc, *scpt;
  4059.   bfd *dynobj;
  4060.   bfd_vma gp;
  4061.   Elf_Internal_Rela *rel;
  4062.   Elf_Internal_Rela *relend;
  4063.   struct mips_got_info *g;
  4064.  
  4065.   dynobj = elf_hash_table (info)->dynobj;
  4066.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  4067.  
  4068.   sgot = NULL;
  4069.   sreloc = NULL;
  4070.   if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
  4071.     scpt = NULL;
  4072.   else
  4073.     scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
  4074.   g = NULL;
  4075.  
  4076.   if (elf_bad_symtab (input_bfd))
  4077.     {
  4078.       locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
  4079.       extsymoff = 0;
  4080.     }
  4081.   else
  4082.     {
  4083.       locsymcount = symtab_hdr->sh_info;
  4084.       extsymoff = symtab_hdr->sh_info;
  4085.     }
  4086.  
  4087.   gp = _bfd_get_gp_value (output_bfd);
  4088.  
  4089.   rel = relocs;
  4090.   relend = relocs + input_section->reloc_count;
  4091.   for (; rel < relend; rel++)
  4092.     {
  4093.       int r_type;
  4094.       reloc_howto_type *howto;
  4095.       unsigned long r_symndx;
  4096.       bfd_vma addend;
  4097.       struct elf_link_hash_entry *h;
  4098.       asection *sec;
  4099.       Elf_Internal_Sym *sym;
  4100.       bfd_reloc_status_type r;
  4101.  
  4102.       r_type = ELF32_R_TYPE (rel->r_info);
  4103.       if (r_type < 0 || r_type >= (int) R_MIPS_max)
  4104.     {
  4105.       bfd_set_error (bfd_error_bad_value);
  4106.       return false;
  4107.     }
  4108.       howto = elf_mips_howto_table + r_type;
  4109.  
  4110.       if (dynobj != NULL
  4111.       && (r_type == R_MIPS_CALL16
  4112.           || r_type == R_MIPS_GOT16
  4113.           || r_type == R_MIPS_CALL_HI16
  4114.           || r_type == R_MIPS_CALL_LO16
  4115.           || r_type == R_MIPS_GOT_HI16
  4116.           || r_type == R_MIPS_GOT_LO16))
  4117.     {
  4118.       /* We need the .got section.  */
  4119.       if (sgot == NULL)
  4120.         {
  4121.           sgot = bfd_get_section_by_name (dynobj, ".got");
  4122.           BFD_ASSERT (sgot != NULL);
  4123.           BFD_ASSERT (elf_section_data (sgot) != NULL);
  4124.           g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
  4125.           BFD_ASSERT (g != NULL);
  4126.         }
  4127.     }
  4128.  
  4129.       r_symndx = ELF32_R_SYM (rel->r_info);
  4130.  
  4131.       /* Mix in the change in GP address for a GP relative reloc.  */
  4132.       if (r_type != R_MIPS_GPREL16
  4133.       && r_type != R_MIPS_LITERAL
  4134.       && r_type != R_MIPS_GPREL32)
  4135.     addend = 0;
  4136.       else
  4137.     {
  4138.       if (gp == 0)
  4139.         {
  4140.           if (! ((*info->callbacks->reloc_dangerous)
  4141.              (info,
  4142.               "GP relative relocation when GP not defined",
  4143.               input_bfd, input_section,
  4144.               rel->r_offset)))
  4145.         return false;
  4146.           /* Only give the error once per link.  */
  4147.           gp = 4;
  4148.           _bfd_set_gp_value (output_bfd, gp);
  4149.         }
  4150.  
  4151.       if (r_symndx < extsymoff
  4152.           || (elf_bad_symtab (input_bfd)
  4153.           && local_sections[r_symndx] != NULL))
  4154.         {
  4155.           /* This is a relocation against a section.  The current
  4156.          addend in the instruction is the difference between
  4157.          INPUT_SECTION->vma and the GP value of INPUT_BFD.  We
  4158.          must change this to be the difference between the
  4159.          final definition (which will end up in RELOCATION)
  4160.          and the GP value of OUTPUT_BFD (which is in GP).  */
  4161.           addend = elf_gp (input_bfd) - gp;
  4162.         }
  4163.       else if (! info->relocateable)
  4164.         {
  4165.           /* We are doing a final link.  The current addend in the
  4166.          instruction is simply the desired offset into the
  4167.          symbol (normally zero).  We want the instruction to
  4168.          hold the difference between the final definition of
  4169.          the symbol (which will end up in RELOCATION) and the
  4170.          GP value of OUTPUT_BFD (which is in GP).  */
  4171.           addend = - gp;
  4172.         }
  4173.       else
  4174.         {
  4175.           /* We are generating relocateable output, and we aren't
  4176.          going to define this symbol, so we just leave the
  4177.          instruction alone.  */
  4178.           addend = 0;
  4179.         }
  4180.     }
  4181.  
  4182.       h = NULL;
  4183.       sym = NULL;
  4184.       sec = NULL;
  4185.       if (info->relocateable)
  4186.     {
  4187.       /* This is a relocateable link.  We don't have to change
  4188.          anything, unless the reloc is against a section symbol,
  4189.          in which case we have to adjust according to where the
  4190.          section symbol winds up in the output section.  */
  4191.       if (r_symndx >= locsymcount
  4192.           || (elf_bad_symtab (input_bfd)
  4193.           && local_sections[r_symndx] == NULL))
  4194.         r = bfd_reloc_ok;
  4195.       else
  4196.         {
  4197.           sym = local_syms + r_symndx;
  4198.           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  4199.         r = bfd_reloc_ok;
  4200.           else
  4201.         {
  4202.           sec = local_sections[r_symndx];
  4203.  
  4204.           /* It would be logical to add sym->st_value here,
  4205.              but Irix 5 sometimes generates a garbage symbol
  4206.              value.  */
  4207.           addend += sec->output_offset;
  4208.  
  4209.           /* If this is HI16 or GOT16 with an associated LO16,
  4210.              adjust the addend accordingly.  Otherwise, just
  4211.              relocate.  */
  4212.           if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
  4213.             r = _bfd_relocate_contents (howto, input_bfd,
  4214.                         addend,
  4215.                         contents + rel->r_offset);
  4216.           else
  4217.             {
  4218.               Elf_Internal_Rela *lorel;
  4219.  
  4220.               /* As a GNU extension, permit an arbitrary
  4221.              number of R_MIPS_HI16 relocs before the
  4222.              R_MIPS_LO16 reloc.  This permits gcc to emit
  4223.              the HI and LO relocs itself.  */
  4224.               if (r_type == R_MIPS_GOT16)
  4225.             lorel = rel + 1;
  4226.               else
  4227.             {
  4228.               for (lorel = rel + 1;
  4229.                    (lorel < relend
  4230.                 && (ELF32_R_TYPE (lorel->r_info)
  4231.                     == R_MIPS_HI16));
  4232.                    lorel++)
  4233.                 ;
  4234.             }
  4235.               if (lorel < relend
  4236.               && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
  4237.             {
  4238.               mips_elf_relocate_hi16 (input_bfd, rel, lorel,
  4239.                           contents, addend);
  4240.               r = bfd_reloc_ok;
  4241.             }
  4242.               else
  4243.             r = _bfd_relocate_contents (howto, input_bfd,
  4244.                             addend,
  4245.                             contents + rel->r_offset);
  4246.             }
  4247.         }
  4248.         }
  4249.     }
  4250.       else
  4251.     {
  4252.       bfd_vma relocation;
  4253.       boolean local;
  4254.  
  4255.       /* This is a final link.  */
  4256.       sym = NULL;
  4257.       if (r_symndx < extsymoff
  4258.           || (elf_bad_symtab (input_bfd)
  4259.           && local_sections[r_symndx] != NULL))
  4260.         {
  4261.           local = true;
  4262.           sym = local_syms + r_symndx;
  4263.           sec = local_sections[r_symndx];
  4264.           relocation = (sec->output_section->vma
  4265.                 + sec->output_offset);
  4266.  
  4267.           /* It would be logical to always add sym->st_value here,
  4268.          but Irix 5 sometimes generates a garbage symbol
  4269.          value.  */
  4270.           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  4271.         relocation += sym->st_value;
  4272.         }
  4273.       else
  4274.         {
  4275.           long indx;
  4276.  
  4277.           local = false;
  4278.           indx = r_symndx - extsymoff;
  4279.           h = elf_sym_hashes (input_bfd)[indx];
  4280.           while (h->root.type == bfd_link_hash_indirect
  4281.              || h->root.type == bfd_link_hash_warning)
  4282.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4283.           if (strcmp (h->root.root.string, "_gp_disp") == 0)
  4284.         {
  4285.           if (gp == 0)
  4286.             {
  4287.               if (! ((*info->callbacks->reloc_dangerous)
  4288.                  (info,
  4289.                   "_gp_disp used when GP not defined",
  4290.                   input_bfd, input_section,
  4291.                   rel->r_offset)))
  4292.             return false;
  4293.               /* Only give the error once per link.  */
  4294.               gp = 4;
  4295.               _bfd_set_gp_value (output_bfd, gp);
  4296.               relocation = 0;
  4297.             }
  4298.           else
  4299.             {
  4300.               sec = input_section;
  4301.               if (sec->output_section != NULL)
  4302.             relocation = (gp
  4303.                       - (rel->r_offset
  4304.                      + sec->output_section->vma
  4305.                      + sec->output_offset));
  4306.               else
  4307.             relocation = gp - rel->r_offset;
  4308.               if (r_type == R_MIPS_LO16)
  4309.             relocation += 4;
  4310.             }
  4311.         }
  4312.           else if (h->root.type == bfd_link_hash_defined
  4313.           || h->root.type == bfd_link_hash_defweak)
  4314.         {
  4315.           sec = h->root.u.def.section;
  4316.           if (sec->output_section == NULL)
  4317.             relocation = 0;
  4318.           else
  4319.             relocation = (h->root.u.def.value
  4320.                   + sec->output_section->vma
  4321.                   + sec->output_offset);
  4322.         }
  4323.           else if (h->root.type == bfd_link_hash_undefweak)
  4324.         relocation = 0;
  4325.           else if (info->shared && ! info->symbolic)
  4326.         relocation = 0;
  4327.           else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
  4328.         {
  4329.           /* If this is a dynamic link, we should have created
  4330.                      a _DYNAMIC_LINK symbol in
  4331.                      mips_elf_create_dynamic_sections.  Otherwise, we
  4332.                      should define the symbol with a value of 0.
  4333.                      FIXME: It should probably get into the symbol
  4334.                      table somehow as well.  */
  4335.           BFD_ASSERT (! info->shared);
  4336.           BFD_ASSERT (bfd_get_section_by_name (output_bfd,
  4337.                                ".dynamic") == NULL);
  4338.           relocation = 0;
  4339.         }
  4340.           else
  4341.         {
  4342.           if (! ((*info->callbacks->undefined_symbol)
  4343.              (info, h->root.root.string, input_bfd,
  4344.               input_section, rel->r_offset)))
  4345.             return false;
  4346.           relocation = 0;
  4347.         }
  4348.         }
  4349.  
  4350.       if (r_type == R_MIPS_HI16)
  4351.         {
  4352.           Elf_Internal_Rela *lorel;
  4353.  
  4354.           /* As a GNU extension, permit an arbitrary number of
  4355.          R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
  4356.          This permits gcc to emit the HI and LO relocs itself.  */
  4357.           for (lorel = rel + 1;
  4358.            (lorel < relend
  4359.             && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
  4360.            lorel++)
  4361.         ;
  4362.           if (lorel < relend
  4363.           && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
  4364.         {
  4365.           mips_elf_relocate_hi16 (input_bfd, rel, lorel,
  4366.                       contents, relocation + addend);
  4367.           r = bfd_reloc_ok;
  4368.         }
  4369.           else
  4370.         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  4371.                           contents, rel->r_offset,
  4372.                           relocation, addend);
  4373.         }
  4374.       else if (r_type == R_MIPS_GOT16 && local)
  4375.         {
  4376.           /* GOT16 must also have an associated LO16 in the local
  4377.          case.  In this case, the addend is extracted and the
  4378.          section in which the referenced object is determined.
  4379.          Then the final address of the object is computed and
  4380.          the GOT entry for the hipage (an aligned 64kb chunk)
  4381.          is added to .got section if needed.  The offset field
  4382.          of the GOT16-relocated instruction is replaced by the
  4383.          index of this GOT entry for the hipage.  */
  4384.           if ((rel + 1) < relend
  4385.           && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
  4386.         {
  4387.           mips_elf_relocate_got_local (output_bfd, input_bfd, sgot,
  4388.                            rel, rel + 1,
  4389.                            contents,
  4390.                            relocation + addend);
  4391.           r = bfd_reloc_ok;
  4392.         }
  4393.           else
  4394.         r = bfd_reloc_outofrange;
  4395.         }
  4396.       else if (r_type == R_MIPS_CALL16
  4397.            || r_type == R_MIPS_GOT16
  4398.            || r_type == R_MIPS_CALL_LO16
  4399.            || r_type == R_MIPS_GOT_LO16)
  4400.         {
  4401.           bfd_vma offset;
  4402.  
  4403.           /* This symbol must be registered as a global symbol
  4404.          having the corresponding got entry.  */
  4405.           BFD_ASSERT (h->got_offset != (bfd_vma) -1);
  4406.  
  4407.           offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
  4408.           BFD_ASSERT (g->local_gotno <= offset
  4409.               && offset < sgot->_raw_size);
  4410.           bfd_put_32 (output_bfd, relocation + addend,
  4411.               sgot->contents + offset);
  4412.           offset = (sgot->output_section->vma + sgot->output_offset
  4413.             + offset - gp);
  4414.           mips_elf_relocate_global_got (input_bfd, rel, contents,
  4415.                         offset);
  4416.           r = bfd_reloc_ok;
  4417.         }
  4418.       else if (r_type == R_MIPS_CALL_HI16
  4419.            || r_type == R_MIPS_GOT_HI16)
  4420.         {
  4421.           bfd_vma offset;
  4422.  
  4423.           /* This must be a global symbol with a got entry.  The
  4424.                  next reloc must be the corresponding LO16 reloc.  */
  4425.           BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
  4426.           BFD_ASSERT ((rel + 1) < relend);
  4427.           BFD_ASSERT (ELF32_R_TYPE ((rel + 1)->r_info)
  4428.               == (r_type == R_MIPS_CALL_HI16
  4429.                   ? R_MIPS_CALL_LO16
  4430.                   : R_MIPS_GOT_LO16));
  4431.  
  4432.           offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
  4433.           BFD_ASSERT (g->local_gotno <= offset
  4434.               && offset < sgot->_raw_size);
  4435.           bfd_put_32 (output_bfd, relocation + addend,
  4436.               sgot->contents + offset);
  4437.           offset = (sgot->output_section->vma + sgot->output_offset
  4438.             + offset - gp);
  4439.           mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
  4440.                       offset);
  4441.           r = bfd_reloc_ok;
  4442.         }
  4443.       else if (r_type == R_MIPS_REL32
  4444.            || r_type == R_MIPS_32)
  4445.         {
  4446.           Elf_Internal_Rel outrel;
  4447.           Elf32_crinfo cptrel;
  4448.           bfd_byte *cr;
  4449.  
  4450.           if ((info->shared
  4451.            || (elf_hash_table (info)->dynamic_sections_created
  4452.                && h != NULL
  4453.                && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
  4454.                == 0)))
  4455.           && (input_section->flags & SEC_ALLOC) != 0)
  4456.         {
  4457.           /* When generating a shared object, these
  4458.              relocations are copied into the output file to be
  4459.              resolved at run time.  */
  4460.           if (sreloc == NULL)
  4461.             {
  4462.               sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
  4463.               BFD_ASSERT (sreloc != NULL);
  4464.             }
  4465.  
  4466.           outrel.r_offset = (rel->r_offset
  4467.                      + input_section->output_section->vma
  4468.                      + input_section->output_offset);
  4469.  
  4470.           addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
  4471.  
  4472.           if (h != NULL
  4473.               && (! info->symbolic
  4474.               || (h->elf_link_hash_flags
  4475.                   & ELF_LINK_HASH_DEF_REGULAR) == 0))
  4476.             {
  4477.               BFD_ASSERT (h->dynindx != -1);
  4478.               outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
  4479.               sec = input_section;
  4480.             }
  4481.           else
  4482.             {
  4483.               long indx;
  4484.  
  4485.               if (h == NULL)
  4486.             sec = local_sections[r_symndx];
  4487.               else
  4488.             {
  4489.               BFD_ASSERT (h->root.type == bfd_link_hash_defined
  4490.                       || (h->root.type
  4491.                       == bfd_link_hash_defweak));
  4492.               sec = h->root.u.def.section;
  4493.             }
  4494.               if (sec != NULL && bfd_is_abs_section (sec))
  4495.             indx = 0;
  4496.               else if (sec == NULL || sec->owner == NULL)
  4497.             {
  4498.               bfd_set_error (bfd_error_bad_value);
  4499.               return false;
  4500.             }
  4501.               else
  4502.             {
  4503.               asection *osec;
  4504.  
  4505.               osec = sec->output_section;
  4506.               indx = elf_section_data (osec)->dynindx;
  4507.               if (indx == 0)
  4508.                 abort ();
  4509.             }
  4510.  
  4511.               outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
  4512.               addend += relocation;
  4513.             }
  4514.  
  4515.           bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
  4516.           bfd_elf32_swap_reloc_out (output_bfd, &outrel,
  4517.                          (((Elf32_External_Rel *)
  4518.                            sreloc->contents)
  4519.                           + sreloc->reloc_count));
  4520.           ++sreloc->reloc_count;
  4521.  
  4522.           if (SGI_COMPAT (output_bfd))
  4523.             {
  4524.               if (scpt == NULL)
  4525.             continue;
  4526.  
  4527.               /* Make an entry of compact relocation info.  */
  4528.               mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
  4529.               cptrel.vaddr = (rel->r_offset
  4530.                       + input_section->output_section->vma
  4531.                       + input_section->output_offset);
  4532.               if (r_type == R_MIPS_REL32)
  4533.             mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
  4534.               else
  4535.             mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
  4536.               mips_elf_set_cr_dist2to (cptrel, 0);
  4537.               cptrel.konst = addend;
  4538.  
  4539.               cr = (scpt->contents
  4540.                 + sizeof (Elf32_External_compact_rel));
  4541.               bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
  4542.                          ((Elf32_External_crinfo *) cr
  4543.                           + scpt->reloc_count));
  4544.               ++scpt->reloc_count;
  4545.             }
  4546.  
  4547.           /* This reloc will be computed at runtime, so
  4548.              there's no need to do anything now.  */
  4549.           continue;
  4550.         }
  4551.           else
  4552.         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  4553.                           contents, rel->r_offset,
  4554.                           relocation, addend);
  4555.         }
  4556.       else
  4557.         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  4558.                       contents, rel->r_offset,
  4559.                       relocation, addend);
  4560.  
  4561.       if (SGI_COMPAT (abfd)
  4562.           && scpt != NULL
  4563.           && (input_section->flags & SEC_ALLOC) != 0)
  4564.         {
  4565.           Elf32_crinfo cptrel;
  4566.           bfd_byte *cr;
  4567.  
  4568.           /* Make an entry of compact relocation info.  */
  4569.           mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
  4570.           cptrel.vaddr = (rel->r_offset
  4571.                   + input_section->output_section->vma
  4572.                   + input_section->output_offset);
  4573.  
  4574.           switch (r_type)
  4575.         {
  4576.         case R_MIPS_26:
  4577.           mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
  4578.           /* XXX How should we set dist2to in this case. */
  4579.           mips_elf_set_cr_dist2to (cptrel, 8);
  4580.           cptrel.konst = addend + relocation;
  4581.           cr = scpt->contents + sizeof (Elf32_External_compact_rel);
  4582.           bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
  4583.                          ((Elf32_External_crinfo *) cr
  4584.                           + scpt->reloc_count));
  4585.           ++scpt->reloc_count;
  4586.           break;
  4587.  
  4588.         case R_MIPS_GPREL16:
  4589.         case R_MIPS_LITERAL:
  4590.         case R_MIPS_GPREL32:
  4591.           mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
  4592.           cptrel.konst = gp - cptrel.vaddr;
  4593.           mips_elf_set_cr_dist2to (cptrel, 4);
  4594.           cr = scpt->contents + sizeof (Elf32_External_compact_rel);
  4595.           bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
  4596.                          ((Elf32_External_crinfo *) cr
  4597.                           + scpt->reloc_count));
  4598.           ++scpt->reloc_count;
  4599.           break;
  4600.  
  4601.         default:
  4602.           break;
  4603.         }
  4604.         }
  4605.     }
  4606.  
  4607.       if (r != bfd_reloc_ok)
  4608.     {
  4609.       switch (r)
  4610.         {
  4611.         default:
  4612.         case bfd_reloc_outofrange:
  4613.           abort ();
  4614.         case bfd_reloc_overflow:
  4615.           {
  4616.         const char *name;
  4617.  
  4618.         if (h != NULL)
  4619.           name = h->root.root.string;
  4620.         else
  4621.           {
  4622.             name = bfd_elf_string_from_elf_section (input_bfd,
  4623.                                 symtab_hdr->sh_link,
  4624.                                 sym->st_name);
  4625.             if (name == NULL)
  4626.               return false;
  4627.             if (*name == '\0')
  4628.               name = bfd_section_name (input_bfd, sec);
  4629.           }
  4630.         if (! ((*info->callbacks->reloc_overflow)
  4631.                (info, name, howto->name, (bfd_vma) 0,
  4632.             input_bfd, input_section, rel->r_offset)))
  4633.           return false;
  4634.           }
  4635.           break;
  4636.         }
  4637.     }
  4638.     }
  4639.  
  4640.   return true;
  4641. }
  4642.  
  4643. /* Functions for the dynamic linker.  */
  4644.  
  4645. /* The name of the dynamic interpreter.  This is put in the .interp
  4646.    section.  */
  4647.  
  4648. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  4649.  
  4650. /* Create dynamic sections when linking against a dynamic object.  */
  4651.  
  4652. static boolean
  4653. mips_elf_create_dynamic_sections (abfd, info)
  4654.      bfd *abfd;
  4655.      struct bfd_link_info *info;
  4656. {
  4657.   struct elf_link_hash_entry *h;
  4658.   flagword flags;
  4659.   register asection *s;
  4660.   const char * const *namep;
  4661.  
  4662.   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  4663.        | SEC_READONLY);
  4664.  
  4665.   /* Mips ABI requests the .dynamic section to be read only.  */
  4666.   s = bfd_get_section_by_name (abfd, ".dynamic");
  4667.   if (s != NULL)
  4668.     {
  4669.       if (! bfd_set_section_flags (abfd, s, flags))
  4670.     return false;
  4671.     }
  4672.  
  4673.   /* We need to create .got section.  */
  4674.   if (! mips_elf_create_got_section (abfd, info))
  4675.     return false;
  4676.  
  4677.   /* Create .stub section.  */
  4678.   if (bfd_get_section_by_name (abfd, ".stub") == NULL)
  4679.     {
  4680.       s = bfd_make_section (abfd, ".stub");
  4681.       if (s == NULL
  4682.       || ! bfd_set_section_flags (abfd, s, flags)
  4683.       || ! bfd_set_section_alignment (abfd, s, 2))
  4684.     return false;
  4685.     }
  4686.  
  4687.   if (SGI_COMPAT (abfd)
  4688.       && !info->shared
  4689.       && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
  4690.     {
  4691.       s = bfd_make_section (abfd, ".rld_map");
  4692.       if (s == NULL
  4693.       || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
  4694.       || ! bfd_set_section_alignment (abfd, s, 2))
  4695.     return false;
  4696.     }
  4697.  
  4698.   if (SGI_COMPAT (abfd))
  4699.     {
  4700.       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
  4701.     {
  4702.       h = NULL;
  4703.       if (! (_bfd_generic_link_add_one_symbol
  4704.          (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
  4705.           (bfd_vma) 0, (const char *) NULL, false,
  4706.           get_elf_backend_data (abfd)->collect,
  4707.           (struct bfd_link_hash_entry **) &h)))
  4708.         return false;
  4709.       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
  4710.       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  4711.       h->type = STT_SECTION;
  4712.  
  4713.       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  4714.         return false;
  4715.     }
  4716.  
  4717.       /* We need to create a .compact_rel section.  */
  4718.       if (! mips_elf_create_compact_rel_section (abfd, info))
  4719.     return false;
  4720.  
  4721.       /* Change aligments of some sections.  */
  4722.       s = bfd_get_section_by_name (abfd, ".hash");
  4723.       if (s != NULL)
  4724.     bfd_set_section_alignment (abfd, s, 4);
  4725.       s = bfd_get_section_by_name (abfd, ".dynsym");
  4726.       if (s != NULL)
  4727.     bfd_set_section_alignment (abfd, s, 4);
  4728.       s = bfd_get_section_by_name (abfd, ".dynstr");
  4729.       if (s != NULL)
  4730.     bfd_set_section_alignment (abfd, s, 4);
  4731.       s = bfd_get_section_by_name (abfd, ".reginfo");
  4732.       if (s != NULL)
  4733.     bfd_set_section_alignment (abfd, s, 4);
  4734.       s = bfd_get_section_by_name (abfd, ".dynamic");
  4735.       if (s != NULL)
  4736.     bfd_set_section_alignment (abfd, s, 4);
  4737.     }
  4738.  
  4739.   if (!info->shared)
  4740.     {
  4741.       h = NULL;
  4742.       if (! (_bfd_generic_link_add_one_symbol
  4743.          (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
  4744.           (bfd_vma) 0, (const char *) NULL, false,
  4745.           get_elf_backend_data (abfd)->collect,
  4746.           (struct bfd_link_hash_entry **) &h)))
  4747.     return false;
  4748.       h->elf_link_hash_flags ^=~ ELF_LINK_NON_ELF;
  4749.       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  4750.       h->type = STT_SECTION;
  4751.  
  4752.       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  4753.     return false;
  4754.  
  4755.       if (! mips_elf_hash_table (info)->use_rld_obj_head)
  4756.     {
  4757.       /* __rld_map is a four byte word located in the .data section
  4758.          and is filled in by the rtld to contain a pointer to
  4759.          the _r_debug structure. Its symbol value will be set in
  4760.          mips_elf_finish_dynamic_symbol.  */
  4761.       s = bfd_get_section_by_name (abfd, ".rld_map");
  4762.       BFD_ASSERT (s != NULL);
  4763.  
  4764.       h = NULL;
  4765.       if (! (_bfd_generic_link_add_one_symbol
  4766.          (info, abfd, "__rld_map", BSF_GLOBAL, s,
  4767.           (bfd_vma) 0, (const char *) NULL, false,
  4768.           get_elf_backend_data (abfd)->collect,
  4769.           (struct bfd_link_hash_entry **) &h)))
  4770.         return false;
  4771.       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
  4772.       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  4773.       h->type = STT_OBJECT;
  4774.  
  4775.       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  4776.         return false;
  4777.     }
  4778.     }
  4779.  
  4780.   return true;
  4781. }
  4782.  
  4783. /* Create the .compact_rel section.  */
  4784.  
  4785. static boolean
  4786. mips_elf_create_compact_rel_section (abfd, info)
  4787.      bfd *abfd;
  4788.      struct bfd_link_info *info;
  4789. {
  4790.   flagword flags;
  4791.   register asection *s;
  4792.  
  4793.   if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
  4794.     {
  4795.       flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY;
  4796.  
  4797.       s = bfd_make_section (abfd, ".compact_rel");
  4798.       if (s == NULL
  4799.       || ! bfd_set_section_flags (abfd, s, flags)
  4800.       || ! bfd_set_section_alignment (abfd, s, 2))
  4801.     return false;
  4802.  
  4803.       s->_raw_size = sizeof (Elf32_External_compact_rel);
  4804.     }
  4805.  
  4806.   return true;
  4807. }
  4808.   
  4809. /* Create the .got section to hold the global offset table. */
  4810.  
  4811. static boolean
  4812. mips_elf_create_got_section (abfd, info)
  4813.      bfd *abfd;
  4814.      struct bfd_link_info *info;
  4815. {
  4816.   flagword flags;
  4817.   register asection *s;
  4818.   struct elf_link_hash_entry *h;
  4819.   struct mips_got_info *g;
  4820.  
  4821.   /* This function may be called more than once.  */
  4822.   if (bfd_get_section_by_name (abfd, ".got") != NULL)
  4823.     return true;
  4824.  
  4825.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  4826.  
  4827.   s = bfd_make_section (abfd, ".got");
  4828.   if (s == NULL
  4829.       || ! bfd_set_section_flags (abfd, s, flags)
  4830.       || ! bfd_set_section_alignment (abfd, s, 4))
  4831.     return false;
  4832.  
  4833.   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
  4834.      linker script because we don't want to define the symbol if we
  4835.      are not creating a global offset table.  */
  4836.   h = NULL;
  4837.   if (! (_bfd_generic_link_add_one_symbol
  4838.      (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
  4839.       (bfd_vma) 0, (const char *) NULL, false,
  4840.       get_elf_backend_data (abfd)->collect,
  4841.       (struct bfd_link_hash_entry **) &h)))
  4842.     return false;
  4843.   h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
  4844.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  4845.   h->type = STT_OBJECT;
  4846.  
  4847.   if (info->shared
  4848.       && ! bfd_elf32_link_record_dynamic_symbol (info, h))
  4849.     return false;
  4850.  
  4851.   /* The first several global offset table entries are reserved.  */
  4852.   s->_raw_size = MIPS_RESERVED_GOTNO * 4;
  4853.  
  4854.   g = (struct mips_got_info *) bfd_alloc (abfd,
  4855.                       sizeof (struct mips_got_info));
  4856.   if (g == NULL)
  4857.     return false;
  4858.   g->global_gotsym = 0;
  4859.   g->local_gotno = MIPS_RESERVED_GOTNO;
  4860.   if (elf_section_data (s) == NULL)
  4861.     {
  4862.       s->used_by_bfd =
  4863.     (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
  4864.       if (elf_section_data (s) == NULL)
  4865.     return false;
  4866.     }
  4867.   elf_section_data (s)->tdata = (PTR) g;
  4868.  
  4869.   return true;
  4870. }
  4871.  
  4872. /* Look through the relocs for a section during the first phase, and
  4873.    allocate space in the global offset table.  */
  4874.  
  4875. static boolean
  4876. mips_elf_check_relocs (abfd, info, sec, relocs)
  4877.      bfd *abfd;
  4878.      struct bfd_link_info *info;
  4879.      asection *sec;
  4880.      const Elf_Internal_Rela *relocs;
  4881. {
  4882.   bfd *dynobj;
  4883.   Elf_Internal_Shdr *symtab_hdr;
  4884.   struct elf_link_hash_entry **sym_hashes;
  4885.   struct mips_got_info *g;
  4886.   size_t extsymoff;
  4887.   const Elf_Internal_Rela *rel;
  4888.   const Elf_Internal_Rela *rel_end;
  4889.   asection *sgot;
  4890.   asection *sreloc;
  4891.  
  4892.   if (info->relocateable)
  4893.     return true;
  4894.  
  4895.   dynobj = elf_hash_table (info)->dynobj;
  4896.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  4897.   sym_hashes = elf_sym_hashes (abfd);
  4898.   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
  4899.  
  4900.   sgot = NULL;
  4901.   sreloc = NULL;
  4902.  
  4903.   rel_end = relocs + sec->reloc_count;
  4904.   for (rel = relocs; rel < rel_end; rel++)
  4905.     {
  4906.       unsigned long r_symndx;
  4907.       struct elf_link_hash_entry *h;
  4908.  
  4909.       r_symndx = ELF32_R_SYM (rel->r_info);
  4910.  
  4911.       if (r_symndx < extsymoff)
  4912.     h = NULL;
  4913.       else
  4914.     h = sym_hashes[r_symndx - extsymoff];
  4915.  
  4916.       /* Some relocs require a global offset table.  */
  4917.       if (dynobj == NULL)
  4918.     {
  4919.       switch (ELF32_R_TYPE (rel->r_info))
  4920.         {
  4921.         case R_MIPS_GOT16:
  4922.         case R_MIPS_CALL16:
  4923.         case R_MIPS_CALL_HI16:
  4924.         case R_MIPS_CALL_LO16:
  4925.         case R_MIPS_GOT_HI16:
  4926.         case R_MIPS_GOT_LO16:
  4927.           elf_hash_table (info)->dynobj = dynobj = abfd;
  4928.           if (! mips_elf_create_got_section (dynobj, info))
  4929.         return false;
  4930.           break;
  4931.  
  4932.         default:
  4933.           break;
  4934.         }
  4935.     }
  4936.  
  4937.       switch (ELF32_R_TYPE (rel->r_info))
  4938.     {
  4939.     case R_MIPS_CALL16:
  4940.     case R_MIPS_CALL_HI16:
  4941.     case R_MIPS_CALL_LO16:
  4942.       /* This symbol requires a global offset table entry.  */
  4943.       if (sgot == NULL)
  4944.         {
  4945.           sgot = bfd_get_section_by_name (dynobj, ".got");
  4946.           BFD_ASSERT (sgot != NULL);
  4947.           BFD_ASSERT (elf_section_data (sgot) != NULL);
  4948.           g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
  4949.           BFD_ASSERT (g != NULL);
  4950.         }
  4951.  
  4952.       BFD_ASSERT (h != NULL);
  4953.  
  4954.       /* Make sure this symbol is output as a dynamic symbol.  */
  4955.       if (h->dynindx == -1)
  4956.         {
  4957.           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  4958.         return false;
  4959.         }
  4960.  
  4961.       if (h->got_offset != (bfd_vma) -1)
  4962.         {
  4963.           /* We have already allocated space in the .got.  */
  4964.           break;
  4965.         }
  4966.  
  4967.       /* Note the index of the first global got symbol in .dynsym.  */
  4968.       if (g->global_gotsym == 0
  4969.           || g->global_gotsym > (unsigned long) h->dynindx)
  4970.         g->global_gotsym = h->dynindx;
  4971.  
  4972.       /* Make this symbol to have the corresponding got entry.  */
  4973.       h->got_offset = 0;
  4974.  
  4975.       /* We need a stub, not a plt entry for the undefined
  4976.          function.  But we record it as if it needs plt.  See
  4977.          elf_adjust_dynamic_symbol in elflink.h.  */
  4978.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
  4979.       h->type = STT_FUNC;
  4980.  
  4981.       break;
  4982.  
  4983.     case R_MIPS_GOT16:
  4984.     case R_MIPS_GOT_HI16:
  4985.     case R_MIPS_GOT_LO16:
  4986.       /* This symbol requires a global offset table entry.  */
  4987.       if (sgot == NULL)
  4988.         {
  4989.           sgot = bfd_get_section_by_name (dynobj, ".got");
  4990.           BFD_ASSERT (sgot != NULL);
  4991.           BFD_ASSERT (elf_section_data (sgot) != NULL);
  4992.           g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
  4993.           BFD_ASSERT (g != NULL);
  4994.         }
  4995.  
  4996.       if (h != NULL)
  4997.         {
  4998.           /* Make sure this symbol is output as a dynamic symbol.  */
  4999.           if (h->dynindx == -1)
  5000.         {
  5001.           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  5002.             return false;
  5003.         }
  5004.  
  5005.           if (h->got_offset != (bfd_vma) -1)
  5006.         {
  5007.           /* We have already allocated space in the .got.  */
  5008.           break;
  5009.         }
  5010.           /* Note the index of the first global got symbol in
  5011.                  .dynsym.  */
  5012.           if (g->global_gotsym == 0
  5013.           || g->global_gotsym > (unsigned long) h->dynindx)
  5014.         g->global_gotsym = h->dynindx;
  5015.  
  5016.           /* Make this symbol to be the global got symbol.  */
  5017.           h->got_offset = 0;
  5018.         }
  5019.  
  5020.       break;
  5021.  
  5022.     case R_MIPS_32:
  5023.     case R_MIPS_REL32:
  5024.       if ((info->shared || h != NULL)
  5025.           && (sec->flags & SEC_ALLOC) != 0)
  5026.         {
  5027.           if (info->shared)
  5028.         {
  5029.           /* When creating a shared object, we must copy these
  5030.              reloc types into the output file as R_MIPS_REL32
  5031.              relocs.  We create the .rel.dyn reloc section in
  5032.              dynobj and make room for this reloc.  */
  5033.           if (sreloc == NULL)
  5034.             {
  5035.               const char *name = ".rel.dyn";
  5036.  
  5037.               sreloc = bfd_get_section_by_name (dynobj, name);
  5038.               if (sreloc == NULL)
  5039.             {
  5040.               sreloc = bfd_make_section (dynobj, name);
  5041.               if (sreloc == NULL
  5042.                   || ! bfd_set_section_flags (dynobj, sreloc,
  5043.                               (SEC_ALLOC
  5044.                                | SEC_LOAD
  5045.                                | SEC_HAS_CONTENTS
  5046.                                | SEC_IN_MEMORY
  5047.                                | SEC_READONLY))
  5048.                   || ! bfd_set_section_alignment (dynobj, sreloc,
  5049.                                   4))
  5050.                 return false;
  5051.  
  5052.               /* Add a null element. */
  5053.               sreloc->_raw_size += sizeof (Elf32_External_Rel);
  5054.               ++sreloc->reloc_count;
  5055.             }
  5056.             }
  5057.  
  5058.           sreloc->_raw_size += sizeof (Elf32_External_Rel);
  5059.         }
  5060.           else
  5061.         {
  5062.           struct mips_elf_link_hash_entry *hmips;
  5063.  
  5064.           /* We only need to copy this reloc if the symbol is
  5065.                      defined in a dynamic object.  */
  5066.           hmips = (struct mips_elf_link_hash_entry *) h;
  5067.           ++hmips->mips_32_relocs;
  5068.         }
  5069.         }
  5070.  
  5071.       if (SGI_COMPAT (abfd))
  5072.         mips_elf_hash_table (info)->compact_rel_size +=
  5073.           sizeof (Elf32_External_crinfo);
  5074.  
  5075.       break;
  5076.  
  5077.     case R_MIPS_26:
  5078.     case R_MIPS_GPREL16:
  5079.     case R_MIPS_LITERAL:
  5080.     case R_MIPS_GPREL32:
  5081.       if (SGI_COMPAT (abfd))
  5082.         mips_elf_hash_table (info)->compact_rel_size +=
  5083.           sizeof (Elf32_External_crinfo);
  5084.       break;
  5085.  
  5086.     default:
  5087.       break;
  5088.     }
  5089.     }
  5090.  
  5091.   return true;
  5092. }
  5093.  
  5094. /* Adjust a symbol defined by a dynamic object and referenced by a
  5095.    regular object.  The current definition is in some section of the
  5096.    dynamic object, but we're not including those sections.  We have to
  5097.    change the definition to something the rest of the link can
  5098.    understand.  */
  5099.  
  5100. static boolean
  5101. mips_elf_adjust_dynamic_symbol (info, h)
  5102.      struct bfd_link_info *info;
  5103.      struct elf_link_hash_entry *h;
  5104. {
  5105.   bfd *dynobj;
  5106.   struct mips_elf_link_hash_entry *hmips;
  5107.   asection *s;
  5108.  
  5109.   dynobj = elf_hash_table (info)->dynobj;
  5110.  
  5111.   /* Make sure we know what is going on here.  */
  5112.   BFD_ASSERT (dynobj != NULL
  5113.           && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
  5114.           || h->weakdef != NULL
  5115.           || ((h->elf_link_hash_flags
  5116.                & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  5117.               && (h->elf_link_hash_flags
  5118.               & ELF_LINK_HASH_REF_REGULAR) != 0
  5119.               && (h->elf_link_hash_flags
  5120.               & ELF_LINK_HASH_DEF_REGULAR) == 0)));
  5121.  
  5122.   /* If this symbol is defined in a dynamic object, we need to copy
  5123.      any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
  5124.      file.  */
  5125.   hmips = (struct mips_elf_link_hash_entry *) h;
  5126.   if (! info->relocateable
  5127.       && hmips->mips_32_relocs != 0
  5128.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  5129.     {
  5130.       s = bfd_get_section_by_name (dynobj, ".rel.dyn");
  5131.       BFD_ASSERT (s != NULL);
  5132.  
  5133.       s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
  5134.     }
  5135.  
  5136.   /* For a function, create a stub, if needed. */
  5137.   if (h->type == STT_FUNC
  5138.       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
  5139.     {
  5140.       if (! elf_hash_table (info)->dynamic_sections_created)
  5141.     return true;
  5142.  
  5143.       /* If this symbol is not defined in a regular file, then set
  5144.      the symbol to the stub location.  This is required to make
  5145.      function pointers compare as equal between the normal
  5146.      executable and the shared library.  */
  5147.       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  5148.     {
  5149.       /* We need .stub section.  */
  5150.       s = bfd_get_section_by_name (dynobj, ".stub");
  5151.       BFD_ASSERT (s != NULL);
  5152.  
  5153.       h->root.u.def.section = s;
  5154.       h->root.u.def.value = s->_raw_size;
  5155.  
  5156.       /* XXX Write this stub address somewhere.  */
  5157.       h->plt_offset = s->_raw_size;
  5158.  
  5159.       /* Make room for this stub code.  */
  5160.       s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
  5161.  
  5162.       /* The last half word of the stub will be filled with the index
  5163.          of this symbol in .dynsym section.  */
  5164.       return true;
  5165.     }
  5166.     }
  5167.  
  5168.   /* If this is a weak symbol, and there is a real definition, the
  5169.      processor independent code will have arranged for us to see the
  5170.      real definition first, and we can just use the same value.  */
  5171.   if (h->weakdef != NULL)
  5172.     {
  5173.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
  5174.           || h->weakdef->root.type == bfd_link_hash_defweak);
  5175.       h->root.u.def.section = h->weakdef->root.u.def.section;
  5176.       h->root.u.def.value = h->weakdef->root.u.def.value;
  5177.       return true;
  5178.     }
  5179.  
  5180.   /* This is a reference to a symbol defined by a dynamic object which
  5181.      is not a function.  */
  5182.  
  5183.   return true;
  5184. }
  5185.  
  5186. /* Set the sizes of the dynamic sections.  */
  5187.  
  5188. static boolean
  5189. mips_elf_size_dynamic_sections (output_bfd, info)
  5190.      bfd *output_bfd;
  5191.      struct bfd_link_info *info;
  5192. {
  5193.   bfd *dynobj;
  5194.   asection *s;
  5195.   boolean reltext;
  5196.   asection *sgot;
  5197.   struct mips_got_info *g;
  5198.  
  5199.   dynobj = elf_hash_table (info)->dynobj;
  5200.   BFD_ASSERT (dynobj != NULL);
  5201.  
  5202.   if (elf_hash_table (info)->dynamic_sections_created)
  5203.     {
  5204.       /* Set the contents of the .interp section to the interpreter.  */
  5205.       if (! info->shared)
  5206.     {
  5207.       s = bfd_get_section_by_name (dynobj, ".interp");
  5208.       BFD_ASSERT (s != NULL);
  5209.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  5210.       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  5211.     }
  5212.     }
  5213.  
  5214.   /* Recompute the size of .got for local entires (reserved and
  5215.      hipages) if needed.  To estimate it, get the upper bound of total
  5216.      size of loadable sections.  */
  5217.   sgot = bfd_get_section_by_name (dynobj, ".got");
  5218.  
  5219.   if (sgot != NULL)
  5220.     {
  5221.       bfd_size_type loadable_size = 0;
  5222.       bfd_size_type local_gotno;
  5223.       struct _bfd *sub;
  5224.  
  5225.       BFD_ASSERT (elf_section_data (sgot) != NULL);
  5226.       g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
  5227.       BFD_ASSERT (g != NULL);
  5228.  
  5229.       for (sub = info->input_bfds; sub; sub = sub->link_next)
  5230.     for (s = sub->sections; s != NULL; s = s->next)
  5231.       {
  5232.         if ((s->flags & SEC_ALLOC) == 0)
  5233.           continue;
  5234.         loadable_size += (s->_raw_size + 0xf) & ~0xf;
  5235.       }
  5236.  
  5237.       loadable_size += MIPS_FUNCTION_STUB_SIZE;
  5238.  
  5239.       /* Assume there are two loadable segments consisting of
  5240.      contiguous sections.  Is 5 enough? */
  5241.       local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
  5242.       g->local_gotno = local_gotno;
  5243.       sgot->_raw_size += local_gotno * 4;
  5244.     }
  5245.  
  5246.   /* The check_relocs and adjust_dynamic_symbol entry points have
  5247.      determined the sizes of the various dynamic sections.  Allocate
  5248.      memory for them.  */
  5249.   reltext = false;
  5250.   for (s = dynobj->sections; s != NULL; s = s->next)
  5251.     {
  5252.       const char *name;
  5253.       boolean strip;
  5254.  
  5255.       /* It's OK to base decisions on the section name, because none
  5256.      of the dynobj section names depend upon the input files.  */
  5257.       name = bfd_get_section_name (dynobj, s);
  5258.  
  5259.       if ((s->flags & SEC_IN_MEMORY) == 0)
  5260.     continue;
  5261.  
  5262.       strip = false;
  5263.  
  5264.       if (strncmp (name, ".rel", 4) == 0)
  5265.     {
  5266.       if (s->_raw_size == 0)
  5267.         strip = true;
  5268.       else
  5269.         {
  5270.           asection *target;
  5271.  
  5272.           /* If this relocation section applies to a read only
  5273.                  section, then we probably need a DT_TEXTREL entry.
  5274.                  If the relocation section is .rel.dyn, we always
  5275.                  assert a DT_TEXTREL entry rather than testing whether
  5276.                  there exists a relocation to a read only section or
  5277.                  not.  */
  5278.           target = bfd_get_section_by_name (output_bfd, name + 4);
  5279.           if ((target != NULL && (target->flags & SEC_READONLY) != 0)
  5280.           || strcmp (name, ".rel.dyn") == 0)
  5281.         reltext = true;
  5282.  
  5283.           /* We use the reloc_count field as a counter if we need
  5284.          to copy relocs into the output file.  */
  5285.           if (strcmp (name, ".rel.dyn") != 0)
  5286.         s->reloc_count = 0;
  5287.         }
  5288.     }
  5289.       else if (strncmp (name, ".got", 4) == 0)
  5290.     {
  5291.       int i;
  5292.  
  5293.       BFD_ASSERT (elf_section_data (s) != NULL);
  5294.       g = (struct mips_got_info *) elf_section_data (s)->tdata;
  5295.       BFD_ASSERT (g != NULL);
  5296.  
  5297.       /* Fix the size of .got section for the correspondence of
  5298.          global symbols and got entries. This adds some useless
  5299.          got entries. Is this required by ABI really?  */
  5300.       i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
  5301.       s->_raw_size += i * 4;
  5302.     }
  5303.       else if (strncmp (name, ".stub", 5) == 0)
  5304.     {
  5305.       /* Irix rld assumes that the function stub isn't at the end
  5306.          of .text section. So put a dummy. XXX  */
  5307.       s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
  5308.     }
  5309.       else if (! info->shared
  5310.            && ! mips_elf_hash_table (info)->use_rld_obj_head
  5311.            && strncmp (name, ".rld_map", 8) == 0)
  5312.     {
  5313.       /* We add a room for __rld_map. It will be filled in by the
  5314.          rtld to contain a pointer to the _r_debug structure.  */
  5315.       s->_raw_size += 4;
  5316.     }
  5317.       else if (SGI_COMPAT (output_bfd)
  5318.            && strncmp (name, ".compact_rel", 12) == 0)
  5319.     s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
  5320.       else if (strncmp (name, ".init", 5) != 0)
  5321.     {
  5322.       /* It's not one of our sections, so don't allocate space.  */
  5323.       continue;
  5324.     }
  5325.  
  5326.       if (strip)
  5327.     {
  5328.       asection **spp;
  5329.  
  5330.       for (spp = &s->output_section->owner->sections;
  5331.            *spp != s->output_section;
  5332.            spp = &(*spp)->next)
  5333.         ;
  5334.       *spp = s->output_section->next;
  5335.       --s->output_section->owner->section_count;
  5336.  
  5337.       continue;
  5338.     }
  5339.  
  5340.       /* Allocate memory for the section contents.  */
  5341.       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
  5342.       if (s->contents == NULL && s->_raw_size != 0)
  5343.     {
  5344.       bfd_set_error (bfd_error_no_memory);
  5345.       return false;
  5346.     }
  5347.       memset (s->contents, 0, s->_raw_size);
  5348.     }
  5349.  
  5350.   if (elf_hash_table (info)->dynamic_sections_created)
  5351.     {
  5352.       /* Add some entries to the .dynamic section.  We fill in the
  5353.      values later, in elf_mips_finish_dynamic_sections, but we
  5354.      must add the entries now so that we get the correct size for
  5355.      the .dynamic section.  The DT_DEBUG entry is filled in by the
  5356.      dynamic linker and used by the debugger.  */
  5357.       if (! info->shared)
  5358.     {
  5359.       if (SGI_COMPAT (output_bfd))
  5360.         {
  5361.           /* SGI object has the equivalence of DT_DEBUG in the
  5362.          DT_MIPS_RLD_MAP entry.  */
  5363.           if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
  5364.         return false;
  5365.         }
  5366.       else
  5367.         if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
  5368.           return false;
  5369.     }
  5370.  
  5371.       if (reltext)
  5372.     {
  5373.       if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
  5374.         return false;
  5375.     }
  5376.  
  5377.       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
  5378.     return false;
  5379.  
  5380.       if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
  5381.     {
  5382.       if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
  5383.         return false;
  5384.  
  5385.       if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
  5386.         return false;
  5387.  
  5388.       if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
  5389.         return false;
  5390.     }
  5391.  
  5392.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
  5393.     return false;
  5394.  
  5395.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
  5396.     return false;
  5397.  
  5398.       if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
  5399.     {
  5400.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
  5401.         return false;
  5402.  
  5403.       s = bfd_get_section_by_name (dynobj, ".liblist");
  5404.       BFD_ASSERT (s != NULL);
  5405.  
  5406.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
  5407.         return false;
  5408.     }
  5409.  
  5410.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
  5411.     return false;
  5412.  
  5413.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
  5414.     return false;
  5415.  
  5416. #if 0
  5417.       /* Time stamps in executable files are a bad idea.  */
  5418.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
  5419.     return false;
  5420. #endif
  5421.  
  5422. #if 0 /* FIXME  */
  5423.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
  5424.     return false;
  5425. #endif
  5426.  
  5427. #if 0 /* FIXME  */
  5428.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
  5429.     return false;
  5430. #endif
  5431.  
  5432.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
  5433.     return false;
  5434.  
  5435.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
  5436.     return false;
  5437.  
  5438.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
  5439.     return false;
  5440.  
  5441.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
  5442.     return false;
  5443.  
  5444.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
  5445.     return false;
  5446.  
  5447.       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
  5448.     return false;
  5449.  
  5450. #if 0 /* (SGI_COMPAT) */
  5451.       if (! bfd_get_section_by_name (dynobj, ".init"))
  5452.     if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
  5453.       return false;
  5454.  
  5455.       if (! bfd_get_section_by_name (dynobj, ".fini"))
  5456.     if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
  5457.       return false;
  5458. #endif
  5459.     }
  5460.  
  5461.   /* If we use dynamic linking, we generate a section symbol for each
  5462.      output section.  These are local symbols, which means that they
  5463.      must come first in the dynamic symbol table.
  5464.      That means we must increment the dynamic symbol index of every
  5465.      other dynamic symbol.  */
  5466.   {
  5467.     const char * const *namep;
  5468.     unsigned int c, i;
  5469.     bfd_size_type strindex;
  5470.     struct bfd_strtab_hash *dynstr;
  5471.     struct mips_got_info *g;
  5472.  
  5473.     c = 0;
  5474.     if (elf_hash_table (info)->dynamic_sections_created)
  5475.       {
  5476.     if (SGI_COMPAT (output_bfd))
  5477.       {
  5478.         c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
  5479.         elf_link_hash_traverse (elf_hash_table (info),
  5480.                     mips_elf_adjust_dynindx,
  5481.                     (PTR) &c);
  5482.         elf_hash_table (info)->dynsymcount += c;
  5483.  
  5484.         dynstr = elf_hash_table (info)->dynstr;
  5485.         BFD_ASSERT (dynstr != NULL);
  5486.  
  5487.         for (i = 1, namep = mips_elf_dynsym_sec_names;
  5488.          *namep != NULL;
  5489.          i++, namep++)
  5490.           {
  5491.         s = bfd_get_section_by_name (output_bfd, *namep);
  5492.         if (s != NULL)
  5493.           elf_section_data (s)->dynindx = i;
  5494.  
  5495.         strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
  5496.         if (strindex == (bfd_size_type) -1)
  5497.           return false;
  5498.  
  5499.         mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
  5500.           }
  5501.       }
  5502.     else
  5503.       {
  5504.         c = bfd_count_sections (output_bfd);
  5505.         elf_link_hash_traverse (elf_hash_table (info),
  5506.                     mips_elf_adjust_dynindx,
  5507.                     (PTR) &c);
  5508.         elf_hash_table (info)->dynsymcount += c;
  5509.  
  5510.         for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
  5511.           {
  5512.         elf_section_data (s)->dynindx = i;
  5513.         /* These symbols will have no names, so we don't need to
  5514.            fiddle with dynstr_index.  */
  5515.           }
  5516.       }
  5517.       }
  5518.  
  5519.     s = bfd_get_section_by_name (dynobj, ".got");
  5520.     BFD_ASSERT (s != NULL);
  5521.     BFD_ASSERT (elf_section_data (s) != NULL);
  5522.     g = (struct mips_got_info *) elf_section_data (s)->tdata;
  5523.     BFD_ASSERT (g != NULL);
  5524.  
  5525.     /* If there are no global got symbols, fake the last symbol so for
  5526.        safety.  */
  5527.     if (g->global_gotsym)
  5528.       g->global_gotsym += c;
  5529.     else
  5530.       g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
  5531.   }
  5532.  
  5533.   return true;
  5534. }
  5535.  
  5536. /* Increment the index of a dynamic symbol by a given amount.  Called
  5537.    via elf_link_hash_traverse.  */
  5538.  
  5539. static boolean
  5540. mips_elf_adjust_dynindx (h, cparg)
  5541.      struct elf_link_hash_entry *h;
  5542.      PTR cparg;
  5543. {
  5544.   unsigned int *cp = (unsigned int *) cparg;
  5545.  
  5546.   if (h->dynindx != -1)
  5547.     h->dynindx += *cp;
  5548.   return true;
  5549. }
  5550.  
  5551. /* Finish up dynamic symbol handling.  We set the contents of various
  5552.    dynamic sections here.  */
  5553.  
  5554. static boolean
  5555. mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
  5556.      bfd *output_bfd;
  5557.      struct bfd_link_info *info;
  5558.      struct elf_link_hash_entry *h;
  5559.      Elf_Internal_Sym *sym;
  5560. {
  5561.   bfd *dynobj;
  5562.   bfd_vma gval;
  5563.   asection *sgot;
  5564.   struct mips_got_info *g;
  5565.   const char *name;
  5566.  
  5567.   dynobj = elf_hash_table (info)->dynobj;
  5568.   gval = sym->st_value;
  5569.  
  5570.   if (h->plt_offset != (bfd_vma) -1)
  5571.     {
  5572.       asection *s;
  5573.       bfd_byte *p;
  5574.       bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
  5575.  
  5576.       /* This symbol has a stub.  Set it up.  */
  5577.  
  5578.       BFD_ASSERT (h->dynindx != -1);
  5579.  
  5580.       s = bfd_get_section_by_name (dynobj, ".stub");
  5581.       BFD_ASSERT (s != NULL);
  5582.  
  5583.       /* Fill the stub.  */
  5584.       p = stub;
  5585.       bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
  5586.       p += 4;
  5587.       bfd_put_32 (output_bfd, STUB_MOVE, p);
  5588.       p += 4;
  5589.  
  5590.       /* FIXME: Can h->dynindex be more than 64K?  */
  5591.       if (h->dynindx & 0xffff0000)
  5592.     return false;
  5593.  
  5594.       bfd_put_32 (output_bfd, STUB_JALR, p);
  5595.       p += 4;
  5596.       bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
  5597.  
  5598.       BFD_ASSERT (h->plt_offset <= s->_raw_size);
  5599.       memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
  5600.  
  5601.       /* Mark the symbol as undefined.  plt_offset != -1 occurs
  5602.      only for the referenced symbol.  */
  5603.       sym->st_shndx = SHN_UNDEF;
  5604.  
  5605.       /* The run-time linker uses the st_value field of the symbol
  5606.      to reset the global offset table entry for this external
  5607.      to its stub address when unlinking a shared object.  */
  5608.       gval = s->output_section->vma + s->output_offset + h->plt_offset;
  5609.       sym->st_value = gval;
  5610.     }
  5611.  
  5612.   BFD_ASSERT (h->dynindx != -1);
  5613.  
  5614.   sgot = bfd_get_section_by_name (dynobj, ".got");
  5615.   BFD_ASSERT (sgot != NULL);
  5616.   BFD_ASSERT (elf_section_data (sgot) != NULL);
  5617.   g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
  5618.   BFD_ASSERT (g != NULL);
  5619.  
  5620.   if ((unsigned long) h->dynindx >= g->global_gotsym)
  5621.     {
  5622.       bfd_size_type offset;
  5623.  
  5624.       /* This symbol has an entry in the global offset table.  Set its
  5625.      value to the corresponding got entry, if needed.  */
  5626.       if (h->got_offset == (bfd_vma) -1)
  5627.     {
  5628.       offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
  5629.       BFD_ASSERT (g->local_gotno * 4 <= offset
  5630.               && offset < sgot->_raw_size);
  5631.       bfd_put_32 (output_bfd, gval, sgot->contents + offset);
  5632.     }
  5633.     }
  5634.  
  5635.   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
  5636.   name = h->root.root.string;
  5637.   if (strcmp (name, "_DYNAMIC") == 0
  5638.       || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
  5639.     sym->st_shndx = SHN_ABS;
  5640.   else if (strcmp (name, "_DYNAMIC_LINK") == 0)
  5641.     {
  5642.       sym->st_shndx = SHN_ABS;
  5643.       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  5644.       sym->st_value = 1;
  5645.     }
  5646.   else if (SGI_COMPAT (output_bfd))
  5647.     {
  5648.       if (strcmp (name, "_gp_disp") == 0)
  5649.     {
  5650.       sym->st_shndx = SHN_ABS;
  5651.       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  5652.       sym->st_value = elf_gp (output_bfd);
  5653.     }
  5654.       else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
  5655.            || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
  5656.     {
  5657.       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  5658.       sym->st_other = STO_PROTECTED;
  5659.       sym->st_value = 0;
  5660.       sym->st_shndx = SHN_MIPS_DATA;
  5661.     }
  5662.       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
  5663.     {
  5664.       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
  5665.       sym->st_other = STO_PROTECTED;
  5666.       sym->st_value = mips_elf_hash_table (info)->procedure_count;
  5667.       sym->st_shndx = SHN_ABS;
  5668.     }
  5669.       else if (sym->st_shndx != SHN_UNDEF)
  5670.     {
  5671.       if (h->type == STT_FUNC)
  5672.         sym->st_shndx = SHN_MIPS_TEXT;
  5673.       else if (h->type == STT_OBJECT)
  5674.         sym->st_shndx = SHN_MIPS_DATA;
  5675.     }
  5676.     }
  5677.  
  5678.   if (SGI_COMPAT (output_bfd)
  5679.       && ! info->shared)
  5680.     {
  5681.       if (! mips_elf_hash_table (info)->use_rld_obj_head
  5682.       && strcmp (name, "__rld_map") == 0)
  5683.     {
  5684.       asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
  5685.       BFD_ASSERT (s != NULL);
  5686.       sym->st_value = s->output_section->vma + s->output_offset;
  5687.       bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
  5688.       if (mips_elf_hash_table (info)->rld_value == 0)
  5689.         mips_elf_hash_table (info)->rld_value = sym->st_value;
  5690.     }
  5691.       else if (mips_elf_hash_table (info)->use_rld_obj_head
  5692.            && strcmp (name, "__rld_obj_head") == 0)
  5693.     {
  5694.       asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
  5695.       BFD_ASSERT (s != NULL);
  5696.       mips_elf_hash_table (info)->rld_value = sym->st_value;
  5697.     }
  5698.     }
  5699.  
  5700.   return true;
  5701. }
  5702.  
  5703. /* Finish up the dynamic sections.  */
  5704.  
  5705. static boolean
  5706. mips_elf_finish_dynamic_sections (output_bfd, info)
  5707.      bfd *output_bfd;
  5708.      struct bfd_link_info *info;
  5709. {
  5710.   bfd *dynobj;
  5711.   asection *sdyn;
  5712.   asection *sgot;
  5713.   struct mips_got_info *g;
  5714.  
  5715.   dynobj = elf_hash_table (info)->dynobj;
  5716.  
  5717.   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  5718.  
  5719.   sgot = bfd_get_section_by_name (dynobj, ".got");
  5720.   BFD_ASSERT (sgot != NULL);
  5721.  
  5722.   BFD_ASSERT (elf_section_data (sgot) != NULL);
  5723.   g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
  5724.   BFD_ASSERT (g != NULL);
  5725.  
  5726.   if (elf_hash_table (info)->dynamic_sections_created)
  5727.     {
  5728.       Elf32_External_Dyn *dyncon, *dynconend;
  5729.  
  5730.       BFD_ASSERT (sdyn != NULL);
  5731.  
  5732.       dyncon = (Elf32_External_Dyn *) sdyn->contents;
  5733.       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  5734.       for (; dyncon < dynconend; dyncon++)
  5735.     {
  5736.       Elf_Internal_Dyn dyn;
  5737.       const char *name;
  5738.       size_t elemsize;
  5739.       asection *s;
  5740.  
  5741.       bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  5742.  
  5743.       switch (dyn.d_tag)
  5744.         {
  5745.         default:
  5746.           break;
  5747.  
  5748.         case DT_RELENT:
  5749.           s = bfd_get_section_by_name (dynobj, ".rel.dyn");
  5750.           BFD_ASSERT (s != NULL);
  5751.           dyn.d_un.d_val = sizeof (Elf32_External_Rel);
  5752.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5753.           break;
  5754.  
  5755.         case DT_STRSZ:
  5756.           /* Rewrite DT_STRSZ.  */
  5757.           dyn.d_un.d_val =
  5758.         _bfd_stringtab_size (elf_hash_table (info)->dynstr);
  5759.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5760.           break;
  5761.  
  5762.         case DT_PLTGOT:
  5763.           name = ".got";
  5764.           goto get_vma;
  5765.         case DT_MIPS_CONFLICT:
  5766.           name = ".conflict";
  5767.           goto get_vma;
  5768.         case DT_MIPS_LIBLIST:
  5769.           name = ".liblist";
  5770.         get_vma:
  5771.           s = bfd_get_section_by_name (output_bfd, name);
  5772.           BFD_ASSERT (s != NULL);
  5773.           dyn.d_un.d_ptr = s->vma;
  5774.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5775.           break;
  5776.  
  5777.         case DT_MIPS_RLD_VERSION:
  5778.           dyn.d_un.d_val = 1; /* XXX */
  5779.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5780.           break;
  5781.  
  5782.         case DT_MIPS_FLAGS:
  5783.           dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
  5784.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5785.           break;
  5786.  
  5787.         case DT_MIPS_CONFLICTNO:
  5788.           name = ".conflict";
  5789.           elemsize = sizeof (Elf32_Conflict);
  5790.           goto set_elemno;
  5791.  
  5792.         case DT_MIPS_LIBLISTNO:
  5793.           name = ".liblist";
  5794.           elemsize = sizeof (Elf32_Lib);
  5795.         set_elemno:
  5796.           s = bfd_get_section_by_name (output_bfd, name);
  5797.           if (s != NULL)
  5798.         {
  5799.           if (s->_cooked_size != 0)
  5800.             dyn.d_un.d_val = s->_cooked_size / elemsize;
  5801.           else
  5802.             dyn.d_un.d_val = s->_raw_size / elemsize;
  5803.         }
  5804.           else
  5805.             dyn.d_un.d_val = 0;
  5806.  
  5807.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5808.           break;
  5809.  
  5810.         case DT_MIPS_TIME_STAMP:
  5811.           time ((time_t *) &dyn.d_un.d_val);
  5812.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5813.           break;
  5814.           
  5815.         case DT_MIPS_ICHECKSUM:
  5816.           /* XXX FIXME: */
  5817.           break;
  5818.  
  5819.         case DT_MIPS_IVERSION:
  5820.           /* XXX FIXME: */
  5821.           break;
  5822.  
  5823.         case DT_MIPS_BASE_ADDRESS:
  5824.           s = output_bfd->sections;
  5825.           BFD_ASSERT (s != NULL);
  5826.           dyn.d_un.d_ptr = s->vma & ~(0xffff);
  5827.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5828.           break;
  5829.  
  5830.         case DT_MIPS_LOCAL_GOTNO:
  5831.           dyn.d_un.d_val = g->local_gotno;
  5832.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5833.           break;
  5834.  
  5835.         case DT_MIPS_SYMTABNO:
  5836.           name = ".dynsym";
  5837.           elemsize = sizeof (Elf32_External_Sym);
  5838.           s = bfd_get_section_by_name (output_bfd, name);
  5839.           BFD_ASSERT (s != NULL);
  5840.  
  5841.           if (s->_cooked_size != 0)
  5842.         dyn.d_un.d_val = s->_cooked_size / elemsize;
  5843.           else
  5844.         dyn.d_un.d_val = s->_raw_size / elemsize;
  5845.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5846.           break;
  5847.  
  5848.         case DT_MIPS_UNREFEXTNO:
  5849.           /* XXX FIXME: */
  5850.           dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
  5851.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5852.           break;
  5853.  
  5854.         case DT_MIPS_GOTSYM:
  5855.           dyn.d_un.d_val = g->global_gotsym;
  5856.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5857.           break;
  5858.  
  5859.         case DT_MIPS_HIPAGENO:
  5860.           dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
  5861.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5862.           break;
  5863.  
  5864.         case DT_MIPS_RLD_MAP:
  5865.           dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
  5866.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5867.           break;
  5868.  
  5869.         }
  5870.     }
  5871.     }
  5872.  
  5873.   /* The first entry of the global offset table will be filled at
  5874.      runtime. The second entry will be used by some runtime loaders.
  5875.      This isn't the case of Irix rld. */
  5876.   if (sgot->_raw_size > 0)
  5877.     {
  5878.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  5879.       bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
  5880.     }
  5881.  
  5882.   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  5883.  
  5884.   {
  5885.     asection *sdynsym;
  5886.     asection *s;
  5887.     unsigned int i;
  5888.     bfd_vma last;
  5889.     Elf_Internal_Sym sym;
  5890.     long dindx;
  5891.     const char *name;
  5892.     const char * const * namep = mips_elf_dynsym_sec_names;
  5893.     Elf32_compact_rel cpt;
  5894.  
  5895.     /* Set up the section symbols for the output sections. SGI sets
  5896.        the STT_NOTYPE attribute for these symbols.  Should we do so?  */
  5897.  
  5898.     sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
  5899.     if (sdynsym != NULL)
  5900.       {
  5901.     if (SGI_COMPAT (output_bfd))
  5902.       {
  5903.         sym.st_size = 0;
  5904.         sym.st_name = 0;
  5905.         sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
  5906.         sym.st_other = 0;
  5907.  
  5908.         i = 0;
  5909.         while ((name = *namep++) != NULL)
  5910.           {
  5911.         s = bfd_get_section_by_name (output_bfd, name);
  5912.         if (s != NULL)
  5913.           {
  5914.             sym.st_value = s->vma;
  5915.             dindx = elf_section_data (s)->dynindx;
  5916.             last = s->vma + s->_raw_size;
  5917.           }
  5918.         else
  5919.           {
  5920.             sym.st_value = last;
  5921.             dindx++;
  5922.           }
  5923.  
  5924.         sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
  5925.                 ? SHN_MIPS_TEXT
  5926.                 : SHN_MIPS_DATA);
  5927.         ++i;
  5928.         sym.st_name =
  5929.           mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
  5930.  
  5931.         bfd_elf32_swap_symbol_out (output_bfd, &sym,
  5932.                        (((Elf32_External_Sym *)
  5933.                          sdynsym->contents)
  5934.                         + dindx));
  5935.           }
  5936.  
  5937.         /* Set the sh_info field of the output .dynsym section to
  5938.            the index of the first global symbol.  */
  5939.         elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
  5940.           SIZEOF_MIPS_DYNSYM_SECNAMES;
  5941.       }
  5942.     else
  5943.       {
  5944.         sym.st_size = 0;
  5945.         sym.st_name = 0;
  5946.         sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  5947.         sym.st_other = 0;
  5948.  
  5949.         for (s = output_bfd->sections; s != NULL; s = s->next)
  5950.           {
  5951.         int indx;
  5952.  
  5953.         sym.st_value = s->vma;
  5954.  
  5955.         indx = elf_section_data (s)->this_idx;
  5956.         BFD_ASSERT (indx > 0);
  5957.         sym.st_shndx = indx;
  5958.  
  5959.         bfd_elf32_swap_symbol_out (output_bfd, &sym,
  5960.                        (((Elf32_External_Sym *)
  5961.                          sdynsym->contents)
  5962.                         + elf_section_data (s)->dynindx));
  5963.           }
  5964.  
  5965.         /* Set the sh_info field of the output .dynsym section to
  5966.            the index of the first global symbol.  */
  5967.         elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
  5968.           bfd_count_sections (output_bfd) + 1;
  5969.       }
  5970.       }
  5971.  
  5972.     if (SGI_COMPAT (output_bfd))
  5973.       {
  5974.     /* Write .compact_rel section out.  */
  5975.     s = bfd_get_section_by_name (dynobj, ".compact_rel");
  5976.     if (s != NULL)
  5977.       {
  5978.         cpt.id1 = 1;
  5979.         cpt.num = s->reloc_count;
  5980.         cpt.id2 = 2;
  5981.         cpt.offset = (s->output_section->filepos
  5982.               + sizeof (Elf32_External_compact_rel));
  5983.         cpt.reserved0 = 0;
  5984.         cpt.reserved1 = 0;
  5985.         bfd_elf32_swap_compact_rel_out (output_bfd, &cpt, 
  5986.                         ((Elf32_External_compact_rel *)
  5987.                          s->contents));
  5988.  
  5989.         /* Clean up a dummy stub function entry in .text.  */
  5990.         s = bfd_get_section_by_name (dynobj, ".stub");
  5991.         if (s != NULL)
  5992.           {
  5993.         file_ptr dummy_offset;
  5994.  
  5995.         BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
  5996.         dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
  5997.         memset (s->contents + dummy_offset, 0,
  5998.             MIPS_FUNCTION_STUB_SIZE);
  5999.           }
  6000.       }
  6001.       }
  6002.  
  6003.     /* Clean up a first relocation in .rel.dyn.  */
  6004.     s = bfd_get_section_by_name (dynobj, ".rel.dyn");
  6005.     if (s != NULL)
  6006.       memset (s->contents, 0, sizeof (Elf32_External_Rel));
  6007.   }
  6008.  
  6009.   return true;
  6010. }
  6011.  
  6012. /* This is almost identical to bfd_generic_get_... except that some
  6013.    MIPS relocations need to be handled specially.  Sigh.  */
  6014.  
  6015. static bfd_byte *
  6016. elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
  6017.                        relocateable, symbols)
  6018.      bfd *abfd;
  6019.      struct bfd_link_info *link_info;
  6020.      struct bfd_link_order *link_order;
  6021.      bfd_byte *data;
  6022.      boolean relocateable;
  6023.      asymbol **symbols;
  6024. {
  6025.   /* Get enough memory to hold the stuff */
  6026.   bfd *input_bfd = link_order->u.indirect.section->owner;
  6027.   asection *input_section = link_order->u.indirect.section;
  6028.  
  6029.   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  6030.   arelent **reloc_vector = NULL;
  6031.   long reloc_count;
  6032.  
  6033.   if (reloc_size < 0)
  6034.     goto error_return;
  6035.  
  6036.   reloc_vector = (arelent **) bfd_malloc (reloc_size);
  6037.   if (reloc_vector == NULL && reloc_size != 0)
  6038.     goto error_return;
  6039.  
  6040.   /* read in the section */
  6041.   if (!bfd_get_section_contents (input_bfd,
  6042.                  input_section,
  6043.                  (PTR) data,
  6044.                  0,
  6045.                  input_section->_raw_size))
  6046.     goto error_return;
  6047.  
  6048.   /* We're not relaxing the section, so just copy the size info */
  6049.   input_section->_cooked_size = input_section->_raw_size;
  6050.   input_section->reloc_done = true;
  6051.  
  6052.   reloc_count = bfd_canonicalize_reloc (input_bfd,
  6053.                     input_section,
  6054.                     reloc_vector,
  6055.                     symbols);
  6056.   if (reloc_count < 0)
  6057.     goto error_return;
  6058.  
  6059.   if (reloc_count > 0)
  6060.     {
  6061.       arelent **parent;
  6062.       /* for mips */
  6063.       int gp_found;
  6064.       bfd_vma gp = 0x12345678;    /* initialize just to shut gcc up */
  6065.  
  6066.       {
  6067.     struct bfd_hash_entry *h;
  6068.     struct bfd_link_hash_entry *lh;
  6069.     /* Skip all this stuff if we aren't mixing formats.  */
  6070.     if (abfd && input_bfd
  6071.         && abfd->xvec == input_bfd->xvec)
  6072.       lh = 0;
  6073.     else
  6074.       {
  6075.         h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
  6076.         lh = (struct bfd_link_hash_entry *) h;
  6077.       }
  6078.       lookup:
  6079.     if (lh)
  6080.       {
  6081.         switch (lh->type)
  6082.           {
  6083.           case bfd_link_hash_undefined:
  6084.           case bfd_link_hash_undefweak:
  6085.           case bfd_link_hash_common:
  6086.         gp_found = 0;
  6087.         break;
  6088.           case bfd_link_hash_defined:
  6089.           case bfd_link_hash_defweak:
  6090.         gp_found = 1;
  6091.         gp = lh->u.def.value;
  6092.         break;
  6093.           case bfd_link_hash_indirect:
  6094.           case bfd_link_hash_warning:
  6095.         lh = lh->u.i.link;
  6096.         /* @@FIXME  ignoring warning for now */
  6097.         goto lookup;
  6098.           case bfd_link_hash_new:
  6099.           default:
  6100.         abort ();
  6101.           }
  6102.       }
  6103.     else
  6104.       gp_found = 0;
  6105.       }
  6106.       /* end mips */
  6107.       for (parent = reloc_vector; *parent != (arelent *) NULL;
  6108.        parent++)
  6109.     {
  6110.       char *error_message = (char *) NULL;
  6111.       bfd_reloc_status_type r;
  6112.  
  6113.       /* Specific to MIPS: Deal with relocation types that require
  6114.          knowing the gp of the output bfd.  */
  6115.       asymbol *sym = *(*parent)->sym_ptr_ptr;
  6116.       if (bfd_is_abs_section (sym->section) && abfd)
  6117.         {
  6118.           /* The special_function wouldn't get called anyways.  */
  6119.         }
  6120.       else if (!gp_found)
  6121.         {
  6122.           /* The gp isn't there; let the special function code
  6123.          fall over on its own.  */
  6124.         }
  6125.       else if ((*parent)->howto->special_function
  6126.            == _bfd_mips_elf_gprel16_reloc)
  6127.         {
  6128.           /* bypass special_function call */
  6129.           r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
  6130.                    relocateable, (PTR) data, gp);
  6131.           goto skip_bfd_perform_relocation;
  6132.         }
  6133.       /* end mips specific stuff */
  6134.  
  6135.       r = bfd_perform_relocation (input_bfd,
  6136.                       *parent,
  6137.                       (PTR) data,
  6138.                       input_section,
  6139.                       relocateable ? abfd : (bfd *) NULL,
  6140.                       &error_message);
  6141.     skip_bfd_perform_relocation:
  6142.  
  6143.       if (relocateable)
  6144.         {
  6145.           asection *os = input_section->output_section;
  6146.  
  6147.           /* A partial link, so keep the relocs */
  6148.           os->orelocation[os->reloc_count] = *parent;
  6149.           os->reloc_count++;
  6150.         }
  6151.  
  6152.       if (r != bfd_reloc_ok)
  6153.         {
  6154.           switch (r)
  6155.         {
  6156.         case bfd_reloc_undefined:
  6157.           if (!((*link_info->callbacks->undefined_symbol)
  6158.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  6159.              input_bfd, input_section, (*parent)->address)))
  6160.             goto error_return;
  6161.           break;
  6162.         case bfd_reloc_dangerous:
  6163.           BFD_ASSERT (error_message != (char *) NULL);
  6164.           if (!((*link_info->callbacks->reloc_dangerous)
  6165.             (link_info, error_message, input_bfd, input_section,
  6166.              (*parent)->address)))
  6167.             goto error_return;
  6168.           break;
  6169.         case bfd_reloc_overflow:
  6170.           if (!((*link_info->callbacks->reloc_overflow)
  6171.             (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  6172.              (*parent)->howto->name, (*parent)->addend,
  6173.              input_bfd, input_section, (*parent)->address)))
  6174.             goto error_return;
  6175.           break;
  6176.         case bfd_reloc_outofrange:
  6177.         default:
  6178.           abort ();
  6179.           break;
  6180.         }
  6181.  
  6182.         }
  6183.     }
  6184.     }
  6185.   if (reloc_vector != NULL)
  6186.     free (reloc_vector);
  6187.   return data;
  6188.  
  6189. error_return:
  6190.   if (reloc_vector != NULL)
  6191.     free (reloc_vector);
  6192.   return NULL;
  6193. }
  6194. #define bfd_elf32_bfd_get_relocated_section_contents \
  6195.   elf32_mips_get_relocated_section_contents
  6196.  
  6197. /* ECOFF swapping routines.  These are used when dealing with the
  6198.    .mdebug section, which is in the ECOFF debugging format.  */
  6199. static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
  6200. {
  6201.   /* Symbol table magic number.  */
  6202.   magicSym,
  6203.   /* Alignment of debugging information.  E.g., 4.  */
  6204.   4,
  6205.   /* Sizes of external symbolic information.  */
  6206.   sizeof (struct hdr_ext),
  6207.   sizeof (struct dnr_ext),
  6208.   sizeof (struct pdr_ext),
  6209.   sizeof (struct sym_ext),
  6210.   sizeof (struct opt_ext),
  6211.   sizeof (struct fdr_ext),
  6212.   sizeof (struct rfd_ext),
  6213.   sizeof (struct ext_ext),
  6214.   /* Functions to swap in external symbolic data.  */
  6215.   ecoff_swap_hdr_in,
  6216.   ecoff_swap_dnr_in,
  6217.   ecoff_swap_pdr_in,
  6218.   ecoff_swap_sym_in,
  6219.   ecoff_swap_opt_in,
  6220.   ecoff_swap_fdr_in,
  6221.   ecoff_swap_rfd_in,
  6222.   ecoff_swap_ext_in,
  6223.   _bfd_ecoff_swap_tir_in,
  6224.   _bfd_ecoff_swap_rndx_in,
  6225.   /* Functions to swap out external symbolic data.  */
  6226.   ecoff_swap_hdr_out,
  6227.   ecoff_swap_dnr_out,
  6228.   ecoff_swap_pdr_out,
  6229.   ecoff_swap_sym_out,
  6230.   ecoff_swap_opt_out,
  6231.   ecoff_swap_fdr_out,
  6232.   ecoff_swap_rfd_out,
  6233.   ecoff_swap_ext_out,
  6234.   _bfd_ecoff_swap_tir_out,
  6235.   _bfd_ecoff_swap_rndx_out,
  6236.   /* Function to read in symbolic data.  */
  6237.   _bfd_mips_elf_read_ecoff_info
  6238. };
  6239.  
  6240. #define TARGET_LITTLE_SYM        bfd_elf32_littlemips_vec
  6241. #define TARGET_LITTLE_NAME        "elf32-littlemips"
  6242. #define TARGET_BIG_SYM            bfd_elf32_bigmips_vec
  6243. #define TARGET_BIG_NAME            "elf32-bigmips"
  6244. #define ELF_ARCH            bfd_arch_mips
  6245. #define ELF_MACHINE_CODE        EM_MIPS
  6246.  
  6247. /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
  6248.    a value of 0x1000, and we are compatible.  */
  6249. #define ELF_MAXPAGESIZE            0x1000
  6250.  
  6251. #define elf_backend_collect        true
  6252. #define elf_backend_type_change_ok    true
  6253. #define elf_info_to_howto        0
  6254. #define elf_info_to_howto_rel        mips_info_to_howto_rel
  6255. #define elf_backend_sym_is_global    mips_elf_sym_is_global
  6256. #define elf_backend_object_p        mips_elf32_object_p
  6257. #define elf_backend_section_from_shdr    mips_elf32_section_from_shdr
  6258. #define elf_backend_fake_sections    _bfd_mips_elf_fake_sections
  6259. #define elf_backend_section_from_bfd_section \
  6260.                     _bfd_mips_elf_section_from_bfd_section
  6261. #define elf_backend_section_processing    mips_elf32_section_processing
  6262. #define elf_backend_symbol_processing    _bfd_mips_elf_symbol_processing
  6263. #define elf_backend_additional_program_headers \
  6264.                     mips_elf_additional_program_headers
  6265. #define elf_backend_modify_segment_map    mips_elf_modify_segment_map
  6266. #define elf_backend_final_write_processing \
  6267.                     _bfd_mips_elf_final_write_processing
  6268. #define elf_backend_ecoff_debug_swap    &mips_elf32_ecoff_debug_swap
  6269.  
  6270. #define bfd_elf32_bfd_is_local_label    mips_elf_is_local_label
  6271. #define bfd_elf32_find_nearest_line    _bfd_mips_elf_find_nearest_line
  6272. #define bfd_elf32_set_section_contents    _bfd_mips_elf_set_section_contents
  6273. #define bfd_elf32_bfd_link_hash_table_create \
  6274.                     mips_elf_link_hash_table_create
  6275. #define bfd_elf32_bfd_final_link    mips_elf_final_link
  6276. #define bfd_elf32_bfd_copy_private_bfd_data \
  6277.                     _bfd_mips_elf_copy_private_bfd_data
  6278. #define bfd_elf32_bfd_merge_private_bfd_data \
  6279.                     _bfd_mips_elf_merge_private_bfd_data
  6280. #define bfd_elf32_bfd_set_private_flags    _bfd_mips_elf_set_private_flags
  6281. #define elf_backend_add_symbol_hook    mips_elf_add_symbol_hook
  6282. #define elf_backend_create_dynamic_sections \
  6283.                     mips_elf_create_dynamic_sections
  6284. #define elf_backend_check_relocs    mips_elf_check_relocs
  6285. #define elf_backend_adjust_dynamic_symbol \
  6286.                     mips_elf_adjust_dynamic_symbol
  6287. #define elf_backend_size_dynamic_sections \
  6288.                     mips_elf_size_dynamic_sections
  6289. #define elf_backend_relocate_section    mips_elf_relocate_section
  6290. #define elf_backend_finish_dynamic_symbol \
  6291.                     mips_elf_finish_dynamic_symbol
  6292. #define elf_backend_finish_dynamic_sections \
  6293.                     mips_elf_finish_dynamic_sections
  6294.  
  6295. #include "elf32-target.h"
  6296.