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