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