1 /* SPARC-specific support for 32-bit ELF
2    Copyright (C) 1993-2018 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
elf32_sparc_grok_psinfo(bfd * abfd,Elf_Internal_Note * note)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
elf32_sparc_merge_private_bfd_data(bfd * ibfd,struct bfd_link_info * info)70 elf32_sparc_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
71 {
72   bfd *obfd = info->output_bfd;
73   bfd_boolean error;
74   unsigned long ibfd_mach;
75   /* FIXME: This should not be static.  */
76   static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
77 
78   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
79       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
80     return TRUE;
81 
82   error = FALSE;
83 
84   ibfd_mach = bfd_get_mach (ibfd);
85   if (bfd_mach_sparc_64bit_p (ibfd_mach))
86     {
87       error = TRUE;
88       _bfd_error_handler
89 	(_("%B: compiled for a 64 bit system and target is 32 bit"), ibfd);
90     }
91   else if ((ibfd->flags & DYNAMIC) == 0)
92     {
93       if (bfd_get_mach (obfd) < ibfd_mach)
94 	bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
95     }
96 
97   if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
98        != previous_ibfd_e_flags)
99       && previous_ibfd_e_flags != (unsigned long) -1)
100     {
101       _bfd_error_handler
102 	(_("%B: linking little endian files with big endian files"), ibfd);
103       error = TRUE;
104     }
105   previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
106 
107   if (error)
108     {
109       bfd_set_error (bfd_error_bad_value);
110       return FALSE;
111     }
112 
113   return _bfd_sparc_elf_merge_private_bfd_data (ibfd, info);
114 }
115 
116 /* The final processing done just before writing out the object file.
117    We need to set the e_machine field appropriately.  */
118 
119 static void
elf32_sparc_final_write_processing(bfd * abfd,bfd_boolean linker ATTRIBUTE_UNUSED)120 elf32_sparc_final_write_processing (bfd *abfd,
121 				    bfd_boolean linker ATTRIBUTE_UNUSED)
122 {
123   switch (bfd_get_mach (abfd))
124     {
125     case bfd_mach_sparc :
126     case bfd_mach_sparc_sparclet :
127     case bfd_mach_sparc_sparclite :
128       break; /* nothing to do */
129     case bfd_mach_sparc_v8plus :
130       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
131       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
132       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
133       break;
134     case bfd_mach_sparc_v8plusa :
135       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
136       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
137       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
138       break;
139     case bfd_mach_sparc_v8plusb :
140     case bfd_mach_sparc_v8plusc :
141     case bfd_mach_sparc_v8plusd :
142     case bfd_mach_sparc_v8pluse :
143     case bfd_mach_sparc_v8plusv :
144     case bfd_mach_sparc_v8plusm :
145     case bfd_mach_sparc_v8plusm8 :
146       elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
147       elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
148       elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
149 				       | EF_SPARC_SUN_US3;
150       break;
151     case bfd_mach_sparc_sparclite_le :
152       elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
153       break;
154     default :
155       abort ();
156       break;
157     }
158 }
159 
160 static enum elf_reloc_type_class
elf32_sparc_reloc_type_class(const struct bfd_link_info * info ATTRIBUTE_UNUSED,const asection * rel_sec ATTRIBUTE_UNUSED,const Elf_Internal_Rela * rela)161 elf32_sparc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
162 			      const asection *rel_sec ATTRIBUTE_UNUSED,
163 			      const Elf_Internal_Rela *rela)
164 {
165   switch ((int) ELF32_R_TYPE (rela->r_info))
166     {
167     case R_SPARC_RELATIVE:
168       return reloc_class_relative;
169     case R_SPARC_JMP_SLOT:
170       return reloc_class_plt;
171     case R_SPARC_COPY:
172       return reloc_class_copy;
173     default:
174       return reloc_class_normal;
175     }
176 }
177 
178 /* Hook called by the linker routine which adds symbols from an object
179    file.  */
180 
181 static bfd_boolean
elf32_sparc_add_symbol_hook(bfd * abfd,struct bfd_link_info * info,Elf_Internal_Sym * sym,const char ** namep ATTRIBUTE_UNUSED,flagword * flagsp ATTRIBUTE_UNUSED,asection ** secp ATTRIBUTE_UNUSED,bfd_vma * valp ATTRIBUTE_UNUSED)182 elf32_sparc_add_symbol_hook (bfd * abfd,
183 			     struct bfd_link_info * info,
184 			     Elf_Internal_Sym * sym,
185 			     const char ** namep ATTRIBUTE_UNUSED,
186 			     flagword * flagsp ATTRIBUTE_UNUSED,
187 			     asection ** secp ATTRIBUTE_UNUSED,
188 			     bfd_vma * valp ATTRIBUTE_UNUSED)
189 {
190   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
191       && (abfd->flags & DYNAMIC) == 0
192       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
193     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
194   return TRUE;
195 }
196 
197 #define TARGET_BIG_SYM	sparc_elf32_vec
198 #define TARGET_BIG_NAME	"elf32-sparc"
199 #define ELF_ARCH	bfd_arch_sparc
200 #define ELF_TARGET_ID	SPARC_ELF_DATA
201 #define ELF_MACHINE_CODE EM_SPARC
202 #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
203 #define ELF_MAXPAGESIZE 0x10000
204 #define ELF_COMMONPAGESIZE 0x2000
205 
206 #define bfd_elf32_bfd_merge_private_bfd_data \
207 					elf32_sparc_merge_private_bfd_data
208 #define elf_backend_final_write_processing \
209 					elf32_sparc_final_write_processing
210 #define elf_backend_grok_psinfo		elf32_sparc_grok_psinfo
211 #define elf_backend_reloc_type_class	elf32_sparc_reloc_type_class
212 
213 #define elf_info_to_howto		_bfd_sparc_elf_info_to_howto
214 #define bfd_elf32_bfd_reloc_type_lookup	_bfd_sparc_elf_reloc_type_lookup
215 #define bfd_elf32_bfd_reloc_name_lookup \
216   _bfd_sparc_elf_reloc_name_lookup
217 #define bfd_elf32_bfd_link_hash_table_create \
218 					_bfd_sparc_elf_link_hash_table_create
219 #define bfd_elf32_bfd_relax_section	_bfd_sparc_elf_relax_section
220 #define bfd_elf32_new_section_hook	_bfd_sparc_elf_new_section_hook
221 #define elf_backend_copy_indirect_symbol \
222 					_bfd_sparc_elf_copy_indirect_symbol
223 #define elf_backend_create_dynamic_sections \
224 					_bfd_sparc_elf_create_dynamic_sections
225 #define elf_backend_check_relocs	_bfd_sparc_elf_check_relocs
226 #define elf_backend_adjust_dynamic_symbol \
227 					_bfd_sparc_elf_adjust_dynamic_symbol
228 #define elf_backend_omit_section_dynsym	_bfd_sparc_elf_omit_section_dynsym
229 #define elf_backend_size_dynamic_sections \
230 					_bfd_sparc_elf_size_dynamic_sections
231 #define elf_backend_relocate_section	_bfd_sparc_elf_relocate_section
232 #define elf_backend_finish_dynamic_symbol \
233 					_bfd_sparc_elf_finish_dynamic_symbol
234 #define elf_backend_finish_dynamic_sections \
235 					_bfd_sparc_elf_finish_dynamic_sections
236 #define bfd_elf32_mkobject		_bfd_sparc_elf_mkobject
237 #define elf_backend_object_p		_bfd_sparc_elf_object_p
238 #define elf_backend_gc_mark_hook	_bfd_sparc_elf_gc_mark_hook
239 #define elf_backend_plt_sym_val		_bfd_sparc_elf_plt_sym_val
240 #define elf_backend_init_index_section	_bfd_elf_init_1_index_section
241 #define elf_backend_fixup_symbol	_bfd_sparc_elf_fixup_symbol
242 
243 #define elf_backend_can_gc_sections 1
244 #define elf_backend_can_refcount 1
245 #define elf_backend_want_got_plt 0
246 #define elf_backend_plt_readonly 0
247 #define elf_backend_want_plt_sym 1
248 #define elf_backend_got_header_size 4
249 #define elf_backend_want_dynrelro 1
250 #define elf_backend_rela_normal 1
251 
252 #define elf_backend_add_symbol_hook		elf32_sparc_add_symbol_hook
253 
254 #define elf_backend_linux_prpsinfo32_ugid16	TRUE
255 
256 #include "elf32-target.h"
257 
258 /* Solaris 2.  */
259 
260 #undef	TARGET_BIG_SYM
261 #define	TARGET_BIG_SYM				sparc_elf32_sol2_vec
262 #undef	TARGET_BIG_NAME
263 #define	TARGET_BIG_NAME				"elf32-sparc-sol2"
264 
265 #undef elf32_bed
266 #define elf32_bed				elf32_sparc_sol2_bed
267 
268 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
269    boundary.  */
270 #undef	elf_backend_static_tls_alignment
271 #define elf_backend_static_tls_alignment	8
272 
273 #undef	elf_backend_strtab_flags
274 #define elf_backend_strtab_flags	SHF_STRINGS
275 
276 static bfd_boolean
elf32_sparc_copy_solaris_special_section_fields(const bfd * ibfd ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,const Elf_Internal_Shdr * isection ATTRIBUTE_UNUSED,Elf_Internal_Shdr * osection ATTRIBUTE_UNUSED)277 elf32_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
278 						 bfd *obfd ATTRIBUTE_UNUSED,
279 						 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
280 						 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
281 {
282   /* PR 19938: FIXME: Need to add code for setting the sh_info
283      and sh_link fields of Solaris specific section types.  */
284   return FALSE;
285 }
286 
287 #undef  elf_backend_copy_special_section_fields
288 #define elf_backend_copy_special_section_fields elf32_sparc_copy_solaris_special_section_fields
289 
290 #include "elf32-target.h"
291 
292 /* A wrapper around _bfd_sparc_elf_link_hash_table_create that identifies
293    the target system as VxWorks.  */
294 
295 static struct bfd_link_hash_table *
elf32_sparc_vxworks_link_hash_table_create(bfd * abfd)296 elf32_sparc_vxworks_link_hash_table_create (bfd *abfd)
297 {
298   struct bfd_link_hash_table *ret;
299 
300   ret = _bfd_sparc_elf_link_hash_table_create (abfd);
301   if (ret)
302     {
303       struct _bfd_sparc_elf_link_hash_table *htab;
304 
305       htab = (struct _bfd_sparc_elf_link_hash_table *) ret;
306       htab->is_vxworks = 1;
307     }
308   return ret;
309 }
310 
311 /* A final_write_processing hook that does both the SPARC- and VxWorks-
312    specific handling.  */
313 
314 static void
elf32_sparc_vxworks_final_write_processing(bfd * abfd,bfd_boolean linker)315 elf32_sparc_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
316 {
317   elf32_sparc_final_write_processing (abfd, linker);
318   elf_vxworks_final_write_processing (abfd, linker);
319 }
320 
321 #undef  TARGET_BIG_SYM
322 #define TARGET_BIG_SYM	sparc_elf32_vxworks_vec
323 #undef  TARGET_BIG_NAME
324 #define TARGET_BIG_NAME	"elf32-sparc-vxworks"
325 
326 #undef  ELF_MINPAGESIZE
327 #define ELF_MINPAGESIZE	0x1000
328 
329 #undef bfd_elf32_bfd_link_hash_table_create
330 #define bfd_elf32_bfd_link_hash_table_create \
331   elf32_sparc_vxworks_link_hash_table_create
332 
333 #undef  elf_backend_want_got_plt
334 #define elf_backend_want_got_plt		1
335 #undef  elf_backend_plt_readonly
336 #define elf_backend_plt_readonly		1
337 #undef  elf_backend_got_header_size
338 #define elf_backend_got_header_size		12
339 #undef  elf_backend_dtrel_excludes_plt
340 #define elf_backend_dtrel_excludes_plt		1
341 #undef  elf_backend_add_symbol_hook
342 #define elf_backend_add_symbol_hook \
343   elf_vxworks_add_symbol_hook
344 #undef  elf_backend_link_output_symbol_hook
345 #define elf_backend_link_output_symbol_hook \
346   elf_vxworks_link_output_symbol_hook
347 #undef  elf_backend_emit_relocs
348 #define elf_backend_emit_relocs \
349   elf_vxworks_emit_relocs
350 #undef  elf_backend_final_write_processing
351 #define elf_backend_final_write_processing \
352   elf32_sparc_vxworks_final_write_processing
353 #undef  elf_backend_static_tls_alignment
354 #undef  elf_backend_strtab_flags
355 #undef  elf_backend_copy_special_section_fields
356 
357 #undef  elf32_bed
358 #define elf32_bed				sparc_elf_vxworks_bed
359 
360 #include "elf32-target.h"
361