Back to index

cell-binutils  2.17cvs20070401
elfxx-sparc.c
Go to the documentation of this file.
00001 /* SPARC-specific support for ELF
00002    Copyright 2005, 2006, 2007 Free Software Foundation, Inc.
00003 
00004    This file is part of BFD, the Binary File Descriptor library.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00019 
00020 /* This file handles functionality common to the different SPARC ABI's.  */
00021 
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "bfdlink.h"
00025 #include "libbfd.h"
00026 #include "libiberty.h"
00027 #include "elf-bfd.h"
00028 #include "elf/sparc.h"
00029 #include "opcode/sparc.h"
00030 #include "elfxx-sparc.h"
00031 #include "elf-vxworks.h"
00032 
00033 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
00034 #define MINUS_ONE (~ (bfd_vma) 0)
00035 
00036 #define ABI_64_P(abfd) \
00037   (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
00038 
00039 /* The relocation "howto" table.  */
00040 
00041 /* Utility for performing the standard initial work of an instruction
00042    relocation.
00043    *PRELOCATION will contain the relocated item.
00044    *PINSN will contain the instruction from the input stream.
00045    If the result is `bfd_reloc_other' the caller can continue with
00046    performing the relocation.  Otherwise it must stop and return the
00047    value to its caller.  */
00048 
00049 static bfd_reloc_status_type
00050 init_insn_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
00051                PTR data, asection *input_section, bfd *output_bfd,
00052                bfd_vma *prelocation, bfd_vma *pinsn)
00053 {
00054   bfd_vma relocation;
00055   reloc_howto_type *howto = reloc_entry->howto;
00056 
00057   if (output_bfd != (bfd *) NULL
00058       && (symbol->flags & BSF_SECTION_SYM) == 0
00059       && (! howto->partial_inplace
00060          || reloc_entry->addend == 0))
00061     {
00062       reloc_entry->address += input_section->output_offset;
00063       return bfd_reloc_ok;
00064     }
00065 
00066   /* This works because partial_inplace is FALSE.  */
00067   if (output_bfd != NULL)
00068     return bfd_reloc_continue;
00069 
00070   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
00071     return bfd_reloc_outofrange;
00072 
00073   relocation = (symbol->value
00074               + symbol->section->output_section->vma
00075               + symbol->section->output_offset);
00076   relocation += reloc_entry->addend;
00077   if (howto->pc_relative)
00078     {
00079       relocation -= (input_section->output_section->vma
00080                    + input_section->output_offset);
00081       relocation -= reloc_entry->address;
00082     }
00083 
00084   *prelocation = relocation;
00085   *pinsn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
00086   return bfd_reloc_other;
00087 }
00088 
00089 /* For unsupported relocs.  */
00090 
00091 static bfd_reloc_status_type
00092 sparc_elf_notsup_reloc (bfd *abfd ATTRIBUTE_UNUSED,
00093                      arelent *reloc_entry ATTRIBUTE_UNUSED,
00094                      asymbol *symbol ATTRIBUTE_UNUSED,
00095                      PTR data ATTRIBUTE_UNUSED,
00096                      asection *input_section ATTRIBUTE_UNUSED,
00097                      bfd *output_bfd ATTRIBUTE_UNUSED,
00098                      char **error_message ATTRIBUTE_UNUSED)
00099 {
00100   return bfd_reloc_notsupported;
00101 }
00102 
00103 /* Handle the WDISP16 reloc.  */
00104 
00105 static bfd_reloc_status_type
00106 sparc_elf_wdisp16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
00107                       PTR data, asection *input_section, bfd *output_bfd,
00108                       char **error_message ATTRIBUTE_UNUSED)
00109 {
00110   bfd_vma relocation;
00111   bfd_vma insn;
00112   bfd_reloc_status_type status;
00113 
00114   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
00115                          input_section, output_bfd, &relocation, &insn);
00116   if (status != bfd_reloc_other)
00117     return status;
00118 
00119   insn &= ~ (bfd_vma) 0x303fff;
00120   insn |= (((relocation >> 2) & 0xc000) << 6) | ((relocation >> 2) & 0x3fff);
00121   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
00122 
00123   if ((bfd_signed_vma) relocation < - 0x40000
00124       || (bfd_signed_vma) relocation > 0x3ffff)
00125     return bfd_reloc_overflow;
00126   else
00127     return bfd_reloc_ok;
00128 }
00129 
00130 /* Handle the HIX22 reloc.  */
00131 
00132 static bfd_reloc_status_type
00133 sparc_elf_hix22_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
00134                      PTR data, asection *input_section, bfd *output_bfd,
00135                      char **error_message ATTRIBUTE_UNUSED)
00136 {
00137   bfd_vma relocation;
00138   bfd_vma insn;
00139   bfd_reloc_status_type status;
00140 
00141   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
00142                          input_section, output_bfd, &relocation, &insn);
00143   if (status != bfd_reloc_other)
00144     return status;
00145 
00146   relocation ^= MINUS_ONE;
00147   insn = (insn &~ (bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
00148   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
00149 
00150   if ((relocation & ~ (bfd_vma) 0xffffffff) != 0)
00151     return bfd_reloc_overflow;
00152   else
00153     return bfd_reloc_ok;
00154 }
00155 
00156 /* Handle the LOX10 reloc.  */
00157 
00158 static bfd_reloc_status_type
00159 sparc_elf_lox10_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
00160                      PTR data, asection *input_section, bfd *output_bfd,
00161                      char **error_message ATTRIBUTE_UNUSED)
00162 {
00163   bfd_vma relocation;
00164   bfd_vma insn;
00165   bfd_reloc_status_type status;
00166 
00167   status = init_insn_reloc (abfd, reloc_entry, symbol, data,
00168                          input_section, output_bfd, &relocation, &insn);
00169   if (status != bfd_reloc_other)
00170     return status;
00171 
00172   insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
00173   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
00174 
00175   return bfd_reloc_ok;
00176 }
00177 
00178 static reloc_howto_type _bfd_sparc_elf_howto_table[] =
00179 {
00180   HOWTO(R_SPARC_NONE,      0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
00181   HOWTO(R_SPARC_8,         0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       FALSE,0,0x000000ff,TRUE),
00182   HOWTO(R_SPARC_16,        0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      FALSE,0,0x0000ffff,TRUE),
00183   HOWTO(R_SPARC_32,        0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      FALSE,0,0xffffffff,TRUE),
00184   HOWTO(R_SPARC_DISP8,     0,0, 8,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   FALSE,0,0x000000ff,TRUE),
00185   HOWTO(R_SPARC_DISP16,    0,1,16,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  FALSE,0,0x0000ffff,TRUE),
00186   HOWTO(R_SPARC_DISP32,    0,2,32,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  FALSE,0,0xffffffff,TRUE),
00187   HOWTO(R_SPARC_WDISP30,   2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
00188   HOWTO(R_SPARC_WDISP22,   2,2,22,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
00189   HOWTO(R_SPARC_HI22,     10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    FALSE,0,0x003fffff,TRUE),
00190   HOWTO(R_SPARC_22,        0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      FALSE,0,0x003fffff,TRUE),
00191   HOWTO(R_SPARC_13,        0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      FALSE,0,0x00001fff,TRUE),
00192   HOWTO(R_SPARC_LO10,      0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    FALSE,0,0x000003ff,TRUE),
00193   HOWTO(R_SPARC_GOT10,     0,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   FALSE,0,0x000003ff,TRUE),
00194   HOWTO(R_SPARC_GOT13,     0,2,13,FALSE,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   FALSE,0,0x00001fff,TRUE),
00195   HOWTO(R_SPARC_GOT22,    10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   FALSE,0,0x003fffff,TRUE),
00196   HOWTO(R_SPARC_PC10,      0,2,10,TRUE, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    FALSE,0,0x000003ff,TRUE),
00197   HOWTO(R_SPARC_PC22,     10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    FALSE,0,0x003fffff,TRUE),
00198   HOWTO(R_SPARC_WPLT30,    2,2,30,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  FALSE,0,0x3fffffff,TRUE),
00199   HOWTO(R_SPARC_COPY,      0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    FALSE,0,0x00000000,TRUE),
00200   HOWTO(R_SPARC_GLOB_DAT,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GLOB_DAT",FALSE,0,0x00000000,TRUE),
00201   HOWTO(R_SPARC_JMP_SLOT,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_JMP_SLOT",FALSE,0,0x00000000,TRUE),
00202   HOWTO(R_SPARC_RELATIVE,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
00203   HOWTO(R_SPARC_UA32,      0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA32",    FALSE,0,0xffffffff,TRUE),
00204   HOWTO(R_SPARC_PLT32,     0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PLT32",   FALSE,0,0xffffffff,TRUE),
00205   HOWTO(R_SPARC_HIPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_HIPLT22",  FALSE,0,0x00000000,TRUE),
00206   HOWTO(R_SPARC_LOPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_LOPLT10",  FALSE,0,0x00000000,TRUE),
00207   HOWTO(R_SPARC_PCPLT32,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT32",  FALSE,0,0x00000000,TRUE),
00208   HOWTO(R_SPARC_PCPLT22,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT22",  FALSE,0,0x00000000,TRUE),
00209   HOWTO(R_SPARC_PCPLT10,   0,0,00,FALSE,0,complain_overflow_dont,    sparc_elf_notsup_reloc, "R_SPARC_PCPLT10",  FALSE,0,0x00000000,TRUE),
00210   HOWTO(R_SPARC_10,        0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      FALSE,0,0x000003ff,TRUE),
00211   HOWTO(R_SPARC_11,        0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      FALSE,0,0x000007ff,TRUE),
00212   HOWTO(R_SPARC_64,        0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_64",      FALSE,0,MINUS_ONE, TRUE),
00213   HOWTO(R_SPARC_OLO10,     0,2,13,FALSE,0,complain_overflow_signed,  sparc_elf_notsup_reloc, "R_SPARC_OLO10",   FALSE,0,0x00001fff,TRUE),
00214   HOWTO(R_SPARC_HH22,     42,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_HH22",    FALSE,0,0x003fffff,TRUE),
00215   HOWTO(R_SPARC_HM10,     32,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HM10",    FALSE,0,0x000003ff,TRUE),
00216   HOWTO(R_SPARC_LM22,     10,2,22,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LM22",    FALSE,0,0x003fffff,TRUE),
00217   HOWTO(R_SPARC_PC_HH22,  42,2,22,TRUE, 0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_PC_HH22",    FALSE,0,0x003fffff,TRUE),
00218   HOWTO(R_SPARC_PC_HM10,  32,2,10,TRUE, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC_HM10",    FALSE,0,0x000003ff,TRUE),
00219   HOWTO(R_SPARC_PC_LM22,  10,2,22,TRUE, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC_LM22",    FALSE,0,0x003fffff,TRUE),
00220   HOWTO(R_SPARC_WDISP16,   2,2,16,TRUE, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
00221   HOWTO(R_SPARC_WDISP19,   2,2,19,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
00222   HOWTO(R_SPARC_UNUSED_42, 0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_UNUSED_42",FALSE,0,0x00000000,TRUE),
00223   HOWTO(R_SPARC_7,         0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       FALSE,0,0x0000007f,TRUE),
00224   HOWTO(R_SPARC_5,         0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       FALSE,0,0x0000001f,TRUE),
00225   HOWTO(R_SPARC_6,         0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       FALSE,0,0x0000003f,TRUE),
00226   HOWTO(R_SPARC_DISP64,    0,4,64,TRUE, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP64",  FALSE,0,MINUS_ONE, TRUE),
00227   HOWTO(R_SPARC_PLT64,     0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PLT64",   FALSE,0,MINUS_ONE, TRUE),
00228   HOWTO(R_SPARC_HIX22,     0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,  "R_SPARC_HIX22",   FALSE,0,MINUS_ONE, FALSE),
00229   HOWTO(R_SPARC_LOX10,     0,4, 0,FALSE,0,complain_overflow_dont,    sparc_elf_lox10_reloc,  "R_SPARC_LOX10",   FALSE,0,MINUS_ONE, FALSE),
00230   HOWTO(R_SPARC_H44,      22,2,22,FALSE,0,complain_overflow_unsigned,bfd_elf_generic_reloc,  "R_SPARC_H44",     FALSE,0,0x003fffff,FALSE),
00231   HOWTO(R_SPARC_M44,      12,2,10,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_M44",     FALSE,0,0x000003ff,FALSE),
00232   HOWTO(R_SPARC_L44,       0,2,13,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_L44",     FALSE,0,0x00000fff,FALSE),
00233   HOWTO(R_SPARC_REGISTER,  0,4, 0,FALSE,0,complain_overflow_bitfield,sparc_elf_notsup_reloc, "R_SPARC_REGISTER",FALSE,0,MINUS_ONE, FALSE),
00234   HOWTO(R_SPARC_UA64,        0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA64",      FALSE,0,MINUS_ONE, TRUE),
00235   HOWTO(R_SPARC_UA16,        0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_UA16",      FALSE,0,0x0000ffff,TRUE),
00236   HOWTO(R_SPARC_TLS_GD_HI22,10,2,22,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_HI22",FALSE,0,0x003fffff,TRUE),
00237   HOWTO(R_SPARC_TLS_GD_LO10,0,2,10,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_LO10",FALSE,0,0x000003ff,TRUE),
00238   HOWTO(R_SPARC_TLS_GD_ADD,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_ADD",FALSE,0,0x00000000,TRUE),
00239   HOWTO(R_SPARC_TLS_GD_CALL,2,2,30,TRUE,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_TLS_GD_CALL",FALSE,0,0x3fffffff,TRUE),
00240   HOWTO(R_SPARC_TLS_LDM_HI22,10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_HI22",FALSE,0,0x003fffff,TRUE),
00241   HOWTO(R_SPARC_TLS_LDM_LO10,0,2,10,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_LO10",FALSE,0,0x000003ff,TRUE),
00242   HOWTO(R_SPARC_TLS_LDM_ADD,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_ADD",FALSE,0,0x00000000,TRUE),
00243   HOWTO(R_SPARC_TLS_LDM_CALL,2,2,30,TRUE,0,complain_overflow_signed, bfd_elf_generic_reloc,  "R_SPARC_TLS_LDM_CALL",FALSE,0,0x3fffffff,TRUE),
00244   HOWTO(R_SPARC_TLS_LDO_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc,"R_SPARC_TLS_LDO_HIX22",FALSE,0,0x003fffff, FALSE),
00245   HOWTO(R_SPARC_TLS_LDO_LOX10,0,2,0,FALSE,0,complain_overflow_dont,  sparc_elf_lox10_reloc,  "R_SPARC_TLS_LDO_LOX10",FALSE,0,0x000003ff, FALSE),
00246   HOWTO(R_SPARC_TLS_LDO_ADD,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_LDO_ADD",FALSE,0,0x00000000,TRUE),
00247   HOWTO(R_SPARC_TLS_IE_HI22,10,2,22,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_HI22",FALSE,0,0x003fffff,TRUE),
00248   HOWTO(R_SPARC_TLS_IE_LO10,0,2,10,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LO10",FALSE,0,0x000003ff,TRUE),
00249   HOWTO(R_SPARC_TLS_IE_LD,0,0, 0,FALSE,0,complain_overflow_dont,     bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LD",FALSE,0,0x00000000,TRUE),
00250   HOWTO(R_SPARC_TLS_IE_LDX,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_LDX",FALSE,0,0x00000000,TRUE),
00251   HOWTO(R_SPARC_TLS_IE_ADD,0,0, 0,FALSE,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_TLS_IE_ADD",FALSE,0,0x00000000,TRUE),
00252   HOWTO(R_SPARC_TLS_LE_HIX22,0,2,0,FALSE,0,complain_overflow_bitfield,sparc_elf_hix22_reloc, "R_SPARC_TLS_LE_HIX22",FALSE,0,0x003fffff, FALSE),
00253   HOWTO(R_SPARC_TLS_LE_LOX10,0,2,0,FALSE,0,complain_overflow_dont,   sparc_elf_lox10_reloc,  "R_SPARC_TLS_LE_LOX10",FALSE,0,0x000003ff, FALSE),
00254   HOWTO(R_SPARC_TLS_DTPMOD32,0,0, 0,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_DTPMOD32",FALSE,0,0x00000000,TRUE),
00255   HOWTO(R_SPARC_TLS_DTPMOD64,0,0, 0,FALSE,0,complain_overflow_dont,  bfd_elf_generic_reloc,  "R_SPARC_TLS_DTPMOD64",FALSE,0,0x00000000,TRUE),
00256   HOWTO(R_SPARC_TLS_DTPOFF32,0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF32",FALSE,0,0xffffffff,TRUE),
00257   HOWTO(R_SPARC_TLS_DTPOFF64,0,4,64,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_TLS_DTPOFF64",FALSE,0,MINUS_ONE,TRUE),
00258   HOWTO(R_SPARC_TLS_TPOFF32,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_TPOFF32",FALSE,0,0x00000000,TRUE),
00259   HOWTO(R_SPARC_TLS_TPOFF64,0,0, 0,FALSE,0,complain_overflow_dont,   bfd_elf_generic_reloc,  "R_SPARC_TLS_TPOFF64",FALSE,0,0x00000000,TRUE)
00260 };
00261 static reloc_howto_type sparc_vtinherit_howto =
00262   HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
00263 static reloc_howto_type sparc_vtentry_howto =
00264   HOWTO (R_SPARC_GNU_VTENTRY, 0,2,0,FALSE,0,complain_overflow_dont, _bfd_elf_rel_vtable_reloc_fn,"R_SPARC_GNU_VTENTRY", FALSE,0,0, FALSE);
00265 static reloc_howto_type sparc_rev32_howto =
00266   HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE);
00267 
00268 struct elf_reloc_map {
00269   bfd_reloc_code_real_type bfd_reloc_val;
00270   unsigned char elf_reloc_val;
00271 };
00272 
00273 static const struct elf_reloc_map sparc_reloc_map[] =
00274 {
00275   { BFD_RELOC_NONE, R_SPARC_NONE, },
00276   { BFD_RELOC_16, R_SPARC_16, },
00277   { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
00278   { BFD_RELOC_8, R_SPARC_8 },
00279   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
00280   { BFD_RELOC_CTOR, R_SPARC_64 },
00281   { BFD_RELOC_32, R_SPARC_32 },
00282   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
00283   { BFD_RELOC_HI22, R_SPARC_HI22 },
00284   { BFD_RELOC_LO10, R_SPARC_LO10, },
00285   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
00286   { BFD_RELOC_64_PCREL, R_SPARC_DISP64 },
00287   { BFD_RELOC_SPARC22, R_SPARC_22 },
00288   { BFD_RELOC_SPARC13, R_SPARC_13 },
00289   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
00290   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
00291   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
00292   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
00293   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
00294   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
00295   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
00296   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
00297   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
00298   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
00299   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
00300   { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
00301   { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
00302   { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
00303   { BFD_RELOC_SPARC_10, R_SPARC_10 },
00304   { BFD_RELOC_SPARC_11, R_SPARC_11 },
00305   { BFD_RELOC_SPARC_64, R_SPARC_64 },
00306   { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
00307   { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
00308   { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
00309   { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
00310   { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
00311   { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
00312   { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
00313   { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
00314   { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
00315   { BFD_RELOC_SPARC_7, R_SPARC_7 },
00316   { BFD_RELOC_SPARC_5, R_SPARC_5 },
00317   { BFD_RELOC_SPARC_6, R_SPARC_6 },
00318   { BFD_RELOC_SPARC_DISP64, R_SPARC_DISP64 },
00319   { BFD_RELOC_SPARC_TLS_GD_HI22, R_SPARC_TLS_GD_HI22 },
00320   { BFD_RELOC_SPARC_TLS_GD_LO10, R_SPARC_TLS_GD_LO10 },
00321   { BFD_RELOC_SPARC_TLS_GD_ADD, R_SPARC_TLS_GD_ADD },
00322   { BFD_RELOC_SPARC_TLS_GD_CALL, R_SPARC_TLS_GD_CALL },
00323   { BFD_RELOC_SPARC_TLS_LDM_HI22, R_SPARC_TLS_LDM_HI22 },
00324   { BFD_RELOC_SPARC_TLS_LDM_LO10, R_SPARC_TLS_LDM_LO10 },
00325   { BFD_RELOC_SPARC_TLS_LDM_ADD, R_SPARC_TLS_LDM_ADD },
00326   { BFD_RELOC_SPARC_TLS_LDM_CALL, R_SPARC_TLS_LDM_CALL },
00327   { BFD_RELOC_SPARC_TLS_LDO_HIX22, R_SPARC_TLS_LDO_HIX22 },
00328   { BFD_RELOC_SPARC_TLS_LDO_LOX10, R_SPARC_TLS_LDO_LOX10 },
00329   { BFD_RELOC_SPARC_TLS_LDO_ADD, R_SPARC_TLS_LDO_ADD },
00330   { BFD_RELOC_SPARC_TLS_IE_HI22, R_SPARC_TLS_IE_HI22 },
00331   { BFD_RELOC_SPARC_TLS_IE_LO10, R_SPARC_TLS_IE_LO10 },
00332   { BFD_RELOC_SPARC_TLS_IE_LD, R_SPARC_TLS_IE_LD },
00333   { BFD_RELOC_SPARC_TLS_IE_LDX, R_SPARC_TLS_IE_LDX },
00334   { BFD_RELOC_SPARC_TLS_IE_ADD, R_SPARC_TLS_IE_ADD },
00335   { BFD_RELOC_SPARC_TLS_LE_HIX22, R_SPARC_TLS_LE_HIX22 },
00336   { BFD_RELOC_SPARC_TLS_LE_LOX10, R_SPARC_TLS_LE_LOX10 },
00337   { BFD_RELOC_SPARC_TLS_DTPMOD32, R_SPARC_TLS_DTPMOD32 },
00338   { BFD_RELOC_SPARC_TLS_DTPMOD64, R_SPARC_TLS_DTPMOD64 },
00339   { BFD_RELOC_SPARC_TLS_DTPOFF32, R_SPARC_TLS_DTPOFF32 },
00340   { BFD_RELOC_SPARC_TLS_DTPOFF64, R_SPARC_TLS_DTPOFF64 },
00341   { BFD_RELOC_SPARC_TLS_TPOFF32, R_SPARC_TLS_TPOFF32 },
00342   { BFD_RELOC_SPARC_TLS_TPOFF64, R_SPARC_TLS_TPOFF64 },
00343   { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
00344   { BFD_RELOC_SPARC_PLT64, R_SPARC_PLT64 },
00345   { BFD_RELOC_SPARC_HIX22, R_SPARC_HIX22 },
00346   { BFD_RELOC_SPARC_LOX10, R_SPARC_LOX10 },
00347   { BFD_RELOC_SPARC_H44, R_SPARC_H44 },
00348   { BFD_RELOC_SPARC_M44, R_SPARC_M44 },
00349   { BFD_RELOC_SPARC_L44, R_SPARC_L44 },
00350   { BFD_RELOC_SPARC_REGISTER, R_SPARC_REGISTER },
00351   { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
00352   { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
00353   { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
00354 };
00355 
00356 reloc_howto_type *
00357 _bfd_sparc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00358                               bfd_reloc_code_real_type code)
00359 {
00360   unsigned int i;
00361 
00362   switch (code)
00363     {
00364     case BFD_RELOC_VTABLE_INHERIT:
00365       return &sparc_vtinherit_howto;
00366 
00367     case BFD_RELOC_VTABLE_ENTRY:
00368       return &sparc_vtentry_howto;
00369 
00370     case BFD_RELOC_SPARC_REV32:
00371       return &sparc_rev32_howto;
00372 
00373     default:
00374       for (i = 0;
00375           i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map);
00376           i++)
00377        {
00378          if (sparc_reloc_map[i].bfd_reloc_val == code)
00379            return (_bfd_sparc_elf_howto_table
00380                   + (int) sparc_reloc_map[i].elf_reloc_val);
00381        }
00382     }
00383     bfd_set_error (bfd_error_bad_value);
00384     return NULL;
00385 }
00386 
00387 reloc_howto_type *
00388 _bfd_sparc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00389                               const char *r_name)
00390 {
00391   unsigned int i;
00392 
00393   for (i = 0;
00394        i < (sizeof (_bfd_sparc_elf_howto_table)
00395            / sizeof (_bfd_sparc_elf_howto_table[0]));
00396        i++)
00397     if (_bfd_sparc_elf_howto_table[i].name != NULL
00398        && strcasecmp (_bfd_sparc_elf_howto_table[i].name, r_name) == 0)
00399       return &_bfd_sparc_elf_howto_table[i];
00400 
00401   if (strcasecmp (sparc_vtinherit_howto.name, r_name) == 0)
00402     return &sparc_vtinherit_howto;
00403   if (strcasecmp (sparc_vtentry_howto.name, r_name) == 0)
00404     return &sparc_vtentry_howto;
00405   if (strcasecmp (sparc_rev32_howto.name, r_name) == 0)
00406     return &sparc_rev32_howto;
00407 
00408   return NULL;
00409 }
00410 
00411 reloc_howto_type *
00412 _bfd_sparc_elf_info_to_howto_ptr (unsigned int r_type)
00413 {
00414   switch (r_type)
00415     {
00416     case R_SPARC_GNU_VTINHERIT:
00417       return &sparc_vtinherit_howto;
00418 
00419     case R_SPARC_GNU_VTENTRY:
00420       return &sparc_vtentry_howto;
00421 
00422     case R_SPARC_REV32:
00423       return &sparc_rev32_howto;
00424 
00425     default:
00426       if (r_type >= (unsigned int) R_SPARC_max_std)
00427        {
00428          (*_bfd_error_handler) (_("invalid relocation type %d"),
00429                              (int) r_type);
00430          r_type = R_SPARC_NONE;
00431        }
00432       return &_bfd_sparc_elf_howto_table[r_type];
00433     }
00434 }
00435 
00436 /* Both 32-bit and 64-bit sparc encode this in an identical manner,
00437    so just take advantage of that.  */
00438 #define SPARC_ELF_R_TYPE(r_info)   \
00439        ((r_info) & 0xff)
00440 
00441 void
00442 _bfd_sparc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
00443                            Elf_Internal_Rela *dst)
00444 {
00445   unsigned int r_type = SPARC_ELF_R_TYPE (dst->r_info);
00446 
00447   cache_ptr->howto = _bfd_sparc_elf_info_to_howto_ptr (r_type);
00448 }
00449 
00450 
00451 /* The nop opcode we use.  */
00452 #define SPARC_NOP 0x01000000
00453 
00454 #define SPARC_INSN_BYTES    4
00455 
00456 /* The SPARC linker needs to keep track of the number of relocs that it
00457    decides to copy as dynamic relocs in check_relocs for each symbol.
00458    This is so that it can later discard them if they are found to be
00459    unnecessary.  We store the information in a field extending the
00460    regular ELF linker hash table.  */
00461 
00462 struct _bfd_sparc_elf_dyn_relocs
00463 {
00464   struct _bfd_sparc_elf_dyn_relocs *next;
00465 
00466   /* The input section of the reloc.  */
00467   asection *sec;
00468 
00469   /* Total number of relocs copied for the input section.  */
00470   bfd_size_type count;
00471 
00472   /* Number of pc-relative relocs copied for the input section.  */
00473   bfd_size_type pc_count;
00474 };
00475 
00476 /* SPARC ELF linker hash entry.  */
00477 
00478 struct _bfd_sparc_elf_link_hash_entry
00479 {
00480   struct elf_link_hash_entry elf;
00481 
00482   /* Track dynamic relocs copied for this symbol.  */
00483   struct _bfd_sparc_elf_dyn_relocs *dyn_relocs;
00484 
00485 #define GOT_UNKNOWN     0
00486 #define GOT_NORMAL      1
00487 #define GOT_TLS_GD      2
00488 #define GOT_TLS_IE      3
00489   unsigned char tls_type;
00490 };
00491 
00492 #define _bfd_sparc_elf_hash_entry(ent) ((struct _bfd_sparc_elf_link_hash_entry *)(ent))
00493 
00494 struct _bfd_sparc_elf_obj_tdata
00495 {
00496   struct elf_obj_tdata root;
00497 
00498   /* tls_type for each local got entry.  */
00499   char *local_got_tls_type;
00500 
00501   /* TRUE if TLS GD relocs has been seen for this object.  */
00502   bfd_boolean has_tlsgd;
00503 };
00504 
00505 #define _bfd_sparc_elf_tdata(abfd) \
00506   ((struct _bfd_sparc_elf_obj_tdata *) (abfd)->tdata.any)
00507 
00508 #define _bfd_sparc_elf_local_got_tls_type(abfd) \
00509   (_bfd_sparc_elf_tdata (abfd)->local_got_tls_type)
00510 
00511 bfd_boolean
00512 _bfd_sparc_elf_mkobject (bfd *abfd)
00513 {
00514   if (abfd->tdata.any == NULL)
00515     {
00516       bfd_size_type amt = sizeof (struct _bfd_sparc_elf_obj_tdata);
00517       abfd->tdata.any = bfd_zalloc (abfd, amt);
00518       if (abfd->tdata.any == NULL)
00519        return FALSE;
00520     }
00521   return bfd_elf_mkobject (abfd);
00522 }
00523 
00524 static void
00525 sparc_put_word_32 (bfd *bfd, bfd_vma val, void *ptr)
00526 {
00527   bfd_put_32 (bfd, val, ptr);
00528 }
00529 
00530 static void
00531 sparc_put_word_64 (bfd *bfd, bfd_vma val, void *ptr)
00532 {
00533   bfd_put_64 (bfd, val, ptr);
00534 }
00535 
00536 static void
00537 sparc_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
00538 {
00539   const struct elf_backend_data *bed;
00540   bfd_byte *loc;
00541 
00542   bed = get_elf_backend_data (abfd);
00543   loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
00544   bed->s->swap_reloca_out (abfd, rel, loc);
00545 }
00546 
00547 static bfd_vma
00548 sparc_elf_r_info_64 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
00549                    bfd_vma index ATTRIBUTE_UNUSED,
00550                    bfd_vma type ATTRIBUTE_UNUSED)
00551 {
00552   return ELF64_R_INFO (index,
00553                      (in_rel ?
00554                      ELF64_R_TYPE_INFO (ELF64_R_TYPE_DATA (in_rel->r_info),
00555                                       type) : type));
00556 }
00557 
00558 static bfd_vma
00559 sparc_elf_r_info_32 (Elf_Internal_Rela *in_rel ATTRIBUTE_UNUSED,
00560                    bfd_vma index, bfd_vma type)
00561 {
00562   return ELF32_R_INFO (index, type);
00563 }
00564 
00565 static bfd_vma
00566 sparc_elf_r_symndx_64 (bfd_vma r_info)
00567 {
00568   bfd_vma r_symndx = ELF32_R_SYM (r_info);
00569   return (r_symndx >> 24);
00570 }
00571 
00572 static bfd_vma
00573 sparc_elf_r_symndx_32 (bfd_vma r_info)
00574 {
00575   return ELF32_R_SYM (r_info);
00576 }
00577 
00578 /* PLT/GOT stuff */
00579 
00580 #define PLT32_ENTRY_SIZE 12
00581 #define PLT32_HEADER_SIZE   (4 * PLT32_ENTRY_SIZE)
00582 
00583 /* The first four entries in a 32-bit procedure linkage table are reserved,
00584    and the initial contents are unimportant (we zero them out).
00585    Subsequent entries look like this.  See the SVR4 ABI SPARC
00586    supplement to see how this works.  */
00587 
00588 /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
00589 #define PLT32_ENTRY_WORD0 0x03000000
00590 /* b,a .plt0.  We fill in the offset later.  */
00591 #define PLT32_ENTRY_WORD1 0x30800000
00592 /* nop.  */
00593 #define PLT32_ENTRY_WORD2 SPARC_NOP
00594 
00595 static int
00596 sparc32_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
00597                       bfd_vma max ATTRIBUTE_UNUSED,
00598                       bfd_vma *r_offset)
00599 {
00600       bfd_put_32 (output_bfd,
00601                 PLT32_ENTRY_WORD0 + offset,
00602                 splt->contents + offset);
00603       bfd_put_32 (output_bfd,
00604                 (PLT32_ENTRY_WORD1
00605                  + (((- (offset + 4)) >> 2) & 0x3fffff)),
00606                 splt->contents + offset + 4);
00607       bfd_put_32 (output_bfd, (bfd_vma) PLT32_ENTRY_WORD2,
00608                 splt->contents + offset + 8);
00609 
00610       *r_offset = offset;
00611 
00612       return offset / PLT32_ENTRY_SIZE - 4;
00613 }
00614 
00615 /* Both the headers and the entries are icache aligned.  */
00616 #define PLT64_ENTRY_SIZE    32
00617 #define PLT64_HEADER_SIZE   (4 * PLT64_ENTRY_SIZE)
00618 #define PLT64_LARGE_THRESHOLD      32768
00619 
00620 static int
00621 sparc64_plt_entry_build (bfd *output_bfd, asection *splt, bfd_vma offset,
00622                       bfd_vma max, bfd_vma *r_offset)
00623 {
00624   unsigned char *entry = splt->contents + offset;
00625   const unsigned int nop = SPARC_NOP;
00626   int index;
00627 
00628   if (offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
00629     {
00630       unsigned int sethi, ba;
00631 
00632       *r_offset = offset;
00633 
00634       index = (offset / PLT64_ENTRY_SIZE);
00635 
00636       sethi = 0x03000000 | (index * PLT64_ENTRY_SIZE);
00637       ba = 0x30680000
00638        | (((splt->contents + PLT64_ENTRY_SIZE) - (entry + 4)) / 4 & 0x7ffff);
00639 
00640       bfd_put_32 (output_bfd, (bfd_vma) sethi, entry);
00641       bfd_put_32 (output_bfd, (bfd_vma) ba,    entry + 4);
00642       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 8);
00643       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 12);
00644       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 16);
00645       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 20);
00646       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 24);
00647       bfd_put_32 (output_bfd, (bfd_vma) nop,   entry + 28);
00648     }
00649   else
00650     {
00651       unsigned char *ptr;
00652       unsigned int ldx;
00653       int block, last_block, ofs, last_ofs, chunks_this_block;
00654       const int insn_chunk_size = (6 * 4);
00655       const int ptr_chunk_size = (1 * 8);
00656       const int entries_per_block = 160;
00657       const int block_size = entries_per_block * (insn_chunk_size
00658                                             + ptr_chunk_size);
00659 
00660       /* Entries 32768 and higher are grouped into blocks of 160.
00661         The blocks are further subdivided into 160 sequences of
00662         6 instructions and 160 pointers.  If a block does not require
00663         the full 160 entries, let's say it requires N, then there
00664         will be N sequences of 6 instructions and N pointers.  */
00665 
00666       offset -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
00667       max -= (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE);
00668 
00669       block = offset / block_size;
00670       last_block = max / block_size;
00671       if (block != last_block)
00672        {
00673          chunks_this_block = 160;
00674        }
00675       else
00676        {
00677          last_ofs = max % block_size;
00678          chunks_this_block = last_ofs / (insn_chunk_size + ptr_chunk_size);
00679        }
00680 
00681       ofs = offset % block_size;
00682 
00683       index = (PLT64_LARGE_THRESHOLD +
00684               (block * 160) +
00685               (ofs / insn_chunk_size));
00686 
00687       ptr = splt->contents
00688        + (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
00689        + (block * block_size)
00690        + (chunks_this_block * insn_chunk_size)
00691        + (ofs / insn_chunk_size) * ptr_chunk_size;
00692 
00693       *r_offset = (bfd_vma) (ptr - splt->contents);
00694 
00695       ldx = 0xc25be000 | ((ptr - (entry+4)) & 0x1fff);
00696 
00697       /* mov %o7,%g5
00698         call .+8
00699         nop
00700         ldx [%o7+P],%g1
00701         jmpl %o7+%g1,%g1
00702         mov %g5,%o7  */
00703       bfd_put_32 (output_bfd, (bfd_vma) 0x8a10000f, entry);
00704       bfd_put_32 (output_bfd, (bfd_vma) 0x40000002, entry + 4);
00705       bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,  entry + 8);
00706       bfd_put_32 (output_bfd, (bfd_vma) ldx,        entry + 12);
00707       bfd_put_32 (output_bfd, (bfd_vma) 0x83c3c001, entry + 16);
00708       bfd_put_32 (output_bfd, (bfd_vma) 0x9e100005, entry + 20);
00709 
00710       bfd_put_64 (output_bfd, (bfd_vma) (splt->contents - (entry + 4)), ptr);
00711     }
00712 
00713   return index - 4;
00714 }
00715 
00716 /* The format of the first PLT entry in a VxWorks executable.  */
00717 static const bfd_vma sparc_vxworks_exec_plt0_entry[] =
00718   {
00719     0x05000000,      /* sethi  %hi(_GLOBAL_OFFSET_TABLE_+8), %g2 */
00720     0x8410a000,      /* or     %g2, %lo(_GLOBAL_OFFSET_TABLE_+8), %g2 */
00721     0xc4008000,      /* ld     [ %g2 ], %g2 */
00722     0x81c08000,      /* jmp    %g2 */
00723     0x01000000       /* nop */
00724   };
00725 
00726 /* The format of subsequent PLT entries.  */
00727 static const bfd_vma sparc_vxworks_exec_plt_entry[] =
00728   {
00729     0x03000000,      /* sethi  %hi(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
00730     0x82106000,      /* or     %g1, %lo(_GLOBAL_OFFSET_TABLE_+f@got), %g1 */
00731     0xc2004000,      /* ld     [ %g1 ], %g1 */
00732     0x81c04000,      /* jmp    %g1 */
00733     0x01000000,      /* nop */
00734     0x03000000,      /* sethi  %hi(f@pltindex), %g1 */
00735     0x10800000,      /* b      _PLT_resolve */
00736     0x82106000       /* or     %g1, %lo(f@pltindex), %g1 */
00737   };
00738 
00739 /* The format of the first PLT entry in a VxWorks shared object.  */
00740 static const bfd_vma sparc_vxworks_shared_plt0_entry[] =
00741   {
00742     0xc405e008,      /* ld     [ %l7 + 8 ], %g2 */
00743     0x81c08000,      /* jmp    %g2 */
00744     0x01000000       /* nop */
00745   };
00746 
00747 /* The format of subsequent PLT entries.  */
00748 static const bfd_vma sparc_vxworks_shared_plt_entry[] =
00749   {
00750     0x03000000,      /* sethi  %hi(f@got), %g1 */
00751     0x82106000,      /* or     %g1, %lo(f@got), %g1 */
00752     0xc205c001,      /* ld     [ %l7 + %g1 ], %g1 */
00753     0x81c04000,      /* jmp    %g1 */
00754     0x01000000,      /* nop */
00755     0x03000000,      /* sethi  %hi(f@pltindex), %g1 */
00756     0x10800000,      /* b      _PLT_resolve */
00757     0x82106000       /* or     %g1, %lo(f@pltindex), %g1 */
00758   };
00759 
00760 #define SPARC_ELF_PUT_WORD(htab, bfd, val, ptr)  \
00761        htab->put_word(bfd, val, ptr)
00762 
00763 #define SPARC_ELF_R_INFO(htab, in_rel, index, type)     \
00764        htab->r_info(in_rel, index, type)
00765 
00766 #define SPARC_ELF_R_SYMNDX(htab, r_info)  \
00767        htab->r_symndx(r_info)
00768 
00769 #define SPARC_ELF_WORD_BYTES(htab) \
00770        htab->bytes_per_word
00771 
00772 #define SPARC_ELF_RELA_BYTES(htab) \
00773        htab->bytes_per_rela
00774 
00775 #define SPARC_ELF_DTPOFF_RELOC(htab)      \
00776        htab->dtpoff_reloc
00777 
00778 #define SPARC_ELF_DTPMOD_RELOC(htab)      \
00779        htab->dtpmod_reloc
00780 
00781 #define SPARC_ELF_TPOFF_RELOC(htab)       \
00782        htab->tpoff_reloc
00783 
00784 #define SPARC_ELF_BUILD_PLT_ENTRY(htab, obfd, splt, off, max, r_off) \
00785        htab->build_plt_entry (obfd, splt, off, max, r_off)
00786 
00787 /* Create an entry in an SPARC ELF linker hash table.  */
00788 
00789 static struct bfd_hash_entry *
00790 link_hash_newfunc (struct bfd_hash_entry *entry,
00791                  struct bfd_hash_table *table, const char *string)
00792 {
00793   /* Allocate the structure if it has not already been allocated by a
00794      subclass.  */
00795   if (entry == NULL)
00796     {
00797       entry = bfd_hash_allocate (table,
00798                              sizeof (struct _bfd_sparc_elf_link_hash_entry));
00799       if (entry == NULL)
00800        return entry;
00801     }
00802 
00803   /* Call the allocation method of the superclass.  */
00804   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
00805   if (entry != NULL)
00806     {
00807       struct _bfd_sparc_elf_link_hash_entry *eh;
00808 
00809       eh = (struct _bfd_sparc_elf_link_hash_entry *) entry;
00810       eh->dyn_relocs = NULL;
00811       eh->tls_type = GOT_UNKNOWN;
00812     }
00813 
00814   return entry;
00815 }
00816 
00817 /* The name of the dynamic interpreter.  This is put in the .interp
00818    section.  */
00819 
00820 #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
00821 #define ELF64_DYNAMIC_INTERPRETER "/usr/lib/sparcv9/ld.so.1"
00822 
00823 /* Create a SPARC ELF linker hash table.  */
00824 
00825 struct bfd_link_hash_table *
00826 _bfd_sparc_elf_link_hash_table_create (bfd *abfd)
00827 {
00828   struct _bfd_sparc_elf_link_hash_table *ret;
00829   bfd_size_type amt = sizeof (struct _bfd_sparc_elf_link_hash_table);
00830 
00831   ret = (struct _bfd_sparc_elf_link_hash_table *) bfd_zmalloc (amt);
00832   if (ret == NULL)
00833     return NULL;
00834 
00835   if (ABI_64_P (abfd))
00836     {
00837       ret->put_word = sparc_put_word_64;
00838       ret->r_info = sparc_elf_r_info_64;
00839       ret->r_symndx = sparc_elf_r_symndx_64;
00840       ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF64;
00841       ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD64;
00842       ret->tpoff_reloc = R_SPARC_TLS_TPOFF64;
00843       ret->word_align_power = 3;
00844       ret->align_power_max = 4;
00845       ret->bytes_per_word = 8;
00846       ret->bytes_per_rela = sizeof (Elf64_External_Rela);
00847       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
00848       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
00849     }
00850   else
00851     {
00852       ret->put_word = sparc_put_word_32;
00853       ret->r_info = sparc_elf_r_info_32;
00854       ret->r_symndx = sparc_elf_r_symndx_32;
00855       ret->dtpoff_reloc = R_SPARC_TLS_DTPOFF32;
00856       ret->dtpmod_reloc = R_SPARC_TLS_DTPMOD32;
00857       ret->tpoff_reloc = R_SPARC_TLS_TPOFF32;
00858       ret->word_align_power = 2;
00859       ret->align_power_max = 3;
00860       ret->bytes_per_word = 4;
00861       ret->bytes_per_rela = sizeof (Elf32_External_Rela);
00862       ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
00863       ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
00864     }
00865 
00866   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
00867                                   sizeof (struct _bfd_sparc_elf_link_hash_entry)))
00868     {
00869       free (ret);
00870       return NULL;
00871     }
00872 
00873   return &ret->elf.root;
00874 }
00875 
00876 /* Create .got and .rela.got sections in DYNOBJ, and set up
00877    shortcuts to them in our hash table.  */
00878 
00879 static bfd_boolean
00880 create_got_section (bfd *dynobj, struct bfd_link_info *info)
00881 {
00882   struct _bfd_sparc_elf_link_hash_table *htab;
00883 
00884   if (! _bfd_elf_create_got_section (dynobj, info))
00885     return FALSE;
00886 
00887   htab = _bfd_sparc_elf_hash_table (info);
00888   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
00889   BFD_ASSERT (htab->sgot != NULL);
00890 
00891   htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
00892                                           SEC_ALLOC
00893                                           | SEC_LOAD
00894                                           | SEC_HAS_CONTENTS
00895                                           | SEC_IN_MEMORY
00896                                           | SEC_LINKER_CREATED
00897                                           | SEC_READONLY);
00898   if (htab->srelgot == NULL
00899       || ! bfd_set_section_alignment (dynobj, htab->srelgot,
00900                                   htab->word_align_power))
00901     return FALSE;
00902 
00903   if (htab->is_vxworks)
00904     {
00905       htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
00906       if (!htab->sgotplt)
00907        return FALSE;
00908     }
00909 
00910   return TRUE;
00911 }
00912 
00913 /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
00914    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
00915    hash table.  */
00916 
00917 bfd_boolean
00918 _bfd_sparc_elf_create_dynamic_sections (bfd *dynobj,
00919                                    struct bfd_link_info *info)
00920 {
00921   struct _bfd_sparc_elf_link_hash_table *htab;
00922 
00923   htab = _bfd_sparc_elf_hash_table (info);
00924   if (!htab->sgot && !create_got_section (dynobj, info))
00925     return FALSE;
00926 
00927   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
00928     return FALSE;
00929 
00930   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
00931   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
00932   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
00933   if (!info->shared)
00934     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
00935 
00936   if (htab->is_vxworks)
00937     {
00938       if (!elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
00939        return FALSE;
00940       if (info->shared)
00941        {
00942          htab->plt_header_size
00943            = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt0_entry);
00944          htab->plt_entry_size
00945            = 4 * ARRAY_SIZE (sparc_vxworks_shared_plt_entry);
00946        }
00947       else
00948        {
00949          htab->plt_header_size
00950            = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt0_entry);
00951          htab->plt_entry_size
00952            = 4 * ARRAY_SIZE (sparc_vxworks_exec_plt_entry);
00953        }
00954     }
00955   else
00956     {
00957       if (ABI_64_P (dynobj))
00958        {
00959          htab->build_plt_entry = sparc64_plt_entry_build;
00960          htab->plt_header_size = PLT64_HEADER_SIZE;
00961          htab->plt_entry_size = PLT64_ENTRY_SIZE;
00962        }
00963       else
00964        {
00965          htab->build_plt_entry = sparc32_plt_entry_build;
00966          htab->plt_header_size = PLT32_HEADER_SIZE;
00967          htab->plt_entry_size = PLT32_ENTRY_SIZE;
00968        }
00969     }
00970 
00971   if (!htab->splt || !htab->srelplt || !htab->sdynbss
00972       || (!info->shared && !htab->srelbss))
00973     abort ();
00974 
00975   return TRUE;
00976 }
00977 
00978 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
00979 
00980 void
00981 _bfd_sparc_elf_copy_indirect_symbol (struct bfd_link_info *info,
00982                                  struct elf_link_hash_entry *dir,
00983                                  struct elf_link_hash_entry *ind)
00984 {
00985   struct _bfd_sparc_elf_link_hash_entry *edir, *eind;
00986 
00987   edir = (struct _bfd_sparc_elf_link_hash_entry *) dir;
00988   eind = (struct _bfd_sparc_elf_link_hash_entry *) ind;
00989 
00990   if (eind->dyn_relocs != NULL)
00991     {
00992       if (edir->dyn_relocs != NULL)
00993        {
00994          struct _bfd_sparc_elf_dyn_relocs **pp;
00995          struct _bfd_sparc_elf_dyn_relocs *p;
00996 
00997          /* Add reloc counts against the indirect sym to the direct sym
00998             list.  Merge any entries against the same section.  */
00999          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
01000            {
01001              struct _bfd_sparc_elf_dyn_relocs *q;
01002 
01003              for (q = edir->dyn_relocs; q != NULL; q = q->next)
01004               if (q->sec == p->sec)
01005                 {
01006                   q->pc_count += p->pc_count;
01007                   q->count += p->count;
01008                   *pp = p->next;
01009                   break;
01010                 }
01011              if (q == NULL)
01012               pp = &p->next;
01013            }
01014          *pp = edir->dyn_relocs;
01015        }
01016 
01017       edir->dyn_relocs = eind->dyn_relocs;
01018       eind->dyn_relocs = NULL;
01019     }
01020 
01021   if (ind->root.type == bfd_link_hash_indirect
01022       && dir->got.refcount <= 0)
01023     {
01024       edir->tls_type = eind->tls_type;
01025       eind->tls_type = GOT_UNKNOWN;
01026     }
01027   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
01028 }
01029 
01030 static int
01031 sparc_elf_tls_transition (struct bfd_link_info *info, bfd *abfd,
01032                        int r_type, int is_local)
01033 {
01034   if (! ABI_64_P (abfd)
01035       && r_type == R_SPARC_TLS_GD_HI22
01036       && ! _bfd_sparc_elf_tdata (abfd)->has_tlsgd)
01037     r_type = R_SPARC_REV32;
01038 
01039   if (info->shared)
01040     return r_type;
01041 
01042   switch (r_type)
01043     {
01044     case R_SPARC_TLS_GD_HI22:
01045       if (is_local)
01046        return R_SPARC_TLS_LE_HIX22;
01047       return R_SPARC_TLS_IE_HI22;
01048     case R_SPARC_TLS_GD_LO10:
01049       if (is_local)
01050        return R_SPARC_TLS_LE_LOX10;
01051       return R_SPARC_TLS_IE_LO10;
01052     case R_SPARC_TLS_IE_HI22:
01053       if (is_local)
01054        return R_SPARC_TLS_LE_HIX22;
01055       return r_type;
01056     case R_SPARC_TLS_IE_LO10:
01057       if (is_local)
01058        return R_SPARC_TLS_LE_LOX10;
01059       return r_type;
01060     case R_SPARC_TLS_LDM_HI22:
01061       return R_SPARC_TLS_LE_HIX22;
01062     case R_SPARC_TLS_LDM_LO10:
01063       return R_SPARC_TLS_LE_LOX10;
01064     }
01065 
01066   return r_type;
01067 }
01068 
01069 /* Look through the relocs for a section during the first phase, and
01070    allocate space in the global offset table or procedure linkage
01071    table.  */
01072 
01073 bfd_boolean
01074 _bfd_sparc_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
01075                           asection *sec, const Elf_Internal_Rela *relocs)
01076 {
01077   struct _bfd_sparc_elf_link_hash_table *htab;
01078   Elf_Internal_Shdr *symtab_hdr;
01079   struct elf_link_hash_entry **sym_hashes;
01080   bfd_vma *local_got_offsets;
01081   const Elf_Internal_Rela *rel;
01082   const Elf_Internal_Rela *rel_end;
01083   asection *sreloc;
01084   int num_relocs;
01085   bfd_boolean checked_tlsgd = FALSE;
01086 
01087   if (info->relocatable)
01088     return TRUE;
01089 
01090   htab = _bfd_sparc_elf_hash_table (info);
01091   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01092   sym_hashes = elf_sym_hashes (abfd);
01093   local_got_offsets = elf_local_got_offsets (abfd);
01094 
01095   sreloc = NULL;
01096 
01097   if (ABI_64_P (abfd))
01098     num_relocs = NUM_SHDR_ENTRIES (& elf_section_data (sec)->rel_hdr);
01099   else
01100     num_relocs = sec->reloc_count;
01101   rel_end = relocs + num_relocs;
01102   for (rel = relocs; rel < rel_end; rel++)
01103     {
01104       unsigned int r_type;
01105       unsigned long r_symndx;
01106       struct elf_link_hash_entry *h;
01107 
01108       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
01109       r_type = SPARC_ELF_R_TYPE (rel->r_info);
01110 
01111       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
01112        {
01113          (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
01114                              abfd, r_symndx);
01115          return FALSE;
01116        }
01117 
01118       if (r_symndx < symtab_hdr->sh_info)
01119        h = NULL;
01120       else
01121        {
01122          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
01123          while (h->root.type == bfd_link_hash_indirect
01124                || h->root.type == bfd_link_hash_warning)
01125            h = (struct elf_link_hash_entry *) h->root.u.i.link;
01126        }
01127 
01128       /* Compatibility with old R_SPARC_REV32 reloc conflicting
01129         with R_SPARC_TLS_GD_HI22.  */
01130       if (! ABI_64_P (abfd) && ! checked_tlsgd)
01131        switch (r_type)
01132          {
01133          case R_SPARC_TLS_GD_HI22:
01134            {
01135              const Elf_Internal_Rela *relt;
01136 
01137              for (relt = rel + 1; relt < rel_end; relt++)
01138               if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
01139                   || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
01140                   || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
01141                 break;
01142              checked_tlsgd = TRUE;
01143              _bfd_sparc_elf_tdata (abfd)->has_tlsgd = relt < rel_end;
01144            }
01145            break;
01146          case R_SPARC_TLS_GD_LO10:
01147          case R_SPARC_TLS_GD_ADD:
01148          case R_SPARC_TLS_GD_CALL:
01149            checked_tlsgd = TRUE;
01150            _bfd_sparc_elf_tdata (abfd)->has_tlsgd = TRUE;
01151            break;
01152          }
01153 
01154       r_type = sparc_elf_tls_transition (info, abfd, r_type, h == NULL);
01155       switch (r_type)
01156        {
01157        case R_SPARC_TLS_LDM_HI22:
01158        case R_SPARC_TLS_LDM_LO10:
01159          htab->tls_ldm_got.refcount += 1;
01160          break;
01161 
01162        case R_SPARC_TLS_LE_HIX22:
01163        case R_SPARC_TLS_LE_LOX10:
01164          if (info->shared)
01165            goto r_sparc_plt32;
01166          break;
01167 
01168        case R_SPARC_TLS_IE_HI22:
01169        case R_SPARC_TLS_IE_LO10:
01170          if (info->shared)
01171            info->flags |= DF_STATIC_TLS;
01172          /* Fall through */
01173 
01174        case R_SPARC_GOT10:
01175        case R_SPARC_GOT13:
01176        case R_SPARC_GOT22:
01177        case R_SPARC_TLS_GD_HI22:
01178        case R_SPARC_TLS_GD_LO10:
01179          /* This symbol requires a global offset table entry.  */
01180          {
01181            int tls_type, old_tls_type;
01182 
01183            switch (r_type)
01184              {
01185              default:
01186              case R_SPARC_GOT10:
01187              case R_SPARC_GOT13:
01188              case R_SPARC_GOT22:
01189               tls_type = GOT_NORMAL;
01190               break;
01191              case R_SPARC_TLS_GD_HI22:
01192              case R_SPARC_TLS_GD_LO10:
01193               tls_type = GOT_TLS_GD;
01194               break;
01195              case R_SPARC_TLS_IE_HI22:
01196              case R_SPARC_TLS_IE_LO10:
01197               tls_type = GOT_TLS_IE;
01198               break;
01199              }
01200 
01201            if (h != NULL)
01202              {
01203               h->got.refcount += 1;
01204               old_tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
01205              }
01206            else
01207              {
01208               bfd_signed_vma *local_got_refcounts;
01209 
01210               /* This is a global offset table entry for a local symbol.  */
01211               local_got_refcounts = elf_local_got_refcounts (abfd);
01212               if (local_got_refcounts == NULL)
01213                 {
01214                   bfd_size_type size;
01215 
01216                   size = symtab_hdr->sh_info;
01217                   size *= (sizeof (bfd_signed_vma) + sizeof(char));
01218                   local_got_refcounts = ((bfd_signed_vma *)
01219                                       bfd_zalloc (abfd, size));
01220                   if (local_got_refcounts == NULL)
01221                     return FALSE;
01222                   elf_local_got_refcounts (abfd) = local_got_refcounts;
01223                   _bfd_sparc_elf_local_got_tls_type (abfd)
01224                     = (char *) (local_got_refcounts + symtab_hdr->sh_info);
01225                 }
01226               local_got_refcounts[r_symndx] += 1;
01227               old_tls_type = _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx];
01228              }
01229 
01230            /* If a TLS symbol is accessed using IE at least once,
01231               there is no point to use dynamic model for it.  */
01232            if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
01233               && (old_tls_type != GOT_TLS_GD
01234                   || tls_type != GOT_TLS_IE))
01235              {
01236               if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
01237                 tls_type = old_tls_type;
01238               else
01239                 {
01240                   (*_bfd_error_handler)
01241                     (_("%B: `%s' accessed both as normal and thread local symbol"),
01242                      abfd, h ? h->root.root.string : "<local>");
01243                   return FALSE;
01244                 }
01245              }
01246 
01247            if (old_tls_type != tls_type)
01248              {
01249               if (h != NULL)
01250                 _bfd_sparc_elf_hash_entry (h)->tls_type = tls_type;
01251               else
01252                 _bfd_sparc_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
01253              }
01254          }
01255 
01256          if (htab->sgot == NULL)
01257            {
01258              if (htab->elf.dynobj == NULL)
01259               htab->elf.dynobj = abfd;
01260              if (!create_got_section (htab->elf.dynobj, info))
01261               return FALSE;
01262            }
01263          break;
01264 
01265        case R_SPARC_TLS_GD_CALL:
01266        case R_SPARC_TLS_LDM_CALL:
01267          if (info->shared)
01268            {
01269              /* These are basically R_SPARC_TLS_WPLT30 relocs against
01270                __tls_get_addr.  */
01271              struct bfd_link_hash_entry *bh = NULL;
01272              if (! _bfd_generic_link_add_one_symbol (info, abfd,
01273                                                 "__tls_get_addr", 0,
01274                                                 bfd_und_section_ptr, 0,
01275                                                 NULL, FALSE, FALSE,
01276                                                 &bh))
01277               return FALSE;
01278              h = (struct elf_link_hash_entry *) bh;
01279            }
01280          else
01281            break;
01282          /* Fall through */
01283 
01284        case R_SPARC_PLT32:
01285        case R_SPARC_WPLT30:
01286        case R_SPARC_HIPLT22:
01287        case R_SPARC_LOPLT10:
01288        case R_SPARC_PCPLT32:
01289        case R_SPARC_PCPLT22:
01290        case R_SPARC_PCPLT10:
01291        case R_SPARC_PLT64:
01292          /* This symbol requires a procedure linkage table entry.  We
01293             actually build the entry in adjust_dynamic_symbol,
01294             because this might be a case of linking PIC code without
01295             linking in any dynamic objects, in which case we don't
01296             need to generate a procedure linkage table after all.  */
01297 
01298          if (h == NULL)
01299            {
01300              if (! ABI_64_P (abfd))
01301               {
01302                 /* The Solaris native assembler will generate a WPLT30
01303                    reloc for a local symbol if you assemble a call from
01304                    one section to another when using -K pic.  We treat
01305                    it as WDISP30.  */
01306                 if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
01307                   goto r_sparc_plt32;
01308                 break;
01309               }
01310 
01311              /* It does not make sense to have a procedure linkage
01312                  table entry for a local symbol.  */
01313              bfd_set_error (bfd_error_bad_value);
01314              return FALSE;
01315            }
01316 
01317          h->needs_plt = 1;
01318 
01319          {
01320            int this_r_type;
01321 
01322            this_r_type = SPARC_ELF_R_TYPE (rel->r_info);
01323            if (this_r_type == R_SPARC_PLT32
01324               || this_r_type == R_SPARC_PLT64)
01325              goto r_sparc_plt32;
01326          }
01327          h->plt.refcount += 1;
01328          break;
01329 
01330        case R_SPARC_PC10:
01331        case R_SPARC_PC22:
01332        case R_SPARC_PC_HH22:
01333        case R_SPARC_PC_HM10:
01334        case R_SPARC_PC_LM22:
01335          if (h != NULL)
01336            h->non_got_ref = 1;
01337 
01338          if (h != NULL
01339              && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
01340            break;
01341          /* Fall through.  */
01342 
01343        case R_SPARC_DISP8:
01344        case R_SPARC_DISP16:
01345        case R_SPARC_DISP32:
01346        case R_SPARC_DISP64:
01347        case R_SPARC_WDISP30:
01348        case R_SPARC_WDISP22:
01349        case R_SPARC_WDISP19:
01350        case R_SPARC_WDISP16:
01351        case R_SPARC_8:
01352        case R_SPARC_16:
01353        case R_SPARC_32:
01354        case R_SPARC_HI22:
01355        case R_SPARC_22:
01356        case R_SPARC_13:
01357        case R_SPARC_LO10:
01358        case R_SPARC_UA16:
01359        case R_SPARC_UA32:
01360        case R_SPARC_10:
01361        case R_SPARC_11:
01362        case R_SPARC_64:
01363        case R_SPARC_OLO10:
01364        case R_SPARC_HH22:
01365        case R_SPARC_HM10:
01366        case R_SPARC_LM22:
01367        case R_SPARC_7:
01368        case R_SPARC_5:
01369        case R_SPARC_6:
01370        case R_SPARC_HIX22:
01371        case R_SPARC_LOX10:
01372        case R_SPARC_H44:
01373        case R_SPARC_M44:
01374        case R_SPARC_L44:
01375        case R_SPARC_UA64:
01376          if (h != NULL)
01377            h->non_got_ref = 1;
01378 
01379        r_sparc_plt32:
01380          if (h != NULL && !info->shared)
01381            {
01382              /* We may need a .plt entry if the function this reloc
01383                refers to is in a shared lib.  */
01384              h->plt.refcount += 1;
01385            }
01386 
01387          /* If we are creating a shared library, and this is a reloc
01388             against a global symbol, or a non PC relative reloc
01389             against a local symbol, then we need to copy the reloc
01390             into the shared library.  However, if we are linking with
01391             -Bsymbolic, we do not need to copy a reloc against a
01392             global symbol which is defined in an object we are
01393             including in the link (i.e., DEF_REGULAR is set).  At
01394             this point we have not seen all the input files, so it is
01395             possible that DEF_REGULAR is not set now but will be set
01396             later (it is never cleared).  In case of a weak definition,
01397             DEF_REGULAR may be cleared later by a strong definition in
01398             a shared library.  We account for that possibility below by
01399             storing information in the relocs_copied field of the hash
01400             table entry.  A similar situation occurs when creating
01401             shared libraries and symbol visibility changes render the
01402             symbol local.
01403 
01404             If on the other hand, we are creating an executable, we
01405             may need to keep relocations for symbols satisfied by a
01406             dynamic library if we manage to avoid copy relocs for the
01407             symbol.  */
01408          if ((info->shared
01409               && (sec->flags & SEC_ALLOC) != 0
01410               && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
01411                  || (h != NULL
01412                      && (! info->symbolic
01413                         || h->root.type == bfd_link_hash_defweak
01414                         || !h->def_regular))))
01415              || (!info->shared
01416                 && (sec->flags & SEC_ALLOC) != 0
01417                 && h != NULL
01418                 && (h->root.type == bfd_link_hash_defweak
01419                     || !h->def_regular)))
01420            {
01421              struct _bfd_sparc_elf_dyn_relocs *p;
01422              struct _bfd_sparc_elf_dyn_relocs **head;
01423 
01424              /* When creating a shared object, we must copy these
01425                relocs into the output file.  We create a reloc
01426                section in dynobj and make room for the reloc.  */
01427              if (sreloc == NULL)
01428               {
01429                 const char *name;
01430                 bfd *dynobj;
01431 
01432                 name = (bfd_elf_string_from_elf_section
01433                        (abfd,
01434                         elf_elfheader (abfd)->e_shstrndx,
01435                         elf_section_data (sec)->rel_hdr.sh_name));
01436                 if (name == NULL)
01437                   return FALSE;
01438 
01439                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
01440                            && strcmp (bfd_get_section_name (abfd, sec),
01441                                     name + 5) == 0);
01442 
01443                 if (htab->elf.dynobj == NULL)
01444                   htab->elf.dynobj = abfd;
01445                 dynobj = htab->elf.dynobj;
01446 
01447                 sreloc = bfd_get_section_by_name (dynobj, name);
01448                 if (sreloc == NULL)
01449                   {
01450                     flagword flags;
01451 
01452                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
01453                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
01454                     if ((sec->flags & SEC_ALLOC) != 0)
01455                      flags |= SEC_ALLOC | SEC_LOAD;
01456                     sreloc = bfd_make_section_with_flags (dynobj,
01457                                                      name,
01458                                                      flags);
01459                     if (sreloc == NULL
01460                        || ! bfd_set_section_alignment (dynobj, sreloc,
01461                                                    htab->word_align_power))
01462                      return FALSE;
01463                   }
01464                 elf_section_data (sec)->sreloc = sreloc;
01465               }
01466 
01467              /* If this is a global symbol, we count the number of
01468                relocations we need for this symbol.  */
01469              if (h != NULL)
01470               head = &((struct _bfd_sparc_elf_link_hash_entry *) h)->dyn_relocs;
01471              else
01472               {
01473                 /* Track dynamic relocs needed for local syms too.
01474                    We really need local syms available to do this
01475                    easily.  Oh well.  */
01476 
01477                 asection *s;
01478                 void *vpp;
01479 
01480                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
01481                                            sec, r_symndx);
01482                 if (s == NULL)
01483                   return FALSE;
01484 
01485                 vpp = &elf_section_data (s)->local_dynrel;
01486                 head = (struct _bfd_sparc_elf_dyn_relocs **) vpp;
01487               }
01488 
01489              p = *head;
01490              if (p == NULL || p->sec != sec)
01491               {
01492                 bfd_size_type amt = sizeof *p;
01493                 p = ((struct _bfd_sparc_elf_dyn_relocs *)
01494                      bfd_alloc (htab->elf.dynobj, amt));
01495                 if (p == NULL)
01496                   return FALSE;
01497                 p->next = *head;
01498                 *head = p;
01499                 p->sec = sec;
01500                 p->count = 0;
01501                 p->pc_count = 0;
01502               }
01503 
01504              p->count += 1;
01505              if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
01506               p->pc_count += 1;
01507            }
01508 
01509          break;
01510 
01511        case R_SPARC_GNU_VTINHERIT:
01512          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
01513            return FALSE;
01514          break;
01515 
01516        case R_SPARC_GNU_VTENTRY:
01517          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
01518            return FALSE;
01519          break;
01520 
01521        case R_SPARC_REGISTER:
01522          /* Nothing to do.  */
01523          break;
01524 
01525        default:
01526          break;
01527        }
01528     }
01529 
01530   return TRUE;
01531 }
01532 
01533 asection *
01534 _bfd_sparc_elf_gc_mark_hook (asection *sec,
01535                           struct bfd_link_info *info,
01536                           Elf_Internal_Rela *rel,
01537                           struct elf_link_hash_entry *h,
01538                           Elf_Internal_Sym *sym)
01539 {
01540   if (h != NULL)
01541     switch (SPARC_ELF_R_TYPE (rel->r_info))
01542       {
01543       case R_SPARC_GNU_VTINHERIT:
01544       case R_SPARC_GNU_VTENTRY:
01545        return NULL;
01546       }
01547 
01548   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
01549 }
01550 
01551 /* Update the got entry reference counts for the section being removed.  */
01552 bfd_boolean
01553 _bfd_sparc_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
01554                            asection *sec, const Elf_Internal_Rela *relocs)
01555 {
01556   struct _bfd_sparc_elf_link_hash_table *htab;
01557   Elf_Internal_Shdr *symtab_hdr;
01558   struct elf_link_hash_entry **sym_hashes;
01559   bfd_signed_vma *local_got_refcounts;
01560   const Elf_Internal_Rela *rel, *relend;
01561 
01562   elf_section_data (sec)->local_dynrel = NULL;
01563 
01564   htab = _bfd_sparc_elf_hash_table (info);
01565   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01566   sym_hashes = elf_sym_hashes (abfd);
01567   local_got_refcounts = elf_local_got_refcounts (abfd);
01568 
01569   relend = relocs + sec->reloc_count;
01570   for (rel = relocs; rel < relend; rel++)
01571     {
01572       unsigned long r_symndx;
01573       unsigned int r_type;
01574       struct elf_link_hash_entry *h = NULL;
01575 
01576       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
01577       if (r_symndx >= symtab_hdr->sh_info)
01578        {
01579          struct _bfd_sparc_elf_link_hash_entry *eh;
01580          struct _bfd_sparc_elf_dyn_relocs **pp;
01581          struct _bfd_sparc_elf_dyn_relocs *p;
01582 
01583          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
01584          while (h->root.type == bfd_link_hash_indirect
01585                || h->root.type == bfd_link_hash_warning)
01586            h = (struct elf_link_hash_entry *) h->root.u.i.link;
01587          eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
01588          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
01589            if (p->sec == sec)
01590              {
01591               /* Everything must go for SEC.  */
01592               *pp = p->next;
01593               break;
01594              }
01595        }
01596 
01597       r_type = SPARC_ELF_R_TYPE (rel->r_info);
01598       r_type = sparc_elf_tls_transition (info, abfd, r_type, h != NULL);
01599       switch (r_type)
01600        {
01601        case R_SPARC_TLS_LDM_HI22:
01602        case R_SPARC_TLS_LDM_LO10:
01603          if (_bfd_sparc_elf_hash_table (info)->tls_ldm_got.refcount > 0)
01604            _bfd_sparc_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
01605          break;
01606 
01607        case R_SPARC_TLS_GD_HI22:
01608        case R_SPARC_TLS_GD_LO10:
01609        case R_SPARC_TLS_IE_HI22:
01610        case R_SPARC_TLS_IE_LO10:
01611        case R_SPARC_GOT10:
01612        case R_SPARC_GOT13:
01613        case R_SPARC_GOT22:
01614          if (h != NULL)
01615            {
01616              if (h->got.refcount > 0)
01617               h->got.refcount--;
01618            }
01619          else
01620            {
01621              if (local_got_refcounts[r_symndx] > 0)
01622               local_got_refcounts[r_symndx]--;
01623            }
01624          break;
01625 
01626        case R_SPARC_PC10:
01627        case R_SPARC_PC22:
01628        case R_SPARC_PC_HH22:
01629        case R_SPARC_PC_HM10:
01630        case R_SPARC_PC_LM22:
01631          if (h != NULL
01632              && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
01633            break;
01634          /* Fall through.  */
01635 
01636        case R_SPARC_DISP8:
01637        case R_SPARC_DISP16:
01638        case R_SPARC_DISP32:
01639        case R_SPARC_DISP64:
01640        case R_SPARC_WDISP30:
01641        case R_SPARC_WDISP22:
01642        case R_SPARC_WDISP19:
01643        case R_SPARC_WDISP16:
01644        case R_SPARC_8:
01645        case R_SPARC_16:
01646        case R_SPARC_32:
01647        case R_SPARC_HI22:
01648        case R_SPARC_22:
01649        case R_SPARC_13:
01650        case R_SPARC_LO10:
01651        case R_SPARC_UA16:
01652        case R_SPARC_UA32:
01653        case R_SPARC_PLT32:
01654        case R_SPARC_10:
01655        case R_SPARC_11:
01656        case R_SPARC_64:
01657        case R_SPARC_OLO10:
01658        case R_SPARC_HH22:
01659        case R_SPARC_HM10:
01660        case R_SPARC_LM22:
01661        case R_SPARC_7:
01662        case R_SPARC_5:
01663        case R_SPARC_6:
01664        case R_SPARC_HIX22:
01665        case R_SPARC_LOX10:
01666        case R_SPARC_H44:
01667        case R_SPARC_M44:
01668        case R_SPARC_L44:
01669        case R_SPARC_UA64:
01670          if (info->shared)
01671            break;
01672          /* Fall through.  */
01673 
01674        case R_SPARC_WPLT30:
01675          if (h != NULL)
01676            {
01677              if (h->plt.refcount > 0)
01678               h->plt.refcount--;
01679            }
01680          break;
01681 
01682        default:
01683          break;
01684        }
01685     }
01686 
01687   return TRUE;
01688 }
01689 
01690 /* Adjust a symbol defined by a dynamic object and referenced by a
01691    regular object.  The current definition is in some section of the
01692    dynamic object, but we're not including those sections.  We have to
01693    change the definition to something the rest of the link can
01694    understand.  */
01695 
01696 bfd_boolean
01697 _bfd_sparc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
01698                                  struct elf_link_hash_entry *h)
01699 {
01700   struct _bfd_sparc_elf_link_hash_table *htab;
01701   struct _bfd_sparc_elf_link_hash_entry * eh;
01702   struct _bfd_sparc_elf_dyn_relocs *p;
01703   asection *s;
01704   unsigned int power_of_two;
01705 
01706   htab = _bfd_sparc_elf_hash_table (info);
01707 
01708   /* Make sure we know what is going on here.  */
01709   BFD_ASSERT (htab->elf.dynobj != NULL
01710              && (h->needs_plt
01711                 || h->u.weakdef != NULL
01712                 || (h->def_dynamic
01713                     && h->ref_regular
01714                     && !h->def_regular)));
01715 
01716   /* If this is a function, put it in the procedure linkage table.  We
01717      will fill in the contents of the procedure linkage table later
01718      (although we could actually do it here).  The STT_NOTYPE
01719      condition is a hack specifically for the Oracle libraries
01720      delivered for Solaris; for some inexplicable reason, they define
01721      some of their functions as STT_NOTYPE when they really should be
01722      STT_FUNC.  */
01723   if (h->type == STT_FUNC
01724       || h->needs_plt
01725       || (h->type == STT_NOTYPE
01726          && (h->root.type == bfd_link_hash_defined
01727              || h->root.type == bfd_link_hash_defweak)
01728          && (h->root.u.def.section->flags & SEC_CODE) != 0))
01729     {
01730       if (h->plt.refcount <= 0
01731          || (! info->shared
01732              && !h->def_dynamic
01733              && !h->ref_dynamic
01734              && h->root.type != bfd_link_hash_undefweak
01735              && h->root.type != bfd_link_hash_undefined))
01736        {
01737          /* This case can occur if we saw a WPLT30 reloc in an input
01738             file, but the symbol was never referred to by a dynamic
01739             object, or if all references were garbage collected.  In
01740             such a case, we don't actually need to build a procedure
01741             linkage table, and we can just do a WDISP30 reloc instead.  */
01742          h->plt.offset = (bfd_vma) -1;
01743          h->needs_plt = 0;
01744        }
01745 
01746       return TRUE;
01747     }
01748   else
01749     h->plt.offset = (bfd_vma) -1;
01750 
01751   /* If this is a weak symbol, and there is a real definition, the
01752      processor independent code will have arranged for us to see the
01753      real definition first, and we can just use the same value.  */
01754   if (h->u.weakdef != NULL)
01755     {
01756       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
01757                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
01758       h->root.u.def.section = h->u.weakdef->root.u.def.section;
01759       h->root.u.def.value = h->u.weakdef->root.u.def.value;
01760       return TRUE;
01761     }
01762 
01763   /* This is a reference to a symbol defined by a dynamic object which
01764      is not a function.  */
01765 
01766   /* If we are creating a shared library, we must presume that the
01767      only references to the symbol are via the global offset table.
01768      For such cases we need not do anything here; the relocations will
01769      be handled correctly by relocate_section.  */
01770   if (info->shared)
01771     return TRUE;
01772 
01773   /* If there are no references to this symbol that do not use the
01774      GOT, we don't need to generate a copy reloc.  */
01775   if (!h->non_got_ref)
01776     return TRUE;
01777 
01778   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
01779   for (p = eh->dyn_relocs; p != NULL; p = p->next)
01780     {
01781       s = p->sec->output_section;
01782       if (s != NULL && (s->flags & SEC_READONLY) != 0)
01783        break;
01784     }
01785 
01786   /* If we didn't find any dynamic relocs in read-only sections, then
01787      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
01788   if (p == NULL)
01789     {
01790       h->non_got_ref = 0;
01791       return TRUE;
01792     }
01793 
01794   if (h->size == 0)
01795     {
01796       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
01797                           h->root.root.string);
01798       return TRUE;
01799     }
01800 
01801   /* We must allocate the symbol in our .dynbss section, which will
01802      become part of the .bss section of the executable.  There will be
01803      an entry for this symbol in the .dynsym section.  The dynamic
01804      object will contain position independent code, so all references
01805      from the dynamic object to this symbol will go through the global
01806      offset table.  The dynamic linker will use the .dynsym entry to
01807      determine the address it must put in the global offset table, so
01808      both the dynamic object and the regular object will refer to the
01809      same memory location for the variable.  */
01810 
01811   /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
01812      to copy the initial value out of the dynamic object and into the
01813      runtime process image.  We need to remember the offset into the
01814      .rel.bss section we are going to use.  */
01815   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
01816     {
01817       htab->srelbss->size += SPARC_ELF_RELA_BYTES (htab);
01818       h->needs_copy = 1;
01819     }
01820 
01821   /* We need to figure out the alignment required for this symbol.  I
01822      have no idea how ELF linkers handle this.  */
01823   power_of_two = bfd_log2 (h->size);
01824   if (power_of_two > htab->align_power_max)
01825     power_of_two = htab->align_power_max;
01826 
01827   /* Apply the required alignment.  */
01828   s = htab->sdynbss;
01829   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
01830   if (power_of_two > bfd_get_section_alignment (dynobj, s))
01831     {
01832       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
01833        return FALSE;
01834     }
01835 
01836   /* Define the symbol as being at this point in the section.  */
01837   h->root.u.def.section = s;
01838   h->root.u.def.value = s->size;
01839 
01840   /* Increment the section size to make room for the symbol.  */
01841   s->size += h->size;
01842 
01843   return TRUE;
01844 }
01845 
01846 /* Allocate space in .plt, .got and associated reloc sections for
01847    dynamic relocs.  */
01848 
01849 static bfd_boolean
01850 allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
01851 {
01852   struct bfd_link_info *info;
01853   struct _bfd_sparc_elf_link_hash_table *htab;
01854   struct _bfd_sparc_elf_link_hash_entry *eh;
01855   struct _bfd_sparc_elf_dyn_relocs *p;
01856 
01857   if (h->root.type == bfd_link_hash_indirect)
01858     return TRUE;
01859 
01860   if (h->root.type == bfd_link_hash_warning)
01861     /* When warning symbols are created, they **replace** the "real"
01862        entry in the hash table, thus we never get to see the real
01863        symbol in a hash traversal.  So look at it now.  */
01864     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01865 
01866   info = (struct bfd_link_info *) inf;
01867   htab = _bfd_sparc_elf_hash_table (info);
01868 
01869   if (htab->elf.dynamic_sections_created
01870       && h->plt.refcount > 0)
01871     {
01872       /* Make sure this symbol is output as a dynamic symbol.
01873         Undefined weak syms won't yet be marked as dynamic.  */
01874       if (h->dynindx == -1
01875          && !h->forced_local)
01876        {
01877          if (! bfd_elf_link_record_dynamic_symbol (info, h))
01878            return FALSE;
01879        }
01880 
01881       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
01882        {
01883          asection *s = htab->splt;
01884 
01885          /* Allocate room for the header.  */
01886          if (s->size == 0)
01887            {
01888              s->size = htab->plt_header_size;
01889 
01890              /* Allocate space for the .rela.plt.unloaded relocations.  */
01891              if (htab->is_vxworks && !info->shared)
01892               htab->srelplt2->size = sizeof (Elf32_External_Rela) * 2;
01893            }
01894 
01895          /* The procedure linkage table size is bounded by the magnitude
01896             of the offset we can describe in the entry.  */
01897          if (s->size >= (SPARC_ELF_WORD_BYTES(htab) == 8 ?
01898                        (((bfd_vma)1 << 31) << 1) : 0x400000))
01899            {
01900              bfd_set_error (bfd_error_bad_value);
01901              return FALSE;
01902            }
01903 
01904          if (SPARC_ELF_WORD_BYTES(htab) == 8
01905              && s->size >= PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE)
01906            {
01907              bfd_vma off = s->size - PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE;
01908 
01909 
01910              off = (off % (160 * PLT64_ENTRY_SIZE)) / PLT64_ENTRY_SIZE;
01911 
01912              h->plt.offset = (s->size - (off * 8));
01913            }
01914          else
01915            h->plt.offset = s->size;
01916 
01917          /* If this symbol is not defined in a regular file, and we are
01918             not generating a shared library, then set the symbol to this
01919             location in the .plt.  This is required to make function
01920             pointers compare as equal between the normal executable and
01921             the shared library.  */
01922          if (! info->shared
01923              && !h->def_regular)
01924            {
01925              h->root.u.def.section = s;
01926              h->root.u.def.value = h->plt.offset;
01927            }
01928 
01929          /* Make room for this entry.  */
01930          s->size += htab->plt_entry_size;
01931 
01932          /* We also need to make an entry in the .rela.plt section.  */
01933          htab->srelplt->size += SPARC_ELF_RELA_BYTES (htab);
01934 
01935          if (htab->is_vxworks)
01936            {
01937              /* Allocate space for the .got.plt entry.  */
01938              htab->sgotplt->size += 4;
01939 
01940              /* ...and for the .rela.plt.unloaded relocations.  */
01941              if (!info->shared)
01942               htab->srelplt2->size += sizeof (Elf32_External_Rela) * 3;
01943            }
01944        }
01945       else
01946        {
01947          h->plt.offset = (bfd_vma) -1;
01948          h->needs_plt = 0;
01949        }
01950     }
01951   else
01952     {
01953       h->plt.offset = (bfd_vma) -1;
01954       h->needs_plt = 0;
01955     }
01956 
01957   /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
01958      make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry.  */
01959   if (h->got.refcount > 0
01960       && !info->shared
01961       && h->dynindx == -1
01962       && _bfd_sparc_elf_hash_entry(h)->tls_type == GOT_TLS_IE)
01963     h->got.offset = (bfd_vma) -1;
01964   else if (h->got.refcount > 0)
01965     {
01966       asection *s;
01967       bfd_boolean dyn;
01968       int tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
01969 
01970       /* Make sure this symbol is output as a dynamic symbol.
01971         Undefined weak syms won't yet be marked as dynamic.  */
01972       if (h->dynindx == -1
01973          && !h->forced_local)
01974        {
01975          if (! bfd_elf_link_record_dynamic_symbol (info, h))
01976            return FALSE;
01977        }
01978 
01979       s = htab->sgot;
01980       h->got.offset = s->size;
01981       s->size += SPARC_ELF_WORD_BYTES (htab);
01982       /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots.  */
01983       if (tls_type == GOT_TLS_GD)
01984        s->size += SPARC_ELF_WORD_BYTES (htab);
01985       dyn = htab->elf.dynamic_sections_created;
01986       /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
01987         R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if
01988         global.  */
01989       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
01990          || tls_type == GOT_TLS_IE)
01991        htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
01992       else if (tls_type == GOT_TLS_GD)
01993        htab->srelgot->size += 2 * SPARC_ELF_RELA_BYTES (htab);
01994       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
01995        htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
01996     }
01997   else
01998     h->got.offset = (bfd_vma) -1;
01999 
02000   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
02001   if (eh->dyn_relocs == NULL)
02002     return TRUE;
02003 
02004   /* In the shared -Bsymbolic case, discard space allocated for
02005      dynamic pc-relative relocs against symbols which turn out to be
02006      defined in regular objects.  For the normal shared case, discard
02007      space for pc-relative relocs that have become local due to symbol
02008      visibility changes.  */
02009 
02010   if (info->shared)
02011     {
02012       if (h->def_regular
02013          && (h->forced_local
02014              || info->symbolic))
02015        {
02016          struct _bfd_sparc_elf_dyn_relocs **pp;
02017 
02018          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
02019            {
02020              p->count -= p->pc_count;
02021              p->pc_count = 0;
02022              if (p->count == 0)
02023               *pp = p->next;
02024              else
02025               pp = &p->next;
02026            }
02027        }
02028 
02029       /* Also discard relocs on undefined weak syms with non-default
02030         visibility.  */
02031       if (eh->dyn_relocs != NULL
02032          && h->root.type == bfd_link_hash_undefweak)
02033        {
02034          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
02035            eh->dyn_relocs = NULL;
02036 
02037          /* Make sure undefined weak symbols are output as a dynamic
02038             symbol in PIEs.  */
02039          else if (h->dynindx == -1
02040                  && !h->forced_local)
02041            {
02042              if (! bfd_elf_link_record_dynamic_symbol (info, h))
02043               return FALSE;
02044            }
02045        }
02046     }
02047   else
02048     {
02049       /* For the non-shared case, discard space for relocs against
02050         symbols which turn out to need copy relocs or are not
02051         dynamic.  */
02052 
02053       if (!h->non_got_ref
02054          && ((h->def_dynamic
02055               && !h->def_regular)
02056              || (htab->elf.dynamic_sections_created
02057                 && (h->root.type == bfd_link_hash_undefweak
02058                     || h->root.type == bfd_link_hash_undefined))))
02059        {
02060          /* Make sure this symbol is output as a dynamic symbol.
02061             Undefined weak syms won't yet be marked as dynamic.  */
02062          if (h->dynindx == -1
02063              && !h->forced_local)
02064            {
02065              if (! bfd_elf_link_record_dynamic_symbol (info, h))
02066               return FALSE;
02067            }
02068 
02069          /* If that succeeded, we know we'll be keeping all the
02070             relocs.  */
02071          if (h->dynindx != -1)
02072            goto keep;
02073        }
02074 
02075       eh->dyn_relocs = NULL;
02076 
02077     keep: ;
02078     }
02079 
02080   /* Finally, allocate space.  */
02081   for (p = eh->dyn_relocs; p != NULL; p = p->next)
02082     {
02083       asection *sreloc = elf_section_data (p->sec)->sreloc;
02084       sreloc->size += p->count * SPARC_ELF_RELA_BYTES (htab);
02085     }
02086 
02087   return TRUE;
02088 }
02089 
02090 /* Find any dynamic relocs that apply to read-only sections.  */
02091 
02092 static bfd_boolean
02093 readonly_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
02094 {
02095   struct _bfd_sparc_elf_link_hash_entry *eh;
02096   struct _bfd_sparc_elf_dyn_relocs *p;
02097 
02098   if (h->root.type == bfd_link_hash_warning)
02099     h = (struct elf_link_hash_entry *) h->root.u.i.link;
02100 
02101   eh = (struct _bfd_sparc_elf_link_hash_entry *) h;
02102   for (p = eh->dyn_relocs; p != NULL; p = p->next)
02103     {
02104       asection *s = p->sec->output_section;
02105 
02106       if (s != NULL && (s->flags & SEC_READONLY) != 0)
02107        {
02108          struct bfd_link_info *info = (struct bfd_link_info *) inf;
02109 
02110          info->flags |= DF_TEXTREL;
02111 
02112          /* Not an error, just cut short the traversal.  */
02113          return FALSE;
02114        }
02115     }
02116   return TRUE;
02117 }
02118 
02119 /* Return true if the dynamic symbol for a given section should be
02120    omitted when creating a shared library.  */
02121 
02122 bfd_boolean
02123 _bfd_sparc_elf_omit_section_dynsym (bfd *output_bfd,
02124                                 struct bfd_link_info *info,
02125                                 asection *p)
02126 {
02127   /* We keep the .got section symbol so that explicit relocations
02128      against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
02129      can be turned into relocations against the .got symbol.  */
02130   if (strcmp (p->name, ".got") == 0)
02131     return FALSE;
02132 
02133   return _bfd_elf_link_omit_section_dynsym (output_bfd, info, p);
02134 }
02135 
02136 /* Set the sizes of the dynamic sections.  */
02137 
02138 bfd_boolean
02139 _bfd_sparc_elf_size_dynamic_sections (bfd *output_bfd,
02140                                   struct bfd_link_info *info)
02141 {
02142   struct _bfd_sparc_elf_link_hash_table *htab;
02143   bfd *dynobj;
02144   asection *s;
02145   bfd *ibfd;
02146 
02147   htab = _bfd_sparc_elf_hash_table (info);
02148   dynobj = htab->elf.dynobj;
02149   BFD_ASSERT (dynobj != NULL);
02150 
02151   if (elf_hash_table (info)->dynamic_sections_created)
02152     {
02153       /* Set the contents of the .interp section to the interpreter.  */
02154       if (info->executable)
02155        {
02156          s = bfd_get_section_by_name (dynobj, ".interp");
02157          BFD_ASSERT (s != NULL);
02158          s->size = htab->dynamic_interpreter_size;
02159          s->contents = (unsigned char *) htab->dynamic_interpreter;
02160        }
02161     }
02162 
02163   /* Set up .got offsets for local syms, and space for local dynamic
02164      relocs.  */
02165   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
02166     {
02167       bfd_signed_vma *local_got;
02168       bfd_signed_vma *end_local_got;
02169       char *local_tls_type;
02170       bfd_size_type locsymcount;
02171       Elf_Internal_Shdr *symtab_hdr;
02172       asection *srel;
02173 
02174       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
02175        continue;
02176 
02177       for (s = ibfd->sections; s != NULL; s = s->next)
02178        {
02179          struct _bfd_sparc_elf_dyn_relocs *p;
02180 
02181          for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
02182            {
02183              if (!bfd_is_abs_section (p->sec)
02184                 && bfd_is_abs_section (p->sec->output_section))
02185               {
02186                 /* Input section has been discarded, either because
02187                    it is a copy of a linkonce section or due to
02188                    linker script /DISCARD/, so we'll be discarding
02189                    the relocs too.  */
02190               }
02191              else if (p->count != 0)
02192               {
02193                 srel = elf_section_data (p->sec)->sreloc;
02194                 srel->size += p->count * SPARC_ELF_RELA_BYTES (htab);
02195                 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
02196                   info->flags |= DF_TEXTREL;
02197               }
02198            }
02199        }
02200 
02201       local_got = elf_local_got_refcounts (ibfd);
02202       if (!local_got)
02203        continue;
02204 
02205       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
02206       locsymcount = symtab_hdr->sh_info;
02207       end_local_got = local_got + locsymcount;
02208       local_tls_type = _bfd_sparc_elf_local_got_tls_type (ibfd);
02209       s = htab->sgot;
02210       srel = htab->srelgot;
02211       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
02212        {
02213          if (*local_got > 0)
02214            {
02215              *local_got = s->size;
02216              s->size += SPARC_ELF_WORD_BYTES (htab);
02217              if (*local_tls_type == GOT_TLS_GD)
02218               s->size += SPARC_ELF_WORD_BYTES (htab);
02219              if (info->shared
02220                 || *local_tls_type == GOT_TLS_GD
02221                 || *local_tls_type == GOT_TLS_IE)
02222               srel->size += SPARC_ELF_RELA_BYTES (htab);
02223            }
02224          else
02225            *local_got = (bfd_vma) -1;
02226        }
02227     }
02228 
02229   if (htab->tls_ldm_got.refcount > 0)
02230     {
02231       /* Allocate 2 got entries and 1 dynamic reloc for
02232         R_SPARC_TLS_LDM_{HI22,LO10} relocs.  */
02233       htab->tls_ldm_got.offset = htab->sgot->size;
02234       htab->sgot->size += (2 * SPARC_ELF_WORD_BYTES (htab));
02235       htab->srelgot->size += SPARC_ELF_RELA_BYTES (htab);
02236     }
02237   else
02238     htab->tls_ldm_got.offset = -1;
02239 
02240   /* Allocate global sym .plt and .got entries, and space for global
02241      sym dynamic relocs.  */
02242   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
02243 
02244   if (! ABI_64_P (output_bfd)
02245       && !htab->is_vxworks
02246       && elf_hash_table (info)->dynamic_sections_created)
02247     {
02248       /* Make space for the trailing nop in .plt.  */
02249       if (htab->splt->size > 0)
02250        htab->splt->size += 1 * SPARC_INSN_BYTES;
02251 
02252       /* If the .got section is more than 0x1000 bytes, we add
02253         0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
02254         bit relocations have a greater chance of working.
02255 
02256         FIXME: Make this optimization work for 64-bit too.  */
02257       if (htab->sgot->size >= 0x1000
02258          && elf_hash_table (info)->hgot->root.u.def.value == 0)
02259        elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
02260     }
02261 
02262   /* The check_relocs and adjust_dynamic_symbol entry points have
02263      determined the sizes of the various dynamic sections.  Allocate
02264      memory for them.  */
02265   for (s = dynobj->sections; s != NULL; s = s->next)
02266     {
02267       if ((s->flags & SEC_LINKER_CREATED) == 0)
02268        continue;
02269 
02270       if (s == htab->splt
02271          || s == htab->sgot
02272          || s == htab->sdynbss
02273          || s == htab->sgotplt)
02274        {
02275          /* Strip this section if we don't need it; see the
02276             comment below.  */
02277        }
02278       else if (CONST_STRNEQ (s->name, ".rela"))
02279        {
02280          if (s->size != 0)
02281            {
02282              /* We use the reloc_count field as a counter if we need
02283                to copy relocs into the output file.  */
02284              s->reloc_count = 0;
02285            }
02286        }
02287       else
02288        {
02289          /* It's not one of our sections.  */
02290          continue;
02291        }
02292 
02293       if (s->size == 0)
02294        {
02295          /* If we don't need this section, strip it from the
02296             output file.  This is mostly to handle .rela.bss and
02297             .rela.plt.  We must create both sections in
02298             create_dynamic_sections, because they must be created
02299             before the linker maps input sections to output
02300             sections.  The linker does that before
02301             adjust_dynamic_symbol is called, and it is that
02302             function which decides whether anything needs to go
02303             into these sections.  */
02304          s->flags |= SEC_EXCLUDE;
02305          continue;
02306        }
02307 
02308       if ((s->flags & SEC_HAS_CONTENTS) == 0)
02309        continue;
02310 
02311       /* Allocate memory for the section contents.  Zero the memory
02312         for the benefit of .rela.plt, which has 4 unused entries
02313         at the beginning, and we don't want garbage.  */
02314       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
02315       if (s->contents == NULL)
02316        return FALSE;
02317     }
02318 
02319   if (elf_hash_table (info)->dynamic_sections_created)
02320     {
02321       /* Add some entries to the .dynamic section.  We fill in the
02322         values later, in _bfd_sparc_elf_finish_dynamic_sections, but we
02323         must add the entries now so that we get the correct size for
02324         the .dynamic section.  The DT_DEBUG entry is filled in by the
02325         dynamic linker and used by the debugger.  */
02326 #define add_dynamic_entry(TAG, VAL) \
02327   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
02328 
02329       if (info->executable)
02330        {
02331          if (!add_dynamic_entry (DT_DEBUG, 0))
02332            return FALSE;
02333        }
02334 
02335       if (htab->srelplt->size != 0)
02336        {
02337          if (!add_dynamic_entry (DT_PLTGOT, 0)
02338              || !add_dynamic_entry (DT_PLTRELSZ, 0)
02339              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
02340              || !add_dynamic_entry (DT_JMPREL, 0))
02341            return FALSE;
02342        }
02343 
02344       if (!add_dynamic_entry (DT_RELA, 0)
02345          || !add_dynamic_entry (DT_RELASZ, 0)
02346          || !add_dynamic_entry (DT_RELAENT,
02347                              SPARC_ELF_RELA_BYTES (htab)))
02348        return FALSE;
02349 
02350       /* If any dynamic relocs apply to a read-only section,
02351         then we need a DT_TEXTREL entry.  */
02352       if ((info->flags & DF_TEXTREL) == 0)
02353        elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
02354                             (PTR) info);
02355 
02356       if (info->flags & DF_TEXTREL)
02357        {
02358          if (!add_dynamic_entry (DT_TEXTREL, 0))
02359            return FALSE;
02360        }
02361 
02362       if (ABI_64_P (output_bfd))
02363        {
02364          int reg;
02365          struct _bfd_sparc_elf_app_reg * app_regs;
02366          struct elf_strtab_hash *dynstr;
02367          struct elf_link_hash_table *eht = elf_hash_table (info);
02368 
02369          /* Add dynamic STT_REGISTER symbols and corresponding DT_SPARC_REGISTER
02370             entries if needed.  */
02371          app_regs = _bfd_sparc_elf_hash_table (info)->app_regs;
02372          dynstr = eht->dynstr;
02373 
02374          for (reg = 0; reg < 4; reg++)
02375            if (app_regs [reg].name != NULL)
02376              {
02377               struct elf_link_local_dynamic_entry *entry, *e;
02378 
02379               if (!add_dynamic_entry (DT_SPARC_REGISTER, 0))
02380                 return FALSE;
02381 
02382               entry = (struct elf_link_local_dynamic_entry *)
02383                 bfd_hash_allocate (&info->hash->table, sizeof (*entry));
02384               if (entry == NULL)
02385                 return FALSE;
02386 
02387               /* We cheat here a little bit: the symbol will not be local, so we
02388                  put it at the end of the dynlocal linked list.  We will fix it
02389                  later on, as we have to fix other fields anyway.  */
02390               entry->isym.st_value = reg < 2 ? reg + 2 : reg + 4;
02391               entry->isym.st_size = 0;
02392               if (*app_regs [reg].name != '\0')
02393                 entry->isym.st_name
02394                   = _bfd_elf_strtab_add (dynstr, app_regs[reg].name, FALSE);
02395               else
02396                 entry->isym.st_name = 0;
02397               entry->isym.st_other = 0;
02398               entry->isym.st_info = ELF_ST_INFO (app_regs [reg].bind,
02399                                              STT_REGISTER);
02400               entry->isym.st_shndx = app_regs [reg].shndx;
02401               entry->next = NULL;
02402               entry->input_bfd = output_bfd;
02403               entry->input_indx = -1;
02404 
02405               if (eht->dynlocal == NULL)
02406                 eht->dynlocal = entry;
02407               else
02408                 {
02409                   for (e = eht->dynlocal; e->next; e = e->next)
02410                     ;
02411                   e->next = entry;
02412                 }
02413               eht->dynsymcount++;
02414              }
02415        }
02416     }
02417 #undef add_dynamic_entry
02418 
02419   return TRUE;
02420 }
02421 
02422 bfd_boolean
02423 _bfd_sparc_elf_new_section_hook (bfd *abfd, asection *sec)
02424 {
02425   if (!sec->used_by_bfd)
02426     {
02427       struct _bfd_sparc_elf_section_data *sdata;
02428       bfd_size_type amt = sizeof (*sdata);
02429 
02430       sdata = bfd_zalloc (abfd, amt);
02431       if (sdata == NULL)
02432        return FALSE;
02433       sec->used_by_bfd = sdata;
02434     }
02435 
02436   return _bfd_elf_new_section_hook (abfd, sec);
02437 }
02438 
02439 bfd_boolean
02440 _bfd_sparc_elf_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
02441                            struct bfd_section *section,
02442                            struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
02443                            bfd_boolean *again)
02444 {
02445   *again = FALSE;
02446   sec_do_relax (section) = 1;
02447   return TRUE;
02448 }
02449 
02450 /* Return the base VMA address which should be subtracted from real addresses
02451    when resolving @dtpoff relocation.
02452    This is PT_TLS segment p_vaddr.  */
02453 
02454 static bfd_vma
02455 dtpoff_base (struct bfd_link_info *info)
02456 {
02457   /* If tls_sec is NULL, we should have signalled an error already.  */
02458   if (elf_hash_table (info)->tls_sec == NULL)
02459     return 0;
02460   return elf_hash_table (info)->tls_sec->vma;
02461 }
02462 
02463 /* Return the relocation value for @tpoff relocation
02464    if STT_TLS virtual address is ADDRESS.  */
02465 
02466 static bfd_vma
02467 tpoff (struct bfd_link_info *info, bfd_vma address)
02468 {
02469   struct elf_link_hash_table *htab = elf_hash_table (info);
02470 
02471   /* If tls_sec is NULL, we should have signalled an error already.  */
02472   if (htab->tls_sec == NULL)
02473     return 0;
02474   return address - htab->tls_size - htab->tls_sec->vma;
02475 }
02476 
02477 /* Relocate a SPARC ELF section.  */
02478 
02479 bfd_boolean
02480 _bfd_sparc_elf_relocate_section (bfd *output_bfd,
02481                              struct bfd_link_info *info,
02482                              bfd *input_bfd,
02483                              asection *input_section,
02484                              bfd_byte *contents,
02485                              Elf_Internal_Rela *relocs,
02486                              Elf_Internal_Sym *local_syms,
02487                              asection **local_sections)
02488 {
02489   struct _bfd_sparc_elf_link_hash_table *htab;
02490   Elf_Internal_Shdr *symtab_hdr;
02491   struct elf_link_hash_entry **sym_hashes;
02492   bfd_vma *local_got_offsets;
02493   bfd_vma got_base;
02494   asection *sreloc;
02495   Elf_Internal_Rela *rel;
02496   Elf_Internal_Rela *relend;
02497   int num_relocs;
02498 
02499   htab = _bfd_sparc_elf_hash_table (info);
02500   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02501   sym_hashes = elf_sym_hashes (input_bfd);
02502   local_got_offsets = elf_local_got_offsets (input_bfd);
02503 
02504   if (elf_hash_table (info)->hgot == NULL)
02505     got_base = 0;
02506   else
02507     got_base = elf_hash_table (info)->hgot->root.u.def.value;
02508 
02509   sreloc = elf_section_data (input_section)->sreloc;
02510 
02511   rel = relocs;
02512   if (ABI_64_P (output_bfd))
02513     num_relocs = NUM_SHDR_ENTRIES (& elf_section_data (input_section)->rel_hdr);
02514   else
02515     num_relocs = input_section->reloc_count;
02516   relend = relocs + num_relocs;
02517   for (; rel < relend; rel++)
02518     {
02519       int r_type, tls_type;
02520       reloc_howto_type *howto;
02521       unsigned long r_symndx;
02522       struct elf_link_hash_entry *h;
02523       Elf_Internal_Sym *sym;
02524       asection *sec;
02525       bfd_vma relocation, off;
02526       bfd_reloc_status_type r;
02527       bfd_boolean is_plt = FALSE;
02528       bfd_boolean unresolved_reloc;
02529 
02530       r_type = SPARC_ELF_R_TYPE (rel->r_info);
02531       if (r_type == R_SPARC_GNU_VTINHERIT
02532          || r_type == R_SPARC_GNU_VTENTRY)
02533        continue;
02534 
02535       if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
02536        {
02537          bfd_set_error (bfd_error_bad_value);
02538          return FALSE;
02539        }
02540       howto = _bfd_sparc_elf_howto_table + r_type;
02541 
02542       r_symndx = SPARC_ELF_R_SYMNDX (htab, rel->r_info);
02543       h = NULL;
02544       sym = NULL;
02545       sec = NULL;
02546       unresolved_reloc = FALSE;
02547       if (r_symndx < symtab_hdr->sh_info)
02548        {
02549          sym = local_syms + r_symndx;
02550          sec = local_sections[r_symndx];
02551          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
02552        }
02553       else
02554        {
02555          bfd_boolean warned;
02556 
02557          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
02558                                r_symndx, symtab_hdr, sym_hashes,
02559                                h, sec, relocation,
02560                                unresolved_reloc, warned);
02561          if (warned)
02562            {
02563              /* To avoid generating warning messages about truncated
02564                relocations, set the relocation's address to be the same as
02565                the start of this section.  */
02566              if (input_section->output_section != NULL)
02567               relocation = input_section->output_section->vma;
02568              else
02569               relocation = 0;
02570            }
02571        }
02572 
02573       if (sec != NULL && elf_discarded_section (sec))
02574        {
02575          /* For relocs against symbols from removed linkonce
02576             sections, or sections discarded by a linker script, we
02577             just want the section contents zeroed.  Avoid any
02578             special processing.  */
02579          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
02580          rel->r_info = 0;
02581          rel->r_addend = 0;
02582          continue;
02583        }
02584 
02585       if (info->relocatable)
02586        continue;
02587 
02588       switch (r_type)
02589        {
02590        case R_SPARC_GOT10:
02591        case R_SPARC_GOT13:
02592        case R_SPARC_GOT22:
02593          /* Relocation is to the entry for this symbol in the global
02594             offset table.  */
02595          if (htab->sgot == NULL)
02596            abort ();
02597 
02598          if (h != NULL)
02599            {
02600              bfd_boolean dyn;
02601 
02602              off = h->got.offset;
02603              BFD_ASSERT (off != (bfd_vma) -1);
02604              dyn = elf_hash_table (info)->dynamic_sections_created;
02605 
02606              if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
02607                 || (info->shared
02608                     && (info->symbolic
02609                        || h->dynindx == -1
02610                        || h->forced_local)
02611                     && h->def_regular))
02612               {
02613                 /* This is actually a static link, or it is a
02614                    -Bsymbolic link and the symbol is defined
02615                    locally, or the symbol was forced to be local
02616                    because of a version file.  We must initialize
02617                    this entry in the global offset table.  Since the
02618                    offset must always be a multiple of 8 for 64-bit
02619                    and 4 for 32-bit, we use the least significant bit
02620                    to record whether we have initialized it already.
02621 
02622                    When doing a dynamic link, we create a .rela.got
02623                    relocation entry to initialize the value.  This
02624                    is done in the finish_dynamic_symbol routine.  */
02625                 if ((off & 1) != 0)
02626                   off &= ~1;
02627                 else
02628                   {
02629                     SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
02630                                      htab->sgot->contents + off);
02631                     h->got.offset |= 1;
02632                   }
02633               }
02634              else
02635               unresolved_reloc = FALSE;
02636            }
02637          else
02638            {
02639              BFD_ASSERT (local_got_offsets != NULL
02640                        && local_got_offsets[r_symndx] != (bfd_vma) -1);
02641 
02642              off = local_got_offsets[r_symndx];
02643 
02644              /* The offset must always be a multiple of 8 on 64-bit and
02645                4 on 32-bit.  We use the least significant bit to record
02646                whether we have already processed this entry.  */
02647              if ((off & 1) != 0)
02648               off &= ~1;
02649              else
02650               {
02651 
02652                 if (info->shared)
02653                   {
02654                     asection *s;
02655                     Elf_Internal_Rela outrel;
02656 
02657                     /* We need to generate a R_SPARC_RELATIVE reloc
02658                       for the dynamic linker.  */
02659                     s = htab->srelgot;
02660                     BFD_ASSERT (s != NULL);
02661 
02662                     outrel.r_offset = (htab->sgot->output_section->vma
02663                                     + htab->sgot->output_offset
02664                                     + off);
02665                     outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
02666                                                  0, R_SPARC_RELATIVE);
02667                     outrel.r_addend = relocation;
02668                     relocation = 0;
02669                     sparc_elf_append_rela (output_bfd, s, &outrel);
02670                   }
02671 
02672                 SPARC_ELF_PUT_WORD (htab, output_bfd, relocation,
02673                                   htab->sgot->contents + off);
02674                 local_got_offsets[r_symndx] |= 1;
02675               }
02676            }
02677          relocation = htab->sgot->output_offset + off - got_base;
02678          break;
02679 
02680        case R_SPARC_PLT32:
02681        case R_SPARC_PLT64:
02682          if (h == NULL || h->plt.offset == (bfd_vma) -1)
02683            {
02684              r_type = (r_type == R_SPARC_PLT32) ? R_SPARC_32 : R_SPARC_64;
02685              goto r_sparc_plt32;
02686            }
02687          /* Fall through.  */
02688 
02689        case R_SPARC_WPLT30:
02690        case R_SPARC_HIPLT22:
02691        case R_SPARC_LOPLT10:
02692        case R_SPARC_PCPLT32:
02693        case R_SPARC_PCPLT22:
02694        case R_SPARC_PCPLT10:
02695        r_sparc_wplt30:
02696          /* Relocation is to the entry for this symbol in the
02697             procedure linkage table.  */
02698 
02699          if (! ABI_64_P (output_bfd))
02700            {
02701              /* The Solaris native assembler will generate a WPLT30 reloc
02702                for a local symbol if you assemble a call from one
02703                section to another when using -K pic.  We treat it as
02704                WDISP30.  */
02705              if (h == NULL)
02706               break;
02707            }
02708          else
02709            {
02710              BFD_ASSERT (h != NULL);
02711            }
02712 
02713          if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL)
02714            {
02715              /* We didn't make a PLT entry for this symbol.  This
02716                happens when statically linking PIC code, or when
02717                using -Bsymbolic.  */
02718              break;
02719            }
02720 
02721          relocation = (htab->splt->output_section->vma
02722                      + htab->splt->output_offset
02723                      + h->plt.offset);
02724          unresolved_reloc = FALSE;
02725          if (r_type == R_SPARC_PLT32 || r_type == R_SPARC_PLT64)
02726            {
02727              r_type = r_type == R_SPARC_PLT32 ? R_SPARC_32 : R_SPARC_64;
02728              is_plt = TRUE;
02729              goto r_sparc_plt32;
02730            }
02731          break;
02732 
02733        case R_SPARC_PC10:
02734        case R_SPARC_PC22:
02735        case R_SPARC_PC_HH22:
02736        case R_SPARC_PC_HM10:
02737        case R_SPARC_PC_LM22:
02738          if (h != NULL
02739              && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
02740            break;
02741          /* Fall through.  */
02742        case R_SPARC_DISP8:
02743        case R_SPARC_DISP16:
02744        case R_SPARC_DISP32:
02745        case R_SPARC_DISP64:
02746        case R_SPARC_WDISP30:
02747        case R_SPARC_WDISP22:
02748        case R_SPARC_WDISP19:
02749        case R_SPARC_WDISP16:
02750        case R_SPARC_8:
02751        case R_SPARC_16:
02752        case R_SPARC_32:
02753        case R_SPARC_HI22:
02754        case R_SPARC_22:
02755        case R_SPARC_13:
02756        case R_SPARC_LO10:
02757        case R_SPARC_UA16:
02758        case R_SPARC_UA32:
02759        case R_SPARC_10:
02760        case R_SPARC_11:
02761        case R_SPARC_64:
02762        case R_SPARC_OLO10:
02763        case R_SPARC_HH22:
02764        case R_SPARC_HM10:
02765        case R_SPARC_LM22:
02766        case R_SPARC_7:
02767        case R_SPARC_5:
02768        case R_SPARC_6:
02769        case R_SPARC_HIX22:
02770        case R_SPARC_LOX10:
02771        case R_SPARC_H44:
02772        case R_SPARC_M44:
02773        case R_SPARC_L44:
02774        case R_SPARC_UA64:
02775        r_sparc_plt32:
02776          if ((input_section->flags & SEC_ALLOC) == 0)
02777            break;
02778 
02779          if ((info->shared
02780               && (h == NULL
02781                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02782                  || h->root.type != bfd_link_hash_undefweak)
02783               && (! howto->pc_relative
02784                  || (h != NULL
02785                      && h->dynindx != -1
02786                      && (! info->symbolic
02787                         || !h->def_regular))))
02788              || (!info->shared
02789                 && h != NULL
02790                 && h->dynindx != -1
02791                 && !h->non_got_ref
02792                 && ((h->def_dynamic
02793                      && !h->def_regular)
02794                     || h->root.type == bfd_link_hash_undefweak
02795                     || h->root.type == bfd_link_hash_undefined)))
02796            {
02797              Elf_Internal_Rela outrel;
02798              bfd_boolean skip, relocate = FALSE;
02799 
02800              /* When generating a shared object, these relocations
02801                are copied into the output file to be resolved at run
02802                time.  */
02803 
02804              BFD_ASSERT (sreloc != NULL);
02805 
02806              skip = FALSE;
02807 
02808              outrel.r_offset =
02809               _bfd_elf_section_offset (output_bfd, info, input_section,
02810                                     rel->r_offset);
02811              if (outrel.r_offset == (bfd_vma) -1)
02812               skip = TRUE;
02813              else if (outrel.r_offset == (bfd_vma) -2)
02814               skip = TRUE, relocate = TRUE;
02815              outrel.r_offset += (input_section->output_section->vma
02816                               + input_section->output_offset);
02817 
02818              /* Optimize unaligned reloc usage now that we know where
02819                it finally resides.  */
02820              switch (r_type)
02821               {
02822               case R_SPARC_16:
02823                 if (outrel.r_offset & 1)
02824                   r_type = R_SPARC_UA16;
02825                 break;
02826               case R_SPARC_UA16:
02827                 if (!(outrel.r_offset & 1))
02828                   r_type = R_SPARC_16;
02829                 break;
02830               case R_SPARC_32:
02831                 if (outrel.r_offset & 3)
02832                   r_type = R_SPARC_UA32;
02833                 break;
02834               case R_SPARC_UA32:
02835                 if (!(outrel.r_offset & 3))
02836                   r_type = R_SPARC_32;
02837                 break;
02838               case R_SPARC_64:
02839                 if (outrel.r_offset & 7)
02840                   r_type = R_SPARC_UA64;
02841                 break;
02842               case R_SPARC_UA64:
02843                 if (!(outrel.r_offset & 7))
02844                   r_type = R_SPARC_64;
02845                 break;
02846               case R_SPARC_DISP8:
02847               case R_SPARC_DISP16:
02848               case R_SPARC_DISP32:
02849               case R_SPARC_DISP64:
02850                 /* If the symbol is not dynamic, we should not keep
02851                    a dynamic relocation.  But an .rela.* slot has been
02852                    allocated for it, output R_SPARC_NONE.
02853                    FIXME: Add code tracking needed dynamic relocs as
02854                    e.g. i386 has.  */
02855                 if (h->dynindx == -1)
02856                   skip = TRUE, relocate = TRUE;
02857                 break;
02858               }
02859 
02860              if (skip)
02861               memset (&outrel, 0, sizeof outrel);
02862              /* h->dynindx may be -1 if the symbol was marked to
02863                become local.  */
02864              else if (h != NULL && ! is_plt
02865                      && ((! info->symbolic && h->dynindx != -1)
02866                         || !h->def_regular))
02867               {
02868                 BFD_ASSERT (h->dynindx != -1);
02869                 outrel.r_info = SPARC_ELF_R_INFO (htab, rel, h->dynindx, r_type);
02870                 outrel.r_addend = rel->r_addend;
02871               }
02872              else
02873               {
02874                 if (r_type == R_SPARC_32 || r_type == R_SPARC_64)
02875                   {
02876                     outrel.r_info = SPARC_ELF_R_INFO (htab, NULL,
02877                                                  0, R_SPARC_RELATIVE);
02878                     outrel.r_addend = relocation + rel->r_addend;
02879                   }
02880                 else
02881                   {
02882                     long indx;
02883 
02884                     outrel.r_addend = relocation + rel->r_addend;
02885 
02886                     if (is_plt)
02887                      sec = htab->splt;
02888 
02889                     if (bfd_is_abs_section (sec))
02890                      indx = 0;
02891                     else if (sec == NULL || sec->owner == NULL)
02892                      {
02893                        bfd_set_error (bfd_error_bad_value);
02894                        return FALSE;
02895                      }
02896                     else
02897                      {
02898                        asection *osec;
02899 
02900                        /* We are turning this relocation into one
02901                           against a section symbol.  It would be
02902                           proper to subtract the symbol's value,
02903                           osec->vma, from the emitted reloc addend,
02904                           but ld.so expects buggy relocs.  */
02905                        osec = sec->output_section;
02906                        indx = elf_section_data (osec)->dynindx;
02907 
02908                        if (indx == 0)
02909                          {
02910                            osec = htab->elf.text_index_section;
02911                            indx = elf_section_data (osec)->dynindx;
02912                          }
02913 
02914                        /* FIXME: we really should be able to link non-pic
02915                           shared libraries.  */
02916                        if (indx == 0)
02917                          {
02918                            BFD_FAIL ();
02919                            (*_bfd_error_handler)
02920                             (_("%B: probably compiled without -fPIC?"),
02921                              input_bfd);
02922                            bfd_set_error (bfd_error_bad_value);
02923                            return FALSE;
02924                          }
02925                      }
02926 
02927                     outrel.r_info = SPARC_ELF_R_INFO (htab, rel, indx,
02928                                                  r_type);
02929                   }
02930               }
02931 
02932              sparc_elf_append_rela (output_bfd, sreloc, &outrel);
02933 
02934              /* This reloc will be computed at runtime, so there's no
02935                need to do anything now.  */
02936              if (! relocate)
02937               continue;
02938            }
02939          break;
02940 
02941        case R_SPARC_TLS_GD_HI22:
02942          if (! ABI_64_P (input_bfd)
02943              && ! _bfd_sparc_elf_tdata (input_bfd)->has_tlsgd)
02944            {
02945              /* R_SPARC_REV32 used the same reloc number as
02946                R_SPARC_TLS_GD_HI22.  */
02947              r_type = R_SPARC_REV32;
02948              break;
02949            }
02950          /* Fall through */
02951 
02952        case R_SPARC_TLS_GD_LO10:
02953        case R_SPARC_TLS_IE_HI22:
02954        case R_SPARC_TLS_IE_LO10:
02955          r_type = sparc_elf_tls_transition (info, input_bfd, r_type, h == NULL);
02956          tls_type = GOT_UNKNOWN;
02957          if (h == NULL && local_got_offsets)
02958            tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
02959          else if (h != NULL)
02960            {
02961              tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
02962              if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
02963               switch (SPARC_ELF_R_TYPE (rel->r_info))
02964                 {
02965                 case R_SPARC_TLS_GD_HI22:
02966                 case R_SPARC_TLS_IE_HI22:
02967                   r_type = R_SPARC_TLS_LE_HIX22;
02968                   break;
02969                 default:
02970                   r_type = R_SPARC_TLS_LE_LOX10;
02971                   break;
02972                 }
02973            }
02974          if (tls_type == GOT_TLS_IE)
02975            switch (r_type)
02976              {
02977              case R_SPARC_TLS_GD_HI22:
02978               r_type = R_SPARC_TLS_IE_HI22;
02979               break;
02980              case R_SPARC_TLS_GD_LO10:
02981               r_type = R_SPARC_TLS_IE_LO10;
02982               break;
02983              }
02984 
02985          if (r_type == R_SPARC_TLS_LE_HIX22)
02986            {
02987              relocation = tpoff (info, relocation);
02988              break;
02989            }
02990          if (r_type == R_SPARC_TLS_LE_LOX10)
02991            {
02992              /* Change add into xor.  */
02993              relocation = tpoff (info, relocation);
02994              bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
02995                                              contents + rel->r_offset)
02996                                    | 0x80182000), contents + rel->r_offset);
02997              break;
02998            }
02999 
03000          if (h != NULL)
03001            {
03002              off = h->got.offset;
03003              h->got.offset |= 1;
03004            }
03005          else
03006            {
03007              BFD_ASSERT (local_got_offsets != NULL);
03008              off = local_got_offsets[r_symndx];
03009              local_got_offsets[r_symndx] |= 1;
03010            }
03011 
03012        r_sparc_tlsldm:
03013          if (htab->sgot == NULL)
03014            abort ();
03015 
03016          if ((off & 1) != 0)
03017            off &= ~1;
03018          else
03019            {
03020              Elf_Internal_Rela outrel;
03021              int dr_type, indx;
03022 
03023              if (htab->srelgot == NULL)
03024               abort ();
03025 
03026              SPARC_ELF_PUT_WORD (htab, output_bfd, 0, htab->sgot->contents + off);
03027              outrel.r_offset = (htab->sgot->output_section->vma
03028                              + htab->sgot->output_offset + off);
03029              indx = h && h->dynindx != -1 ? h->dynindx : 0;
03030              if (r_type == R_SPARC_TLS_IE_HI22
03031                 || r_type == R_SPARC_TLS_IE_LO10)
03032               dr_type = SPARC_ELF_TPOFF_RELOC (htab);
03033              else
03034               dr_type = SPARC_ELF_DTPMOD_RELOC (htab);
03035              if (dr_type == SPARC_ELF_TPOFF_RELOC (htab) && indx == 0)
03036               outrel.r_addend = relocation - dtpoff_base (info);
03037              else
03038               outrel.r_addend = 0;
03039              outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx, dr_type);
03040              sparc_elf_append_rela (output_bfd, htab->srelgot, &outrel);
03041 
03042              if (r_type == R_SPARC_TLS_GD_HI22
03043                 || r_type == R_SPARC_TLS_GD_LO10)
03044               {
03045                 if (indx == 0)
03046                   {
03047                     BFD_ASSERT (! unresolved_reloc);
03048                     SPARC_ELF_PUT_WORD (htab, output_bfd,
03049                                      relocation - dtpoff_base (info),
03050                                      (htab->sgot->contents + off
03051                                       + SPARC_ELF_WORD_BYTES (htab)));
03052                   }
03053                 else
03054                   {
03055                     SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
03056                                      (htab->sgot->contents + off
03057                                       + SPARC_ELF_WORD_BYTES (htab)));
03058                     outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, indx,
03059                                                  SPARC_ELF_DTPOFF_RELOC (htab));
03060                     outrel.r_offset += SPARC_ELF_WORD_BYTES (htab);
03061                     sparc_elf_append_rela (output_bfd, htab->srelgot,
03062                                         &outrel);
03063                   }
03064               }
03065              else if (dr_type == SPARC_ELF_DTPMOD_RELOC (htab))
03066               {
03067                 SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
03068                                   (htab->sgot->contents + off
03069                                    + SPARC_ELF_WORD_BYTES (htab)));
03070               }
03071            }
03072 
03073          if (off >= (bfd_vma) -2)
03074            abort ();
03075 
03076          relocation = htab->sgot->output_offset + off - got_base;
03077          unresolved_reloc = FALSE;
03078          howto = _bfd_sparc_elf_howto_table + r_type;
03079          break;
03080 
03081        case R_SPARC_TLS_LDM_HI22:
03082        case R_SPARC_TLS_LDM_LO10:
03083          if (! info->shared)
03084            {
03085              bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
03086              continue;
03087            }
03088          off = htab->tls_ldm_got.offset;
03089          htab->tls_ldm_got.offset |= 1;
03090          goto r_sparc_tlsldm;
03091 
03092        case R_SPARC_TLS_LDO_HIX22:
03093        case R_SPARC_TLS_LDO_LOX10:
03094          if (info->shared)
03095            {
03096              relocation -= dtpoff_base (info);
03097              break;
03098            }
03099 
03100          r_type = (r_type == R_SPARC_TLS_LDO_HIX22
03101                   ? R_SPARC_TLS_LE_HIX22 : R_SPARC_TLS_LE_LOX10);
03102          /* Fall through.  */
03103 
03104        case R_SPARC_TLS_LE_HIX22:
03105        case R_SPARC_TLS_LE_LOX10:
03106          if (info->shared)
03107            {
03108              Elf_Internal_Rela outrel;
03109              bfd_boolean skip, relocate = FALSE;
03110 
03111              BFD_ASSERT (sreloc != NULL);
03112              skip = FALSE;
03113              outrel.r_offset =
03114               _bfd_elf_section_offset (output_bfd, info, input_section,
03115                                     rel->r_offset);
03116              if (outrel.r_offset == (bfd_vma) -1)
03117               skip = TRUE;
03118              else if (outrel.r_offset == (bfd_vma) -2)
03119               skip = TRUE, relocate = TRUE;
03120              outrel.r_offset += (input_section->output_section->vma
03121                               + input_section->output_offset);
03122              if (skip)
03123               memset (&outrel, 0, sizeof outrel);
03124              else
03125               {
03126                 outrel.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, r_type);
03127                 outrel.r_addend = relocation - dtpoff_base (info)
03128                                 + rel->r_addend;
03129               }
03130 
03131              sparc_elf_append_rela (output_bfd, sreloc, &outrel);
03132              continue;
03133            }
03134          relocation = tpoff (info, relocation);
03135          break;
03136 
03137        case R_SPARC_TLS_LDM_CALL:
03138          if (! info->shared)
03139            {
03140              /* mov %g0, %o0 */
03141              bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
03142              continue;
03143            }
03144          /* Fall through */
03145 
03146        case R_SPARC_TLS_GD_CALL:
03147          tls_type = GOT_UNKNOWN;
03148          if (h == NULL && local_got_offsets)
03149            tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
03150          else if (h != NULL)
03151            tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
03152          if (! info->shared
03153              || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
03154            {
03155              bfd_vma insn;
03156 
03157              if (!info->shared && (h == NULL || h->dynindx == -1))
03158               {
03159                 /* GD -> LE */
03160                 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
03161                 continue;
03162               }
03163 
03164              /* GD -> IE */
03165              if (rel + 1 < relend
03166                 && SPARC_ELF_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
03167                 && rel[1].r_offset == rel->r_offset + 4
03168                 && SPARC_ELF_R_SYMNDX (htab, rel[1].r_info) == r_symndx
03169                 && (((insn = bfd_get_32 (input_bfd,
03170                                       contents + rel[1].r_offset))
03171                      >> 25) & 0x1f) == 8)
03172               {
03173                 /* We have
03174                    call __tls_get_addr, %tgd_call(foo)
03175                     add %reg1, %reg2, %o0, %tgd_add(foo)
03176                    and change it into IE:
03177                    {ld,ldx} [%reg1 + %reg2], %o0, %tie_ldx(foo)
03178                    add %g7, %o0, %o0, %tie_add(foo).
03179                    add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
03180                    ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2,
03181                    ldx is 0xc0580000 | (rd << 25) | (rs1 << 14) | rs2.  */
03182                 bfd_put_32 (output_bfd, insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000),
03183                            contents + rel->r_offset);
03184                 bfd_put_32 (output_bfd, 0x9001c008,
03185                            contents + rel->r_offset + 4);
03186                 rel++;
03187                 continue;
03188               }
03189 
03190              bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset);
03191              continue;
03192            }
03193 
03194          h = (struct elf_link_hash_entry *)
03195              bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
03196                                 FALSE, TRUE);
03197          BFD_ASSERT (h != NULL);
03198          r_type = R_SPARC_WPLT30;
03199          howto = _bfd_sparc_elf_howto_table + r_type;
03200          goto r_sparc_wplt30;
03201 
03202        case R_SPARC_TLS_GD_ADD:
03203          tls_type = GOT_UNKNOWN;
03204          if (h == NULL && local_got_offsets)
03205            tls_type = _bfd_sparc_elf_local_got_tls_type (input_bfd) [r_symndx];
03206          else if (h != NULL)
03207            tls_type = _bfd_sparc_elf_hash_entry(h)->tls_type;
03208          if (! info->shared || tls_type == GOT_TLS_IE)
03209            {
03210              /* add %reg1, %reg2, %reg3, %tgd_add(foo)
03211                changed into IE:
03212                {ld,ldx} [%reg1 + %reg2], %reg3, %tie_ldx(foo)
03213                or LE:
03214                add %g7, %reg2, %reg3.  */
03215              bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
03216              if ((h != NULL && h->dynindx != -1) || info->shared)
03217               relocation = insn | (ABI_64_P (output_bfd) ? 0xc0580000 : 0xc0000000);
03218              else
03219               relocation = (insn & ~0x7c000) | 0x1c000;
03220              bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
03221            }
03222          continue;
03223 
03224        case R_SPARC_TLS_LDM_ADD:
03225          if (! info->shared)
03226            bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
03227          continue;
03228 
03229        case R_SPARC_TLS_LDO_ADD:
03230          if (! info->shared)
03231            {
03232              /* Change rs1 into %g7.  */
03233              bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
03234              insn = (insn & ~0x7c000) | 0x1c000;
03235              bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
03236            }
03237          continue;
03238 
03239        case R_SPARC_TLS_IE_LD:
03240        case R_SPARC_TLS_IE_LDX:
03241          if (! info->shared && (h == NULL || h->dynindx == -1))
03242            {
03243              bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
03244              int rs2 = insn & 0x1f;
03245              int rd = (insn >> 25) & 0x1f;
03246 
03247              if (rs2 == rd)
03248               relocation = SPARC_NOP;
03249              else
03250               relocation = 0x80100000 | (insn & 0x3e00001f);
03251              bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
03252            }
03253          continue;
03254 
03255        case R_SPARC_TLS_IE_ADD:
03256          /* Totally useless relocation.  */
03257          continue;
03258 
03259        case R_SPARC_TLS_DTPOFF32:
03260        case R_SPARC_TLS_DTPOFF64:
03261          relocation -= dtpoff_base (info);
03262          break;
03263 
03264        default:
03265          break;
03266        }
03267 
03268       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
03269         because such sections are not SEC_ALLOC and thus ld.so will
03270         not process them.  */
03271       if (unresolved_reloc
03272          && !((input_section->flags & SEC_DEBUGGING) != 0
03273               && h->def_dynamic))
03274        (*_bfd_error_handler)
03275          (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
03276           input_bfd,
03277           input_section,
03278           (long) rel->r_offset,
03279           howto->name,
03280           h->root.root.string);
03281 
03282       r = bfd_reloc_continue;
03283       if (r_type == R_SPARC_OLO10)
03284        {
03285            bfd_vma x;
03286 
03287            if (! ABI_64_P (output_bfd))
03288              abort ();
03289 
03290            relocation += rel->r_addend;
03291            relocation = (relocation & 0x3ff) + ELF64_R_TYPE_DATA (rel->r_info);
03292 
03293            x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03294            x = (x & ~(bfd_vma) 0x1fff) | (relocation & 0x1fff);
03295            bfd_put_32 (input_bfd, x, contents + rel->r_offset);
03296 
03297            r = bfd_check_overflow (howto->complain_on_overflow,
03298                                 howto->bitsize, howto->rightshift,
03299                                 bfd_arch_bits_per_address (input_bfd),
03300                                 relocation);
03301        }
03302       else if (r_type == R_SPARC_WDISP16)
03303        {
03304          bfd_vma x;
03305 
03306          relocation += rel->r_addend;
03307          relocation -= (input_section->output_section->vma
03308                       + input_section->output_offset);
03309          relocation -= rel->r_offset;
03310 
03311          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03312          x |= ((((relocation >> 2) & 0xc000) << 6)
03313               | ((relocation >> 2) & 0x3fff));
03314          bfd_put_32 (input_bfd, x, contents + rel->r_offset);
03315 
03316          r = bfd_check_overflow (howto->complain_on_overflow,
03317                               howto->bitsize, howto->rightshift,
03318                               bfd_arch_bits_per_address (input_bfd),
03319                               relocation);
03320        }
03321       else if (r_type == R_SPARC_REV32)
03322        {
03323          bfd_vma x;
03324 
03325          relocation = relocation + rel->r_addend;
03326 
03327          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03328          x = x + relocation;
03329          bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
03330          r = bfd_reloc_ok;
03331        }
03332       else if (r_type == R_SPARC_TLS_LDO_HIX22
03333               || r_type == R_SPARC_TLS_LE_HIX22)
03334        {
03335          bfd_vma x;
03336 
03337          relocation += rel->r_addend;
03338          if (r_type == R_SPARC_TLS_LE_HIX22)
03339            relocation ^= MINUS_ONE;
03340 
03341          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03342          x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
03343          bfd_put_32 (input_bfd, x, contents + rel->r_offset);
03344          r = bfd_reloc_ok;
03345        }
03346       else if (r_type == R_SPARC_TLS_LDO_LOX10
03347               || r_type == R_SPARC_TLS_LE_LOX10)
03348        {
03349          bfd_vma x;
03350 
03351          relocation += rel->r_addend;
03352          relocation &= 0x3ff;
03353          if (r_type == R_SPARC_TLS_LE_LOX10)
03354            relocation |= 0x1c00;
03355 
03356          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03357          x = (x & ~(bfd_vma) 0x1fff) | relocation;
03358          bfd_put_32 (input_bfd, x, contents + rel->r_offset);
03359 
03360          r = bfd_reloc_ok;
03361        }
03362       else if (r_type == R_SPARC_HIX22)
03363        {
03364          bfd_vma x;
03365 
03366          relocation += rel->r_addend;
03367          relocation = relocation ^ MINUS_ONE;
03368 
03369          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03370          x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
03371          bfd_put_32 (input_bfd, x, contents + rel->r_offset);
03372 
03373          r = bfd_check_overflow (howto->complain_on_overflow,
03374                               howto->bitsize, howto->rightshift,
03375                               bfd_arch_bits_per_address (input_bfd),
03376                               relocation);
03377        }
03378       else if (r_type == R_SPARC_LOX10)
03379        {
03380          bfd_vma x;
03381 
03382          relocation += rel->r_addend;
03383          relocation = (relocation & 0x3ff) | 0x1c00;
03384 
03385          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03386          x = (x & ~(bfd_vma) 0x1fff) | relocation;
03387          bfd_put_32 (input_bfd, x, contents + rel->r_offset);
03388 
03389          r = bfd_reloc_ok;
03390        }
03391       else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
03392               && sec_do_relax (input_section)
03393               && rel->r_offset + 4 < input_section->size)
03394        {
03395 #define G0           0
03396 #define O7           15
03397 #define XCC          (2 << 20)
03398 #define COND(x)             (((x)&0xf)<<25)
03399 #define CONDA        COND(0x8)
03400 #define INSN_BPA     (F2(0,1) | CONDA | BPRED | XCC)
03401 #define INSN_BA             (F2(0,2) | CONDA)
03402 #define INSN_OR             F3(2, 0x2, 0)
03403 #define INSN_NOP     F2(0,4)
03404 
03405          bfd_vma x, y;
03406 
03407          /* If the instruction is a call with either:
03408             restore
03409             arithmetic instruction with rd == %o7
03410             where rs1 != %o7 and rs2 if it is register != %o7
03411             then we can optimize if the call destination is near
03412             by changing the call into a branch always.  */
03413          x = bfd_get_32 (input_bfd, contents + rel->r_offset);
03414          y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
03415          if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
03416            {
03417              if (((y & OP3(~0)) == OP3(0x3d) /* restore */
03418                  || ((y & OP3(0x28)) == 0 /* arithmetic */
03419                      && (y & RD(~0)) == RD(O7)))
03420                 && (y & RS1(~0)) != RS1(O7)
03421                 && ((y & F3I(~0))
03422                     || (y & RS2(~0)) != RS2(O7)))
03423               {
03424                 bfd_vma reloc;
03425 
03426                 reloc = relocation + rel->r_addend - rel->r_offset;
03427                 reloc -= (input_section->output_section->vma
03428                          + input_section->output_offset);
03429 
03430                 /* Ensure the branch fits into simm22.  */
03431                 if ((reloc & 3) == 0
03432                     && ((reloc & ~(bfd_vma)0x7fffff) == 0
03433                        || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
03434                   {
03435                     reloc >>= 2;
03436 
03437                     /* Check whether it fits into simm19.  */
03438                     if (((reloc & 0x3c0000) == 0
03439                         || (reloc & 0x3c0000) == 0x3c0000)
03440                        && (ABI_64_P (output_bfd)
03441                            || elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
03442                      x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
03443                     else
03444                      x = INSN_BA | (reloc & 0x3fffff); /* ba */
03445                     bfd_put_32 (input_bfd, x, contents + rel->r_offset);
03446                     r = bfd_reloc_ok;
03447                     if (rel->r_offset >= 4
03448                        && (y & (0xffffffff ^ RS1(~0)))
03449                           == (INSN_OR | RD(O7) | RS2(G0)))
03450                      {
03451                        bfd_vma z;
03452                        unsigned int reg;
03453 
03454                        z = bfd_get_32 (input_bfd,
03455                                      contents + rel->r_offset - 4);
03456                        if ((z & (0xffffffff ^ RD(~0)))
03457                            != (INSN_OR | RS1(O7) | RS2(G0)))
03458                          break;
03459 
03460                        /* The sequence was
03461                           or %o7, %g0, %rN
03462                           call foo
03463                           or %rN, %g0, %o7
03464 
03465                           If call foo was replaced with ba, replace
03466                           or %rN, %g0, %o7 with nop.  */
03467 
03468                        reg = (y & RS1(~0)) >> 14;
03469                        if (reg != ((z & RD(~0)) >> 25)
03470                            || reg == G0 || reg == O7)
03471                          break;
03472 
03473                        bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
03474                                   contents + rel->r_offset + 4);
03475                      }
03476 
03477                   }
03478               }
03479            }
03480        }
03481 
03482       if (r == bfd_reloc_continue)
03483        r = _bfd_final_link_relocate (howto, input_bfd, input_section,
03484                                   contents, rel->r_offset,
03485                                   relocation, rel->r_addend);
03486 
03487       if (r != bfd_reloc_ok)
03488        {
03489          switch (r)
03490            {
03491            default:
03492            case bfd_reloc_outofrange:
03493              abort ();
03494            case bfd_reloc_overflow:
03495              {
03496               const char *name;
03497 
03498               /* The Solaris native linker silently disregards overflows. 
03499                  We don't, but this breaks stabs debugging info, whose
03500                  relocations are only 32-bits wide.  Ignore overflows in
03501                  this case and also for discarded entries.  */
03502               if ((r_type == R_SPARC_32 || r_type == R_SPARC_DISP32)
03503                   && (((input_section->flags & SEC_DEBUGGING) != 0
03504                       && strcmp (bfd_section_name (input_bfd,
03505                                                 input_section),
03506                                 ".stab") == 0)
03507                      || _bfd_elf_section_offset (output_bfd, info,
03508                                               input_section,
03509                                               rel->r_offset)
03510                           == (bfd_vma)-1))
03511                 break;
03512 
03513               if (h != NULL)
03514                 {
03515                   /* Assume this is a call protected by other code that
03516                      detect the symbol is undefined.  If this is the case,
03517                      we can safely ignore the overflow.  If not, the
03518                      program is hosed anyway, and a little warning isn't
03519                      going to help.  */
03520                   if (h->root.type == bfd_link_hash_undefweak
03521                      && howto->pc_relative)
03522                     break;
03523 
03524                    name = NULL;
03525                 }
03526               else
03527                 {
03528                   name = bfd_elf_string_from_elf_section (input_bfd,
03529                                                      symtab_hdr->sh_link,
03530                                                      sym->st_name);
03531                   if (name == NULL)
03532                     return FALSE;
03533                   if (*name == '\0')
03534                     name = bfd_section_name (input_bfd, sec);
03535                 }
03536               if (! ((*info->callbacks->reloc_overflow)
03537                      (info, (h ? &h->root : NULL), name, howto->name,
03538                      (bfd_vma) 0, input_bfd, input_section,
03539                      rel->r_offset)))
03540                 return FALSE;
03541              }
03542              break;
03543            }
03544        }
03545     }
03546 
03547   return TRUE;
03548 }
03549 
03550 /* Build a VxWorks PLT entry.  PLT_INDEX is the index of the PLT entry
03551    and PLT_OFFSET is the byte offset from the start of .plt.  GOT_OFFSET
03552    is the offset of the associated .got.plt entry from
03553    _GLOBAL_OFFSET_TABLE_.  */
03554 
03555 static void
03556 sparc_vxworks_build_plt_entry (bfd *output_bfd, struct bfd_link_info *info,
03557                             bfd_vma plt_offset, bfd_vma plt_index,
03558                             bfd_vma got_offset)
03559 {
03560   bfd_vma got_base;
03561   const bfd_vma *plt_entry;
03562   struct _bfd_sparc_elf_link_hash_table *htab;
03563   bfd_byte *loc;
03564   Elf_Internal_Rela rela;
03565 
03566   htab = _bfd_sparc_elf_hash_table (info);
03567   if (info->shared)
03568     {
03569       plt_entry = sparc_vxworks_shared_plt_entry;
03570       got_base = 0;
03571     }
03572   else
03573     {
03574       plt_entry = sparc_vxworks_exec_plt_entry;
03575       got_base = (htab->elf.hgot->root.u.def.value
03576                 + htab->elf.hgot->root.u.def.section->output_offset
03577                 + htab->elf.hgot->root.u.def.section->output_section->vma);
03578     }
03579 
03580   /* Fill in the entry in the procedure linkage table.  */
03581   bfd_put_32 (output_bfd, plt_entry[0] + ((got_base + got_offset) >> 10),
03582              htab->splt->contents + plt_offset);
03583   bfd_put_32 (output_bfd, plt_entry[1] + ((got_base + got_offset) & 0x3ff),
03584              htab->splt->contents + plt_offset + 4);
03585   bfd_put_32 (output_bfd, plt_entry[2],
03586              htab->splt->contents + plt_offset + 8);
03587   bfd_put_32 (output_bfd, plt_entry[3],
03588              htab->splt->contents + plt_offset + 12);
03589   bfd_put_32 (output_bfd, plt_entry[4],
03590              htab->splt->contents + plt_offset + 16);
03591   bfd_put_32 (output_bfd, plt_entry[5] + (plt_index >> 10),
03592              htab->splt->contents + plt_offset + 20);
03593   /* PC-relative displacement for a branch to the start of
03594      the PLT section.  */
03595   bfd_put_32 (output_bfd, plt_entry[6] + (((-plt_offset - 24) >> 2)
03596                                      & 0x003fffff),
03597              htab->splt->contents + plt_offset + 24);
03598   bfd_put_32 (output_bfd, plt_entry[7] + (plt_index & 0x3ff),
03599              htab->splt->contents + plt_offset + 28);
03600 
03601   /* Fill in the .got.plt entry, pointing initially at the
03602      second half of the PLT entry.  */
03603   BFD_ASSERT (htab->sgotplt != NULL);
03604   bfd_put_32 (output_bfd,
03605              htab->splt->output_section->vma
03606              + htab->splt->output_offset
03607              + plt_offset + 20,
03608              htab->sgotplt->contents + got_offset);
03609 
03610   /* Add relocations to .rela.plt.unloaded.  */
03611   if (!info->shared)
03612     {
03613       loc = (htab->srelplt2->contents
03614             + (2 + 3 * plt_index) * sizeof (Elf32_External_Rela));
03615 
03616       /* Relocate the initial sethi.  */
03617       rela.r_offset = (htab->splt->output_section->vma
03618                      + htab->splt->output_offset
03619                      + plt_offset);
03620       rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
03621       rela.r_addend = got_offset;
03622       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
03623       loc += sizeof (Elf32_External_Rela);
03624 
03625       /* Likewise the following or.  */
03626       rela.r_offset += 4;
03627       rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
03628       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
03629       loc += sizeof (Elf32_External_Rela);
03630 
03631       /* Relocate the .got.plt entry.  */
03632       rela.r_offset = (htab->sgotplt->output_section->vma
03633                      + htab->sgotplt->output_offset
03634                      + got_offset);
03635       rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
03636       rela.r_addend = plt_offset + 20;
03637       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
03638     }
03639 }
03640 
03641 /* Finish up dynamic symbol handling.  We set the contents of various
03642    dynamic sections here.  */
03643 
03644 bfd_boolean
03645 _bfd_sparc_elf_finish_dynamic_symbol (bfd *output_bfd,
03646                                   struct bfd_link_info *info,
03647                                   struct elf_link_hash_entry *h,
03648                                   Elf_Internal_Sym *sym)
03649 {
03650   bfd *dynobj;
03651   struct _bfd_sparc_elf_link_hash_table *htab;
03652   const struct elf_backend_data *bed;
03653 
03654   htab = _bfd_sparc_elf_hash_table (info);
03655   dynobj = htab->elf.dynobj;
03656   bed = get_elf_backend_data (output_bfd);
03657 
03658   if (h->plt.offset != (bfd_vma) -1)
03659     {
03660       asection *splt;
03661       asection *srela;
03662       Elf_Internal_Rela rela;
03663       bfd_byte *loc;
03664       bfd_vma r_offset, got_offset;
03665       int rela_index;
03666 
03667       /* This symbol has an entry in the PLT.  Set it up.  */
03668 
03669       BFD_ASSERT (h->dynindx != -1);
03670 
03671       splt = htab->splt;
03672       srela = htab->srelplt;
03673       BFD_ASSERT (splt != NULL && srela != NULL);
03674 
03675       /* Fill in the entry in the .rela.plt section.  */
03676       if (htab->is_vxworks)
03677        {
03678          /* Work out the index of this PLT entry.  */
03679          rela_index = ((h->plt.offset - htab->plt_header_size)
03680                      / htab->plt_entry_size);
03681 
03682          /* Calculate the offset of the associated .got.plt entry.
03683             The first three entries are reserved.  */
03684          got_offset = (rela_index + 3) * 4;
03685 
03686          sparc_vxworks_build_plt_entry (output_bfd, info, h->plt.offset,
03687                                     rela_index, got_offset);
03688 
03689 
03690          /* On VxWorks, the relocation points to the .got.plt entry,
03691             not the .plt entry.  */
03692          rela.r_offset = (htab->sgotplt->output_section->vma
03693                         + htab->sgotplt->output_offset
03694                         + got_offset);
03695          rela.r_addend = 0;
03696        }
03697       else
03698        {
03699          /* Fill in the entry in the procedure linkage table.  */
03700          rela_index = SPARC_ELF_BUILD_PLT_ENTRY (htab, output_bfd, splt,
03701                                             h->plt.offset, splt->size,
03702                                             &r_offset);
03703 
03704          rela.r_offset = r_offset
03705            + (splt->output_section->vma + splt->output_offset);
03706          if (! ABI_64_P (output_bfd)
03707              || h->plt.offset < (PLT64_LARGE_THRESHOLD * PLT64_ENTRY_SIZE))
03708            {
03709              rela.r_addend = 0;
03710            }
03711          else
03712            {
03713              rela.r_addend = (-(h->plt.offset + 4)
03714                             - splt->output_section->vma
03715                             - splt->output_offset);
03716            }
03717        }
03718       rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_JMP_SLOT);
03719 
03720       /* Adjust for the first 4 reserved elements in the .plt section
03721         when setting the offset in the .rela.plt section.
03722         Sun forgot to read their own ABI and copied elf32-sparc behaviour,
03723         thus .plt[4] has corresponding .rela.plt[0] and so on.  */
03724 
03725       loc = srela->contents;
03726       loc += rela_index * bed->s->sizeof_rela;
03727       bed->s->swap_reloca_out (output_bfd, &rela, loc);
03728 
03729       if (!h->def_regular)
03730        {
03731          /* Mark the symbol as undefined, rather than as defined in
03732             the .plt section.  Leave the value alone.  */
03733          sym->st_shndx = SHN_UNDEF;
03734          /* If the symbol is weak, we do need to clear the value.
03735             Otherwise, the PLT entry would provide a definition for
03736             the symbol even if the symbol wasn't defined anywhere,
03737             and so the symbol would never be NULL.  */
03738          if (!h->ref_regular_nonweak)
03739            sym->st_value = 0;
03740        }
03741     }
03742 
03743   if (h->got.offset != (bfd_vma) -1
03744       && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_GD
03745       && _bfd_sparc_elf_hash_entry(h)->tls_type != GOT_TLS_IE)
03746     {
03747       asection *sgot;
03748       asection *srela;
03749       Elf_Internal_Rela rela;
03750 
03751       /* This symbol has an entry in the GOT.  Set it up.  */
03752 
03753       sgot = htab->sgot;
03754       srela = htab->srelgot;
03755       BFD_ASSERT (sgot != NULL && srela != NULL);
03756 
03757       rela.r_offset = (sgot->output_section->vma
03758                      + sgot->output_offset
03759                      + (h->got.offset &~ (bfd_vma) 1));
03760 
03761       /* If this is a -Bsymbolic link, and the symbol is defined
03762         locally, we just want to emit a RELATIVE reloc.  Likewise if
03763         the symbol was forced to be local because of a version file.
03764         The entry in the global offset table will already have been
03765         initialized in the relocate_section function.  */
03766       if (info->shared
03767          && (info->symbolic || h->dynindx == -1)
03768          && h->def_regular)
03769        {
03770          asection *sec = h->root.u.def.section;
03771          rela.r_info = SPARC_ELF_R_INFO (htab, NULL, 0, R_SPARC_RELATIVE);
03772          rela.r_addend = (h->root.u.def.value
03773                         + sec->output_section->vma
03774                         + sec->output_offset);
03775        }
03776       else
03777        {
03778          rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_GLOB_DAT);
03779          rela.r_addend = 0;
03780        }
03781 
03782       SPARC_ELF_PUT_WORD (htab, output_bfd, 0,
03783                        sgot->contents + (h->got.offset & ~(bfd_vma) 1));
03784       sparc_elf_append_rela (output_bfd, srela, &rela);
03785     }
03786 
03787   if (h->needs_copy)
03788     {
03789       asection *s;
03790       Elf_Internal_Rela rela;
03791 
03792       /* This symbols needs a copy reloc.  Set it up.  */
03793       BFD_ASSERT (h->dynindx != -1);
03794 
03795       s = bfd_get_section_by_name (h->root.u.def.section->owner,
03796                                ".rela.bss");
03797       BFD_ASSERT (s != NULL);
03798 
03799       rela.r_offset = (h->root.u.def.value
03800                      + h->root.u.def.section->output_section->vma
03801                      + h->root.u.def.section->output_offset);
03802       rela.r_info = SPARC_ELF_R_INFO (htab, NULL, h->dynindx, R_SPARC_COPY);
03803       rela.r_addend = 0;
03804       sparc_elf_append_rela (output_bfd, s, &rela);
03805     }
03806 
03807   /* Mark some specially defined symbols as absolute.  On VxWorks,
03808      _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
03809      ".got" section.  Likewise _PROCEDURE_LINKAGE_TABLE_ and ".plt".  */
03810   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
03811       || (!htab->is_vxworks
03812          && (h == htab->elf.hgot || h == htab->elf.hplt)))
03813     sym->st_shndx = SHN_ABS;
03814 
03815   return TRUE;
03816 }
03817 
03818 /* Finish up the dynamic sections.  */
03819 
03820 static bfd_boolean
03821 sparc_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
03822                 bfd *dynobj, asection *sdyn,
03823                 asection *splt ATTRIBUTE_UNUSED)
03824 {
03825   struct _bfd_sparc_elf_link_hash_table *htab;
03826   const struct elf_backend_data *bed;
03827   bfd_byte *dyncon, *dynconend;
03828   size_t dynsize;
03829   int stt_regidx = -1;
03830   bfd_boolean abi_64_p;
03831 
03832   htab = _bfd_sparc_elf_hash_table (info);
03833   bed = get_elf_backend_data (output_bfd);
03834   dynsize = bed->s->sizeof_dyn;
03835   dynconend = sdyn->contents + sdyn->size;
03836   abi_64_p = ABI_64_P (output_bfd);
03837   for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
03838     {
03839       Elf_Internal_Dyn dyn;
03840       const char *name;
03841       bfd_boolean size;
03842 
03843       bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
03844 
03845       if (htab->is_vxworks && dyn.d_tag == DT_RELASZ)
03846        {
03847          /* On VxWorks, DT_RELASZ should not include the relocations
03848             in .rela.plt.  */
03849          if (htab->srelplt)
03850            {
03851              dyn.d_un.d_val -= htab->srelplt->size;
03852              bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
03853            }
03854        }
03855       else if (htab->is_vxworks && dyn.d_tag == DT_PLTGOT)
03856        {
03857          /* On VxWorks, DT_PLTGOT should point to the start of the GOT,
03858             not to the start of the PLT.  */
03859          if (htab->sgotplt)
03860            {
03861              dyn.d_un.d_val = (htab->sgotplt->output_section->vma
03862                             + htab->sgotplt->output_offset);
03863              bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
03864            }
03865        }
03866       else if (abi_64_p && dyn.d_tag == DT_SPARC_REGISTER)
03867        {
03868          if (stt_regidx == -1)
03869            {
03870              stt_regidx =
03871               _bfd_elf_link_lookup_local_dynindx (info, output_bfd, -1);
03872              if (stt_regidx == -1)
03873               return FALSE;
03874            }
03875          dyn.d_un.d_val = stt_regidx++;
03876          bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
03877        }
03878       else
03879        {
03880          switch (dyn.d_tag)
03881            {
03882            case DT_PLTGOT:   name = ".plt"; size = FALSE; break;
03883            case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
03884            case DT_JMPREL:   name = ".rela.plt"; size = FALSE; break;
03885            default:        name = NULL; size = FALSE; break;
03886            }
03887 
03888          if (name != NULL)
03889            {
03890              asection *s;
03891 
03892              s = bfd_get_section_by_name (output_bfd, name);
03893              if (s == NULL)
03894               dyn.d_un.d_val = 0;
03895              else
03896               {
03897                 if (! size)
03898                   dyn.d_un.d_ptr = s->vma;
03899                 else
03900                   dyn.d_un.d_val = s->size;
03901               }
03902              bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
03903            }
03904        }
03905     }
03906   return TRUE;
03907 }
03908 
03909 /* Install the first PLT entry in a VxWorks executable and make sure that
03910    .rela.plt.unloaded relocations have the correct symbol indexes.  */
03911 
03912 static void
03913 sparc_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
03914 {
03915   struct _bfd_sparc_elf_link_hash_table *htab;
03916   Elf_Internal_Rela rela;
03917   bfd_vma got_base;
03918   bfd_byte *loc;
03919 
03920   htab = _bfd_sparc_elf_hash_table (info);
03921 
03922   /* Calculate the absolute value of _GLOBAL_OFFSET_TABLE_.  */
03923   got_base = (htab->elf.hgot->root.u.def.section->output_section->vma
03924              + htab->elf.hgot->root.u.def.section->output_offset
03925              + htab->elf.hgot->root.u.def.value);
03926 
03927   /* Install the initial PLT entry.  */
03928   bfd_put_32 (output_bfd,
03929              sparc_vxworks_exec_plt0_entry[0] + ((got_base + 8) >> 10),
03930              htab->splt->contents);
03931   bfd_put_32 (output_bfd,
03932              sparc_vxworks_exec_plt0_entry[1] + ((got_base + 8) & 0x3ff),
03933              htab->splt->contents + 4);
03934   bfd_put_32 (output_bfd,
03935              sparc_vxworks_exec_plt0_entry[2],
03936              htab->splt->contents + 8);
03937   bfd_put_32 (output_bfd,
03938              sparc_vxworks_exec_plt0_entry[3],
03939              htab->splt->contents + 12);
03940   bfd_put_32 (output_bfd,
03941              sparc_vxworks_exec_plt0_entry[4],
03942              htab->splt->contents + 16);
03943 
03944   loc = htab->srelplt2->contents;
03945 
03946   /* Add an unloaded relocation for the initial entry's "sethi".  */
03947   rela.r_offset = (htab->splt->output_section->vma
03948                  + htab->splt->output_offset);
03949   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
03950   rela.r_addend = 8;
03951   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
03952   loc += sizeof (Elf32_External_Rela);
03953 
03954   /* Likewise the following "or".  */
03955   rela.r_offset += 4;
03956   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
03957   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
03958   loc += sizeof (Elf32_External_Rela);
03959 
03960   /* Fix up the remaining .rela.plt.unloaded relocations.  They may have
03961      the wrong symbol index for _G_O_T_ or _P_L_T_ depending on the order
03962      in which symbols were output.  */
03963   while (loc < htab->srelplt2->contents + htab->srelplt2->size)
03964     {
03965       Elf_Internal_Rela rel;
03966 
03967       /* The entry's initial "sethi" (against _G_O_T_).  */
03968       bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
03969       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_HI22);
03970       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
03971       loc += sizeof (Elf32_External_Rela);
03972 
03973       /* The following "or" (also against _G_O_T_).  */
03974       bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
03975       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_SPARC_LO10);
03976       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
03977       loc += sizeof (Elf32_External_Rela);
03978 
03979       /* The .got.plt entry (against _P_L_T_).  */
03980       bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
03981       rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_SPARC_32);
03982       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
03983       loc += sizeof (Elf32_External_Rela);
03984     }
03985 }
03986 
03987 /* Install the first PLT entry in a VxWorks shared object.  */
03988 
03989 static void
03990 sparc_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
03991 {
03992   struct _bfd_sparc_elf_link_hash_table *htab;
03993   unsigned int i;
03994 
03995   htab = _bfd_sparc_elf_hash_table (info);
03996   for (i = 0; i < ARRAY_SIZE (sparc_vxworks_shared_plt0_entry); i++)
03997     bfd_put_32 (output_bfd, sparc_vxworks_shared_plt0_entry[i],
03998               htab->splt->contents + i * 4);
03999 }
04000 
04001 bfd_boolean
04002 _bfd_sparc_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
04003 {
04004   bfd *dynobj;
04005   asection *sdyn;
04006   struct _bfd_sparc_elf_link_hash_table *htab;
04007 
04008   htab = _bfd_sparc_elf_hash_table (info);
04009   dynobj = htab->elf.dynobj;
04010 
04011   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
04012 
04013   if (elf_hash_table (info)->dynamic_sections_created)
04014     {
04015       asection *splt;
04016 
04017       splt = bfd_get_section_by_name (dynobj, ".plt");
04018       BFD_ASSERT (splt != NULL && sdyn != NULL);
04019 
04020       if (!sparc_finish_dyn (output_bfd, info, dynobj, sdyn, splt))
04021        return FALSE;
04022 
04023       /* Initialize the contents of the .plt section.  */
04024       if (splt->size > 0)
04025        {
04026          if (htab->is_vxworks)
04027            {
04028              if (info->shared)
04029               sparc_vxworks_finish_shared_plt (output_bfd, info);
04030              else
04031               sparc_vxworks_finish_exec_plt (output_bfd, info);
04032            }
04033          else
04034            {
04035              memset (splt->contents, 0, htab->plt_header_size);
04036              if (!ABI_64_P (output_bfd))
04037               bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
04038                          splt->contents + splt->size - 4);
04039            }
04040        }
04041 
04042       elf_section_data (splt->output_section)->this_hdr.sh_entsize
04043        = (htab->is_vxworks || !ABI_64_P (output_bfd))
04044          ? 0 : htab->plt_entry_size;
04045     }
04046 
04047   /* Set the first entry in the global offset table to the address of
04048      the dynamic section.  */
04049   if (htab->sgot && htab->sgot->size > 0)
04050     {
04051       bfd_vma val = (sdyn ?
04052                    sdyn->output_section->vma + sdyn->output_offset :
04053                    0);
04054 
04055       SPARC_ELF_PUT_WORD (htab, output_bfd, val, htab->sgot->contents);
04056     }
04057 
04058   if (htab->sgot)
04059     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize =
04060       SPARC_ELF_WORD_BYTES (htab);
04061 
04062   return TRUE;
04063 }
04064 
04065 
04066 /* Set the right machine number for a SPARC ELF file.  */
04067 
04068 bfd_boolean
04069 _bfd_sparc_elf_object_p (bfd *abfd)
04070 {
04071   if (ABI_64_P (abfd))
04072     {
04073       unsigned long mach = bfd_mach_sparc_v9;
04074 
04075       if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
04076        mach = bfd_mach_sparc_v9b;
04077       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
04078        mach = bfd_mach_sparc_v9a;
04079       return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, mach);
04080     }
04081   else
04082     {
04083       if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
04084        {
04085          if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
04086            return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
04087                                          bfd_mach_sparc_v8plusb);
04088          else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
04089            return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
04090                                          bfd_mach_sparc_v8plusa);
04091          else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
04092            return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
04093                                          bfd_mach_sparc_v8plus);
04094          else
04095            return FALSE;
04096        }
04097       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
04098        return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
04099                                      bfd_mach_sparc_sparclite_le);
04100       else
04101        return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
04102     }
04103 }
04104 
04105 /* Return address for Ith PLT stub in section PLT, for relocation REL
04106    or (bfd_vma) -1 if it should not be included.  */
04107 
04108 bfd_vma
04109 _bfd_sparc_elf_plt_sym_val (bfd_vma i, const asection *plt, const arelent *rel)
04110 {
04111   if (ABI_64_P (plt->owner))
04112     {
04113       bfd_vma j;
04114 
04115       i += PLT64_HEADER_SIZE / PLT64_ENTRY_SIZE;
04116       if (i < PLT64_LARGE_THRESHOLD)
04117        return plt->vma + i * PLT64_ENTRY_SIZE;
04118 
04119       j = (i - PLT64_LARGE_THRESHOLD) % 160;
04120       i -= j;
04121       return plt->vma + i * PLT64_ENTRY_SIZE + j * 4 * 6;
04122     }
04123   else
04124     return rel->address;
04125 }