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