]> Git Repo - binutils.git/blob - bfd/elf32-sparc.c
2000-08-03 H.J. Lu <[email protected]>
[binutils.git] / bfd / elf32-sparc.c
1 /* SPARC-specific support for 32-bit ELF
2    Copyright (C) 1993, 94, 95, 96, 97, 98, 99, 2000
3    Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/sparc.h"
27 #include "opcode/sparc.h"
28
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 boolean elf32_sparc_check_relocs
34   PARAMS ((bfd *, struct bfd_link_info *, asection *,
35            const Elf_Internal_Rela *));
36 static boolean elf32_sparc_adjust_dynamic_symbol
37   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
38 static boolean elf32_sparc_size_dynamic_sections
39   PARAMS ((bfd *, struct bfd_link_info *));
40 static boolean elf32_sparc_relax_section
41   PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
42 static boolean elf32_sparc_relocate_section
43   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
44            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
45 static boolean elf32_sparc_finish_dynamic_symbol
46   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
47            Elf_Internal_Sym *));
48 static boolean elf32_sparc_finish_dynamic_sections
49   PARAMS ((bfd *, struct bfd_link_info *));
50 static boolean elf32_sparc_merge_private_bfd_data PARAMS ((bfd *, bfd *));
51 static boolean elf32_sparc_object_p
52   PARAMS ((bfd *));
53 static void elf32_sparc_final_write_processing
54   PARAMS ((bfd *, boolean));
55 \f
56 /* The relocation "howto" table.  */
57
58 static bfd_reloc_status_type sparc_elf_notsupported_reloc
59   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
60 static bfd_reloc_status_type sparc_elf_wdisp16_reloc
61   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
62
63 reloc_howto_type _bfd_sparc_elf_howto_table[] =
64 {
65   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
66   HOWTO(R_SPARC_8,         0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_8",       false,0,0x000000ff,true),
67   HOWTO(R_SPARC_16,        0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_16",      false,0,0x0000ffff,true),
68   HOWTO(R_SPARC_32,        0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_32",      false,0,0xffffffff,true),
69   HOWTO(R_SPARC_DISP8,     0,0, 8,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP8",   false,0,0x000000ff,true),
70   HOWTO(R_SPARC_DISP16,    0,1,16,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP16",  false,0,0x0000ffff,true),
71   HOWTO(R_SPARC_DISP32,    0,2,32,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_DISP32",  false,0,0x00ffffff,true),
72   HOWTO(R_SPARC_WDISP30,   2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP30", false,0,0x3fffffff,true),
73   HOWTO(R_SPARC_WDISP22,   2,2,22,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP22", false,0,0x003fffff,true),
74   HOWTO(R_SPARC_HI22,     10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_HI22",    false,0,0x003fffff,true),
75   HOWTO(R_SPARC_22,        0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_22",      false,0,0x003fffff,true),
76   HOWTO(R_SPARC_13,        0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_13",      false,0,0x00001fff,true),
77   HOWTO(R_SPARC_LO10,      0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_LO10",    false,0,0x000003ff,true),
78   HOWTO(R_SPARC_GOT10,     0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT10",   false,0,0x000003ff,true),
79   HOWTO(R_SPARC_GOT13,     0,2,13,false,0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_GOT13",   false,0,0x00001fff,true),
80   HOWTO(R_SPARC_GOT22,    10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_GOT22",   false,0,0x003fffff,true),
81   HOWTO(R_SPARC_PC10,      0,2,10,true, 0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_PC10",    false,0,0x000003ff,true),
82   HOWTO(R_SPARC_PC22,     10,2,22,true, 0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_PC22",    false,0,0x003fffff,true),
83   HOWTO(R_SPARC_WPLT30,    2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WPLT30",  false,0,0x3fffffff,true),
84   HOWTO(R_SPARC_COPY,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_COPY",    false,0,0x00000000,true),
85   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),
86   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),
87   HOWTO(R_SPARC_RELATIVE,  0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_RELATIVE",false,0,0x00000000,true),
88   HOWTO(R_SPARC_UA32,      0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_UA32",    false,0,0x00000000,true),
89   HOWTO(R_SPARC_PLT32,     0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PLT32",    false,0,0x00000000,true),
90   HOWTO(R_SPARC_HIPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HIPLT22",  false,0,0x00000000,true),
91   HOWTO(R_SPARC_LOPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LOPLT10",  false,0,0x00000000,true),
92   HOWTO(R_SPARC_PCPLT32,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT32",  false,0,0x00000000,true),
93   HOWTO(R_SPARC_PCPLT22,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT22",  false,0,0x00000000,true),
94   HOWTO(R_SPARC_PCPLT10,   0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_PCPLT10",  false,0,0x00000000,true),
95   HOWTO(R_SPARC_10,        0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_10",      false,0,0x000003ff,true),
96   HOWTO(R_SPARC_11,        0,2,11,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_11",      false,0,0x000007ff,true),
97   /* These are for sparc64 in a 64 bit environment.
98      Values need to be here because the table is indexed by reloc number.  */
99   HOWTO(R_SPARC_64,        0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_64",      false,0,0x00000000,true),
100   HOWTO(R_SPARC_OLO10,     0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_OLO10",   false,0,0x00000000,true),
101   HOWTO(R_SPARC_HH22,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HH22",    false,0,0x00000000,true),
102   HOWTO(R_SPARC_HM10,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_HM10",    false,0,0x00000000,true),
103   HOWTO(R_SPARC_LM22,      0,0,00,false,0,complain_overflow_dont,    sparc_elf_notsupported_reloc,  "R_SPARC_LM22",    false,0,0x00000000,true),
104   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),
105   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),
106   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),
107   /* End sparc64 in 64 bit environment values.
108      The following are for sparc64 in a 32 bit environment.  */
109   HOWTO(R_SPARC_WDISP16,   2,2,16,true, 0,complain_overflow_signed,  sparc_elf_wdisp16_reloc,"R_SPARC_WDISP16", false,0,0x00000000,true),
110   HOWTO(R_SPARC_WDISP19,   2,2,19,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,  "R_SPARC_WDISP19", false,0,0x0007ffff,true),
111   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),
112   HOWTO(R_SPARC_7,         0,2, 7,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_7",       false,0,0x0000007f,true),
113   HOWTO(R_SPARC_5,         0,2, 5,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_5",       false,0,0x0000001f,true),
114   HOWTO(R_SPARC_6,         0,2, 6,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_6",       false,0,0x0000003f,true),
115   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
116   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
117   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
118   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
119   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
120   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
121   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
122   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
123   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
124   HOWTO(R_SPARC_NONE,      0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,  "R_SPARC_NONE",    false,0,0x00000000,true),
125   HOWTO(R_SPARC_REV32,      0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,  "R_SPARC_REV32",      false,0,0xffffffff,true),
126 };
127 static reloc_howto_type elf32_sparc_vtinherit_howto =
128   HOWTO (R_SPARC_GNU_VTINHERIT, 0,2,0,false,0,complain_overflow_dont, NULL, "R_SPARC_GNU_VTINHERIT", false,0, 0, false);
129 static reloc_howto_type elf32_sparc_vtentry_howto =
130   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);
131
132
133 struct elf_reloc_map {
134   bfd_reloc_code_real_type bfd_reloc_val;
135   unsigned char elf_reloc_val;
136 };
137
138 static CONST struct elf_reloc_map sparc_reloc_map[] =
139 {
140   { BFD_RELOC_NONE, R_SPARC_NONE, },
141   { BFD_RELOC_16, R_SPARC_16, },
142   { BFD_RELOC_8, R_SPARC_8 },
143   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
144   { BFD_RELOC_CTOR, R_SPARC_32 },
145   { BFD_RELOC_32, R_SPARC_32 },
146   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
147   { BFD_RELOC_HI22, R_SPARC_HI22 },
148   { BFD_RELOC_LO10, R_SPARC_LO10, },
149   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
150   { BFD_RELOC_SPARC22, R_SPARC_22 },
151   { BFD_RELOC_SPARC13, R_SPARC_13 },
152   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
153   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
154   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
155   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
156   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
157   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
158   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
159   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
160   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
161   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
162   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
163   /* ??? Doesn't dwarf use this?  */
164 /*{ BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
165   {BFD_RELOC_SPARC_10, R_SPARC_10},
166   {BFD_RELOC_SPARC_11, R_SPARC_11},
167   {BFD_RELOC_SPARC_64, R_SPARC_64},
168   {BFD_RELOC_SPARC_OLO10, R_SPARC_OLO10},
169   {BFD_RELOC_SPARC_HH22, R_SPARC_HH22},
170   {BFD_RELOC_SPARC_HM10, R_SPARC_HM10},
171   {BFD_RELOC_SPARC_LM22, R_SPARC_LM22},
172   {BFD_RELOC_SPARC_PC_HH22, R_SPARC_PC_HH22},
173   {BFD_RELOC_SPARC_PC_HM10, R_SPARC_PC_HM10},
174   {BFD_RELOC_SPARC_PC_LM22, R_SPARC_PC_LM22},
175   {BFD_RELOC_SPARC_WDISP16, R_SPARC_WDISP16},
176   {BFD_RELOC_SPARC_WDISP19, R_SPARC_WDISP19},
177   {BFD_RELOC_SPARC_7, R_SPARC_7},
178   {BFD_RELOC_SPARC_5, R_SPARC_5},
179   {BFD_RELOC_SPARC_6, R_SPARC_6},
180   {BFD_RELOC_SPARC_REV32, R_SPARC_REV32 },
181   {BFD_RELOC_VTABLE_INHERIT, R_SPARC_GNU_VTINHERIT},
182   {BFD_RELOC_VTABLE_ENTRY, R_SPARC_GNU_VTENTRY},
183 };
184
185 static reloc_howto_type *
186 elf32_sparc_reloc_type_lookup (abfd, code)
187      bfd *abfd ATTRIBUTE_UNUSED;
188      bfd_reloc_code_real_type code;
189 {
190   unsigned int i;
191   
192   switch (code)
193     {
194     case BFD_RELOC_VTABLE_INHERIT:
195       return &elf32_sparc_vtinherit_howto;
196
197     case BFD_RELOC_VTABLE_ENTRY:
198       return &elf32_sparc_vtentry_howto;
199
200     default:
201       for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
202         {
203           if (sparc_reloc_map[i].bfd_reloc_val == code)
204             return &_bfd_sparc_elf_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
205         }
206     }
207     bfd_set_error (bfd_error_bad_value);
208     return NULL;
209 }
210
211 /* We need to use ELF32_R_TYPE so we have our own copy of this function,
212    and elf64-sparc.c has its own copy.  */
213
214 static void
215 elf32_sparc_info_to_howto (abfd, cache_ptr, dst)
216      bfd *abfd ATTRIBUTE_UNUSED;
217      arelent *cache_ptr;
218      Elf_Internal_Rela *dst;
219 {
220   switch (ELF32_R_TYPE(dst->r_info))
221     {
222     case R_SPARC_GNU_VTINHERIT:
223       cache_ptr->howto = &elf32_sparc_vtinherit_howto;
224       break;
225
226     case R_SPARC_GNU_VTENTRY:
227       cache_ptr->howto = &elf32_sparc_vtentry_howto;
228       break;
229
230     default:
231       BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max_std);
232       cache_ptr->howto = &_bfd_sparc_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
233     }
234 }
235 \f
236 /* For unsupported relocs.  */
237
238 static bfd_reloc_status_type
239 sparc_elf_notsupported_reloc (abfd,
240                              reloc_entry,
241                              symbol,
242                              data,
243                              input_section,
244                              output_bfd,
245                              error_message)
246      bfd *abfd ATTRIBUTE_UNUSED;
247      arelent *reloc_entry ATTRIBUTE_UNUSED;
248      asymbol *symbol ATTRIBUTE_UNUSED;
249      PTR data ATTRIBUTE_UNUSED;
250      asection *input_section ATTRIBUTE_UNUSED;
251      bfd *output_bfd ATTRIBUTE_UNUSED;
252      char **error_message ATTRIBUTE_UNUSED;
253 {
254   return bfd_reloc_notsupported;
255 }
256
257 /* Handle the WDISP16 reloc.  */
258
259 static bfd_reloc_status_type
260 sparc_elf_wdisp16_reloc (abfd,
261                          reloc_entry,
262                          symbol,
263                          data,
264                          input_section,
265                          output_bfd,
266                          error_message)
267      bfd *abfd;
268      arelent *reloc_entry;
269      asymbol *symbol;
270      PTR data;
271      asection *input_section;
272      bfd *output_bfd;
273      char **error_message ATTRIBUTE_UNUSED;
274 {
275   bfd_vma relocation;
276   bfd_vma x;
277
278   if (output_bfd != (bfd *) NULL
279       && (symbol->flags & BSF_SECTION_SYM) == 0
280       && (! reloc_entry->howto->partial_inplace
281           || reloc_entry->addend == 0))
282     {
283       reloc_entry->address += input_section->output_offset;
284       return bfd_reloc_ok;
285     }
286
287   if (output_bfd != NULL)
288     return bfd_reloc_continue;
289
290   if (reloc_entry->address > input_section->_cooked_size)
291     return bfd_reloc_outofrange;
292
293   relocation = (symbol->value
294                 + symbol->section->output_section->vma
295                 + symbol->section->output_offset);
296   relocation += reloc_entry->addend;
297   relocation -= (input_section->output_section->vma
298                  + input_section->output_offset);
299   relocation -= reloc_entry->address;
300
301   x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
302   x |= ((((relocation >> 2) & 0xc000) << 6)
303         | ((relocation >> 2) & 0x3fff));
304   bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
305
306   if ((bfd_signed_vma) relocation < - 0x40000
307       || (bfd_signed_vma) relocation > 0x3ffff)
308     return bfd_reloc_overflow;
309   else
310     return bfd_reloc_ok;
311 }
312 \f
313 /* Functions for the SPARC ELF linker.  */
314
315 /* The name of the dynamic interpreter.  This is put in the .interp
316    section.  */
317
318 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
319
320 /* The nop opcode we use.  */
321
322 #define SPARC_NOP 0x01000000
323
324 /* The size in bytes of an entry in the procedure linkage table.  */
325
326 #define PLT_ENTRY_SIZE 12
327
328 /* The first four entries in a procedure linkage table are reserved,
329    and the initial contents are unimportant (we zero them out).
330    Subsequent entries look like this.  See the SVR4 ABI SPARC
331    supplement to see how this works.  */
332
333 /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
334 #define PLT_ENTRY_WORD0 0x03000000
335 /* b,a .plt0.  We fill in the offset later.  */
336 #define PLT_ENTRY_WORD1 0x30800000
337 /* nop.  */
338 #define PLT_ENTRY_WORD2 SPARC_NOP
339
340 /* Look through the relocs for a section during the first phase, and
341    allocate space in the global offset table or procedure linkage
342    table.  */
343
344 static boolean
345 elf32_sparc_check_relocs (abfd, info, sec, relocs)
346      bfd *abfd;
347      struct bfd_link_info *info;
348      asection *sec;
349      const Elf_Internal_Rela *relocs;
350 {
351   bfd *dynobj;
352   Elf_Internal_Shdr *symtab_hdr;
353   struct elf_link_hash_entry **sym_hashes;
354   bfd_vma *local_got_offsets;
355   const Elf_Internal_Rela *rel;
356   const Elf_Internal_Rela *rel_end;
357   asection *sgot;
358   asection *srelgot;
359   asection *sreloc;
360
361   if (info->relocateable)
362     return true;
363
364   dynobj = elf_hash_table (info)->dynobj;
365   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
366   sym_hashes = elf_sym_hashes (abfd);
367   local_got_offsets = elf_local_got_offsets (abfd);
368
369   sgot = NULL;
370   srelgot = NULL;
371   sreloc = NULL;
372
373   rel_end = relocs + sec->reloc_count;
374   for (rel = relocs; rel < rel_end; rel++)
375     {
376       unsigned long r_symndx;
377       struct elf_link_hash_entry *h;
378
379       r_symndx = ELF32_R_SYM (rel->r_info);
380       if (r_symndx < symtab_hdr->sh_info)
381         h = NULL;
382       else
383         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
384
385       switch (ELF32_R_TYPE (rel->r_info))
386         {
387         case R_SPARC_GOT10:
388         case R_SPARC_GOT13:
389         case R_SPARC_GOT22:
390           /* This symbol requires a global offset table entry.  */
391
392           if (dynobj == NULL)
393             {
394               /* Create the .got section.  */
395               elf_hash_table (info)->dynobj = dynobj = abfd;
396               if (! _bfd_elf_create_got_section (dynobj, info))
397                 return false;
398             }
399
400           if (sgot == NULL)
401             {
402               sgot = bfd_get_section_by_name (dynobj, ".got");
403               BFD_ASSERT (sgot != NULL);
404             }
405
406           if (srelgot == NULL
407               && (h != NULL || info->shared))
408             {
409               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
410               if (srelgot == NULL)
411                 {
412                   srelgot = bfd_make_section (dynobj, ".rela.got");
413                   if (srelgot == NULL
414                       || ! bfd_set_section_flags (dynobj, srelgot,
415                                                   (SEC_ALLOC
416                                                    | SEC_LOAD
417                                                    | SEC_HAS_CONTENTS
418                                                    | SEC_IN_MEMORY
419                                                    | SEC_LINKER_CREATED
420                                                    | SEC_READONLY))
421                       || ! bfd_set_section_alignment (dynobj, srelgot, 2))
422                     return false;
423                 }
424             }
425
426           if (h != NULL)
427             {
428               if (h->got.offset != (bfd_vma) -1)
429                 {
430                   /* We have already allocated space in the .got.  */
431                   break;
432                 }
433               h->got.offset = sgot->_raw_size;
434
435               /* Make sure this symbol is output as a dynamic symbol.  */
436               if (h->dynindx == -1)
437                 {
438                   if (! bfd_elf32_link_record_dynamic_symbol (info, h))
439                     return false;
440                 }
441
442               srelgot->_raw_size += sizeof (Elf32_External_Rela);
443             }
444           else
445             {
446               /* This is a global offset table entry for a local
447                  symbol.  */
448               if (local_got_offsets == NULL)
449                 {
450                   size_t size;
451                   register unsigned int i;
452
453                   size = symtab_hdr->sh_info * sizeof (bfd_vma);
454                   local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
455                   if (local_got_offsets == NULL)
456                     return false;
457                   elf_local_got_offsets (abfd) = local_got_offsets;
458                   for (i = 0; i < symtab_hdr->sh_info; i++)
459                     local_got_offsets[i] = (bfd_vma) -1;
460                 }
461               if (local_got_offsets[r_symndx] != (bfd_vma) -1)
462                 {
463                   /* We have already allocated space in the .got.  */
464                   break;
465                 }
466               local_got_offsets[r_symndx] = sgot->_raw_size;
467
468               if (info->shared)
469                 {
470                   /* If we are generating a shared object, we need to
471                      output a R_SPARC_RELATIVE reloc so that the
472                      dynamic linker can adjust this GOT entry.  */
473                   srelgot->_raw_size += sizeof (Elf32_External_Rela);
474                 }
475             }
476
477           sgot->_raw_size += 4;
478
479           /* If the .got section is more than 0x1000 bytes, we add
480              0x1000 to the value of _GLOBAL_OFFSET_TABLE_, so that 13
481              bit relocations have a greater chance of working.  */
482           if (sgot->_raw_size >= 0x1000
483               && elf_hash_table (info)->hgot->root.u.def.value == 0)
484             elf_hash_table (info)->hgot->root.u.def.value = 0x1000;
485
486           break;
487
488         case R_SPARC_WPLT30:
489           /* This symbol requires a procedure linkage table entry.  We
490              actually build the entry in adjust_dynamic_symbol,
491              because this might be a case of linking PIC code without
492              linking in any dynamic objects, in which case we don't
493              need to generate a procedure linkage table after all.  */
494
495           if (h == NULL)
496             {
497               /* The Solaris native assembler will generate a WPLT30
498                  reloc for a local symbol if you assemble a call from
499                  one section to another when using -K pic.  We treat
500                  it as WDISP30.  */
501               break;
502             }
503
504           /* Make sure this symbol is output as a dynamic symbol.  */
505           if (h->dynindx == -1)
506             {
507               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
508                 return false;
509             }
510
511           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
512
513           break;
514
515         case R_SPARC_PC10:
516         case R_SPARC_PC22:
517           if (h != NULL)
518             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
519
520           if (h != NULL
521               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
522             break;
523           /* Fall through.  */
524         case R_SPARC_DISP8:
525         case R_SPARC_DISP16:
526         case R_SPARC_DISP32:
527         case R_SPARC_WDISP30:
528         case R_SPARC_WDISP22:
529         case R_SPARC_WDISP19:
530         case R_SPARC_WDISP16:
531           if (h != NULL)
532             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
533
534           /* If we are linking with -Bsymbolic, we do not need to copy
535              a PC relative reloc against a global symbol which is
536              defined in an object we are including in the link (i.e.,
537              DEF_REGULAR is set).  FIXME: At this point we have not
538              seen all the input files, so it is possible that
539              DEF_REGULAR is not set now but will be set later (it is
540              never cleared).  This needs to be handled as in
541              elf32-i386.c.  */
542           if (h == NULL
543               || (info->symbolic
544                   && (h->elf_link_hash_flags
545                       & ELF_LINK_HASH_DEF_REGULAR) != 0))
546             break;
547           /* Fall through.  */
548         case R_SPARC_8:
549         case R_SPARC_16:
550         case R_SPARC_32:
551         case R_SPARC_HI22:
552         case R_SPARC_22:
553         case R_SPARC_13:
554         case R_SPARC_LO10:
555         case R_SPARC_UA32:
556           if (h != NULL)
557             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
558
559           if (info->shared)
560             {
561               /* When creating a shared object, we must copy these
562                  relocs into the output file.  We create a reloc
563                  section in dynobj and make room for the reloc.  */
564               if (sreloc == NULL)
565                 {
566                   const char *name;
567
568                   name = (bfd_elf_string_from_elf_section
569                           (abfd,
570                            elf_elfheader (abfd)->e_shstrndx,
571                            elf_section_data (sec)->rel_hdr.sh_name));
572                   if (name == NULL)
573                     return false;
574
575                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
576                               && strcmp (bfd_get_section_name (abfd, sec),
577                                          name + 5) == 0);
578
579                   sreloc = bfd_get_section_by_name (dynobj, name);
580                   if (sreloc == NULL)
581                     {
582                       flagword flags;
583
584                       sreloc = bfd_make_section (dynobj, name);
585                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
586                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
587                       if ((sec->flags & SEC_ALLOC) != 0)
588                         flags |= SEC_ALLOC | SEC_LOAD;
589                       if (sreloc == NULL
590                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
591                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
592                         return false;
593                     }
594                 }
595
596               sreloc->_raw_size += sizeof (Elf32_External_Rela);
597             }
598
599           break;
600
601         case R_SPARC_GNU_VTINHERIT:
602           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
603             return false;
604           break;
605
606         case R_SPARC_GNU_VTENTRY:
607           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
608             return false;
609           break;
610
611         default:
612           break;
613         }
614     }
615
616   return true;
617 }
618
619 static asection *
620 elf32_sparc_gc_mark_hook (abfd, info, rel, h, sym)
621        bfd *abfd;
622        struct bfd_link_info *info ATTRIBUTE_UNUSED;
623        Elf_Internal_Rela *rel;
624        struct elf_link_hash_entry *h;
625        Elf_Internal_Sym *sym;
626 {
627
628   if (h != NULL)
629     {
630       switch (ELF32_R_TYPE (rel->r_info))
631       {
632       case R_SPARC_GNU_VTINHERIT:
633       case R_SPARC_GNU_VTENTRY:
634         break;
635
636       default:
637         switch (h->root.type)
638           {
639           case bfd_link_hash_defined:
640           case bfd_link_hash_defweak:
641             return h->root.u.def.section;
642
643           case bfd_link_hash_common:
644             return h->root.u.c.p->section;
645
646           default:
647             break;
648           }
649        }
650      }
651    else
652      {
653        if (!(elf_bad_symtab (abfd)
654            && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
655          && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
656                 && sym->st_shndx != SHN_COMMON))
657           {
658             return bfd_section_from_elf_index (abfd, sym->st_shndx);
659           }
660       }
661
662   return NULL;
663 }
664
665 /* Update the got entry reference counts for the section being removed.  */
666 static boolean
667 elf32_sparc_gc_sweep_hook (abfd, info, sec, relocs)
668      bfd *abfd;
669      struct bfd_link_info *info ATTRIBUTE_UNUSED;
670      asection *sec;
671      const Elf_Internal_Rela *relocs;
672 {
673
674   Elf_Internal_Shdr *symtab_hdr;
675   struct elf_link_hash_entry **sym_hashes;
676   bfd_signed_vma *local_got_refcounts;
677   const Elf_Internal_Rela *rel, *relend;
678   unsigned long r_symndx;
679   struct elf_link_hash_entry *h;
680
681   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
682   sym_hashes = elf_sym_hashes (abfd);
683   local_got_refcounts = elf_local_got_refcounts (abfd);
684
685   relend = relocs + sec->reloc_count;
686   for (rel = relocs; rel < relend; rel++)
687     switch (ELF32_R_TYPE (rel->r_info))
688       {
689       case R_SPARC_GOT10:
690       case R_SPARC_GOT13:
691       case R_SPARC_GOT22:
692         r_symndx = ELF32_R_SYM (rel->r_info);
693         if (r_symndx >= symtab_hdr->sh_info)
694           {
695             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
696             if (h->got.refcount > 0)
697               h->got.refcount--;
698           }
699         else
700           {
701             if (local_got_refcounts[r_symndx] > 0)
702               local_got_refcounts[r_symndx]--;
703           }
704         break;
705
706       case R_SPARC_PLT32:
707       case R_SPARC_HIPLT22:
708       case R_SPARC_LOPLT10:
709       case R_SPARC_PCPLT32:
710       case R_SPARC_PCPLT10:
711         r_symndx = ELF32_R_SYM (rel->r_info);
712         if (r_symndx >= symtab_hdr->sh_info)
713           {
714             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
715             if (h->plt.refcount > 0)
716               h->plt.refcount--;
717           }
718         break;
719
720       default:
721         break;
722       }
723
724   return true;
725 }
726
727 /* Adjust a symbol defined by a dynamic object and referenced by a
728    regular object.  The current definition is in some section of the
729    dynamic object, but we're not including those sections.  We have to
730    change the definition to something the rest of the link can
731    understand.  */
732
733 static boolean
734 elf32_sparc_adjust_dynamic_symbol (info, h)
735      struct bfd_link_info *info;
736      struct elf_link_hash_entry *h;
737 {
738   bfd *dynobj;
739   asection *s;
740   unsigned int power_of_two;
741
742   dynobj = elf_hash_table (info)->dynobj;
743
744   /* Make sure we know what is going on here.  */
745   BFD_ASSERT (dynobj != NULL
746               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
747                   || h->weakdef != NULL
748                   || ((h->elf_link_hash_flags
749                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
750                       && (h->elf_link_hash_flags
751                           & ELF_LINK_HASH_REF_REGULAR) != 0
752                       && (h->elf_link_hash_flags
753                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
754
755   /* If this is a function, put it in the procedure linkage table.  We
756      will fill in the contents of the procedure linkage table later
757      (although we could actually do it here).  The STT_NOTYPE
758      condition is a hack specifically for the Oracle libraries
759      delivered for Solaris; for some inexplicable reason, they define
760      some of their functions as STT_NOTYPE when they really should be
761      STT_FUNC.  */
762   if (h->type == STT_FUNC
763       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
764       || (h->type == STT_NOTYPE
765           && (h->root.type == bfd_link_hash_defined
766               || h->root.type == bfd_link_hash_defweak)
767           && (h->root.u.def.section->flags & SEC_CODE) != 0))
768     {
769       if (! elf_hash_table (info)->dynamic_sections_created
770           || ((!info->shared || info->symbolic || h->dynindx == -1)
771               && (h->elf_link_hash_flags
772                   & ELF_LINK_HASH_DEF_REGULAR) != 0))
773         {
774           /* This case can occur if we saw a WPLT30 reloc in an input
775              file, but none of the input files were dynamic objects.
776              Or, when linking the main application or a -Bsymbolic
777              shared library against PIC code.  Or when a global symbol
778              has been made private, e.g. via versioning.
779
780              In these cases we know what value the symbol will resolve
781              to, so we don't actually need to build a procedure linkage
782              table, and we can just do a WDISP30 reloc instead.  */
783
784           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
785           return true;
786         }
787
788       s = bfd_get_section_by_name (dynobj, ".plt");
789       BFD_ASSERT (s != NULL);
790
791       /* The first four entries in .plt are reserved.  */
792       if (s->_raw_size == 0)
793         s->_raw_size = 4 * PLT_ENTRY_SIZE;
794
795       /* The procedure linkage table has a maximum size.  */
796       if (s->_raw_size >= 0x400000)
797         {
798           bfd_set_error (bfd_error_bad_value);
799           return false;
800         }
801
802      /* If this symbol is not defined in a regular file, and we are
803        not generating a shared library, then set the symbol to this
804        location in the .plt.  This is required to make function
805        pointers compare as equal between the normal executable and
806        the shared library.  */
807      if (! info->shared
808         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
809       {
810         h->root.u.def.section = s;
811         h->root.u.def.value = s->_raw_size;
812       }
813
814       h->plt.offset = s->_raw_size;
815
816       /* Make room for this entry.  */
817       s->_raw_size += PLT_ENTRY_SIZE;
818
819       /* We also need to make an entry in the .rela.plt section.  */
820
821       s = bfd_get_section_by_name (dynobj, ".rela.plt");
822       BFD_ASSERT (s != NULL);
823       s->_raw_size += sizeof (Elf32_External_Rela);
824
825       return true;
826     }
827
828   /* If this is a weak symbol, and there is a real definition, the
829      processor independent code will have arranged for us to see the
830      real definition first, and we can just use the same value.  */
831   if (h->weakdef != NULL)
832     {
833       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
834                   || h->weakdef->root.type == bfd_link_hash_defweak);
835       h->root.u.def.section = h->weakdef->root.u.def.section;
836       h->root.u.def.value = h->weakdef->root.u.def.value;
837       return true;
838     }
839
840   /* This is a reference to a symbol defined by a dynamic object which
841      is not a function.  */
842
843   /* If we are creating a shared library, we must presume that the
844      only references to the symbol are via the global offset table.
845      For such cases we need not do anything here; the relocations will
846      be handled correctly by relocate_section.  */
847   if (info->shared)
848     return true;
849
850   /* If there are no references to this symbol that do not use the
851      GOT, we don't need to generate a copy reloc.  */
852   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
853     return true;
854
855   /* We must allocate the symbol in our .dynbss section, which will
856      become part of the .bss section of the executable.  There will be
857      an entry for this symbol in the .dynsym section.  The dynamic
858      object will contain position independent code, so all references
859      from the dynamic object to this symbol will go through the global
860      offset table.  The dynamic linker will use the .dynsym entry to
861      determine the address it must put in the global offset table, so
862      both the dynamic object and the regular object will refer to the
863      same memory location for the variable.  */
864
865   s = bfd_get_section_by_name (dynobj, ".dynbss");
866   BFD_ASSERT (s != NULL);
867
868   /* We must generate a R_SPARC_COPY reloc to tell the dynamic linker
869      to copy the initial value out of the dynamic object and into the
870      runtime process image.  We need to remember the offset into the
871      .rel.bss section we are going to use.  */
872   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
873     {
874       asection *srel;
875
876       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
877       BFD_ASSERT (srel != NULL);
878       srel->_raw_size += sizeof (Elf32_External_Rela);
879       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
880     }
881
882   /* We need to figure out the alignment required for this symbol.  I
883      have no idea how ELF linkers handle this.  */
884   power_of_two = bfd_log2 (h->size);
885   if (power_of_two > 3)
886     power_of_two = 3;
887
888   /* Apply the required alignment.  */
889   s->_raw_size = BFD_ALIGN (s->_raw_size,
890                             (bfd_size_type) (1 << power_of_two));
891   if (power_of_two > bfd_get_section_alignment (dynobj, s))
892     {
893       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
894         return false;
895     }
896
897   /* Define the symbol as being at this point in the section.  */
898   h->root.u.def.section = s;
899   h->root.u.def.value = s->_raw_size;
900
901   /* Increment the section size to make room for the symbol.  */
902   s->_raw_size += h->size;
903
904   return true;
905 }
906
907 /* Set the sizes of the dynamic sections.  */
908
909 static boolean
910 elf32_sparc_size_dynamic_sections (output_bfd, info)
911      bfd *output_bfd;
912      struct bfd_link_info *info;
913 {
914   bfd *dynobj;
915   asection *s;
916   boolean reltext;
917   boolean relplt;
918
919   dynobj = elf_hash_table (info)->dynobj;
920   BFD_ASSERT (dynobj != NULL);
921
922   if (elf_hash_table (info)->dynamic_sections_created)
923     {
924       /* Set the contents of the .interp section to the interpreter.  */
925       if (! info->shared)
926         {
927           s = bfd_get_section_by_name (dynobj, ".interp");
928           BFD_ASSERT (s != NULL);
929           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
930           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
931         }
932
933       /* Make space for the trailing nop in .plt.  */
934       s = bfd_get_section_by_name (dynobj, ".plt");
935       BFD_ASSERT (s != NULL);
936       if (s->_raw_size > 0)
937         s->_raw_size += 4;
938     }
939   else
940     {
941       /* We may have created entries in the .rela.got section.
942          However, if we are not creating the dynamic sections, we will
943          not actually use these entries.  Reset the size of .rela.got,
944          which will cause it to get stripped from the output file
945          below.  */
946       s = bfd_get_section_by_name (dynobj, ".rela.got");
947       if (s != NULL)
948         s->_raw_size = 0;
949     }
950
951   /* The check_relocs and adjust_dynamic_symbol entry points have
952      determined the sizes of the various dynamic sections.  Allocate
953      memory for them.  */
954   reltext = false;
955   relplt = false;
956   for (s = dynobj->sections; s != NULL; s = s->next)
957     {
958       const char *name;
959       boolean strip;
960
961       if ((s->flags & SEC_LINKER_CREATED) == 0)
962         continue;
963
964       /* It's OK to base decisions on the section name, because none
965          of the dynobj section names depend upon the input files.  */
966       name = bfd_get_section_name (dynobj, s);
967
968       strip = false;
969
970       if (strncmp (name, ".rela", 5) == 0)
971         {
972           if (s->_raw_size == 0)
973             {
974               /* If we don't need this section, strip it from the
975                  output file.  This is to handle .rela.bss and
976                  .rel.plt.  We must create it in
977                  create_dynamic_sections, because it must be created
978                  before the linker maps input sections to output
979                  sections.  The linker does that before
980                  adjust_dynamic_symbol is called, and it is that
981                  function which decides whether anything needs to go
982                  into these sections.  */
983               strip = true;
984             }
985           else
986             {
987               const char *outname;
988               asection *target;
989
990               /* If this relocation section applies to a read only
991                  section, then we probably need a DT_TEXTREL entry.  */
992               outname = bfd_get_section_name (output_bfd,
993                                               s->output_section);
994               target = bfd_get_section_by_name (output_bfd, outname + 5);
995               if (target != NULL
996                   && (target->flags & SEC_READONLY) != 0
997                   && (target->flags & SEC_ALLOC) != 0)
998                 reltext = true;
999
1000               if (strcmp (name, ".rela.plt") == 0)
1001                 relplt = true;
1002
1003               /* We use the reloc_count field as a counter if we need
1004                  to copy relocs into the output file.  */
1005               s->reloc_count = 0;
1006             }
1007         }
1008       else if (strcmp (name, ".plt") != 0
1009                && strcmp (name, ".got") != 0)
1010         {
1011           /* It's not one of our sections, so don't allocate space.  */
1012           continue;
1013         }
1014
1015       if (strip)
1016         {
1017           _bfd_strip_section_from_output (info, s);
1018           continue;
1019         }
1020
1021       /* Allocate memory for the section contents.  */
1022       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1023       if (s->contents == NULL && s->_raw_size != 0)
1024         return false;
1025     }
1026
1027   if (elf_hash_table (info)->dynamic_sections_created)
1028     {
1029       /* Add some entries to the .dynamic section.  We fill in the
1030          values later, in elf32_sparc_finish_dynamic_sections, but we
1031          must add the entries now so that we get the correct size for
1032          the .dynamic section.  The DT_DEBUG entry is filled in by the
1033          dynamic linker and used by the debugger.  */
1034       if (! info->shared)
1035         {
1036           if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1037             return false;
1038         }
1039
1040       if (relplt)
1041         {
1042           if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1043               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1044               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1045               || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1046             return false;
1047         }
1048
1049       if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1050           || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1051           || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1052                                             sizeof (Elf32_External_Rela)))
1053         return false;
1054
1055       if (reltext)
1056         {
1057           if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1058             return false;
1059           info->flags |= DF_TEXTREL;
1060         }
1061     }
1062
1063   return true;
1064 }
1065
1066
1067 #define SET_SEC_DO_RELAX(section) do { elf_section_data(section)->tdata = (void *)1; } while (0)
1068 #define SEC_DO_RELAX(section) (elf_section_data(section)->tdata == (void *)1)
1069
1070 /*ARGSUSED*/
1071 static boolean
1072 elf32_sparc_relax_section (abfd, section, link_info, again)
1073      bfd *abfd ATTRIBUTE_UNUSED;
1074      asection *section ATTRIBUTE_UNUSED;
1075      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1076      boolean *again;
1077 {
1078   *again = false;
1079   SET_SEC_DO_RELAX (section);
1080   return true;
1081 }
1082
1083 /* Relocate a SPARC ELF section.  */
1084
1085 static boolean
1086 elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
1087                               contents, relocs, local_syms, local_sections)
1088      bfd *output_bfd;
1089      struct bfd_link_info *info;
1090      bfd *input_bfd;
1091      asection *input_section;
1092      bfd_byte *contents;
1093      Elf_Internal_Rela *relocs;
1094      Elf_Internal_Sym *local_syms;
1095      asection **local_sections;
1096 {
1097   bfd *dynobj;
1098   Elf_Internal_Shdr *symtab_hdr;
1099   struct elf_link_hash_entry **sym_hashes;
1100   bfd_vma *local_got_offsets;
1101   bfd_vma got_base;
1102   asection *sgot;
1103   asection *splt;
1104   asection *sreloc;
1105   Elf_Internal_Rela *rel;
1106   Elf_Internal_Rela *relend;
1107
1108   dynobj = elf_hash_table (info)->dynobj;
1109   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1110   sym_hashes = elf_sym_hashes (input_bfd);
1111   local_got_offsets = elf_local_got_offsets (input_bfd);
1112
1113   if (elf_hash_table (info)->hgot == NULL)
1114     got_base = 0;
1115   else
1116     got_base = elf_hash_table (info)->hgot->root.u.def.value;
1117
1118   sgot = NULL;
1119   splt = NULL;
1120   sreloc = NULL;
1121
1122   rel = relocs;
1123   relend = relocs + input_section->reloc_count;
1124   for (; rel < relend; rel++)
1125     {
1126       int r_type;
1127       reloc_howto_type *howto;
1128       unsigned long r_symndx;
1129       struct elf_link_hash_entry *h;
1130       Elf_Internal_Sym *sym;
1131       asection *sec;
1132       bfd_vma relocation;
1133       bfd_reloc_status_type r;
1134
1135       r_type = ELF32_R_TYPE (rel->r_info);
1136
1137       if (r_type == R_SPARC_GNU_VTINHERIT 
1138           || r_type == R_SPARC_GNU_VTENTRY)
1139         continue;
1140
1141       if (r_type < 0 || r_type >= (int) R_SPARC_max_std)
1142         {
1143           bfd_set_error (bfd_error_bad_value);
1144           return false;
1145         }
1146       howto = _bfd_sparc_elf_howto_table + r_type;
1147
1148       r_symndx = ELF32_R_SYM (rel->r_info);
1149
1150       if (info->relocateable)
1151         {
1152           /* This is a relocateable link.  We don't have to change
1153              anything, unless the reloc is against a section symbol,
1154              in which case we have to adjust according to where the
1155              section symbol winds up in the output section.  */
1156           if (r_symndx < symtab_hdr->sh_info)
1157             {
1158               sym = local_syms + r_symndx;
1159               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1160                 {
1161                   sec = local_sections[r_symndx];
1162                   rel->r_addend += sec->output_offset + sym->st_value;
1163                 }
1164             }
1165
1166           continue;
1167         }
1168
1169       /* This is a final link.  */
1170       h = NULL;
1171       sym = NULL;
1172       sec = NULL;
1173       if (r_symndx < symtab_hdr->sh_info)
1174         {
1175           sym = local_syms + r_symndx;
1176           sec = local_sections[r_symndx];
1177           relocation = (sec->output_section->vma
1178                         + sec->output_offset
1179                         + sym->st_value);
1180         }
1181       else
1182         {
1183           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1184           while (h->root.type == bfd_link_hash_indirect
1185                  || h->root.type == bfd_link_hash_warning)
1186             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1187           if (h->root.type == bfd_link_hash_defined
1188               || h->root.type == bfd_link_hash_defweak)
1189             {
1190               sec = h->root.u.def.section;
1191               if ((r_type == R_SPARC_WPLT30
1192                    && h->plt.offset != (bfd_vma) -1)
1193                   || ((r_type == R_SPARC_GOT10
1194                        || r_type == R_SPARC_GOT13
1195                        || r_type == R_SPARC_GOT22)
1196                       && elf_hash_table (info)->dynamic_sections_created
1197                       && (! info->shared
1198                           || (! info->symbolic && h->dynindx != -1)
1199                           || (h->elf_link_hash_flags
1200                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
1201                   || (info->shared
1202                       && ((! info->symbolic && h->dynindx != -1)
1203                           || (h->elf_link_hash_flags
1204                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
1205                       && (r_type == R_SPARC_8
1206                           || r_type == R_SPARC_16
1207                           || r_type == R_SPARC_32
1208                           || r_type == R_SPARC_DISP8
1209                           || r_type == R_SPARC_DISP16
1210                           || r_type == R_SPARC_DISP32
1211                           || r_type == R_SPARC_WDISP30
1212                           || r_type == R_SPARC_WDISP22
1213                           || r_type == R_SPARC_WDISP19
1214                           || r_type == R_SPARC_WDISP16
1215                           || r_type == R_SPARC_HI22
1216                           || r_type == R_SPARC_22
1217                           || r_type == R_SPARC_13
1218                           || r_type == R_SPARC_LO10
1219                           || r_type == R_SPARC_UA32
1220                           || ((r_type == R_SPARC_PC10
1221                                || r_type == R_SPARC_PC22)
1222                               && strcmp (h->root.root.string,
1223                                          "_GLOBAL_OFFSET_TABLE_") != 0))))
1224                 {
1225                   /* In these cases, we don't need the relocation
1226                      value.  We check specially because in some
1227                      obscure cases sec->output_section will be NULL.  */
1228                   relocation = 0;
1229                 }
1230               else
1231                 relocation = (h->root.u.def.value
1232                               + sec->output_section->vma
1233                               + sec->output_offset);
1234             }
1235           else if (h->root.type == bfd_link_hash_undefweak)
1236             relocation = 0;
1237           else if (info->shared && !info->symbolic
1238                    && !info->no_undefined
1239                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1240             relocation = 0;
1241           else
1242             {
1243               if (! ((*info->callbacks->undefined_symbol)
1244                      (info, h->root.root.string, input_bfd,
1245                       input_section, rel->r_offset,
1246                       (!info->shared || info->no_undefined
1247                        || ELF_ST_VISIBILITY (h->other)))))
1248                 return false;
1249               relocation = 0;
1250             }
1251         }
1252
1253       switch (r_type)
1254         {
1255         case R_SPARC_GOT10:
1256         case R_SPARC_GOT13:
1257         case R_SPARC_GOT22:
1258           /* Relocation is to the entry for this symbol in the global
1259              offset table.  */
1260           if (sgot == NULL)
1261             {
1262               sgot = bfd_get_section_by_name (dynobj, ".got");
1263               BFD_ASSERT (sgot != NULL);
1264             }
1265
1266           if (h != NULL)
1267             {
1268               bfd_vma off;
1269
1270               off = h->got.offset;
1271               BFD_ASSERT (off != (bfd_vma) -1);
1272
1273               if (! elf_hash_table (info)->dynamic_sections_created
1274                   || (info->shared
1275                       && (info->symbolic || h->dynindx == -1)
1276                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1277                 {
1278                   /* This is actually a static link, or it is a
1279                      -Bsymbolic link and the symbol is defined
1280                      locally, or the symbol was forced to be local
1281                      because of a version file.  We must initialize
1282                      this entry in the global offset table.  Since the
1283                      offset must always be a multiple of 4, we use the
1284                      least significant bit to record whether we have
1285                      initialized it already.
1286
1287                      When doing a dynamic link, we create a .rela.got
1288                      relocation entry to initialize the value.  This
1289                      is done in the finish_dynamic_symbol routine.  */
1290                   if ((off & 1) != 0)
1291                     off &= ~1;
1292                   else
1293                     {
1294                       bfd_put_32 (output_bfd, relocation,
1295                                   sgot->contents + off);
1296                       h->got.offset |= 1;
1297                     }
1298                 }
1299
1300               relocation = sgot->output_offset + off - got_base;
1301             }
1302           else
1303             {
1304               bfd_vma off;
1305
1306               BFD_ASSERT (local_got_offsets != NULL
1307                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1308
1309               off = local_got_offsets[r_symndx];
1310
1311               /* The offset must always be a multiple of 4.  We use
1312                  the least significant bit to record whether we have
1313                  already processed this entry.  */
1314               if ((off & 1) != 0)
1315                 off &= ~1;
1316               else
1317                 {
1318                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1319
1320                   if (info->shared)
1321                     {
1322                       asection *srelgot;
1323                       Elf_Internal_Rela outrel;
1324
1325                       /* We need to generate a R_SPARC_RELATIVE reloc
1326                          for the dynamic linker.  */
1327                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1328                       BFD_ASSERT (srelgot != NULL);
1329
1330                       outrel.r_offset = (sgot->output_section->vma
1331                                          + sgot->output_offset
1332                                          + off);
1333                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1334                       outrel.r_addend = 0;
1335                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1336                                                  (((Elf32_External_Rela *)
1337                                                    srelgot->contents)
1338                                                   + srelgot->reloc_count));
1339                       ++srelgot->reloc_count;
1340                     }
1341
1342                   local_got_offsets[r_symndx] |= 1;
1343                 }
1344
1345               relocation = sgot->output_offset + off - got_base;
1346             }
1347
1348           break;
1349
1350         case R_SPARC_WPLT30:
1351           /* Relocation is to the entry for this symbol in the
1352              procedure linkage table.  */
1353
1354           /* The Solaris native assembler will generate a WPLT30 reloc
1355              for a local symbol if you assemble a call from one
1356              section to another when using -K pic.  We treat it as
1357              WDISP30.  */
1358           if (h == NULL)
1359             break;
1360
1361           if (h->plt.offset == (bfd_vma) -1)
1362             {
1363               /* We didn't make a PLT entry for this symbol.  This
1364                  happens when statically linking PIC code, or when
1365                  using -Bsymbolic.  */
1366               break;
1367             }
1368
1369           if (splt == NULL)
1370             {
1371               splt = bfd_get_section_by_name (dynobj, ".plt");
1372               BFD_ASSERT (splt != NULL);
1373             }
1374
1375           relocation = (splt->output_section->vma
1376                         + splt->output_offset
1377                         + h->plt.offset);
1378           break;
1379
1380         case R_SPARC_PC10:
1381         case R_SPARC_PC22:
1382           if (h != NULL
1383               && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1384             break;
1385           /* Fall through.  */
1386         case R_SPARC_DISP8:
1387         case R_SPARC_DISP16:
1388         case R_SPARC_DISP32:
1389         case R_SPARC_WDISP30:
1390         case R_SPARC_WDISP22:
1391         case R_SPARC_WDISP19:
1392         case R_SPARC_WDISP16:
1393           if (h == NULL
1394               || (info->symbolic
1395                   && (h->elf_link_hash_flags
1396                       & ELF_LINK_HASH_DEF_REGULAR) != 0))
1397             break;
1398           /* Fall through.  */
1399         case R_SPARC_8:
1400         case R_SPARC_16:
1401         case R_SPARC_32:
1402         case R_SPARC_HI22:
1403         case R_SPARC_22:
1404         case R_SPARC_13:
1405         case R_SPARC_LO10:
1406         case R_SPARC_UA32:
1407           if (info->shared)
1408             {
1409               Elf_Internal_Rela outrel;
1410               boolean skip;
1411
1412               /* When generating a shared object, these relocations
1413                  are copied into the output file to be resolved at run
1414                  time.  */
1415
1416               if (sreloc == NULL)
1417                 {
1418                   const char *name;
1419
1420                   name = (bfd_elf_string_from_elf_section
1421                           (input_bfd,
1422                            elf_elfheader (input_bfd)->e_shstrndx,
1423                            elf_section_data (input_section)->rel_hdr.sh_name));
1424                   if (name == NULL)
1425                     return false;
1426
1427                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1428                               && strcmp (bfd_get_section_name (input_bfd,
1429                                                                input_section),
1430                                          name + 5) == 0);
1431
1432                   sreloc = bfd_get_section_by_name (dynobj, name);
1433                   BFD_ASSERT (sreloc != NULL);
1434                 }
1435
1436               skip = false;
1437
1438               if (elf_section_data (input_section)->stab_info == NULL)
1439                 outrel.r_offset = rel->r_offset;
1440               else
1441                 {
1442                   bfd_vma off;
1443
1444                   off = (_bfd_stab_section_offset
1445                          (output_bfd, &elf_hash_table (info)->stab_info,
1446                           input_section,
1447                           &elf_section_data (input_section)->stab_info,
1448                           rel->r_offset));
1449                   if (off == (bfd_vma) -1)
1450                     skip = true;
1451                   outrel.r_offset = off;
1452                 }
1453
1454               outrel.r_offset += (input_section->output_section->vma
1455                                   + input_section->output_offset);
1456
1457               if (skip)
1458                 memset (&outrel, 0, sizeof outrel);
1459               /* h->dynindx may be -1 if the symbol was marked to
1460                  become local.  */
1461               else if (h != NULL
1462                        && ((! info->symbolic && h->dynindx != -1)
1463                            || (h->elf_link_hash_flags
1464                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
1465                 {
1466                   BFD_ASSERT (h->dynindx != -1);
1467                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1468                   outrel.r_addend = rel->r_addend;
1469                 }
1470               else
1471                 {
1472                   if (r_type == R_SPARC_32)
1473                     {
1474                       outrel.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1475                       outrel.r_addend = relocation + rel->r_addend;
1476                     }
1477                   else
1478                     {
1479                       long indx;
1480
1481                       if (h == NULL)
1482                         sec = local_sections[r_symndx];
1483                       else
1484                         {
1485                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
1486                                       || (h->root.type
1487                                           == bfd_link_hash_defweak));
1488                           sec = h->root.u.def.section;
1489                         }
1490                       if (sec != NULL && bfd_is_abs_section (sec))
1491                         indx = 0;
1492                       else if (sec == NULL || sec->owner == NULL)
1493                         {
1494                           bfd_set_error (bfd_error_bad_value);
1495                           return false;
1496                         }
1497                       else
1498                         {
1499                           asection *osec;
1500
1501                           osec = sec->output_section;
1502                           indx = elf_section_data (osec)->dynindx;
1503
1504                           /* FIXME: we really should be able to link non-pic
1505                              shared libraries.  */
1506                           if (indx == 0)
1507                             {
1508                               BFD_FAIL ();
1509                               (*_bfd_error_handler)
1510                                 (_("%s: probably compiled without -fPIC?"),
1511                                  bfd_get_filename (input_bfd));
1512                               bfd_set_error (bfd_error_bad_value);
1513                               return false;
1514                             }
1515                         }
1516
1517                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1518                       outrel.r_addend = relocation + rel->r_addend;
1519                     }
1520                 }
1521
1522               bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1523                                          (((Elf32_External_Rela *)
1524                                            sreloc->contents)
1525                                           + sreloc->reloc_count));
1526               ++sreloc->reloc_count;
1527
1528               /* This reloc will be computed at runtime, so there's no
1529                  need to do anything now, unless this is a RELATIVE
1530                  reloc in an unallocated section.  */
1531               if (skip
1532                   || (input_section->flags & SEC_ALLOC) != 0
1533                   || ELF32_R_TYPE (outrel.r_info) != R_SPARC_RELATIVE)
1534                 continue;
1535             }
1536           break;
1537
1538         default:
1539           break;
1540         }
1541
1542       r = bfd_reloc_continue;
1543       if (r_type == R_SPARC_WDISP16)
1544         {
1545           bfd_vma x;
1546
1547           relocation += rel->r_addend;
1548           relocation -= (input_section->output_section->vma
1549                          + input_section->output_offset);
1550           relocation -= rel->r_offset;
1551
1552           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1553           x |= ((((relocation >> 2) & 0xc000) << 6)
1554                 | ((relocation >> 2) & 0x3fff));
1555           bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1556
1557           if ((bfd_signed_vma) relocation < - 0x40000
1558               || (bfd_signed_vma) relocation > 0x3ffff)
1559             r = bfd_reloc_overflow;
1560           else
1561             r = bfd_reloc_ok;
1562         }
1563       else if (r_type == R_SPARC_REV32)
1564         {
1565           bfd_vma x;
1566
1567           relocation = relocation + rel->r_addend;
1568
1569           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1570           x = x + relocation;
1571           bfd_putl32 (/*input_bfd,*/ x, contents + rel->r_offset);
1572           r = bfd_reloc_ok;
1573         }
1574       else if ((r_type == R_SPARC_WDISP30 || r_type == R_SPARC_WPLT30)
1575                && SEC_DO_RELAX (input_section)
1576                && rel->r_offset + 4 < input_section->_raw_size)
1577         {
1578 #define G0              0
1579 #define O7              15
1580 #define XCC             (2 << 20)
1581 #define COND(x)         (((x)&0xf)<<25)
1582 #define CONDA           COND(0x8)
1583 #define INSN_BPA        (F2(0,1) | CONDA | BPRED | XCC)
1584 #define INSN_BA         (F2(0,2) | CONDA)
1585 #define INSN_OR         F3(2, 0x2, 0)
1586 #define INSN_NOP        F2(0,4)
1587
1588           bfd_vma x, y;
1589
1590           /* If the instruction is a call with either:
1591              restore
1592              arithmetic instruction with rd == %o7
1593              where rs1 != %o7 and rs2 if it is register != %o7
1594              then we can optimize if the call destination is near
1595              by changing the call into a branch always.  */
1596           x = bfd_get_32 (input_bfd, contents + rel->r_offset);
1597           y = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
1598           if ((x & OP(~0)) == OP(1) && (y & OP(~0)) == OP(2))
1599             {
1600               if (((y & OP3(~0)) == OP3(0x3d) /* restore */
1601                    || ((y & OP3(0x28)) == 0 /* arithmetic */
1602                        && (y & RD(~0)) == RD(O7)))
1603                   && (y & RS1(~0)) != RS1(O7)
1604                   && ((y & F3I(~0))
1605                       || (y & RS2(~0)) != RS2(O7)))
1606                 {
1607                   bfd_vma reloc;
1608
1609                   reloc = relocation + rel->r_addend - rel->r_offset;
1610                   reloc -= (input_section->output_section->vma
1611                            + input_section->output_offset);
1612
1613                   /* Ensure the reloc fits into simm22.  */
1614                   if ((reloc & 3) == 0
1615                       && ((reloc & ~(bfd_vma)0x7fffff) == 0
1616                           || ((reloc | 0x7fffff) == ~(bfd_vma)0)))
1617                     {
1618                       reloc >>= 2;
1619                 
1620                       /* Check whether it fits into simm19 on v9.  */
1621                       if (((reloc & 0x3c0000) == 0
1622                            || (reloc & 0x3c0000) == 0x3c0000)
1623                           && (elf_elfheader (output_bfd)->e_flags & EF_SPARC_32PLUS))
1624                         x = INSN_BPA | (reloc & 0x7ffff); /* ba,pt %xcc */
1625                       else
1626                         x = INSN_BA | (reloc & 0x3fffff); /* ba */
1627                       bfd_put_32 (input_bfd, x, contents + rel->r_offset);
1628                       r = bfd_reloc_ok;
1629                       if (rel->r_offset >= 4
1630                           && (y & (0xffffffff ^ RS1(~0)))
1631                               == (INSN_OR | RD(O7) | RS2(G0)))
1632                         {
1633                           bfd_vma z;
1634                           unsigned int reg;
1635
1636                           z = bfd_get_32 (input_bfd,
1637                                           contents + rel->r_offset - 4);
1638                           if ((z & (0xffffffff ^ RD(~0)))
1639                               != (INSN_OR | RS1(O7) | RS2(G0)))
1640                             break;
1641
1642                           /* The sequence was
1643                              or %o7, %g0, %rN
1644                              call foo
1645                              or %rN, %g0, %o7
1646
1647                              If call foo was replaced with ba, replace
1648                              or %rN, %g0, %o7 with nop.  */
1649
1650                           reg = (y & RS1(~0)) >> 14;
1651                           if (reg != ((z & RD(~0)) >> 25)
1652                               || reg == G0 || reg == O7)
1653                             break;
1654
1655                           bfd_put_32 (input_bfd, INSN_NOP,
1656                                       contents + rel->r_offset + 4);
1657                         }
1658
1659                     }
1660                 }
1661             }
1662         }
1663
1664       if (r == bfd_reloc_continue)
1665         r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1666                                       contents, rel->r_offset,
1667                                       relocation, rel->r_addend);
1668
1669
1670       if (r != bfd_reloc_ok)
1671         {
1672           switch (r)
1673             {
1674             default:
1675             case bfd_reloc_outofrange:
1676               abort ();
1677             case bfd_reloc_overflow:
1678               {
1679                 const char *name;
1680
1681                 if (h != NULL)
1682                   name = h->root.root.string;
1683                 else
1684                   {
1685                     name = bfd_elf_string_from_elf_section (input_bfd,
1686                                                             symtab_hdr->sh_link,
1687                                                             sym->st_name);
1688                     if (name == NULL)
1689                       return false;
1690                     if (*name == '\0')
1691                       name = bfd_section_name (input_bfd, sec);
1692                   }
1693                 if (! ((*info->callbacks->reloc_overflow)
1694                        (info, name, howto->name, (bfd_vma) 0,
1695                         input_bfd, input_section, rel->r_offset)))
1696                   return false;
1697               }
1698               break;
1699             }
1700         }
1701     }
1702
1703   return true;
1704 }
1705
1706 /* Finish up dynamic symbol handling.  We set the contents of various
1707    dynamic sections here.  */
1708
1709 static boolean
1710 elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
1711      bfd *output_bfd;
1712      struct bfd_link_info *info;
1713      struct elf_link_hash_entry *h;
1714      Elf_Internal_Sym *sym;
1715 {
1716   bfd *dynobj;
1717
1718   dynobj = elf_hash_table (info)->dynobj;
1719
1720   if (h->plt.offset != (bfd_vma) -1)
1721     {
1722       asection *splt;
1723       asection *srela;
1724       Elf_Internal_Rela rela;
1725
1726       /* This symbol has an entry in the procedure linkage table.  Set
1727          it up.  */
1728
1729       BFD_ASSERT (h->dynindx != -1);
1730
1731       splt = bfd_get_section_by_name (dynobj, ".plt");
1732       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1733       BFD_ASSERT (splt != NULL && srela != NULL);
1734
1735       /* Fill in the entry in the procedure linkage table.  */
1736       bfd_put_32 (output_bfd,
1737                   PLT_ENTRY_WORD0 + h->plt.offset,
1738                   splt->contents + h->plt.offset);
1739       bfd_put_32 (output_bfd,
1740                   (PLT_ENTRY_WORD1
1741                    + (((- (h->plt.offset + 4)) >> 2) & 0x3fffff)),
1742                   splt->contents + h->plt.offset + 4);
1743       bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
1744                   splt->contents + h->plt.offset + 8);
1745
1746       /* Fill in the entry in the .rela.plt section.  */
1747       rela.r_offset = (splt->output_section->vma
1748                        + splt->output_offset
1749                        + h->plt.offset);
1750       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
1751       rela.r_addend = 0;
1752       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1753                                  ((Elf32_External_Rela *) srela->contents
1754                                   + h->plt.offset / PLT_ENTRY_SIZE - 4));
1755
1756       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1757         {
1758           /* Mark the symbol as undefined, rather than as defined in
1759              the .plt section.  Leave the value alone.  */
1760           sym->st_shndx = SHN_UNDEF;
1761         }
1762     }
1763
1764   if (h->got.offset != (bfd_vma) -1)
1765     {
1766       asection *sgot;
1767       asection *srela;
1768       Elf_Internal_Rela rela;
1769
1770       /* This symbol has an entry in the global offset table.  Set it
1771          up.  */
1772
1773       sgot = bfd_get_section_by_name (dynobj, ".got");
1774       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1775       BFD_ASSERT (sgot != NULL && srela != NULL);
1776
1777       rela.r_offset = (sgot->output_section->vma
1778                        + sgot->output_offset
1779                        + (h->got.offset &~ 1));
1780
1781       /* If this is a -Bsymbolic link, and the symbol is defined
1782          locally, we just want to emit a RELATIVE reloc.  Likewise if
1783          the symbol was forced to be local because of a version file.
1784          The entry in the global offset table will already have been
1785          initialized in the relocate_section function.  */
1786       if (info->shared
1787           && (info->symbolic || h->dynindx == -1)
1788           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1789         rela.r_info = ELF32_R_INFO (0, R_SPARC_RELATIVE);
1790       else
1791         {
1792           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1793           rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_GLOB_DAT);
1794         }
1795
1796       rela.r_addend = 0;
1797       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1798                                  ((Elf32_External_Rela *) srela->contents
1799                                   + srela->reloc_count));
1800       ++srela->reloc_count;
1801     }
1802
1803   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1804     {
1805       asection *s;
1806       Elf_Internal_Rela rela;
1807
1808       /* This symbols needs a copy reloc.  Set it up.  */
1809
1810       BFD_ASSERT (h->dynindx != -1);
1811
1812       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1813                                    ".rela.bss");
1814       BFD_ASSERT (s != NULL);
1815
1816       rela.r_offset = (h->root.u.def.value
1817                        + h->root.u.def.section->output_section->vma
1818                        + h->root.u.def.section->output_offset);
1819       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
1820       rela.r_addend = 0;
1821       bfd_elf32_swap_reloca_out (output_bfd, &rela,
1822                                  ((Elf32_External_Rela *) s->contents
1823                                   + s->reloc_count));
1824       ++s->reloc_count;
1825     }
1826
1827   /* Mark some specially defined symbols as absolute.  */
1828   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1829       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
1830       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
1831     sym->st_shndx = SHN_ABS;
1832
1833   return true;
1834 }
1835
1836 /* Finish up the dynamic sections.  */
1837
1838 static boolean
1839 elf32_sparc_finish_dynamic_sections (output_bfd, info)
1840      bfd *output_bfd;
1841      struct bfd_link_info *info;
1842 {
1843   bfd *dynobj;
1844   asection *sdyn;
1845   asection *sgot;
1846
1847   dynobj = elf_hash_table (info)->dynobj;
1848
1849   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1850
1851   if (elf_hash_table (info)->dynamic_sections_created)
1852     {
1853       asection *splt;
1854       Elf32_External_Dyn *dyncon, *dynconend;
1855
1856       splt = bfd_get_section_by_name (dynobj, ".plt");
1857       BFD_ASSERT (splt != NULL && sdyn != NULL);
1858
1859       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1860       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1861       for (; dyncon < dynconend; dyncon++)
1862         {
1863           Elf_Internal_Dyn dyn;
1864           const char *name;
1865           boolean size;
1866
1867           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1868
1869           switch (dyn.d_tag)
1870             {
1871             case DT_PLTGOT:   name = ".plt"; size = false; break;
1872             case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
1873             case DT_JMPREL:   name = ".rela.plt"; size = false; break;
1874             default:      name = NULL; size = false; break;
1875             }
1876
1877           if (name != NULL)
1878             {
1879               asection *s;
1880
1881               s = bfd_get_section_by_name (output_bfd, name);
1882               if (s == NULL)
1883                 dyn.d_un.d_val = 0;
1884               else
1885                 {
1886                   if (! size)
1887                     dyn.d_un.d_ptr = s->vma;
1888                   else
1889                     {
1890                       if (s->_cooked_size != 0)
1891                         dyn.d_un.d_val = s->_cooked_size;
1892                       else
1893                         dyn.d_un.d_val = s->_raw_size;
1894                     }
1895                 }
1896               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1897             }
1898         }
1899
1900       /* Clear the first four entries in the procedure linkage table,
1901          and put a nop in the last four bytes.  */
1902       if (splt->_raw_size > 0)
1903         {
1904           memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
1905           bfd_put_32 (output_bfd, SPARC_NOP,
1906                       splt->contents + splt->_raw_size - 4);
1907         }
1908
1909       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1910         PLT_ENTRY_SIZE;
1911     }
1912
1913   /* Set the first entry in the global offset table to the address of
1914      the dynamic section.  */
1915   sgot = bfd_get_section_by_name (dynobj, ".got");
1916   BFD_ASSERT (sgot != NULL);
1917   if (sgot->_raw_size > 0)
1918     {
1919       if (sdyn == NULL)
1920         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1921       else
1922         bfd_put_32 (output_bfd,
1923                     sdyn->output_section->vma + sdyn->output_offset,
1924                     sgot->contents);
1925     }
1926
1927   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1928
1929   return true;
1930 }
1931 \f
1932 /* Functions for dealing with the e_flags field.
1933
1934    We don't define set_private_flags or copy_private_bfd_data because
1935    the only currently defined values are based on the bfd mach number,
1936    so we use the latter instead and defer setting e_flags until the
1937    file is written out.  */
1938
1939 /* Merge backend specific data from an object file to the output
1940    object file when linking.  */
1941
1942 static boolean
1943 elf32_sparc_merge_private_bfd_data (ibfd, obfd)
1944      bfd *ibfd;
1945      bfd *obfd;
1946 {
1947   boolean error;
1948   /* FIXME: This should not be static.  */
1949   static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
1950
1951   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1952       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1953     return true;
1954
1955   error = false;
1956
1957 #if 0
1958   /* ??? The native linker doesn't do this so we can't (otherwise gcc would
1959      have to know which linker is being used).  Instead, the native linker
1960      bumps up the architecture level when it has to.  However, I still think
1961      warnings like these are good, so it would be nice to have them turned on
1962      by some option.  */
1963
1964   /* If the output machine is normal sparc, we can't allow v9 input files.  */
1965   if (bfd_get_mach (obfd) == bfd_mach_sparc
1966       && (bfd_get_mach (ibfd) == bfd_mach_sparc_v8plus
1967           || bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa))
1968     {
1969       error = true;
1970       (*_bfd_error_handler)
1971         (_("%s: compiled for a v8plus system and target is v8"),
1972          bfd_get_filename (ibfd));
1973     }
1974   /* If the output machine is v9, we can't allow v9+vis input files.  */
1975   if (bfd_get_mach (obfd) == bfd_mach_sparc_v8plus
1976       && bfd_get_mach (ibfd) == bfd_mach_sparc_v8plusa)
1977     {
1978       error = true;
1979       (*_bfd_error_handler)
1980         (_("%s: compiled for a v8plusa system and target is v8plus"),
1981          bfd_get_filename (ibfd));
1982     }
1983 #else
1984   if (bfd_get_mach (ibfd) >= bfd_mach_sparc_v9)
1985     {
1986       error = true;
1987       (*_bfd_error_handler)
1988         (_("%s: compiled for a 64 bit system and target is 32 bit"),
1989          bfd_get_filename (ibfd));
1990     }
1991   else if ((ibfd->flags & DYNAMIC) == 0)
1992     {
1993       if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
1994         bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
1995     }
1996 #endif
1997
1998   if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
1999        != previous_ibfd_e_flags)
2000       && previous_ibfd_e_flags != (unsigned long) -1)
2001     {
2002       (*_bfd_error_handler)
2003         (_("%s: linking little endian files with big endian files"),
2004          bfd_get_filename (ibfd));
2005       error = true;
2006     }
2007   previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
2008
2009   if (error)
2010     {
2011       bfd_set_error (bfd_error_bad_value);
2012       return false;
2013     }
2014
2015   return true;
2016 }
2017 \f
2018 /* Set the right machine number.  */
2019
2020 static boolean
2021 elf32_sparc_object_p (abfd)
2022      bfd *abfd;
2023 {
2024   if (elf_elfheader (abfd)->e_machine == EM_SPARC32PLUS)
2025     {
2026       if (elf_elfheader (abfd)->e_flags & EF_SPARC_SUN_US1)
2027         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2028                                           bfd_mach_sparc_v8plusa);
2029       else if (elf_elfheader (abfd)->e_flags & EF_SPARC_32PLUS)
2030         return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2031                                           bfd_mach_sparc_v8plus);
2032       else
2033         return false;
2034     }
2035   else if (elf_elfheader (abfd)->e_flags & EF_SPARC_LEDATA)
2036     return bfd_default_set_arch_mach (abfd, bfd_arch_sparc,
2037                                       bfd_mach_sparc_sparclite_le);
2038   else
2039     return bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
2040 }
2041
2042 /* The final processing done just before writing out the object file.
2043    We need to set the e_machine field appropriately.  */
2044
2045 static void
2046 elf32_sparc_final_write_processing (abfd, linker)
2047      bfd *abfd;
2048      boolean linker ATTRIBUTE_UNUSED;
2049 {
2050   switch (bfd_get_mach (abfd))
2051     {
2052     case bfd_mach_sparc :
2053       break; /* nothing to do */
2054     case bfd_mach_sparc_v8plus :
2055       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2056       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2057       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
2058       break;
2059     case bfd_mach_sparc_v8plusa :
2060       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
2061       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
2062       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
2063       break;
2064     case bfd_mach_sparc_sparclite_le :
2065       elf_elfheader (abfd)->e_machine = EM_SPARC;
2066       elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
2067       break;
2068     default :
2069       abort ();
2070       break;
2071     }
2072 }
2073 \f
2074 #define TARGET_BIG_SYM  bfd_elf32_sparc_vec
2075 #define TARGET_BIG_NAME "elf32-sparc"
2076 #define ELF_ARCH        bfd_arch_sparc
2077 #define ELF_MACHINE_CODE EM_SPARC
2078 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
2079 #define ELF_MAXPAGESIZE 0x10000
2080
2081 #define bfd_elf32_bfd_reloc_type_lookup elf32_sparc_reloc_type_lookup
2082 #define bfd_elf32_bfd_relax_section     elf32_sparc_relax_section
2083 #define elf_info_to_howto               elf32_sparc_info_to_howto
2084 #define elf_backend_create_dynamic_sections \
2085                                         _bfd_elf_create_dynamic_sections
2086 #define elf_backend_check_relocs        elf32_sparc_check_relocs
2087 #define elf_backend_adjust_dynamic_symbol \
2088                                         elf32_sparc_adjust_dynamic_symbol
2089 #define elf_backend_size_dynamic_sections \
2090                                         elf32_sparc_size_dynamic_sections
2091 #define elf_backend_relocate_section    elf32_sparc_relocate_section
2092 #define elf_backend_finish_dynamic_symbol \
2093                                         elf32_sparc_finish_dynamic_symbol
2094 #define elf_backend_finish_dynamic_sections \
2095                                         elf32_sparc_finish_dynamic_sections
2096 #define bfd_elf32_bfd_merge_private_bfd_data \
2097                                         elf32_sparc_merge_private_bfd_data
2098 #define elf_backend_object_p            elf32_sparc_object_p
2099 #define elf_backend_final_write_processing \
2100                                         elf32_sparc_final_write_processing
2101 #define elf_backend_gc_mark_hook        elf32_sparc_gc_mark_hook
2102 #define elf_backend_gc_sweep_hook       elf32_sparc_gc_sweep_hook
2103
2104 #define elf_backend_can_gc_sections 1
2105 #define elf_backend_want_got_plt 0
2106 #define elf_backend_plt_readonly 0
2107 #define elf_backend_want_plt_sym 1
2108 #define elf_backend_got_header_size 4
2109 #define elf_backend_plt_header_size (4*PLT_ENTRY_SIZE)
2110
2111 #include "elf32-target.h"
This page took 0.148114 seconds and 4 git commands to generate.