1 /* SPARC-specific support for 64-bit ELF
2    Copyright (C) 1993-2021 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 <limits.h>
23 #include "bfd.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 
30 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
31 #define MINUS_ONE (~ (bfd_vma) 0)
32 
33 /* Due to the way how we handle R_SPARC_OLO10, each entry in a SHT_RELA
34    section can represent up to two relocs, we must tell the user to allocate
35    more space.  */
36 
37 static long
elf64_sparc_get_reloc_upper_bound(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)38 elf64_sparc_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
39 {
40 #if SIZEOF_LONG == SIZEOF_INT
41   if (sec->reloc_count >= LONG_MAX / 2 / sizeof (arelent *))
42     {
43       bfd_set_error (bfd_error_file_too_big);
44       return -1;
45     }
46 #endif
47   return (sec->reloc_count * 2 + 1) * sizeof (arelent *);
48 }
49 
50 static long
elf64_sparc_get_dynamic_reloc_upper_bound(bfd * abfd)51 elf64_sparc_get_dynamic_reloc_upper_bound (bfd *abfd)
52 {
53   long ret = _bfd_elf_get_dynamic_reloc_upper_bound (abfd);
54   if (ret > LONG_MAX / 2)
55     {
56       bfd_set_error (bfd_error_file_too_big);
57       ret = -1;
58     }
59   else if (ret > 0)
60     ret *= 2;
61   return ret;
62 }
63 
64 /* Read  relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
65    them.  We cannot use generic elf routines for this,  because R_SPARC_OLO10
66    has secondary addend in ELF64_R_TYPE_DATA.  We handle it as two relocations
67    for the same location,  R_SPARC_LO10 and R_SPARC_13.  */
68 
69 static bool
elf64_sparc_slurp_one_reloc_table(bfd * abfd,asection * asect,Elf_Internal_Shdr * rel_hdr,asymbol ** symbols,bool dynamic)70 elf64_sparc_slurp_one_reloc_table (bfd *abfd, asection *asect,
71 				   Elf_Internal_Shdr *rel_hdr,
72 				   asymbol **symbols, bool dynamic)
73 {
74   void * allocated = NULL;
75   bfd_byte *native_relocs;
76   arelent *relent;
77   unsigned int i;
78   int entsize;
79   bfd_size_type count;
80   arelent *relents;
81 
82   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
83     return false;
84   allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
85   if (allocated == NULL)
86     return false;
87 
88   native_relocs = (bfd_byte *) allocated;
89 
90   relents = asect->relocation + canon_reloc_count (asect);
91 
92   entsize = rel_hdr->sh_entsize;
93   BFD_ASSERT (entsize == sizeof (Elf64_External_Rela));
94 
95   count = rel_hdr->sh_size / entsize;
96 
97   for (i = 0, relent = relents; i < count;
98        i++, relent++, native_relocs += entsize)
99     {
100       Elf_Internal_Rela rela;
101       unsigned int r_type;
102 
103       bfd_elf64_swap_reloca_in (abfd, native_relocs, &rela);
104 
105       /* The address of an ELF reloc is section relative for an object
106 	 file, and absolute for an executable file or shared library.
107 	 The address of a normal BFD reloc is always section relative,
108 	 and the address of a dynamic reloc is absolute..  */
109       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
110 	relent->address = rela.r_offset;
111       else
112 	relent->address = rela.r_offset - asect->vma;
113 
114       if (ELF64_R_SYM (rela.r_info) == STN_UNDEF)
115 	relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
116       else if (/* PR 17512: file: 996185f8.  */
117 	       ELF64_R_SYM (rela.r_info) > (dynamic
118 					    ? bfd_get_dynamic_symcount (abfd)
119 					    : bfd_get_symcount (abfd)))
120 	{
121 	  _bfd_error_handler
122 	    /* xgettext:c-format */
123 	    (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
124 	     abfd, asect, i, (long) ELF64_R_SYM (rela.r_info));
125 	  bfd_set_error (bfd_error_bad_value);
126 	  relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
127 	}
128       else
129 	{
130 	  asymbol **ps, *s;
131 
132 	  ps = symbols + ELF64_R_SYM (rela.r_info) - 1;
133 	  s = *ps;
134 
135 	  /* Canonicalize ELF section symbols.  FIXME: Why?  */
136 	  if ((s->flags & BSF_SECTION_SYM) == 0)
137 	    relent->sym_ptr_ptr = ps;
138 	  else
139 	    relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
140 	}
141 
142       relent->addend = rela.r_addend;
143 
144       r_type = ELF64_R_TYPE_ID (rela.r_info);
145       if (r_type == R_SPARC_OLO10)
146 	{
147 	  relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_LO10);
148 	  relent[1].address = relent->address;
149 	  relent++;
150 	  relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
151 	  relent->addend = ELF64_R_TYPE_DATA (rela.r_info);
152 	  relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_13);
153 	}
154       else
155 	{
156 	  relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, r_type);
157 	  if (relent->howto == NULL)
158 	    goto error_return;
159 	}
160     }
161 
162   canon_reloc_count (asect) += relent - relents;
163 
164   free (allocated);
165   return true;
166 
167  error_return:
168   free (allocated);
169   return false;
170 }
171 
172 /* Read in and swap the external relocs.  */
173 
174 static bool
elf64_sparc_slurp_reloc_table(bfd * abfd,asection * asect,asymbol ** symbols,bool dynamic)175 elf64_sparc_slurp_reloc_table (bfd *abfd, asection *asect,
176 			       asymbol **symbols, bool dynamic)
177 {
178   struct bfd_elf_section_data * const d = elf_section_data (asect);
179   Elf_Internal_Shdr *rel_hdr;
180   Elf_Internal_Shdr *rel_hdr2;
181   bfd_size_type amt;
182 
183   if (asect->relocation != NULL)
184     return true;
185 
186   if (! dynamic)
187     {
188       if ((asect->flags & SEC_RELOC) == 0
189 	  || asect->reloc_count == 0)
190 	return true;
191 
192       rel_hdr = d->rel.hdr;
193       rel_hdr2 = d->rela.hdr;
194 
195       BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
196 		  || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
197     }
198   else
199     {
200       /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
201 	 case because relocations against this section may use the
202 	 dynamic symbol table, and in that case bfd_section_from_shdr
203 	 in elf.c does not update the RELOC_COUNT.  */
204       if (asect->size == 0)
205 	return true;
206 
207       rel_hdr = &d->this_hdr;
208       asect->reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
209       rel_hdr2 = NULL;
210     }
211 
212   amt = asect->reloc_count;
213   amt *= 2 * sizeof (arelent);
214   asect->relocation = (arelent *) bfd_alloc (abfd, amt);
215   if (asect->relocation == NULL)
216     return false;
217 
218   /* The elf64_sparc_slurp_one_reloc_table routine increments
219      canon_reloc_count.  */
220   canon_reloc_count (asect) = 0;
221 
222   if (rel_hdr
223       && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols,
224 					     dynamic))
225     return false;
226 
227   if (rel_hdr2
228       && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr2, symbols,
229 					     dynamic))
230     return false;
231 
232   return true;
233 }
234 
235 /* Canonicalize the relocs.  */
236 
237 static long
elf64_sparc_canonicalize_reloc(bfd * abfd,sec_ptr section,arelent ** relptr,asymbol ** symbols)238 elf64_sparc_canonicalize_reloc (bfd *abfd, sec_ptr section,
239 				arelent **relptr, asymbol **symbols)
240 {
241   arelent *tblptr;
242   unsigned int i;
243   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
244 
245   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
246     return -1;
247 
248   tblptr = section->relocation;
249   for (i = 0; i < canon_reloc_count (section); i++)
250     *relptr++ = tblptr++;
251 
252   *relptr = NULL;
253 
254   return canon_reloc_count (section);
255 }
256 
257 
258 /* Canonicalize the dynamic relocation entries.  Note that we return
259    the dynamic relocations as a single block, although they are
260    actually associated with particular sections; the interface, which
261    was designed for SunOS style shared libraries, expects that there
262    is only one set of dynamic relocs.  Any section that was actually
263    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
264    the dynamic symbol table, is considered to be a dynamic reloc
265    section.  */
266 
267 static long
elf64_sparc_canonicalize_dynamic_reloc(bfd * abfd,arelent ** storage,asymbol ** syms)268 elf64_sparc_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage,
269 					asymbol **syms)
270 {
271   asection *s;
272   long ret;
273 
274   if (elf_dynsymtab (abfd) == 0)
275     {
276       bfd_set_error (bfd_error_invalid_operation);
277       return -1;
278     }
279 
280   ret = 0;
281   for (s = abfd->sections; s != NULL; s = s->next)
282     {
283       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
284 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
285 	{
286 	  arelent *p;
287 	  long count, i;
288 
289 	  if (! elf64_sparc_slurp_reloc_table (abfd, s, syms, true))
290 	    return -1;
291 	  count = canon_reloc_count (s);
292 	  p = s->relocation;
293 	  for (i = 0; i < count; i++)
294 	    *storage++ = p++;
295 	  ret += count;
296 	}
297     }
298 
299   *storage = NULL;
300 
301   return ret;
302 }
303 
304 /* Install a new set of internal relocs.  */
305 
306 static void
elf64_sparc_set_reloc(bfd * abfd ATTRIBUTE_UNUSED,asection * asect,arelent ** location,unsigned int count)307 elf64_sparc_set_reloc (bfd *abfd ATTRIBUTE_UNUSED,
308 		       asection *asect,
309 		       arelent **location,
310 		       unsigned int count)
311 {
312   asect->orelocation = location;
313   canon_reloc_count (asect) = count;
314 }
315 
316 /* Write out the relocs.  */
317 
318 static void
elf64_sparc_write_relocs(bfd * abfd,asection * sec,void * data)319 elf64_sparc_write_relocs (bfd *abfd, asection *sec, void * data)
320 {
321   bool *failedp = (bool *) data;
322   Elf_Internal_Shdr *rela_hdr;
323   bfd_vma addr_offset;
324   Elf64_External_Rela *outbound_relocas, *src_rela;
325   unsigned int idx, count;
326   asymbol *last_sym = 0;
327   int last_sym_idx = 0;
328 
329   /* If we have already failed, don't do anything.  */
330   if (*failedp)
331     return;
332 
333   if ((sec->flags & SEC_RELOC) == 0)
334     return;
335 
336   /* The linker backend writes the relocs out itself, and sets the
337      reloc_count field to zero to inhibit writing them here.  Also,
338      sometimes the SEC_RELOC flag gets set even when there aren't any
339      relocs.  */
340   if (canon_reloc_count (sec) == 0)
341     return;
342 
343   /* We can combine two relocs that refer to the same address
344      into R_SPARC_OLO10 if first one is R_SPARC_LO10 and the
345      latter is R_SPARC_13 with no associated symbol.  */
346   count = 0;
347   for (idx = 0; idx < canon_reloc_count (sec); idx++)
348     {
349       bfd_vma addr;
350 
351       ++count;
352 
353       addr = sec->orelocation[idx]->address;
354       if (sec->orelocation[idx]->howto->type == R_SPARC_LO10
355 	  && idx < canon_reloc_count (sec) - 1)
356 	{
357 	  arelent *r = sec->orelocation[idx + 1];
358 
359 	  if (r->howto->type == R_SPARC_13
360 	      && r->address == addr
361 	      && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
362 	      && (*r->sym_ptr_ptr)->value == 0)
363 	    ++idx;
364 	}
365     }
366 
367   rela_hdr = elf_section_data (sec)->rela.hdr;
368 
369   rela_hdr->sh_size = rela_hdr->sh_entsize * count;
370   rela_hdr->contents = bfd_alloc (abfd, rela_hdr->sh_size);
371   if (rela_hdr->contents == NULL)
372     {
373       *failedp = true;
374       return;
375     }
376 
377   /* Figure out whether the relocations are RELA or REL relocations.  */
378   if (rela_hdr->sh_type != SHT_RELA)
379     abort ();
380 
381   /* The address of an ELF reloc is section relative for an object
382      file, and absolute for an executable file or shared library.
383      The address of a BFD reloc is always section relative.  */
384   addr_offset = 0;
385   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
386     addr_offset = sec->vma;
387 
388   /* orelocation has the data, reloc_count has the count...  */
389   outbound_relocas = (Elf64_External_Rela *) rela_hdr->contents;
390   src_rela = outbound_relocas;
391 
392   for (idx = 0; idx < canon_reloc_count (sec); idx++)
393     {
394       Elf_Internal_Rela dst_rela;
395       arelent *ptr;
396       asymbol *sym;
397       int n;
398 
399       ptr = sec->orelocation[idx];
400       sym = *ptr->sym_ptr_ptr;
401       if (sym == last_sym)
402 	n = last_sym_idx;
403       else if (bfd_is_abs_section (sym->section) && sym->value == 0)
404 	n = STN_UNDEF;
405       else
406 	{
407 	  last_sym = sym;
408 	  n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
409 	  if (n < 0)
410 	    {
411 	      *failedp = true;
412 	      return;
413 	    }
414 	  last_sym_idx = n;
415 	}
416 
417       if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
418 	  && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
419 	  && ! _bfd_elf_validate_reloc (abfd, ptr))
420 	{
421 	  *failedp = true;
422 	  return;
423 	}
424 
425       if (ptr->howto->type == R_SPARC_LO10
426 	  && idx < canon_reloc_count (sec) - 1)
427 	{
428 	  arelent *r = sec->orelocation[idx + 1];
429 
430 	  if (r->howto->type == R_SPARC_13
431 	      && r->address == ptr->address
432 	      && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
433 	      && (*r->sym_ptr_ptr)->value == 0)
434 	    {
435 	      idx++;
436 	      dst_rela.r_info
437 		= ELF64_R_INFO (n, ELF64_R_TYPE_INFO (r->addend,
438 						      R_SPARC_OLO10));
439 	    }
440 	  else
441 	    dst_rela.r_info = ELF64_R_INFO (n, R_SPARC_LO10);
442 	}
443       else
444 	dst_rela.r_info = ELF64_R_INFO (n, ptr->howto->type);
445 
446       dst_rela.r_offset = ptr->address + addr_offset;
447       dst_rela.r_addend = ptr->addend;
448 
449       bfd_elf64_swap_reloca_out (abfd, &dst_rela, (bfd_byte *) src_rela);
450       ++src_rela;
451     }
452 }
453 
454 /* Hook called by the linker routine which adds symbols from an object
455    file.  We use it for STT_REGISTER symbols.  */
456 
457 static bool
elf64_sparc_add_symbol_hook(bfd * abfd,struct bfd_link_info * info,Elf_Internal_Sym * sym,const char ** namep,flagword * flagsp ATTRIBUTE_UNUSED,asection ** secp ATTRIBUTE_UNUSED,bfd_vma * valp ATTRIBUTE_UNUSED)458 elf64_sparc_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
459 			     Elf_Internal_Sym *sym, const char **namep,
460 			     flagword *flagsp ATTRIBUTE_UNUSED,
461 			     asection **secp ATTRIBUTE_UNUSED,
462 			     bfd_vma *valp ATTRIBUTE_UNUSED)
463 {
464   static const char *const stt_types[] = { "NOTYPE", "OBJECT", "FUNCTION" };
465 
466   if (ELF_ST_TYPE (sym->st_info) == STT_REGISTER)
467     {
468       int reg;
469       struct _bfd_sparc_elf_app_reg *p;
470 
471       reg = (int)sym->st_value;
472       switch (reg & ~1)
473 	{
474 	case 2: reg -= 2; break;
475 	case 6: reg -= 4; break;
476 	default:
477 	  _bfd_error_handler
478 	    (_("%pB: only registers %%g[2367] can be declared using STT_REGISTER"),
479 	     abfd);
480 	  return false;
481 	}
482 
483       if (info->output_bfd->xvec != abfd->xvec
484 	  || (abfd->flags & DYNAMIC) != 0)
485 	{
486 	  /* STT_REGISTER only works when linking an elf64_sparc object.
487 	     If STT_REGISTER comes from a dynamic object, don't put it into
488 	     the output bfd.  The dynamic linker will recheck it.  */
489 	  *namep = NULL;
490 	  return true;
491 	}
492 
493       p = _bfd_sparc_elf_hash_table(info)->app_regs + reg;
494 
495       if (p->name != NULL && strcmp (p->name, *namep))
496 	{
497 	  _bfd_error_handler
498 	    /* xgettext:c-format */
499 	    (_("register %%g%d used incompatibly: %s in %pB,"
500 	       " previously %s in %pB"),
501 	     (int) sym->st_value, **namep ? *namep : "#scratch", abfd,
502 	     *p->name ? p->name : "#scratch", p->abfd);
503 	  return false;
504 	}
505 
506       if (p->name == NULL)
507 	{
508 	  if (**namep)
509 	    {
510 	      struct elf_link_hash_entry *h;
511 
512 	      h = (struct elf_link_hash_entry *)
513 		bfd_link_hash_lookup (info->hash, *namep, false, false, false);
514 
515 	      if (h != NULL)
516 		{
517 		  unsigned char type = h->type;
518 
519 		  if (type > STT_FUNC)
520 		    type = 0;
521 		  _bfd_error_handler
522 		    /* xgettext:c-format */
523 		    (_("symbol `%s' has differing types: REGISTER in %pB,"
524 		       " previously %s in %pB"),
525 		     *namep, abfd, stt_types[type], p->abfd);
526 		  return false;
527 		}
528 
529 	      p->name = bfd_hash_allocate (&info->hash->table,
530 					   strlen (*namep) + 1);
531 	      if (!p->name)
532 		return false;
533 
534 	      strcpy (p->name, *namep);
535 	    }
536 	  else
537 	    p->name = "";
538 	  p->bind = ELF_ST_BIND (sym->st_info);
539 	  p->abfd = abfd;
540 	  p->shndx = sym->st_shndx;
541 	}
542       else
543 	{
544 	  if (p->bind == STB_WEAK
545 	      && ELF_ST_BIND (sym->st_info) == STB_GLOBAL)
546 	    {
547 	      p->bind = STB_GLOBAL;
548 	      p->abfd = abfd;
549 	    }
550 	}
551       *namep = NULL;
552       return true;
553     }
554   else if (*namep && **namep
555 	   && info->output_bfd->xvec == abfd->xvec)
556     {
557       int i;
558       struct _bfd_sparc_elf_app_reg *p;
559 
560       p = _bfd_sparc_elf_hash_table(info)->app_regs;
561       for (i = 0; i < 4; i++, p++)
562 	if (p->name != NULL && ! strcmp (p->name, *namep))
563 	  {
564 	    unsigned char type = ELF_ST_TYPE (sym->st_info);
565 
566 	    if (type > STT_FUNC)
567 	      type = 0;
568 	    _bfd_error_handler
569 	      /* xgettext:c-format */
570 	      (_("Symbol `%s' has differing types: %s in %pB,"
571 		 " previously REGISTER in %pB"),
572 	       *namep, stt_types[type], abfd, p->abfd);
573 	    return false;
574 	  }
575     }
576   return true;
577 }
578 
579 /* This function takes care of emitting STT_REGISTER symbols
580    which we cannot easily keep in the symbol hash table.  */
581 
582 static bool
elf64_sparc_output_arch_syms(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,void * flaginfo,int (* func)(void *,const char *,Elf_Internal_Sym *,asection *,struct elf_link_hash_entry *))583 elf64_sparc_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
584 			      struct bfd_link_info *info,
585 			      void * flaginfo,
586 			      int (*func) (void *, const char *,
587 					   Elf_Internal_Sym *,
588 					   asection *,
589 					   struct elf_link_hash_entry *))
590 {
591   int reg;
592   struct _bfd_sparc_elf_app_reg *app_regs =
593     _bfd_sparc_elf_hash_table(info)->app_regs;
594   Elf_Internal_Sym sym;
595 
596   for (reg = 0; reg < 4; reg++)
597     if (app_regs [reg].name != NULL)
598       {
599 	if (info->strip == strip_some
600 	    && bfd_hash_lookup (info->keep_hash,
601 				app_regs [reg].name,
602 				false, false) == NULL)
603 	  continue;
604 
605 	sym.st_value = reg < 2 ? reg + 2 : reg + 4;
606 	sym.st_size = 0;
607 	sym.st_other = 0;
608 	sym.st_info = ELF_ST_INFO (app_regs [reg].bind, STT_REGISTER);
609 	sym.st_shndx = app_regs [reg].shndx;
610 	sym.st_target_internal = 0;
611 	if ((*func) (flaginfo, app_regs [reg].name, &sym,
612 		     sym.st_shndx == SHN_ABS
613 		     ? bfd_abs_section_ptr : bfd_und_section_ptr,
614 		     NULL) != 1)
615 	  return false;
616       }
617 
618   return true;
619 }
620 
621 static int
elf64_sparc_get_symbol_type(Elf_Internal_Sym * elf_sym,int type)622 elf64_sparc_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
623 {
624   if (ELF_ST_TYPE (elf_sym->st_info) == STT_REGISTER)
625     return STT_REGISTER;
626   else
627     return type;
628 }
629 
630 /* A STB_GLOBAL,STT_REGISTER symbol should be BSF_GLOBAL
631    even in SHN_UNDEF section.  */
632 
633 static void
elf64_sparc_symbol_processing(bfd * abfd ATTRIBUTE_UNUSED,asymbol * asym)634 elf64_sparc_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
635 {
636   elf_symbol_type *elfsym;
637 
638   elfsym = (elf_symbol_type *) asym;
639   if (elfsym->internal_elf_sym.st_info
640       == ELF_ST_INFO (STB_GLOBAL, STT_REGISTER))
641     {
642       asym->flags |= BSF_GLOBAL;
643     }
644 }
645 
646 
647 /* Functions for dealing with the e_flags field.  */
648 
649 /* Merge backend specific data from an object file to the output
650    object file when linking.  */
651 
652 static bool
elf64_sparc_merge_private_bfd_data(bfd * ibfd,struct bfd_link_info * info)653 elf64_sparc_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
654 {
655   bfd *obfd = info->output_bfd;
656   bool error;
657   flagword new_flags, old_flags;
658   int new_mm, old_mm;
659 
660   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
661       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
662     return true;
663 
664   new_flags = elf_elfheader (ibfd)->e_flags;
665   old_flags = elf_elfheader (obfd)->e_flags;
666 
667   if (!elf_flags_init (obfd))   /* First call, no flags set */
668     {
669       elf_flags_init (obfd) = true;
670       elf_elfheader (obfd)->e_flags = new_flags;
671     }
672 
673   else if (new_flags == old_flags)      /* Compatible flags are ok */
674     ;
675 
676   else					/* Incompatible flags */
677     {
678       error = false;
679 
680 #define EF_SPARC_ISA_EXTENSIONS \
681   (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3 | EF_SPARC_HAL_R1)
682 
683       if ((ibfd->flags & DYNAMIC) != 0)
684 	{
685 	  /* We don't want dynamic objects memory ordering and
686 	     architecture to have any role. That's what dynamic linker
687 	     should do.  */
688 	  new_flags &= ~(EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS);
689 	  new_flags |= (old_flags
690 			& (EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS));
691 	}
692       else
693 	{
694 	  /* Choose the highest architecture requirements.  */
695 	  old_flags |= (new_flags & EF_SPARC_ISA_EXTENSIONS);
696 	  new_flags |= (old_flags & EF_SPARC_ISA_EXTENSIONS);
697 	  if ((old_flags & (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3))
698 	      && (old_flags & EF_SPARC_HAL_R1))
699 	    {
700 	      error = true;
701 	      _bfd_error_handler
702 		(_("%pB: linking UltraSPARC specific with HAL specific code"),
703 		 ibfd);
704 	    }
705 	  /* Choose the most restrictive memory ordering.  */
706 	  old_mm = (old_flags & EF_SPARCV9_MM);
707 	  new_mm = (new_flags & EF_SPARCV9_MM);
708 	  old_flags &= ~EF_SPARCV9_MM;
709 	  new_flags &= ~EF_SPARCV9_MM;
710 	  if (new_mm < old_mm)
711 	    old_mm = new_mm;
712 	  old_flags |= old_mm;
713 	  new_flags |= old_mm;
714 	}
715 
716       /* Warn about any other mismatches */
717       if (new_flags != old_flags)
718 	{
719 	  error = true;
720 	  _bfd_error_handler
721 	    /* xgettext:c-format */
722 	    (_("%pB: uses different e_flags (%#x) fields than previous modules (%#x)"),
723 	     ibfd, new_flags, old_flags);
724 	}
725 
726       elf_elfheader (obfd)->e_flags = old_flags;
727 
728       if (error)
729 	{
730 	  bfd_set_error (bfd_error_bad_value);
731 	  return false;
732 	}
733     }
734   return _bfd_sparc_elf_merge_private_bfd_data (ibfd, info);
735 }
736 
737 /* MARCO: Set the correct entry size for the .stab section.  */
738 
739 static bool
elf64_sparc_fake_sections(bfd * abfd ATTRIBUTE_UNUSED,Elf_Internal_Shdr * hdr ATTRIBUTE_UNUSED,asection * sec)740 elf64_sparc_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
741 			   Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED,
742 			   asection *sec)
743 {
744   const char *name;
745 
746   name = bfd_section_name (sec);
747 
748   if (strcmp (name, ".stab") == 0)
749     {
750       /* Even in the 64bit case the stab entries are only 12 bytes long.  */
751       elf_section_data (sec)->this_hdr.sh_entsize = 12;
752     }
753 
754   return true;
755 }
756 
757 /* Print a STT_REGISTER symbol to file FILE.  */
758 
759 static const char *
elf64_sparc_print_symbol_all(bfd * abfd ATTRIBUTE_UNUSED,void * filep,asymbol * symbol)760 elf64_sparc_print_symbol_all (bfd *abfd ATTRIBUTE_UNUSED, void * filep,
761 			      asymbol *symbol)
762 {
763   FILE *file = (FILE *) filep;
764   int reg, type;
765 
766   if (ELF_ST_TYPE (((elf_symbol_type *) symbol)->internal_elf_sym.st_info)
767       != STT_REGISTER)
768     return NULL;
769 
770   reg = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
771   type = symbol->flags;
772   fprintf (file, "REG_%c%c%11s%c%c    R", "GOLI" [reg / 8], '0' + (reg & 7), "",
773 		 ((type & BSF_LOCAL)
774 		  ? (type & BSF_GLOBAL) ? '!' : 'l'
775 		  : (type & BSF_GLOBAL) ? 'g' : ' '),
776 		 (type & BSF_WEAK) ? 'w' : ' ');
777   if (symbol->name == NULL || symbol->name [0] == '\0')
778     return "#scratch";
779   else
780     return symbol->name;
781 }
782 
783 /* Used to decide how to sort relocs in an optimal manner for the
784    dynamic linker, before writing them out.  */
785 
786 static enum elf_reloc_type_class
elf64_sparc_reloc_type_class(const struct bfd_link_info * info,const asection * rel_sec ATTRIBUTE_UNUSED,const Elf_Internal_Rela * rela)787 elf64_sparc_reloc_type_class (const struct bfd_link_info *info,
788 			      const asection *rel_sec ATTRIBUTE_UNUSED,
789 			      const Elf_Internal_Rela *rela)
790 {
791   bfd *abfd = info->output_bfd;
792   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
793   struct _bfd_sparc_elf_link_hash_table *htab
794     = _bfd_sparc_elf_hash_table (info);
795   BFD_ASSERT (htab != NULL);
796 
797   if (htab->elf.dynsym != NULL
798       && htab->elf.dynsym->contents != NULL)
799     {
800       /* Check relocation against STT_GNU_IFUNC symbol if there are
801 	 dynamic symbols.  */
802       unsigned long r_symndx = htab->r_symndx (rela->r_info);
803       if (r_symndx != STN_UNDEF)
804 	{
805 	  Elf_Internal_Sym sym;
806 	  if (!bed->s->swap_symbol_in (abfd,
807 				       (htab->elf.dynsym->contents
808 					+ r_symndx * bed->s->sizeof_sym),
809 				       0, &sym))
810 	    abort ();
811 
812 	  if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
813 	    return reloc_class_ifunc;
814 	}
815     }
816 
817   switch ((int) ELF64_R_TYPE (rela->r_info))
818     {
819     case R_SPARC_IRELATIVE:
820       return reloc_class_ifunc;
821     case R_SPARC_RELATIVE:
822       return reloc_class_relative;
823     case R_SPARC_JMP_SLOT:
824       return reloc_class_plt;
825     case R_SPARC_COPY:
826       return reloc_class_copy;
827     default:
828       return reloc_class_normal;
829     }
830 }
831 
832 /* Relocations in the 64 bit SPARC ELF ABI are more complex than in
833    standard ELF, because R_SPARC_OLO10 has secondary addend in
834    ELF64_R_TYPE_DATA field.  This structure is used to redirect the
835    relocation handling routines.  */
836 
837 const struct elf_size_info elf64_sparc_size_info =
838 {
839   sizeof (Elf64_External_Ehdr),
840   sizeof (Elf64_External_Phdr),
841   sizeof (Elf64_External_Shdr),
842   sizeof (Elf64_External_Rel),
843   sizeof (Elf64_External_Rela),
844   sizeof (Elf64_External_Sym),
845   sizeof (Elf64_External_Dyn),
846   sizeof (Elf_External_Note),
847   4,		/* hash-table entry size.  */
848   /* Internal relocations per external relocations.
849      For link purposes we use just 1 internal per
850      1 external, for assembly and slurp symbol table
851      we use 2.  */
852   1,
853   64,		/* arch_size.  */
854   3,		/* log_file_align.  */
855   ELFCLASS64,
856   EV_CURRENT,
857   bfd_elf64_write_out_phdrs,
858   bfd_elf64_write_shdrs_and_ehdr,
859   bfd_elf64_checksum_contents,
860   elf64_sparc_write_relocs,
861   bfd_elf64_swap_symbol_in,
862   bfd_elf64_swap_symbol_out,
863   elf64_sparc_slurp_reloc_table,
864   bfd_elf64_slurp_symbol_table,
865   bfd_elf64_swap_dyn_in,
866   bfd_elf64_swap_dyn_out,
867   bfd_elf64_swap_reloc_in,
868   bfd_elf64_swap_reloc_out,
869   bfd_elf64_swap_reloca_in,
870   bfd_elf64_swap_reloca_out
871 };
872 
873 #define TARGET_BIG_SYM	sparc_elf64_vec
874 #define TARGET_BIG_NAME	"elf64-sparc"
875 #define ELF_ARCH	bfd_arch_sparc
876 #define ELF_MAXPAGESIZE 0x100000
877 #define ELF_COMMONPAGESIZE 0x2000
878 
879 /* This is the official ABI value.  */
880 #define ELF_MACHINE_CODE EM_SPARCV9
881 
882 /* This is the value that we used before the ABI was released.  */
883 #define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
884 
885 #define elf_backend_reloc_type_class \
886   elf64_sparc_reloc_type_class
887 #define bfd_elf64_get_reloc_upper_bound \
888   elf64_sparc_get_reloc_upper_bound
889 #define bfd_elf64_get_dynamic_reloc_upper_bound \
890   elf64_sparc_get_dynamic_reloc_upper_bound
891 #define bfd_elf64_canonicalize_reloc \
892   elf64_sparc_canonicalize_reloc
893 #define bfd_elf64_canonicalize_dynamic_reloc \
894   elf64_sparc_canonicalize_dynamic_reloc
895 #define bfd_elf64_set_reloc \
896   elf64_sparc_set_reloc
897 #define elf_backend_add_symbol_hook \
898   elf64_sparc_add_symbol_hook
899 #define elf_backend_get_symbol_type \
900   elf64_sparc_get_symbol_type
901 #define elf_backend_symbol_processing \
902   elf64_sparc_symbol_processing
903 #define elf_backend_print_symbol_all \
904   elf64_sparc_print_symbol_all
905 #define elf_backend_output_arch_syms \
906   elf64_sparc_output_arch_syms
907 #define bfd_elf64_bfd_merge_private_bfd_data \
908   elf64_sparc_merge_private_bfd_data
909 #define elf_backend_fake_sections \
910   elf64_sparc_fake_sections
911 #define elf_backend_size_info \
912   elf64_sparc_size_info
913 
914 #define elf_backend_plt_sym_val	\
915   _bfd_sparc_elf_plt_sym_val
916 #define bfd_elf64_bfd_link_hash_table_create \
917   _bfd_sparc_elf_link_hash_table_create
918 #define elf_info_to_howto \
919   _bfd_sparc_elf_info_to_howto
920 #define elf_backend_copy_indirect_symbol \
921   _bfd_sparc_elf_copy_indirect_symbol
922 #define bfd_elf64_bfd_reloc_type_lookup \
923   _bfd_sparc_elf_reloc_type_lookup
924 #define bfd_elf64_bfd_reloc_name_lookup \
925   _bfd_sparc_elf_reloc_name_lookup
926 #define bfd_elf64_bfd_relax_section \
927   _bfd_sparc_elf_relax_section
928 #define bfd_elf64_new_section_hook \
929   _bfd_sparc_elf_new_section_hook
930 
931 #define elf_backend_create_dynamic_sections \
932   _bfd_sparc_elf_create_dynamic_sections
933 #define elf_backend_relocs_compatible \
934   _bfd_elf_relocs_compatible
935 #define elf_backend_check_relocs \
936   _bfd_sparc_elf_check_relocs
937 #define elf_backend_adjust_dynamic_symbol \
938   _bfd_sparc_elf_adjust_dynamic_symbol
939 #define elf_backend_omit_section_dynsym \
940   _bfd_sparc_elf_omit_section_dynsym
941 #define elf_backend_size_dynamic_sections \
942   _bfd_sparc_elf_size_dynamic_sections
943 #define elf_backend_relocate_section \
944   _bfd_sparc_elf_relocate_section
945 #define elf_backend_finish_dynamic_symbol \
946   _bfd_sparc_elf_finish_dynamic_symbol
947 #define elf_backend_finish_dynamic_sections \
948   _bfd_sparc_elf_finish_dynamic_sections
949 #define elf_backend_fixup_symbol \
950   _bfd_sparc_elf_fixup_symbol
951 
952 #define bfd_elf64_mkobject \
953   _bfd_sparc_elf_mkobject
954 #define elf_backend_object_p \
955   _bfd_sparc_elf_object_p
956 #define elf_backend_gc_mark_hook \
957   _bfd_sparc_elf_gc_mark_hook
958 #define elf_backend_init_index_section \
959   _bfd_elf_init_1_index_section
960 
961 #define elf_backend_can_gc_sections 1
962 #define elf_backend_can_refcount 1
963 #define elf_backend_want_got_plt 0
964 #define elf_backend_plt_readonly 0
965 #define elf_backend_want_plt_sym 1
966 #define elf_backend_got_header_size 8
967 #define elf_backend_want_dynrelro 1
968 #define elf_backend_rela_normal 1
969 
970 /* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table.  */
971 #define elf_backend_plt_alignment 8
972 
973 #include "elf64-target.h"
974 
975 /* FreeBSD support */
976 #undef  TARGET_BIG_SYM
977 #define TARGET_BIG_SYM sparc_elf64_fbsd_vec
978 #undef  TARGET_BIG_NAME
979 #define TARGET_BIG_NAME "elf64-sparc-freebsd"
980 #undef	ELF_OSABI
981 #define	ELF_OSABI ELFOSABI_FREEBSD
982 
983 #undef  elf64_bed
984 #define elf64_bed				elf64_sparc_fbsd_bed
985 
986 #include "elf64-target.h"
987 
988 /* Solaris 2.  */
989 
990 #undef	TARGET_BIG_SYM
991 #define	TARGET_BIG_SYM				sparc_elf64_sol2_vec
992 #undef	TARGET_BIG_NAME
993 #define	TARGET_BIG_NAME				"elf64-sparc-sol2"
994 
995 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
996    objects won't be recognized.  */
997 #undef	ELF_OSABI
998 
999 #undef elf64_bed
1000 #define elf64_bed				elf64_sparc_sol2_bed
1001 
1002 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
1003    boundary.  */
1004 #undef elf_backend_static_tls_alignment
1005 #define elf_backend_static_tls_alignment	16
1006 
1007 #undef  elf_backend_strtab_flags
1008 #define elf_backend_strtab_flags       SHF_STRINGS
1009 
1010 static bool
elf64_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)1011 elf64_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
1012                                   bfd *obfd ATTRIBUTE_UNUSED,
1013                                   const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
1014                                   Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
1015 {
1016   /* PR 19938: FIXME: Need to add code for setting the sh_info
1017      and sh_link fields of Solaris specific section types.  */
1018   return false;
1019 }
1020 
1021 #undef  elf_backend_copy_special_section_fields
1022 #define elf_backend_copy_special_section_fields elf64_sparc_copy_solaris_special_section_fields
1023 
1024 #include "elf64-target.h"
1025 
1026 #undef  elf_backend_strtab_flags
1027 #undef  elf_backend_copy_special_section_fields
1028