8aec9eaee9cd5cdcad71b8721a4ffe3edc17bc7b
[external/binutils.git] / bfd / elf32-arc.c
1 /* ARC-specific support for 32-bit ELF
2    Copyright (C) 1994-2017 Free Software Foundation, Inc.
3    Contributed by Cupertino Miranda (cmiranda@synopsys.com).
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/arc.h"
27 #include "libiberty.h"
28 #include "opcode/arc-func.h"
29 #include "opcode/arc.h"
30 #include "arc-plt.h"
31
32 #define FEATURE_LIST_NAME bfd_feature_list
33 #define CONFLICT_LIST bfd_conflict_list
34 #include "opcode/arc-attrs.h"
35
36 /* #define ARC_ENABLE_DEBUG 1  */
37 #ifdef ARC_ENABLE_DEBUG
38 static const char *
39 name_for_global_symbol (struct elf_link_hash_entry *h)
40 {
41   static char *local_str = "(local)";
42   if (h == NULL)
43     return local_str;
44   return h->root.root.string;
45 }
46 #define ARC_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
47 #else
48 #define ARC_DEBUG(...)
49 #endif
50
51
52 #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND)           \
53   {                                                                     \
54     struct elf_link_hash_table *_htab = elf_hash_table (info);          \
55     Elf_Internal_Rela _rel;                                             \
56     bfd_byte * _loc;                                                    \
57                                                                         \
58     if (_htab->dynamic_sections_created == TRUE)                                \
59       {                                                                 \
60         BFD_ASSERT (_htab->srel##SECTION &&_htab->srel##SECTION->contents); \
61         _loc = _htab->srel##SECTION->contents                           \
62           + ((_htab->srel##SECTION->reloc_count)                        \
63              * sizeof (Elf32_External_Rela));                           \
64         _htab->srel##SECTION->reloc_count++;                            \
65         _rel.r_addend = ADDEND;                                         \
66         _rel.r_offset = (_htab->s##SECTION)->output_section->vma        \
67           + (_htab->s##SECTION)->output_offset + OFFSET;                \
68         BFD_ASSERT ((long) SYM_IDX != -1);                              \
69         _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE);                     \
70         bfd_elf32_swap_reloca_out (BFD, &_rel, _loc);                   \
71       }                                                                 \
72   }
73
74
75 /* The default symbols representing the init and fini dyn values.
76    TODO: Check what is the relation of those strings with arclinux.em
77    and DT_INIT.  */
78 #define INIT_SYM_STRING "_init"
79 #define FINI_SYM_STRING "_fini"
80
81 char * init_str = INIT_SYM_STRING;
82 char * fini_str = FINI_SYM_STRING;
83
84 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
85       case VALUE: \
86         return "R_" #TYPE; \
87         break;
88
89 static ATTRIBUTE_UNUSED const char *
90 reloc_type_to_name (unsigned int type)
91 {
92   switch (type)
93     {
94       #include "elf/arc-reloc.def"
95
96       default:
97         return "UNKNOWN";
98         break;
99     }
100 }
101 #undef ARC_RELOC_HOWTO
102
103 /* Try to minimize the amount of space occupied by relocation tables
104    on the ROM (not that the ROM won't be swamped by other ELF overhead).  */
105
106 #define USE_REL 1
107
108 static ATTRIBUTE_UNUSED bfd_boolean
109 is_reloc_PC_relative (reloc_howto_type *howto)
110 {
111   return (strstr (howto->name, "PC") != NULL) ? TRUE : FALSE;
112 }
113
114 static bfd_boolean
115 is_reloc_SDA_relative (reloc_howto_type *howto)
116 {
117   return (strstr (howto->name, "SDA") != NULL) ? TRUE : FALSE;
118 }
119
120 static bfd_boolean
121 is_reloc_for_GOT (reloc_howto_type * howto)
122 {
123   if (strstr (howto->name, "TLS") != NULL)
124     return FALSE;
125   return (strstr (howto->name, "GOT") != NULL) ? TRUE : FALSE;
126 }
127
128 static bfd_boolean
129 is_reloc_for_PLT (reloc_howto_type * howto)
130 {
131   return (strstr (howto->name, "PLT") != NULL) ? TRUE : FALSE;
132 }
133
134 static bfd_boolean
135 is_reloc_for_TLS (reloc_howto_type *howto)
136 {
137   return (strstr (howto->name, "TLS") != NULL) ? TRUE : FALSE;
138 }
139
140 struct arc_relocation_data
141 {
142   bfd_signed_vma  reloc_offset;
143   bfd_signed_vma  reloc_addend;
144   bfd_signed_vma  got_offset_value;
145
146   bfd_signed_vma  sym_value;
147   asection *      sym_section;
148
149   reloc_howto_type *howto;
150
151   asection *      input_section;
152
153   bfd_signed_vma  sdata_begin_symbol_vma;
154   bfd_boolean     sdata_begin_symbol_vma_set;
155   bfd_signed_vma  got_symbol_vma;
156
157   bfd_boolean     should_relocate;
158
159   const char *    symbol_name;
160 };
161
162 /* Should be included at this location due to static declarations
163  * defined before this point.  */
164 #include "arc-got.h"
165
166 #define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
167 #define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
168 #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
169 #define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
170 #define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
171 #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
172
173
174 static bfd_reloc_status_type
175 arc_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED,
176                arelent *reloc_entry,
177                asymbol *symbol_in,
178                void *data ATTRIBUTE_UNUSED,
179                asection *input_section,
180                bfd *output_bfd,
181                char ** error_message ATTRIBUTE_UNUSED)
182 {
183   if (output_bfd != NULL)
184     {
185       reloc_entry->address += input_section->output_offset;
186
187       /* In case of relocateable link and if the reloc is against a
188          section symbol, the addend needs to be adjusted according to
189          where the section symbol winds up in the output section.  */
190       if ((symbol_in->flags & BSF_SECTION_SYM) && symbol_in->section)
191         reloc_entry->addend += symbol_in->section->output_offset;
192
193       return bfd_reloc_ok;
194     }
195
196   return bfd_reloc_continue;
197 }
198
199
200 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
201   TYPE = VALUE,
202 enum howto_list
203 {
204 #include "elf/arc-reloc.def"
205   HOWTO_LIST_LAST
206 };
207 #undef ARC_RELOC_HOWTO
208
209 #define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
210   [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, FALSE, 0,                \
211                   complain_overflow_##OVERFLOW, arc_elf_reloc,          \
212                   "R_" #TYPE, FALSE, 0, 0, FALSE),
213
214 static struct reloc_howto_struct elf_arc_howto_table[] =
215 {
216 #include "elf/arc-reloc.def"
217 /* Example of what is generated by the preprocessor.  Currently kept as an
218    example.
219  HOWTO (R_ARC_NONE, // Type.
220     0, // Rightshift.
221     2, // Size (0 = byte, 1 = short, 2 = long).
222     32, // Bitsize.
223     FALSE, // PC_relative.
224     0, // Bitpos.
225     complain_overflow_bitfield, // Complain_on_overflow.
226     bfd_elf_generic_reloc, // Special_function.
227     "R_ARC_NONE", // Name.
228     TRUE, // Partial_inplace.
229     0, // Src_mask.
230     0, // Dst_mask.
231     FALSE), // PCrel_offset.
232 */
233 };
234 #undef ARC_RELOC_HOWTO
235
236 static void arc_elf_howto_init (void)
237 {
238 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
239   elf_arc_howto_table[TYPE].pc_relative = \
240     (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
241   elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \
242   /* Only 32 bit data relocations should be marked as ME.  */ \
243   if (strstr (#FORMULA, " ME ") != NULL) \
244     { \
245       BFD_ASSERT (SIZE == 2); \
246     }
247
248 #include "elf/arc-reloc.def"
249
250 }
251 #undef ARC_RELOC_HOWTO
252
253
254 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
255   [TYPE] = VALUE,
256 const int howto_table_lookup[] =
257 {
258 #include "elf/arc-reloc.def"
259 };
260 #undef ARC_RELOC_HOWTO
261
262 static reloc_howto_type *
263 arc_elf_howto (unsigned int r_type)
264 {
265   if (elf_arc_howto_table[R_ARC_32].dst_mask == 0)
266     arc_elf_howto_init ();
267   return &elf_arc_howto_table[r_type];
268 }
269
270 /* Map BFD reloc types to ARC ELF reloc types.  */
271
272 struct arc_reloc_map
273 {
274   bfd_reloc_code_real_type  bfd_reloc_val;
275   unsigned char             elf_reloc_val;
276 };
277
278 /* ARC ELF linker hash entry.  */
279 struct elf_arc_link_hash_entry
280 {
281   struct elf_link_hash_entry root;
282
283   /* Track dynamic relocs copied for this symbol.  */
284   struct elf_dyn_relocs *dyn_relocs;
285 };
286
287 /* ARC ELF linker hash table.  */
288 struct elf_arc_link_hash_table
289 {
290   struct elf_link_hash_table elf;
291 };
292
293 static struct bfd_hash_entry *
294 elf_arc_link_hash_newfunc (struct bfd_hash_entry *entry,
295                            struct bfd_hash_table *table,
296                            const char *string)
297 {
298   /* Allocate the structure if it has not already been allocated by a
299      subclass.  */
300   if (entry == NULL)
301     {
302       entry = (struct bfd_hash_entry *)
303           bfd_hash_allocate (table,
304                              sizeof (struct elf_arc_link_hash_entry));
305       if (entry == NULL)
306         return entry;
307     }
308
309   /* Call the allocation method of the superclass.  */
310   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
311   if (entry != NULL)
312     {
313       struct elf_arc_link_hash_entry *eh;
314
315       eh = (struct elf_arc_link_hash_entry *) entry;
316       eh->dyn_relocs = NULL;
317     }
318
319   return entry;
320 }
321
322 /* Destroy an ARC ELF linker hash table.  */
323 static void
324 elf_arc_link_hash_table_free (bfd *obfd)
325 {
326   _bfd_elf_link_hash_table_free (obfd);
327 }
328
329 /* Create an ARC ELF linker hash table.  */
330
331 static struct bfd_link_hash_table *
332 arc_elf_link_hash_table_create (bfd *abfd)
333 {
334   struct elf_arc_link_hash_table *ret;
335
336   ret = (struct elf_arc_link_hash_table *) bfd_zmalloc (sizeof (*ret));
337   if (ret == NULL)
338     return NULL;
339
340   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
341                                       elf_arc_link_hash_newfunc,
342                                       sizeof (struct elf_arc_link_hash_entry),
343                                       ARC_ELF_DATA))
344     {
345       free (ret);
346       return NULL;
347     }
348
349   ret->elf.init_got_refcount.refcount = 0;
350   ret->elf.init_got_refcount.glist = NULL;
351   ret->elf.init_got_offset.offset = 0;
352   ret->elf.init_got_offset.glist = NULL;
353
354   ret->elf.root.hash_table_free = elf_arc_link_hash_table_free;
355
356   return &ret->elf.root;
357 }
358
359 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
360   { BFD_RELOC_##TYPE, R_##TYPE },
361 static const struct arc_reloc_map arc_reloc_map[] =
362 {
363 #include "elf/arc-reloc.def"
364
365   {BFD_RELOC_NONE,  R_ARC_NONE},
366   {BFD_RELOC_8,  R_ARC_8},
367   {BFD_RELOC_16, R_ARC_16},
368   {BFD_RELOC_24, R_ARC_24},
369   {BFD_RELOC_32, R_ARC_32},
370 };
371 #undef ARC_RELOC_HOWTO
372
373 typedef ATTRIBUTE_UNUSED bfd_vma (*replace_func) (unsigned, int ATTRIBUTE_UNUSED);
374
375 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
376   case TYPE: \
377     func = (void *) RELOC_FUNCTION; \
378     break;
379 static replace_func
380 get_replace_function (bfd *abfd, unsigned int r_type)
381 {
382   void *func = NULL;
383
384   switch (r_type)
385     {
386       #include "elf/arc-reloc.def"
387     }
388
389   if (func == replace_bits24 && bfd_big_endian (abfd))
390     return (replace_func) replace_bits24_be;
391
392   return (replace_func) func;
393 }
394 #undef ARC_RELOC_HOWTO
395
396 static reloc_howto_type *
397 arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
398                                  bfd_reloc_code_real_type code)
399 {
400   unsigned int i;
401
402   for (i = ARRAY_SIZE (arc_reloc_map); i--;)
403     {
404       if (arc_reloc_map[i].bfd_reloc_val == code)
405         return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
406     }
407
408   return NULL;
409 }
410
411 /* Function to set the ELF flag bits.  */
412 static bfd_boolean
413 arc_elf_set_private_flags (bfd *abfd, flagword flags)
414 {
415   elf_elfheader (abfd)->e_flags = flags;
416   elf_flags_init (abfd) = TRUE;
417   return TRUE;
418 }
419
420 /* Print private flags.  */
421 static bfd_boolean
422 arc_elf_print_private_bfd_data (bfd *abfd, void * ptr)
423 {
424   FILE *file = (FILE *) ptr;
425   flagword flags;
426
427   BFD_ASSERT (abfd != NULL && ptr != NULL);
428
429   /* Print normal ELF private data.  */
430   _bfd_elf_print_private_bfd_data (abfd, ptr);
431
432   flags = elf_elfheader (abfd)->e_flags;
433   fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
434
435   switch (flags & EF_ARC_MACH_MSK)
436     {
437     case EF_ARC_CPU_ARCV2HS : fprintf (file, " -mcpu=ARCv2HS");    break;
438     case EF_ARC_CPU_ARCV2EM : fprintf (file, " -mcpu=ARCv2EM");    break;
439     case E_ARC_MACH_ARC600  : fprintf (file, " -mcpu=ARC600");     break;
440     case E_ARC_MACH_ARC601  : fprintf (file, " -mcpu=ARC601");     break;
441     case E_ARC_MACH_ARC700  : fprintf (file, " -mcpu=ARC700");     break;
442     default:
443       fprintf (file, "-mcpu=unknown");
444       break;
445     }
446
447   switch (flags & EF_ARC_OSABI_MSK)
448     {
449     case E_ARC_OSABI_ORIG : fprintf (file, " (ABI:legacy)"); break;
450     case E_ARC_OSABI_V2   : fprintf (file, " (ABI:v2)");     break;
451     case E_ARC_OSABI_V3   : fprintf (file, " (ABI:v3)");     break;
452     case E_ARC_OSABI_V4   : fprintf (file, " (ABI:v4)");     break;
453     default:
454       fprintf (file, " (ABI:unknown)");
455       break;
456     }
457
458   fputc ('\n', file);
459   return TRUE;
460 }
461
462 /* Copy backend specific data from one object module to another.  */
463
464 static bfd_boolean
465 arc_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
466 {
467   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
468       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
469     return TRUE;
470
471   BFD_ASSERT (!elf_flags_init (obfd)
472               || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
473
474   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
475   elf_flags_init (obfd) = TRUE;
476
477   /* Copy object attributes.  */
478   _bfd_elf_copy_obj_attributes (ibfd, obfd);
479
480   return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
481 }
482
483 static reloc_howto_type *
484 bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
485                                  const char *r_name)
486 {
487   unsigned int i;
488
489   for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
490     if (elf_arc_howto_table[i].name != NULL
491         && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
492       return arc_elf_howto (i);
493
494   return NULL;
495 }
496
497 /* Set the howto pointer for an ARC ELF reloc.  */
498
499 static void
500 arc_info_to_howto_rel (bfd * abfd ATTRIBUTE_UNUSED,
501                        arelent * cache_ptr,
502                        Elf_Internal_Rela * dst)
503 {
504   unsigned int r_type;
505
506   r_type = ELF32_R_TYPE (dst->r_info);
507   BFD_ASSERT (r_type < (unsigned int) R_ARC_max);
508   cache_ptr->howto = arc_elf_howto (r_type);
509 }
510
511 /* Extract CPU features from an NTBS.  */
512
513 static unsigned
514 arc_extract_features (const char *p)
515 {
516   unsigned i, r = 0;
517
518   if (!p)
519     return 0;
520
521   for (i = 0; i < ARRAY_SIZE (bfd_feature_list); i++)
522     {
523       char *t = strstr (p, bfd_feature_list[i].attr);
524       unsigned l = strlen (bfd_feature_list[i].attr);
525       if ((t != NULL)
526           && (t[l] == ','
527               || t[l] == '\0'))
528         r |= bfd_feature_list[i].feature;
529     }
530
531   return r;
532 }
533
534 /* Concatenate two strings.  s1 can be NULL but not
535    s2.  */
536
537 static char *
538 arc_stralloc (char * s1, const char * s2)
539 {
540   char *p;
541
542   /* Only s1 can be null.  */
543   BFD_ASSERT (s2);
544
545   p = s1 ? concat (s1, ",", s2, NULL) : (char *)s2;
546
547   return p;
548 }
549
550 /* Merge ARC object attributes from IBFD into OBFD.  Raise an error if
551    there are conflicting attributes.  */
552
553 static bfd_boolean
554 arc_elf_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
555 {
556   bfd *obfd = info->output_bfd;
557   obj_attribute *in_attr;
558   obj_attribute *out_attr;
559   int i;
560   bfd_boolean result = TRUE;
561   const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
562   char *tagname = NULL;
563
564   /* Skip the linker stubs file.  This preserves previous behavior
565      of accepting unknown attributes in the first input file - but
566      is that a bug?  */
567   if (ibfd->flags & BFD_LINKER_CREATED)
568     return TRUE;
569
570   /* Skip any input that hasn't attribute section.
571      This enables to link object files without attribute section with
572      any others.  */
573   if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
574     return TRUE;
575
576   if (!elf_known_obj_attributes_proc (obfd)[0].i)
577     {
578       /* This is the first object.  Copy the attributes.  */
579       _bfd_elf_copy_obj_attributes (ibfd, obfd);
580
581       out_attr = elf_known_obj_attributes_proc (obfd);
582
583       /* Use the Tag_null value to indicate the attributes have been
584          initialized.  */
585       out_attr[0].i = 1;
586
587       return TRUE;
588     }
589
590   in_attr = elf_known_obj_attributes_proc (ibfd);
591   out_attr = elf_known_obj_attributes_proc (obfd);
592
593   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
594     {
595       /* Merge this attribute with existing attributes.  */
596       switch (i)
597         {
598         case Tag_ARC_PCS_config:
599           if (out_attr[i].i == 0)
600             out_attr[i].i = in_attr[i].i;
601           else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
602             {
603               const char *tagval[] = { "Absent", "Bare-metal/mwdt",
604                                         "Bare-metal/newlib", "Linux/uclibc",
605                                         "Linux/glibc" };
606               BFD_ASSERT (in_attr[i].i < 5);
607               BFD_ASSERT (out_attr[i].i < 5);
608               /* It's sometimes ok to mix different configs, so this is only
609                  a warning.  */
610               _bfd_error_handler
611                 (_("Warning: %B: Conflicting platform configuration "
612                    "%s with %s.\n"), ibfd,
613                  tagval[in_attr[i].i],
614                  tagval[out_attr[i].i]);
615             }
616           break;
617
618         case Tag_ARC_CPU_base:
619           if (out_attr[i].i == 0)
620             out_attr[i].i = in_attr[i].i;
621           else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i
622                    && ((out_attr[i].i + in_attr[i].i) < 6))
623             {
624               const char *tagval[] = { "Absent", "ARC6xx", "ARC7xx",
625                                         "ARCEM", "ARCHS" };
626               BFD_ASSERT (in_attr[i].i < 5);
627               BFD_ASSERT (out_attr[i].i < 5);
628               /* We cannot mix code for different CPUs.  */
629               _bfd_error_handler
630                 (_("error: %B: unable to merge CPU base attributes "
631                    "%s with %s.\n"),
632                  obfd,
633                  tagval[in_attr[i].i],
634                  tagval[out_attr[i].i]);
635               result = FALSE;
636               break;
637             }
638           else
639             {
640               /* The CPUs may be different, check if we can still mix
641                  the objects against the output choosen CPU.  */
642               unsigned in_feature = 0;
643               unsigned out_feature = 0;
644               char *p1 = in_attr[Tag_ARC_ISA_config].s;
645               char *p2 = out_attr[Tag_ARC_ISA_config].s;
646               unsigned j;
647               unsigned cpu_out;
648               unsigned opcode_map[] = {0, ARC_OPCODE_ARC600, ARC_OPCODE_ARC700,
649                                        ARC_OPCODE_ARCv2EM, ARC_OPCODE_ARCv2HS};
650
651               BFD_ASSERT (in_attr[i].i < (sizeof (opcode_map)
652                                           / sizeof (unsigned)));
653               BFD_ASSERT (out_attr[i].i < (sizeof (opcode_map)
654                                            / sizeof (unsigned)));
655               cpu_out = opcode_map[out_attr[i].i];
656
657               in_feature = arc_extract_features (p1);
658               out_feature = arc_extract_features (p2);
659
660               /* First, check if a feature is compatible with the
661                  output object chosen CPU.  */
662               for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
663                 if (((in_feature | out_feature) & bfd_feature_list[j].feature)
664                     && (!(cpu_out & bfd_feature_list[j].cpus)))
665                   {
666                     _bfd_error_handler
667                       (_("error: %B: unable to merge ISA extension attributes "
668                          "%s.\n"),
669                        obfd, bfd_feature_list[j].name);
670                     result = FALSE;
671                     break;
672                   }
673               /* Second, if we have compatible features with the
674                  chosen CPU, check if they are compatible among
675                  them.  */
676               for (j = 0; j < ARRAY_SIZE (bfd_conflict_list); j++)
677                 if (((in_feature | out_feature) & bfd_conflict_list[j])
678                     == bfd_conflict_list[j])
679                   {
680                     unsigned k;
681                     for (k = 0; k < ARRAY_SIZE (bfd_feature_list); k++)
682                       {
683                         if (in_feature &  bfd_feature_list[k].feature
684                             & bfd_conflict_list[j])
685                           p1 = (char *) bfd_feature_list[k].name;
686                         if (out_feature &  bfd_feature_list[k].feature
687                             & bfd_conflict_list[j])
688                           p2 = (char *) bfd_feature_list[k].name;
689                       }
690                     _bfd_error_handler
691                       (_("error: %B: conflicting ISA extension attributes "
692                          "%s with %s.\n"),
693                        obfd, p1, p2);
694                     result = FALSE;
695                     break;
696                   }
697               /* Everithing is alright.  */
698               out_feature |= in_feature;
699               p1 = NULL;
700               for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
701                 if (out_feature & bfd_feature_list[j].feature)
702                   p1 = arc_stralloc (p1, bfd_feature_list[j].attr);
703               if (p1)
704                 out_attr[Tag_ARC_ISA_config].s =
705                   _bfd_elf_attr_strdup (obfd, p1);
706             }
707           /* Fall through.  */
708         case Tag_ARC_CPU_variation:
709         case Tag_ARC_ISA_mpy_option:
710         case Tag_ARC_ABI_osver:
711           /* Use the largest value specified.  */
712           if (in_attr[i].i > out_attr[i].i)
713             out_attr[i].i = in_attr[i].i;
714           break;
715
716         case Tag_ARC_CPU_name:
717           break;
718
719         case Tag_ARC_ABI_rf16:
720           if (out_attr[i].i == 0)
721             out_attr[i].i = in_attr[i].i;
722           else if (out_attr[i].i != in_attr[i].i)
723             {
724               /* We cannot mix code with rf16 and without.  */
725               _bfd_error_handler
726                 (_("error: %B: cannot mix rf16 with full register set %B.\n"),
727                  obfd, ibfd);
728               result = FALSE;
729             }
730           break;
731
732         case Tag_ARC_ABI_pic:
733           tagname = "PIC";
734           /* fall through */
735         case Tag_ARC_ABI_sda:
736           if (!tagname)
737             tagname = "SDA";
738           /* fall through */
739         case Tag_ARC_ABI_tls:
740           {
741             const char *tagval[] = { "Absent", "MWDT", "GNU" };
742
743             if (!tagname)
744               tagname = "TLS";
745
746             BFD_ASSERT (in_attr[i].i < 3);
747             BFD_ASSERT (out_attr[i].i < 3);
748             if (out_attr[i].i != 0 && in_attr[i].i != 0
749                 && out_attr[i].i != in_attr[i].i)
750               {
751                 _bfd_error_handler
752                   (_("error: %B: conflicting attributes %s: %s with %s.\n"),
753                    obfd, tagname,
754                    tagval[in_attr[i].i],
755                    tagval[out_attr[i].i]);
756                 result = FALSE;
757               }
758             tagname = NULL;
759             break;
760           }
761
762         case Tag_ARC_ABI_double_size:
763           tagname = "Double size";
764           /* fall through */
765         case Tag_ARC_ABI_enumsize:
766           if (!tagname)
767             tagname = "Enum size";
768           /* fall through */
769         case Tag_ARC_ABI_exceptions:
770           if (!tagname)
771             tagname = "ABI exceptions";
772
773           if (out_attr[i].i != 0 && in_attr[i].i != 0
774               && out_attr[i].i != in_attr[i].i)
775             {
776               _bfd_error_handler
777                 (_("error: %B: conflicting attributes %s.\n"),
778                  obfd, tagname);
779               result = FALSE;
780             }
781           break;
782
783         case Tag_ARC_ISA_apex:
784           break; /* Do nothing for APEX attributes.  */
785
786         case Tag_ARC_ISA_config:
787           /* It is handled in Tag_ARC_CPU_base.  */
788           break;
789
790         default:
791           result
792             = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
793         }
794
795       /* If out_attr was copied from in_attr then it won't have a type yet.  */
796       if (in_attr[i].type && !out_attr[i].type)
797         out_attr[i].type = in_attr[i].type;
798     }
799
800   /* Merge Tag_compatibility attributes and any common GNU ones.  */
801   if (!_bfd_elf_merge_object_attributes (ibfd, info))
802     return FALSE;
803
804   /* Check for any attributes not known on ARC.  */
805   result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
806
807   return result;
808 }
809
810 /* Merge backend specific data from an object file to the output
811    object file when linking.  */
812
813 static bfd_boolean
814 arc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
815 {
816   bfd *obfd = info->output_bfd;
817   unsigned short mach_ibfd;
818   static unsigned short mach_obfd = EM_NONE;
819   flagword out_flags;
820   flagword in_flags;
821   asection *sec;
822
823    /* Check if we have the same endianess.  */
824   if (! _bfd_generic_verify_endian_match (ibfd, info))
825     return FALSE;
826
827   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
828       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
829     return TRUE;
830
831   /* Collect ELF flags.  */
832   in_flags = elf_elfheader (ibfd)->e_flags & EF_ARC_MACH_MSK;
833   out_flags = elf_elfheader (obfd)->e_flags & EF_ARC_MACH_MSK;
834
835   if (!elf_flags_init (obfd)) /* First call, no flags set.  */
836     {
837       elf_flags_init (obfd) = TRUE;
838       out_flags = in_flags;
839     }
840
841   if (!arc_elf_merge_attributes (ibfd, info))
842     return FALSE;
843
844   /* Check to see if the input BFD actually contains any sections.  Do
845      not short-circuit dynamic objects; their section list may be
846      emptied by elf_link_add_object_symbols.  */
847   if (!(ibfd->flags & DYNAMIC))
848     {
849       bfd_boolean null_input_bfd = TRUE;
850       bfd_boolean only_data_sections = TRUE;
851
852       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
853         {
854           if ((bfd_get_section_flags (ibfd, sec)
855                & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
856               == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
857             only_data_sections = FALSE;
858
859           null_input_bfd = FALSE;
860         }
861
862       if (null_input_bfd || only_data_sections)
863         return TRUE;
864     }
865
866   /* Complain about various flag/architecture mismatches.  */
867   mach_ibfd = elf_elfheader (ibfd)->e_machine;
868   if (mach_obfd == EM_NONE)
869     {
870       mach_obfd = mach_ibfd;
871     }
872   else
873     {
874       if (mach_ibfd != mach_obfd)
875         {
876           /* xgettext:c-format */
877           _bfd_error_handler (_("ERROR: Attempting to link %B "
878                                 "with a binary %B of different architecture"),
879                               ibfd, obfd);
880           return FALSE;
881         }
882       else if ((in_flags != out_flags)
883                /* If we have object attributes, then we already
884                   checked the objects compatibility, skip it.  */
885                && !bfd_elf_get_obj_attr_int (ibfd, OBJ_ATTR_PROC,
886                                              Tag_ARC_CPU_base))
887         {
888           /* Warn if different flags.  */
889           _bfd_error_handler
890             /* xgettext:c-format */
891             (_("%B: uses different e_flags (0x%lx) fields than "
892                "previous modules (0x%lx)"),
893              ibfd, (long) in_flags, (long) out_flags);
894           if (in_flags && out_flags)
895             return FALSE;
896           /* MWDT doesnt set the eflags hence make sure we choose the
897              eflags set by gcc.  */
898           in_flags = in_flags > out_flags ? in_flags : out_flags;
899         }
900       else
901         {
902           /* Everything is correct; don't change the output flags.  */
903           in_flags = out_flags;
904         }
905     }
906
907   /* Update the flags.  */
908   elf_elfheader (obfd)->e_flags = in_flags;
909
910   if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
911     {
912       return bfd_set_arch_mach (obfd, bfd_arch_arc, bfd_get_mach (ibfd));
913     }
914
915   return TRUE;
916 }
917
918 /* Return a best guess for the machine number based on the attributes.  */
919
920 static unsigned int
921 bfd_arc_get_mach_from_attributes (bfd * abfd)
922 {
923   int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ARC_CPU_base);
924   unsigned e_machine = elf_elfheader (abfd)->e_machine;
925
926   switch (arch)
927     {
928     case TAG_CPU_ARC6xx:
929       return bfd_mach_arc_arc600;
930     case TAG_CPU_ARC7xx:
931       return bfd_mach_arc_arc700;
932     case TAG_CPU_ARCEM:
933     case TAG_CPU_ARCHS:
934       return bfd_mach_arc_arcv2;
935     default:
936       break;
937     }
938   return (e_machine == EM_ARC_COMPACT)
939     ? bfd_mach_arc_arc700 : bfd_mach_arc_arcv2;
940 }
941
942 /* Set the right machine number for an ARC ELF file.  */
943 static bfd_boolean
944 arc_elf_object_p (bfd * abfd)
945 {
946   /* Make sure this is initialised, or you'll have the potential of passing
947      garbage---or misleading values---into the call to
948      bfd_default_set_arch_mach ().  */
949   unsigned int    mach = bfd_mach_arc_arc700;
950   unsigned long   arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH_MSK;
951   unsigned        e_machine = elf_elfheader (abfd)->e_machine;
952
953   if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
954     {
955       switch (arch)
956         {
957           case E_ARC_MACH_ARC600:
958             mach = bfd_mach_arc_arc600;
959             break;
960           case E_ARC_MACH_ARC601:
961             mach = bfd_mach_arc_arc601;
962             break;
963           case E_ARC_MACH_ARC700:
964             mach = bfd_mach_arc_arc700;
965             break;
966           case EF_ARC_CPU_ARCV2HS:
967           case EF_ARC_CPU_ARCV2EM:
968             mach = bfd_mach_arc_arcv2;
969             break;
970           default:
971             mach = bfd_arc_get_mach_from_attributes (abfd);
972             break;
973         }
974     }
975   else
976     {
977       if (e_machine == EM_ARC)
978         {
979           _bfd_error_handler
980             (_("Error: The ARC4 architecture is no longer supported.\n"));
981           return FALSE;
982         }
983       else
984         {
985           _bfd_error_handler
986             (_("Warning: unset or old architecture flags. \n"
987                "               Use default machine.\n"));
988         }
989     }
990
991   return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
992 }
993
994 /* The final processing done just before writing out an ARC ELF object file.
995    This gets the ARC architecture right based on the machine number.  */
996
997 static void
998 arc_elf_final_write_processing (bfd * abfd,
999                                 bfd_boolean linker ATTRIBUTE_UNUSED)
1000 {
1001   unsigned long emf;
1002   int osver = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC,
1003                                         Tag_ARC_ABI_osver);
1004   flagword e_flags = elf_elfheader (abfd)->e_flags & ~EF_ARC_OSABI_MSK;
1005
1006   switch (bfd_get_mach (abfd))
1007     {
1008     case bfd_mach_arc_arc600:
1009       emf = EM_ARC_COMPACT;
1010       break;
1011     case bfd_mach_arc_arc601:
1012       emf = EM_ARC_COMPACT;
1013       break;
1014     case bfd_mach_arc_arc700:
1015       emf = EM_ARC_COMPACT;
1016       break;
1017     case bfd_mach_arc_arcv2:
1018       emf = EM_ARC_COMPACT2;
1019       break;
1020     default:
1021       return;
1022     }
1023
1024   elf_elfheader (abfd)->e_machine = emf;
1025
1026   /* Record whatever is the current syscall ABI version.  */
1027   if (osver)
1028     e_flags |= ((osver & 0x0f) << 8);
1029   else
1030     e_flags |= E_ARC_OSABI_V3;
1031
1032   elf_elfheader (abfd)->e_flags |=  e_flags;
1033 }
1034
1035 #ifdef ARC_ENABLE_DEBUG
1036 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
1037
1038 static void
1039 debug_arc_reloc (struct arc_relocation_data reloc_data)
1040 {
1041   ARC_DEBUG ("Reloc type=%s, should_relocate = %s\n",
1042              reloc_data.howto->name,
1043              reloc_data.should_relocate ? "true" : "false");
1044   ARC_DEBUG ("  offset = 0x%x, addend = 0x%x\n",
1045              (unsigned int) reloc_data.reloc_offset,
1046              (unsigned int) reloc_data.reloc_addend);
1047   ARC_DEBUG (" Symbol:\n");
1048   ARC_DEBUG ("  value = 0x%08x\n",
1049              (unsigned int) reloc_data.sym_value);
1050   if (reloc_data.sym_section != NULL)
1051     {
1052       ARC_DEBUG (" Symbol Section:\n");
1053       ARC_DEBUG ("  section name = %s, output_offset 0x%08x",
1054                  reloc_data.sym_section->name,
1055                  (unsigned int) reloc_data.sym_section->output_offset);
1056       if (reloc_data.sym_section->output_section != NULL)
1057         ARC_DEBUG (", output_section->vma = 0x%08x",
1058                    ((unsigned int) reloc_data.sym_section->output_section->vma));
1059       ARC_DEBUG ("\n");
1060       if (reloc_data.sym_section->owner && reloc_data.sym_section->owner->filename)
1061         ARC_DEBUG ("  file: %s\n", reloc_data.sym_section->owner->filename);
1062     }
1063   else
1064     {
1065       ARC_DEBUG ("  symbol section is NULL\n");
1066     }
1067
1068   ARC_DEBUG (" Input_section:\n");
1069   if (reloc_data.input_section != NULL)
1070     {
1071       ARC_DEBUG ("  section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
1072                  reloc_data.input_section->name,
1073                  (unsigned int) reloc_data.input_section->output_offset,
1074                  (unsigned int) reloc_data.input_section->output_section->vma);
1075       ARC_DEBUG ("  changed_address = 0x%08x\n",
1076                  (unsigned int) (reloc_data.input_section->output_section->vma
1077                                  + reloc_data.input_section->output_offset
1078                                  + reloc_data.reloc_offset));
1079       ARC_DEBUG ("  file: %s\n", reloc_data.input_section->owner->filename);
1080     }
1081   else
1082     {
1083       ARC_DEBUG ("      input section is NULL\n");
1084     }
1085 }
1086 #else
1087 #define DEBUG_ARC_RELOC(A)
1088 #endif /* ARC_ENABLE_DEBUG */
1089
1090 static bfd_vma
1091 middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
1092 {
1093   if (do_it)
1094     {
1095       insn
1096         = ((insn & 0xffff0000) >> 16)
1097           | ((insn & 0xffff) << 16);
1098     }
1099   return insn;
1100 }
1101
1102 /* This function is called for relocations that are otherwise marked as NOT
1103    requiring overflow checks.  In here we perform non-standard checks of
1104    the relocation value.  */
1105
1106 static inline bfd_reloc_status_type
1107 arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
1108                              bfd_signed_vma relocation,
1109                              struct bfd_link_info *info ATTRIBUTE_UNUSED)
1110 {
1111   switch (reloc_data.howto->type)
1112     {
1113     case R_ARC_NPS_CMEM16:
1114       if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
1115         {
1116           if (reloc_data.reloc_addend == 0)
1117             _bfd_error_handler
1118               /* xgettext:c-format */
1119               (_("%B(%A+0x%lx): CMEM relocation to `%s' is invalid, "
1120                  "16 MSB should be 0x%04x (value is 0x%lx)"),
1121                reloc_data.input_section->owner,
1122                reloc_data.input_section,
1123                reloc_data.reloc_offset,
1124                reloc_data.symbol_name,
1125                NPS_CMEM_HIGH_VALUE,
1126                (relocation));
1127           else
1128             _bfd_error_handler
1129               /* xgettext:c-format */
1130               (_("%B(%A+0x%lx): CMEM relocation to `%s+0x%lx' is invalid, "
1131                  "16 MSB should be 0x%04x (value is 0x%lx)"),
1132                reloc_data.input_section->owner,
1133                reloc_data.input_section,
1134                reloc_data.reloc_offset,
1135                reloc_data.symbol_name,
1136                reloc_data.reloc_addend,
1137                NPS_CMEM_HIGH_VALUE,
1138                (relocation));
1139           return bfd_reloc_overflow;
1140         }
1141       break;
1142
1143     default:
1144       break;
1145     }
1146
1147   return bfd_reloc_ok;
1148 }
1149
1150 #define ME(reloc) (reloc)
1151
1152 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
1153                             && (!bfd_big_endian (BFD)))
1154
1155 #define S ((bfd_signed_vma) (reloc_data.sym_value                       \
1156            + (reloc_data.sym_section->output_section != NULL ?          \
1157               (reloc_data.sym_section->output_offset                    \
1158                + reloc_data.sym_section->output_section->vma) : 0)))
1159 #define L ((bfd_signed_vma) (reloc_data.sym_value                       \
1160            + (reloc_data.sym_section->output_section != NULL ?          \
1161               (reloc_data.sym_section->output_offset                    \
1162               + reloc_data.sym_section->output_section->vma) : 0)))
1163 #define A (reloc_data.reloc_addend)
1164 #define B (0)
1165 #define G (reloc_data.got_offset_value)
1166 #define GOT (reloc_data.got_symbol_vma)
1167 #define GOT_BEGIN (htab->sgot->output_section->vma)
1168
1169 #define MES (0)
1170         /* P: relative offset to PCL The offset should be to the
1171           current location aligned to 32 bits.  */
1172 #define P ((bfd_signed_vma) (                                           \
1173            (                                                            \
1174             (reloc_data.input_section->output_section != NULL ?         \
1175              reloc_data.input_section->output_section->vma : 0)         \
1176             + reloc_data.input_section->output_offset                   \
1177             + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0)))      \
1178            & ~0x3))
1179 #define PDATA ((bfd_signed_vma) ( \
1180             (reloc_data.input_section->output_section->vma \
1181              + reloc_data.input_section->output_offset \
1182              + (reloc_data.reloc_offset))))
1183 #define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
1184                                     + reloc_data.sym_section->output_offset)
1185
1186 #define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
1187 #define TLS_REL (bfd_signed_vma) \
1188   ((elf_hash_table (info))->tls_sec->output_section->vma)
1189 #define TLS_TBSS (8)
1190 #define TCB_SIZE (8)
1191
1192 #define none (0)
1193
1194 #ifdef ARC_ENABLE_DEBUG
1195 #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE)                    \
1196   do                                                                    \
1197     {                                                                   \
1198       asection *sym_section = reloc_data.sym_section;                   \
1199       asection *input_section = reloc_data.input_section;               \
1200       ARC_DEBUG ("RELOC_TYPE = " TYPE "\n");                            \
1201       ARC_DEBUG ("FORMULA = " FORMULA "\n");                            \
1202       ARC_DEBUG ("S = %#lx\n", S);                                      \
1203       ARC_DEBUG ("A = %#lx\n", A);                                      \
1204       ARC_DEBUG ("L = %lx\n", L);                                       \
1205       if (sym_section->output_section != NULL)                          \
1206         ARC_DEBUG ("symbol_section->vma = %#lx\n",                      \
1207                    sym_section->output_section->vma                     \
1208                    + sym_section->output_offset);                       \
1209       else                                                              \
1210         ARC_DEBUG ("symbol_section->vma = NULL\n");                     \
1211       if (input_section->output_section != NULL)                        \
1212         ARC_DEBUG ("symbol_section->vma = %#lx\n",                      \
1213                    input_section->output_section->vma                   \
1214                    + input_section->output_offset);                     \
1215       else                                                              \
1216         ARC_DEBUG ("symbol_section->vma = NULL\n");                     \
1217       ARC_DEBUG ("PCL = %#lx\n", P);                                    \
1218       ARC_DEBUG ("P = %#lx\n", P);                                      \
1219       ARC_DEBUG ("G = %#lx\n", G);                                      \
1220       ARC_DEBUG ("SDA_OFFSET = %#lx\n", _SDA_BASE_);                    \
1221       ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
1222       ARC_DEBUG ("GOT_OFFSET = %#lx\n", GOT);                           \
1223       ARC_DEBUG ("relocation = %#08lx\n", relocation);                  \
1224       ARC_DEBUG ("before = %#08x\n", (unsigned) insn);                  \
1225       ARC_DEBUG ("data   = %08x (%u) (%d)\n", (unsigned) relocation,    \
1226                  (unsigned) relocation, (int) relocation);              \
1227     }                                                                   \
1228   while (0)
1229
1230 #define PRINT_DEBUG_RELOC_INFO_AFTER                            \
1231   do                                                            \
1232     {                                                           \
1233       ARC_DEBUG ("after  = 0x%08x\n", (unsigned int) insn);     \
1234     }                                                           \
1235   while (0)
1236
1237 #else
1238
1239 #define PRINT_DEBUG_RELOC_INFO_BEFORE(...)
1240 #define PRINT_DEBUG_RELOC_INFO_AFTER 
1241
1242 #endif /* ARC_ENABLE_DEBUG */
1243
1244 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
1245   case R_##TYPE:                                                        \
1246     {                                                                   \
1247       bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE;                \
1248       relocation = FORMULA  ;                                           \
1249       PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE);                  \
1250       insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd));      \
1251       insn = (* get_replace_function (abfd, TYPE)) (insn, relocation);  \
1252       insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd));      \
1253       PRINT_DEBUG_RELOC_INFO_AFTER;                                     \
1254     }                                                                   \
1255     break;
1256
1257 static bfd_reloc_status_type
1258 arc_do_relocation (bfd_byte * contents,
1259                    struct arc_relocation_data reloc_data,
1260                    struct bfd_link_info *info)
1261 {
1262   bfd_signed_vma relocation = 0;
1263   bfd_vma insn;
1264   bfd_vma orig_insn ATTRIBUTE_UNUSED;
1265   bfd * abfd = reloc_data.input_section->owner;
1266   struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
1267   bfd_reloc_status_type flag;
1268
1269   if (!reloc_data.should_relocate)
1270     return bfd_reloc_ok;
1271
1272   switch (reloc_data.howto->size)
1273     {
1274       case 2:
1275         insn = arc_bfd_get_32 (abfd,
1276                                contents + reloc_data.reloc_offset,
1277                                reloc_data.input_section);
1278         break;
1279       case 1:
1280         insn = arc_bfd_get_16 (abfd,
1281                                contents + reloc_data.reloc_offset,
1282                                reloc_data.input_section);
1283         break;
1284       case 0:
1285         insn = arc_bfd_get_8 (abfd,
1286                                contents + reloc_data.reloc_offset,
1287                                reloc_data.input_section);
1288         break;
1289       default:
1290         insn = 0;
1291         BFD_ASSERT (0);
1292         break;
1293     }
1294
1295   orig_insn = insn;
1296
1297   switch (reloc_data.howto->type)
1298     {
1299 #include "elf/arc-reloc.def"
1300
1301       default:
1302         BFD_ASSERT (0);
1303         break;
1304     }
1305
1306   /* Check for relocation overflow.  */
1307   if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
1308     flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
1309                                reloc_data.howto->bitsize,
1310                                reloc_data.howto->rightshift,
1311                                bfd_arch_bits_per_address (abfd),
1312                                relocation);
1313   else
1314     flag = arc_special_overflow_checks (reloc_data, relocation, info);
1315
1316   if (flag != bfd_reloc_ok)
1317     {
1318       ARC_DEBUG ("Relocation overflows !\n");
1319       DEBUG_ARC_RELOC (reloc_data);
1320       ARC_DEBUG ("Relocation value = signed -> %d, unsigned -> %u"
1321                  ", hex -> (0x%08x)\n",
1322                 (int) relocation, (unsigned) relocation, (int) relocation);
1323
1324       return flag;
1325     }
1326
1327   /* Write updated instruction back to memory.  */
1328   switch (reloc_data.howto->size)
1329     {
1330       case 2:
1331         arc_bfd_put_32 (abfd, insn,
1332                        contents + reloc_data.reloc_offset,
1333                        reloc_data.input_section);
1334         break;
1335       case 1:
1336         arc_bfd_put_16 (abfd, insn,
1337                        contents + reloc_data.reloc_offset,
1338                        reloc_data.input_section);
1339         break;
1340       case 0:
1341         arc_bfd_put_8 (abfd, insn,
1342                        contents + reloc_data.reloc_offset,
1343                        reloc_data.input_section);
1344         break;
1345       default:
1346         ARC_DEBUG ("size = %d\n", reloc_data.howto->size);
1347         BFD_ASSERT (0);
1348         break;
1349     }
1350
1351   return bfd_reloc_ok;
1352 }
1353 #undef S
1354 #undef A
1355 #undef B
1356 #undef G
1357 #undef GOT
1358 #undef L
1359 #undef MES
1360 #undef P
1361 #undef SECTSTAR
1362 #undef SECTSTART
1363 #undef _SDA_BASE_
1364 #undef none
1365
1366 #undef ARC_RELOC_HOWTO
1367
1368
1369 /* Relocate an arc ELF section.
1370    Function : elf_arc_relocate_section
1371    Brief    : Relocate an arc section, by handling all the relocations
1372              appearing in that section.
1373    Args     : output_bfd    : The bfd being written to.
1374               info          : Link information.
1375               input_bfd     : The input bfd.
1376               input_section : The section being relocated.
1377               contents      : contents of the section being relocated.
1378               relocs        : List of relocations in the section.
1379               local_syms    : is a pointer to the swapped in local symbols.
1380               local_section : is an array giving the section in the input file
1381                               corresponding to the st_shndx field of each
1382                               local symbol.  */
1383 static bfd_boolean
1384 elf_arc_relocate_section (bfd *                   output_bfd,
1385                           struct bfd_link_info *  info,
1386                           bfd *                   input_bfd,
1387                           asection *              input_section,
1388                           bfd_byte *              contents,
1389                           Elf_Internal_Rela *     relocs,
1390                           Elf_Internal_Sym *      local_syms,
1391                           asection **             local_sections)
1392 {
1393   Elf_Internal_Shdr *            symtab_hdr;
1394   struct elf_link_hash_entry **  sym_hashes;
1395   Elf_Internal_Rela *            rel;
1396   Elf_Internal_Rela *            wrel;
1397   Elf_Internal_Rela *            relend;
1398   struct elf_link_hash_table *   htab = elf_hash_table (info);
1399
1400   symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
1401   sym_hashes = elf_sym_hashes (input_bfd);
1402
1403   rel = wrel = relocs;
1404   relend = relocs + input_section->reloc_count;
1405   for (; rel < relend; wrel++, rel++)
1406     {
1407       enum elf_arc_reloc_type       r_type;
1408       reloc_howto_type *            howto;
1409       unsigned long                 r_symndx;
1410       struct elf_link_hash_entry *  h;
1411       Elf_Internal_Sym *            sym;
1412       asection *                    sec;
1413       struct elf_link_hash_entry *  h2;
1414       const char *                  msg;
1415
1416       struct arc_relocation_data reloc_data =
1417       {
1418         .reloc_offset = 0,
1419         .reloc_addend = 0,
1420         .got_offset_value = 0,
1421         .sym_value = 0, 
1422         .sym_section = NULL,
1423         .howto = NULL,
1424         .input_section = NULL,
1425         .sdata_begin_symbol_vma = 0,
1426         .sdata_begin_symbol_vma_set = FALSE,
1427         .got_symbol_vma = 0,
1428         .should_relocate = FALSE
1429       };
1430
1431       r_type = ELF32_R_TYPE (rel->r_info);
1432
1433       if (r_type >= (int) R_ARC_max)
1434         {
1435           bfd_set_error (bfd_error_bad_value);
1436           return FALSE;
1437         }
1438       howto = arc_elf_howto (r_type);
1439
1440       r_symndx = ELF32_R_SYM (rel->r_info);
1441
1442       /* If we are generating another .o file and the symbol in not
1443          local, skip this relocation.  */
1444       if (bfd_link_relocatable (info))
1445         {
1446           /* This is a relocateable link.  We don't have to change
1447              anything, unless the reloc is against a section symbol,
1448              in which case we have to adjust according to where the
1449              section symbol winds up in the output section.  */
1450
1451           /* Checks if this is a local symbol and thus the reloc
1452              might (will??) be against a section symbol.  */
1453           if (r_symndx < symtab_hdr->sh_info)
1454             {
1455               sym = local_syms + r_symndx;
1456               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1457                 {
1458                   sec = local_sections[r_symndx];
1459
1460                   /* For RELA relocs.  Just adjust the addend
1461                      value in the relocation entry.  */
1462                   rel->r_addend += sec->output_offset + sym->st_value;
1463
1464                   ARC_DEBUG ("local symbols reloc (section=%d %s) seen in %s\n",
1465                              (int) r_symndx, local_sections[r_symndx]->name,
1466                              __PRETTY_FUNCTION__);
1467                 }
1468             }
1469         }
1470
1471       h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
1472                                  FALSE, FALSE, TRUE);
1473
1474       if (!reloc_data.sdata_begin_symbol_vma_set
1475           && h2 != NULL && h2->root.type != bfd_link_hash_undefined
1476           && h2->root.u.def.section->output_section != NULL)
1477         /* TODO: Verify this condition.  */
1478         {
1479           reloc_data.sdata_begin_symbol_vma =
1480             (h2->root.u.def.value
1481              + h2->root.u.def.section->output_section->vma);
1482           reloc_data.sdata_begin_symbol_vma_set = TRUE;
1483         }
1484
1485       reloc_data.input_section = input_section;
1486       reloc_data.howto = howto;
1487       reloc_data.reloc_offset = rel->r_offset;
1488       reloc_data.reloc_addend = rel->r_addend;
1489
1490       /* This is a final link.  */
1491       h = NULL;
1492       sym = NULL;
1493       sec = NULL;
1494
1495       if (r_symndx < symtab_hdr->sh_info) /* A local symbol.  */
1496         {
1497           sym = local_syms + r_symndx;
1498           sec = local_sections[r_symndx];
1499         }
1500       else
1501         {
1502           /* TODO: This code is repeated from below.  We should
1503              clean it and remove duplications.
1504              Sec is used check for discarded sections.
1505              Need to redesign code below.  */
1506
1507           /* Get the symbol's entry in the symtab.  */
1508           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1509
1510           while (h->root.type == bfd_link_hash_indirect
1511                  || h->root.type == bfd_link_hash_warning)
1512             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1513
1514           /* If we have encountered a definition for this symbol.  */
1515           if (h->root.type == bfd_link_hash_defined
1516               || h->root.type == bfd_link_hash_defweak)
1517             {
1518               reloc_data.sym_value = h->root.u.def.value;
1519               sec = h->root.u.def.section;
1520             }
1521         }
1522
1523       /* Clean relocs for symbols in discarded sections.  */
1524       if (sec != NULL && discarded_section (sec))
1525         {
1526           _bfd_clear_contents (howto, input_bfd, input_section,
1527                                contents + rel->r_offset);
1528           rel->r_offset = rel->r_offset;
1529           rel->r_info = 0;
1530           rel->r_addend = 0;
1531
1532           /* For ld -r, remove relocations in debug sections against
1533              sections defined in discarded sections.  Not done for
1534              eh_frame editing code expects to be present.  */
1535            if (bfd_link_relocatable (info)
1536                && (input_section->flags & SEC_DEBUGGING))
1537              wrel--;
1538
1539           continue;
1540         }
1541
1542       if (bfd_link_relocatable (info))
1543         {
1544           if (wrel != rel)
1545             *wrel = *rel;
1546           continue;
1547         }
1548
1549       if (r_symndx < symtab_hdr->sh_info) /* A local symbol.  */
1550         {
1551           reloc_data.sym_value = sym->st_value;
1552           reloc_data.sym_section = sec;
1553           reloc_data.symbol_name =
1554             bfd_elf_string_from_elf_section (input_bfd,
1555                                              symtab_hdr->sh_link,
1556                                              sym->st_name);
1557
1558           /* Mergeable section handling.  */
1559           if ((sec->flags & SEC_MERGE)
1560               && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1561             {
1562               asection *msec;
1563               msec = sec;
1564               rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym,
1565                                                       &msec, rel->r_addend);
1566               rel->r_addend -= (sec->output_section->vma
1567                                 + sec->output_offset
1568                                 + sym->st_value);
1569               rel->r_addend += msec->output_section->vma + msec->output_offset;
1570
1571               reloc_data.reloc_addend = rel->r_addend;
1572             }
1573
1574           BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1575           if (htab->sgot != NULL)
1576             reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1577                                         + htab->sgot->output_offset;
1578
1579           reloc_data.should_relocate = TRUE;
1580         }
1581       else /* Global symbol.  */
1582         {
1583           /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro
1584              (defined in elf-bfd.h) here.  */
1585
1586           /* Get the symbol's entry in the symtab.  */
1587           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1588
1589           while (h->root.type == bfd_link_hash_indirect
1590                  || h->root.type == bfd_link_hash_warning)
1591             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1592
1593           /* TODO: Need to validate what was the intention.  */
1594           /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
1595           reloc_data.symbol_name = h->root.root.string;
1596
1597           /* If we have encountered a definition for this symbol.  */
1598           if (h->root.type == bfd_link_hash_defined
1599               || h->root.type == bfd_link_hash_defweak)
1600             {
1601               reloc_data.sym_value = h->root.u.def.value;
1602               reloc_data.sym_section = h->root.u.def.section;
1603
1604               reloc_data.should_relocate = TRUE;
1605
1606               if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
1607                 {
1608                   /* TODO: Change it to use arc_do_relocation with
1609                     ARC_32 reloc.  Try to use ADD_RELA macro.  */
1610                   bfd_vma relocation =
1611                     reloc_data.sym_value + reloc_data.reloc_addend
1612                     + (reloc_data.sym_section->output_section != NULL ?
1613                         (reloc_data.sym_section->output_offset
1614                          + reloc_data.sym_section->output_section->vma)
1615                       : 0);
1616
1617                   BFD_ASSERT (h->got.glist);
1618                   bfd_vma got_offset = h->got.glist->offset;
1619                   bfd_put_32 (output_bfd, relocation,
1620                               htab->sgot->contents + got_offset);
1621                 }
1622               if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1)
1623                 {
1624                   /* TODO: This is repeated up here.  */
1625                   reloc_data.sym_value = h->plt.offset;
1626                   reloc_data.sym_section = htab->splt;
1627                 }
1628             }
1629           else if (h->root.type == bfd_link_hash_undefweak)
1630             {
1631               /* Is weak symbol and has no definition.  */
1632               if (is_reloc_for_GOT (howto))
1633                 {
1634                   reloc_data.sym_value = h->root.u.def.value;
1635                   reloc_data.sym_section = htab->sgot;
1636                   reloc_data.should_relocate = TRUE;
1637                 }
1638               else if (is_reloc_for_PLT (howto)
1639                        && h->plt.offset != (bfd_vma) -1)
1640                 {
1641                   /* TODO: This is repeated up here.  */
1642                   reloc_data.sym_value = h->plt.offset;
1643                   reloc_data.sym_section = htab->splt;
1644                   reloc_data.should_relocate = TRUE;
1645                 }
1646               else
1647                 continue;
1648             }
1649           else
1650             {
1651               if (is_reloc_for_GOT (howto))
1652                 {
1653                   reloc_data.sym_value = h->root.u.def.value;
1654                   reloc_data.sym_section = htab->sgot;
1655
1656                   reloc_data.should_relocate = TRUE;
1657                 }
1658               else if (is_reloc_for_PLT (howto))
1659                 {
1660                   /* Fail if it is linking for PIE and the symbol is
1661                      undefined.  */
1662                   if (bfd_link_executable (info))
1663                     (*info->callbacks->undefined_symbol)
1664                       (info, h->root.root.string, input_bfd, input_section,
1665                        rel->r_offset, TRUE);
1666                   reloc_data.sym_value = h->plt.offset;
1667                   reloc_data.sym_section = htab->splt;
1668
1669                   reloc_data.should_relocate = TRUE;
1670                 }
1671               else if (!bfd_link_pic (info) || bfd_link_executable (info))
1672                 (*info->callbacks->undefined_symbol)
1673                   (info, h->root.root.string, input_bfd, input_section,
1674                    rel->r_offset, TRUE);
1675             }
1676
1677           BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1678           if (htab->sgot != NULL)
1679             reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1680                                         + htab->sgot->output_offset;
1681         }
1682
1683       if ((is_reloc_for_GOT (howto)
1684            || is_reloc_for_TLS (howto)))
1685         {
1686           reloc_data.should_relocate = TRUE;
1687
1688           struct got_entry **list
1689             = get_got_entry_list_for_symbol (output_bfd, r_symndx, h);
1690
1691           reloc_data.got_offset_value
1692             = relocate_fix_got_relocs_for_got_info (list,
1693                                                     tls_type_for_reloc (howto),
1694                                                     info,
1695                                                     output_bfd,
1696                                                     r_symndx,
1697                                                     local_syms,
1698                                                     local_sections,
1699                                                     h,
1700                                                     &reloc_data);
1701
1702           if (h == NULL)
1703             {
1704               create_got_dynrelocs_for_single_entry (
1705                   got_entry_for_type (list,
1706                                 arc_got_entry_type_for_reloc (howto)),
1707                   output_bfd, info, NULL);
1708             }
1709         }
1710
1711       switch (r_type)
1712         {
1713           case R_ARC_32:
1714           case R_ARC_32_ME:
1715           case R_ARC_PC32:
1716           case R_ARC_32_PCREL:
1717             if ((bfd_link_pic (info))
1718                 && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
1719                     || (h != NULL
1720                         && h->dynindx != -1
1721                         && (!info->symbolic || !h->def_regular))))
1722               {
1723                 Elf_Internal_Rela outrel;
1724                 bfd_byte *loc;
1725                 bfd_boolean skip = FALSE;
1726                 bfd_boolean relocate = FALSE;
1727                 asection *sreloc = _bfd_elf_get_dynamic_reloc_section
1728                                  (input_bfd, input_section,
1729                                   /*RELA*/ TRUE);
1730
1731                 BFD_ASSERT (sreloc != NULL);
1732
1733                 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
1734                                                            info,
1735                                                            input_section,
1736                                                            rel->r_offset);
1737                 if (outrel.r_offset == (bfd_vma) -1)
1738                   skip = TRUE;
1739
1740                 outrel.r_addend = rel->r_addend;
1741                 outrel.r_offset += (input_section->output_section->vma
1742                                     + input_section->output_offset);
1743
1744 #define IS_ARC_PCREL_TYPE(TYPE) \
1745   (   (TYPE == R_ARC_PC32)      \
1746    || (TYPE == R_ARC_32_PCREL))
1747
1748                 if (skip)
1749                   {
1750                     memset (&outrel, 0, sizeof outrel);
1751                     relocate = FALSE;
1752                   }
1753                 else if (h != NULL
1754                          && h->dynindx != -1
1755                          && ((IS_ARC_PCREL_TYPE (r_type))
1756                          || !(bfd_link_executable (info)
1757                               || SYMBOLIC_BIND (info, h))
1758                          || ! h->def_regular))
1759                   {
1760                     BFD_ASSERT (h != NULL);
1761                     if ((input_section->flags & SEC_ALLOC) != 0)
1762                       relocate = FALSE;
1763                     else
1764                       relocate = TRUE;
1765
1766                     BFD_ASSERT (h->dynindx != -1);
1767                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1768                   }
1769                 else
1770                   {
1771                     /* Handle local symbols, they either do not have a
1772                        global hash table entry (h == NULL), or are
1773                        forced local due to a version script
1774                        (h->forced_local), or the third condition is
1775                        legacy, it appears to say something like, for
1776                        links where we are pre-binding the symbols, or
1777                        there's not an entry for this symbol in the
1778                        dynamic symbol table, and it's a regular symbol
1779                        not defined in a shared object, then treat the
1780                        symbol as local, resolve it now.  */
1781                     relocate = TRUE;
1782                     /* outrel.r_addend = 0; */
1783                     outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
1784                   }
1785
1786                 BFD_ASSERT (sreloc->contents != 0);
1787
1788                 loc = sreloc->contents;
1789                 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
1790                 sreloc->reloc_count += 1;
1791
1792                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1793
1794                 if (!relocate)
1795                   continue;
1796               }
1797             break;
1798           default:
1799             break;
1800         }
1801
1802       if (is_reloc_SDA_relative (howto)
1803           && !reloc_data.sdata_begin_symbol_vma_set)
1804         {
1805           _bfd_error_handler
1806             ("Error: Linker symbol __SDATA_BEGIN__ not found");
1807           bfd_set_error (bfd_error_bad_value);
1808           return FALSE;
1809         }
1810
1811       DEBUG_ARC_RELOC (reloc_data);
1812
1813       /* Make sure we have with a dynamic linker.  In case of GOT and PLT
1814          the sym_section should point to .got or .plt respectively.  */
1815       if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
1816           && reloc_data.sym_section == NULL)
1817         {
1818           _bfd_error_handler
1819             (_("GOT and PLT relocations cannot be fixed with a non dynamic linker."));
1820           bfd_set_error (bfd_error_bad_value);
1821           return FALSE;
1822         }
1823
1824       msg = NULL;
1825       switch (arc_do_relocation (contents, reloc_data, info))
1826         {
1827         case bfd_reloc_ok:
1828           continue; /* The reloc processing loop.  */
1829
1830         case bfd_reloc_overflow:
1831           (*info->callbacks->reloc_overflow)
1832             (info, (h ? &h->root : NULL), reloc_data.symbol_name, howto->name, (bfd_vma) 0,
1833              input_bfd, input_section, rel->r_offset);
1834           break;
1835
1836         case bfd_reloc_undefined:
1837           (*info->callbacks->undefined_symbol)
1838             (info, reloc_data.symbol_name, input_bfd, input_section, rel->r_offset, TRUE);
1839           break;
1840
1841         case bfd_reloc_other:
1842           /* xgettext:c-format */
1843           msg = _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
1844           break;
1845
1846         case bfd_reloc_outofrange:
1847           /* xgettext:c-format */
1848           msg = _("%B(%A): internal error: out of range error");
1849           break;
1850
1851         case bfd_reloc_notsupported:
1852           /* xgettext:c-format */
1853           msg = _("%B(%A): internal error: unsupported relocation error");
1854           break;
1855
1856         case bfd_reloc_dangerous:
1857           /* xgettext:c-format */
1858           msg = _("%B(%A): internal error: dangerous relocation");
1859           break;
1860
1861         default:
1862           /* xgettext:c-format */
1863           msg = _("%B(%A): internal error: unknown error");
1864           break;
1865         }
1866
1867       if (msg)
1868         _bfd_error_handler (msg, input_bfd, input_section, reloc_data.symbol_name);
1869       return FALSE;
1870     }
1871
1872   return TRUE;
1873 }
1874
1875 #define elf_arc_hash_table(p) \
1876     (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
1877   == ARC_ELF_DATA ? ((struct elf_arc_link_hash_table *) ((p)->hash)) : NULL)
1878
1879 static bfd_boolean
1880 elf_arc_check_relocs (bfd *                      abfd,
1881                       struct bfd_link_info *     info,
1882                       asection *                 sec,
1883                       const Elf_Internal_Rela *  relocs)
1884 {
1885   Elf_Internal_Shdr *           symtab_hdr;
1886   struct elf_link_hash_entry ** sym_hashes;
1887   const Elf_Internal_Rela *     rel;
1888   const Elf_Internal_Rela *     rel_end;
1889   bfd *                         dynobj;
1890   asection *                    sreloc = NULL;
1891
1892   if (bfd_link_relocatable (info))
1893     return TRUE;
1894
1895   dynobj = (elf_hash_table (info))->dynobj;
1896   symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
1897   sym_hashes = elf_sym_hashes (abfd);
1898
1899   rel_end = relocs + sec->reloc_count;
1900   for (rel = relocs; rel < rel_end; rel++)
1901     {
1902       enum elf_arc_reloc_type r_type;
1903       reloc_howto_type *howto;
1904       unsigned long   r_symndx;
1905       struct elf_link_hash_entry *h;
1906
1907       r_type = ELF32_R_TYPE (rel->r_info);
1908
1909       if (r_type >= (int) R_ARC_max)
1910         {
1911           bfd_set_error (bfd_error_bad_value);
1912           return FALSE;
1913         }
1914       howto = arc_elf_howto (r_type);
1915
1916       if (dynobj == NULL
1917           && (is_reloc_for_GOT (howto)
1918               || is_reloc_for_TLS (howto)))
1919         {
1920           dynobj = elf_hash_table (info)->dynobj = abfd;
1921           if (! _bfd_elf_create_got_section (abfd, info))
1922             return FALSE;
1923         }
1924
1925       /* Load symbol information.  */
1926       r_symndx = ELF32_R_SYM (rel->r_info);
1927       if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol.  */
1928         h = NULL;
1929       else /* Global one.  */
1930         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1931
1932       switch (r_type)
1933         {
1934           case R_ARC_32:
1935           case R_ARC_32_ME:
1936             /* During shared library creation, these relocs should not
1937                appear in a shared library (as memory will be read only
1938                and the dynamic linker can not resolve these.  However
1939                the error should not occur for e.g. debugging or
1940                non-readonly sections.  */
1941             if (h != NULL
1942                 && (bfd_link_dll (info) && !bfd_link_pie (info))
1943                 && (sec->flags & SEC_ALLOC) != 0
1944                 && (sec->flags & SEC_READONLY) != 0
1945                 && ((sec->flags & SEC_CODE) != 0
1946                     || (sec->flags & SEC_DEBUGGING) != 0))
1947               {
1948                 const char *name;
1949                 if (h)
1950                   name = h->root.root.string;
1951                 else
1952                   /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);  */
1953                   name = "UNKNOWN";
1954                 _bfd_error_handler
1955                   /* xgettext:c-format */
1956                   (_("\
1957 %B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1958                     abfd,
1959                     arc_elf_howto (r_type)->name,
1960                     name);
1961                 bfd_set_error (bfd_error_bad_value);
1962                 return FALSE;
1963               }
1964
1965             /* In some cases we are not setting the 'non_got_ref'
1966                flag, even though the relocations don't require a GOT
1967                access.  We should extend the testing in this area to
1968                ensure that no significant cases are being missed.  */
1969             if (h)
1970               h->non_got_ref = 1;
1971             /* FALLTHROUGH */
1972           case R_ARC_PC32:
1973           case R_ARC_32_PCREL:
1974             if ((bfd_link_pic (info))
1975                 && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
1976                     || (h != NULL
1977                         && (!info->symbolic || !h->def_regular))))
1978               {
1979                 if (sreloc == NULL)
1980                   {
1981                     sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
1982                                                                   2, abfd,
1983                                                                   /*rela*/
1984                                                                   TRUE);
1985
1986                     if (sreloc == NULL)
1987                       return FALSE;
1988                   }
1989                 sreloc->size += sizeof (Elf32_External_Rela);
1990
1991               }
1992           default:
1993             break;
1994         }
1995
1996       if (is_reloc_for_PLT (howto))
1997         {
1998           if (h == NULL)
1999             continue;
2000           else
2001             h->needs_plt = 1;
2002         }
2003
2004       /* Add info to the symbol got_entry_list.  */
2005       if (is_reloc_for_GOT (howto)
2006           || is_reloc_for_TLS (howto))
2007         {
2008           arc_fill_got_info_for_reloc (
2009                   arc_got_entry_type_for_reloc (howto),
2010                   get_got_entry_list_for_symbol (abfd, r_symndx, h),
2011                   info,
2012                   h);
2013         }
2014     }
2015
2016   return TRUE;
2017 }
2018
2019 #define ELF_DYNAMIC_INTERPRETER  "/sbin/ld-uClibc.so"
2020
2021 static struct plt_version_t *
2022 arc_get_plt_version (struct bfd_link_info *info)
2023 {
2024   int i;
2025
2026   for (i = 0; i < 1; i++)
2027     {
2028       ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
2029                  (int) plt_versions[i].entry_size,
2030                  (int) plt_versions[i].elem_size);
2031     }
2032
2033   if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
2034     {
2035       if (bfd_link_pic (info))
2036         return &(plt_versions[ELF_ARCV2_PIC]);
2037       else
2038         return &(plt_versions[ELF_ARCV2_ABS]);
2039     }
2040   else
2041     {
2042       if (bfd_link_pic (info))
2043         return &(plt_versions[ELF_ARC_PIC]);
2044       else
2045         return &(plt_versions[ELF_ARC_ABS]);
2046     }
2047 }
2048
2049 static bfd_vma
2050 add_symbol_to_plt (struct bfd_link_info *info)
2051 {
2052   struct elf_link_hash_table *htab = elf_hash_table (info);
2053   bfd_vma ret;
2054
2055   struct plt_version_t *plt_data = arc_get_plt_version (info);
2056
2057   /* If this is the first .plt entry, make room for the special first
2058      entry.  */
2059   if (htab->splt->size == 0)
2060     htab->splt->size += plt_data->entry_size;
2061
2062   ret = htab->splt->size;
2063
2064   htab->splt->size += plt_data->elem_size;
2065   ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab->splt->size);
2066
2067   htab->sgotplt->size += 4;
2068   htab->srelplt->size += sizeof (Elf32_External_Rela);
2069
2070   return ret;
2071 }
2072
2073 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS)       \
2074   plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
2075
2076 static void
2077 plt_do_relocs_for_symbol (bfd *abfd,
2078                           struct elf_link_hash_table *htab,
2079                           const struct plt_reloc *reloc,
2080                           bfd_vma plt_offset,
2081                           bfd_vma symbol_got_offset)
2082 {
2083   while (SYM_ONLY (reloc->symbol) != LAST_RELOC)
2084     {
2085       bfd_vma relocation = 0;
2086
2087       switch (SYM_ONLY (reloc->symbol))
2088         {
2089           case SGOT:
2090                 relocation
2091                   = htab->sgotplt->output_section->vma
2092                     + htab->sgotplt->output_offset + symbol_got_offset;
2093                 break;
2094         }
2095       relocation += reloc->addend;
2096
2097       if (IS_RELATIVE (reloc->symbol))
2098         {
2099           bfd_vma reloc_offset = reloc->offset;
2100           reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0;
2101           reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0;
2102
2103           relocation -= htab->splt->output_section->vma
2104                          + htab->splt->output_offset
2105                          + plt_offset + reloc_offset;
2106         }
2107
2108       /* TODO: being ME is not a property of the relocation but of the
2109          section of which is applying the relocation. */
2110       if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
2111         {
2112           relocation
2113             = ((relocation & 0xffff0000) >> 16)
2114               | ((relocation & 0xffff) << 16);
2115         }
2116
2117       switch (reloc->size)
2118         {
2119           case 32:
2120             bfd_put_32 (htab->splt->output_section->owner,
2121                         relocation,
2122                         htab->splt->contents + plt_offset + reloc->offset);
2123             break;
2124         }
2125
2126       reloc = &(reloc[1]); /* Jump to next relocation.  */
2127     }
2128 }
2129
2130 static void
2131 relocate_plt_for_symbol (bfd *output_bfd,
2132                          struct bfd_link_info *info,
2133                          struct elf_link_hash_entry *h)
2134 {
2135   struct plt_version_t *plt_data = arc_get_plt_version (info);
2136   struct elf_link_hash_table *htab = elf_hash_table (info);
2137
2138   bfd_vma plt_index = (h->plt.offset  - plt_data->entry_size)
2139                       / plt_data->elem_size;
2140   bfd_vma got_offset = (plt_index + 3) * 4;
2141
2142   ARC_DEBUG ("arc_info: PLT_OFFSET = %#lx, PLT_ENTRY_VMA = %#lx, \
2143 GOT_ENTRY_OFFSET = %#lx, GOT_ENTRY_VMA = %#lx, for symbol %s\n",
2144              (long) h->plt.offset,
2145              (long) (htab->splt->output_section->vma
2146                      + htab->splt->output_offset
2147                      + h->plt.offset),
2148              (long) got_offset,
2149              (long) (htab->sgotplt->output_section->vma
2150                      + htab->sgotplt->output_offset
2151                      + got_offset),
2152              h->root.root.string);
2153
2154   {
2155     bfd_vma i = 0;
2156     uint16_t *ptr = (uint16_t *) plt_data->elem;
2157
2158     for (i = 0; i < plt_data->elem_size/2; i++)
2159       {
2160         uint16_t data = ptr[i];
2161         bfd_put_16 (output_bfd,
2162                     (bfd_vma) data,
2163                     htab->splt->contents + h->plt.offset + (i*2));
2164       }
2165   }
2166
2167   plt_do_relocs_for_symbol (output_bfd, htab,
2168                             plt_data->elem_relocs,
2169                             h->plt.offset,
2170                             got_offset);
2171
2172   /* Fill in the entry in the global offset table.  */
2173   bfd_put_32 (output_bfd,
2174               (bfd_vma) (htab->splt->output_section->vma
2175                          + htab->splt->output_offset),
2176               htab->sgotplt->contents + got_offset);
2177
2178   /* TODO: Fill in the entry in the .rela.plt section.  */
2179   {
2180     Elf_Internal_Rela rel;
2181     bfd_byte *loc;
2182
2183     rel.r_offset = (htab->sgotplt->output_section->vma
2184                     + htab->sgotplt->output_offset
2185                     + got_offset);
2186     rel.r_addend = 0;
2187
2188     BFD_ASSERT (h->dynindx != -1);
2189     rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT);
2190
2191     loc = htab->srelplt->contents;
2192     loc += plt_index * sizeof (Elf32_External_Rela); /* relA */
2193     bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2194   }
2195 }
2196
2197 static void
2198 relocate_plt_for_entry (bfd *abfd,
2199                         struct bfd_link_info *info)
2200 {
2201   struct plt_version_t *plt_data = arc_get_plt_version (info);
2202   struct elf_link_hash_table *htab = elf_hash_table (info);
2203
2204   {
2205     bfd_vma i = 0;
2206     uint16_t *ptr = (uint16_t *) plt_data->entry;
2207     for (i = 0; i < plt_data->entry_size/2; i++)
2208       {
2209         uint16_t data = ptr[i];
2210         bfd_put_16 (abfd,
2211                     (bfd_vma) data,
2212                     htab->splt->contents + (i*2));
2213       }
2214   }
2215   PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
2216 }
2217
2218 /* Desc : Adjust a symbol defined by a dynamic object and referenced
2219    by a regular object.  The current definition is in some section of
2220    the dynamic object, but we're not including those sections.  We
2221    have to change the definition to something the rest of the link can
2222    understand.  */
2223
2224 static bfd_boolean
2225 elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
2226                               struct elf_link_hash_entry *h)
2227 {
2228   asection *s;
2229   bfd *dynobj = (elf_hash_table (info))->dynobj;
2230   struct elf_link_hash_table *htab = elf_hash_table (info);
2231
2232   if (h->type == STT_FUNC
2233       || h->type == STT_GNU_IFUNC
2234       || h->needs_plt == 1)
2235     {
2236       if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic)
2237         {
2238           /* This case can occur if we saw a PLT32 reloc in an input
2239              file, but the symbol was never referred to by a dynamic
2240              object.  In such a case, we don't actually need to build
2241              a procedure linkage table, and we can just do a PC32
2242              reloc instead.  */
2243           BFD_ASSERT (h->needs_plt);
2244           return TRUE;
2245         }
2246
2247       /* Make sure this symbol is output as a dynamic symbol.  */
2248       if (h->dynindx == -1 && !h->forced_local
2249           && !bfd_elf_link_record_dynamic_symbol (info, h))
2250         return FALSE;
2251
2252       if (bfd_link_pic (info)
2253           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2254         {
2255           bfd_vma loc = add_symbol_to_plt (info);
2256
2257           if (bfd_link_executable (info) && !h->def_regular)
2258             {
2259               h->root.u.def.section = htab->splt;
2260               h->root.u.def.value = loc;
2261             }
2262           h->plt.offset = loc;
2263         }
2264       else
2265         {
2266           h->plt.offset = (bfd_vma) -1;
2267           h->needs_plt = 0;
2268         }
2269       return TRUE;
2270     }
2271
2272   /* If this is a weak symbol, and there is a real definition, the
2273      processor independent code will have arranged for us to see the
2274      real definition first, and we can just use the same value.  */
2275   if (h->u.weakdef != NULL)
2276     {
2277       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2278                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2279       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2280       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2281       return TRUE;
2282     }
2283
2284   /* This is a reference to a symbol defined by a dynamic object which
2285      is not a function.  */
2286
2287   /* If we are creating a shared library, we must presume that the
2288      only references to the symbol are via the global offset table.
2289      For such cases we need not do anything here; the relocations will
2290      be handled correctly by relocate_section.  */
2291   if (!bfd_link_executable (info))
2292     return TRUE;
2293
2294   /* If there are no non-GOT references, we do not need a copy
2295      relocation.  */
2296   if (!h->non_got_ref)
2297     return TRUE;
2298
2299   /* If -z nocopyreloc was given, we won't generate them either.  */
2300   if (info->nocopyreloc)
2301     {
2302       h->non_got_ref = 0;
2303       return TRUE;
2304     }
2305
2306   /* We must allocate the symbol in our .dynbss section, which will
2307      become part of the .bss section of the executable.  There will be
2308      an entry for this symbol in the .dynsym section.  The dynamic
2309      object will contain position independent code, so all references
2310      from the dynamic object to this symbol will go through the global
2311      offset table.  The dynamic linker will use the .dynsym entry to
2312      determine the address it must put in the global offset table, so
2313      both the dynamic object and the regular object will refer to the
2314      same memory location for the variable.  */
2315
2316   if (htab == NULL)
2317     return FALSE;
2318
2319   /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
2320      copy the initial value out of the dynamic object and into the
2321      runtime process image.  We need to remember the offset into the
2322      .rela.bss section we are going to use.  */
2323   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2324     {
2325       struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
2326
2327       BFD_ASSERT (arc_htab->elf.srelbss != NULL);
2328       arc_htab->elf.srelbss->size += sizeof (Elf32_External_Rela);
2329       h->needs_copy = 1;
2330     }
2331
2332   /* TODO: Move this also to arc_hash_table.  */
2333   s = bfd_get_section_by_name (dynobj, ".dynbss");
2334   BFD_ASSERT (s != NULL);
2335
2336   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2337 }
2338
2339 /* Function :  elf_arc_finish_dynamic_symbol
2340    Brief    :  Finish up dynamic symbol handling.  We set the
2341              contents of various dynamic sections here.
2342    Args     :  output_bfd :
2343                info       :
2344                h          :
2345                sym        :
2346    Returns  : True/False as the return status.  */
2347
2348 static bfd_boolean
2349 elf_arc_finish_dynamic_symbol (bfd * output_bfd,
2350                                struct bfd_link_info *info,
2351                                struct elf_link_hash_entry *h,
2352                                Elf_Internal_Sym * sym)
2353 {
2354   if (h->plt.offset != (bfd_vma) -1)
2355     {
2356       relocate_plt_for_symbol (output_bfd, info, h);
2357
2358       if (!h->def_regular)
2359         {
2360           /* Mark the symbol as undefined, rather than as defined in
2361              the .plt section.  Leave the value alone.  */
2362           sym->st_shndx = SHN_UNDEF;
2363         }
2364     }
2365
2366
2367   /* This function traverses list of GOT entries and
2368      create respective dynamic relocs.  */
2369   /* TODO: Make function to get list and not access the list directly.  */
2370   /* TODO: Move function to relocate_section create this relocs eagerly.  */
2371   create_got_dynrelocs_for_got_info (&h->got.glist,
2372                                      output_bfd,
2373                                      info,
2374                                      h);
2375
2376   if (h->needs_copy)
2377     {
2378       struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
2379
2380       if (h->dynindx == -1
2381           || (h->root.type != bfd_link_hash_defined
2382               && h->root.type != bfd_link_hash_defweak)
2383           || arc_htab->elf.srelbss == NULL)
2384         abort ();
2385
2386       bfd_vma rel_offset = (h->root.u.def.value
2387                             + h->root.u.def.section->output_section->vma
2388                             + h->root.u.def.section->output_offset);
2389
2390       bfd_byte * loc = arc_htab->elf.srelbss->contents
2391         + (arc_htab->elf.srelbss->reloc_count * sizeof (Elf32_External_Rela));
2392       arc_htab->elf.srelbss->reloc_count++;
2393
2394       Elf_Internal_Rela rel;
2395       rel.r_addend = 0;
2396       rel.r_offset = rel_offset;
2397
2398       BFD_ASSERT (h->dynindx != -1);
2399       rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY);
2400
2401       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2402     }
2403
2404   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2405   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2406       || strcmp (h->root.root.string, "__DYNAMIC") == 0
2407       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2408     sym->st_shndx = SHN_ABS;
2409
2410   return TRUE;
2411 }
2412
2413 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION)             \
2414   case TAG:                                                     \
2415   if (SYMBOL != NULL)                                           \
2416     h = elf_link_hash_lookup (elf_hash_table (info),            \
2417                               SYMBOL, FALSE, FALSE, TRUE);      \
2418   else if (SECTION != NULL)                                     \
2419     s = bfd_get_linker_section (dynobj, SECTION);               \
2420   break;
2421
2422 /* Function :  elf_arc_finish_dynamic_sections
2423    Brief    :  Finish up the dynamic sections handling.
2424    Args     :  output_bfd :
2425                info       :
2426                h          :
2427                sym        :
2428    Returns  : True/False as the return status.  */
2429
2430 static bfd_boolean
2431 elf_arc_finish_dynamic_sections (bfd * output_bfd,
2432                                  struct bfd_link_info *info)
2433 {
2434   struct elf_link_hash_table *htab = elf_hash_table (info);
2435   bfd *dynobj = (elf_hash_table (info))->dynobj;
2436   asection *sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2437
2438   if (sdyn)
2439     {
2440       Elf32_External_Dyn *dyncon, *dynconend;
2441
2442       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2443       dynconend
2444         = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2445       for (; dyncon < dynconend; dyncon++)
2446         {
2447           Elf_Internal_Dyn internal_dyn;
2448           bfd_boolean     do_it = FALSE;
2449
2450           struct elf_link_hash_entry *h = NULL;
2451           asection       *s = NULL;
2452
2453           bfd_elf32_swap_dyn_in (dynobj, dyncon, &internal_dyn);
2454
2455           switch (internal_dyn.d_tag)
2456             {
2457               GET_SYMBOL_OR_SECTION (DT_INIT, info->init_function, NULL)
2458               GET_SYMBOL_OR_SECTION (DT_FINI, info->fini_function, NULL)
2459               GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt")
2460               GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt")
2461               GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt")
2462               GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version")
2463               GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d")
2464               GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r")
2465               default:
2466                 break;
2467             }
2468
2469           /* In case the dynamic symbols should be updated with a symbol.  */
2470           if (h != NULL
2471               && (h->root.type == bfd_link_hash_defined
2472                   || h->root.type == bfd_link_hash_defweak))
2473             {
2474               asection       *asec_ptr;
2475
2476               internal_dyn.d_un.d_val = h->root.u.def.value;
2477               asec_ptr = h->root.u.def.section;
2478               if (asec_ptr->output_section != NULL)
2479                 {
2480                   internal_dyn.d_un.d_val +=
2481                     (asec_ptr->output_section->vma
2482                      + asec_ptr->output_offset);
2483                 }
2484               else
2485                 {
2486                   /* The symbol is imported from another shared
2487                      library and does not apply to this one.  */
2488                   internal_dyn.d_un.d_val = 0;
2489                 }
2490               do_it = TRUE;
2491             }
2492           else if (s != NULL) /* With a section information.  */
2493             {
2494               switch (internal_dyn.d_tag)
2495                 {
2496                   case DT_PLTGOT:
2497                   case DT_JMPREL:
2498                   case DT_VERSYM:
2499                   case DT_VERDEF:
2500                   case DT_VERNEED:
2501                     internal_dyn.d_un.d_ptr = (s->output_section->vma
2502                                                + s->output_offset);
2503                     do_it = TRUE;
2504                     break;
2505
2506                   case DT_PLTRELSZ:
2507                     internal_dyn.d_un.d_val = s->size;
2508                     do_it = TRUE;
2509                     break;
2510
2511                   default:
2512                     break;
2513                 }
2514             }
2515
2516           if (do_it)
2517             bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon);
2518         }
2519
2520       if (htab->splt->size > 0)
2521         {
2522           relocate_plt_for_entry (output_bfd, info);
2523         }
2524
2525       /* TODO: Validate this.  */
2526       if (htab->srelplt->output_section != bfd_abs_section_ptr)
2527         elf_section_data (htab->srelplt->output_section)
2528           ->this_hdr.sh_entsize = 12;
2529     }
2530
2531   /* Fill in the first three entries in the global offset table.  */
2532   if (htab->sgot)
2533     {
2534       struct elf_link_hash_entry *h;
2535       h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_",
2536                                  FALSE, FALSE, TRUE);
2537
2538         if (h != NULL && h->root.type != bfd_link_hash_undefined
2539             && h->root.u.def.section != NULL)
2540         {
2541           asection *sec = h->root.u.def.section;
2542
2543           if (sdyn == NULL)
2544             bfd_put_32 (output_bfd, (bfd_vma) 0,
2545                         sec->contents);
2546           else
2547             bfd_put_32 (output_bfd,
2548                         sdyn->output_section->vma + sdyn->output_offset,
2549                         sec->contents);
2550           bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 4);
2551           bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 8);
2552         }
2553     }
2554
2555   return TRUE;
2556 }
2557
2558 #define ADD_DYNAMIC_SYMBOL(NAME, TAG)                                   \
2559   h =  elf_link_hash_lookup (elf_hash_table (info),                     \
2560                              NAME, FALSE, FALSE, FALSE);                \
2561   if ((h != NULL && (h->ref_regular || h->def_regular)))                \
2562     if (! _bfd_elf_add_dynamic_entry (info, TAG, 0))                    \
2563       return FALSE;
2564
2565 /* Set the sizes of the dynamic sections.  */
2566 static bfd_boolean
2567 elf_arc_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2568                                struct bfd_link_info *info)
2569 {
2570   bfd *dynobj;
2571   asection *s;
2572   bfd_boolean relocs_exist = FALSE;
2573   bfd_boolean reltext_exist = FALSE;
2574   struct elf_link_hash_table *htab = elf_hash_table (info);
2575
2576   dynobj = htab->dynobj;
2577   BFD_ASSERT (dynobj != NULL);
2578
2579   if (htab->dynamic_sections_created)
2580     {
2581       struct elf_link_hash_entry *h;
2582
2583       /* Set the contents of the .interp section to the
2584          interpreter.  */
2585       if (bfd_link_executable (info) && !info->nointerp)
2586         {
2587           s = bfd_get_section_by_name (dynobj, ".interp");
2588           BFD_ASSERT (s != NULL);
2589           s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
2590           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2591         }
2592
2593       /* Add some entries to the .dynamic section.  We fill in some of
2594          the values later, in elf_bfd_final_link, but we must add the
2595          entries now so that we know the final size of the .dynamic
2596          section.  Checking if the .init section is present.  We also
2597          create DT_INIT and DT_FINI entries if the init_str has been
2598          changed by the user.  */
2599       ADD_DYNAMIC_SYMBOL (info->init_function, DT_INIT);
2600       ADD_DYNAMIC_SYMBOL (info->fini_function, DT_FINI);
2601     }
2602   else
2603     {
2604       /* We may have created entries in the .rela.got section.
2605          However, if we are not creating the dynamic sections, we will
2606          not actually use these entries.  Reset the size of .rela.got,
2607          which will cause it to get stripped from the output file
2608          below.  */
2609       if (htab->srelgot != NULL)
2610         htab->srelgot->size = 0;
2611     }
2612
2613   for (s = dynobj->sections; s != NULL; s = s->next)
2614     {
2615       if ((s->flags & SEC_LINKER_CREATED) == 0)
2616         continue;
2617
2618       if (s == htab->splt
2619           || s == htab->sgot
2620           || s == htab->sgotplt
2621           || s == htab->sdynbss)
2622         {
2623           /* Strip this section if we don't need it.  */
2624         }
2625       else if (strncmp (s->name, ".rela", 5) == 0)
2626         {
2627           if (s->size != 0 && s != htab->srelplt)
2628             {
2629               if (!reltext_exist)
2630                 {
2631                   const char *name = s->name + 5;
2632                   bfd *ibfd;
2633                   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
2634                     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour)
2635                       {
2636                         asection *target = bfd_get_section_by_name (ibfd, name);
2637                         if (target != NULL
2638                             && elf_section_data (target)->sreloc == s
2639                             && ((target->output_section->flags
2640                                  & (SEC_READONLY | SEC_ALLOC))
2641                                 == (SEC_READONLY | SEC_ALLOC)))
2642                           {
2643                             reltext_exist = TRUE;
2644                             break;
2645                           }
2646                       }
2647                 }
2648               relocs_exist = TRUE;
2649             }
2650
2651           /* We use the reloc_count field as a counter if we need to
2652              copy relocs into the output file.  */
2653           s->reloc_count = 0;
2654         }
2655       else
2656         {
2657           /* It's not one of our sections, so don't allocate space.  */
2658           continue;
2659         }
2660
2661       if (s->size == 0)
2662         {
2663           s->flags |= SEC_EXCLUDE;
2664           continue;
2665         }
2666
2667       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2668         continue;
2669
2670       /* Allocate memory for the section contents.  */
2671       s->contents = bfd_zalloc (dynobj, s->size);
2672       if (s->contents == NULL)
2673         return FALSE;
2674     }
2675
2676   if (htab->dynamic_sections_created)
2677     {
2678       /* TODO: Check if this is needed.  */
2679       if (!bfd_link_pic (info))
2680         if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
2681                 return FALSE;
2682
2683       if (htab->splt && (htab->splt->flags & SEC_EXCLUDE) == 0)
2684         if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
2685             || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2686             || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2687             || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
2688           return FALSE;
2689
2690       if (relocs_exist)
2691         if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
2692             || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
2693             || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
2694                                             sizeof (Elf32_External_Rela)))
2695           return FALSE;
2696
2697       if (reltext_exist)
2698         if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
2699           return FALSE;
2700     }
2701
2702   return TRUE;
2703 }
2704
2705
2706 /* Classify dynamic relocs such that -z combreloc can reorder and combine
2707    them.  */
2708 static enum elf_reloc_type_class
2709 elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2710                             const asection *rel_sec ATTRIBUTE_UNUSED,
2711                             const Elf_Internal_Rela *rela)
2712 {
2713   switch ((int) ELF32_R_TYPE (rela->r_info))
2714     {
2715     case R_ARC_RELATIVE:
2716       return reloc_class_relative;
2717     case R_ARC_JMP_SLOT:
2718       return reloc_class_plt;
2719     case R_ARC_COPY:
2720       return reloc_class_copy;
2721     /* TODO: Needed in future to support ifunc.  */
2722     /*
2723     case R_ARC_IRELATIVE:
2724       return reloc_class_ifunc;
2725     */
2726     default:
2727       return reloc_class_normal;
2728     }
2729 }
2730
2731 const struct elf_size_info arc_elf32_size_info =
2732 {
2733   sizeof (Elf32_External_Ehdr),
2734   sizeof (Elf32_External_Phdr),
2735   sizeof (Elf32_External_Shdr),
2736   sizeof (Elf32_External_Rel),
2737   sizeof (Elf32_External_Rela),
2738   sizeof (Elf32_External_Sym),
2739   sizeof (Elf32_External_Dyn),
2740   sizeof (Elf_External_Note),
2741   4,
2742   1,
2743   32, 2,
2744   ELFCLASS32, EV_CURRENT,
2745   bfd_elf32_write_out_phdrs,
2746   bfd_elf32_write_shdrs_and_ehdr,
2747   bfd_elf32_checksum_contents,
2748   bfd_elf32_write_relocs,
2749   bfd_elf32_swap_symbol_in,
2750   bfd_elf32_swap_symbol_out,
2751   bfd_elf32_slurp_reloc_table,
2752   bfd_elf32_slurp_symbol_table,
2753   bfd_elf32_swap_dyn_in,
2754   bfd_elf32_swap_dyn_out,
2755   bfd_elf32_swap_reloc_in,
2756   bfd_elf32_swap_reloc_out,
2757   bfd_elf32_swap_reloca_in,
2758   bfd_elf32_swap_reloca_out
2759 };
2760
2761 #define elf_backend_size_info           arc_elf32_size_info
2762
2763 /* Hook called by the linker routine which adds symbols from an object
2764    file.  */
2765
2766 static bfd_boolean
2767 elf_arc_add_symbol_hook (bfd * abfd,
2768                          struct bfd_link_info * info,
2769                          Elf_Internal_Sym * sym,
2770                          const char ** namep ATTRIBUTE_UNUSED,
2771                          flagword * flagsp ATTRIBUTE_UNUSED,
2772                          asection ** secp ATTRIBUTE_UNUSED,
2773                          bfd_vma * valp ATTRIBUTE_UNUSED)
2774 {
2775   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
2776       && (abfd->flags & DYNAMIC) == 0
2777       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
2778     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
2779
2780   return TRUE;
2781 }
2782
2783 /* GDB expects general purpose registers to be in section .reg.  However Linux
2784    kernel doesn't create this section and instead writes registers to NOTE
2785    section.  It is up to the binutils to create a pseudo-section .reg from the
2786    contents of NOTE.  Also BFD will read pid and signal number from NOTE.  This
2787    function relies on offsets inside elf_prstatus structure in Linux to be
2788    stable.  */
2789
2790 static bfd_boolean
2791 elf32_arc_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2792 {
2793   int offset;
2794   size_t size;
2795
2796   switch (note->descsz)
2797     {
2798     default:
2799       return FALSE;
2800
2801     case 236: /* sizeof (struct elf_prstatus) on Linux/arc.  */
2802       /* pr_cursig */
2803       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2804       /* pr_pid */
2805       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
2806       /* pr_regs */
2807       offset = 72;
2808       size = (40 * 4); /* There are 40 registers in user_regs_struct.  */
2809       break;
2810     }
2811   /* Make a ".reg/999" section.  */
2812   return _bfd_elfcore_make_pseudosection (abfd, ".reg", size,
2813                                           note->descpos + offset);
2814 }
2815
2816 /* Determine whether an object attribute tag takes an integer, a
2817    string or both.  */
2818
2819 static int
2820 elf32_arc_obj_attrs_arg_type (int tag)
2821 {
2822   if (tag == Tag_ARC_CPU_name
2823            || tag == Tag_ARC_ISA_config
2824            || tag == Tag_ARC_ISA_apex)
2825     return ATTR_TYPE_FLAG_STR_VAL;
2826   else if (tag < (Tag_ARC_ISA_mpy_option + 1))
2827     return ATTR_TYPE_FLAG_INT_VAL;
2828   else
2829     return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
2830 }
2831
2832 /* Attribute numbers >=14 can be safely ignored.  */
2833
2834 static bfd_boolean
2835 elf32_arc_obj_attrs_handle_unknown (bfd *abfd, int tag)
2836 {
2837   if ((tag & 127) < (Tag_ARC_ISA_mpy_option + 1))
2838     {
2839       _bfd_error_handler
2840         (_("%B: Unknown mandatory ARC object attribute %d."),
2841          abfd, tag);
2842       bfd_set_error (bfd_error_bad_value);
2843       return FALSE;
2844     }
2845   else
2846     {
2847       _bfd_error_handler
2848         (_("Warning: %B: Unknown ARC object attribute %d."),
2849          abfd, tag);
2850       return TRUE;
2851     }
2852 }
2853
2854 /* Handle an ARC specific section when reading an object file.  This is
2855    called when bfd_section_from_shdr finds a section with an unknown
2856    type.  */
2857
2858 static bfd_boolean
2859 elf32_arc_section_from_shdr (bfd *abfd,
2860                              Elf_Internal_Shdr * hdr,
2861                              const char *name,
2862                              int shindex)
2863 {
2864   switch (hdr->sh_type)
2865     {
2866     case SHT_ARC_ATTRIBUTES:
2867       break;
2868
2869     default:
2870       return FALSE;
2871     }
2872
2873   if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2874     return FALSE;
2875
2876   return TRUE;
2877 }
2878
2879 #define TARGET_LITTLE_SYM   arc_elf32_le_vec
2880 #define TARGET_LITTLE_NAME  "elf32-littlearc"
2881 #define TARGET_BIG_SYM      arc_elf32_be_vec
2882 #define TARGET_BIG_NAME     "elf32-bigarc"
2883 #define ELF_ARCH            bfd_arch_arc
2884 #define ELF_TARGET_ID       ARC_ELF_DATA
2885 #define ELF_MACHINE_CODE    EM_ARC_COMPACT
2886 #define ELF_MACHINE_ALT1    EM_ARC_COMPACT2
2887 #define ELF_MAXPAGESIZE     0x2000
2888
2889 #define bfd_elf32_bfd_link_hash_table_create    arc_elf_link_hash_table_create
2890
2891 #define bfd_elf32_bfd_merge_private_bfd_data    arc_elf_merge_private_bfd_data
2892 #define bfd_elf32_bfd_reloc_type_lookup         arc_elf32_bfd_reloc_type_lookup
2893 #define bfd_elf32_bfd_set_private_flags         arc_elf_set_private_flags
2894 #define bfd_elf32_bfd_print_private_bfd_data    arc_elf_print_private_bfd_data
2895 #define bfd_elf32_bfd_copy_private_bfd_data     arc_elf_copy_private_bfd_data
2896
2897 #define elf_info_to_howto_rel                arc_info_to_howto_rel
2898 #define elf_backend_object_p                 arc_elf_object_p
2899 #define elf_backend_final_write_processing   arc_elf_final_write_processing
2900
2901 #define elf_backend_relocate_section         elf_arc_relocate_section
2902 #define elf_backend_check_relocs             elf_arc_check_relocs
2903 #define elf_backend_create_dynamic_sections  _bfd_elf_create_dynamic_sections
2904
2905 #define elf_backend_reloc_type_class            elf32_arc_reloc_type_class
2906
2907 #define elf_backend_adjust_dynamic_symbol    elf_arc_adjust_dynamic_symbol
2908 #define elf_backend_finish_dynamic_symbol    elf_arc_finish_dynamic_symbol
2909
2910 #define elf_backend_finish_dynamic_sections  elf_arc_finish_dynamic_sections
2911 #define elf_backend_size_dynamic_sections    elf_arc_size_dynamic_sections
2912 #define elf_backend_add_symbol_hook          elf_arc_add_symbol_hook
2913
2914 #define elf_backend_can_gc_sections     1
2915 #define elf_backend_want_got_plt        1
2916 #define elf_backend_plt_readonly        1
2917 #define elf_backend_rela_plts_and_copies_p 1
2918 #define elf_backend_want_plt_sym        0
2919 #define elf_backend_got_header_size     12
2920 #define elf_backend_dtrel_excludes_plt  1
2921
2922 #define elf_backend_may_use_rel_p       0
2923 #define elf_backend_may_use_rela_p      1
2924 #define elf_backend_default_use_rela_p  1
2925
2926 #define elf_backend_grok_prstatus elf32_arc_grok_prstatus
2927
2928 #define elf_backend_default_execstack   0
2929
2930 #undef  elf_backend_obj_attrs_vendor
2931 #define elf_backend_obj_attrs_vendor            "ARC"
2932 #undef  elf_backend_obj_attrs_section
2933 #define elf_backend_obj_attrs_section           ".ARC.attributes"
2934 #undef  elf_backend_obj_attrs_arg_type
2935 #define elf_backend_obj_attrs_arg_type          elf32_arc_obj_attrs_arg_type
2936 #undef  elf_backend_obj_attrs_section_type
2937 #define elf_backend_obj_attrs_section_type      SHT_ARC_ATTRIBUTES
2938 #define elf_backend_obj_attrs_handle_unknown    elf32_arc_obj_attrs_handle_unknown
2939
2940 #define elf_backend_section_from_shdr           elf32_arc_section_from_shdr
2941
2942 #include "elf32-target.h"