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