cbe20b5616050161b6846e1a33555f03fe136195
[external/binutils.git] / bfd / elf32-sparc.c
1 /* SPARC-specific support for 32-bit ELF
2    Copyright (C) 1993-2016 Free Software Foundation, Inc.
3
4    This file is part of BFD, the Binary File Descriptor library.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "sysdep.h"
22 #include "bfd.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 #include "elfxx-sparc.h"
29 #include "elf-vxworks.h"
30
31 /* Support for core dump NOTE sections.  */
32
33 static bfd_boolean
34 elf32_sparc_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
35 {
36   switch (note->descsz)
37     {
38     default:
39       return FALSE;
40
41     case 260:                   /* Solaris prpsinfo_t.  */
42       elf_tdata (abfd)->core->program
43         = _bfd_elfcore_strndup (abfd, note->descdata + 84, 16);
44       elf_tdata (abfd)->core->command
45         = _bfd_elfcore_strndup (abfd, note->descdata + 100, 80);
46       break;
47
48     case 336:                   /* Solaris psinfo_t.  */
49       elf_tdata (abfd)->core->program
50         = _bfd_elfcore_strndup (abfd, note->descdata + 88, 16);
51       elf_tdata (abfd)->core->command
52         = _bfd_elfcore_strndup (abfd, note->descdata + 104, 80);
53       break;
54     }
55
56   return TRUE;
57 }
58
59 /* Functions for dealing with the e_flags field.
60
61    We don't define set_private_flags or copy_private_bfd_data because
62    the only currently defined values are based on the bfd mach number,
63    so we use the latter instead and defer setting e_flags until the
64    file is written out.  */
65
66 /* Merge backend specific data from an object file to the output
67    object file when linking.  */
68
69 static bfd_boolean
70 elf32_sparc_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
71 {
72   bfd_boolean error;
73   unsigned long ibfd_mach;
74   /* FIXME: This should not be static.  */
75   static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
76
77   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
78       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
79     return TRUE;
80
81   error = FALSE;
82
83   ibfd_mach = bfd_get_mach (ibfd);
84   if (bfd_mach_sparc_64bit_p (ibfd_mach))
85     {
86       error = TRUE;
87       _bfd_error_handler
88         (_("%B: compiled for a 64 bit system and target is 32 bit"), ibfd);
89     }
90   else if ((ibfd->flags & DYNAMIC) == 0)
91     {
92       if (bfd_get_mach (obfd) < ibfd_mach)
93         bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
94     }
95
96   if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
97        != previous_ibfd_e_flags)
98       && previous_ibfd_e_flags != (unsigned long) -1)
99     {
100       _bfd_error_handler
101         (_("%B: linking little endian files with big endian files"), ibfd);
102       error = TRUE;
103     }
104   previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
105
106   if (error)
107     {
108       bfd_set_error (bfd_error_bad_value);
109       return FALSE;
110     }
111
112   return _bfd_sparc_elf_merge_private_bfd_data (ibfd, obfd);
113 }
114
115 /* The final processing done just before writing out the object file.
116    We need to set the e_machine field appropriately.  */
117
118 static void
119 elf32_sparc_final_write_processing (bfd *abfd,
120                                     bfd_boolean linker ATTRIBUTE_UNUSED)
121 {
122   switch (bfd_get_mach (abfd))
123     {
124     case bfd_mach_sparc :
125     case bfd_mach_sparc_sparclet :
126     case bfd_mach_sparc_sparclite :
127       break; /* nothing to do */
128     case bfd_mach_sparc_v8plus :
129       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
130       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
131       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
132       break;
133     case bfd_mach_sparc_v8plusa :
134       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
135       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
136       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
137       break;
138     case bfd_mach_sparc_v8plusb :
139     case bfd_mach_sparc_v8plusc :
140     case bfd_mach_sparc_v8plusd :
141     case bfd_mach_sparc_v8pluse :
142     case bfd_mach_sparc_v8plusv :
143     case bfd_mach_sparc_v8plusm :
144       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
145       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
146       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
147                                        | EF_SPARC_SUN_US3;
148       break;
149     case bfd_mach_sparc_sparclite_le :
150       elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
151       break;
152     default :
153       abort ();
154       break;
155     }
156 }
157
158 static enum elf_reloc_type_class
159 elf32_sparc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
160                               const asection *rel_sec ATTRIBUTE_UNUSED,
161                               const Elf_Internal_Rela *rela)
162 {
163   switch ((int) ELF32_R_TYPE (rela->r_info))
164     {
165     case R_SPARC_RELATIVE:
166       return reloc_class_relative;
167     case R_SPARC_JMP_SLOT:
168       return reloc_class_plt;
169     case R_SPARC_COPY:
170       return reloc_class_copy;
171     default:
172       return reloc_class_normal;
173     }
174 }
175
176 /* Hook called by the linker routine which adds symbols from an object
177    file.  */
178
179 static bfd_boolean
180 elf32_sparc_add_symbol_hook (bfd * abfd,
181                              struct bfd_link_info * info,
182                              Elf_Internal_Sym * sym,
183                              const char ** namep ATTRIBUTE_UNUSED,
184                              flagword * flagsp ATTRIBUTE_UNUSED,
185                              asection ** secp ATTRIBUTE_UNUSED,
186                              bfd_vma * valp ATTRIBUTE_UNUSED)
187 {
188   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
189       && (abfd->flags & DYNAMIC) == 0
190       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
191     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
192   return TRUE;
193 }
194
195 #define TARGET_BIG_SYM  sparc_elf32_vec
196 #define TARGET_BIG_NAME "elf32-sparc"
197 #define ELF_ARCH        bfd_arch_sparc
198 #define ELF_TARGET_ID   SPARC_ELF_DATA
199 #define ELF_MACHINE_CODE EM_SPARC
200 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
201 #define ELF_MAXPAGESIZE 0x10000
202 #define ELF_COMMONPAGESIZE 0x2000
203
204 #define bfd_elf32_bfd_merge_private_bfd_data \
205                                         elf32_sparc_merge_private_bfd_data
206 #define elf_backend_final_write_processing \
207                                         elf32_sparc_final_write_processing
208 #define elf_backend_grok_psinfo         elf32_sparc_grok_psinfo
209 #define elf_backend_reloc_type_class    elf32_sparc_reloc_type_class
210
211 #define elf_info_to_howto               _bfd_sparc_elf_info_to_howto
212 #define bfd_elf32_bfd_reloc_type_lookup _bfd_sparc_elf_reloc_type_lookup
213 #define bfd_elf32_bfd_reloc_name_lookup \
214   _bfd_sparc_elf_reloc_name_lookup
215 #define bfd_elf32_bfd_link_hash_table_create \
216                                         _bfd_sparc_elf_link_hash_table_create
217 #define bfd_elf32_bfd_relax_section     _bfd_sparc_elf_relax_section
218 #define bfd_elf32_new_section_hook      _bfd_sparc_elf_new_section_hook
219 #define elf_backend_copy_indirect_symbol \
220                                         _bfd_sparc_elf_copy_indirect_symbol
221 #define elf_backend_create_dynamic_sections \
222                                         _bfd_sparc_elf_create_dynamic_sections
223 #define elf_backend_check_relocs        _bfd_sparc_elf_check_relocs
224 #define elf_backend_adjust_dynamic_symbol \
225                                         _bfd_sparc_elf_adjust_dynamic_symbol
226 #define elf_backend_omit_section_dynsym _bfd_sparc_elf_omit_section_dynsym
227 #define elf_backend_size_dynamic_sections \
228                                         _bfd_sparc_elf_size_dynamic_sections
229 #define elf_backend_relocate_section    _bfd_sparc_elf_relocate_section
230 #define elf_backend_finish_dynamic_symbol \
231                                         _bfd_sparc_elf_finish_dynamic_symbol
232 #define elf_backend_finish_dynamic_sections \
233                                         _bfd_sparc_elf_finish_dynamic_sections
234 #define bfd_elf32_mkobject              _bfd_sparc_elf_mkobject
235 #define elf_backend_object_p            _bfd_sparc_elf_object_p
236 #define elf_backend_gc_mark_hook        _bfd_sparc_elf_gc_mark_hook
237 #define elf_backend_gc_sweep_hook       _bfd_sparc_elf_gc_sweep_hook
238 #define elf_backend_plt_sym_val         _bfd_sparc_elf_plt_sym_val
239 #define elf_backend_init_index_section  _bfd_elf_init_1_index_section
240
241 #define elf_backend_can_gc_sections 1
242 #define elf_backend_can_refcount 1
243 #define elf_backend_want_got_plt 0
244 #define elf_backend_plt_readonly 0
245 #define elf_backend_want_plt_sym 1
246 #define elf_backend_got_header_size 4
247 #define elf_backend_rela_normal 1
248
249 #define elf_backend_add_symbol_hook             elf32_sparc_add_symbol_hook
250
251 #include "elf32-target.h"
252
253 /* Solaris 2.  */
254
255 #undef  TARGET_BIG_SYM
256 #define TARGET_BIG_SYM                          sparc_elf32_sol2_vec
257 #undef  TARGET_BIG_NAME
258 #define TARGET_BIG_NAME                         "elf32-sparc-sol2"
259
260 #undef elf32_bed
261 #define elf32_bed                               elf32_sparc_sol2_bed
262
263 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
264    boundary.  */
265 #undef  elf_backend_static_tls_alignment
266 #define elf_backend_static_tls_alignment        8
267
268 #undef  elf_backend_strtab_flags
269 #define elf_backend_strtab_flags        SHF_STRINGS
270
271 static bfd_boolean
272 elf32_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
273                                                  bfd *obfd ATTRIBUTE_UNUSED,
274                                                  const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
275                                                  Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
276 {
277   /* PR 19938: FIXME: Need to add code for setting the sh_info
278      and sh_link fields of Solaris specific section types.  */
279   return FALSE;
280 }
281
282 #undef  elf_backend_copy_special_section_fields
283 #define elf_backend_copy_special_section_fields elf32_sparc_copy_solaris_special_section_fields
284
285 #include "elf32-target.h"
286
287 /* A wrapper around _bfd_sparc_elf_link_hash_table_create that identifies
288    the target system as VxWorks.  */
289
290 static struct bfd_link_hash_table *
291 elf32_sparc_vxworks_link_hash_table_create (bfd *abfd)
292 {
293   struct bfd_link_hash_table *ret;
294
295   ret = _bfd_sparc_elf_link_hash_table_create (abfd);
296   if (ret)
297     {
298       struct _bfd_sparc_elf_link_hash_table *htab;
299
300       htab = (struct _bfd_sparc_elf_link_hash_table *) ret;
301       htab->is_vxworks = 1;
302     }
303   return ret;
304 }
305
306 /* A final_write_processing hook that does both the SPARC- and VxWorks-
307    specific handling.  */
308
309 static void
310 elf32_sparc_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
311 {
312   elf32_sparc_final_write_processing (abfd, linker);
313   elf_vxworks_final_write_processing (abfd, linker);
314 }
315
316 #undef  TARGET_BIG_SYM
317 #define TARGET_BIG_SYM  sparc_elf32_vxworks_vec
318 #undef  TARGET_BIG_NAME
319 #define TARGET_BIG_NAME "elf32-sparc-vxworks"
320
321 #undef  ELF_MINPAGESIZE
322 #define ELF_MINPAGESIZE 0x1000
323
324 #undef bfd_elf32_bfd_link_hash_table_create
325 #define bfd_elf32_bfd_link_hash_table_create \
326   elf32_sparc_vxworks_link_hash_table_create
327
328 #undef  elf_backend_want_got_plt
329 #define elf_backend_want_got_plt                1
330 #undef  elf_backend_plt_readonly
331 #define elf_backend_plt_readonly                1
332 #undef  elf_backend_got_header_size
333 #define elf_backend_got_header_size             12
334 #undef  elf_backend_add_symbol_hook
335 #define elf_backend_add_symbol_hook \
336   elf_vxworks_add_symbol_hook
337 #undef  elf_backend_link_output_symbol_hook
338 #define elf_backend_link_output_symbol_hook \
339   elf_vxworks_link_output_symbol_hook
340 #undef  elf_backend_emit_relocs
341 #define elf_backend_emit_relocs \
342   elf_vxworks_emit_relocs
343 #undef  elf_backend_final_write_processing
344 #define elf_backend_final_write_processing \
345   elf32_sparc_vxworks_final_write_processing
346 #undef  elf_backend_static_tls_alignment
347 #undef  elf_backend_strtab_flags
348 #undef  elf_backend_copy_special_section_fields
349
350 #undef  elf32_bed
351 #define elf32_bed                               sparc_elf_vxworks_bed
352
353 #include "elf32-target.h"