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