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