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