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