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