* elf64-sparc.c (struct sparc64_elf_link_hash_table): New 'sgot' and
[deliverable/binutils-gdb.git] / bfd / elf32-sparc.c
CommitLineData
252b5132 1/* SPARC-specific support for 32-bit ELF
f0abc2a1 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
b2a8e766 3 2003, 2004 Free Software Foundation, Inc.
252b5132 4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ae9a127f
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
252b5132 11
ae9a127f
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
ae9a127f
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132
RH
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/sparc.h"
f7775d95 27#include "opcode/sparc.h"
252b5132
RH
28
29static reloc_howto_type *elf32_sparc_reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31static void elf32_sparc_info_to_howto
32 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
b34976b6 33static bfd_boolean elf32_sparc_check_relocs
252b5132
RH
34 PARAMS ((bfd *, struct bfd_link_info *, asection *,
35 const Elf_Internal_Rela *));
b34976b6 36static bfd_boolean elf32_sparc_adjust_dynamic_symbol
252b5132 37 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
b9734f35
JJ
38static bfd_boolean allocate_dynrelocs
39 PARAMS ((struct elf_link_hash_entry *, PTR));
40static bfd_boolean readonly_dynrelocs
41 PARAMS ((struct elf_link_hash_entry *, PTR));
151e5294
EB
42static bfd_boolean elf32_sparc_omit_section_dynsym
43 PARAMS ((bfd *, struct bfd_link_info *, asection *));
b34976b6 44static bfd_boolean elf32_sparc_size_dynamic_sections
252b5132 45 PARAMS ((bfd *, struct bfd_link_info *));
f0abc2a1
AM
46static bfd_boolean elf32_sparc_new_section_hook
47 PARAMS ((bfd *, asection *));
b34976b6
AM
48static bfd_boolean elf32_sparc_relax_section
49 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
b9734f35
JJ
50static bfd_vma dtpoff_base
51 PARAMS ((struct bfd_link_info *));
52static bfd_vma tpoff
53 PARAMS ((struct bfd_link_info *, bfd_vma));
b34976b6 54static bfd_boolean elf32_sparc_relocate_section
252b5132
RH
55 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
56 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
b34976b6 57static bfd_boolean elf32_sparc_finish_dynamic_symbol
252b5132
RH
58 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
59 Elf_Internal_Sym *));
b34976b6 60static bfd_boolean elf32_sparc_finish_dynamic_sections
252b5132 61 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6
AM
62static bfd_boolean elf32_sparc_merge_private_bfd_data
63 PARAMS ((bfd *, bfd *));
b9734f35
JJ
64static struct bfd_hash_entry *link_hash_newfunc
65 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
66static struct bfd_link_hash_table *elf32_sparc_link_hash_table_create
67 PARAMS ((bfd *));
26e41594
AM
68static bfd_boolean create_got_section
69 PARAMS ((bfd *, struct bfd_link_info *));
b9734f35
JJ
70static bfd_boolean elf32_sparc_create_dynamic_sections
71 PARAMS ((bfd *, struct bfd_link_info *));
72static void elf32_sparc_copy_indirect_symbol
9c5bfbb7 73 PARAMS ((const struct elf_backend_data *, struct elf_link_hash_entry *,
b9734f35
JJ
74 struct elf_link_hash_entry *));
75static int elf32_sparc_tls_transition
76 PARAMS ((struct bfd_link_info *, bfd *, int, int));
77
78static bfd_boolean elf32_sparc_mkobject
79 PARAMS ((bfd *));
b34976b6 80static bfd_boolean elf32_sparc_object_p
252b5132
RH
81 PARAMS ((bfd *));
82static void elf32_sparc_final_write_processing
b34976b6 83 PARAMS ((bfd *, bfd_boolean));
db6751f2 84static enum elf_reloc_type_class elf32_sparc_reloc_type_class
f51e552e 85 PARAMS ((const Elf_Internal_Rela *));
917583ad 86static asection * elf32_sparc_gc_mark_hook
1e2f5b6e 87 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
917583ad 88 struct elf_link_hash_entry *, Elf_Internal_Sym *));
b34976b6 89static bfd_boolean elf32_sparc_gc_sweep_hook
917583ad
NC
90 PARAMS ((bfd *, struct bfd_link_info *, asection *,
91 const Elf_Internal_Rela *));
252b5132
RH
92\f
93/* The relocation "howto" table. */
94
95static bfd_reloc_status_type sparc_elf_notsupported_reloc
96 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
97static bfd_reloc_status_type sparc_elf_wdisp16_reloc
98 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
b9734f35
JJ
99static bfd_reloc_status_type sparc_elf_hix22_reloc
100 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
101static bfd_reloc_status_type sparc_elf_lox10_reloc
102 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
252b5132
RH
103
104reloc_howto_type _bfd_sparc_elf_howto_table[] =
105{
b34976b6
AM
106 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
107 HOWTO(R_SPARC_8, 0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_8", FALSE,0,0x000000ff,TRUE),
108 HOWTO(R_SPARC_16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_16", FALSE,0,0x0000ffff,TRUE),
109 HOWTO(R_SPARC_32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_32", FALSE,0,0xffffffff,TRUE),
110 HOWTO(R_SPARC_DISP8, 0,0, 8,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP8", FALSE,0,0x000000ff,TRUE),
111 HOWTO(R_SPARC_DISP16, 0,1,16,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP16", FALSE,0,0x0000ffff,TRUE),
112 HOWTO(R_SPARC_DISP32, 0,2,32,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_DISP32", FALSE,0,0xffffffff,TRUE),
113 HOWTO(R_SPARC_WDISP30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
114 HOWTO(R_SPARC_WDISP22, 2,2,22,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
115 HOWTO(R_SPARC_HI22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_HI22", FALSE,0,0x003fffff,TRUE),
116 HOWTO(R_SPARC_22, 0,2,22,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_22", FALSE,0,0x003fffff,TRUE),
117 HOWTO(R_SPARC_13, 0,2,13,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_13", FALSE,0,0x00001fff,TRUE),
118 HOWTO(R_SPARC_LO10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_LO10", FALSE,0,0x000003ff,TRUE),
119 HOWTO(R_SPARC_GOT10, 0,2,10,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT10", FALSE,0,0x000003ff,TRUE),
120 HOWTO(R_SPARC_GOT13, 0,2,13,FALSE,0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_GOT13", FALSE,0,0x00001fff,TRUE),
121 HOWTO(R_SPARC_GOT22, 10,2,22,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_GOT22", FALSE,0,0x003fffff,TRUE),
122 HOWTO(R_SPARC_PC10, 0,2,10,TRUE, 0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_PC10", FALSE,0,0x000003ff,TRUE),
123 HOWTO(R_SPARC_PC22, 10,2,22,TRUE, 0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PC22", FALSE,0,0x003fffff,TRUE),
124 HOWTO(R_SPARC_WPLT30, 2,2,30,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WPLT30", FALSE,0,0x3fffffff,TRUE),
125 HOWTO(R_SPARC_COPY, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_COPY", FALSE,0,0x00000000,TRUE),
126 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),
127 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),
128 HOWTO(R_SPARC_RELATIVE, 0,0,00,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
129 HOWTO(R_SPARC_UA32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA32", FALSE,0,0xffffffff,TRUE),
130 HOWTO(R_SPARC_PLT32, 0,0,00,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_PLT32", FALSE,0,0xffffffff,TRUE),
131 HOWTO(R_SPARC_HIPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HIPLT22", FALSE,0,0x00000000,TRUE),
132 HOWTO(R_SPARC_LOPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LOPLT10", FALSE,0,0x00000000,TRUE),
133 HOWTO(R_SPARC_PCPLT32, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT32", FALSE,0,0x00000000,TRUE),
134 HOWTO(R_SPARC_PCPLT22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT22", FALSE,0,0x00000000,TRUE),
135 HOWTO(R_SPARC_PCPLT10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PCPLT10", FALSE,0,0x00000000,TRUE),
136 HOWTO(R_SPARC_10, 0,2,10,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_10", FALSE,0,0x000003ff,TRUE),
137 HOWTO(R_SPARC_11, 0,2,11,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_11", FALSE,0,0x000007ff,TRUE),
252b5132
RH
138 /* These are for sparc64 in a 64 bit environment.
139 Values need to be here because the table is indexed by reloc number. */
b34976b6
AM
140 HOWTO(R_SPARC_64, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_64", FALSE,0,0x00000000,TRUE),
141 HOWTO(R_SPARC_OLO10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_OLO10", FALSE,0,0x00000000,TRUE),
142 HOWTO(R_SPARC_HH22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HH22", FALSE,0,0x00000000,TRUE),
143 HOWTO(R_SPARC_HM10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_HM10", FALSE,0,0x00000000,TRUE),
144 HOWTO(R_SPARC_LM22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_LM22", FALSE,0,0x00000000,TRUE),
145 HOWTO(R_SPARC_PC_HH22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HH22", FALSE,0,0x00000000,TRUE),
146 HOWTO(R_SPARC_PC_HM10, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_HM10", FALSE,0,0x00000000,TRUE),
147 HOWTO(R_SPARC_PC_LM22, 0,0,00,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_PC_LM22", FALSE,0,0x00000000,TRUE),
252b5132
RH
148 /* End sparc64 in 64 bit environment values.
149 The following are for sparc64 in a 32 bit environment. */
b34976b6
AM
150 HOWTO(R_SPARC_WDISP16, 2,2,16,TRUE, 0,complain_overflow_signed, sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", FALSE,0,0x00000000,TRUE),
151 HOWTO(R_SPARC_WDISP19, 2,2,19,TRUE, 0,complain_overflow_signed, bfd_elf_generic_reloc, "R_SPARC_WDISP19", FALSE,0,0x0007ffff,TRUE),
152 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),
153 HOWTO(R_SPARC_7, 0,2, 7,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_7", FALSE,0,0x0000007f,TRUE),
154 HOWTO(R_SPARC_5, 0,2, 5,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_5", FALSE,0,0x0000001f,TRUE),
155 HOWTO(R_SPARC_6, 0,2, 6,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_6", FALSE,0,0x0000003f,TRUE),
156 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
157 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
158 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
159 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
160 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
161 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
162 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
163 HOWTO(R_SPARC_NONE, 0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_NONE", FALSE,0,0x00000000,TRUE),
164 HOWTO(R_SPARC_UA64, 0,0, 0,FALSE,0,complain_overflow_dont, sparc_elf_notsupported_reloc, "R_SPARC_UA64", FALSE,0,0x00000000,TRUE),
165 HOWTO(R_SPARC_UA16, 0,1,16,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_UA16", FALSE,0,0x0000ffff,TRUE),
b9734f35
JJ
166 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),
167 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),
168 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),
169 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),
170 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),
171 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),
172 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),
173 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),
174 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),
175 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),
176 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),
177 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),
178 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),
179 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),
180 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),
181 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),
182 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),
183 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),
184 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),
185 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),
186 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),
187 HOWTO(R_SPARC_TLS_DTPOFF64,0,0, 0,FALSE,0,complain_overflow_dont, bfd_elf_generic_reloc, "R_SPARC_TLS_DTPOFF64",FALSE,0,0x00000000,TRUE),
188 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),
189 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)
252b5132
RH
190};
191static reloc_howto_type elf32_sparc_vtinherit_howto =
b34976b6 192 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
252b5132 193static reloc_howto_type elf32_sparc_vtentry_howto =
b34976b6 194 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);
b9734f35
JJ
195static reloc_howto_type elf32_sparc_rev32_howto =
196 HOWTO(R_SPARC_REV32, 0,2,32,FALSE,0,complain_overflow_bitfield,bfd_elf_generic_reloc, "R_SPARC_REV32", FALSE,0,0xffffffff,TRUE);
252b5132 197
252b5132
RH
198struct elf_reloc_map {
199 bfd_reloc_code_real_type bfd_reloc_val;
200 unsigned char elf_reloc_val;
201};
202
dc810e39 203static const struct elf_reloc_map sparc_reloc_map[] =
252b5132
RH
204{
205 { BFD_RELOC_NONE, R_SPARC_NONE, },
206 { BFD_RELOC_16, R_SPARC_16, },
bd5e6e7e 207 { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
252b5132
RH
208 { BFD_RELOC_8, R_SPARC_8 },
209 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
210 { BFD_RELOC_CTOR, R_SPARC_32 },
211 { BFD_RELOC_32, R_SPARC_32 },
212 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
213 { BFD_RELOC_HI22, R_SPARC_HI22 },
214 { BFD_RELOC_LO10, R_SPARC_LO10, },
215 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
bd5e6e7e 216 { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
252b5132
RH
217 { BFD_RELOC_SPARC22, R_SPARC_22 },
218 { BFD_RELOC_SPARC13, R_SPARC_13 },
219 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
220 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
221 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
222 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
223 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
224 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
225 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
226 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
227 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
228 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
229 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
0f2712ed
NC
230 { BFD_RELOC_SPARC_UA16, R_SPARC_UA16 },
231 { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 },
232 { BFD_RELOC_SPARC_UA64, R_SPARC_UA64 },
233 { BFD_RELOC_SPARC_10, R_SPARC_10 },
234 { BFD_RELOC_SPARC_11, R_SPARC_11 },
235 { BFD_RELOC_SPARC_64, R_SPARC_64 },
236 { BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10 },
237 { BFD_RELOC_SPARC_HH22, R_SPARC_HH22 },
238 { BFD_RELOC_SPARC_HM10, R_SPARC_HM10 },
239 { BFD_RELOC_SPARC_LM22, R_SPARC_LM22 },
240 { BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22 },
241 { BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10 },
242 { BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22 },
243 { BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16 },
244 { BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19 },
245 { BFD_RELOC_SPARC_7, R_SPARC_7 },
246 { BFD_RELOC_SPARC_5, R_SPARC_5 },
247 { BFD_RELOC_SPARC_6, R_SPARC_6 },
b9734f35
JJ
248 { BFD_RELOC_SPARC_TLS_GD_HI22, R_SPARC_TLS_GD_HI22 },
249 { BFD_RELOC_SPARC_TLS_GD_LO10, R_SPARC_TLS_GD_LO10 },
250 { BFD_RELOC_SPARC_TLS_GD_ADD, R_SPARC_TLS_GD_ADD },
251 { BFD_RELOC_SPARC_TLS_GD_CALL, R_SPARC_TLS_GD_CALL },
252 { BFD_RELOC_SPARC_TLS_LDM_HI22, R_SPARC_TLS_LDM_HI22 },
253 { BFD_RELOC_SPARC_TLS_LDM_LO10, R_SPARC_TLS_LDM_LO10 },
254 { BFD_RELOC_SPARC_TLS_LDM_ADD, R_SPARC_TLS_LDM_ADD },
255 { BFD_RELOC_SPARC_TLS_LDM_CALL, R_SPARC_TLS_LDM_CALL },
256 { BFD_RELOC_SPARC_TLS_LDO_HIX22, R_SPARC_TLS_LDO_HIX22 },
257 { BFD_RELOC_SPARC_TLS_LDO_LOX10, R_SPARC_TLS_LDO_LOX10 },
258 { BFD_RELOC_SPARC_TLS_LDO_ADD, R_SPARC_TLS_LDO_ADD },
259 { BFD_RELOC_SPARC_TLS_IE_HI22, R_SPARC_TLS_IE_HI22 },
260 { BFD_RELOC_SPARC_TLS_IE_LO10, R_SPARC_TLS_IE_LO10 },
261 { BFD_RELOC_SPARC_TLS_IE_LD, R_SPARC_TLS_IE_LD },
262 { BFD_RELOC_SPARC_TLS_IE_LDX, R_SPARC_TLS_IE_LDX },
263 { BFD_RELOC_SPARC_TLS_IE_ADD, R_SPARC_TLS_IE_ADD },
264 { BFD_RELOC_SPARC_TLS_LE_HIX22, R_SPARC_TLS_LE_HIX22 },
265 { BFD_RELOC_SPARC_TLS_LE_LOX10, R_SPARC_TLS_LE_LOX10 },
266 { BFD_RELOC_SPARC_TLS_DTPMOD32, R_SPARC_TLS_DTPMOD32 },
267 { BFD_RELOC_SPARC_TLS_DTPMOD64, R_SPARC_TLS_DTPMOD64 },
268 { BFD_RELOC_SPARC_TLS_DTPOFF32, R_SPARC_TLS_DTPOFF32 },
269 { BFD_RELOC_SPARC_TLS_DTPOFF64, R_SPARC_TLS_DTPOFF64 },
270 { BFD_RELOC_SPARC_TLS_TPOFF32, R_SPARC_TLS_TPOFF32 },
271 { BFD_RELOC_SPARC_TLS_TPOFF64, R_SPARC_TLS_TPOFF64 },
0f2712ed
NC
272 { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
273 { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
b9734f35 274 { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 }
252b5132
RH
275};
276
277static reloc_howto_type *
278elf32_sparc_reloc_type_lookup (abfd, code)
5f771d47 279 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
280 bfd_reloc_code_real_type code;
281{
282 unsigned int i;
435b1e90 283
252b5132
RH
284 switch (code)
285 {
286 case BFD_RELOC_VTABLE_INHERIT:
287 return &elf32_sparc_vtinherit_howto;
288
289 case BFD_RELOC_VTABLE_ENTRY:
290 return &elf32_sparc_vtentry_howto;
291
b9734f35
JJ
292 case BFD_RELOC_SPARC_REV32:
293 return &elf32_sparc_rev32_howto;
294
252b5132 295 default:
26e41594
AM
296 for (i = 0;
297 i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map);
298 i++)
299 {
300 if (sparc_reloc_map[i].bfd_reloc_val == code)
301 return (_bfd_sparc_elf_howto_table
302 + (int) sparc_reloc_map[i].elf_reloc_val);
303 }
252b5132
RH
304 }
305 bfd_set_error (bfd_error_bad_value);
306 return NULL;
307}
308
309/* We need to use ELF32_R_TYPE so we have our own copy of this function,
310 and elf64-sparc.c has its own copy. */
311
312static void
313elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
5f771d47 314 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
315 arelent *cache_ptr;
316 Elf_Internal_Rela *dst;
317{
60dac299
RH
318 switch (ELF32_R_TYPE(dst->r_info))
319 {
320 case R_SPARC_GNU_VTINHERIT:
321 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
322 break;
323
324 case R_SPARC_GNU_VTENTRY:
325 cache_ptr->howto = &elf32_sparc_vtentry_howto;
326 break;
327
b9734f35
JJ
328 case R_SPARC_REV32:
329 cache_ptr->howto = &elf32_sparc_rev32_howto;
330 break;
331
60dac299
RH
332 default:
333 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
334 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
335 }
252b5132
RH
336}
337\f
338/* For unsupported relocs. */
339
340static bfd_reloc_status_type
341sparc_elf_notsupported_reloc (abfd,
342 reloc_entry,
343 symbol,
344 data,
345 input_section,
346 output_bfd,
347 error_message)
5f771d47
ILT
348 bfd *abfd ATTRIBUTE_UNUSED;
349 arelent *reloc_entry ATTRIBUTE_UNUSED;
350 asymbol *symbol ATTRIBUTE_UNUSED;
351 PTR data ATTRIBUTE_UNUSED;
352 asection *input_section ATTRIBUTE_UNUSED;
353 bfd *output_bfd ATTRIBUTE_UNUSED;
354 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
355{
356 return bfd_reloc_notsupported;
357}
358
359/* Handle the WDISP16 reloc. */
360
361static bfd_reloc_status_type
362sparc_elf_wdisp16_reloc (abfd,
363 reloc_entry,
364 symbol,
365 data,
366 input_section,
367 output_bfd,
368 error_message)
369 bfd *abfd;
370 arelent *reloc_entry;
371 asymbol *symbol;
372 PTR data;
373 asection *input_section;
374 bfd *output_bfd;
5f771d47 375 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
376{
377 bfd_vma relocation;
378 bfd_vma x;
379
380 if (output_bfd != (bfd *) NULL
381 && (symbol->flags & BSF_SECTION_SYM) == 0
382 && (! reloc_entry->howto->partial_inplace
383 || reloc_entry->addend == 0))
384 {
385 reloc_entry->address += input_section->output_offset;
386 return bfd_reloc_ok;
387 }
388
389 if (output_bfd != NULL)
390 return bfd_reloc_continue;
391
07515404 392 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
252b5132
RH
393 return bfd_reloc_outofrange;
394
395 relocation = (symbol->value
396 + symbol->section->output_section->vma
397 + symbol->section->output_offset);
398 relocation += reloc_entry->addend;
399 relocation -= (input_section->output_section->vma
400 + input_section->output_offset);
401 relocation -= reloc_entry->address;
402
403 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
404 x |= ((((relocation >> 2) & 0xc000) << 6)
405 | ((relocation >> 2) & 0x3fff));
406 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
407
408 if ((bfd_signed_vma) relocation < - 0x40000
409 || (bfd_signed_vma) relocation > 0x3ffff)
410 return bfd_reloc_overflow;
411 else
412 return bfd_reloc_ok;
413}
b9734f35
JJ
414
415/* Handle the HIX22 reloc. */
416
417static bfd_reloc_status_type
418sparc_elf_hix22_reloc (abfd,
419 reloc_entry,
420 symbol,
421 data,
422 input_section,
423 output_bfd,
424 error_message)
425 bfd *abfd;
426 arelent *reloc_entry;
427 asymbol *symbol;
428 PTR data;
429 asection *input_section;
430 bfd *output_bfd;
431 char **error_message ATTRIBUTE_UNUSED;
432{
433 bfd_vma relocation;
434 bfd_vma insn;
435
436 if (output_bfd != (bfd *) NULL
437 && (symbol->flags & BSF_SECTION_SYM) == 0)
438 {
439 reloc_entry->address += input_section->output_offset;
440 return bfd_reloc_ok;
441 }
442
443 if (output_bfd != NULL)
444 return bfd_reloc_continue;
445
07515404 446 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b9734f35
JJ
447 return bfd_reloc_outofrange;
448
449 relocation = (symbol->value
450 + symbol->section->output_section->vma
451 + symbol->section->output_offset);
452 relocation += reloc_entry->addend;
453 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
454
455 insn = (insn &~ (bfd_vma) 0x3fffff) | (((~relocation) >> 10) & 0x3fffff);
456 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
457
458 return bfd_reloc_ok;
459}
460
461/* Handle the LOX10 reloc. */
462
463static bfd_reloc_status_type
464sparc_elf_lox10_reloc (abfd,
465 reloc_entry,
466 symbol,
467 data,
468 input_section,
469 output_bfd,
470 error_message)
471 bfd *abfd;
472 arelent *reloc_entry;
473 asymbol *symbol;
474 PTR data;
475 asection *input_section;
476 bfd *output_bfd;
477 char **error_message ATTRIBUTE_UNUSED;
478{
479 bfd_vma relocation;
480 bfd_vma insn;
481
482 if (output_bfd != (bfd *) NULL
483 && (symbol->flags & BSF_SECTION_SYM) == 0)
484 {
485 reloc_entry->address += input_section->output_offset;
486 return bfd_reloc_ok;
487 }
488
489 if (output_bfd != NULL)
490 return bfd_reloc_continue;
491
07515404 492 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b9734f35
JJ
493 return bfd_reloc_outofrange;
494
495 relocation = (symbol->value
496 + symbol->section->output_section->vma
497 + symbol->section->output_offset);
498 relocation += reloc_entry->addend;
499 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
500
501 insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
502 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
503
504 return bfd_reloc_ok;
505}
0647ed71
MK
506\f
507/* Support for core dump NOTE sections. */
508
509static bfd_boolean
510elf32_sparc_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
511{
512 switch (note->descsz)
513 {
514 default:
515 return FALSE;
516
517 case 260: /* Solaris prpsinfo_t. */
518 elf_tdata (abfd)->core_program
519 = _bfd_elfcore_strndup (abfd, note->descdata + 84, 16);
520 elf_tdata (abfd)->core_command
521 = _bfd_elfcore_strndup (abfd, note->descdata + 100, 80);
522 break;
523
524 case 336: /* Solaris psinfo_t. */
525 elf_tdata (abfd)->core_program
526 = _bfd_elfcore_strndup (abfd, note->descdata + 88, 16);
527 elf_tdata (abfd)->core_command
528 = _bfd_elfcore_strndup (abfd, note->descdata + 104, 80);
529 break;
530 }
531
532 return TRUE;
533}
534
252b5132
RH
535\f
536/* Functions for the SPARC ELF linker. */
537
538/* The name of the dynamic interpreter. This is put in the .interp
539 section. */
540
541#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
542
543/* The nop opcode we use. */
544
545#define SPARC_NOP 0x01000000
546
547/* The size in bytes of an entry in the procedure linkage table. */
548
549#define PLT_ENTRY_SIZE 12
550
551/* The first four entries in a procedure linkage table are reserved,
552 and the initial contents are unimportant (we zero them out).
553 Subsequent entries look like this. See the SVR4 ABI SPARC
554 supplement to see how this works. */
555
556/* sethi %hi(.-.plt0),%g1. We fill in the address later. */
557#define PLT_ENTRY_WORD0 0x03000000
558/* b,a .plt0. We fill in the offset later. */
559#define PLT_ENTRY_WORD1 0x30800000
560/* nop. */
561#define PLT_ENTRY_WORD2 SPARC_NOP
562
b9734f35
JJ
563/* The SPARC linker needs to keep track of the number of relocs that it
564 decides to copy as dynamic relocs in check_relocs for each symbol.
565 This is so that it can later discard them if they are found to be
566 unnecessary. We store the information in a field extending the
567 regular ELF linker hash table. */
568
569struct elf32_sparc_dyn_relocs
570{
571 struct elf32_sparc_dyn_relocs *next;
572
573 /* The input section of the reloc. */
574 asection *sec;
575
576 /* Total number of relocs copied for the input section. */
577 bfd_size_type count;
578
579 /* Number of pc-relative relocs copied for the input section. */
580 bfd_size_type pc_count;
581};
582
583/* SPARC ELF linker hash entry. */
584
585struct elf32_sparc_link_hash_entry
586{
587 struct elf_link_hash_entry elf;
588
589 /* Track dynamic relocs copied for this symbol. */
590 struct elf32_sparc_dyn_relocs *dyn_relocs;
591
592#define GOT_UNKNOWN 0
593#define GOT_NORMAL 1
594#define GOT_TLS_GD 2
595#define GOT_TLS_IE 3
596 unsigned char tls_type;
597};
598
599#define elf32_sparc_hash_entry(ent) ((struct elf32_sparc_link_hash_entry *)(ent))
600
601struct elf32_sparc_obj_tdata
602{
603 struct elf_obj_tdata root;
604
605 /* tls_type for each local got entry. */
606 char *local_got_tls_type;
607
608 /* TRUE if TLS GD relocs has been seen for this object. */
609 bfd_boolean has_tlsgd;
610};
611
612#define elf32_sparc_tdata(abfd) \
613 ((struct elf32_sparc_obj_tdata *) (abfd)->tdata.any)
614
615#define elf32_sparc_local_got_tls_type(abfd) \
616 (elf32_sparc_tdata (abfd)->local_got_tls_type)
617
618static bfd_boolean
619elf32_sparc_mkobject (abfd)
620 bfd *abfd;
621{
622 bfd_size_type amt = sizeof (struct elf32_sparc_obj_tdata);
623 abfd->tdata.any = bfd_zalloc (abfd, amt);
624 if (abfd->tdata.any == NULL)
625 return FALSE;
626 return TRUE;
627}
628
629/* SPARC ELF linker hash table. */
630
631struct elf32_sparc_link_hash_table
632{
633 struct elf_link_hash_table elf;
634
635 /* Short-cuts to get to dynamic linker sections. */
636 asection *sgot;
637 asection *srelgot;
638 asection *splt;
639 asection *srelplt;
640 asection *sdynbss;
641 asection *srelbss;
642
643 union {
644 bfd_signed_vma refcount;
645 bfd_vma offset;
646 } tls_ldm_got;
647
648 /* Small local sym to section mapping cache. */
649 struct sym_sec_cache sym_sec;
650};
651
652/* Get the SPARC ELF linker hash table from a link_info structure. */
653
654#define elf32_sparc_hash_table(p) \
655 ((struct elf32_sparc_link_hash_table *) ((p)->hash))
656
657/* Create an entry in an i386 ELF linker hash table. */
658
659static struct bfd_hash_entry *
660link_hash_newfunc (entry, table, string)
661 struct bfd_hash_entry *entry;
662 struct bfd_hash_table *table;
663 const char *string;
664{
665 /* Allocate the structure if it has not already been allocated by a
666 subclass. */
667 if (entry == NULL)
668 {
669 entry = bfd_hash_allocate (table,
26e41594 670 sizeof (struct elf32_sparc_link_hash_entry));
b9734f35 671 if (entry == NULL)
26e41594 672 return entry;
b9734f35
JJ
673 }
674
675 /* Call the allocation method of the superclass. */
676 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
677 if (entry != NULL)
678 {
679 struct elf32_sparc_link_hash_entry *eh;
680
681 eh = (struct elf32_sparc_link_hash_entry *) entry;
682 eh->dyn_relocs = NULL;
683 eh->tls_type = GOT_UNKNOWN;
684 }
685
686 return entry;
687}
688
689/* Create a SPARC ELF linker hash table. */
690
691static struct bfd_link_hash_table *
692elf32_sparc_link_hash_table_create (abfd)
693 bfd *abfd;
694{
695 struct elf32_sparc_link_hash_table *ret;
696 bfd_size_type amt = sizeof (struct elf32_sparc_link_hash_table);
697
698 ret = (struct elf32_sparc_link_hash_table *) bfd_malloc (amt);
699 if (ret == NULL)
700 return NULL;
701
702 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
703 {
704 free (ret);
705 return NULL;
706 }
707
708 ret->sgot = NULL;
709 ret->srelgot = NULL;
710 ret->splt = NULL;
711 ret->srelplt = NULL;
712 ret->sdynbss = NULL;
713 ret->srelbss = NULL;
714 ret->tls_ldm_got.refcount = 0;
715 ret->sym_sec.abfd = NULL;
716
717 return &ret->elf.root;
718}
719
720/* Create .got and .rela.got sections in DYNOBJ, and set up
721 shortcuts to them in our hash table. */
722
723static bfd_boolean
724create_got_section (dynobj, info)
725 bfd *dynobj;
726 struct bfd_link_info *info;
727{
728 struct elf32_sparc_link_hash_table *htab;
729
730 if (! _bfd_elf_create_got_section (dynobj, info))
731 return FALSE;
732
733 htab = elf32_sparc_hash_table (info);
734 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
735 if (!htab->sgot)
736 abort ();
737
738 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
739 if (htab->srelgot == NULL
740 || ! bfd_set_section_flags (dynobj, htab->srelgot,
741 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
742 | SEC_IN_MEMORY | SEC_LINKER_CREATED
743 | SEC_READONLY))
744 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
745 return FALSE;
746 return TRUE;
747}
748
749/* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
750 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
751 hash table. */
752
753static bfd_boolean
754elf32_sparc_create_dynamic_sections (dynobj, info)
755 bfd *dynobj;
756 struct bfd_link_info *info;
757{
758 struct elf32_sparc_link_hash_table *htab;
759
760 htab = elf32_sparc_hash_table (info);
761 if (!htab->sgot && !create_got_section (dynobj, info))
762 return FALSE;
763
764 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
765 return FALSE;
766
767 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
768 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
769 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
770 if (!info->shared)
771 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
772
773 if (!htab->splt || !htab->srelplt || !htab->sdynbss
774 || (!info->shared && !htab->srelbss))
775 abort ();
776
777 return TRUE;
778}
779
780/* Copy the extra info we tack onto an elf_link_hash_entry. */
781
782static void
783elf32_sparc_copy_indirect_symbol (bed, dir, ind)
9c5bfbb7 784 const struct elf_backend_data *bed;
b9734f35
JJ
785 struct elf_link_hash_entry *dir, *ind;
786{
787 struct elf32_sparc_link_hash_entry *edir, *eind;
788
789 edir = (struct elf32_sparc_link_hash_entry *) dir;
790 eind = (struct elf32_sparc_link_hash_entry *) ind;
791
792 if (eind->dyn_relocs != NULL)
793 {
794 if (edir->dyn_relocs != NULL)
26e41594
AM
795 {
796 struct elf32_sparc_dyn_relocs **pp;
797 struct elf32_sparc_dyn_relocs *p;
798
799 if (ind->root.type == bfd_link_hash_indirect)
800 abort ();
801
802 /* Add reloc counts against the weak sym to the strong sym
803 list. Merge any entries against the same section. */
804 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
805 {
806 struct elf32_sparc_dyn_relocs *q;
807
808 for (q = edir->dyn_relocs; q != NULL; q = q->next)
809 if (q->sec == p->sec)
810 {
811 q->pc_count += p->pc_count;
812 q->count += p->count;
813 *pp = p->next;
814 break;
815 }
816 if (q == NULL)
817 pp = &p->next;
818 }
819 *pp = edir->dyn_relocs;
820 }
b9734f35
JJ
821
822 edir->dyn_relocs = eind->dyn_relocs;
823 eind->dyn_relocs = NULL;
824 }
825
826 if (ind->root.type == bfd_link_hash_indirect
827 && dir->got.refcount <= 0)
828 {
829 edir->tls_type = eind->tls_type;
830 eind->tls_type = GOT_UNKNOWN;
831 }
832 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
833}
834
835static int
836elf32_sparc_tls_transition (info, abfd, r_type, is_local)
837 struct bfd_link_info *info;
838 bfd *abfd;
839 int r_type;
840 int is_local;
841{
842 if (r_type == R_SPARC_TLS_GD_HI22
843 && ! elf32_sparc_tdata (abfd)->has_tlsgd)
844 r_type = R_SPARC_REV32;
845
846 if (info->shared)
847 return r_type;
848
849 switch (r_type)
850 {
851 case R_SPARC_TLS_GD_HI22:
852 if (is_local)
853 return R_SPARC_TLS_LE_HIX22;
854 return R_SPARC_TLS_IE_HI22;
855 case R_SPARC_TLS_GD_LO10:
856 if (is_local)
26e41594 857 return R_SPARC_TLS_LE_LOX10;
b9734f35
JJ
858 return R_SPARC_TLS_IE_LO10;
859 case R_SPARC_TLS_IE_HI22:
860 if (is_local)
26e41594 861 return R_SPARC_TLS_LE_HIX22;
b9734f35
JJ
862 return r_type;
863 case R_SPARC_TLS_IE_LO10:
864 if (is_local)
26e41594 865 return R_SPARC_TLS_LE_LOX10;
b9734f35
JJ
866 return r_type;
867 case R_SPARC_TLS_LDM_HI22:
868 return R_SPARC_TLS_LE_HIX22;
869 case R_SPARC_TLS_LDM_LO10:
870 return R_SPARC_TLS_LE_LOX10;
871 }
872
873 return r_type;
874}
875
252b5132
RH
876/* Look through the relocs for a section during the first phase, and
877 allocate space in the global offset table or procedure linkage
878 table. */
879
b34976b6 880static bfd_boolean
252b5132
RH
881elf32_sparc_check_relocs (abfd, info, sec, relocs)
882 bfd *abfd;
883 struct bfd_link_info *info;
884 asection *sec;
885 const Elf_Internal_Rela *relocs;
886{
b9734f35 887 struct elf32_sparc_link_hash_table *htab;
252b5132
RH
888 Elf_Internal_Shdr *symtab_hdr;
889 struct elf_link_hash_entry **sym_hashes;
890 bfd_vma *local_got_offsets;
891 const Elf_Internal_Rela *rel;
892 const Elf_Internal_Rela *rel_end;
252b5132 893 asection *sreloc;
b9734f35 894 bfd_boolean checked_tlsgd = FALSE;
252b5132 895
1049f94e 896 if (info->relocatable)
b34976b6 897 return TRUE;
252b5132 898
b9734f35 899 htab = elf32_sparc_hash_table (info);
252b5132
RH
900 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
901 sym_hashes = elf_sym_hashes (abfd);
902 local_got_offsets = elf_local_got_offsets (abfd);
903
252b5132
RH
904 sreloc = NULL;
905
906 rel_end = relocs + sec->reloc_count;
907 for (rel = relocs; rel < rel_end; rel++)
908 {
b9734f35 909 unsigned int r_type;
252b5132
RH
910 unsigned long r_symndx;
911 struct elf_link_hash_entry *h;
912
913 r_symndx = ELF32_R_SYM (rel->r_info);
b9734f35
JJ
914 r_type = ELF32_R_TYPE (rel->r_info);
915
916 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
26e41594 917 {
d003868e
AM
918 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
919 abfd, r_symndx);
26e41594
AM
920 return FALSE;
921 }
b9734f35 922
252b5132
RH
923 if (r_symndx < symtab_hdr->sh_info)
924 h = NULL;
925 else
926 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
927
b9734f35
JJ
928 /* Compatibility with old R_SPARC_REV32 reloc conflicting
929 with R_SPARC_TLS_GD_HI22. */
930 if (! checked_tlsgd)
931 switch (r_type)
932 {
933 case R_SPARC_TLS_GD_HI22:
934 {
935 const Elf_Internal_Rela *relt;
936
937 for (relt = rel + 1; relt < rel_end; relt++)
938 if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
939 || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
940 || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
941 break;
942 checked_tlsgd = TRUE;
943 elf32_sparc_tdata (abfd)->has_tlsgd = relt < rel_end;
944 }
945 break;
946 case R_SPARC_TLS_GD_LO10:
947 case R_SPARC_TLS_GD_ADD:
948 case R_SPARC_TLS_GD_CALL:
949 checked_tlsgd = TRUE;
950 elf32_sparc_tdata (abfd)->has_tlsgd = TRUE;
951 break;
952 }
953
954 r_type = elf32_sparc_tls_transition (info, abfd, r_type, h == NULL);
955 switch (r_type)
252b5132 956 {
b9734f35
JJ
957 case R_SPARC_TLS_LDM_HI22:
958 case R_SPARC_TLS_LDM_LO10:
959 htab->tls_ldm_got.refcount += 1;
960 break;
961
962 case R_SPARC_TLS_LE_HIX22:
963 case R_SPARC_TLS_LE_LOX10:
964 if (info->shared)
965 goto r_sparc_plt32;
966 break;
967
968 case R_SPARC_TLS_IE_HI22:
969 case R_SPARC_TLS_IE_LO10:
970 if (info->shared)
971 info->flags |= DF_STATIC_TLS;
972 /* Fall through */
973
252b5132
RH
974 case R_SPARC_GOT10:
975 case R_SPARC_GOT13:
976 case R_SPARC_GOT22:
b9734f35
JJ
977 case R_SPARC_TLS_GD_HI22:
978 case R_SPARC_TLS_GD_LO10:
252b5132 979 /* This symbol requires a global offset table entry. */
b9734f35
JJ
980 {
981 int tls_type, old_tls_type;
252b5132 982
b9734f35
JJ
983 switch (r_type)
984 {
985 default:
986 case R_SPARC_GOT10:
987 case R_SPARC_GOT13:
988 case R_SPARC_GOT22:
989 tls_type = GOT_NORMAL;
990 break;
991 case R_SPARC_TLS_GD_HI22:
992 case R_SPARC_TLS_GD_LO10:
993 tls_type = GOT_TLS_GD;
994 break;
995 case R_SPARC_TLS_IE_HI22:
996 case R_SPARC_TLS_IE_LO10:
997 tls_type = GOT_TLS_IE;
998 break;
999 }
252b5132 1000
b9734f35
JJ
1001 if (h != NULL)
1002 {
1003 h->got.refcount += 1;
1004 old_tls_type = elf32_sparc_hash_entry(h)->tls_type;
1005 }
1006 else
1007 {
1008 bfd_signed_vma *local_got_refcounts;
252b5132 1009
b9734f35
JJ
1010 /* This is a global offset table entry for a local symbol. */
1011 local_got_refcounts = elf_local_got_refcounts (abfd);
1012 if (local_got_refcounts == NULL)
1013 {
1014 bfd_size_type size;
252b5132 1015
b9734f35
JJ
1016 size = symtab_hdr->sh_info;
1017 size *= (sizeof (bfd_signed_vma) + sizeof(char));
1018 local_got_refcounts = ((bfd_signed_vma *)
1019 bfd_zalloc (abfd, size));
1020 if (local_got_refcounts == NULL)
1021 return FALSE;
1022 elf_local_got_refcounts (abfd) = local_got_refcounts;
1023 elf32_sparc_local_got_tls_type (abfd)
1024 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1025 }
1026 local_got_refcounts[r_symndx] += 1;
1027 old_tls_type = elf32_sparc_local_got_tls_type (abfd) [r_symndx];
1028 }
252b5132 1029
b9734f35
JJ
1030 /* If a TLS symbol is accessed using IE at least once,
1031 there is no point to use dynamic model for it. */
1032 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1033 && (old_tls_type != GOT_TLS_GD
1034 || tls_type != GOT_TLS_IE))
1035 {
1036 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
1037 tls_type = old_tls_type;
1038 else
1039 {
1040 (*_bfd_error_handler)
d003868e
AM
1041 (_("%B: `%s' accessed both as normal and thread local symbol"),
1042 abfd, h ? h->root.root.string : "<local>");
b34976b6 1043 return FALSE;
b9734f35
JJ
1044 }
1045 }
252b5132 1046
b9734f35
JJ
1047 if (old_tls_type != tls_type)
1048 {
1049 if (h != NULL)
1050 elf32_sparc_hash_entry (h)->tls_type = tls_type;
1051 else
1052 elf32_sparc_local_got_tls_type (abfd) [r_symndx] = tls_type;
1053 }
1054 }
252b5132 1055
2293c499
AM
1056 if (htab->sgot == NULL)
1057 {
1058 if (htab->elf.dynobj == NULL)
1059 htab->elf.dynobj = abfd;
1060 if (!create_got_section (htab->elf.dynobj, info))
1061 return FALSE;
1062 }
b9734f35 1063 break;
252b5132 1064
b9734f35
JJ
1065 case R_SPARC_TLS_GD_CALL:
1066 case R_SPARC_TLS_LDM_CALL:
1067 if (info->shared)
1068 {
1069 /* These are basically R_SPARC_TLS_WPLT30 relocs against
1070 __tls_get_addr. */
1071 struct bfd_link_hash_entry *bh = NULL;
1072 if (! _bfd_generic_link_add_one_symbol (info, abfd,
1073 "__tls_get_addr", 0,
1074 bfd_und_section_ptr, 0,
1075 NULL, FALSE, FALSE,
1076 &bh))
1077 return FALSE;
1078 h = (struct elf_link_hash_entry *) bh;
252b5132 1079 }
b9734f35
JJ
1080 else
1081 break;
1082 /* Fall through */
252b5132 1083
bd5e6e7e 1084 case R_SPARC_PLT32:
252b5132
RH
1085 case R_SPARC_WPLT30:
1086 /* This symbol requires a procedure linkage table entry. We
26e41594
AM
1087 actually build the entry in adjust_dynamic_symbol,
1088 because this might be a case of linking PIC code without
1089 linking in any dynamic objects, in which case we don't
1090 need to generate a procedure linkage table after all. */
252b5132
RH
1091
1092 if (h == NULL)
1093 {
1094 /* The Solaris native assembler will generate a WPLT30
26e41594
AM
1095 reloc for a local symbol if you assemble a call from
1096 one section to another when using -K pic. We treat
1097 it as WDISP30. */
b9734f35 1098 if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
bd5e6e7e 1099 goto r_sparc_plt32;
252b5132
RH
1100 break;
1101 }
1102
f5385ebf 1103 h->needs_plt = 1;
252b5132 1104
b9734f35 1105 if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
bd5e6e7e 1106 goto r_sparc_plt32;
b9734f35 1107 h->plt.refcount += 1;
252b5132
RH
1108 break;
1109
1110 case R_SPARC_PC10:
1111 case R_SPARC_PC22:
7843f00e 1112 if (h != NULL)
f5385ebf 1113 h->non_got_ref = 1;
7843f00e 1114
252b5132
RH
1115 if (h != NULL
1116 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1117 break;
1118 /* Fall through. */
b9734f35 1119
252b5132
RH
1120 case R_SPARC_DISP8:
1121 case R_SPARC_DISP16:
1122 case R_SPARC_DISP32:
1123 case R_SPARC_WDISP30:
1124 case R_SPARC_WDISP22:
1125 case R_SPARC_WDISP19:
1126 case R_SPARC_WDISP16:
252b5132
RH
1127 case R_SPARC_8:
1128 case R_SPARC_16:
1129 case R_SPARC_32:
1130 case R_SPARC_HI22:
1131 case R_SPARC_22:
1132 case R_SPARC_13:
1133 case R_SPARC_LO10:
0f2712ed 1134 case R_SPARC_UA16:
252b5132 1135 case R_SPARC_UA32:
7843f00e 1136 if (h != NULL)
f5385ebf 1137 h->non_got_ref = 1;
7843f00e 1138
bd5e6e7e 1139 r_sparc_plt32:
b9734f35
JJ
1140 if (h != NULL && !info->shared)
1141 {
1142 /* We may need a .plt entry if the function this reloc
1143 refers to is in a shared lib. */
1144 h->plt.refcount += 1;
1145 }
1146
1147 /* If we are creating a shared library, and this is a reloc
1148 against a global symbol, or a non PC relative reloc
1149 against a local symbol, then we need to copy the reloc
1150 into the shared library. However, if we are linking with
1151 -Bsymbolic, we do not need to copy a reloc against a
1152 global symbol which is defined in an object we are
1153 including in the link (i.e., DEF_REGULAR is set). At
1154 this point we have not seen all the input files, so it is
1155 possible that DEF_REGULAR is not set now but will be set
1156 later (it is never cleared). In case of a weak definition,
1157 DEF_REGULAR may be cleared later by a strong definition in
1158 a shared library. We account for that possibility below by
1159 storing information in the relocs_copied field of the hash
1160 table entry. A similar situation occurs when creating
1161 shared libraries and symbol visibility changes render the
1162 symbol local.
1163
1164 If on the other hand, we are creating an executable, we
1165 may need to keep relocations for symbols satisfied by a
1166 dynamic library if we manage to avoid copy relocs for the
1167 symbol. */
1168 if ((info->shared
1169 && (sec->flags & SEC_ALLOC) != 0
1170 && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
1171 || (h != NULL
1172 && (! info->symbolic
1173 || h->root.type == bfd_link_hash_defweak
f5385ebf 1174 || !h->def_regular))))
b9734f35
JJ
1175 || (!info->shared
1176 && (sec->flags & SEC_ALLOC) != 0
1177 && h != NULL
1178 && (h->root.type == bfd_link_hash_defweak
f5385ebf 1179 || !h->def_regular)))
252b5132 1180 {
b9734f35
JJ
1181 struct elf32_sparc_dyn_relocs *p;
1182 struct elf32_sparc_dyn_relocs **head;
1183
252b5132 1184 /* When creating a shared object, we must copy these
26e41594
AM
1185 relocs into the output file. We create a reloc
1186 section in dynobj and make room for the reloc. */
252b5132
RH
1187 if (sreloc == NULL)
1188 {
1189 const char *name;
b9734f35 1190 bfd *dynobj;
252b5132
RH
1191
1192 name = (bfd_elf_string_from_elf_section
1193 (abfd,
1194 elf_elfheader (abfd)->e_shstrndx,
1195 elf_section_data (sec)->rel_hdr.sh_name));
1196 if (name == NULL)
b34976b6 1197 return FALSE;
252b5132
RH
1198
1199 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1200 && strcmp (bfd_get_section_name (abfd, sec),
1201 name + 5) == 0);
1202
b9734f35
JJ
1203 if (htab->elf.dynobj == NULL)
1204 htab->elf.dynobj = abfd;
1205 dynobj = htab->elf.dynobj;
1206
252b5132
RH
1207 sreloc = bfd_get_section_by_name (dynobj, name);
1208 if (sreloc == NULL)
1209 {
1210 flagword flags;
1211
1212 sreloc = bfd_make_section (dynobj, name);
1213 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1214 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1215 if ((sec->flags & SEC_ALLOC) != 0)
1216 flags |= SEC_ALLOC | SEC_LOAD;
1217 if (sreloc == NULL
1218 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1219 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
b34976b6 1220 return FALSE;
252b5132 1221 }
b9734f35 1222 elf_section_data (sec)->sreloc = sreloc;
252b5132
RH
1223 }
1224
b9734f35
JJ
1225 /* If this is a global symbol, we count the number of
1226 relocations we need for this symbol. */
1227 if (h != NULL)
1228 head = &((struct elf32_sparc_link_hash_entry *) h)->dyn_relocs;
1229 else
1230 {
1231 /* Track dynamic relocs needed for local syms too.
1232 We really need local syms available to do this
1233 easily. Oh well. */
1234
1235 asection *s;
1236 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1237 sec, r_symndx);
1238 if (s == NULL)
1239 return FALSE;
1240
1241 head = ((struct elf32_sparc_dyn_relocs **)
1242 &elf_section_data (s)->local_dynrel);
1243 }
1244
1245 p = *head;
1246 if (p == NULL || p->sec != sec)
1247 {
1248 bfd_size_type amt = sizeof *p;
1249 p = ((struct elf32_sparc_dyn_relocs *)
1250 bfd_alloc (htab->elf.dynobj, amt));
1251 if (p == NULL)
1252 return FALSE;
1253 p->next = *head;
1254 *head = p;
1255 p->sec = sec;
1256 p->count = 0;
1257 p->pc_count = 0;
1258 }
1259
1260 p->count += 1;
1261 if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
1262 p->pc_count += 1;
252b5132
RH
1263 }
1264
1265 break;
1266
26e41594 1267 case R_SPARC_GNU_VTINHERIT:
c152c796 1268 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
26e41594
AM
1269 return FALSE;
1270 break;
252b5132 1271
26e41594 1272 case R_SPARC_GNU_VTENTRY:
c152c796 1273 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
26e41594
AM
1274 return FALSE;
1275 break;
252b5132
RH
1276
1277 default:
1278 break;
1279 }
1280 }
1281
b34976b6 1282 return TRUE;
252b5132
RH
1283}
1284
1285static asection *
1e2f5b6e
AM
1286elf32_sparc_gc_mark_hook (sec, info, rel, h, sym)
1287 asection *sec;
5f771d47 1288 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1289 Elf_Internal_Rela *rel;
1290 struct elf_link_hash_entry *h;
1291 Elf_Internal_Sym *sym;
1292{
252b5132
RH
1293 if (h != NULL)
1294 {
1295 switch (ELF32_R_TYPE (rel->r_info))
1296 {
1297 case R_SPARC_GNU_VTINHERIT:
1298 case R_SPARC_GNU_VTENTRY:
26e41594 1299 break;
252b5132
RH
1300
1301 default:
26e41594
AM
1302 switch (h->root.type)
1303 {
1304 case bfd_link_hash_defined:
1305 case bfd_link_hash_defweak:
1306 return h->root.u.def.section;
252b5132 1307
26e41594
AM
1308 case bfd_link_hash_common:
1309 return h->root.u.c.p->section;
e049a0de
ILT
1310
1311 default:
1312 break;
26e41594 1313 }
252b5132
RH
1314 }
1315 }
1316 else
1e2f5b6e 1317 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
252b5132
RH
1318
1319 return NULL;
1320}
1321
1322/* Update the got entry reference counts for the section being removed. */
b34976b6 1323static bfd_boolean
252b5132
RH
1324elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
1325 bfd *abfd;
5f771d47 1326 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1327 asection *sec;
1328 const Elf_Internal_Rela *relocs;
1329{
252b5132
RH
1330 Elf_Internal_Shdr *symtab_hdr;
1331 struct elf_link_hash_entry **sym_hashes;
1332 bfd_signed_vma *local_got_refcounts;
1333 const Elf_Internal_Rela *rel, *relend;
26e41594
AM
1334
1335 elf_section_data (sec)->local_dynrel = NULL;
252b5132
RH
1336
1337 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1338 sym_hashes = elf_sym_hashes (abfd);
1339 local_got_refcounts = elf_local_got_refcounts (abfd);
1340
1341 relend = relocs + sec->reloc_count;
1342 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1343 {
1344 unsigned long r_symndx;
1345 unsigned int r_type;
1346 struct elf_link_hash_entry *h = NULL;
b9734f35 1347
26e41594
AM
1348 r_symndx = ELF32_R_SYM (rel->r_info);
1349 if (r_symndx >= symtab_hdr->sh_info)
1350 {
1351 struct elf32_sparc_link_hash_entry *eh;
1352 struct elf32_sparc_dyn_relocs **pp;
1353 struct elf32_sparc_dyn_relocs *p;
b9734f35 1354
26e41594
AM
1355 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1356 eh = (struct elf32_sparc_link_hash_entry *) h;
1357 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1358 if (p->sec == sec)
1359 {
1360 /* Everything must go for SEC. */
1361 *pp = p->next;
1362 break;
1363 }
1364 }
b9734f35 1365
26e41594
AM
1366 r_type = ELF32_R_TYPE (rel->r_info);
1367 r_type = elf32_sparc_tls_transition (info, abfd, r_type, h != NULL);
1368 switch (r_type)
1369 {
1370 case R_SPARC_TLS_LDM_HI22:
1371 case R_SPARC_TLS_LDM_LO10:
1372 if (elf32_sparc_hash_table (info)->tls_ldm_got.refcount > 0)
1373 elf32_sparc_hash_table (info)->tls_ldm_got.refcount -= 1;
1374 break;
b9734f35 1375
26e41594
AM
1376 case R_SPARC_TLS_GD_HI22:
1377 case R_SPARC_TLS_GD_LO10:
1378 case R_SPARC_TLS_IE_HI22:
1379 case R_SPARC_TLS_IE_LO10:
1380 case R_SPARC_GOT10:
1381 case R_SPARC_GOT13:
1382 case R_SPARC_GOT22:
1383 if (h != NULL)
1384 {
1385 if (h->got.refcount > 0)
1386 h->got.refcount--;
1387 }
1388 else
1389 {
1390 if (local_got_refcounts[r_symndx] > 0)
1391 local_got_refcounts[r_symndx]--;
1392 }
1393 break;
b9734f35 1394
26e41594
AM
1395 case R_SPARC_PC10:
1396 case R_SPARC_PC22:
1397 if (h != NULL
1398 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1399 break;
1400 /* Fall through. */
b9734f35 1401
26e41594
AM
1402 case R_SPARC_DISP8:
1403 case R_SPARC_DISP16:
1404 case R_SPARC_DISP32:
1405 case R_SPARC_WDISP30:
1406 case R_SPARC_WDISP22:
1407 case R_SPARC_WDISP19:
1408 case R_SPARC_WDISP16:
1409 case R_SPARC_8:
1410 case R_SPARC_16:
1411 case R_SPARC_32:
1412 case R_SPARC_HI22:
1413 case R_SPARC_22:
1414 case R_SPARC_13:
1415 case R_SPARC_LO10:
1416 case R_SPARC_UA16:
1417 case R_SPARC_UA32:
1418 case R_SPARC_PLT32:
1419 if (info->shared)
1420 break;
1421 /* Fall through. */
b9734f35 1422
26e41594
AM
1423 case R_SPARC_WPLT30:
1424 if (h != NULL)
1425 {
1426 if (h->plt.refcount > 0)
1427 h->plt.refcount--;
1428 }
1429 break;
252b5132 1430
26e41594
AM
1431 default:
1432 break;
1433 }
1434 }
252b5132 1435
b34976b6 1436 return TRUE;
252b5132
RH
1437}
1438
1439/* Adjust a symbol defined by a dynamic object and referenced by a
1440 regular object. The current definition is in some section of the
1441 dynamic object, but we're not including those sections. We have to
1442 change the definition to something the rest of the link can
1443 understand. */
1444
b34976b6 1445static bfd_boolean
252b5132
RH
1446elf32_sparc_adjust_dynamic_symbol (info, h)
1447 struct bfd_link_info *info;
1448 struct elf_link_hash_entry *h;
1449{
b9734f35
JJ
1450 struct elf32_sparc_link_hash_table *htab;
1451 struct elf32_sparc_link_hash_entry * eh;
1452 struct elf32_sparc_dyn_relocs *p;
252b5132
RH
1453 asection *s;
1454 unsigned int power_of_two;
1455
b9734f35 1456 htab = elf32_sparc_hash_table (info);
252b5132
RH
1457
1458 /* Make sure we know what is going on here. */
b9734f35 1459 BFD_ASSERT (htab->elf.dynobj != NULL
f5385ebf 1460 && (h->needs_plt
f6e332e6 1461 || h->u.weakdef != NULL
f5385ebf
AM
1462 || (h->def_dynamic
1463 && h->ref_regular
1464 && !h->def_regular)));
252b5132
RH
1465
1466 /* If this is a function, put it in the procedure linkage table. We
1467 will fill in the contents of the procedure linkage table later
1468 (although we could actually do it here). The STT_NOTYPE
1469 condition is a hack specifically for the Oracle libraries
1470 delivered for Solaris; for some inexplicable reason, they define
1471 some of their functions as STT_NOTYPE when they really should be
1472 STT_FUNC. */
1473 if (h->type == STT_FUNC
f5385ebf 1474 || h->needs_plt
252b5132
RH
1475 || (h->type == STT_NOTYPE
1476 && (h->root.type == bfd_link_hash_defined
1477 || h->root.type == bfd_link_hash_defweak)
1478 && (h->root.u.def.section->flags & SEC_CODE) != 0))
1479 {
b9734f35
JJ
1480 if (h->plt.refcount <= 0
1481 || (! info->shared
f5385ebf
AM
1482 && !h->def_dynamic
1483 && !h->ref_dynamic
b9734f35
JJ
1484 && h->root.type != bfd_link_hash_undefweak
1485 && h->root.type != bfd_link_hash_undefined))
252b5132
RH
1486 {
1487 /* This case can occur if we saw a WPLT30 reloc in an input
b9734f35
JJ
1488 file, but the symbol was never referred to by a dynamic
1489 object, or if all references were garbage collected. In
1490 such a case, we don't actually need to build a procedure
1491 linkage table, and we can just do a WDISP30 reloc instead. */
1492 h->plt.offset = (bfd_vma) -1;
f5385ebf 1493 h->needs_plt = 0;
252b5132
RH
1494 }
1495
b34976b6 1496 return TRUE;
252b5132 1497 }
b9734f35
JJ
1498 else
1499 h->plt.offset = (bfd_vma) -1;
252b5132
RH
1500
1501 /* If this is a weak symbol, and there is a real definition, the
1502 processor independent code will have arranged for us to see the
1503 real definition first, and we can just use the same value. */
f6e332e6 1504 if (h->u.weakdef != NULL)
252b5132 1505 {
f6e332e6
AM
1506 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1507 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1508 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1509 h->root.u.def.value = h->u.weakdef->root.u.def.value;
b34976b6 1510 return TRUE;
252b5132
RH
1511 }
1512
1513 /* This is a reference to a symbol defined by a dynamic object which
1514 is not a function. */
1515
1516 /* If we are creating a shared library, we must presume that the
1517 only references to the symbol are via the global offset table.
1518 For such cases we need not do anything here; the relocations will
1519 be handled correctly by relocate_section. */
1520 if (info->shared)
b34976b6 1521 return TRUE;
252b5132 1522
7843f00e
ILT
1523 /* If there are no references to this symbol that do not use the
1524 GOT, we don't need to generate a copy reloc. */
f5385ebf 1525 if (!h->non_got_ref)
b34976b6 1526 return TRUE;
7843f00e 1527
b9734f35
JJ
1528 eh = (struct elf32_sparc_link_hash_entry *) h;
1529 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1530 {
1531 s = p->sec->output_section;
1532 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1533 break;
1534 }
1535
1536 /* If we didn't find any dynamic relocs in read-only sections, then
1537 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1538 if (p == NULL)
1539 {
f5385ebf 1540 h->non_got_ref = 0;
b9734f35
JJ
1541 return TRUE;
1542 }
1543
252b5132
RH
1544 /* We must allocate the symbol in our .dynbss section, which will
1545 become part of the .bss section of the executable. There will be
1546 an entry for this symbol in the .dynsym section. The dynamic
1547 object will contain position independent code, so all references
1548 from the dynamic object to this symbol will go through the global
1549 offset table. The dynamic linker will use the .dynsym entry to
1550 determine the address it must put in the global offset table, so
1551 both the dynamic object and the regular object will refer to the
1552 same memory location for the variable. */
1553
b9734f35
JJ
1554 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
1555 to copy the initial value out of the dynamic object and into the
1556 runtime process image. We need to remember the offset into the
1557 .rel.bss section we are going to use. */
1558 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1559 {
eea6121a 1560 htab->srelbss->size += sizeof (Elf32_External_Rela);
f5385ebf 1561 h->needs_copy = 1;
b9734f35
JJ
1562 }
1563
1564 /* We need to figure out the alignment required for this symbol. I
1565 have no idea how ELF linkers handle this. */
1566 power_of_two = bfd_log2 (h->size);
1567 if (power_of_two > 3)
1568 power_of_two = 3;
1569
1570 /* Apply the required alignment. */
1571 s = htab->sdynbss;
eea6121a 1572 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
b9734f35
JJ
1573 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1574 {
1575 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1576 return FALSE;
1577 }
1578
1579 /* Define the symbol as being at this point in the section. */
1580 h->root.u.def.section = s;
eea6121a 1581 h->root.u.def.value = s->size;
b9734f35
JJ
1582
1583 /* Increment the section size to make room for the symbol. */
eea6121a 1584 s->size += h->size;
b9734f35
JJ
1585
1586 return TRUE;
1587}
1588
b9734f35
JJ
1589/* Allocate space in .plt, .got and associated reloc sections for
1590 dynamic relocs. */
1591
1592static bfd_boolean
1593allocate_dynrelocs (h, inf)
1594 struct elf_link_hash_entry *h;
1595 PTR inf;
1596{
1597 struct bfd_link_info *info;
1598 struct elf32_sparc_link_hash_table *htab;
1599 struct elf32_sparc_link_hash_entry *eh;
1600 struct elf32_sparc_dyn_relocs *p;
1601
1602 if (h->root.type == bfd_link_hash_indirect)
1603 return TRUE;
1604
1605 if (h->root.type == bfd_link_hash_warning)
1606 /* When warning symbols are created, they **replace** the "real"
1607 entry in the hash table, thus we never get to see the real
1608 symbol in a hash traversal. So look at it now. */
1609 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1610
1611 info = (struct bfd_link_info *) inf;
1612 htab = elf32_sparc_hash_table (info);
1613
1614 if (htab->elf.dynamic_sections_created
1615 && h->plt.refcount > 0)
1616 {
1617 /* Make sure this symbol is output as a dynamic symbol.
1618 Undefined weak syms won't yet be marked as dynamic. */
1619 if (h->dynindx == -1
f5385ebf 1620 && !h->forced_local)
b9734f35 1621 {
c152c796 1622 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b9734f35
JJ
1623 return FALSE;
1624 }
1625
c152c796 1626 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
b9734f35
JJ
1627 {
1628 asection *s = htab->splt;
1629
1630 /* The first four entries in .plt are reserved. */
eea6121a
AM
1631 if (s->size == 0)
1632 s->size = 4 * PLT_ENTRY_SIZE;
26e41594 1633
b9734f35 1634 /* The procedure linkage table has a maximum size. */
eea6121a 1635 if (s->size >= 0x400000)
b9734f35
JJ
1636 {
1637 bfd_set_error (bfd_error_bad_value);
1638 return FALSE;
1639 }
1640
eea6121a 1641 h->plt.offset = s->size;
b9734f35
JJ
1642
1643 /* If this symbol is not defined in a regular file, and we are
1644 not generating a shared library, then set the symbol to this
1645 location in the .plt. This is required to make function
1646 pointers compare as equal between the normal executable and
1647 the shared library. */
1648 if (! info->shared
f5385ebf 1649 && !h->def_regular)
b9734f35
JJ
1650 {
1651 h->root.u.def.section = s;
1652 h->root.u.def.value = h->plt.offset;
1653 }
1654
1655 /* Make room for this entry. */
eea6121a 1656 s->size += PLT_ENTRY_SIZE;
b9734f35
JJ
1657
1658 /* We also need to make an entry in the .rela.plt section. */
eea6121a 1659 htab->srelplt->size += sizeof (Elf32_External_Rela);
b9734f35
JJ
1660 }
1661 else
1662 {
1663 h->plt.offset = (bfd_vma) -1;
f5385ebf 1664 h->needs_plt = 0;
b9734f35
JJ
1665 }
1666 }
1667 else
1668 {
1669 h->plt.offset = (bfd_vma) -1;
f5385ebf 1670 h->needs_plt = 0;
b9734f35
JJ
1671 }
1672
1673 /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
1674 make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry. */
1675 if (h->got.refcount > 0
1676 && !info->shared
1677 && h->dynindx == -1
1678 && elf32_sparc_hash_entry(h)->tls_type == GOT_TLS_IE)
1679 h->got.offset = (bfd_vma) -1;
1680 else if (h->got.refcount > 0)
1681 {
1682 asection *s;
1683 bfd_boolean dyn;
1684 int tls_type = elf32_sparc_hash_entry(h)->tls_type;
1685
1686 /* Make sure this symbol is output as a dynamic symbol.
1687 Undefined weak syms won't yet be marked as dynamic. */
1688 if (h->dynindx == -1
f5385ebf 1689 && !h->forced_local)
b9734f35 1690 {
c152c796 1691 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b9734f35
JJ
1692 return FALSE;
1693 }
1694
1695 s = htab->sgot;
eea6121a
AM
1696 h->got.offset = s->size;
1697 s->size += 4;
b9734f35
JJ
1698 /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots. */
1699 if (tls_type == GOT_TLS_GD)
eea6121a 1700 s->size += 4;
b9734f35
JJ
1701 dyn = htab->elf.dynamic_sections_created;
1702 /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
1703 R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if
1704 global. */
1705 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1706 || tls_type == GOT_TLS_IE)
eea6121a 1707 htab->srelgot->size += sizeof (Elf32_External_Rela);
b9734f35 1708 else if (tls_type == GOT_TLS_GD)
eea6121a 1709 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
c152c796 1710 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
eea6121a 1711 htab->srelgot->size += sizeof (Elf32_External_Rela);
b9734f35
JJ
1712 }
1713 else
1714 h->got.offset = (bfd_vma) -1;
1715
1716 eh = (struct elf32_sparc_link_hash_entry *) h;
1717 if (eh->dyn_relocs == NULL)
1718 return TRUE;
1719
1720 /* In the shared -Bsymbolic case, discard space allocated for
1721 dynamic pc-relative relocs against symbols which turn out to be
1722 defined in regular objects. For the normal shared case, discard
1723 space for pc-relative relocs that have become local due to symbol
1724 visibility changes. */
1725
1726 if (info->shared)
1727 {
f5385ebf
AM
1728 if (h->def_regular
1729 && (h->forced_local
b9734f35
JJ
1730 || info->symbolic))
1731 {
1732 struct elf32_sparc_dyn_relocs **pp;
1733
1734 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1735 {
1736 p->count -= p->pc_count;
1737 p->pc_count = 0;
1738 if (p->count == 0)
1739 *pp = p->next;
1740 else
1741 pp = &p->next;
1742 }
1743 }
1744 }
1745 else
1746 {
1747 /* For the non-shared case, discard space for relocs against
1748 symbols which turn out to need copy relocs or are not
1749 dynamic. */
1750
f5385ebf
AM
1751 if (!h->non_got_ref
1752 && ((h->def_dynamic
1753 && !h->def_regular)
b9734f35
JJ
1754 || (htab->elf.dynamic_sections_created
1755 && (h->root.type == bfd_link_hash_undefweak
1756 || h->root.type == bfd_link_hash_undefined))))
1757 {
1758 /* Make sure this symbol is output as a dynamic symbol.
1759 Undefined weak syms won't yet be marked as dynamic. */
1760 if (h->dynindx == -1
f5385ebf 1761 && !h->forced_local)
b9734f35 1762 {
c152c796 1763 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b9734f35
JJ
1764 return FALSE;
1765 }
1766
1767 /* If that succeeded, we know we'll be keeping all the
1768 relocs. */
1769 if (h->dynindx != -1)
1770 goto keep;
1771 }
252b5132 1772
b9734f35 1773 eh->dyn_relocs = NULL;
252b5132 1774
b9734f35 1775 keep: ;
252b5132
RH
1776 }
1777
b9734f35
JJ
1778 /* Finally, allocate space. */
1779 for (p = eh->dyn_relocs; p != NULL; p = p->next)
252b5132 1780 {
b9734f35 1781 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 1782 sreloc->size += p->count * sizeof (Elf32_External_Rela);
252b5132
RH
1783 }
1784
b9734f35
JJ
1785 return TRUE;
1786}
252b5132 1787
b9734f35 1788/* Find any dynamic relocs that apply to read-only sections. */
252b5132 1789
b9734f35
JJ
1790static bfd_boolean
1791readonly_dynrelocs (h, inf)
1792 struct elf_link_hash_entry *h;
1793 PTR inf;
1794{
1795 struct elf32_sparc_link_hash_entry *eh;
1796 struct elf32_sparc_dyn_relocs *p;
1797
1798 if (h->root.type == bfd_link_hash_warning)
1799 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1800
1801 eh = (struct elf32_sparc_link_hash_entry *) h;
1802 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1803 {
1804 asection *s = p->sec->output_section;
1805
1806 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1807 {
1808 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1809
1810 info->flags |= DF_TEXTREL;
1811
1812 /* Not an error, just cut short the traversal. */
1813 return FALSE;
1814 }
1815 }
b34976b6 1816 return TRUE;
252b5132
RH
1817}
1818
151e5294
EB
1819/* Return true if the dynamic symbol for a given section should be
1820 omitted when creating a shared library. */
1821
1822static bfd_boolean
1823elf32_sparc_omit_section_dynsym (bfd *output_bfd,
1824 struct bfd_link_info *info,
1825 asection *p)
1826{
1827 /* We keep the .got section symbol so that explicit relocations
1828 against the _GLOBAL_OFFSET_TABLE_ symbol emitted in PIC mode
1829 can be turned into relocations against the .got symbol. */
1830 if (strcmp (p->name, ".got") == 0)
1831 return FALSE;
1832
1833 return _bfd_elf_link_omit_section_dynsym (output_bfd, info, p);
1834}
1835
252b5132
RH
1836/* Set the sizes of the dynamic sections. */
1837
b34976b6 1838static bfd_boolean
252b5132 1839elf32_sparc_size_dynamic_sections (output_bfd, info)
db6751f2 1840 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
1841 struct bfd_link_info *info;
1842{
b9734f35 1843 struct elf32_sparc_link_hash_table *htab;
252b5132
RH
1844 bfd *dynobj;
1845 asection *s;
b9734f35 1846 bfd *ibfd;
252b5132 1847
b9734f35
JJ
1848 htab = elf32_sparc_hash_table (info);
1849 dynobj = htab->elf.dynobj;
252b5132
RH
1850 BFD_ASSERT (dynobj != NULL);
1851
1852 if (elf_hash_table (info)->dynamic_sections_created)
1853 {
1854 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 1855 if (info->executable)
252b5132
RH
1856 {
1857 s = bfd_get_section_by_name (dynobj, ".interp");
1858 BFD_ASSERT (s != NULL);
eea6121a 1859 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
252b5132
RH
1860 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1861 }
b9734f35 1862 }
252b5132 1863
b9734f35
JJ
1864 /* Set up .got offsets for local syms, and space for local dynamic
1865 relocs. */
1866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1867 {
1868 bfd_signed_vma *local_got;
1869 bfd_signed_vma *end_local_got;
1870 char *local_tls_type;
1871 bfd_size_type locsymcount;
1872 Elf_Internal_Shdr *symtab_hdr;
1873 asection *srel;
1874
1875 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1876 continue;
1877
1878 for (s = ibfd->sections; s != NULL; s = s->next)
1879 {
1880 struct elf32_sparc_dyn_relocs *p;
1881
1882 for (p = *((struct elf32_sparc_dyn_relocs **)
1883 &elf_section_data (s)->local_dynrel);
1884 p != NULL;
1885 p = p->next)
1886 {
1887 if (!bfd_is_abs_section (p->sec)
1888 && bfd_is_abs_section (p->sec->output_section))
1889 {
1890 /* Input section has been discarded, either because
1891 it is a copy of a linkonce section or due to
1892 linker script /DISCARD/, so we'll be discarding
1893 the relocs too. */
1894 }
1895 else if (p->count != 0)
1896 {
1897 srel = elf_section_data (p->sec)->sreloc;
eea6121a 1898 srel->size += p->count * sizeof (Elf32_External_Rela);
b9734f35
JJ
1899 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1900 info->flags |= DF_TEXTREL;
1901 }
1902 }
1903 }
1904
1905 local_got = elf_local_got_refcounts (ibfd);
1906 if (!local_got)
1907 continue;
1908
1909 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1910 locsymcount = symtab_hdr->sh_info;
1911 end_local_got = local_got + locsymcount;
1912 local_tls_type = elf32_sparc_local_got_tls_type (ibfd);
1913 s = htab->sgot;
1914 srel = htab->srelgot;
1915 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1916 {
1917 if (*local_got > 0)
1918 {
eea6121a
AM
1919 *local_got = s->size;
1920 s->size += 4;
b9734f35 1921 if (*local_tls_type == GOT_TLS_GD)
eea6121a 1922 s->size += 4;
b9734f35
JJ
1923 if (info->shared
1924 || *local_tls_type == GOT_TLS_GD
1925 || *local_tls_type == GOT_TLS_IE)
eea6121a 1926 srel->size += sizeof (Elf32_External_Rela);
b9734f35
JJ
1927 }
1928 else
1929 *local_got = (bfd_vma) -1;
1930 }
1931 }
1932
1933 if (htab->tls_ldm_got.refcount > 0)
1934 {
1935 /* Allocate 2 got entries and 1 dynamic reloc for
1936 R_SPARC_TLS_LDM_{HI22,LO10} relocs. */
eea6121a
AM
1937 htab->tls_ldm_got.offset = htab->sgot->size;
1938 htab->sgot->size += 8;
1939 htab->srelgot->size += sizeof (Elf32_External_Rela);
252b5132
RH
1940 }
1941 else
b9734f35
JJ
1942 htab->tls_ldm_got.offset = -1;
1943
1944 /* Allocate global sym .plt and .got entries, and space for global
1945 sym dynamic relocs. */
1946 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1947
1948 if (elf_hash_table (info)->dynamic_sections_created)
252b5132 1949 {
b9734f35 1950 /* Make space for the trailing nop in .plt. */
eea6121a
AM
1951 if (htab->splt->size > 0)
1952 htab->splt->size += 4;
b9734f35
JJ
1953
1954 /* If the .got section is more than 0x1000 bytes, we add
1955 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
1956 bit relocations have a greater chance of working. */
eea6121a 1957 if (htab->sgot->size >= 0x1000
b9734f35
JJ
1958 && elf_hash_table (info)->hgot->root.u.def.value == 0)
1959 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
252b5132
RH
1960 }
1961
1962 /* The check_relocs and adjust_dynamic_symbol entry points have
1963 determined the sizes of the various dynamic sections. Allocate
1964 memory for them. */
252b5132
RH
1965 for (s = dynobj->sections; s != NULL; s = s->next)
1966 {
1967 const char *name;
b9734f35 1968 bfd_boolean strip = FALSE;
252b5132
RH
1969
1970 if ((s->flags & SEC_LINKER_CREATED) == 0)
1971 continue;
1972
1973 /* It's OK to base decisions on the section name, because none
1974 of the dynobj section names depend upon the input files. */
1975 name = bfd_get_section_name (dynobj, s);
1976
252b5132
RH
1977 if (strncmp (name, ".rela", 5) == 0)
1978 {
eea6121a 1979 if (s->size == 0)
252b5132
RH
1980 {
1981 /* If we don't need this section, strip it from the
1982 output file. This is to handle .rela.bss and
1983 .rel.plt. We must create it in
1984 create_dynamic_sections, because it must be created
1985 before the linker maps input sections to output
1986 sections. The linker does that before
1987 adjust_dynamic_symbol is called, and it is that
1988 function which decides whether anything needs to go
1989 into these sections. */
b34976b6 1990 strip = TRUE;
252b5132
RH
1991 }
1992 else
1993 {
252b5132
RH
1994 /* We use the reloc_count field as a counter if we need
1995 to copy relocs into the output file. */
1996 s->reloc_count = 0;
1997 }
1998 }
b9734f35 1999 else if (s != htab->splt && s != htab->sgot)
252b5132
RH
2000 {
2001 /* It's not one of our sections, so don't allocate space. */
2002 continue;
2003 }
2004
2005 if (strip)
2006 {
7f8d5fc9 2007 _bfd_strip_section_from_output (info, s);
252b5132
RH
2008 continue;
2009 }
2010
2011 /* Allocate memory for the section contents. */
7a9af8c4
NC
2012 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
2013 Unused entries should be reclaimed before the section's contents
2014 are written out, but at the moment this does not happen. Thus in
2015 order to prevent writing out garbage, we initialise the section's
2016 contents to zero. */
eea6121a
AM
2017 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2018 if (s->contents == NULL && s->size != 0)
b34976b6 2019 return FALSE;
252b5132
RH
2020 }
2021
2022 if (elf_hash_table (info)->dynamic_sections_created)
2023 {
2024 /* Add some entries to the .dynamic section. We fill in the
2025 values later, in elf32_sparc_finish_dynamic_sections, but we
2026 must add the entries now so that we get the correct size for
2027 the .dynamic section. The DT_DEBUG entry is filled in by the
2028 dynamic linker and used by the debugger. */
dc810e39 2029#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2030 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2031
36af4a4e 2032 if (info->executable)
252b5132 2033 {
dc810e39 2034 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2035 return FALSE;
252b5132
RH
2036 }
2037
eea6121a 2038 if (htab->srelplt->size != 0)
252b5132 2039 {
dc810e39
AM
2040 if (!add_dynamic_entry (DT_PLTGOT, 0)
2041 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2042 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2043 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 2044 return FALSE;
252b5132
RH
2045 }
2046
dc810e39
AM
2047 if (!add_dynamic_entry (DT_RELA, 0)
2048 || !add_dynamic_entry (DT_RELASZ, 0)
2049 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
b34976b6 2050 return FALSE;
252b5132 2051
b9734f35
JJ
2052 /* If any dynamic relocs apply to a read-only section,
2053 then we need a DT_TEXTREL entry. */
2054 if ((info->flags & DF_TEXTREL) == 0)
2055 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2056 (PTR) info);
2057
db6751f2 2058 if (info->flags & DF_TEXTREL)
252b5132 2059 {
dc810e39 2060 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 2061 return FALSE;
252b5132
RH
2062 }
2063 }
dc810e39 2064#undef add_dynamic_entry
252b5132 2065
b34976b6 2066 return TRUE;
252b5132
RH
2067}
2068
f0abc2a1
AM
2069struct elf32_sparc_section_data
2070{
2071 struct bfd_elf_section_data elf;
2072 unsigned int do_relax;
2073};
2074
2075#define sec_do_relax(sec) \
68bfbfcc 2076 ((struct elf32_sparc_section_data *) elf_section_data (sec))->do_relax
f0abc2a1
AM
2077
2078static bfd_boolean
2079elf32_sparc_new_section_hook (abfd, sec)
2080 bfd *abfd;
2081 asection *sec;
2082{
2083 struct elf32_sparc_section_data *sdata;
2084 bfd_size_type amt = sizeof (*sdata);
2085
2086 sdata = (struct elf32_sparc_section_data *) bfd_zalloc (abfd, amt);
2087 if (sdata == NULL)
2088 return FALSE;
2089 sec->used_by_bfd = (PTR) sdata;
2090
2091 return _bfd_elf_new_section_hook (abfd, sec);
2092}
f7775d95 2093
b34976b6 2094static bfd_boolean
f7775d95
JJ
2095elf32_sparc_relax_section (abfd, section, link_info, again)
2096 bfd *abfd ATTRIBUTE_UNUSED;
2097 asection *section ATTRIBUTE_UNUSED;
2098 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
b34976b6 2099 bfd_boolean *again;
f7775d95 2100{
b34976b6 2101 *again = FALSE;
f0abc2a1 2102 sec_do_relax (section) = 1;
b34976b6 2103 return TRUE;
f7775d95
JJ
2104}
2105
b9734f35
JJ
2106/* Return the base VMA address which should be subtracted from real addresses
2107 when resolving @dtpoff relocation.
2108 This is PT_TLS segment p_vaddr. */
2109
2110static bfd_vma
2111dtpoff_base (info)
2112 struct bfd_link_info *info;
2113{
e1918d23
AM
2114 /* If tls_sec is NULL, we should have signalled an error already. */
2115 if (elf_hash_table (info)->tls_sec == NULL)
b9734f35 2116 return 0;
e1918d23 2117 return elf_hash_table (info)->tls_sec->vma;
b9734f35
JJ
2118}
2119
2120/* Return the relocation value for @tpoff relocation
2121 if STT_TLS virtual address is ADDRESS. */
2122
2123static bfd_vma
2124tpoff (info, address)
2125 struct bfd_link_info *info;
2126 bfd_vma address;
2127{
e1918d23 2128 struct elf_link_hash_table *htab = elf_hash_table (info);
b9734f35 2129
e1918d23
AM
2130 /* If tls_sec is NULL, we should have signalled an error already. */
2131 if (htab->tls_sec == NULL)
b9734f35 2132 return 0;
e1918d23 2133 return address - htab->tls_size - htab->tls_sec->vma;
b9734f35 2134}
e36f7d53 2135
252b5132
RH
2136/* Relocate a SPARC ELF section. */
2137
b34976b6 2138static bfd_boolean
252b5132
RH
2139elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
2140 contents, relocs, local_syms, local_sections)
2141 bfd *output_bfd;
2142 struct bfd_link_info *info;
2143 bfd *input_bfd;
2144 asection *input_section;
2145 bfd_byte *contents;
2146 Elf_Internal_Rela *relocs;
2147 Elf_Internal_Sym *local_syms;
2148 asection **local_sections;
2149{
b9734f35 2150 struct elf32_sparc_link_hash_table *htab;
252b5132
RH
2151 Elf_Internal_Shdr *symtab_hdr;
2152 struct elf_link_hash_entry **sym_hashes;
2153 bfd_vma *local_got_offsets;
2154 bfd_vma got_base;
252b5132
RH
2155 asection *sreloc;
2156 Elf_Internal_Rela *rel;
2157 Elf_Internal_Rela *relend;
2158
1049f94e 2159 if (info->relocatable)
b34976b6 2160 return TRUE;
f0fe0e16 2161
b9734f35 2162 htab = elf32_sparc_hash_table (info);
252b5132
RH
2163 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2164 sym_hashes = elf_sym_hashes (input_bfd);
2165 local_got_offsets = elf_local_got_offsets (input_bfd);
2166
2167 if (elf_hash_table (info)->hgot == NULL)
2168 got_base = 0;
2169 else
2170 got_base = elf_hash_table (info)->hgot->root.u.def.value;
2171
b9734f35 2172 sreloc = elf_section_data (input_section)->sreloc;
252b5132
RH
2173
2174 rel = relocs;
2175 relend = relocs + input_section->reloc_count;
2176 for (; rel < relend; rel++)
2177 {
b9734f35 2178 int r_type, tls_type;
252b5132
RH
2179 reloc_howto_type *howto;
2180 unsigned long r_symndx;
2181 struct elf_link_hash_entry *h;
2182 Elf_Internal_Sym *sym;
2183 asection *sec;
e36f7d53 2184 bfd_vma relocation, off;
252b5132 2185 bfd_reloc_status_type r;
b34976b6
AM
2186 bfd_boolean is_plt = FALSE;
2187 bfd_boolean unresolved_reloc;
252b5132
RH
2188
2189 r_type = ELF32_R_TYPE (rel->r_info);
2190
435b1e90 2191 if (r_type == R_SPARC_GNU_VTINHERIT
26e41594
AM
2192 || r_type == R_SPARC_GNU_VTENTRY)
2193 continue;
252b5132 2194
60dac299 2195 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
252b5132
RH
2196 {
2197 bfd_set_error (bfd_error_bad_value);
b34976b6 2198 return FALSE;
252b5132
RH
2199 }
2200 howto = _bfd_sparc_elf_howto_table + r_type;
2201
252b5132 2202 /* This is a final link. */
f0fe0e16 2203 r_symndx = ELF32_R_SYM (rel->r_info);
252b5132
RH
2204 h = NULL;
2205 sym = NULL;
2206 sec = NULL;
b34976b6 2207 unresolved_reloc = FALSE;
252b5132
RH
2208 if (r_symndx < symtab_hdr->sh_info)
2209 {
2210 sym = local_syms + r_symndx;
2211 sec = local_sections[r_symndx];
8517fae7 2212 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
252b5132
RH
2213 }
2214 else
2215 {
560e09e9 2216 bfd_boolean warned ATTRIBUTE_UNUSED;
e36f7d53 2217
b2a8e766
AM
2218 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2219 r_symndx, symtab_hdr, sym_hashes,
2220 h, sec, relocation,
2221 unresolved_reloc, warned);
252b5132
RH
2222 }
2223
2224 switch (r_type)
2225 {
2226 case R_SPARC_GOT10:
2227 case R_SPARC_GOT13:
2228 case R_SPARC_GOT22:
2229 /* Relocation is to the entry for this symbol in the global
26e41594 2230 offset table. */
b9734f35
JJ
2231 if (htab->sgot == NULL)
2232 abort ();
252b5132
RH
2233
2234 if (h != NULL)
2235 {
b34976b6 2236 bfd_boolean dyn;
252b5132
RH
2237
2238 off = h->got.offset;
2239 BFD_ASSERT (off != (bfd_vma) -1);
e36f7d53 2240 dyn = elf_hash_table (info)->dynamic_sections_created;
252b5132 2241
c152c796 2242 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
252b5132 2243 || (info->shared
e36f7d53
RH
2244 && (info->symbolic
2245 || h->dynindx == -1
f5385ebf
AM
2246 || h->forced_local)
2247 && h->def_regular))
252b5132
RH
2248 {
2249 /* This is actually a static link, or it is a
26e41594
AM
2250 -Bsymbolic link and the symbol is defined
2251 locally, or the symbol was forced to be local
2252 because of a version file. We must initialize
2253 this entry in the global offset table. Since the
2254 offset must always be a multiple of 4, we use the
2255 least significant bit to record whether we have
2256 initialized it already.
252b5132
RH
2257
2258 When doing a dynamic link, we create a .rela.got
2259 relocation entry to initialize the value. This
2260 is done in the finish_dynamic_symbol routine. */
2261 if ((off & 1) != 0)
2262 off &= ~1;
2263 else
2264 {
2265 bfd_put_32 (output_bfd, relocation,
b9734f35 2266 htab->sgot->contents + off);
252b5132
RH
2267 h->got.offset |= 1;
2268 }
2269 }
e36f7d53 2270 else
b34976b6 2271 unresolved_reloc = FALSE;
252b5132
RH
2272 }
2273 else
2274 {
252b5132
RH
2275 BFD_ASSERT (local_got_offsets != NULL
2276 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2277
2278 off = local_got_offsets[r_symndx];
2279
2280 /* The offset must always be a multiple of 4. We use
2281 the least significant bit to record whether we have
2282 already processed this entry. */
2283 if ((off & 1) != 0)
2284 off &= ~1;
2285 else
2286 {
252b5132
RH
2287
2288 if (info->shared)
2289 {
b34976b6 2290 asection *s;
252b5132 2291 Elf_Internal_Rela outrel;
b34976b6 2292 bfd_byte *loc;
252b5132
RH
2293
2294 /* We need to generate a R_SPARC_RELATIVE reloc
2295 for the dynamic linker. */
b9734f35 2296 s = htab->srelgot;
b34976b6 2297 BFD_ASSERT (s != NULL);
252b5132 2298
b9734f35
JJ
2299 outrel.r_offset = (htab->sgot->output_section->vma
2300 + htab->sgot->output_offset
252b5132
RH
2301 + off);
2302 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
6b3ac709
JJ
2303 outrel.r_addend = relocation;
2304 relocation = 0;
b34976b6
AM
2305 loc = s->contents;
2306 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
2307 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132
RH
2308 }
2309
b9734f35
JJ
2310 bfd_put_32 (output_bfd, relocation,
2311 htab->sgot->contents + off);
252b5132
RH
2312 local_got_offsets[r_symndx] |= 1;
2313 }
252b5132 2314 }
b9734f35 2315 relocation = htab->sgot->output_offset + off - got_base;
252b5132
RH
2316 break;
2317
bd5e6e7e
JJ
2318 case R_SPARC_PLT32:
2319 if (h == NULL || h->plt.offset == (bfd_vma) -1)
2320 {
2321 r_type = R_SPARC_32;
2322 goto r_sparc_plt32;
2323 }
2324 /* Fall through. */
252b5132 2325 case R_SPARC_WPLT30:
b9734f35 2326 r_sparc_wplt30:
252b5132 2327 /* Relocation is to the entry for this symbol in the
26e41594 2328 procedure linkage table. */
252b5132
RH
2329
2330 /* The Solaris native assembler will generate a WPLT30 reloc
2331 for a local symbol if you assemble a call from one
2332 section to another when using -K pic. We treat it as
2333 WDISP30. */
2334 if (h == NULL)
2335 break;
2336
6de00a78 2337 if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL)
252b5132
RH
2338 {
2339 /* We didn't make a PLT entry for this symbol. This
26e41594
AM
2340 happens when statically linking PIC code, or when
2341 using -Bsymbolic. */
252b5132
RH
2342 break;
2343 }
2344
b9734f35
JJ
2345 relocation = (htab->splt->output_section->vma
2346 + htab->splt->output_offset
252b5132 2347 + h->plt.offset);
b34976b6 2348 unresolved_reloc = FALSE;
bd5e6e7e
JJ
2349 if (r_type == R_SPARC_PLT32)
2350 {
2351 r_type = R_SPARC_32;
b34976b6 2352 is_plt = TRUE;
bd5e6e7e
JJ
2353 goto r_sparc_plt32;
2354 }
252b5132
RH
2355 break;
2356
2357 case R_SPARC_PC10:
2358 case R_SPARC_PC22:
2359 if (h != NULL
2360 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2361 break;
2362 /* Fall through. */
2363 case R_SPARC_DISP8:
2364 case R_SPARC_DISP16:
2365 case R_SPARC_DISP32:
2366 case R_SPARC_WDISP30:
2367 case R_SPARC_WDISP22:
2368 case R_SPARC_WDISP19:
2369 case R_SPARC_WDISP16:
252b5132
RH
2370 case R_SPARC_8:
2371 case R_SPARC_16:
2372 case R_SPARC_32:
2373 case R_SPARC_HI22:
2374 case R_SPARC_22:
2375 case R_SPARC_13:
2376 case R_SPARC_LO10:
0f2712ed 2377 case R_SPARC_UA16:
252b5132 2378 case R_SPARC_UA32:
bd5e6e7e 2379 r_sparc_plt32:
b9734f35
JJ
2380 /* r_symndx will be zero only for relocs against symbols
2381 from removed linkonce sections, or sections discarded by
2382 a linker script. */
2383 if (r_symndx == 0
2384 || (input_section->flags & SEC_ALLOC) == 0)
2385 break;
2386
2387 if ((info->shared
4dc570c2
JJ
2388 && (h == NULL
2389 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2390 || h->root.type != bfd_link_hash_undefweak)
b9734f35
JJ
2391 && (! howto->pc_relative
2392 || (h != NULL
2393 && h->dynindx != -1
2394 && (! info->symbolic
f5385ebf 2395 || !h->def_regular))))
b9734f35
JJ
2396 || (!info->shared
2397 && h != NULL
2398 && h->dynindx != -1
f5385ebf
AM
2399 && !h->non_got_ref
2400 && ((h->def_dynamic
2401 && !h->def_regular)
b9734f35
JJ
2402 || h->root.type == bfd_link_hash_undefweak
2403 || h->root.type == bfd_link_hash_undefined)))
252b5132
RH
2404 {
2405 Elf_Internal_Rela outrel;
b34976b6
AM
2406 bfd_byte *loc;
2407 bfd_boolean skip, relocate = FALSE;
252b5132
RH
2408
2409 /* When generating a shared object, these relocations
26e41594
AM
2410 are copied into the output file to be resolved at run
2411 time. */
252b5132 2412
b9734f35 2413 BFD_ASSERT (sreloc != NULL);
252b5132 2414
b34976b6 2415 skip = FALSE;
252b5132 2416
c629eae0
JJ
2417 outrel.r_offset =
2418 _bfd_elf_section_offset (output_bfd, info, input_section,
2419 rel->r_offset);
2420 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2421 skip = TRUE;
0bb2d96a 2422 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2423 skip = TRUE, relocate = TRUE;
252b5132
RH
2424 outrel.r_offset += (input_section->output_section->vma
2425 + input_section->output_offset);
2426
20278fa3
JJ
2427 /* Optimize unaligned reloc usage now that we know where
2428 it finally resides. */
2429 switch (r_type)
2430 {
2431 case R_SPARC_16:
2432 if (outrel.r_offset & 1)
2433 r_type = R_SPARC_UA16;
2434 break;
2435 case R_SPARC_UA16:
2436 if (!(outrel.r_offset & 1))
2437 r_type = R_SPARC_16;
2438 break;
2439 case R_SPARC_32:
2440 if (outrel.r_offset & 3)
2441 r_type = R_SPARC_UA32;
2442 break;
2443 case R_SPARC_UA32:
2444 if (!(outrel.r_offset & 3))
2445 r_type = R_SPARC_32;
2446 break;
b88a866a
JJ
2447 case R_SPARC_DISP8:
2448 case R_SPARC_DISP16:
2449 case R_SPARC_DISP32:
2450 /* If the symbol is not dynamic, we should not keep
2451 a dynamic relocation. But an .rela.* slot has been
2452 allocated for it, output R_SPARC_NONE.
2453 FIXME: Add code tracking needed dynamic relocs as
2454 e.g. i386 has. */
2455 if (h->dynindx == -1)
b34976b6 2456 skip = TRUE, relocate = TRUE;
b88a866a 2457 break;
20278fa3
JJ
2458 }
2459
252b5132
RH
2460 if (skip)
2461 memset (&outrel, 0, sizeof outrel);
2462 /* h->dynindx may be -1 if the symbol was marked to
26e41594 2463 become local. */
bd5e6e7e 2464 else if (h != NULL && ! is_plt
252b5132 2465 && ((! info->symbolic && h->dynindx != -1)
f5385ebf 2466 || !h->def_regular))
252b5132
RH
2467 {
2468 BFD_ASSERT (h->dynindx != -1);
2469 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2470 outrel.r_addend = rel->r_addend;
2471 }
2472 else
2473 {
1fbc4a84 2474 if (r_type == R_SPARC_32)
252b5132
RH
2475 {
2476 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
2477 outrel.r_addend = relocation + rel->r_addend;
2478 }
2479 else
2480 {
2481 long indx;
2482
bd5e6e7e 2483 if (is_plt)
b9734f35 2484 sec = htab->splt;
8517fae7
AM
2485
2486 if (bfd_is_abs_section (sec))
252b5132
RH
2487 indx = 0;
2488 else if (sec == NULL || sec->owner == NULL)
2489 {
2490 bfd_set_error (bfd_error_bad_value);
b34976b6 2491 return FALSE;
252b5132
RH
2492 }
2493 else
2494 {
2495 asection *osec;
2496
2497 osec = sec->output_section;
2498 indx = elf_section_data (osec)->dynindx;
2499
2500 /* FIXME: we really should be able to link non-pic
2501 shared libraries. */
2502 if (indx == 0)
2503 {
2504 BFD_FAIL ();
2505 (*_bfd_error_handler)
d003868e
AM
2506 (_("%B: probably compiled without -fPIC?"),
2507 input_bfd);
252b5132 2508 bfd_set_error (bfd_error_bad_value);
b34976b6 2509 return FALSE;
252b5132
RH
2510 }
2511 }
2512
2513 outrel.r_info = ELF32_R_INFO (indx, r_type);
840a9995 2514 outrel.r_addend = relocation + rel->r_addend;
252b5132
RH
2515 }
2516 }
2517
b34976b6
AM
2518 loc = sreloc->contents;
2519 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2520 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132
RH
2521
2522 /* This reloc will be computed at runtime, so there's no
26e41594 2523 need to do anything now. */
0bb2d96a
JJ
2524 if (! relocate)
2525 continue;
252b5132
RH
2526 }
2527 break;
2528
b9734f35
JJ
2529 case R_SPARC_TLS_GD_HI22:
2530 if (! elf32_sparc_tdata (input_bfd)->has_tlsgd)
2531 {
2532 /* R_SPARC_REV32 used the same reloc number as
2533 R_SPARC_TLS_GD_HI22. */
2534 r_type = R_SPARC_REV32;
2535 break;
2536 }
2537 /* Fall through */
2538
2539 case R_SPARC_TLS_GD_LO10:
2540 case R_SPARC_TLS_IE_HI22:
2541 case R_SPARC_TLS_IE_LO10:
2542 r_type = elf32_sparc_tls_transition (info, input_bfd, r_type,
2543 h == NULL);
2544 tls_type = GOT_UNKNOWN;
2545 if (h == NULL && local_got_offsets)
2546 tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2547 else if (h != NULL)
2548 {
2549 tls_type = elf32_sparc_hash_entry(h)->tls_type;
2550 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2551 switch (ELF32_R_TYPE (rel->r_info))
2552 {
2553 case R_SPARC_TLS_GD_HI22:
2554 case R_SPARC_TLS_IE_HI22:
2555 r_type = R_SPARC_TLS_LE_HIX22;
2556 break;
2557 default:
2558 r_type = R_SPARC_TLS_LE_LOX10;
2559 break;
2560 }
2561 }
2562 if (tls_type == GOT_TLS_IE)
2563 switch (r_type)
2564 {
2565 case R_SPARC_TLS_GD_HI22:
2566 r_type = R_SPARC_TLS_IE_HI22;
2567 break;
2568 case R_SPARC_TLS_GD_LO10:
2569 r_type = R_SPARC_TLS_IE_LO10;
2570 break;
2571 }
2572
2573 if (r_type == R_SPARC_TLS_LE_HIX22)
2574 {
2575 relocation = tpoff (info, relocation);
2576 break;
2577 }
2578 if (r_type == R_SPARC_TLS_LE_LOX10)
2579 {
2580 /* Change add into xor. */
2581 relocation = tpoff (info, relocation);
2582 bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
2583 contents + rel->r_offset)
2584 | 0x80182000), contents + rel->r_offset);
2585 break;
2586 }
2587
26e41594 2588 if (h != NULL)
b9734f35
JJ
2589 {
2590 off = h->got.offset;
2591 h->got.offset |= 1;
2592 }
2593 else
2594 {
2595 BFD_ASSERT (local_got_offsets != NULL);
2596 off = local_got_offsets[r_symndx];
2597 local_got_offsets[r_symndx] |= 1;
2598 }
26e41594 2599
b9734f35
JJ
2600 r_sparc_tlsldm:
2601 if (htab->sgot == NULL)
2602 abort ();
2603
2604 if ((off & 1) != 0)
2605 off &= ~1;
26e41594 2606 else
b9734f35
JJ
2607 {
2608 Elf_Internal_Rela outrel;
2609 Elf32_External_Rela *loc;
2610 int dr_type, indx;
2611
2612 if (htab->srelgot == NULL)
2613 abort ();
2614
2615 bfd_put_32 (output_bfd, 0, htab->sgot->contents + off);
2616 outrel.r_offset = (htab->sgot->output_section->vma
2617 + htab->sgot->output_offset + off);
2618 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2619 if (r_type == R_SPARC_TLS_IE_HI22
2620 || r_type == R_SPARC_TLS_IE_LO10)
2621 dr_type = R_SPARC_TLS_TPOFF32;
2622 else
2623 dr_type = R_SPARC_TLS_DTPMOD32;
2624 if (dr_type == R_SPARC_TLS_TPOFF32 && indx == 0)
2625 outrel.r_addend = relocation - dtpoff_base (info);
2626 else
2627 outrel.r_addend = 0;
2628 outrel.r_info = ELF32_R_INFO (indx, dr_type);
2629 loc = (Elf32_External_Rela *) htab->srelgot->contents;
2630 loc += htab->srelgot->reloc_count++;
2631 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2632 (bfd_byte *) loc);
2633
2634 if (r_type == R_SPARC_TLS_GD_HI22
2635 || r_type == R_SPARC_TLS_GD_LO10)
2636 {
2637 if (indx == 0)
2638 {
2639 BFD_ASSERT (! unresolved_reloc);
2640 bfd_put_32 (output_bfd,
2641 relocation - dtpoff_base (info),
2642 htab->sgot->contents + off + 4);
2643 }
2644 else
2645 {
2646 bfd_put_32 (output_bfd, 0,
2647 htab->sgot->contents + off + 4);
2648 outrel.r_info = ELF32_R_INFO (indx,
2649 R_SPARC_TLS_DTPOFF32);
2650 outrel.r_offset += 4;
2651 htab->srelgot->reloc_count++;
2652 loc++;
2653 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2654 (bfd_byte *) loc);
2655 }
2656 }
2657 else if (dr_type == R_SPARC_TLS_DTPMOD32)
2658 {
2659 bfd_put_32 (output_bfd, 0,
2660 htab->sgot->contents + off + 4);
2661 }
2662 }
2663
2664 if (off >= (bfd_vma) -2)
2665 abort ();
2666
2667 relocation = htab->sgot->output_offset + off - got_base;
2668 unresolved_reloc = FALSE;
2669 howto = _bfd_sparc_elf_howto_table + r_type;
2670 break;
2671
2672 case R_SPARC_TLS_LDM_HI22:
2673 case R_SPARC_TLS_LDM_LO10:
2674 if (! info->shared)
2675 {
2676 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2677 continue;
2678 }
2679 off = htab->tls_ldm_got.offset;
2680 htab->tls_ldm_got.offset |= 1;
2681 goto r_sparc_tlsldm;
2682
2683 case R_SPARC_TLS_LDO_HIX22:
2684 case R_SPARC_TLS_LDO_LOX10:
2685 if (info->shared)
2686 relocation -= dtpoff_base (info);
2687 else
2688 relocation = tpoff (info, relocation);
2689 break;
2690
2691 case R_SPARC_TLS_LE_HIX22:
2692 case R_SPARC_TLS_LE_LOX10:
2693 if (info->shared)
2694 {
2695 Elf_Internal_Rela outrel;
2696 bfd_boolean skip, relocate = FALSE;
2697
2698 BFD_ASSERT (sreloc != NULL);
2699 skip = FALSE;
2700 outrel.r_offset =
2701 _bfd_elf_section_offset (output_bfd, info, input_section,
2702 rel->r_offset);
2703 if (outrel.r_offset == (bfd_vma) -1)
2704 skip = TRUE;
2705 else if (outrel.r_offset == (bfd_vma) -2)
2706 skip = TRUE, relocate = TRUE;
2707 outrel.r_offset += (input_section->output_section->vma
2708 + input_section->output_offset);
2709 if (skip)
2710 memset (&outrel, 0, sizeof outrel);
2711 else
2712 {
2713 outrel.r_info = ELF32_R_INFO (0, r_type);
2714 outrel.r_addend = relocation - dtpoff_base (info)
2715 + rel->r_addend;
2716 }
2717
2718 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2719 (bfd_byte *) (((Elf32_External_Rela *)
2720 sreloc->contents)
2721 + sreloc->reloc_count));
2722 ++sreloc->reloc_count;
2723 continue;
2724 }
2725 relocation = tpoff (info, relocation);
2726 break;
2727
2728 case R_SPARC_TLS_LDM_CALL:
2729 if (! info->shared)
2730 {
2731 /* mov %g0, %o0 */
2732 bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
2733 continue;
2734 }
2735 /* Fall through */
2736
2737 case R_SPARC_TLS_GD_CALL:
2738 tls_type = GOT_UNKNOWN;
2739 if (h == NULL && local_got_offsets)
2740 tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2741 else if (h != NULL)
2742 tls_type = elf32_sparc_hash_entry(h)->tls_type;
2743 if (! info->shared
2744 || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
2745 {
2746 bfd_vma insn;
2747
2748 if (!info->shared && (h == NULL || h->dynindx == -1))
2749 {
2750 /* GD -> LE */
2751 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2752 continue;
2753 }
2754
2755 /* GD -> IE */
2756 if (rel + 1 < relend
2757 && ELF32_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
2758 && rel[1].r_offset == rel->r_offset + 4
2759 && ELF32_R_SYM (rel[1].r_info) == r_symndx
2760 && (((insn = bfd_get_32 (input_bfd,
2761 contents + rel[1].r_offset))
2762 >> 25) & 0x1f) == 8)
2763 {
2764 /* We have
2765 call __tls_get_addr, %tgd_call(foo)
2766 add %reg1, %reg2, %o0, %tgd_add(foo)
2767 and change it into IE:
2768 ld [%reg1 + %reg2], %o0, %tie_ld(foo)
2769 add %g7, %o0, %o0, %tie_add(foo).
2770 add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
2771 ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2. */
2772 bfd_put_32 (output_bfd, insn | 0xc0000000,
2773 contents + rel->r_offset);
2774 bfd_put_32 (output_bfd, 0x9001c008,
2775 contents + rel->r_offset + 4);
2776 rel++;
2777 continue;
2778 }
2779
2780 bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset);
2781 continue;
2782 }
2783
2784 h = (struct elf_link_hash_entry *)
2785 bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
2786 FALSE, TRUE);
2787 BFD_ASSERT (h != NULL);
2788 r_type = R_SPARC_WPLT30;
2789 howto = _bfd_sparc_elf_howto_table + r_type;
2790 goto r_sparc_wplt30;
2791
2792 case R_SPARC_TLS_GD_ADD:
2793 tls_type = GOT_UNKNOWN;
2794 if (h == NULL && local_got_offsets)
2795 tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2796 else if (h != NULL)
2797 tls_type = elf32_sparc_hash_entry(h)->tls_type;
2798 if (! info->shared || tls_type == GOT_TLS_IE)
2799 {
2800 /* add %reg1, %reg2, %reg3, %tgd_add(foo)
2801 changed into IE:
2802 ld [%reg1 + %reg2], %reg3, %tie_ld(foo)
2803 or LE:
2804 add %g7, %reg2, %reg3. */
2805 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2806 if ((h != NULL && h->dynindx != -1) || info->shared)
2807 relocation = insn | 0xc0000000;
2808 else
2809 relocation = (insn & ~0x7c000) | 0x1c000;
2810 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
2811 }
2812 continue;
2813
2814 case R_SPARC_TLS_LDM_ADD:
2815 if (! info->shared)
2816 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2817 continue;
2818
2819 case R_SPARC_TLS_LDO_ADD:
2820 if (! info->shared)
2821 {
2822 /* Change rs1 into %g7. */
2823 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2824 insn = (insn & ~0x7c000) | 0x1c000;
2825 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2826 }
2827 continue;
2828
2829 case R_SPARC_TLS_IE_LD:
2830 case R_SPARC_TLS_IE_LDX:
2831 if (! info->shared && (h == NULL || h->dynindx == -1))
2832 {
2833 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2834 int rs2 = insn & 0x1f;
2835 int rd = (insn >> 25) & 0x1f;
2836
2837 if (rs2 == rd)
2838 relocation = SPARC_NOP;
2839 else
2840 relocation = 0x80100000 | (insn & 0x3e00001f);
2841 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
2842 }
2843 continue;
2844
2845 case R_SPARC_TLS_IE_ADD:
2846 /* Totally useless relocation. */
2847 continue;
2848
2849 case R_SPARC_TLS_DTPOFF32:
2850 relocation -= dtpoff_base (info);
2851 break;
2852
252b5132
RH
2853 default:
2854 break;
2855 }
2856
239e1f3a
AM
2857 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2858 because such sections are not SEC_ALLOC and thus ld.so will
2859 not process them. */
e36f7d53 2860 if (unresolved_reloc
239e1f3a 2861 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 2862 && h->def_dynamic))
e36f7d53 2863 (*_bfd_error_handler)
d003868e
AM
2864 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2865 input_bfd,
2866 input_section,
e36f7d53
RH
2867 (long) rel->r_offset,
2868 h->root.root.string);
2869
f7775d95 2870 r = bfd_reloc_continue;
252b5132
RH
2871 if (r_type == R_SPARC_WDISP16)
2872 {
2873 bfd_vma x;
2874
2875 relocation += rel->r_addend;
2876 relocation -= (input_section->output_section->vma
2877 + input_section->output_offset);
2878 relocation -= rel->r_offset;
2879
2880 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2881 x |= ((((relocation >> 2) & 0xc000) << 6)
2882 | ((relocation >> 2) & 0x3fff));
2883 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2884
2885 if ((bfd_signed_vma) relocation < - 0x40000
2886 || (bfd_signed_vma) relocation > 0x3ffff)
2887 r = bfd_reloc_overflow;
2888 else
2889 r = bfd_reloc_ok;
2890 }
2891 else if (r_type == R_SPARC_REV32)
2892 {
2893 bfd_vma x;
2894
2895 relocation = relocation + rel->r_addend;
2896
2897 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2898 x = x + relocation;
2899 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
b9734f35
JJ
2900 r = bfd_reloc_ok;
2901 }
2902 else if (r_type == R_SPARC_TLS_LDO_HIX22
2903 || r_type == R_SPARC_TLS_LE_HIX22)
2904 {
2905 bfd_vma x;
2906
2907 relocation += rel->r_addend;
2908 relocation = relocation ^ 0xffffffff;
2909
2910 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2911 x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
2912 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2913 r = bfd_reloc_ok;
2914 }
2915 else if (r_type == R_SPARC_TLS_LDO_LOX10
2916 || r_type == R_SPARC_TLS_LE_LOX10)
2917 {
2918 bfd_vma x;
2919
2920 relocation += rel->r_addend;
2921 relocation = (relocation & 0x3ff) | 0x1c00;
2922
2923 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2924 x = (x & ~(bfd_vma) 0x1fff) | relocation;
2925 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2926
252b5132
RH
2927 r = bfd_reloc_ok;
2928 }
f7775d95 2929 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
f0abc2a1 2930 && sec_do_relax (input_section)
eea6121a 2931 && rel->r_offset + 4 < input_section->size)
f7775d95
JJ
2932 {
2933#define G0 0
2934#define O7 15
2935#define XCC (2 << 20)
2936#define COND(x) (((x)&0xf)<<25)
2937#define CONDA COND(0x8)
2938#define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
2939#define INSN_BA (F2(0,2) | CONDA)
2940#define INSN_OR F3(2, 0x2, 0)
2941#define INSN_NOP F2(0,4)
2942
2943 bfd_vma x, y;
2944
2945 /* If the instruction is a call with either:
2946 restore
2947 arithmetic instruction with rd == %o7
2948 where rs1 != %o7 and rs2 if it is register != %o7
2949 then we can optimize if the call destination is near
2950 by changing the call into a branch always. */
2951 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2952 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
2953 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
2954 {
2955 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
2956 || ((y & OP3(0x28)) == 0 /* arithmetic */
2957 && (y & RD(~0)) == RD(O7)))
2958 && (y & RS1(~0)) != RS1(O7)
2959 && ((y & F3I(~0))
2960 || (y & RS2(~0)) != RS2(O7)))
2961 {
2962 bfd_vma reloc;
2963
2964 reloc = relocation + rel->r_addend - rel->r_offset;
2965 reloc -= (input_section->output_section->vma
2966 + input_section->output_offset);
2967
2968 /* Ensure the reloc fits into simm22. */
2969 if ((reloc & 3) == 0
2970 && ((reloc & ~(bfd_vma)0x7fffff) == 0
2971 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
2972 {
2973 reloc >>= 2;
435b1e90 2974
f7775d95
JJ
2975 /* Check whether it fits into simm19 on v9. */
2976 if (((reloc & 0x3c0000) == 0
2977 || (reloc & 0x3c0000) == 0x3c0000)
2978 && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
2979 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
2980 else
2981 x = INSN_BA | (reloc & 0x3fffff); /* ba */
2982 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2983 r = bfd_reloc_ok;
2984 if (rel->r_offset >= 4
2985 && (y & (0xffffffff ^ RS1(~0)))
2986 == (INSN_OR | RD(O7) | RS2(G0)))
2987 {
2988 bfd_vma z;
2989 unsigned int reg;
2990
2991 z = bfd_get_32 (input_bfd,
2992 contents + rel->r_offset - 4);
2993 if ((z & (0xffffffff ^ RD(~0)))
2994 != (INSN_OR | RS1(O7) | RS2(G0)))
2995 break;
2996
2997 /* The sequence was
2998 or %o7, %g0, %rN
2999 call foo
3000 or %rN, %g0, %o7
3001
3002 If call foo was replaced with ba, replace
3003 or %rN, %g0, %o7 with nop. */
3004
3005 reg = (y & RS1(~0)) >> 14;
3006 if (reg != ((z & RD(~0)) >> 25)
3007 || reg == G0 || reg == O7)
3008 break;
3009
dc810e39 3010 bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
f7775d95
JJ
3011 contents + rel->r_offset + 4);
3012 }
3013
3014 }
3015 }
3016 }
3017 }
3018
3019 if (r == bfd_reloc_continue)
252b5132
RH
3020 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3021 contents, rel->r_offset,
3022 relocation, rel->r_addend);
3023
252b5132
RH
3024 if (r != bfd_reloc_ok)
3025 {
3026 switch (r)
3027 {
3028 default:
3029 case bfd_reloc_outofrange:
3030 abort ();
3031 case bfd_reloc_overflow:
3032 {
3033 const char *name;
3034
3035 if (h != NULL)
dfeffb9f 3036 name = NULL;
252b5132
RH
3037 else
3038 {
3039 name = bfd_elf_string_from_elf_section (input_bfd,
3040 symtab_hdr->sh_link,
3041 sym->st_name);
3042 if (name == NULL)
b34976b6 3043 return FALSE;
252b5132
RH
3044 if (*name == '\0')
3045 name = bfd_section_name (input_bfd, sec);
3046 }
3047 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3048 (info, (h ? &h->root : NULL), name, howto->name,
3049 (bfd_vma) 0, input_bfd, input_section,
3050 rel->r_offset)))
b34976b6 3051 return FALSE;
252b5132
RH
3052 }
3053 break;
3054 }
3055 }
3056 }
3057
b34976b6 3058 return TRUE;
252b5132
RH
3059}
3060
3061/* Finish up dynamic symbol handling. We set the contents of various
3062 dynamic sections here. */
3063
b34976b6 3064static bfd_boolean
252b5132
RH
3065elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
3066 bfd *output_bfd;
3067 struct bfd_link_info *info;
3068 struct elf_link_hash_entry *h;
3069 Elf_Internal_Sym *sym;
3070{
3071 bfd *dynobj;
b9734f35 3072 struct elf32_sparc_link_hash_table *htab;
26e41594 3073
b9734f35
JJ
3074 htab = elf32_sparc_hash_table (info);
3075 dynobj = htab->elf.dynobj;
252b5132
RH
3076
3077 if (h->plt.offset != (bfd_vma) -1)
3078 {
3079 asection *splt;
3080 asection *srela;
3081 Elf_Internal_Rela rela;
b34976b6 3082 bfd_byte *loc;
252b5132
RH
3083
3084 /* This symbol has an entry in the procedure linkage table. Set
26e41594 3085 it up. */
252b5132
RH
3086
3087 BFD_ASSERT (h->dynindx != -1);
3088
b9734f35
JJ
3089 splt = htab->splt;
3090 srela = htab->srelplt;
252b5132
RH
3091 BFD_ASSERT (splt != NULL && srela != NULL);
3092
3093 /* Fill in the entry in the procedure linkage table. */
3094 bfd_put_32 (output_bfd,
3095 PLT_ENTRY_WORD0 + h->plt.offset,
3096 splt->contents + h->plt.offset);
3097 bfd_put_32 (output_bfd,
3098 (PLT_ENTRY_WORD1
3099 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
3100 splt->contents + h->plt.offset + 4);
dc810e39 3101 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
252b5132
RH
3102 splt->contents + h->plt.offset + 8);
3103
3104 /* Fill in the entry in the .rela.plt section. */
3105 rela.r_offset = (splt->output_section->vma
3106 + splt->output_offset
3107 + h->plt.offset);
3108 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
3109 rela.r_addend = 0;
b34976b6
AM
3110 loc = srela->contents;
3111 loc += (h->plt.offset / PLT_ENTRY_SIZE - 4) * sizeof (Elf32_External_Rela);
3112 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132 3113
f5385ebf 3114 if (!h->def_regular)
252b5132
RH
3115 {
3116 /* Mark the symbol as undefined, rather than as defined in
3117 the .plt section. Leave the value alone. */
3118 sym->st_shndx = SHN_UNDEF;
8701c1bc
JJ
3119 /* If the symbol is weak, we do need to clear the value.
3120 Otherwise, the PLT entry would provide a definition for
3121 the symbol even if the symbol wasn't defined anywhere,
3122 and so the symbol would never be NULL. */
f5385ebf 3123 if (!h->ref_regular_nonweak)
8701c1bc 3124 sym->st_value = 0;
252b5132
RH
3125 }
3126 }
3127
b9734f35
JJ
3128 if (h->got.offset != (bfd_vma) -1
3129 && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_GD
3130 && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_IE)
252b5132
RH
3131 {
3132 asection *sgot;
3133 asection *srela;
3134 Elf_Internal_Rela rela;
b34976b6 3135 bfd_byte *loc;
252b5132
RH
3136
3137 /* This symbol has an entry in the global offset table. Set it
26e41594 3138 up. */
252b5132 3139
b9734f35
JJ
3140 sgot = htab->sgot;
3141 srela = htab->srelgot;
252b5132
RH
3142 BFD_ASSERT (sgot != NULL && srela != NULL);
3143
3144 rela.r_offset = (sgot->output_section->vma
3145 + sgot->output_offset
dc810e39 3146 + (h->got.offset &~ (bfd_vma) 1));
252b5132
RH
3147
3148 /* If this is a -Bsymbolic link, and the symbol is defined
3149 locally, we just want to emit a RELATIVE reloc. Likewise if
3150 the symbol was forced to be local because of a version file.
3151 The entry in the global offset table will already have been
3152 initialized in the relocate_section function. */
3153 if (info->shared
3154 && (info->symbolic || h->dynindx == -1)
f5385ebf 3155 && h->def_regular)
6b3ac709
JJ
3156 {
3157 asection *sec = h->root.u.def.section;
3158 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
3159 rela.r_addend = (h->root.u.def.value
3160 + sec->output_section->vma
3161 + sec->output_offset);
3162 }
252b5132
RH
3163 else
3164 {
252b5132 3165 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
6b3ac709 3166 rela.r_addend = 0;
252b5132
RH
3167 }
3168
6b3ac709
JJ
3169 bfd_put_32 (output_bfd, (bfd_vma) 0,
3170 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
b34976b6
AM
3171 loc = srela->contents;
3172 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3173 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132
RH
3174 }
3175
f5385ebf 3176 if (h->needs_copy)
252b5132
RH
3177 {
3178 asection *s;
3179 Elf_Internal_Rela rela;
b34976b6 3180 bfd_byte *loc;
252b5132
RH
3181
3182 /* This symbols needs a copy reloc. Set it up. */
3183
3184 BFD_ASSERT (h->dynindx != -1);
3185
3186 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3187 ".rela.bss");
3188 BFD_ASSERT (s != NULL);
3189
3190 rela.r_offset = (h->root.u.def.value
3191 + h->root.u.def.section->output_section->vma
3192 + h->root.u.def.section->output_offset);
3193 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
3194 rela.r_addend = 0;
b34976b6
AM
3195 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3196 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132
RH
3197 }
3198
3199 /* Mark some specially defined symbols as absolute. */
3200 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3201 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3202 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3203 sym->st_shndx = SHN_ABS;
3204
b34976b6 3205 return TRUE;
252b5132
RH
3206}
3207
3208/* Finish up the dynamic sections. */
3209
b34976b6 3210static bfd_boolean
252b5132
RH
3211elf32_sparc_finish_dynamic_sections (output_bfd, info)
3212 bfd *output_bfd;
3213 struct bfd_link_info *info;
3214{
3215 bfd *dynobj;
3216 asection *sdyn;
b9734f35 3217 struct elf32_sparc_link_hash_table *htab;
26e41594 3218
b9734f35
JJ
3219 htab = elf32_sparc_hash_table (info);
3220 dynobj = htab->elf.dynobj;
252b5132
RH
3221
3222 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3223
3224 if (elf_hash_table (info)->dynamic_sections_created)
3225 {
3226 asection *splt;
3227 Elf32_External_Dyn *dyncon, *dynconend;
3228
3229 splt = bfd_get_section_by_name (dynobj, ".plt");
3230 BFD_ASSERT (splt != NULL && sdyn != NULL);
3231
3232 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 3233 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
252b5132
RH
3234 for (; dyncon < dynconend; dyncon++)
3235 {
3236 Elf_Internal_Dyn dyn;
3237 const char *name;
b34976b6 3238 bfd_boolean size;
252b5132
RH
3239
3240 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3241
3242 switch (dyn.d_tag)
3243 {
b34976b6
AM
3244 case DT_PLTGOT: name = ".plt"; size = FALSE; break;
3245 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
3246 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
3247 default: name = NULL; size = FALSE; break;
252b5132
RH
3248 }
3249
3250 if (name != NULL)
3251 {
3252 asection *s;
3253
3254 s = bfd_get_section_by_name (output_bfd, name);
3255 if (s == NULL)
3256 dyn.d_un.d_val = 0;
3257 else
3258 {
3259 if (! size)
3260 dyn.d_un.d_ptr = s->vma;
3261 else
eea6121a 3262 dyn.d_un.d_val = s->size;
252b5132
RH
3263 }
3264 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3265 }
3266 }
3267
3268 /* Clear the first four entries in the procedure linkage table,
3269 and put a nop in the last four bytes. */
eea6121a 3270 if (splt->size > 0)
252b5132
RH
3271 {
3272 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
dc810e39 3273 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
eea6121a 3274 splt->contents + splt->size - 4);
252b5132
RH
3275 }
3276
a8a6d7ce 3277 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
252b5132
RH
3278 }
3279
3280 /* Set the first entry in the global offset table to the address of
3281 the dynamic section. */
eea6121a 3282 if (htab->sgot && htab->sgot->size > 0)
252b5132
RH
3283 {
3284 if (sdyn == NULL)
b9734f35 3285 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents);
252b5132
RH
3286 else
3287 bfd_put_32 (output_bfd,
3288 sdyn->output_section->vma + sdyn->output_offset,
b9734f35 3289 htab->sgot->contents);
252b5132
RH
3290 }
3291
b9734f35
JJ
3292 if (htab->sgot)
3293 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
252b5132 3294
b34976b6 3295 return TRUE;
252b5132
RH
3296}
3297\f
3298/* Functions for dealing with the e_flags field.
3299
3300 We don't define set_private_flags or copy_private_bfd_data because
3301 the only currently defined values are based on the bfd mach number,
3302 so we use the latter instead and defer setting e_flags until the
3303 file is written out. */
3304
3305/* Merge backend specific data from an object file to the output
3306 object file when linking. */
3307
b34976b6 3308static bfd_boolean
252b5132
RH
3309elf32_sparc_merge_private_bfd_data (ibfd, obfd)
3310 bfd *ibfd;
3311 bfd *obfd;
3312{
b34976b6 3313 bfd_boolean error;
7946e94a 3314 unsigned long ibfd_mach;
5f771d47
ILT
3315 /* FIXME: This should not be static. */
3316 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
252b5132
RH
3317
3318 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3319 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 3320 return TRUE;
252b5132 3321
b34976b6 3322 error = FALSE;
252b5132 3323
7946e94a
JJ
3324 ibfd_mach = bfd_get_mach (ibfd);
3325 if (bfd_mach_sparc_64bit_p (ibfd_mach))
252b5132 3326 {
b34976b6 3327 error = TRUE;
252b5132 3328 (*_bfd_error_handler)
d003868e 3329 (_("%B: compiled for a 64 bit system and target is 32 bit"), ibfd);
252b5132 3330 }
37fb6db1
ILT
3331 else if ((ibfd->flags & DYNAMIC) == 0)
3332 {
7946e94a
JJ
3333 if (bfd_get_mach (obfd) < ibfd_mach)
3334 bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
37fb6db1 3335 }
252b5132
RH
3336
3337 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
3338 != previous_ibfd_e_flags)
5f771d47 3339 && previous_ibfd_e_flags != (unsigned long) -1)
252b5132
RH
3340 {
3341 (*_bfd_error_handler)
d003868e 3342 (_("%B: linking little endian files with big endian files"), ibfd);
b34976b6 3343 error = TRUE;
252b5132
RH
3344 }
3345 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
3346
3347 if (error)
3348 {
3349 bfd_set_error (bfd_error_bad_value);
b34976b6 3350 return FALSE;
252b5132
RH
3351 }
3352
b34976b6 3353 return TRUE;
252b5132
RH
3354}
3355\f
3356/* Set the right machine number. */
3357
b34976b6 3358static bfd_boolean
252b5132
RH
3359elf32_sparc_object_p (abfd)
3360 bfd *abfd;
3361{
3362 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
3363 {
19f7b010
JJ
3364 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
3365 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3366 bfd_mach_sparc_v8plusb);
3367 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
252b5132
RH
3368 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3369 bfd_mach_sparc_v8plusa);
3370 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
3371 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3372 bfd_mach_sparc_v8plus);
3373 else
b34976b6 3374 return FALSE;
252b5132
RH
3375 }
3376 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
3377 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
20278fa3 3378 bfd_mach_sparc_sparclite_le);
252b5132
RH
3379 else
3380 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
3381}
3382
3383/* The final processing done just before writing out the object file.
3384 We need to set the e_machine field appropriately. */
3385
3386static void
3387elf32_sparc_final_write_processing (abfd, linker)
3388 bfd *abfd;
b34976b6 3389 bfd_boolean linker ATTRIBUTE_UNUSED;
252b5132
RH
3390{
3391 switch (bfd_get_mach (abfd))
3392 {
3393 case bfd_mach_sparc :
fca870e4
AM
3394 case bfd_mach_sparc_sparclet :
3395 case bfd_mach_sparc_sparclite :
252b5132
RH
3396 break; /* nothing to do */
3397 case bfd_mach_sparc_v8plus :
3398 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3399 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3400 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
3401 break;
3402 case bfd_mach_sparc_v8plusa :
3403 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3404 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3405 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
3406 break;
19f7b010
JJ
3407 case bfd_mach_sparc_v8plusb :
3408 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3409 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3410 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
3411 | EF_SPARC_SUN_US3;
3412 break;
252b5132 3413 case bfd_mach_sparc_sparclite_le :
252b5132
RH
3414 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
3415 break;
3416 default :
3417 abort ();
3418 break;
3419 }
3420}
db6751f2
JJ
3421
3422static enum elf_reloc_type_class
f51e552e
AM
3423elf32_sparc_reloc_type_class (rela)
3424 const Elf_Internal_Rela *rela;
db6751f2 3425{
f51e552e 3426 switch ((int) ELF32_R_TYPE (rela->r_info))
db6751f2
JJ
3427 {
3428 case R_SPARC_RELATIVE:
3429 return reloc_class_relative;
3430 case R_SPARC_JMP_SLOT:
3431 return reloc_class_plt;
3432 case R_SPARC_COPY:
3433 return reloc_class_copy;
3434 default:
3435 return reloc_class_normal;
3436 }
3437}
4c45e5c9
JJ
3438
3439/* Return address for Ith PLT stub in section PLT, for relocation REL
3440 or (bfd_vma) -1 if it should not be included. */
3441
3442static bfd_vma
3443elf32_sparc_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
3444 const asection *plt ATTRIBUTE_UNUSED,
3445 const arelent *rel)
3446{
3447 return rel->address;
3448}
252b5132
RH
3449\f
3450#define TARGET_BIG_SYM bfd_elf32_sparc_vec
3451#define TARGET_BIG_NAME "elf32-sparc"
3452#define ELF_ARCH bfd_arch_sparc
3453#define ELF_MACHINE_CODE EM_SPARC
3454#define ELF_MACHINE_ALT1 EM_SPARC32PLUS
3455#define ELF_MAXPAGESIZE 0x10000
3456
3457#define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
b9734f35
JJ
3458#define bfd_elf32_bfd_link_hash_table_create \
3459 elf32_sparc_link_hash_table_create
f7775d95 3460#define bfd_elf32_bfd_relax_section elf32_sparc_relax_section
71e5db1c 3461#define bfd_elf32_new_section_hook elf32_sparc_new_section_hook
252b5132 3462#define elf_info_to_howto elf32_sparc_info_to_howto
b9734f35
JJ
3463#define elf_backend_copy_indirect_symbol \
3464 elf32_sparc_copy_indirect_symbol
252b5132 3465#define elf_backend_create_dynamic_sections \
b9734f35 3466 elf32_sparc_create_dynamic_sections
252b5132
RH
3467#define elf_backend_check_relocs elf32_sparc_check_relocs
3468#define elf_backend_adjust_dynamic_symbol \
3469 elf32_sparc_adjust_dynamic_symbol
151e5294 3470#define elf_backend_omit_section_dynsym elf32_sparc_omit_section_dynsym
252b5132
RH
3471#define elf_backend_size_dynamic_sections \
3472 elf32_sparc_size_dynamic_sections
3473#define elf_backend_relocate_section elf32_sparc_relocate_section
3474#define elf_backend_finish_dynamic_symbol \
3475 elf32_sparc_finish_dynamic_symbol
3476#define elf_backend_finish_dynamic_sections \
3477 elf32_sparc_finish_dynamic_sections
3478#define bfd_elf32_bfd_merge_private_bfd_data \
3479 elf32_sparc_merge_private_bfd_data
b9734f35 3480#define bfd_elf32_mkobject elf32_sparc_mkobject
252b5132
RH
3481#define elf_backend_object_p elf32_sparc_object_p
3482#define elf_backend_final_write_processing \
3483 elf32_sparc_final_write_processing
26e41594 3484#define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
252b5132 3485#define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
0647ed71 3486#define elf_backend_grok_psinfo elf32_sparc_grok_psinfo
db6751f2 3487#define elf_backend_reloc_type_class elf32_sparc_reloc_type_class
4c45e5c9 3488#define elf_backend_plt_sym_val elf32_sparc_plt_sym_val
252b5132
RH
3489
3490#define elf_backend_can_gc_sections 1
b9734f35 3491#define elf_backend_can_refcount 1
252b5132
RH
3492#define elf_backend_want_got_plt 0
3493#define elf_backend_plt_readonly 0
3494#define elf_backend_want_plt_sym 1
3495#define elf_backend_got_header_size 4
f0fe0e16 3496#define elf_backend_rela_normal 1
252b5132
RH
3497
3498#include "elf32-target.h"
This page took 0.457171 seconds and 4 git commands to generate.