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