2004-08-26 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / bfd / elf32-sparc.c
blob5b8053341922b81af8bf6038da631840971410aa
1 /* SPARC-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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"
27 #include "opcode/sparc.h"
29 static reloc_howto_type *elf32_sparc_reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void elf32_sparc_info_to_howto
32 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33 static bfd_boolean elf32_sparc_check_relocs
34 PARAMS ((bfd *, struct bfd_link_info *, asection *,
35 const Elf_Internal_Rela *));
36 static bfd_boolean elf32_sparc_adjust_dynamic_symbol
37 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
38 static bfd_boolean allocate_dynrelocs
39 PARAMS ((struct elf_link_hash_entry *, PTR));
40 static bfd_boolean readonly_dynrelocs
41 PARAMS ((struct elf_link_hash_entry *, PTR));
42 static bfd_boolean elf32_sparc_size_dynamic_sections
43 PARAMS ((bfd *, struct bfd_link_info *));
44 static bfd_boolean elf32_sparc_new_section_hook
45 PARAMS ((bfd *, asection *));
46 static bfd_boolean elf32_sparc_relax_section
47 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
48 static bfd_vma dtpoff_base
49 PARAMS ((struct bfd_link_info *));
50 static bfd_vma tpoff
51 PARAMS ((struct bfd_link_info *, bfd_vma));
52 static bfd_boolean elf32_sparc_relocate_section
53 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55 static bfd_boolean elf32_sparc_finish_dynamic_symbol
56 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
57 Elf_Internal_Sym *));
58 static bfd_boolean elf32_sparc_finish_dynamic_sections
59 PARAMS ((bfd *, struct bfd_link_info *));
60 static bfd_boolean elf32_sparc_merge_private_bfd_data
61 PARAMS ((bfd *, bfd *));
62 static struct bfd_hash_entry *link_hash_newfunc
63 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
64 static struct bfd_link_hash_table *elf32_sparc_link_hash_table_create
65 PARAMS ((bfd *));
66 static bfd_boolean create_got_section
67 PARAMS ((bfd *, struct bfd_link_info *));
68 static bfd_boolean elf32_sparc_create_dynamic_sections
69 PARAMS ((bfd *, struct bfd_link_info *));
70 static void elf32_sparc_copy_indirect_symbol
71 PARAMS ((const struct elf_backend_data *, struct elf_link_hash_entry *,
72 struct elf_link_hash_entry *));
73 static int elf32_sparc_tls_transition
74 PARAMS ((struct bfd_link_info *, bfd *, int, int));
76 static bfd_boolean elf32_sparc_mkobject
77 PARAMS ((bfd *));
78 static bfd_boolean elf32_sparc_object_p
79 PARAMS ((bfd *));
80 static void elf32_sparc_final_write_processing
81 PARAMS ((bfd *, bfd_boolean));
82 static enum elf_reloc_type_class elf32_sparc_reloc_type_class
83 PARAMS ((const Elf_Internal_Rela *));
84 static asection * elf32_sparc_gc_mark_hook
85 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
86 struct elf_link_hash_entry *, Elf_Internal_Sym *));
87 static bfd_boolean elf32_sparc_gc_sweep_hook
88 PARAMS ((bfd *, struct bfd_link_info *, asection *,
89 const Elf_Internal_Rela *));
91 /* The relocation "howto" table. */
93 static bfd_reloc_status_type sparc_elf_notsupported_reloc
94 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
95 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
96 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
97 static bfd_reloc_status_type sparc_elf_hix22_reloc
98 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
99 static bfd_reloc_status_type sparc_elf_lox10_reloc
100 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
102 reloc_howto_type _bfd_sparc_elf_howto_table[] =
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),
136 /* These are for sparc64 in a 64 bit environment.
137 Values need to be here because the table is indexed by reloc number. */
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),
146 /* End sparc64 in 64 bit environment values.
147 The following are for sparc64 in a 32 bit environment. */
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),
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)
189 static reloc_howto_type elf32_sparc_vtinherit_howto =
190 HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,FALSE,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", FALSE,0, 0, FALSE);
191 static reloc_howto_type elf32_sparc_vtentry_howto =
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);
193 static 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);
196 struct elf_reloc_map {
197 bfd_reloc_code_real_type bfd_reloc_val;
198 unsigned char elf_reloc_val;
201 static const struct elf_reloc_map sparc_reloc_map[] =
203 { BFD_RELOC_NONE, R_SPARC_NONE, },
204 { BFD_RELOC_16, R_SPARC_16, },
205 { BFD_RELOC_16_PCREL, R_SPARC_DISP16 },
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 },
214 { BFD_RELOC_SPARC_PLT32, R_SPARC_PLT32 },
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 },
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 },
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 },
270 { BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT },
271 { BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY },
272 { BFD_RELOC_SPARC_REV32, R_SPARC_REV32 }
275 static reloc_howto_type *
276 elf32_sparc_reloc_type_lookup (abfd, code)
277 bfd *abfd ATTRIBUTE_UNUSED;
278 bfd_reloc_code_real_type code;
280 unsigned int i;
282 switch (code)
284 case BFD_RELOC_VTABLE_INHERIT:
285 return &elf32_sparc_vtinherit_howto;
287 case BFD_RELOC_VTABLE_ENTRY:
288 return &elf32_sparc_vtentry_howto;
290 case BFD_RELOC_SPARC_REV32:
291 return &elf32_sparc_rev32_howto;
293 default:
294 for (i = 0;
295 i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map);
296 i++)
298 if (sparc_reloc_map[i].bfd_reloc_val == code)
299 return (_bfd_sparc_elf_howto_table
300 + (int) sparc_reloc_map[i].elf_reloc_val);
303 bfd_set_error (bfd_error_bad_value);
304 return NULL;
307 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
308 and elf64-sparc.c has its own copy. */
310 static void
311 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
312 bfd *abfd ATTRIBUTE_UNUSED;
313 arelent *cache_ptr;
314 Elf_Internal_Rela *dst;
316 switch (ELF32_R_TYPE(dst->r_info))
318 case R_SPARC_GNU_VTINHERIT:
319 cache_ptr->howto = &elf32_sparc_vtinherit_howto;
320 break;
322 case R_SPARC_GNU_VTENTRY:
323 cache_ptr->howto = &elf32_sparc_vtentry_howto;
324 break;
326 case R_SPARC_REV32:
327 cache_ptr->howto = &elf32_sparc_rev32_howto;
328 break;
330 default:
331 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
332 cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
336 /* For unsupported relocs. */
338 static bfd_reloc_status_type
339 sparc_elf_notsupported_reloc (abfd,
340 reloc_entry,
341 symbol,
342 data,
343 input_section,
344 output_bfd,
345 error_message)
346 bfd *abfd ATTRIBUTE_UNUSED;
347 arelent *reloc_entry ATTRIBUTE_UNUSED;
348 asymbol *symbol ATTRIBUTE_UNUSED;
349 PTR data ATTRIBUTE_UNUSED;
350 asection *input_section ATTRIBUTE_UNUSED;
351 bfd *output_bfd ATTRIBUTE_UNUSED;
352 char **error_message ATTRIBUTE_UNUSED;
354 return bfd_reloc_notsupported;
357 /* Handle the WDISP16 reloc. */
359 static bfd_reloc_status_type
360 sparc_elf_wdisp16_reloc (abfd,
361 reloc_entry,
362 symbol,
363 data,
364 input_section,
365 output_bfd,
366 error_message)
367 bfd *abfd;
368 arelent *reloc_entry;
369 asymbol *symbol;
370 PTR data;
371 asection *input_section;
372 bfd *output_bfd;
373 char **error_message ATTRIBUTE_UNUSED;
375 bfd_vma relocation;
376 bfd_vma x;
378 if (output_bfd != (bfd *) NULL
379 && (symbol->flags & BSF_SECTION_SYM) == 0
380 && (! reloc_entry->howto->partial_inplace
381 || reloc_entry->addend == 0))
383 reloc_entry->address += input_section->output_offset;
384 return bfd_reloc_ok;
387 if (output_bfd != NULL)
388 return bfd_reloc_continue;
390 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
391 return bfd_reloc_outofrange;
393 relocation = (symbol->value
394 + symbol->section->output_section->vma
395 + symbol->section->output_offset);
396 relocation += reloc_entry->addend;
397 relocation -= (input_section->output_section->vma
398 + input_section->output_offset);
399 relocation -= reloc_entry->address;
401 x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
402 x |= ((((relocation >> 2) & 0xc000) << 6)
403 | ((relocation >> 2) & 0x3fff));
404 bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
406 if ((bfd_signed_vma) relocation < - 0x40000
407 || (bfd_signed_vma) relocation > 0x3ffff)
408 return bfd_reloc_overflow;
409 else
410 return bfd_reloc_ok;
413 /* Handle the HIX22 reloc. */
415 static bfd_reloc_status_type
416 sparc_elf_hix22_reloc (abfd,
417 reloc_entry,
418 symbol,
419 data,
420 input_section,
421 output_bfd,
422 error_message)
423 bfd *abfd;
424 arelent *reloc_entry;
425 asymbol *symbol;
426 PTR data;
427 asection *input_section;
428 bfd *output_bfd;
429 char **error_message ATTRIBUTE_UNUSED;
431 bfd_vma relocation;
432 bfd_vma insn;
434 if (output_bfd != (bfd *) NULL
435 && (symbol->flags & BSF_SECTION_SYM) == 0)
437 reloc_entry->address += input_section->output_offset;
438 return bfd_reloc_ok;
441 if (output_bfd != NULL)
442 return bfd_reloc_continue;
444 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
445 return bfd_reloc_outofrange;
447 relocation = (symbol->value
448 + symbol->section->output_section->vma
449 + symbol->section->output_offset);
450 relocation += reloc_entry->addend;
451 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
453 insn = (insn &~ (bfd_vma) 0x3fffff) | (((~relocation) >> 10) & 0x3fffff);
454 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
456 return bfd_reloc_ok;
459 /* Handle the LOX10 reloc. */
461 static bfd_reloc_status_type
462 sparc_elf_lox10_reloc (abfd,
463 reloc_entry,
464 symbol,
465 data,
466 input_section,
467 output_bfd,
468 error_message)
469 bfd *abfd;
470 arelent *reloc_entry;
471 asymbol *symbol;
472 PTR data;
473 asection *input_section;
474 bfd *output_bfd;
475 char **error_message ATTRIBUTE_UNUSED;
477 bfd_vma relocation;
478 bfd_vma insn;
480 if (output_bfd != (bfd *) NULL
481 && (symbol->flags & BSF_SECTION_SYM) == 0)
483 reloc_entry->address += input_section->output_offset;
484 return bfd_reloc_ok;
487 if (output_bfd != NULL)
488 return bfd_reloc_continue;
490 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
491 return bfd_reloc_outofrange;
493 relocation = (symbol->value
494 + symbol->section->output_section->vma
495 + symbol->section->output_offset);
496 relocation += reloc_entry->addend;
497 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
499 insn = (insn &~ (bfd_vma) 0x1fff) | 0x1c00 | (relocation & 0x3ff);
500 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
502 return bfd_reloc_ok;
505 /* Support for core dump NOTE sections. */
507 static bfd_boolean
508 elf32_sparc_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
510 switch (note->descsz)
512 default:
513 return FALSE;
515 case 260: /* Solaris prpsinfo_t. */
516 elf_tdata (abfd)->core_program
517 = _bfd_elfcore_strndup (abfd, note->descdata + 84, 16);
518 elf_tdata (abfd)->core_command
519 = _bfd_elfcore_strndup (abfd, note->descdata + 100, 80);
520 break;
522 case 336: /* Solaris psinfo_t. */
523 elf_tdata (abfd)->core_program
524 = _bfd_elfcore_strndup (abfd, note->descdata + 88, 16);
525 elf_tdata (abfd)->core_command
526 = _bfd_elfcore_strndup (abfd, note->descdata + 104, 80);
527 break;
530 return TRUE;
534 /* Functions for the SPARC ELF linker. */
536 /* The name of the dynamic interpreter. This is put in the .interp
537 section. */
539 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
541 /* The nop opcode we use. */
543 #define SPARC_NOP 0x01000000
545 /* The size in bytes of an entry in the procedure linkage table. */
547 #define PLT_ENTRY_SIZE 12
549 /* The first four entries in a procedure linkage table are reserved,
550 and the initial contents are unimportant (we zero them out).
551 Subsequent entries look like this. See the SVR4 ABI SPARC
552 supplement to see how this works. */
554 /* sethi %hi(.-.plt0),%g1. We fill in the address later. */
555 #define PLT_ENTRY_WORD0 0x03000000
556 /* b,a .plt0. We fill in the offset later. */
557 #define PLT_ENTRY_WORD1 0x30800000
558 /* nop. */
559 #define PLT_ENTRY_WORD2 SPARC_NOP
561 /* The SPARC linker needs to keep track of the number of relocs that it
562 decides to copy as dynamic relocs in check_relocs for each symbol.
563 This is so that it can later discard them if they are found to be
564 unnecessary. We store the information in a field extending the
565 regular ELF linker hash table. */
567 struct elf32_sparc_dyn_relocs
569 struct elf32_sparc_dyn_relocs *next;
571 /* The input section of the reloc. */
572 asection *sec;
574 /* Total number of relocs copied for the input section. */
575 bfd_size_type count;
577 /* Number of pc-relative relocs copied for the input section. */
578 bfd_size_type pc_count;
581 /* SPARC ELF linker hash entry. */
583 struct elf32_sparc_link_hash_entry
585 struct elf_link_hash_entry elf;
587 /* Track dynamic relocs copied for this symbol. */
588 struct elf32_sparc_dyn_relocs *dyn_relocs;
590 #define GOT_UNKNOWN 0
591 #define GOT_NORMAL 1
592 #define GOT_TLS_GD 2
593 #define GOT_TLS_IE 3
594 unsigned char tls_type;
597 #define elf32_sparc_hash_entry(ent) ((struct elf32_sparc_link_hash_entry *)(ent))
599 struct elf32_sparc_obj_tdata
601 struct elf_obj_tdata root;
603 /* tls_type for each local got entry. */
604 char *local_got_tls_type;
606 /* TRUE if TLS GD relocs has been seen for this object. */
607 bfd_boolean has_tlsgd;
610 #define elf32_sparc_tdata(abfd) \
611 ((struct elf32_sparc_obj_tdata *) (abfd)->tdata.any)
613 #define elf32_sparc_local_got_tls_type(abfd) \
614 (elf32_sparc_tdata (abfd)->local_got_tls_type)
616 static bfd_boolean
617 elf32_sparc_mkobject (abfd)
618 bfd *abfd;
620 bfd_size_type amt = sizeof (struct elf32_sparc_obj_tdata);
621 abfd->tdata.any = bfd_zalloc (abfd, amt);
622 if (abfd->tdata.any == NULL)
623 return FALSE;
624 return TRUE;
627 /* SPARC ELF linker hash table. */
629 struct elf32_sparc_link_hash_table
631 struct elf_link_hash_table elf;
633 /* Short-cuts to get to dynamic linker sections. */
634 asection *sgot;
635 asection *srelgot;
636 asection *splt;
637 asection *srelplt;
638 asection *sdynbss;
639 asection *srelbss;
641 union {
642 bfd_signed_vma refcount;
643 bfd_vma offset;
644 } tls_ldm_got;
646 /* Small local sym to section mapping cache. */
647 struct sym_sec_cache sym_sec;
650 /* Get the SPARC ELF linker hash table from a link_info structure. */
652 #define elf32_sparc_hash_table(p) \
653 ((struct elf32_sparc_link_hash_table *) ((p)->hash))
655 /* Create an entry in an i386 ELF linker hash table. */
657 static struct bfd_hash_entry *
658 link_hash_newfunc (entry, table, string)
659 struct bfd_hash_entry *entry;
660 struct bfd_hash_table *table;
661 const char *string;
663 /* Allocate the structure if it has not already been allocated by a
664 subclass. */
665 if (entry == NULL)
667 entry = bfd_hash_allocate (table,
668 sizeof (struct elf32_sparc_link_hash_entry));
669 if (entry == NULL)
670 return entry;
673 /* Call the allocation method of the superclass. */
674 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
675 if (entry != NULL)
677 struct elf32_sparc_link_hash_entry *eh;
679 eh = (struct elf32_sparc_link_hash_entry *) entry;
680 eh->dyn_relocs = NULL;
681 eh->tls_type = GOT_UNKNOWN;
684 return entry;
687 /* Create a SPARC ELF linker hash table. */
689 static struct bfd_link_hash_table *
690 elf32_sparc_link_hash_table_create (abfd)
691 bfd *abfd;
693 struct elf32_sparc_link_hash_table *ret;
694 bfd_size_type amt = sizeof (struct elf32_sparc_link_hash_table);
696 ret = (struct elf32_sparc_link_hash_table *) bfd_malloc (amt);
697 if (ret == NULL)
698 return NULL;
700 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
702 free (ret);
703 return NULL;
706 ret->sgot = NULL;
707 ret->srelgot = NULL;
708 ret->splt = NULL;
709 ret->srelplt = NULL;
710 ret->sdynbss = NULL;
711 ret->srelbss = NULL;
712 ret->tls_ldm_got.refcount = 0;
713 ret->sym_sec.abfd = NULL;
715 return &ret->elf.root;
718 /* Create .got and .rela.got sections in DYNOBJ, and set up
719 shortcuts to them in our hash table. */
721 static bfd_boolean
722 create_got_section (dynobj, info)
723 bfd *dynobj;
724 struct bfd_link_info *info;
726 struct elf32_sparc_link_hash_table *htab;
728 if (! _bfd_elf_create_got_section (dynobj, info))
729 return FALSE;
731 htab = elf32_sparc_hash_table (info);
732 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
733 if (!htab->sgot)
734 abort ();
736 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
737 if (htab->srelgot == NULL
738 || ! bfd_set_section_flags (dynobj, htab->srelgot,
739 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
740 | SEC_IN_MEMORY | SEC_LINKER_CREATED
741 | SEC_READONLY))
742 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
743 return FALSE;
744 return TRUE;
747 /* Create .plt, .rela.plt, .got, .rela.got, .dynbss, and
748 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
749 hash table. */
751 static bfd_boolean
752 elf32_sparc_create_dynamic_sections (dynobj, info)
753 bfd *dynobj;
754 struct bfd_link_info *info;
756 struct elf32_sparc_link_hash_table *htab;
758 htab = elf32_sparc_hash_table (info);
759 if (!htab->sgot && !create_got_section (dynobj, info))
760 return FALSE;
762 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
763 return FALSE;
765 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
766 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
767 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
768 if (!info->shared)
769 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
771 if (!htab->splt || !htab->srelplt || !htab->sdynbss
772 || (!info->shared && !htab->srelbss))
773 abort ();
775 return TRUE;
778 /* Copy the extra info we tack onto an elf_link_hash_entry. */
780 static void
781 elf32_sparc_copy_indirect_symbol (bed, dir, ind)
782 const struct elf_backend_data *bed;
783 struct elf_link_hash_entry *dir, *ind;
785 struct elf32_sparc_link_hash_entry *edir, *eind;
787 edir = (struct elf32_sparc_link_hash_entry *) dir;
788 eind = (struct elf32_sparc_link_hash_entry *) ind;
790 if (eind->dyn_relocs != NULL)
792 if (edir->dyn_relocs != NULL)
794 struct elf32_sparc_dyn_relocs **pp;
795 struct elf32_sparc_dyn_relocs *p;
797 if (ind->root.type == bfd_link_hash_indirect)
798 abort ();
800 /* Add reloc counts against the weak sym to the strong sym
801 list. Merge any entries against the same section. */
802 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
804 struct elf32_sparc_dyn_relocs *q;
806 for (q = edir->dyn_relocs; q != NULL; q = q->next)
807 if (q->sec == p->sec)
809 q->pc_count += p->pc_count;
810 q->count += p->count;
811 *pp = p->next;
812 break;
814 if (q == NULL)
815 pp = &p->next;
817 *pp = edir->dyn_relocs;
820 edir->dyn_relocs = eind->dyn_relocs;
821 eind->dyn_relocs = NULL;
824 if (ind->root.type == bfd_link_hash_indirect
825 && dir->got.refcount <= 0)
827 edir->tls_type = eind->tls_type;
828 eind->tls_type = GOT_UNKNOWN;
830 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
833 static int
834 elf32_sparc_tls_transition (info, abfd, r_type, is_local)
835 struct bfd_link_info *info;
836 bfd *abfd;
837 int r_type;
838 int is_local;
840 if (r_type == R_SPARC_TLS_GD_HI22
841 && ! elf32_sparc_tdata (abfd)->has_tlsgd)
842 r_type = R_SPARC_REV32;
844 if (info->shared)
845 return r_type;
847 switch (r_type)
849 case R_SPARC_TLS_GD_HI22:
850 if (is_local)
851 return R_SPARC_TLS_LE_HIX22;
852 return R_SPARC_TLS_IE_HI22;
853 case R_SPARC_TLS_GD_LO10:
854 if (is_local)
855 return R_SPARC_TLS_LE_LOX10;
856 return R_SPARC_TLS_IE_LO10;
857 case R_SPARC_TLS_IE_HI22:
858 if (is_local)
859 return R_SPARC_TLS_LE_HIX22;
860 return r_type;
861 case R_SPARC_TLS_IE_LO10:
862 if (is_local)
863 return R_SPARC_TLS_LE_LOX10;
864 return r_type;
865 case R_SPARC_TLS_LDM_HI22:
866 return R_SPARC_TLS_LE_HIX22;
867 case R_SPARC_TLS_LDM_LO10:
868 return R_SPARC_TLS_LE_LOX10;
871 return r_type;
874 /* Look through the relocs for a section during the first phase, and
875 allocate space in the global offset table or procedure linkage
876 table. */
878 static bfd_boolean
879 elf32_sparc_check_relocs (abfd, info, sec, relocs)
880 bfd *abfd;
881 struct bfd_link_info *info;
882 asection *sec;
883 const Elf_Internal_Rela *relocs;
885 struct elf32_sparc_link_hash_table *htab;
886 Elf_Internal_Shdr *symtab_hdr;
887 struct elf_link_hash_entry **sym_hashes;
888 bfd_vma *local_got_offsets;
889 const Elf_Internal_Rela *rel;
890 const Elf_Internal_Rela *rel_end;
891 asection *sreloc;
892 bfd_boolean checked_tlsgd = FALSE;
894 if (info->relocatable)
895 return TRUE;
897 htab = elf32_sparc_hash_table (info);
898 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
899 sym_hashes = elf_sym_hashes (abfd);
900 local_got_offsets = elf_local_got_offsets (abfd);
902 sreloc = NULL;
904 rel_end = relocs + sec->reloc_count;
905 for (rel = relocs; rel < rel_end; rel++)
907 unsigned int r_type;
908 unsigned long r_symndx;
909 struct elf_link_hash_entry *h;
911 r_symndx = ELF32_R_SYM (rel->r_info);
912 r_type = ELF32_R_TYPE (rel->r_info);
914 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
916 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
917 abfd, r_symndx);
918 return FALSE;
921 if (r_symndx < symtab_hdr->sh_info)
922 h = NULL;
923 else
924 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
926 /* Compatibility with old R_SPARC_REV32 reloc conflicting
927 with R_SPARC_TLS_GD_HI22. */
928 if (! checked_tlsgd)
929 switch (r_type)
931 case R_SPARC_TLS_GD_HI22:
933 const Elf_Internal_Rela *relt;
935 for (relt = rel + 1; relt < rel_end; relt++)
936 if (ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_LO10
937 || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_ADD
938 || ELF32_R_TYPE (relt->r_info) == R_SPARC_TLS_GD_CALL)
939 break;
940 checked_tlsgd = TRUE;
941 elf32_sparc_tdata (abfd)->has_tlsgd = relt < rel_end;
943 break;
944 case R_SPARC_TLS_GD_LO10:
945 case R_SPARC_TLS_GD_ADD:
946 case R_SPARC_TLS_GD_CALL:
947 checked_tlsgd = TRUE;
948 elf32_sparc_tdata (abfd)->has_tlsgd = TRUE;
949 break;
952 r_type = elf32_sparc_tls_transition (info, abfd, r_type, h == NULL);
953 switch (r_type)
955 case R_SPARC_TLS_LDM_HI22:
956 case R_SPARC_TLS_LDM_LO10:
957 htab->tls_ldm_got.refcount += 1;
958 break;
960 case R_SPARC_TLS_LE_HIX22:
961 case R_SPARC_TLS_LE_LOX10:
962 if (info->shared)
963 goto r_sparc_plt32;
964 break;
966 case R_SPARC_TLS_IE_HI22:
967 case R_SPARC_TLS_IE_LO10:
968 if (info->shared)
969 info->flags |= DF_STATIC_TLS;
970 /* Fall through */
972 case R_SPARC_GOT10:
973 case R_SPARC_GOT13:
974 case R_SPARC_GOT22:
975 case R_SPARC_TLS_GD_HI22:
976 case R_SPARC_TLS_GD_LO10:
977 /* This symbol requires a global offset table entry. */
979 int tls_type, old_tls_type;
981 switch (r_type)
983 default:
984 case R_SPARC_GOT10:
985 case R_SPARC_GOT13:
986 case R_SPARC_GOT22:
987 tls_type = GOT_NORMAL;
988 break;
989 case R_SPARC_TLS_GD_HI22:
990 case R_SPARC_TLS_GD_LO10:
991 tls_type = GOT_TLS_GD;
992 break;
993 case R_SPARC_TLS_IE_HI22:
994 case R_SPARC_TLS_IE_LO10:
995 tls_type = GOT_TLS_IE;
996 break;
999 if (h != NULL)
1001 h->got.refcount += 1;
1002 old_tls_type = elf32_sparc_hash_entry(h)->tls_type;
1004 else
1006 bfd_signed_vma *local_got_refcounts;
1008 /* This is a global offset table entry for a local symbol. */
1009 local_got_refcounts = elf_local_got_refcounts (abfd);
1010 if (local_got_refcounts == NULL)
1012 bfd_size_type size;
1014 size = symtab_hdr->sh_info;
1015 size *= (sizeof (bfd_signed_vma) + sizeof(char));
1016 local_got_refcounts = ((bfd_signed_vma *)
1017 bfd_zalloc (abfd, size));
1018 if (local_got_refcounts == NULL)
1019 return FALSE;
1020 elf_local_got_refcounts (abfd) = local_got_refcounts;
1021 elf32_sparc_local_got_tls_type (abfd)
1022 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1024 local_got_refcounts[r_symndx] += 1;
1025 old_tls_type = elf32_sparc_local_got_tls_type (abfd) [r_symndx];
1028 /* If a TLS symbol is accessed using IE at least once,
1029 there is no point to use dynamic model for it. */
1030 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1031 && (old_tls_type != GOT_TLS_GD
1032 || tls_type != GOT_TLS_IE))
1034 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
1035 tls_type = old_tls_type;
1036 else
1038 (*_bfd_error_handler)
1039 (_("%B: `%s' accessed both as normal and thread local symbol"),
1040 abfd, h ? h->root.root.string : "<local>");
1041 return FALSE;
1045 if (old_tls_type != tls_type)
1047 if (h != NULL)
1048 elf32_sparc_hash_entry (h)->tls_type = tls_type;
1049 else
1050 elf32_sparc_local_got_tls_type (abfd) [r_symndx] = tls_type;
1054 if (htab->sgot == NULL)
1056 if (htab->elf.dynobj == NULL)
1057 htab->elf.dynobj = abfd;
1058 if (!create_got_section (htab->elf.dynobj, info))
1059 return FALSE;
1061 break;
1063 case R_SPARC_TLS_GD_CALL:
1064 case R_SPARC_TLS_LDM_CALL:
1065 if (info->shared)
1067 /* These are basically R_SPARC_TLS_WPLT30 relocs against
1068 __tls_get_addr. */
1069 struct bfd_link_hash_entry *bh = NULL;
1070 if (! _bfd_generic_link_add_one_symbol (info, abfd,
1071 "__tls_get_addr", 0,
1072 bfd_und_section_ptr, 0,
1073 NULL, FALSE, FALSE,
1074 &bh))
1075 return FALSE;
1076 h = (struct elf_link_hash_entry *) bh;
1078 else
1079 break;
1080 /* Fall through */
1082 case R_SPARC_PLT32:
1083 case R_SPARC_WPLT30:
1084 /* This symbol requires a procedure linkage table entry. We
1085 actually build the entry in adjust_dynamic_symbol,
1086 because this might be a case of linking PIC code without
1087 linking in any dynamic objects, in which case we don't
1088 need to generate a procedure linkage table after all. */
1090 if (h == NULL)
1092 /* The Solaris native assembler will generate a WPLT30
1093 reloc for a local symbol if you assemble a call from
1094 one section to another when using -K pic. We treat
1095 it as WDISP30. */
1096 if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
1097 goto r_sparc_plt32;
1098 break;
1101 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1103 if (ELF32_R_TYPE (rel->r_info) == R_SPARC_PLT32)
1104 goto r_sparc_plt32;
1105 h->plt.refcount += 1;
1106 break;
1108 case R_SPARC_PC10:
1109 case R_SPARC_PC22:
1110 if (h != NULL)
1111 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1113 if (h != NULL
1114 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1115 break;
1116 /* Fall through. */
1118 case R_SPARC_DISP8:
1119 case R_SPARC_DISP16:
1120 case R_SPARC_DISP32:
1121 case R_SPARC_WDISP30:
1122 case R_SPARC_WDISP22:
1123 case R_SPARC_WDISP19:
1124 case R_SPARC_WDISP16:
1125 case R_SPARC_8:
1126 case R_SPARC_16:
1127 case R_SPARC_32:
1128 case R_SPARC_HI22:
1129 case R_SPARC_22:
1130 case R_SPARC_13:
1131 case R_SPARC_LO10:
1132 case R_SPARC_UA16:
1133 case R_SPARC_UA32:
1134 if (h != NULL)
1135 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1137 r_sparc_plt32:
1138 if (h != NULL && !info->shared)
1140 /* We may need a .plt entry if the function this reloc
1141 refers to is in a shared lib. */
1142 h->plt.refcount += 1;
1145 /* If we are creating a shared library, and this is a reloc
1146 against a global symbol, or a non PC relative reloc
1147 against a local symbol, then we need to copy the reloc
1148 into the shared library. However, if we are linking with
1149 -Bsymbolic, we do not need to copy a reloc against a
1150 global symbol which is defined in an object we are
1151 including in the link (i.e., DEF_REGULAR is set). At
1152 this point we have not seen all the input files, so it is
1153 possible that DEF_REGULAR is not set now but will be set
1154 later (it is never cleared). In case of a weak definition,
1155 DEF_REGULAR may be cleared later by a strong definition in
1156 a shared library. We account for that possibility below by
1157 storing information in the relocs_copied field of the hash
1158 table entry. A similar situation occurs when creating
1159 shared libraries and symbol visibility changes render the
1160 symbol local.
1162 If on the other hand, we are creating an executable, we
1163 may need to keep relocations for symbols satisfied by a
1164 dynamic library if we manage to avoid copy relocs for the
1165 symbol. */
1166 if ((info->shared
1167 && (sec->flags & SEC_ALLOC) != 0
1168 && (! _bfd_sparc_elf_howto_table[r_type].pc_relative
1169 || (h != NULL
1170 && (! info->symbolic
1171 || h->root.type == bfd_link_hash_defweak
1172 || (h->elf_link_hash_flags
1173 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1174 || (!info->shared
1175 && (sec->flags & SEC_ALLOC) != 0
1176 && h != NULL
1177 && (h->root.type == bfd_link_hash_defweak
1178 || (h->elf_link_hash_flags
1179 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1181 struct elf32_sparc_dyn_relocs *p;
1182 struct elf32_sparc_dyn_relocs **head;
1184 /* When creating a shared object, we must copy these
1185 relocs into the output file. We create a reloc
1186 section in dynobj and make room for the reloc. */
1187 if (sreloc == NULL)
1189 const char *name;
1190 bfd *dynobj;
1192 name = (bfd_elf_string_from_elf_section
1193 (abfd,
1194 elf_elfheader (abfd)->e_shstrndx,
1195 elf_section_data (sec)->rel_hdr.sh_name));
1196 if (name == NULL)
1197 return FALSE;
1199 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1200 && strcmp (bfd_get_section_name (abfd, sec),
1201 name + 5) == 0);
1203 if (htab->elf.dynobj == NULL)
1204 htab->elf.dynobj = abfd;
1205 dynobj = htab->elf.dynobj;
1207 sreloc = bfd_get_section_by_name (dynobj, name);
1208 if (sreloc == NULL)
1210 flagword flags;
1212 sreloc = bfd_make_section (dynobj, name);
1213 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1214 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1215 if ((sec->flags & SEC_ALLOC) != 0)
1216 flags |= SEC_ALLOC | SEC_LOAD;
1217 if (sreloc == NULL
1218 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1219 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1220 return FALSE;
1222 elf_section_data (sec)->sreloc = sreloc;
1225 /* If this is a global symbol, we count the number of
1226 relocations we need for this symbol. */
1227 if (h != NULL)
1228 head = &((struct elf32_sparc_link_hash_entry *) h)->dyn_relocs;
1229 else
1231 /* Track dynamic relocs needed for local syms too.
1232 We really need local syms available to do this
1233 easily. Oh well. */
1235 asection *s;
1236 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1237 sec, r_symndx);
1238 if (s == NULL)
1239 return FALSE;
1241 head = ((struct elf32_sparc_dyn_relocs **)
1242 &elf_section_data (s)->local_dynrel);
1245 p = *head;
1246 if (p == NULL || p->sec != sec)
1248 bfd_size_type amt = sizeof *p;
1249 p = ((struct elf32_sparc_dyn_relocs *)
1250 bfd_alloc (htab->elf.dynobj, amt));
1251 if (p == NULL)
1252 return FALSE;
1253 p->next = *head;
1254 *head = p;
1255 p->sec = sec;
1256 p->count = 0;
1257 p->pc_count = 0;
1260 p->count += 1;
1261 if (_bfd_sparc_elf_howto_table[r_type].pc_relative)
1262 p->pc_count += 1;
1265 break;
1267 case R_SPARC_GNU_VTINHERIT:
1268 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1269 return FALSE;
1270 break;
1272 case R_SPARC_GNU_VTENTRY:
1273 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1274 return FALSE;
1275 break;
1277 default:
1278 break;
1282 return TRUE;
1285 static asection *
1286 elf32_sparc_gc_mark_hook (sec, info, rel, h, sym)
1287 asection *sec;
1288 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1289 Elf_Internal_Rela *rel;
1290 struct elf_link_hash_entry *h;
1291 Elf_Internal_Sym *sym;
1293 if (h != NULL)
1295 switch (ELF32_R_TYPE (rel->r_info))
1297 case R_SPARC_GNU_VTINHERIT:
1298 case R_SPARC_GNU_VTENTRY:
1299 break;
1301 default:
1302 switch (h->root.type)
1304 case bfd_link_hash_defined:
1305 case bfd_link_hash_defweak:
1306 return h->root.u.def.section;
1308 case bfd_link_hash_common:
1309 return h->root.u.c.p->section;
1311 default:
1312 break;
1316 else
1317 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1319 return NULL;
1322 /* Update the got entry reference counts for the section being removed. */
1323 static bfd_boolean
1324 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
1325 bfd *abfd;
1326 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1327 asection *sec;
1328 const Elf_Internal_Rela *relocs;
1330 Elf_Internal_Shdr *symtab_hdr;
1331 struct elf_link_hash_entry **sym_hashes;
1332 bfd_signed_vma *local_got_refcounts;
1333 const Elf_Internal_Rela *rel, *relend;
1335 elf_section_data (sec)->local_dynrel = NULL;
1337 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1338 sym_hashes = elf_sym_hashes (abfd);
1339 local_got_refcounts = elf_local_got_refcounts (abfd);
1341 relend = relocs + sec->reloc_count;
1342 for (rel = relocs; rel < relend; rel++)
1344 unsigned long r_symndx;
1345 unsigned int r_type;
1346 struct elf_link_hash_entry *h = NULL;
1348 r_symndx = ELF32_R_SYM (rel->r_info);
1349 if (r_symndx >= symtab_hdr->sh_info)
1351 struct elf32_sparc_link_hash_entry *eh;
1352 struct elf32_sparc_dyn_relocs **pp;
1353 struct elf32_sparc_dyn_relocs *p;
1355 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1356 eh = (struct elf32_sparc_link_hash_entry *) h;
1357 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1358 if (p->sec == sec)
1360 /* Everything must go for SEC. */
1361 *pp = p->next;
1362 break;
1366 r_type = ELF32_R_TYPE (rel->r_info);
1367 r_type = elf32_sparc_tls_transition (info, abfd, r_type, h != NULL);
1368 switch (r_type)
1370 case R_SPARC_TLS_LDM_HI22:
1371 case R_SPARC_TLS_LDM_LO10:
1372 if (elf32_sparc_hash_table (info)->tls_ldm_got.refcount > 0)
1373 elf32_sparc_hash_table (info)->tls_ldm_got.refcount -= 1;
1374 break;
1376 case R_SPARC_TLS_GD_HI22:
1377 case R_SPARC_TLS_GD_LO10:
1378 case R_SPARC_TLS_IE_HI22:
1379 case R_SPARC_TLS_IE_LO10:
1380 case R_SPARC_GOT10:
1381 case R_SPARC_GOT13:
1382 case R_SPARC_GOT22:
1383 if (h != NULL)
1385 if (h->got.refcount > 0)
1386 h->got.refcount--;
1388 else
1390 if (local_got_refcounts[r_symndx] > 0)
1391 local_got_refcounts[r_symndx]--;
1393 break;
1395 case R_SPARC_PC10:
1396 case R_SPARC_PC22:
1397 if (h != NULL
1398 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1399 break;
1400 /* Fall through. */
1402 case R_SPARC_DISP8:
1403 case R_SPARC_DISP16:
1404 case R_SPARC_DISP32:
1405 case R_SPARC_WDISP30:
1406 case R_SPARC_WDISP22:
1407 case R_SPARC_WDISP19:
1408 case R_SPARC_WDISP16:
1409 case R_SPARC_8:
1410 case R_SPARC_16:
1411 case R_SPARC_32:
1412 case R_SPARC_HI22:
1413 case R_SPARC_22:
1414 case R_SPARC_13:
1415 case R_SPARC_LO10:
1416 case R_SPARC_UA16:
1417 case R_SPARC_UA32:
1418 case R_SPARC_PLT32:
1419 if (info->shared)
1420 break;
1421 /* Fall through. */
1423 case R_SPARC_WPLT30:
1424 if (h != NULL)
1426 if (h->plt.refcount > 0)
1427 h->plt.refcount--;
1429 break;
1431 default:
1432 break;
1436 return TRUE;
1439 /* Adjust a symbol defined by a dynamic object and referenced by a
1440 regular object. The current definition is in some section of the
1441 dynamic object, but we're not including those sections. We have to
1442 change the definition to something the rest of the link can
1443 understand. */
1445 static bfd_boolean
1446 elf32_sparc_adjust_dynamic_symbol (info, h)
1447 struct bfd_link_info *info;
1448 struct elf_link_hash_entry *h;
1450 struct elf32_sparc_link_hash_table *htab;
1451 struct elf32_sparc_link_hash_entry * eh;
1452 struct elf32_sparc_dyn_relocs *p;
1453 asection *s;
1454 unsigned int power_of_two;
1456 htab = elf32_sparc_hash_table (info);
1458 /* Make sure we know what is going on here. */
1459 BFD_ASSERT (htab->elf.dynobj != NULL
1460 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1461 || h->weakdef != NULL
1462 || ((h->elf_link_hash_flags
1463 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1464 && (h->elf_link_hash_flags
1465 & ELF_LINK_HASH_REF_REGULAR) != 0
1466 && (h->elf_link_hash_flags
1467 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1469 /* If this is a function, put it in the procedure linkage table. We
1470 will fill in the contents of the procedure linkage table later
1471 (although we could actually do it here). The STT_NOTYPE
1472 condition is a hack specifically for the Oracle libraries
1473 delivered for Solaris; for some inexplicable reason, they define
1474 some of their functions as STT_NOTYPE when they really should be
1475 STT_FUNC. */
1476 if (h->type == STT_FUNC
1477 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
1478 || (h->type == STT_NOTYPE
1479 && (h->root.type == bfd_link_hash_defined
1480 || h->root.type == bfd_link_hash_defweak)
1481 && (h->root.u.def.section->flags & SEC_CODE) != 0))
1483 if (h->plt.refcount <= 0
1484 || (! info->shared
1485 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1486 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1487 && h->root.type != bfd_link_hash_undefweak
1488 && h->root.type != bfd_link_hash_undefined))
1490 /* This case can occur if we saw a WPLT30 reloc in an input
1491 file, but the symbol was never referred to by a dynamic
1492 object, or if all references were garbage collected. In
1493 such a case, we don't actually need to build a procedure
1494 linkage table, and we can just do a WDISP30 reloc instead. */
1495 h->plt.offset = (bfd_vma) -1;
1496 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1499 return TRUE;
1501 else
1502 h->plt.offset = (bfd_vma) -1;
1504 /* If this is a weak symbol, and there is a real definition, the
1505 processor independent code will have arranged for us to see the
1506 real definition first, and we can just use the same value. */
1507 if (h->weakdef != NULL)
1509 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1510 || h->weakdef->root.type == bfd_link_hash_defweak);
1511 h->root.u.def.section = h->weakdef->root.u.def.section;
1512 h->root.u.def.value = h->weakdef->root.u.def.value;
1513 return TRUE;
1516 /* This is a reference to a symbol defined by a dynamic object which
1517 is not a function. */
1519 /* If we are creating a shared library, we must presume that the
1520 only references to the symbol are via the global offset table.
1521 For such cases we need not do anything here; the relocations will
1522 be handled correctly by relocate_section. */
1523 if (info->shared)
1524 return TRUE;
1526 /* If there are no references to this symbol that do not use the
1527 GOT, we don't need to generate a copy reloc. */
1528 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1529 return TRUE;
1531 eh = (struct elf32_sparc_link_hash_entry *) h;
1532 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1534 s = p->sec->output_section;
1535 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1536 break;
1539 /* If we didn't find any dynamic relocs in read-only sections, then
1540 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1541 if (p == NULL)
1543 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1544 return TRUE;
1547 /* We must allocate the symbol in our .dynbss section, which will
1548 become part of the .bss section of the executable. There will be
1549 an entry for this symbol in the .dynsym section. The dynamic
1550 object will contain position independent code, so all references
1551 from the dynamic object to this symbol will go through the global
1552 offset table. The dynamic linker will use the .dynsym entry to
1553 determine the address it must put in the global offset table, so
1554 both the dynamic object and the regular object will refer to the
1555 same memory location for the variable. */
1557 /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
1558 to copy the initial value out of the dynamic object and into the
1559 runtime process image. We need to remember the offset into the
1560 .rel.bss section we are going to use. */
1561 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1563 htab->srelbss->size += sizeof (Elf32_External_Rela);
1564 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1567 /* We need to figure out the alignment required for this symbol. I
1568 have no idea how ELF linkers handle this. */
1569 power_of_two = bfd_log2 (h->size);
1570 if (power_of_two > 3)
1571 power_of_two = 3;
1573 /* Apply the required alignment. */
1574 s = htab->sdynbss;
1575 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1576 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1578 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1579 return FALSE;
1582 /* Define the symbol as being at this point in the section. */
1583 h->root.u.def.section = s;
1584 h->root.u.def.value = s->size;
1586 /* Increment the section size to make room for the symbol. */
1587 s->size += h->size;
1589 return TRUE;
1592 /* Allocate space in .plt, .got and associated reloc sections for
1593 dynamic relocs. */
1595 static bfd_boolean
1596 allocate_dynrelocs (h, inf)
1597 struct elf_link_hash_entry *h;
1598 PTR inf;
1600 struct bfd_link_info *info;
1601 struct elf32_sparc_link_hash_table *htab;
1602 struct elf32_sparc_link_hash_entry *eh;
1603 struct elf32_sparc_dyn_relocs *p;
1605 if (h->root.type == bfd_link_hash_indirect)
1606 return TRUE;
1608 if (h->root.type == bfd_link_hash_warning)
1609 /* When warning symbols are created, they **replace** the "real"
1610 entry in the hash table, thus we never get to see the real
1611 symbol in a hash traversal. So look at it now. */
1612 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1614 info = (struct bfd_link_info *) inf;
1615 htab = elf32_sparc_hash_table (info);
1617 if (htab->elf.dynamic_sections_created
1618 && h->plt.refcount > 0)
1620 /* Make sure this symbol is output as a dynamic symbol.
1621 Undefined weak syms won't yet be marked as dynamic. */
1622 if (h->dynindx == -1
1623 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1625 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1626 return FALSE;
1629 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1631 asection *s = htab->splt;
1633 /* The first four entries in .plt are reserved. */
1634 if (s->size == 0)
1635 s->size = 4 * PLT_ENTRY_SIZE;
1637 /* The procedure linkage table has a maximum size. */
1638 if (s->size >= 0x400000)
1640 bfd_set_error (bfd_error_bad_value);
1641 return FALSE;
1644 h->plt.offset = s->size;
1646 /* If this symbol is not defined in a regular file, and we are
1647 not generating a shared library, then set the symbol to this
1648 location in the .plt. This is required to make function
1649 pointers compare as equal between the normal executable and
1650 the shared library. */
1651 if (! info->shared
1652 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1654 h->root.u.def.section = s;
1655 h->root.u.def.value = h->plt.offset;
1658 /* Make room for this entry. */
1659 s->size += PLT_ENTRY_SIZE;
1661 /* We also need to make an entry in the .rela.plt section. */
1662 htab->srelplt->size += sizeof (Elf32_External_Rela);
1664 else
1666 h->plt.offset = (bfd_vma) -1;
1667 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1670 else
1672 h->plt.offset = (bfd_vma) -1;
1673 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1676 /* If R_SPARC_TLS_IE_{HI22,LO10} symbol is now local to the binary,
1677 make it a R_SPARC_TLS_LE_{HI22,LO10} requiring no TLS entry. */
1678 if (h->got.refcount > 0
1679 && !info->shared
1680 && h->dynindx == -1
1681 && elf32_sparc_hash_entry(h)->tls_type == GOT_TLS_IE)
1682 h->got.offset = (bfd_vma) -1;
1683 else if (h->got.refcount > 0)
1685 asection *s;
1686 bfd_boolean dyn;
1687 int tls_type = elf32_sparc_hash_entry(h)->tls_type;
1689 /* Make sure this symbol is output as a dynamic symbol.
1690 Undefined weak syms won't yet be marked as dynamic. */
1691 if (h->dynindx == -1
1692 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1694 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1695 return FALSE;
1698 s = htab->sgot;
1699 h->got.offset = s->size;
1700 s->size += 4;
1701 /* R_SPARC_TLS_GD_HI{22,LO10} needs 2 consecutive GOT slots. */
1702 if (tls_type == GOT_TLS_GD)
1703 s->size += 4;
1704 dyn = htab->elf.dynamic_sections_created;
1705 /* R_SPARC_TLS_IE_{HI22,LO10} needs one dynamic relocation,
1706 R_SPARC_TLS_GD_{HI22,LO10} needs one if local symbol and two if
1707 global. */
1708 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1709 || tls_type == GOT_TLS_IE)
1710 htab->srelgot->size += sizeof (Elf32_External_Rela);
1711 else if (tls_type == GOT_TLS_GD)
1712 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
1713 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
1714 htab->srelgot->size += sizeof (Elf32_External_Rela);
1716 else
1717 h->got.offset = (bfd_vma) -1;
1719 eh = (struct elf32_sparc_link_hash_entry *) h;
1720 if (eh->dyn_relocs == NULL)
1721 return TRUE;
1723 /* In the shared -Bsymbolic case, discard space allocated for
1724 dynamic pc-relative relocs against symbols which turn out to be
1725 defined in regular objects. For the normal shared case, discard
1726 space for pc-relative relocs that have become local due to symbol
1727 visibility changes. */
1729 if (info->shared)
1731 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1732 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1733 || info->symbolic))
1735 struct elf32_sparc_dyn_relocs **pp;
1737 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1739 p->count -= p->pc_count;
1740 p->pc_count = 0;
1741 if (p->count == 0)
1742 *pp = p->next;
1743 else
1744 pp = &p->next;
1748 else
1750 /* For the non-shared case, discard space for relocs against
1751 symbols which turn out to need copy relocs or are not
1752 dynamic. */
1754 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1755 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1756 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1757 || (htab->elf.dynamic_sections_created
1758 && (h->root.type == bfd_link_hash_undefweak
1759 || h->root.type == bfd_link_hash_undefined))))
1761 /* Make sure this symbol is output as a dynamic symbol.
1762 Undefined weak syms won't yet be marked as dynamic. */
1763 if (h->dynindx == -1
1764 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1766 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1767 return FALSE;
1770 /* If that succeeded, we know we'll be keeping all the
1771 relocs. */
1772 if (h->dynindx != -1)
1773 goto keep;
1776 eh->dyn_relocs = NULL;
1778 keep: ;
1781 /* Finally, allocate space. */
1782 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1784 asection *sreloc = elf_section_data (p->sec)->sreloc;
1785 sreloc->size += p->count * sizeof (Elf32_External_Rela);
1788 return TRUE;
1791 /* Find any dynamic relocs that apply to read-only sections. */
1793 static bfd_boolean
1794 readonly_dynrelocs (h, inf)
1795 struct elf_link_hash_entry *h;
1796 PTR inf;
1798 struct elf32_sparc_link_hash_entry *eh;
1799 struct elf32_sparc_dyn_relocs *p;
1801 if (h->root.type == bfd_link_hash_warning)
1802 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1804 eh = (struct elf32_sparc_link_hash_entry *) h;
1805 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1807 asection *s = p->sec->output_section;
1809 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1811 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1813 info->flags |= DF_TEXTREL;
1815 /* Not an error, just cut short the traversal. */
1816 return FALSE;
1819 return TRUE;
1822 /* Set the sizes of the dynamic sections. */
1824 static bfd_boolean
1825 elf32_sparc_size_dynamic_sections (output_bfd, info)
1826 bfd *output_bfd ATTRIBUTE_UNUSED;
1827 struct bfd_link_info *info;
1829 struct elf32_sparc_link_hash_table *htab;
1830 bfd *dynobj;
1831 asection *s;
1832 bfd *ibfd;
1834 htab = elf32_sparc_hash_table (info);
1835 dynobj = htab->elf.dynobj;
1836 BFD_ASSERT (dynobj != NULL);
1838 if (elf_hash_table (info)->dynamic_sections_created)
1840 /* Set the contents of the .interp section to the interpreter. */
1841 if (info->executable)
1843 s = bfd_get_section_by_name (dynobj, ".interp");
1844 BFD_ASSERT (s != NULL);
1845 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1846 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1850 /* Set up .got offsets for local syms, and space for local dynamic
1851 relocs. */
1852 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1854 bfd_signed_vma *local_got;
1855 bfd_signed_vma *end_local_got;
1856 char *local_tls_type;
1857 bfd_size_type locsymcount;
1858 Elf_Internal_Shdr *symtab_hdr;
1859 asection *srel;
1861 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1862 continue;
1864 for (s = ibfd->sections; s != NULL; s = s->next)
1866 struct elf32_sparc_dyn_relocs *p;
1868 for (p = *((struct elf32_sparc_dyn_relocs **)
1869 &elf_section_data (s)->local_dynrel);
1870 p != NULL;
1871 p = p->next)
1873 if (!bfd_is_abs_section (p->sec)
1874 && bfd_is_abs_section (p->sec->output_section))
1876 /* Input section has been discarded, either because
1877 it is a copy of a linkonce section or due to
1878 linker script /DISCARD/, so we'll be discarding
1879 the relocs too. */
1881 else if (p->count != 0)
1883 srel = elf_section_data (p->sec)->sreloc;
1884 srel->size += p->count * sizeof (Elf32_External_Rela);
1885 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1886 info->flags |= DF_TEXTREL;
1891 local_got = elf_local_got_refcounts (ibfd);
1892 if (!local_got)
1893 continue;
1895 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1896 locsymcount = symtab_hdr->sh_info;
1897 end_local_got = local_got + locsymcount;
1898 local_tls_type = elf32_sparc_local_got_tls_type (ibfd);
1899 s = htab->sgot;
1900 srel = htab->srelgot;
1901 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1903 if (*local_got > 0)
1905 *local_got = s->size;
1906 s->size += 4;
1907 if (*local_tls_type == GOT_TLS_GD)
1908 s->size += 4;
1909 if (info->shared
1910 || *local_tls_type == GOT_TLS_GD
1911 || *local_tls_type == GOT_TLS_IE)
1912 srel->size += sizeof (Elf32_External_Rela);
1914 else
1915 *local_got = (bfd_vma) -1;
1919 if (htab->tls_ldm_got.refcount > 0)
1921 /* Allocate 2 got entries and 1 dynamic reloc for
1922 R_SPARC_TLS_LDM_{HI22,LO10} relocs. */
1923 htab->tls_ldm_got.offset = htab->sgot->size;
1924 htab->sgot->size += 8;
1925 htab->srelgot->size += sizeof (Elf32_External_Rela);
1927 else
1928 htab->tls_ldm_got.offset = -1;
1930 /* Allocate global sym .plt and .got entries, and space for global
1931 sym dynamic relocs. */
1932 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1934 if (elf_hash_table (info)->dynamic_sections_created)
1936 /* Make space for the trailing nop in .plt. */
1937 if (htab->splt->size > 0)
1938 htab->splt->size += 4;
1940 /* If the .got section is more than 0x1000 bytes, we add
1941 0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
1942 bit relocations have a greater chance of working. */
1943 if (htab->sgot->size >= 0x1000
1944 && elf_hash_table (info)->hgot->root.u.def.value == 0)
1945 elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
1948 /* The check_relocs and adjust_dynamic_symbol entry points have
1949 determined the sizes of the various dynamic sections. Allocate
1950 memory for them. */
1951 for (s = dynobj->sections; s != NULL; s = s->next)
1953 const char *name;
1954 bfd_boolean strip = FALSE;
1956 if ((s->flags & SEC_LINKER_CREATED) == 0)
1957 continue;
1959 /* It's OK to base decisions on the section name, because none
1960 of the dynobj section names depend upon the input files. */
1961 name = bfd_get_section_name (dynobj, s);
1963 if (strncmp (name, ".rela", 5) == 0)
1965 if (s->size == 0)
1967 /* If we don't need this section, strip it from the
1968 output file. This is to handle .rela.bss and
1969 .rel.plt. We must create it in
1970 create_dynamic_sections, because it must be created
1971 before the linker maps input sections to output
1972 sections. The linker does that before
1973 adjust_dynamic_symbol is called, and it is that
1974 function which decides whether anything needs to go
1975 into these sections. */
1976 strip = TRUE;
1978 else
1980 /* We use the reloc_count field as a counter if we need
1981 to copy relocs into the output file. */
1982 s->reloc_count = 0;
1985 else if (s != htab->splt && s != htab->sgot)
1987 /* It's not one of our sections, so don't allocate space. */
1988 continue;
1991 if (strip)
1993 _bfd_strip_section_from_output (info, s);
1994 continue;
1997 /* Allocate memory for the section contents. */
1998 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1999 Unused entries should be reclaimed before the section's contents
2000 are written out, but at the moment this does not happen. Thus in
2001 order to prevent writing out garbage, we initialise the section's
2002 contents to zero. */
2003 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2004 if (s->contents == NULL && s->size != 0)
2005 return FALSE;
2008 if (elf_hash_table (info)->dynamic_sections_created)
2010 /* Add some entries to the .dynamic section. We fill in the
2011 values later, in elf32_sparc_finish_dynamic_sections, but we
2012 must add the entries now so that we get the correct size for
2013 the .dynamic section. The DT_DEBUG entry is filled in by the
2014 dynamic linker and used by the debugger. */
2015 #define add_dynamic_entry(TAG, VAL) \
2016 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2018 if (info->executable)
2020 if (!add_dynamic_entry (DT_DEBUG, 0))
2021 return FALSE;
2024 if (htab->srelplt->size != 0)
2026 if (!add_dynamic_entry (DT_PLTGOT, 0)
2027 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2028 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2029 || !add_dynamic_entry (DT_JMPREL, 0))
2030 return FALSE;
2033 if (!add_dynamic_entry (DT_RELA, 0)
2034 || !add_dynamic_entry (DT_RELASZ, 0)
2035 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2036 return FALSE;
2038 /* If any dynamic relocs apply to a read-only section,
2039 then we need a DT_TEXTREL entry. */
2040 if ((info->flags & DF_TEXTREL) == 0)
2041 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2042 (PTR) info);
2044 if (info->flags & DF_TEXTREL)
2046 if (!add_dynamic_entry (DT_TEXTREL, 0))
2047 return FALSE;
2050 #undef add_dynamic_entry
2052 return TRUE;
2055 struct elf32_sparc_section_data
2057 struct bfd_elf_section_data elf;
2058 unsigned int do_relax;
2061 #define sec_do_relax(sec) \
2062 ((struct elf32_sparc_section_data *) elf_section_data (sec))->do_relax
2064 static bfd_boolean
2065 elf32_sparc_new_section_hook (abfd, sec)
2066 bfd *abfd;
2067 asection *sec;
2069 struct elf32_sparc_section_data *sdata;
2070 bfd_size_type amt = sizeof (*sdata);
2072 sdata = (struct elf32_sparc_section_data *) bfd_zalloc (abfd, amt);
2073 if (sdata == NULL)
2074 return FALSE;
2075 sec->used_by_bfd = (PTR) sdata;
2077 return _bfd_elf_new_section_hook (abfd, sec);
2080 static bfd_boolean
2081 elf32_sparc_relax_section (abfd, section, link_info, again)
2082 bfd *abfd ATTRIBUTE_UNUSED;
2083 asection *section ATTRIBUTE_UNUSED;
2084 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
2085 bfd_boolean *again;
2087 *again = FALSE;
2088 sec_do_relax (section) = 1;
2089 return TRUE;
2092 /* Return the base VMA address which should be subtracted from real addresses
2093 when resolving @dtpoff relocation.
2094 This is PT_TLS segment p_vaddr. */
2096 static bfd_vma
2097 dtpoff_base (info)
2098 struct bfd_link_info *info;
2100 /* If tls_sec is NULL, we should have signalled an error already. */
2101 if (elf_hash_table (info)->tls_sec == NULL)
2102 return 0;
2103 return elf_hash_table (info)->tls_sec->vma;
2106 /* Return the relocation value for @tpoff relocation
2107 if STT_TLS virtual address is ADDRESS. */
2109 static bfd_vma
2110 tpoff (info, address)
2111 struct bfd_link_info *info;
2112 bfd_vma address;
2114 struct elf_link_hash_table *htab = elf_hash_table (info);
2116 /* If tls_sec is NULL, we should have signalled an error already. */
2117 if (htab->tls_sec == NULL)
2118 return 0;
2119 return address - htab->tls_size - htab->tls_sec->vma;
2122 /* Relocate a SPARC ELF section. */
2124 static bfd_boolean
2125 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
2126 contents, relocs, local_syms, local_sections)
2127 bfd *output_bfd;
2128 struct bfd_link_info *info;
2129 bfd *input_bfd;
2130 asection *input_section;
2131 bfd_byte *contents;
2132 Elf_Internal_Rela *relocs;
2133 Elf_Internal_Sym *local_syms;
2134 asection **local_sections;
2136 struct elf32_sparc_link_hash_table *htab;
2137 Elf_Internal_Shdr *symtab_hdr;
2138 struct elf_link_hash_entry **sym_hashes;
2139 bfd_vma *local_got_offsets;
2140 bfd_vma got_base;
2141 asection *sreloc;
2142 Elf_Internal_Rela *rel;
2143 Elf_Internal_Rela *relend;
2145 if (info->relocatable)
2146 return TRUE;
2148 htab = elf32_sparc_hash_table (info);
2149 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2150 sym_hashes = elf_sym_hashes (input_bfd);
2151 local_got_offsets = elf_local_got_offsets (input_bfd);
2153 if (elf_hash_table (info)->hgot == NULL)
2154 got_base = 0;
2155 else
2156 got_base = elf_hash_table (info)->hgot->root.u.def.value;
2158 sreloc = elf_section_data (input_section)->sreloc;
2160 rel = relocs;
2161 relend = relocs + input_section->reloc_count;
2162 for (; rel < relend; rel++)
2164 int r_type, tls_type;
2165 reloc_howto_type *howto;
2166 unsigned long r_symndx;
2167 struct elf_link_hash_entry *h;
2168 Elf_Internal_Sym *sym;
2169 asection *sec;
2170 bfd_vma relocation, off;
2171 bfd_reloc_status_type r;
2172 bfd_boolean is_plt = FALSE;
2173 bfd_boolean unresolved_reloc;
2175 r_type = ELF32_R_TYPE (rel->r_info);
2177 if (r_type == R_SPARC_GNU_VTINHERIT
2178 || r_type == R_SPARC_GNU_VTENTRY)
2179 continue;
2181 if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
2183 bfd_set_error (bfd_error_bad_value);
2184 return FALSE;
2186 howto = _bfd_sparc_elf_howto_table + r_type;
2188 /* This is a final link. */
2189 r_symndx = ELF32_R_SYM (rel->r_info);
2190 h = NULL;
2191 sym = NULL;
2192 sec = NULL;
2193 unresolved_reloc = FALSE;
2194 if (r_symndx < symtab_hdr->sh_info)
2196 sym = local_syms + r_symndx;
2197 sec = local_sections[r_symndx];
2198 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2200 else
2202 bfd_boolean warned ATTRIBUTE_UNUSED;
2204 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2205 r_symndx, symtab_hdr, sym_hashes,
2206 h, sec, relocation,
2207 unresolved_reloc, warned);
2210 switch (r_type)
2212 case R_SPARC_GOT10:
2213 case R_SPARC_GOT13:
2214 case R_SPARC_GOT22:
2215 /* Relocation is to the entry for this symbol in the global
2216 offset table. */
2217 if (htab->sgot == NULL)
2218 abort ();
2220 if (h != NULL)
2222 bfd_boolean dyn;
2224 off = h->got.offset;
2225 BFD_ASSERT (off != (bfd_vma) -1);
2226 dyn = elf_hash_table (info)->dynamic_sections_created;
2228 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2229 || (info->shared
2230 && (info->symbolic
2231 || h->dynindx == -1
2232 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2233 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2235 /* This is actually a static link, or it is a
2236 -Bsymbolic link and the symbol is defined
2237 locally, or the symbol was forced to be local
2238 because of a version file. We must initialize
2239 this entry in the global offset table. Since the
2240 offset must always be a multiple of 4, we use the
2241 least significant bit to record whether we have
2242 initialized it already.
2244 When doing a dynamic link, we create a .rela.got
2245 relocation entry to initialize the value. This
2246 is done in the finish_dynamic_symbol routine. */
2247 if ((off & 1) != 0)
2248 off &= ~1;
2249 else
2251 bfd_put_32 (output_bfd, relocation,
2252 htab->sgot->contents + off);
2253 h->got.offset |= 1;
2256 else
2257 unresolved_reloc = FALSE;
2259 else
2261 BFD_ASSERT (local_got_offsets != NULL
2262 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2264 off = local_got_offsets[r_symndx];
2266 /* The offset must always be a multiple of 4. We use
2267 the least significant bit to record whether we have
2268 already processed this entry. */
2269 if ((off & 1) != 0)
2270 off &= ~1;
2271 else
2274 if (info->shared)
2276 asection *s;
2277 Elf_Internal_Rela outrel;
2278 bfd_byte *loc;
2280 /* We need to generate a R_SPARC_RELATIVE reloc
2281 for the dynamic linker. */
2282 s = htab->srelgot;
2283 BFD_ASSERT (s != NULL);
2285 outrel.r_offset = (htab->sgot->output_section->vma
2286 + htab->sgot->output_offset
2287 + off);
2288 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
2289 outrel.r_addend = relocation;
2290 relocation = 0;
2291 loc = s->contents;
2292 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
2293 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2296 bfd_put_32 (output_bfd, relocation,
2297 htab->sgot->contents + off);
2298 local_got_offsets[r_symndx] |= 1;
2301 relocation = htab->sgot->output_offset + off - got_base;
2302 break;
2304 case R_SPARC_PLT32:
2305 if (h == NULL || h->plt.offset == (bfd_vma) -1)
2307 r_type = R_SPARC_32;
2308 goto r_sparc_plt32;
2310 /* Fall through. */
2311 case R_SPARC_WPLT30:
2312 r_sparc_wplt30:
2313 /* Relocation is to the entry for this symbol in the
2314 procedure linkage table. */
2316 /* The Solaris native assembler will generate a WPLT30 reloc
2317 for a local symbol if you assemble a call from one
2318 section to another when using -K pic. We treat it as
2319 WDISP30. */
2320 if (h == NULL)
2321 break;
2323 if (h->plt.offset == (bfd_vma) -1 || htab->splt == NULL)
2325 /* We didn't make a PLT entry for this symbol. This
2326 happens when statically linking PIC code, or when
2327 using -Bsymbolic. */
2328 break;
2331 relocation = (htab->splt->output_section->vma
2332 + htab->splt->output_offset
2333 + h->plt.offset);
2334 unresolved_reloc = FALSE;
2335 if (r_type == R_SPARC_PLT32)
2337 r_type = R_SPARC_32;
2338 is_plt = TRUE;
2339 goto r_sparc_plt32;
2341 break;
2343 case R_SPARC_PC10:
2344 case R_SPARC_PC22:
2345 if (h != NULL
2346 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2347 break;
2348 /* Fall through. */
2349 case R_SPARC_DISP8:
2350 case R_SPARC_DISP16:
2351 case R_SPARC_DISP32:
2352 case R_SPARC_WDISP30:
2353 case R_SPARC_WDISP22:
2354 case R_SPARC_WDISP19:
2355 case R_SPARC_WDISP16:
2356 case R_SPARC_8:
2357 case R_SPARC_16:
2358 case R_SPARC_32:
2359 case R_SPARC_HI22:
2360 case R_SPARC_22:
2361 case R_SPARC_13:
2362 case R_SPARC_LO10:
2363 case R_SPARC_UA16:
2364 case R_SPARC_UA32:
2365 r_sparc_plt32:
2366 /* r_symndx will be zero only for relocs against symbols
2367 from removed linkonce sections, or sections discarded by
2368 a linker script. */
2369 if (r_symndx == 0
2370 || (input_section->flags & SEC_ALLOC) == 0)
2371 break;
2373 if ((info->shared
2374 && (h == NULL
2375 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2376 || h->root.type != bfd_link_hash_undefweak)
2377 && (! howto->pc_relative
2378 || (h != NULL
2379 && h->dynindx != -1
2380 && (! info->symbolic
2381 || (h->elf_link_hash_flags
2382 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2383 || (!info->shared
2384 && h != NULL
2385 && h->dynindx != -1
2386 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2387 && (((h->elf_link_hash_flags
2388 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2389 && (h->elf_link_hash_flags
2390 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2391 || h->root.type == bfd_link_hash_undefweak
2392 || h->root.type == bfd_link_hash_undefined)))
2394 Elf_Internal_Rela outrel;
2395 bfd_byte *loc;
2396 bfd_boolean skip, relocate = FALSE;
2398 /* When generating a shared object, these relocations
2399 are copied into the output file to be resolved at run
2400 time. */
2402 BFD_ASSERT (sreloc != NULL);
2404 skip = FALSE;
2406 outrel.r_offset =
2407 _bfd_elf_section_offset (output_bfd, info, input_section,
2408 rel->r_offset);
2409 if (outrel.r_offset == (bfd_vma) -1)
2410 skip = TRUE;
2411 else if (outrel.r_offset == (bfd_vma) -2)
2412 skip = TRUE, relocate = TRUE;
2413 outrel.r_offset += (input_section->output_section->vma
2414 + input_section->output_offset);
2416 /* Optimize unaligned reloc usage now that we know where
2417 it finally resides. */
2418 switch (r_type)
2420 case R_SPARC_16:
2421 if (outrel.r_offset & 1)
2422 r_type = R_SPARC_UA16;
2423 break;
2424 case R_SPARC_UA16:
2425 if (!(outrel.r_offset & 1))
2426 r_type = R_SPARC_16;
2427 break;
2428 case R_SPARC_32:
2429 if (outrel.r_offset & 3)
2430 r_type = R_SPARC_UA32;
2431 break;
2432 case R_SPARC_UA32:
2433 if (!(outrel.r_offset & 3))
2434 r_type = R_SPARC_32;
2435 break;
2436 case R_SPARC_DISP8:
2437 case R_SPARC_DISP16:
2438 case R_SPARC_DISP32:
2439 /* If the symbol is not dynamic, we should not keep
2440 a dynamic relocation. But an .rela.* slot has been
2441 allocated for it, output R_SPARC_NONE.
2442 FIXME: Add code tracking needed dynamic relocs as
2443 e.g. i386 has. */
2444 if (h->dynindx == -1)
2445 skip = TRUE, relocate = TRUE;
2446 break;
2449 if (skip)
2450 memset (&outrel, 0, sizeof outrel);
2451 /* h->dynindx may be -1 if the symbol was marked to
2452 become local. */
2453 else if (h != NULL && ! is_plt
2454 && ((! info->symbolic && h->dynindx != -1)
2455 || (h->elf_link_hash_flags
2456 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2458 BFD_ASSERT (h->dynindx != -1);
2459 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2460 outrel.r_addend = rel->r_addend;
2462 else
2464 if (r_type == R_SPARC_32)
2466 outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
2467 outrel.r_addend = relocation + rel->r_addend;
2469 else
2471 long indx;
2473 if (is_plt)
2474 sec = htab->splt;
2476 if (bfd_is_abs_section (sec))
2477 indx = 0;
2478 else if (sec == NULL || sec->owner == NULL)
2480 bfd_set_error (bfd_error_bad_value);
2481 return FALSE;
2483 else
2485 asection *osec;
2487 osec = sec->output_section;
2488 indx = elf_section_data (osec)->dynindx;
2490 /* FIXME: we really should be able to link non-pic
2491 shared libraries. */
2492 if (indx == 0)
2494 BFD_FAIL ();
2495 (*_bfd_error_handler)
2496 (_("%B: probably compiled without -fPIC?"),
2497 input_bfd);
2498 bfd_set_error (bfd_error_bad_value);
2499 return FALSE;
2503 outrel.r_info = ELF32_R_INFO (indx, r_type);
2504 outrel.r_addend = relocation + rel->r_addend;
2508 loc = sreloc->contents;
2509 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
2510 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2512 /* This reloc will be computed at runtime, so there's no
2513 need to do anything now. */
2514 if (! relocate)
2515 continue;
2517 break;
2519 case R_SPARC_TLS_GD_HI22:
2520 if (! elf32_sparc_tdata (input_bfd)->has_tlsgd)
2522 /* R_SPARC_REV32 used the same reloc number as
2523 R_SPARC_TLS_GD_HI22. */
2524 r_type = R_SPARC_REV32;
2525 break;
2527 /* Fall through */
2529 case R_SPARC_TLS_GD_LO10:
2530 case R_SPARC_TLS_IE_HI22:
2531 case R_SPARC_TLS_IE_LO10:
2532 r_type = elf32_sparc_tls_transition (info, input_bfd, r_type,
2533 h == NULL);
2534 tls_type = GOT_UNKNOWN;
2535 if (h == NULL && local_got_offsets)
2536 tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2537 else if (h != NULL)
2539 tls_type = elf32_sparc_hash_entry(h)->tls_type;
2540 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2541 switch (ELF32_R_TYPE (rel->r_info))
2543 case R_SPARC_TLS_GD_HI22:
2544 case R_SPARC_TLS_IE_HI22:
2545 r_type = R_SPARC_TLS_LE_HIX22;
2546 break;
2547 default:
2548 r_type = R_SPARC_TLS_LE_LOX10;
2549 break;
2552 if (tls_type == GOT_TLS_IE)
2553 switch (r_type)
2555 case R_SPARC_TLS_GD_HI22:
2556 r_type = R_SPARC_TLS_IE_HI22;
2557 break;
2558 case R_SPARC_TLS_GD_LO10:
2559 r_type = R_SPARC_TLS_IE_LO10;
2560 break;
2563 if (r_type == R_SPARC_TLS_LE_HIX22)
2565 relocation = tpoff (info, relocation);
2566 break;
2568 if (r_type == R_SPARC_TLS_LE_LOX10)
2570 /* Change add into xor. */
2571 relocation = tpoff (info, relocation);
2572 bfd_put_32 (output_bfd, (bfd_get_32 (input_bfd,
2573 contents + rel->r_offset)
2574 | 0x80182000), contents + rel->r_offset);
2575 break;
2578 if (h != NULL)
2580 off = h->got.offset;
2581 h->got.offset |= 1;
2583 else
2585 BFD_ASSERT (local_got_offsets != NULL);
2586 off = local_got_offsets[r_symndx];
2587 local_got_offsets[r_symndx] |= 1;
2590 r_sparc_tlsldm:
2591 if (htab->sgot == NULL)
2592 abort ();
2594 if ((off & 1) != 0)
2595 off &= ~1;
2596 else
2598 Elf_Internal_Rela outrel;
2599 Elf32_External_Rela *loc;
2600 int dr_type, indx;
2602 if (htab->srelgot == NULL)
2603 abort ();
2605 bfd_put_32 (output_bfd, 0, htab->sgot->contents + off);
2606 outrel.r_offset = (htab->sgot->output_section->vma
2607 + htab->sgot->output_offset + off);
2608 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2609 if (r_type == R_SPARC_TLS_IE_HI22
2610 || r_type == R_SPARC_TLS_IE_LO10)
2611 dr_type = R_SPARC_TLS_TPOFF32;
2612 else
2613 dr_type = R_SPARC_TLS_DTPMOD32;
2614 if (dr_type == R_SPARC_TLS_TPOFF32 && indx == 0)
2615 outrel.r_addend = relocation - dtpoff_base (info);
2616 else
2617 outrel.r_addend = 0;
2618 outrel.r_info = ELF32_R_INFO (indx, dr_type);
2619 loc = (Elf32_External_Rela *) htab->srelgot->contents;
2620 loc += htab->srelgot->reloc_count++;
2621 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2622 (bfd_byte *) loc);
2624 if (r_type == R_SPARC_TLS_GD_HI22
2625 || r_type == R_SPARC_TLS_GD_LO10)
2627 if (indx == 0)
2629 BFD_ASSERT (! unresolved_reloc);
2630 bfd_put_32 (output_bfd,
2631 relocation - dtpoff_base (info),
2632 htab->sgot->contents + off + 4);
2634 else
2636 bfd_put_32 (output_bfd, 0,
2637 htab->sgot->contents + off + 4);
2638 outrel.r_info = ELF32_R_INFO (indx,
2639 R_SPARC_TLS_DTPOFF32);
2640 outrel.r_offset += 4;
2641 htab->srelgot->reloc_count++;
2642 loc++;
2643 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2644 (bfd_byte *) loc);
2647 else if (dr_type == R_SPARC_TLS_DTPMOD32)
2649 bfd_put_32 (output_bfd, 0,
2650 htab->sgot->contents + off + 4);
2654 if (off >= (bfd_vma) -2)
2655 abort ();
2657 relocation = htab->sgot->output_offset + off - got_base;
2658 unresolved_reloc = FALSE;
2659 howto = _bfd_sparc_elf_howto_table + r_type;
2660 break;
2662 case R_SPARC_TLS_LDM_HI22:
2663 case R_SPARC_TLS_LDM_LO10:
2664 if (! info->shared)
2666 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2667 continue;
2669 off = htab->tls_ldm_got.offset;
2670 htab->tls_ldm_got.offset |= 1;
2671 goto r_sparc_tlsldm;
2673 case R_SPARC_TLS_LDO_HIX22:
2674 case R_SPARC_TLS_LDO_LOX10:
2675 if (info->shared)
2676 relocation -= dtpoff_base (info);
2677 else
2678 relocation = tpoff (info, relocation);
2679 break;
2681 case R_SPARC_TLS_LE_HIX22:
2682 case R_SPARC_TLS_LE_LOX10:
2683 if (info->shared)
2685 Elf_Internal_Rela outrel;
2686 bfd_boolean skip, relocate = FALSE;
2688 BFD_ASSERT (sreloc != NULL);
2689 skip = FALSE;
2690 outrel.r_offset =
2691 _bfd_elf_section_offset (output_bfd, info, input_section,
2692 rel->r_offset);
2693 if (outrel.r_offset == (bfd_vma) -1)
2694 skip = TRUE;
2695 else if (outrel.r_offset == (bfd_vma) -2)
2696 skip = TRUE, relocate = TRUE;
2697 outrel.r_offset += (input_section->output_section->vma
2698 + input_section->output_offset);
2699 if (skip)
2700 memset (&outrel, 0, sizeof outrel);
2701 else
2703 outrel.r_info = ELF32_R_INFO (0, r_type);
2704 outrel.r_addend = relocation - dtpoff_base (info)
2705 + rel->r_addend;
2708 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
2709 (bfd_byte *) (((Elf32_External_Rela *)
2710 sreloc->contents)
2711 + sreloc->reloc_count));
2712 ++sreloc->reloc_count;
2713 continue;
2715 relocation = tpoff (info, relocation);
2716 break;
2718 case R_SPARC_TLS_LDM_CALL:
2719 if (! info->shared)
2721 /* mov %g0, %o0 */
2722 bfd_put_32 (output_bfd, 0x90100000, contents + rel->r_offset);
2723 continue;
2725 /* Fall through */
2727 case R_SPARC_TLS_GD_CALL:
2728 tls_type = GOT_UNKNOWN;
2729 if (h == NULL && local_got_offsets)
2730 tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2731 else if (h != NULL)
2732 tls_type = elf32_sparc_hash_entry(h)->tls_type;
2733 if (! info->shared
2734 || (r_type == R_SPARC_TLS_GD_CALL && tls_type == GOT_TLS_IE))
2736 bfd_vma insn;
2738 if (!info->shared && (h == NULL || h->dynindx == -1))
2740 /* GD -> LE */
2741 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2742 continue;
2745 /* GD -> IE */
2746 if (rel + 1 < relend
2747 && ELF32_R_TYPE (rel[1].r_info) == R_SPARC_TLS_GD_ADD
2748 && rel[1].r_offset == rel->r_offset + 4
2749 && ELF32_R_SYM (rel[1].r_info) == r_symndx
2750 && (((insn = bfd_get_32 (input_bfd,
2751 contents + rel[1].r_offset))
2752 >> 25) & 0x1f) == 8)
2754 /* We have
2755 call __tls_get_addr, %tgd_call(foo)
2756 add %reg1, %reg2, %o0, %tgd_add(foo)
2757 and change it into IE:
2758 ld [%reg1 + %reg2], %o0, %tie_ld(foo)
2759 add %g7, %o0, %o0, %tie_add(foo).
2760 add is 0x80000000 | (rd << 25) | (rs1 << 14) | rs2,
2761 ld is 0xc0000000 | (rd << 25) | (rs1 << 14) | rs2. */
2762 bfd_put_32 (output_bfd, insn | 0xc0000000,
2763 contents + rel->r_offset);
2764 bfd_put_32 (output_bfd, 0x9001c008,
2765 contents + rel->r_offset + 4);
2766 rel++;
2767 continue;
2770 bfd_put_32 (output_bfd, 0x9001c008, contents + rel->r_offset);
2771 continue;
2774 h = (struct elf_link_hash_entry *)
2775 bfd_link_hash_lookup (info->hash, "__tls_get_addr", FALSE,
2776 FALSE, TRUE);
2777 BFD_ASSERT (h != NULL);
2778 r_type = R_SPARC_WPLT30;
2779 howto = _bfd_sparc_elf_howto_table + r_type;
2780 goto r_sparc_wplt30;
2782 case R_SPARC_TLS_GD_ADD:
2783 tls_type = GOT_UNKNOWN;
2784 if (h == NULL && local_got_offsets)
2785 tls_type = elf32_sparc_local_got_tls_type (input_bfd) [r_symndx];
2786 else if (h != NULL)
2787 tls_type = elf32_sparc_hash_entry(h)->tls_type;
2788 if (! info->shared || tls_type == GOT_TLS_IE)
2790 /* add %reg1, %reg2, %reg3, %tgd_add(foo)
2791 changed into IE:
2792 ld [%reg1 + %reg2], %reg3, %tie_ld(foo)
2793 or LE:
2794 add %g7, %reg2, %reg3. */
2795 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2796 if ((h != NULL && h->dynindx != -1) || info->shared)
2797 relocation = insn | 0xc0000000;
2798 else
2799 relocation = (insn & ~0x7c000) | 0x1c000;
2800 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
2802 continue;
2804 case R_SPARC_TLS_LDM_ADD:
2805 if (! info->shared)
2806 bfd_put_32 (output_bfd, SPARC_NOP, contents + rel->r_offset);
2807 continue;
2809 case R_SPARC_TLS_LDO_ADD:
2810 if (! info->shared)
2812 /* Change rs1 into %g7. */
2813 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2814 insn = (insn & ~0x7c000) | 0x1c000;
2815 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
2817 continue;
2819 case R_SPARC_TLS_IE_LD:
2820 case R_SPARC_TLS_IE_LDX:
2821 if (! info->shared && (h == NULL || h->dynindx == -1))
2823 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2824 int rs2 = insn & 0x1f;
2825 int rd = (insn >> 25) & 0x1f;
2827 if (rs2 == rd)
2828 relocation = SPARC_NOP;
2829 else
2830 relocation = 0x80100000 | (insn & 0x3e00001f);
2831 bfd_put_32 (output_bfd, relocation, contents + rel->r_offset);
2833 continue;
2835 case R_SPARC_TLS_IE_ADD:
2836 /* Totally useless relocation. */
2837 continue;
2839 case R_SPARC_TLS_DTPOFF32:
2840 relocation -= dtpoff_base (info);
2841 break;
2843 default:
2844 break;
2847 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2848 because such sections are not SEC_ALLOC and thus ld.so will
2849 not process them. */
2850 if (unresolved_reloc
2851 && !((input_section->flags & SEC_DEBUGGING) != 0
2852 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2853 (*_bfd_error_handler)
2854 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2855 input_bfd,
2856 input_section,
2857 (long) rel->r_offset,
2858 h->root.root.string);
2860 r = bfd_reloc_continue;
2861 if (r_type == R_SPARC_WDISP16)
2863 bfd_vma x;
2865 relocation += rel->r_addend;
2866 relocation -= (input_section->output_section->vma
2867 + input_section->output_offset);
2868 relocation -= rel->r_offset;
2870 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2871 x |= ((((relocation >> 2) & 0xc000) << 6)
2872 | ((relocation >> 2) & 0x3fff));
2873 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2875 if ((bfd_signed_vma) relocation < - 0x40000
2876 || (bfd_signed_vma) relocation > 0x3ffff)
2877 r = bfd_reloc_overflow;
2878 else
2879 r = bfd_reloc_ok;
2881 else if (r_type == R_SPARC_REV32)
2883 bfd_vma x;
2885 relocation = relocation + rel->r_addend;
2887 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2888 x = x + relocation;
2889 bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
2890 r = bfd_reloc_ok;
2892 else if (r_type == R_SPARC_TLS_LDO_HIX22
2893 || r_type == R_SPARC_TLS_LE_HIX22)
2895 bfd_vma x;
2897 relocation += rel->r_addend;
2898 relocation = relocation ^ 0xffffffff;
2900 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2901 x = (x & ~(bfd_vma) 0x3fffff) | ((relocation >> 10) & 0x3fffff);
2902 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2903 r = bfd_reloc_ok;
2905 else if (r_type == R_SPARC_TLS_LDO_LOX10
2906 || r_type == R_SPARC_TLS_LE_LOX10)
2908 bfd_vma x;
2910 relocation += rel->r_addend;
2911 relocation = (relocation & 0x3ff) | 0x1c00;
2913 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2914 x = (x & ~(bfd_vma) 0x1fff) | relocation;
2915 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2917 r = bfd_reloc_ok;
2919 else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
2920 && sec_do_relax (input_section)
2921 && rel->r_offset + 4 < input_section->size)
2923 #define G0 0
2924 #define O7 15
2925 #define XCC (2 << 20)
2926 #define COND(x) (((x)&0xf)<<25)
2927 #define CONDA COND(0x8)
2928 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
2929 #define INSN_BA (F2(0,2) | CONDA)
2930 #define INSN_OR F3(2, 0x2, 0)
2931 #define INSN_NOP F2(0,4)
2933 bfd_vma x, y;
2935 /* If the instruction is a call with either:
2936 restore
2937 arithmetic instruction with rd == %o7
2938 where rs1 != %o7 and rs2 if it is register != %o7
2939 then we can optimize if the call destination is near
2940 by changing the call into a branch always. */
2941 x = bfd_get_32 (input_bfd, contents + rel->r_offset);
2942 y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
2943 if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
2945 if (((y & OP3(~0)) == OP3(0x3d) /* restore */
2946 || ((y & OP3(0x28)) == 0 /* arithmetic */
2947 && (y & RD(~0)) == RD(O7)))
2948 && (y & RS1(~0)) != RS1(O7)
2949 && ((y & F3I(~0))
2950 || (y & RS2(~0)) != RS2(O7)))
2952 bfd_vma reloc;
2954 reloc = relocation + rel->r_addend - rel->r_offset;
2955 reloc -= (input_section->output_section->vma
2956 + input_section->output_offset);
2958 /* Ensure the reloc fits into simm22. */
2959 if ((reloc & 3) == 0
2960 && ((reloc & ~(bfd_vma)0x7fffff) == 0
2961 || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
2963 reloc >>= 2;
2965 /* Check whether it fits into simm19 on v9. */
2966 if (((reloc & 0x3c0000) == 0
2967 || (reloc & 0x3c0000) == 0x3c0000)
2968 && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
2969 x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
2970 else
2971 x = INSN_BA | (reloc & 0x3fffff); /* ba */
2972 bfd_put_32 (input_bfd, x, contents + rel->r_offset);
2973 r = bfd_reloc_ok;
2974 if (rel->r_offset >= 4
2975 && (y & (0xffffffff ^ RS1(~0)))
2976 == (INSN_OR | RD(O7) | RS2(G0)))
2978 bfd_vma z;
2979 unsigned int reg;
2981 z = bfd_get_32 (input_bfd,
2982 contents + rel->r_offset - 4);
2983 if ((z & (0xffffffff ^ RD(~0)))
2984 != (INSN_OR | RS1(O7) | RS2(G0)))
2985 break;
2987 /* The sequence was
2988 or %o7, %g0, %rN
2989 call foo
2990 or %rN, %g0, %o7
2992 If call foo was replaced with ba, replace
2993 or %rN, %g0, %o7 with nop. */
2995 reg = (y & RS1(~0)) >> 14;
2996 if (reg != ((z & RD(~0)) >> 25)
2997 || reg == G0 || reg == O7)
2998 break;
3000 bfd_put_32 (input_bfd, (bfd_vma) INSN_NOP,
3001 contents + rel->r_offset + 4);
3009 if (r == bfd_reloc_continue)
3010 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3011 contents, rel->r_offset,
3012 relocation, rel->r_addend);
3014 if (r != bfd_reloc_ok)
3016 switch (r)
3018 default:
3019 case bfd_reloc_outofrange:
3020 abort ();
3021 case bfd_reloc_overflow:
3023 const char *name;
3025 if (h != NULL)
3026 name = h->root.root.string;
3027 else
3029 name = bfd_elf_string_from_elf_section (input_bfd,
3030 symtab_hdr->sh_link,
3031 sym->st_name);
3032 if (name == NULL)
3033 return FALSE;
3034 if (*name == '\0')
3035 name = bfd_section_name (input_bfd, sec);
3037 if (! ((*info->callbacks->reloc_overflow)
3038 (info, name, howto->name, (bfd_vma) 0,
3039 input_bfd, input_section, rel->r_offset)))
3040 return FALSE;
3042 break;
3047 return TRUE;
3050 /* Finish up dynamic symbol handling. We set the contents of various
3051 dynamic sections here. */
3053 static bfd_boolean
3054 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
3055 bfd *output_bfd;
3056 struct bfd_link_info *info;
3057 struct elf_link_hash_entry *h;
3058 Elf_Internal_Sym *sym;
3060 bfd *dynobj;
3061 struct elf32_sparc_link_hash_table *htab;
3063 htab = elf32_sparc_hash_table (info);
3064 dynobj = htab->elf.dynobj;
3066 if (h->plt.offset != (bfd_vma) -1)
3068 asection *splt;
3069 asection *srela;
3070 Elf_Internal_Rela rela;
3071 bfd_byte *loc;
3073 /* This symbol has an entry in the procedure linkage table. Set
3074 it up. */
3076 BFD_ASSERT (h->dynindx != -1);
3078 splt = htab->splt;
3079 srela = htab->srelplt;
3080 BFD_ASSERT (splt != NULL && srela != NULL);
3082 /* Fill in the entry in the procedure linkage table. */
3083 bfd_put_32 (output_bfd,
3084 PLT_ENTRY_WORD0 + h->plt.offset,
3085 splt->contents + h->plt.offset);
3086 bfd_put_32 (output_bfd,
3087 (PLT_ENTRY_WORD1
3088 + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
3089 splt->contents + h->plt.offset + 4);
3090 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
3091 splt->contents + h->plt.offset + 8);
3093 /* Fill in the entry in the .rela.plt section. */
3094 rela.r_offset = (splt->output_section->vma
3095 + splt->output_offset
3096 + h->plt.offset);
3097 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
3098 rela.r_addend = 0;
3099 loc = srela->contents;
3100 loc += (h->plt.offset / PLT_ENTRY_SIZE - 4) * sizeof (Elf32_External_Rela);
3101 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3103 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3105 /* Mark the symbol as undefined, rather than as defined in
3106 the .plt section. Leave the value alone. */
3107 sym->st_shndx = SHN_UNDEF;
3108 /* If the symbol is weak, we do need to clear the value.
3109 Otherwise, the PLT entry would provide a definition for
3110 the symbol even if the symbol wasn't defined anywhere,
3111 and so the symbol would never be NULL. */
3112 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
3113 == 0)
3114 sym->st_value = 0;
3118 if (h->got.offset != (bfd_vma) -1
3119 && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_GD
3120 && elf32_sparc_hash_entry(h)->tls_type != GOT_TLS_IE)
3122 asection *sgot;
3123 asection *srela;
3124 Elf_Internal_Rela rela;
3125 bfd_byte *loc;
3127 /* This symbol has an entry in the global offset table. Set it
3128 up. */
3130 sgot = htab->sgot;
3131 srela = htab->srelgot;
3132 BFD_ASSERT (sgot != NULL && srela != NULL);
3134 rela.r_offset = (sgot->output_section->vma
3135 + sgot->output_offset
3136 + (h->got.offset &~ (bfd_vma) 1));
3138 /* If this is a -Bsymbolic link, and the symbol is defined
3139 locally, we just want to emit a RELATIVE reloc. Likewise if
3140 the symbol was forced to be local because of a version file.
3141 The entry in the global offset table will already have been
3142 initialized in the relocate_section function. */
3143 if (info->shared
3144 && (info->symbolic || h->dynindx == -1)
3145 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3147 asection *sec = h->root.u.def.section;
3148 rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
3149 rela.r_addend = (h->root.u.def.value
3150 + sec->output_section->vma
3151 + sec->output_offset);
3153 else
3155 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
3156 rela.r_addend = 0;
3159 bfd_put_32 (output_bfd, (bfd_vma) 0,
3160 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
3161 loc = srela->contents;
3162 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3163 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3166 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3168 asection *s;
3169 Elf_Internal_Rela rela;
3170 bfd_byte *loc;
3172 /* This symbols needs a copy reloc. Set it up. */
3174 BFD_ASSERT (h->dynindx != -1);
3176 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3177 ".rela.bss");
3178 BFD_ASSERT (s != NULL);
3180 rela.r_offset = (h->root.u.def.value
3181 + h->root.u.def.section->output_section->vma
3182 + h->root.u.def.section->output_offset);
3183 rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
3184 rela.r_addend = 0;
3185 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3186 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3189 /* Mark some specially defined symbols as absolute. */
3190 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3191 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3192 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3193 sym->st_shndx = SHN_ABS;
3195 return TRUE;
3198 /* Finish up the dynamic sections. */
3200 static bfd_boolean
3201 elf32_sparc_finish_dynamic_sections (output_bfd, info)
3202 bfd *output_bfd;
3203 struct bfd_link_info *info;
3205 bfd *dynobj;
3206 asection *sdyn;
3207 struct elf32_sparc_link_hash_table *htab;
3209 htab = elf32_sparc_hash_table (info);
3210 dynobj = htab->elf.dynobj;
3212 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3214 if (elf_hash_table (info)->dynamic_sections_created)
3216 asection *splt;
3217 Elf32_External_Dyn *dyncon, *dynconend;
3219 splt = bfd_get_section_by_name (dynobj, ".plt");
3220 BFD_ASSERT (splt != NULL && sdyn != NULL);
3222 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3223 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3224 for (; dyncon < dynconend; dyncon++)
3226 Elf_Internal_Dyn dyn;
3227 const char *name;
3228 bfd_boolean size;
3230 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3232 switch (dyn.d_tag)
3234 case DT_PLTGOT: name = ".plt"; size = FALSE; break;
3235 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
3236 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
3237 default: name = NULL; size = FALSE; break;
3240 if (name != NULL)
3242 asection *s;
3244 s = bfd_get_section_by_name (output_bfd, name);
3245 if (s == NULL)
3246 dyn.d_un.d_val = 0;
3247 else
3249 if (! size)
3250 dyn.d_un.d_ptr = s->vma;
3251 else
3252 dyn.d_un.d_val = s->size;
3254 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3258 /* Clear the first four entries in the procedure linkage table,
3259 and put a nop in the last four bytes. */
3260 if (splt->size > 0)
3262 memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
3263 bfd_put_32 (output_bfd, (bfd_vma) SPARC_NOP,
3264 splt->contents + splt->size - 4);
3267 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
3270 /* Set the first entry in the global offset table to the address of
3271 the dynamic section. */
3272 if (htab->sgot && htab->sgot->size > 0)
3274 if (sdyn == NULL)
3275 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgot->contents);
3276 else
3277 bfd_put_32 (output_bfd,
3278 sdyn->output_section->vma + sdyn->output_offset,
3279 htab->sgot->contents);
3282 if (htab->sgot)
3283 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
3285 return TRUE;
3288 /* Functions for dealing with the e_flags field.
3290 We don't define set_private_flags or copy_private_bfd_data because
3291 the only currently defined values are based on the bfd mach number,
3292 so we use the latter instead and defer setting e_flags until the
3293 file is written out. */
3295 /* Merge backend specific data from an object file to the output
3296 object file when linking. */
3298 static bfd_boolean
3299 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
3300 bfd *ibfd;
3301 bfd *obfd;
3303 bfd_boolean error;
3304 unsigned long ibfd_mach;
3305 /* FIXME: This should not be static. */
3306 static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
3308 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3309 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3310 return TRUE;
3312 error = FALSE;
3314 ibfd_mach = bfd_get_mach (ibfd);
3315 if (bfd_mach_sparc_64bit_p (ibfd_mach))
3317 error = TRUE;
3318 (*_bfd_error_handler)
3319 (_("%B: compiled for a 64 bit system and target is 32 bit"), ibfd);
3321 else if ((ibfd->flags & DYNAMIC) == 0)
3323 if (bfd_get_mach (obfd) < ibfd_mach)
3324 bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
3327 if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
3328 != previous_ibfd_e_flags)
3329 && previous_ibfd_e_flags != (unsigned long) -1)
3331 (*_bfd_error_handler)
3332 (_("%B: linking little endian files with big endian files"), ibfd);
3333 error = TRUE;
3335 previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
3337 if (error)
3339 bfd_set_error (bfd_error_bad_value);
3340 return FALSE;
3343 return TRUE;
3346 /* Set the right machine number. */
3348 static bfd_boolean
3349 elf32_sparc_object_p (abfd)
3350 bfd *abfd;
3352 if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
3354 if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US3)
3355 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3356 bfd_mach_sparc_v8plusb);
3357 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
3358 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3359 bfd_mach_sparc_v8plusa);
3360 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
3361 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3362 bfd_mach_sparc_v8plus);
3363 else
3364 return FALSE;
3366 else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
3367 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
3368 bfd_mach_sparc_sparclite_le);
3369 else
3370 return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
3373 /* The final processing done just before writing out the object file.
3374 We need to set the e_machine field appropriately. */
3376 static void
3377 elf32_sparc_final_write_processing (abfd, linker)
3378 bfd *abfd;
3379 bfd_boolean linker ATTRIBUTE_UNUSED;
3381 switch (bfd_get_mach (abfd))
3383 case bfd_mach_sparc :
3384 break; /* nothing to do */
3385 case bfd_mach_sparc_v8plus :
3386 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3387 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3388 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
3389 break;
3390 case bfd_mach_sparc_v8plusa :
3391 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3392 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3393 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
3394 break;
3395 case bfd_mach_sparc_v8plusb :
3396 elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
3397 elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
3398 elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
3399 | EF_SPARC_SUN_US3;
3400 break;
3401 case bfd_mach_sparc_sparclite_le :
3402 elf_elfheader (abfd)->e_machine = EM_SPARC;
3403 elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
3404 break;
3405 default :
3406 abort ();
3407 break;
3411 static enum elf_reloc_type_class
3412 elf32_sparc_reloc_type_class (rela)
3413 const Elf_Internal_Rela *rela;
3415 switch ((int) ELF32_R_TYPE (rela->r_info))
3417 case R_SPARC_RELATIVE:
3418 return reloc_class_relative;
3419 case R_SPARC_JMP_SLOT:
3420 return reloc_class_plt;
3421 case R_SPARC_COPY:
3422 return reloc_class_copy;
3423 default:
3424 return reloc_class_normal;
3428 /* Return address for Ith PLT stub in section PLT, for relocation REL
3429 or (bfd_vma) -1 if it should not be included. */
3431 static bfd_vma
3432 elf32_sparc_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
3433 const asection *plt ATTRIBUTE_UNUSED,
3434 const arelent *rel)
3436 return rel->address;
3439 #define TARGET_BIG_SYM bfd_elf32_sparc_vec
3440 #define TARGET_BIG_NAME "elf32-sparc"
3441 #define ELF_ARCH bfd_arch_sparc
3442 #define ELF_MACHINE_CODE EM_SPARC
3443 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
3444 #define ELF_MAXPAGESIZE 0x10000
3446 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
3447 #define bfd_elf32_bfd_link_hash_table_create \
3448 elf32_sparc_link_hash_table_create
3449 #define bfd_elf32_bfd_relax_section elf32_sparc_relax_section
3450 #define bfd_elf32_new_section_hook elf32_sparc_new_section_hook
3451 #define elf_info_to_howto elf32_sparc_info_to_howto
3452 #define elf_backend_copy_indirect_symbol \
3453 elf32_sparc_copy_indirect_symbol
3454 #define elf_backend_create_dynamic_sections \
3455 elf32_sparc_create_dynamic_sections
3456 #define elf_backend_check_relocs elf32_sparc_check_relocs
3457 #define elf_backend_adjust_dynamic_symbol \
3458 elf32_sparc_adjust_dynamic_symbol
3459 #define elf_backend_size_dynamic_sections \
3460 elf32_sparc_size_dynamic_sections
3461 #define elf_backend_relocate_section elf32_sparc_relocate_section
3462 #define elf_backend_finish_dynamic_symbol \
3463 elf32_sparc_finish_dynamic_symbol
3464 #define elf_backend_finish_dynamic_sections \
3465 elf32_sparc_finish_dynamic_sections
3466 #define bfd_elf32_bfd_merge_private_bfd_data \
3467 elf32_sparc_merge_private_bfd_data
3468 #define bfd_elf32_mkobject elf32_sparc_mkobject
3469 #define elf_backend_object_p elf32_sparc_object_p
3470 #define elf_backend_final_write_processing \
3471 elf32_sparc_final_write_processing
3472 #define elf_backend_gc_mark_hook elf32_sparc_gc_mark_hook
3473 #define elf_backend_gc_sweep_hook elf32_sparc_gc_sweep_hook
3474 #define elf_backend_grok_psinfo elf32_sparc_grok_psinfo
3475 #define elf_backend_reloc_type_class elf32_sparc_reloc_type_class
3476 #define elf_backend_plt_sym_val elf32_sparc_plt_sym_val
3478 #define elf_backend_can_gc_sections 1
3479 #define elf_backend_can_refcount 1
3480 #define elf_backend_want_got_plt 0
3481 #define elf_backend_plt_readonly 0
3482 #define elf_backend_want_plt_sym 1
3483 #define elf_backend_got_header_size 4
3484 #define elf_backend_rela_normal 1
3486 #include "elf32-target.h"