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