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