[ARC] Fix condition.
[deliverable/binutils-gdb.git] / bfd / elf32-arc.c
CommitLineData
252b5132 1/* ARC-specific support for 32-bit ELF
6f2750fe 2 Copyright (C) 1994-2016 Free Software Foundation, Inc.
886a2506 3 Contributed by Cupertino Miranda (cmiranda@synopsys.com).
252b5132 4
0d2bcfaf 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
0d2bcfaf
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
0d2bcfaf 10 (at your option) any later version.
252b5132 11
0d2bcfaf
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
0d2bcfaf
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
47b0e7ad
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
252b5132
RH
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/arc.h"
ea04a8f6 27#include "libiberty.h"
886a2506 28#include "opcode/arc-func.h"
4b0c052e 29#include "opcode/arc.h"
34e967a5 30#include "arc-plt.h"
886a2506 31
34e967a5
MC
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
886a2506 40#define ARC_DEBUG(...)
34e967a5
MC
41#else
42static char *
43name_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; \
23a42089 59 bfd_byte * _loc; \
34e967a5 60 \
23a42089 61 _loc = _htab->srel##SECTION->contents \
34e967a5
MC
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; \
94e5c971 68 BFD_ASSERT ((long) SYM_IDX != -1); \
34e967a5 69 _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE); \
23a42089 70 bfd_elf32_swap_reloca_out (BFD, &_rel, _loc); \
34e967a5 71 }
886a2506 72
886a2506
NC
73struct dynamic_sections
74{
75 bfd_boolean initialized;
76 asection * sgot;
77 asection * srelgot;
78 asection * sgotplt;
34e967a5 79 asection * srelgotplt;
886a2506
NC
80 asection * sdyn;
81 asection * splt;
82 asection * srelplt;
83};
84
886a2506
NC
85enum dyn_section_types
86{
87 got = 0,
88 relgot,
89 gotplt,
90 dyn,
91 plt,
92 relplt,
93 DYN_SECTION_TYPES_END
94};
95
96const 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
34e967a5
MC
106enum 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
115enum tls_got_entries
116{
33cbe6c0
AB
117 TLS_GOT_NONE = 0,
118 TLS_GOT_MOD,
119 TLS_GOT_OFF,
120 TLS_GOT_MOD_AND_OFF
34e967a5
MC
121};
122
123struct 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
133static void
134new_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
163static bfd_boolean
164symbol_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
886a2506
NC
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
182char * init_str = INIT_SYM_STRING;
183char * fini_str = FINI_SYM_STRING;
184
886a2506
NC
185#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
186 case VALUE: \
b05a65d0 187 return "R_" #TYPE; \
886a2506
NC
188 break;
189
190static ATTRIBUTE_UNUSED const char *
191reloc_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
252b5132 203
252b5132
RH
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). */
0d2bcfaf 206
886a2506
NC
207#define USE_REL 1
208
209static ATTRIBUTE_UNUSED bfd_boolean
210is_reloc_PC_relative (reloc_howto_type *howto)
211{
212 return (strstr (howto->name, "PC") != NULL) ? TRUE : FALSE;
213}
214
215static bfd_boolean
216is_reloc_SDA_relative (reloc_howto_type *howto)
217{
218 return (strstr (howto->name, "SDA") != NULL) ? TRUE : FALSE;
219}
220
221static bfd_boolean
222is_reloc_for_GOT (reloc_howto_type * howto)
223{
34e967a5
MC
224 if (strstr (howto->name, "TLS") != NULL)
225 return FALSE;
886a2506
NC
226 return (strstr (howto->name, "GOT") != NULL) ? TRUE : FALSE;
227}
228
229static bfd_boolean
230is_reloc_for_PLT (reloc_howto_type * howto)
231{
232 return (strstr (howto->name, "PLT") != NULL) ? TRUE : FALSE;
233}
234
34e967a5
MC
235static bfd_boolean
236is_reloc_for_TLS (reloc_howto_type *howto)
237{
238 return (strstr (howto->name, "TLS") != NULL) ? TRUE : FALSE;
239}
240
886a2506
NC
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)
72f3b6aa 243#define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
886a2506
NC
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)
72f3b6aa 246#define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
886a2506 247
252b5132 248
47b0e7ad 249static bfd_reloc_status_type
886a2506
NC
250arc_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,
277enum 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) \
b05a65d0 285 [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, FALSE, 0, complain_overflow_##OVERFLOW, arc_elf_reloc, "R_" #TYPE, FALSE, 0, 0, FALSE),
886a2506
NC
286
287static struct reloc_howto_struct elf_arc_howto_table[] =
288{
289#include "elf/arc-reloc.def"
34e967a5
MC
290/* Example of what is generated by the preprocessor. Currently kept as an
291 example.
886a2506
NC
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
309static void arc_elf_howto_init (void)
310{
311#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
6f4b1afc 312 elf_arc_howto_table[TYPE].pc_relative = \
0a5ff21b 313 (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
72f3b6aa
CZ
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 }
886a2506 320
34e967a5 321#include "elf/arc-reloc.def"
72f3b6aa 322
47b0e7ad 323}
886a2506 324#undef ARC_RELOC_HOWTO
47b0e7ad 325
886a2506
NC
326
327#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
328 [TYPE] = VALUE,
329const int howto_table_lookup[] =
330{
34e967a5 331#include "elf/arc-reloc.def"
252b5132 332};
886a2506
NC
333#undef ARC_RELOC_HOWTO
334
f26dd308
AM
335static reloc_howto_type *
336arc_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}
252b5132
RH
342
343/* Map BFD reloc types to ARC ELF reloc types. */
344
345struct arc_reloc_map
346{
34e967a5
MC
347 bfd_reloc_code_real_type bfd_reloc_val;
348 unsigned char elf_reloc_val;
252b5132
RH
349};
350
886a2506
NC
351#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
352 { BFD_RELOC_##TYPE, R_##TYPE },
252b5132
RH
353static const struct arc_reloc_map arc_reloc_map[] =
354{
34e967a5
MC
355#include "elf/arc-reloc.def"
356
886a2506
NC
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},
252b5132 362};
886a2506 363#undef ARC_RELOC_HOWTO
252b5132 364
094fb063
CZ
365typedef 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;
371static replace_func
372get_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
252b5132 388static reloc_howto_type *
34e967a5 389arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
47b0e7ad 390 bfd_reloc_code_real_type code)
252b5132
RH
391{
392 unsigned int i;
393
ea04a8f6 394 for (i = ARRAY_SIZE (arc_reloc_map); i--;)
886a2506
NC
395 {
396 if (arc_reloc_map[i].bfd_reloc_val == code)
f26dd308 397 return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
886a2506 398 }
ea04a8f6 399
252b5132
RH
400 return NULL;
401}
402
34e967a5
MC
403/* Function to set the ELF flag bits. */
404static bfd_boolean
405arc_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. */
413static bfd_boolean
414arc_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 {
34e967a5
MC
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
455static bfd_boolean
456arc_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
157090f7 474static reloc_howto_type *
34e967a5
MC
475bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
476 const char *r_name)
157090f7
AM
477{
478 unsigned int i;
479
886a2506 480 for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
157090f7
AM
481 if (elf_arc_howto_table[i].name != NULL
482 && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
f26dd308 483 return arc_elf_howto (i);
157090f7
AM
484
485 return NULL;
486}
487
886a2506 488/* Set the howto pointer for an ARC ELF reloc. */
34e967a5 489
252b5132 490static void
886a2506
NC
491arc_info_to_howto_rel (bfd * abfd ATTRIBUTE_UNUSED,
492 arelent * cache_ptr,
493 Elf_Internal_Rela * dst)
252b5132
RH
494{
495 unsigned int r_type;
496
497 r_type = ELF32_R_TYPE (dst->r_info);
886a2506 498 BFD_ASSERT (r_type < (unsigned int) R_ARC_max);
f26dd308 499 cache_ptr->howto = arc_elf_howto (r_type);
252b5132
RH
500}
501
34e967a5
MC
502/* Merge backend specific data from an object file to the output
503 object file when linking. */
504
505static bfd_boolean
506arc_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
252b5132 600/* Set the right machine number for an ARC ELF file. */
b34976b6 601static bfd_boolean
886a2506 602arc_elf_object_p (bfd * abfd)
252b5132 603{
886a2506
NC
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;
252b5132 610
886a2506 611 if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
252b5132 612 {
0d2bcfaf
NC
613 switch (arch)
614 {
886a2506
NC
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;
8699fc3e
AB
624 case E_ARC_MACH_NPS400:
625 mach = bfd_mach_arc_nps400;
626 break;
886a2506
NC
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"));
0d2bcfaf 650 }
252b5132 651 }
886a2506 652
0d2bcfaf 653 return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
252b5132
RH
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
659static void
34e967a5
MC
660arc_elf_final_write_processing (bfd * abfd,
661 bfd_boolean linker ATTRIBUTE_UNUSED)
252b5132 662{
886a2506 663 unsigned long emf;
252b5132 664
0d2bcfaf 665 switch (bfd_get_mach (abfd))
252b5132 666 {
886a2506 667 case bfd_mach_arc_arc600:
886a2506 668 emf = EM_ARC_COMPACT;
0d2bcfaf 669 break;
886a2506 670 case bfd_mach_arc_arc601:
886a2506 671 emf = EM_ARC_COMPACT;
0d2bcfaf 672 break;
886a2506 673 case bfd_mach_arc_arc700:
886a2506 674 emf = EM_ARC_COMPACT;
0d2bcfaf 675 break;
8699fc3e
AB
676 case bfd_mach_arc_nps400:
677 emf = EM_ARC_COMPACT;
678 break;
886a2506 679 case bfd_mach_arc_arcv2:
886a2506 680 emf = EM_ARC_COMPACT2;
0d2bcfaf 681 break;
886a2506 682 default:
3c8adaca 683 goto DO_NOTHING;
252b5132 684 }
34e967a5 685
886a2506 686 elf_elfheader (abfd)->e_machine = emf;
7e458899 687
886a2506
NC
688 /* Record whatever is the current syscall ABI version. */
689 elf_elfheader (abfd)->e_flags |= E_ARC_OSABI_CURRENT;
3c8adaca
CZ
690
691DO_NOTHING:
692 return;
252b5132
RH
693}
694
886a2506
NC
695#define BFD_DEBUG_PIC(...)
696
697struct arc_relocation_data
698{
094fb063
CZ
699 bfd_signed_vma reloc_offset;
700 bfd_signed_vma reloc_addend;
701 bfd_signed_vma got_offset_value;
886a2506 702
094fb063 703 bfd_signed_vma sym_value;
886a2506
NC
704 asection * sym_section;
705
706 reloc_howto_type *howto;
707
708 asection * input_section;
709
094fb063 710 bfd_signed_vma sdata_begin_symbol_vma;
886a2506 711 bfd_boolean sdata_begin_symbol_vma_set;
094fb063 712 bfd_signed_vma got_symbol_vma;
886a2506
NC
713
714 bfd_boolean should_relocate;
4b0c052e
AB
715
716 const char * symbol_name;
886a2506
NC
717};
718
719static void
720debug_arc_reloc (struct arc_relocation_data reloc_data)
721{
34e967a5 722 PR_DEBUG ("Reloc type=%s, should_relocate = %s\n",
886a2506
NC
723 reloc_data.howto->name,
724 reloc_data.should_relocate ? "true" : "false");
34e967a5 725 PR_DEBUG (" offset = 0x%x, addend = 0x%x\n",
886a2506
NC
726 (unsigned int) reloc_data.reloc_offset,
727 (unsigned int) reloc_data.reloc_addend);
34e967a5
MC
728 PR_DEBUG (" Symbol:\n");
729 PR_DEBUG (" value = 0x%08x\n",
886a2506
NC
730 (unsigned int) reloc_data.sym_value);
731 if (reloc_data.sym_section != NULL)
732 {
3c8adaca 733 PR_DEBUG (" Symbol Section:\n");
34e967a5
MC
734 PR_DEBUG (
735 " section name = %s, output_offset 0x%08x",
886a2506 736 reloc_data.sym_section->name,
34e967a5
MC
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 }
34e967a5 744 PR_DEBUG ( "\n");
3c8adaca 745 PR_DEBUG (" file: %s\n", reloc_data.sym_section->owner->filename);
886a2506
NC
746 }
747 else
34e967a5
MC
748 {
749 PR_DEBUG ( " symbol section is NULL\n");
750 }
886a2506 751
34e967a5 752 PR_DEBUG ( " Input_section:\n");
886a2506
NC
753 if (reloc_data.input_section != NULL)
754 {
34e967a5 755 PR_DEBUG (
886a2506
NC
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);
34e967a5 760 PR_DEBUG ( " changed_address = 0x%08x\n",
886a2506
NC
761 (unsigned int) (reloc_data.input_section->output_section->vma +
762 reloc_data.input_section->output_offset +
763 reloc_data.reloc_offset));
3c8adaca 764 PR_DEBUG (" file: %s\n", reloc_data.input_section->owner->filename);
886a2506
NC
765 }
766 else
34e967a5
MC
767 {
768 PR_DEBUG ( " input section is NULL\n");
769 }
886a2506
NC
770}
771
72f3b6aa
CZ
772static bfd_vma
773middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
6f4b1afc 774{
72f3b6aa
CZ
775 if (do_it)
776 {
777 insn =
778 ((insn & 0xffff0000) >> 16) |
779 ((insn & 0xffff) << 16);
780 }
781 return insn;
6f4b1afc
CM
782}
783
4b0c052e
AB
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
788static inline bfd_reloc_status_type
789arc_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
72f3b6aa
CZ
830#define ME(reloc) (reloc)
831
832#define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
833 && (!bfd_big_endian (BFD)))
834
094fb063 835#define S ((bfd_signed_vma) (reloc_data.sym_value \
34e967a5
MC
836 + (reloc_data.sym_section->output_section != NULL ? \
837 (reloc_data.sym_section->output_offset \
094fb063
CZ
838 + reloc_data.sym_section->output_section->vma) : 0)))
839#define L ((bfd_signed_vma) (reloc_data.sym_value \
34e967a5
MC
840 + (reloc_data.sym_section->output_section != NULL ? \
841 (reloc_data.sym_section->output_offset \
094fb063 842 + reloc_data.sym_section->output_section->vma) : 0)))
886a2506
NC
843#define A (reloc_data.reloc_addend)
844#define B (0)
845#define G (reloc_data.got_offset_value)
34e967a5
MC
846#define GOT (reloc_data.got_symbol_vma)
847#define GOT_BEGIN (htab->sgot->output_section->vma)
848
886a2506 849#define MES (0)
34e967a5
MC
850 /* P: relative offset to PCL The offset should be to the
851 current location aligned to 32 bits. */
094fb063 852#define P ((bfd_signed_vma) ( \
34e967a5
MC
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 \
094fb063
CZ
857 + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \
858 & ~0x3))
859#define PDATA ((bfd_signed_vma) ( \
6f4b1afc
CM
860 (reloc_data.input_section->output_section->vma \
861 + reloc_data.input_section->output_offset \
094fb063 862 + (reloc_data.reloc_offset))))
2ab2f40d
CM
863#define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
864 + reloc_data.sym_section->output_offset)
865
094fb063
CZ
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)
34e967a5
MC
869#define TLS_TBSS (8)
870#define TCB_SIZE (8)
871
886a2506
NC
872#define none (0)
873
094fb063 874#define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
34e967a5
MC
875 {\
876 asection *sym_section = reloc_data.sym_section; \
877 asection *input_section = reloc_data.input_section; \
094fb063
CZ
878 ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \
879 ARC_DEBUG ("FORMULA = " FORMULA "\n"); \
34e967a5
MC
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
886a2506
NC
917#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
918 case R_##TYPE: \
919 { \
094fb063 920 bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
886a2506 921 relocation = FORMULA ; \
094fb063 922 PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \
72f3b6aa 923 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
094fb063 924 insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \
72f3b6aa 925 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
34e967a5 926 PRINT_DEBUG_RELOC_INFO_AFTER \
886a2506
NC
927 } \
928 break;
929
930static bfd_reloc_status_type
34e967a5
MC
931arc_do_relocation (bfd_byte * contents,
932 struct arc_relocation_data reloc_data,
933 struct bfd_link_info *info)
886a2506 934{
094fb063 935 bfd_signed_vma relocation = 0;
886a2506
NC
936 bfd_vma insn;
937 bfd_vma orig_insn ATTRIBUTE_UNUSED;
72f3b6aa 938 bfd * abfd = reloc_data.input_section->owner;
34e967a5 939 struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
4b0c052e 940 bfd_reloc_status_type flag;
886a2506
NC
941
942 if (reloc_data.should_relocate == FALSE)
34e967a5 943 return bfd_reloc_ok;
886a2506
NC
944
945 switch (reloc_data.howto->size)
946 {
947 case 2:
72f3b6aa 948 insn = arc_bfd_get_32 (abfd,
886a2506
NC
949 contents + reloc_data.reloc_offset,
950 reloc_data.input_section);
951 break;
952 case 1:
72f3b6aa
CZ
953 insn = arc_bfd_get_16 (abfd,
954 contents + reloc_data.reloc_offset,
955 reloc_data.input_section);
956 break;
886a2506 957 case 0:
72f3b6aa 958 insn = arc_bfd_get_8 (abfd,
886a2506
NC
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 {
34e967a5 972#include "elf/arc-reloc.def"
886a2506
NC
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)
4b0c052e
AB
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);
886a2506 988
34e967a5 989#undef DEBUG_ARC_RELOC
886a2506 990#define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
4b0c052e
AB
991 if (flag != bfd_reloc_ok)
992 {
993 PR_DEBUG ( "Relocation overflows !!!!\n");
886a2506 994
4b0c052e 995 DEBUG_ARC_RELOC (reloc_data);
886a2506 996
4b0c052e
AB
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;
886a2506 1004 }
34e967a5 1005#undef DEBUG_ARC_RELOC
886a2506
NC
1006#define DEBUG_ARC_RELOC(A)
1007
4b0c052e 1008 /* Write updated instruction back to memory. */
886a2506
NC
1009 switch (reloc_data.howto->size)
1010 {
1011 case 2:
72f3b6aa 1012 arc_bfd_put_32 (abfd, insn,
886a2506
NC
1013 contents + reloc_data.reloc_offset,
1014 reloc_data.input_section);
1015 break;
1016 case 1:
72f3b6aa
CZ
1017 arc_bfd_put_16 (abfd, insn,
1018 contents + reloc_data.reloc_offset,
1019 reloc_data.input_section);
1020 break;
886a2506 1021 case 0:
72f3b6aa 1022 arc_bfd_put_8 (abfd, insn,
886a2506
NC
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
34e967a5
MC
1049static struct got_entry **
1050arc_get_local_got_ents (bfd * abfd)
886a2506 1051{
34e967a5 1052 static struct got_entry **local_got_ents = NULL;
886a2506 1053
34e967a5 1054 if (local_got_ents == NULL)
886a2506
NC
1055 {
1056 size_t size;
886a2506
NC
1057 Elf_Internal_Shdr *symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
1058
1059 size = symtab_hdr->sh_info * sizeof (bfd_vma);
34e967a5
MC
1060 local_got_ents = (struct got_entry **)
1061 bfd_alloc (abfd, sizeof(struct got_entry *) * size);
1062 if (local_got_ents == NULL)
886a2506 1063 return FALSE;
34e967a5
MC
1064
1065 memset (local_got_ents, 0, sizeof(struct got_entry *) * size);
1066 elf_local_got_ents (abfd) = local_got_ents;
886a2506
NC
1067 }
1068
34e967a5 1069 return local_got_ents;
886a2506
NC
1070}
1071
886a2506
NC
1072/* Relocate an arc ELF section.
1073 Function : elf_arc_relocate_section
1074 Brief : Relocate an arc section, by handling all the relocations
34e967a5 1075 appearing in that section.
886a2506 1076 Args : output_bfd : The bfd being written to.
34e967a5
MC
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. */
886a2506 1086static bfd_boolean
34e967a5 1087elf_arc_relocate_section (bfd * output_bfd,
886a2506 1088 struct bfd_link_info * info,
34e967a5
MC
1089 bfd * input_bfd,
1090 asection * input_section,
1091 bfd_byte * contents,
886a2506
NC
1092 Elf_Internal_Rela * relocs,
1093 Elf_Internal_Sym * local_syms,
34e967a5 1094 asection ** local_sections)
886a2506 1095{
34e967a5 1096 Elf_Internal_Shdr * symtab_hdr;
886a2506 1097 struct elf_link_hash_entry ** sym_hashes;
34e967a5
MC
1098 struct got_entry ** local_got_ents;
1099 Elf_Internal_Rela * rel;
3c8adaca 1100 Elf_Internal_Rela * wrel;
34e967a5
MC
1101 Elf_Internal_Rela * relend;
1102 struct elf_link_hash_table *htab = elf_hash_table (info);
886a2506
NC
1103
1104 symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
1105 sym_hashes = elf_sym_hashes (input_bfd);
1106
3c8adaca 1107 rel = wrel = relocs;
886a2506 1108 relend = relocs + input_section->reloc_count;
3c8adaca 1109 for (; rel < relend; wrel++, rel++)
886a2506
NC
1110 {
1111 enum elf_arc_reloc_type r_type;
34e967a5
MC
1112 reloc_howto_type * howto;
1113 unsigned long r_symndx;
886a2506 1114 struct elf_link_hash_entry * h;
34e967a5
MC
1115 Elf_Internal_Sym * sym;
1116 asection * sec;
1117 struct elf_link_hash_entry *h2;
886a2506
NC
1118
1119 struct arc_relocation_data reloc_data =
1120 {
34e967a5
MC
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
886a2506
NC
1132 };
1133
34e967a5
MC
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 }
3c8adaca 1141 howto = arc_elf_howto (r_type);
34e967a5
MC
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 }
34e967a5 1176 }
886a2506
NC
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
34e967a5
MC
1182 && h2 != NULL && h2->root.type != bfd_link_hash_undefined
1183 && h2->root.u.def.section->output_section != NULL)
1184 /* TODO: Verify this condition. */
886a2506
NC
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
886a2506
NC
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
886a2506
NC
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];
3c8adaca
CZ
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];
886a2506
NC
1262
1263 reloc_data.sym_value = sym->st_value;
1264 reloc_data.sym_section = sec;
4b0c052e 1265 reloc_data.symbol_name =
3c8adaca
CZ
1266 bfd_elf_string_from_elf_section (input_bfd,
1267 symtab_hdr->sh_link,
1268 sym->st_name);
886a2506 1269
841fdfcd
CZ
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
34e967a5
MC
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;
886a2506
NC
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
3c8adaca
CZ
1384 /* TODO: Need to validate what was the intention. */
1385 /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
4b0c052e 1386 reloc_data.symbol_name = h->root.root.string;
3c8adaca 1387
886a2506
NC
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
34e967a5 1397 if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
886a2506 1398 {
34e967a5
MC
1399 /* TODO: Change it to use arc_do_relocation with
1400 ARC_32 reloc. Try to use ADD_RELA macro. */
886a2506
NC
1401 bfd_vma relocation =
1402 reloc_data.sym_value + reloc_data.reloc_addend
34e967a5
MC
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;
886a2506
NC
1418 }
1419 }
1420 else if (h->root.type == bfd_link_hash_undefweak)
1421 {
1422 /* Is weak symbol and has no definition. */
34e967a5
MC
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;
886a2506
NC
1439 }
1440 else
1441 {
1442 if (is_reloc_for_GOT (howto))
1443 {
886a2506 1444 reloc_data.sym_value = h->root.u.def.value;
34e967a5 1445 reloc_data.sym_section = htab->sgot;
886a2506
NC
1446
1447 reloc_data.should_relocate = TRUE;
1448 }
1449 else if (is_reloc_for_PLT (howto))
1450 {
7e458899
CZ
1451 /* Fail if it is linking for PIE and the symbol is
1452 undefined. */
1a72702b
AM
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);
886a2506 1457 reloc_data.sym_value = h->plt.offset;
34e967a5 1458 reloc_data.sym_section = htab->splt;
886a2506
NC
1459
1460 reloc_data.should_relocate = TRUE;
1461 }
1a72702b
AM
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);
886a2506
NC
1466 }
1467
34e967a5
MC
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
33cbe6c0 1499 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0));
34e967a5
MC
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
33cbe6c0 1507 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0),
34e967a5
MC
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;
886a2506
NC
1571 }
1572
34e967a5
MC
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:
2ab2f40d 1579 if ((bfd_link_pic (info) || bfd_link_pie (info))
34e967a5
MC
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
3c8adaca
CZ
1606#define IS_ARC_PCREL_TYPE(TYPE) \
1607 ( (TYPE == R_ARC_PC32) \
1608 || (TYPE == R_ARC_32_PCREL))
34e967a5
MC
1609 if (skip)
1610 {
1611 memset (&outrel, 0, sizeof outrel);
1612 relocate = FALSE;
1613 }
3c8adaca
CZ
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))
34e967a5 1620 {
94e5c971 1621 BFD_ASSERT (h != NULL);
34e967a5
MC
1622 if ((input_section->flags & SEC_ALLOC) != 0)
1623 relocate = FALSE;
1624 else
1625 relocate = TRUE;
94e5c971
CZ
1626
1627 BFD_ASSERT (h->dynindx != -1);
34e967a5
MC
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. */
3c8adaca
CZ
1642 relocate = TRUE;
1643 /* outrel.r_addend = 0; */
1644 outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
34e967a5
MC
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))
886a2506
NC
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);
34e967a5 1673
2ab2f40d
CM
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. */
815dc1bc
CZ
1676 if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
1677 && reloc_data.sym_section == NULL)
2ab2f40d
CM
1678 {
1679 (*_bfd_error_handler)
1680 (_("GOT and PLT relocations cannot be fixed with a non dynamic linker."));
1681 bfd_set_error (bfd_error_bad_value);
1682 return FALSE;
1683 }
1684
34e967a5 1685 if (arc_do_relocation (contents, reloc_data, info) != bfd_reloc_ok)
886a2506
NC
1686 return FALSE;
1687 }
1688
1689 return TRUE;
1690}
1691
1692static struct dynamic_sections
1693arc_create_dynamic_sections (bfd * abfd, struct bfd_link_info *info)
1694{
34e967a5
MC
1695 struct elf_link_hash_table *htab;
1696 bfd *dynobj;
886a2506 1697 struct dynamic_sections ds =
886a2506 1698 {
34e967a5
MC
1699 .initialized = FALSE,
1700 .sgot = NULL,
1701 .srelgot = NULL,
1702 .sgotplt = NULL,
1703 .srelgotplt = NULL,
1704 .sdyn = NULL,
1705 .splt = NULL,
1706 .srelplt = NULL
1707 };
1708
1709 htab = elf_hash_table (info);
1710 BFD_ASSERT (htab);
1711
1712 /* Create dynamic sections for relocatable executables so that we
1713 can copy relocations. */
1714 if (! htab->dynamic_sections_created && bfd_link_pic (info))
1715 {
1716 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
1717 BFD_ASSERT (0);
886a2506 1718 }
886a2506 1719
34e967a5 1720 dynobj = (elf_hash_table (info))->dynobj;
886a2506 1721
34e967a5 1722 if (dynobj)
886a2506 1723 {
34e967a5
MC
1724 ds.sgot = htab->sgot;
1725 ds.srelgot = htab->srelgot;
886a2506 1726
34e967a5
MC
1727 ds.sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1728 ds.srelgotplt = ds.srelplt;
886a2506 1729
34e967a5
MC
1730 ds.splt = bfd_get_section_by_name (dynobj, ".plt");
1731 ds.srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1732 }
1733
1734 if (htab->dynamic_sections_created)
1735 {
1736 ds.sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1737 }
886a2506
NC
1738
1739 ds.initialized = TRUE;
1740
1741 return ds;
1742}
1743
34e967a5
MC
1744#define ADD_SYMBOL_REF_SEC_AND_RELOC(SECNAME, COND_FOR_RELOC, H) \
1745 htab->s##SECNAME->size; \
1746 { \
1747 if (COND_FOR_RELOC) \
1748 { \
1749 htab->srel##SECNAME->size += sizeof (Elf32_External_Rela); \
1750 ARC_DEBUG ("arc_info: Added reloc space in " \
1751 #SECNAME " section at " __FILE__ \
1752 ":%d for symbol\n", \
1753 __LINE__, name_for_global_symbol (H)); \
1754 } \
1755 if (H) \
1756 if (h->dynindx == -1 && !h->forced_local) \
1757 if (! bfd_elf_link_record_dynamic_symbol (info, H)) \
1758 return FALSE; \
1759 htab->s##SECNAME->size += 4; \
1760 }
886a2506
NC
1761
1762static bfd_boolean
34e967a5 1763elf_arc_check_relocs (bfd * abfd,
886a2506 1764 struct bfd_link_info * info,
34e967a5 1765 asection * sec,
886a2506
NC
1766 const Elf_Internal_Rela * relocs)
1767{
34e967a5
MC
1768 Elf_Internal_Shdr * symtab_hdr;
1769 struct elf_link_hash_entry ** sym_hashes;
1770 struct got_entry ** local_got_ents;
1771 const Elf_Internal_Rela * rel;
1772 const Elf_Internal_Rela * rel_end;
1773 bfd * dynobj;
1774 asection * sreloc = NULL;
1775 struct elf_link_hash_table * htab = elf_hash_table (info);
1776
1777 if (bfd_link_relocatable (info))
1778 return TRUE;
886a2506
NC
1779
1780 dynobj = (elf_hash_table (info))->dynobj;
1781 symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
1782 sym_hashes = elf_sym_hashes (abfd);
34e967a5 1783 local_got_ents = arc_get_local_got_ents (abfd);
886a2506
NC
1784
1785 rel_end = relocs + sec->reloc_count;
1786 for (rel = relocs; rel < rel_end; rel++)
1787 {
1788 enum elf_arc_reloc_type r_type;
1789 reloc_howto_type *howto;
1790 unsigned long r_symndx;
1791 struct elf_link_hash_entry *h;
1792
1793 r_type = ELF32_R_TYPE (rel->r_info);
1794
1795 if (r_type >= (int) R_ARC_max)
1796 {
1797 bfd_set_error (bfd_error_bad_value);
1798 return FALSE;
1799 }
3c8adaca 1800 howto = arc_elf_howto (r_type);
34e967a5
MC
1801
1802 if (dynobj == NULL
1803 && (is_reloc_for_GOT (howto) == TRUE
1804 || is_reloc_for_TLS (howto) == TRUE))
1805 {
1806 dynobj = elf_hash_table (info)->dynobj = abfd;
1807 if (! _bfd_elf_create_got_section (abfd, info))
1808 return FALSE;
1809 }
886a2506
NC
1810
1811 /* Load symbol information. */
1812 r_symndx = ELF32_R_SYM (rel->r_info);
1813 if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol. */
1814 h = NULL;
1815 else /* Global one. */
1816 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1817
34e967a5
MC
1818 switch (r_type)
1819 {
1820 case R_ARC_32:
1821 case R_ARC_32_ME:
1822 /* During shared library creation, these relocs should not
1823 appear in a shared library (as memory will be read only
1824 and the dynamic linker can not resolve these. However
1825 the error should not occur for e.g. debugging or
1826 non-readonly sections. */
2ab2f40d 1827 if ((bfd_link_dll (info) && !bfd_link_pie (info))
34e967a5 1828 && (sec->flags & SEC_ALLOC) != 0
3c8adaca
CZ
1829 && (sec->flags & SEC_READONLY) != 0
1830 && ((sec->flags & SEC_CODE) != 0
1831 || (sec->flags & SEC_DEBUGGING) != 0))
34e967a5
MC
1832 {
1833 const char *name;
1834 if (h)
1835 name = h->root.root.string;
1836 else
1837 /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
1838 name = "UNKNOWN";
1839 (*_bfd_error_handler)
1840 (_("\
1841%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1842 abfd,
1843 arc_elf_howto (r_type)->name,
1844 name);
1845 bfd_set_error (bfd_error_bad_value);
1846 return FALSE;
1847 }
1848
1849 /* In some cases we are not setting the 'non_got_ref'
1850 flag, even though the relocations don't require a GOT
1851 access. We should extend the testing in this area to
1852 ensure that no significant cases are being missed. */
1853 if (h)
1854 h->non_got_ref = 1;
1855 /* FALLTHROUGH */
1856 case R_ARC_PC32:
1857 case R_ARC_32_PCREL:
2ab2f40d 1858 if ((bfd_link_pic (info) || bfd_link_pie (info))
34e967a5
MC
1859 && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
1860 || (h != NULL
1861 && h->dynindx != -1
1862 && (!info->symbolic || !h->def_regular))))
1863 {
1864 if (sreloc == NULL)
1865 {
1866 sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
1867 2, abfd,
1868 /*rela*/
1869 TRUE);
1870
1871 if (sreloc == NULL)
1872 return FALSE;
1873 }
1874 sreloc->size += sizeof (Elf32_External_Rela);
1875
1876 }
1877 default:
1878 break;
1879 }
1880
886a2506
NC
1881 if (is_reloc_for_PLT (howto) == TRUE)
1882 {
1883 if (h == NULL)
1884 continue;
1885 else
1886 h->needs_plt = 1;
1887 }
1888
1889 if (is_reloc_for_GOT (howto) == TRUE)
1890 {
1891 if (h == NULL)
1892 {
1893 /* Local symbol. */
34e967a5
MC
1894 if (local_got_ents[r_symndx] == NULL)
1895 {
1896 bfd_vma offset =
1897 ADD_SYMBOL_REF_SEC_AND_RELOC (got,
1898 bfd_link_pic (info),
1899 NULL);
1900 new_got_entry_to_list (&(local_got_ents[r_symndx]),
33cbe6c0 1901 GOT_NORMAL, offset, TLS_GOT_NONE);
34e967a5 1902 }
886a2506
NC
1903 }
1904 else
1905 {
1906 /* Global symbol. */
1907 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
34e967a5
MC
1908 if (h->got.glist == NULL)
1909 {
1910 bfd_vma offset =
1911 ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
1912 new_got_entry_to_list (&h->got.glist,
33cbe6c0 1913 GOT_NORMAL, offset, TLS_GOT_NONE);
34e967a5 1914 }
886a2506
NC
1915 }
1916 }
886a2506 1917
34e967a5
MC
1918 if (is_reloc_for_TLS (howto) == TRUE)
1919 {
1920 enum tls_type_e type = GOT_UNKNOWN;
886a2506 1921
34e967a5
MC
1922 switch (r_type)
1923 {
1924 case R_ARC_TLS_GD_GOT:
1925 type = GOT_TLS_GD;
1926 break;
1927 case R_ARC_TLS_IE_GOT:
1928 type = GOT_TLS_IE;
1929 break;
1930 default:
1931 break;
1932 }
886a2506 1933
34e967a5
MC
1934 struct got_entry **list = NULL;
1935 if (h != NULL)
1936 list = &(h->got.glist);
1937 else
1938 list = &(local_got_ents[r_symndx]);
886a2506 1939
34e967a5
MC
1940 if (type != GOT_UNKNOWN && !symbol_has_entry_of_type (*list, type))
1941 {
33cbe6c0 1942 enum tls_got_entries entries = TLS_GOT_NONE;
34e967a5
MC
1943 bfd_vma offset =
1944 ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
886a2506 1945
34e967a5
MC
1946 if (type == GOT_TLS_GD)
1947 {
1948 bfd_vma ATTRIBUTE_UNUSED notneeded =
1949 ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
33cbe6c0 1950 entries = TLS_GOT_MOD_AND_OFF;
34e967a5 1951 }
886a2506 1952
33cbe6c0
AB
1953 if (entries == TLS_GOT_NONE)
1954 entries = TLS_GOT_OFF;
886a2506 1955
34e967a5
MC
1956 new_got_entry_to_list (list, type, offset, entries);
1957 }
1958 }
1959 }
886a2506 1960
34e967a5
MC
1961 return TRUE;
1962}
886a2506 1963
34e967a5 1964#define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
886a2506 1965
34e967a5
MC
1966static struct plt_version_t *
1967arc_get_plt_version (struct bfd_link_info *info)
886a2506 1968{
34e967a5 1969 int i;
886a2506 1970
34e967a5
MC
1971 for (i = 0; i < 1; i++)
1972 {
1973 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
1974 plt_versions[i].entry_size,
1975 plt_versions[i].elem_size);
1976 }
886a2506 1977
34e967a5 1978 if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
886a2506 1979 {
34e967a5
MC
1980 if (bfd_link_pic (info))
1981 return &(plt_versions[ELF_ARCV2_PIC]);
1982 else
1983 return &(plt_versions[ELF_ARCV2_ABS]);
1984 }
1985 else
886a2506 1986 {
34e967a5
MC
1987 if (bfd_link_pic (info))
1988 return &(plt_versions[ELF_ARC_PIC]);
1989 else
1990 return &(plt_versions[ELF_ARC_ABS]);
886a2506 1991 }
886a2506
NC
1992}
1993
1994static bfd_vma
1995add_symbol_to_plt (struct bfd_link_info *info)
1996{
34e967a5 1997 struct elf_link_hash_table *htab = elf_hash_table (info);
886a2506
NC
1998 bfd_vma ret;
1999
34e967a5 2000 struct plt_version_t *plt_data = arc_get_plt_version (info);
886a2506 2001
34e967a5
MC
2002 /* If this is the first .plt entry, make room for the special first
2003 entry. */
2004 if (htab->splt->size == 0)
2005 htab->splt->size += plt_data->entry_size;
886a2506 2006
34e967a5
MC
2007 ret = htab->splt->size;
2008
2009 htab->splt->size += plt_data->elem_size;
2010 ARC_DEBUG ("PLT_SIZE = %d\n", htab->splt->size);
2011
2012 htab->sgotplt->size += 4;
2013 htab->srelplt->size += sizeof (Elf32_External_Rela);
886a2506
NC
2014
2015 return ret;
2016}
2017
34e967a5
MC
2018#define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
2019 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
886a2506
NC
2020
2021static void
34e967a5
MC
2022plt_do_relocs_for_symbol (bfd *abfd,
2023 struct elf_link_hash_table *htab,
2024 const struct plt_reloc *reloc,
886a2506
NC
2025 bfd_vma plt_offset,
2026 bfd_vma symbol_got_offset)
2027{
2028 while (SYM_ONLY (reloc->symbol) != LAST_RELOC)
2029 {
2030 bfd_vma relocation = 0;
2031
2032 switch (SYM_ONLY (reloc->symbol))
2033 {
2034 case SGOT:
2035 relocation =
34e967a5
MC
2036 htab->sgotplt->output_section->vma +
2037 htab->sgotplt->output_offset + symbol_got_offset;
886a2506
NC
2038 break;
2039 }
2040 relocation += reloc->addend;
2041
34e967a5
MC
2042 if (IS_RELATIVE (reloc->symbol))
2043 {
2044 bfd_vma reloc_offset = reloc->offset;
2045 reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0;
2046 reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0;
886a2506 2047
34e967a5
MC
2048 relocation -= htab->splt->output_section->vma
2049 + htab->splt->output_offset
2050 + plt_offset + reloc_offset;
2051 }
2052
2053 /* TODO: being ME is not a property of the relocation but of the
2054 section of which is applying the relocation. */
1e5885b7 2055 if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
886a2506
NC
2056 {
2057 relocation =
2058 ((relocation & 0xffff0000) >> 16) |
2059 ((relocation & 0xffff) << 16);
2060 }
2061
2062 switch (reloc->size)
2063 {
2064 case 32:
34e967a5 2065 bfd_put_32 (htab->splt->output_section->owner,
886a2506 2066 relocation,
34e967a5 2067 htab->splt->contents + plt_offset + reloc->offset);
886a2506
NC
2068 break;
2069 }
2070
34e967a5 2071 reloc = &(reloc[1]); /* Jump to next relocation. */
886a2506
NC
2072 }
2073}
2074
2075static void
34e967a5
MC
2076relocate_plt_for_symbol (bfd *output_bfd,
2077 struct bfd_link_info *info,
886a2506
NC
2078 struct elf_link_hash_entry *h)
2079{
34e967a5
MC
2080 struct plt_version_t *plt_data = arc_get_plt_version (info);
2081 struct elf_link_hash_table *htab = elf_hash_table (info);
886a2506 2082
34e967a5
MC
2083 bfd_vma plt_index = (h->plt.offset - plt_data->entry_size)
2084 / plt_data->elem_size;
886a2506
NC
2085 bfd_vma got_offset = (plt_index + 3) * 4;
2086
34e967a5
MC
2087 ARC_DEBUG ("arc_info: PLT_OFFSET = 0x%x, PLT_ENTRY_VMA = 0x%x, \
2088GOT_ENTRY_OFFSET = 0x%x, GOT_ENTRY_VMA = 0x%x, for symbol %s\n",
2089 h->plt.offset,
2090 htab->splt->output_section->vma
2091 + htab->splt->output_offset
2092 + h->plt.offset,
2093 got_offset,
2094 htab->sgotplt->output_section->vma
2095 + htab->sgotplt->output_offset
2096 + got_offset,
2097 h->root.root.string);
2098
1e5885b7
CZ
2099
2100 {
2101 bfd_vma i = 0;
2102 uint16_t *ptr = (uint16_t *) plt_data->elem;
2103 for (i = 0; i < plt_data->elem_size/2; i++)
2104 {
2105 uint16_t data = ptr[i];
2106 bfd_put_16 (output_bfd,
2107 (bfd_vma) data,
2108 htab->splt->contents + h->plt.offset + (i*2));
2109 }
2110 }
2111
34e967a5
MC
2112 plt_do_relocs_for_symbol (output_bfd, htab,
2113 plt_data->elem_relocs,
2114 h->plt.offset,
886a2506 2115 got_offset);
34e967a5
MC
2116
2117 /* Fill in the entry in the global offset table. */
2118 bfd_put_32 (output_bfd,
2119 (bfd_vma) (htab->splt->output_section->vma
2120 + htab->splt->output_offset),
2121 htab->sgotplt->contents + got_offset);
2122
2123 /* TODO: Fill in the entry in the .rela.plt section. */
2124 {
2125 Elf_Internal_Rela rel;
2126 bfd_byte *loc;
2127
2128 rel.r_offset = (htab->sgotplt->output_section->vma
2129 + htab->sgotplt->output_offset
2130 + got_offset);
2131 rel.r_addend = 0;
94e5c971
CZ
2132
2133 BFD_ASSERT (h->dynindx != -1);
34e967a5
MC
2134 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT);
2135
2136 loc = htab->srelplt->contents;
2137 loc += plt_index * sizeof (Elf32_External_Rela); /* relA */
2138 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2139 }
886a2506
NC
2140}
2141
2142static void
34e967a5
MC
2143relocate_plt_for_entry (bfd *abfd,
2144 struct bfd_link_info *info)
886a2506 2145{
34e967a5
MC
2146 struct plt_version_t *plt_data = arc_get_plt_version (info);
2147 struct elf_link_hash_table *htab = elf_hash_table (info);
886a2506 2148
1e5885b7
CZ
2149 {
2150 bfd_vma i = 0;
2151 uint16_t *ptr = (uint16_t *) plt_data->entry;
2152 for (i = 0; i < plt_data->entry_size/2; i++)
2153 {
2154 uint16_t data = ptr[i];
2155 bfd_put_16 (abfd,
2156 (bfd_vma) data,
2157 htab->splt->contents + (i*2));
2158 }
2159 }
34e967a5 2160 PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
886a2506
NC
2161}
2162
34e967a5
MC
2163/* Desc : Adjust a symbol defined by a dynamic object and referenced
2164 by a regular object. The current definition is in some section of
2165 the dynamic object, but we're not including those sections. We
2166 have to change the definition to something the rest of the link can
886a2506
NC
2167 understand. */
2168
2169static bfd_boolean
2170elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
2171 struct elf_link_hash_entry *h)
2172{
34e967a5 2173 asection *s;
886a2506 2174 bfd *dynobj = (elf_hash_table (info))->dynobj;
34e967a5 2175 struct elf_link_hash_table *htab = elf_hash_table (info);
886a2506 2176
34e967a5
MC
2177 if (h->type == STT_FUNC
2178 || h->type == STT_GNU_IFUNC
2179 || h->needs_plt == 1)
886a2506
NC
2180 {
2181 if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic)
2182 {
2183 /* This case can occur if we saw a PLT32 reloc in an input
2184 file, but the symbol was never referred to by a dynamic
2185 object. In such a case, we don't actually need to build
2186 a procedure linkage table, and we can just do a PC32
2187 reloc instead. */
2188 BFD_ASSERT (h->needs_plt);
2189 return TRUE;
2190 }
2191
2192 /* Make sure this symbol is output as a dynamic symbol. */
2193 if (h->dynindx == -1 && !h->forced_local
2194 && !bfd_elf_link_record_dynamic_symbol (info, h))
2195 return FALSE;
2196
34e967a5
MC
2197 if (bfd_link_pic (info)
2198 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
886a2506
NC
2199 {
2200 bfd_vma loc = add_symbol_to_plt (info);
2201
2202 if (!bfd_link_pic (info) && !h->def_regular)
2203 {
34e967a5 2204 h->root.u.def.section = htab->splt;
886a2506
NC
2205 h->root.u.def.value = loc;
2206 }
2207 h->plt.offset = loc;
2208 }
34e967a5
MC
2209 else
2210 {
2211 h->plt.offset = (bfd_vma) -1;
2212 h->needs_plt = 0;
2213 }
2214 return TRUE;
886a2506 2215 }
34e967a5
MC
2216
2217 /* If this is a weak symbol, and there is a real definition, the
2218 processor independent code will have arranged for us to see the
2219 real definition first, and we can just use the same value. */
2220 if (h->u.weakdef != NULL)
886a2506 2221 {
34e967a5
MC
2222 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2223 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2224 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2225 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2226 return TRUE;
886a2506
NC
2227 }
2228
34e967a5
MC
2229 /* This is a reference to a symbol defined by a dynamic object which
2230 is not a function. */
2231
2232 /* If we are creating a shared library, we must presume that the
2233 only references to the symbol are via the global offset table.
2234 For such cases we need not do anything here; the relocations will
2235 be handled correctly by relocate_section. */
3c8adaca
CZ
2236 if (!bfd_link_executable (info))
2237 return TRUE;
2238
2239 /* If there are no non-GOT references, we do not need a copy
2240 relocation. */
2241 if (!h->non_got_ref)
34e967a5
MC
2242 return TRUE;
2243
3c8adaca
CZ
2244 /* If -z nocopyreloc was given, we won't generate them either. */
2245 if (info->nocopyreloc)
2246 {
2247 h->non_got_ref = 0;
2248 return TRUE;
2249 }
2250
34e967a5
MC
2251 /* We must allocate the symbol in our .dynbss section, which will
2252 become part of the .bss section of the executable. There will be
2253 an entry for this symbol in the .dynsym section. The dynamic
2254 object will contain position independent code, so all references
2255 from the dynamic object to this symbol will go through the global
2256 offset table. The dynamic linker will use the .dynsym entry to
2257 determine the address it must put in the global offset table, so
2258 both the dynamic object and the regular object will refer to the
2259 same memory location for the variable. */
2260
3c8adaca
CZ
2261 if (htab == NULL)
2262 return FALSE;
34e967a5
MC
2263
2264 /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
2265 copy the initial value out of the dynamic object and into the
2266 runtime process image. We need to remember the offset into the
2267 .rela.bss section we are going to use. */
2268 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2269 {
2270 asection *srel;
2271
2272 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2273 BFD_ASSERT (srel != NULL);
2274 srel->size += sizeof (Elf32_External_Rela);
2275 h->needs_copy = 1;
2276 }
2277
3c8adaca
CZ
2278 s = bfd_get_section_by_name (dynobj, ".dynbss");
2279 BFD_ASSERT (s != NULL);
34e967a5 2280
3c8adaca 2281 return _bfd_elf_adjust_dynamic_copy (info, h, s);
886a2506
NC
2282}
2283
2284/* Function : elf_arc_finish_dynamic_symbol
2285 Brief : Finish up dynamic symbol handling. We set the
34e967a5 2286 contents of various dynamic sections here.
886a2506 2287 Args : output_bfd :
34e967a5
MC
2288 info :
2289 h :
2290 sym :
886a2506 2291 Returns : True/False as the return status. */
34e967a5 2292
886a2506
NC
2293static bfd_boolean
2294elf_arc_finish_dynamic_symbol (bfd * output_bfd,
2295 struct bfd_link_info *info,
2296 struct elf_link_hash_entry *h,
2297 Elf_Internal_Sym * sym)
2298{
34e967a5 2299 if (h->plt.offset != (bfd_vma) -1)
886a2506 2300 {
34e967a5
MC
2301 relocate_plt_for_symbol (output_bfd, info, h);
2302
2303 if (!h->def_regular)
886a2506 2304 {
34e967a5
MC
2305 /* Mark the symbol as undefined, rather than as defined in
2306 the .plt section. Leave the value alone. */
2307 sym->st_shndx = SHN_UNDEF;
886a2506 2308 }
34e967a5
MC
2309 }
2310
2311 if (h->got.glist != NULL)
2312 {
2313 struct got_entry *list = h->got.glist;
2314
2315 /* Traverse the list of got entries for this symbol. */
2316 while (list)
886a2506 2317 {
34e967a5
MC
2318 bfd_vma got_offset = h->got.glist->offset;
2319
2320 if (list->type == GOT_NORMAL
2321 && list->created_dyn_relocation == FALSE)
2322 {
2323 if (bfd_link_pic (info)
2324 && (info->symbolic || h->dynindx == -1)
2325 && h->def_regular)
2326 {
2327 ADD_RELA (output_bfd, got, got_offset, 0, R_ARC_RELATIVE, 0);
2328 }
94e5c971
CZ
2329 /* Do not fully understand the side effects of this condition.
2330 The relocation space might still being reserved. Perhaps
2331 I should clear its value. */
2332 else if (h->dynindx != -1)
34e967a5
MC
2333 {
2334 ADD_RELA (output_bfd, got, got_offset, h->dynindx,
2335 R_ARC_GLOB_DAT, 0);
2336 }
2337 list->created_dyn_relocation = TRUE;
2338 }
33cbe6c0 2339 else if (list->existing_entries != TLS_GOT_NONE)
34e967a5
MC
2340 {
2341 struct elf_link_hash_table *htab = elf_hash_table (info);
2342 enum tls_got_entries e = list->existing_entries;
2343
2344 BFD_ASSERT (list->type != GOT_TLS_GD
33cbe6c0 2345 || list->existing_entries == TLS_GOT_MOD_AND_OFF);
34e967a5 2346
840855c5 2347 bfd_vma dynindx = h->dynindx == -1 ? 0 : h->dynindx;
33cbe6c0 2348 if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_MOD)
34e967a5 2349 {
840855c5 2350 ADD_RELA (output_bfd, got, got_offset, dynindx,
34e967a5
MC
2351 R_ARC_TLS_DTPMOD, 0);
2352 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2353GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2354 list->type,
2355 got_offset,
2356 htab->sgot->output_section->vma
2357 + htab->sgot->output_offset + got_offset,
840855c5 2358 dynindx, 0);
34e967a5 2359 }
33cbe6c0 2360 if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_OFF)
34e967a5
MC
2361 {
2362 bfd_vma addend = 0;
2363 if (list->type == GOT_TLS_IE)
2364 addend = bfd_get_32 (output_bfd,
2365 htab->sgot->contents + got_offset);
2366
2367 ADD_RELA (output_bfd, got,
33cbe6c0 2368 got_offset + (e == TLS_GOT_MOD_AND_OFF ? 4 : 0),
840855c5 2369 dynindx,
34e967a5
MC
2370 (list->type == GOT_TLS_IE ?
2371 R_ARC_TLS_TPOFF : R_ARC_TLS_DTPOFF),
2372 addend);
2373
2374 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2375GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2376 list->type,
2377 got_offset,
2378 htab->sgot->output_section->vma
2379 + htab->sgot->output_offset + got_offset,
840855c5 2380 dynindx, addend);
34e967a5
MC
2381 }
2382 }
2383
2384 list = list->next;
886a2506 2385 }
34e967a5
MC
2386
2387 h->got.glist = NULL;
2388 }
2389
2390 if (h->needs_copy)
2391 {
2392 bfd_vma rel_offset = (h->root.u.def.value
2393 + h->root.u.def.section->output_section->vma
2394 + h->root.u.def.section->output_offset);
2395
2396 asection *srelbss =
2397 bfd_get_section_by_name (h->root.u.def.section->owner,
2398 ".rela.bss");
2399
23a42089
NC
2400 bfd_byte * loc = srelbss->contents
2401 + (srelbss->reloc_count * sizeof (Elf32_External_Rela));
34e967a5
MC
2402 srelbss->reloc_count++;
2403
2404 Elf_Internal_Rela rel;
2405 rel.r_addend = 0;
2406 rel.r_offset = rel_offset;
94e5c971
CZ
2407
2408 BFD_ASSERT (h->dynindx != -1);
34e967a5
MC
2409 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY);
2410
23a42089 2411 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
886a2506
NC
2412 }
2413
2414 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2415 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2416 || strcmp (h->root.root.string, "__DYNAMIC") == 0
2417 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2418 sym->st_shndx = SHN_ABS;
2419
2420 return TRUE;
2421}
2422
4ade44b7 2423#define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \
34e967a5
MC
2424 case TAG: \
2425 if (SYMBOL != NULL) \
4ade44b7
AM
2426 h = elf_link_hash_lookup (elf_hash_table (info), \
2427 SYMBOL, FALSE, FALSE, TRUE); \
34e967a5 2428 else if (SECTION != NULL) \
4ade44b7 2429 s = bfd_get_linker_section (dynobj, SECTION); \
34e967a5 2430 break;
886a2506
NC
2431
2432/* Function : elf_arc_finish_dynamic_sections
2433 Brief : Finish up the dynamic sections handling.
2434 Args : output_bfd :
34e967a5
MC
2435 info :
2436 h :
2437 sym :
886a2506 2438 Returns : True/False as the return status. */
34e967a5 2439
886a2506 2440static bfd_boolean
34e967a5
MC
2441elf_arc_finish_dynamic_sections (bfd * output_bfd,
2442 struct bfd_link_info *info)
886a2506
NC
2443{
2444 struct dynamic_sections ds = arc_create_dynamic_sections (output_bfd, info);
34e967a5 2445 struct elf_link_hash_table *htab = elf_hash_table (info);
886a2506
NC
2446 bfd *dynobj = (elf_hash_table (info))->dynobj;
2447
2448 if (ds.sdyn)
2449 {
2450 Elf32_External_Dyn *dyncon, *dynconend;
2451
2452 dyncon = (Elf32_External_Dyn *) ds.sdyn->contents;
2453 dynconend =
34e967a5 2454 (Elf32_External_Dyn *) (ds.sdyn->contents + ds.sdyn->size);
886a2506
NC
2455 for (; dyncon < dynconend; dyncon++)
2456 {
2457 Elf_Internal_Dyn internal_dyn;
2458 bfd_boolean do_it = FALSE;
2459
2460 struct elf_link_hash_entry *h = NULL;
2461 asection *s = NULL;
2462
2463 bfd_elf32_swap_dyn_in (dynobj, dyncon, &internal_dyn);
2464
2465 switch (internal_dyn.d_tag)
2466 {
4ade44b7
AM
2467 GET_SYMBOL_OR_SECTION (DT_INIT, "_init", NULL)
2468 GET_SYMBOL_OR_SECTION (DT_FINI, "_fini", NULL)
2469 GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt")
2470 GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt")
2471 GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt")
2472 GET_SYMBOL_OR_SECTION (DT_RELASZ, NULL, ".rela.plt")
2473 GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version")
2474 GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d")
2475 GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r")
886a2506
NC
2476 default:
2477 break;
2478 }
2479
34e967a5 2480 /* In case the dynamic symbols should be updated with a symbol. */
886a2506
NC
2481 if (h != NULL
2482 && (h->root.type == bfd_link_hash_defined
34e967a5 2483 || h->root.type == bfd_link_hash_defweak))
886a2506
NC
2484 {
2485 asection *asec_ptr;
2486
2487 internal_dyn.d_un.d_val = h->root.u.def.value;
2488 asec_ptr = h->root.u.def.section;
2489 if (asec_ptr->output_section != NULL)
2490 {
2491 internal_dyn.d_un.d_val +=
2492 (asec_ptr->output_section->vma +
2493 asec_ptr->output_offset);
2494 }
2495 else
2496 {
34e967a5
MC
2497 /* The symbol is imported from another shared
2498 library and does not apply to this one. */
886a2506
NC
2499 internal_dyn.d_un.d_val = 0;
2500 }
2501 do_it = TRUE;
2502 }
2503 else if (s != NULL) /* With a section information. */
2504 {
2505 switch (internal_dyn.d_tag)
2506 {
2507 case DT_PLTGOT:
2508 case DT_JMPREL:
34e967a5
MC
2509 case DT_VERSYM:
2510 case DT_VERDEF:
2511 case DT_VERNEED:
4ade44b7
AM
2512 internal_dyn.d_un.d_ptr = (s->output_section->vma
2513 + s->output_offset);
886a2506
NC
2514 do_it = TRUE;
2515 break;
2516
2517 case DT_PLTRELSZ:
2518 internal_dyn.d_un.d_val = s->size;
2519 do_it = TRUE;
2520 break;
2521
2522 case DT_RELASZ:
34e967a5
MC
2523 if (s != NULL)
2524 internal_dyn.d_un.d_val -= s->size;
886a2506
NC
2525 do_it = TRUE;
2526 break;
2527
2528 default:
2529 break;
2530 }
2531 }
2532
4ade44b7 2533 if (do_it)
886a2506
NC
2534 bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon);
2535 }
2536
34e967a5 2537 if (htab->splt->size > 0)
886a2506 2538 {
34e967a5 2539 relocate_plt_for_entry (output_bfd, info);
886a2506
NC
2540 }
2541
34e967a5
MC
2542 /* TODO: Validate this. */
2543 elf_section_data (htab->srelplt->output_section)->this_hdr.sh_entsize
2544 = 0xc;
886a2506
NC
2545 }
2546
2547 /* Fill in the first three entries in the global offset table. */
34e967a5 2548 if (htab->sgot)
886a2506 2549 {
3b63d2ce
CM
2550 struct elf_link_hash_entry *h;
2551 h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_",
2552 FALSE, FALSE, TRUE);
2553
2554 if (h != NULL && h->root.type != bfd_link_hash_undefined
2555 && h->root.u.def.section != NULL)
886a2506 2556 {
3b63d2ce
CM
2557 asection *sec = h->root.u.def.section;
2558
886a2506
NC
2559 if (ds.sdyn == NULL)
2560 bfd_put_32 (output_bfd, (bfd_vma) 0,
3b63d2ce 2561 sec->contents);
886a2506
NC
2562 else
2563 bfd_put_32 (output_bfd,
2564 ds.sdyn->output_section->vma + ds.sdyn->output_offset,
3b63d2ce
CM
2565 sec->contents);
2566 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 4);
2567 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 8);
886a2506
NC
2568 }
2569 }
2570
886a2506
NC
2571 return TRUE;
2572}
2573
34e967a5
MC
2574#define ADD_DYNAMIC_SYMBOL(NAME, TAG) \
2575 h = elf_link_hash_lookup (elf_hash_table (info), \
2576 NAME, FALSE, FALSE, FALSE); \
2577 if ((h != NULL && (h->ref_regular || h->def_regular))) \
2578 if (! _bfd_elf_add_dynamic_entry (info, TAG, 0)) \
886a2506
NC
2579 return FALSE;
2580
2581/* Set the sizes of the dynamic sections. */
2582static bfd_boolean
34e967a5
MC
2583elf_arc_size_dynamic_sections (bfd * output_bfd,
2584 struct bfd_link_info *info)
886a2506 2585{
34e967a5 2586 bfd * dynobj;
886a2506 2587 asection * s;
34e967a5
MC
2588 bfd_boolean relocs_exist = FALSE;
2589 bfd_boolean reltext_exist = FALSE;
886a2506 2590 struct dynamic_sections ds = arc_create_dynamic_sections (output_bfd, info);
34e967a5 2591 struct elf_link_hash_table *htab = elf_hash_table (info);
886a2506
NC
2592
2593 dynobj = (elf_hash_table (info))->dynobj;
2594 BFD_ASSERT (dynobj != NULL);
2595
2596 if ((elf_hash_table (info))->dynamic_sections_created)
2597 {
2598 struct elf_link_hash_entry *h;
2599
34e967a5
MC
2600 /* Set the contents of the .interp section to the
2601 interpreter. */
886a2506
NC
2602 if (!bfd_link_pic (info))
2603 {
2604 s = bfd_get_section_by_name (dynobj, ".interp");
2605 BFD_ASSERT (s != NULL);
34e967a5 2606 s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
886a2506
NC
2607 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2608 }
2609
34e967a5
MC
2610 /* Add some entries to the .dynamic section. We fill in some of
2611 the values later, in elf_bfd_final_link, but we must add the
2612 entries now so that we know the final size of the .dynamic
2613 section. Checking if the .init section is present. We also
2614 create DT_INIT and DT_FINI entries if the init_str has been
2615 changed by the user. */
886a2506
NC
2616 ADD_DYNAMIC_SYMBOL ("init", DT_INIT);
2617 ADD_DYNAMIC_SYMBOL ("fini", DT_FINI);
2618 }
2619 else
2620 {
34e967a5
MC
2621 /* We may have created entries in the .rela.got section.
2622 However, if we are not creating the dynamic sections, we will
2623 not actually use these entries. Reset the size of .rela.got,
2624 which will cause it to get stripped from the output file
2625 below. */
2626 if (htab->srelgot != NULL)
2627 htab->srelgot->size = 0;
886a2506
NC
2628 }
2629
34e967a5
MC
2630 if (htab->splt != NULL && htab->splt->size == 0)
2631 htab->splt->flags |= SEC_EXCLUDE;
886a2506
NC
2632 for (s = dynobj->sections; s != NULL; s = s->next)
2633 {
34e967a5 2634 if ((s->flags & SEC_LINKER_CREATED) == 0)
886a2506
NC
2635 continue;
2636
34e967a5 2637 if (strncmp (s->name, ".rela", 5) == 0)
886a2506 2638 {
34e967a5
MC
2639 if (s->size == 0)
2640 {
2641 s->flags |= SEC_EXCLUDE;
2642 }
2643 else
2644 {
2645 if (strcmp (s->name, ".rela.plt") != 0)
2646 {
2647 const char *outname =
2648 bfd_get_section_name (output_bfd,
2649 htab->srelplt->output_section);
2650
2651 asection *target = bfd_get_section_by_name (output_bfd,
2652 outname + 4);
2653
2654 relocs_exist = TRUE;
2655 if (target != NULL && target->size != 0
2656 && (target->flags & SEC_READONLY) != 0
2657 && (target->flags & SEC_ALLOC) != 0)
2658 reltext_exist = TRUE;
2659 }
2660 }
886a2506 2661
34e967a5
MC
2662 /* We use the reloc_count field as a counter if we need to
2663 copy relocs into the output file. */
2664 s->reloc_count = 0;
886a2506 2665 }
34e967a5
MC
2666
2667 if (strcmp (s->name, ".dynamic") == 0)
2668 continue;
2669
2670 if (s->size != 0)
2671 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2672
2673 if (s->contents == NULL && s->size != 0)
2674 return FALSE;
886a2506
NC
2675 }
2676
2677 if (ds.sdyn)
2678 {
34e967a5
MC
2679 /* TODO: Check if this is needed. */
2680 if (!bfd_link_pic (info))
2681 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
2682 return FALSE;
2683
2684 if (htab->splt && (htab->splt->flags & SEC_EXCLUDE) == 0)
886a2506
NC
2685 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
2686 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2687 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2688 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0)
2689 )
2690 return FALSE;
2691
2692 if (relocs_exist == TRUE)
2693 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
2694 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
2ab2f40d 2695 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
886a2506
NC
2696 sizeof (Elf32_External_Rela))
2697 )
2698 return FALSE;
2699
2700 if (reltext_exist == TRUE)
2701 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
2702 return FALSE;
2703 }
2704
2705 return TRUE;
2706}
2707
0f7f3789
CM
2708
2709/* Classify dynamic relocs such that -z combreloc can reorder and combine
2710 them. */
2711static enum elf_reloc_type_class
2712elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2713 const asection *rel_sec ATTRIBUTE_UNUSED,
2714 const Elf_Internal_Rela *rela)
2715{
2716 switch ((int) ELF32_R_TYPE (rela->r_info))
2717 {
2718 case R_ARC_RELATIVE:
2719 return reloc_class_relative;
2720 case R_ARC_JMP_SLOT:
2721 return reloc_class_plt;
2722 case R_ARC_COPY:
2723 return reloc_class_copy;
2724 /* TODO: Needed in future to support ifunc. */
2725 /*
2726 case R_ARC_IRELATIVE:
2727 return reloc_class_ifunc;
2728 */
2729 default:
2730 return reloc_class_normal;
2731 }
2732}
2733
34e967a5
MC
2734const struct elf_size_info arc_elf32_size_info =
2735{
2736 sizeof (Elf32_External_Ehdr),
2737 sizeof (Elf32_External_Phdr),
2738 sizeof (Elf32_External_Shdr),
2739 sizeof (Elf32_External_Rel),
2740 sizeof (Elf32_External_Rela),
2741 sizeof (Elf32_External_Sym),
2742 sizeof (Elf32_External_Dyn),
2743 sizeof (Elf_External_Note),
2744 4,
2745 1,
2746 32, 2,
2747 ELFCLASS32, EV_CURRENT,
2748 bfd_elf32_write_out_phdrs,
2749 bfd_elf32_write_shdrs_and_ehdr,
2750 bfd_elf32_checksum_contents,
2751 bfd_elf32_write_relocs,
2752 bfd_elf32_swap_symbol_in,
2753 bfd_elf32_swap_symbol_out,
2754 bfd_elf32_slurp_reloc_table,
2755 bfd_elf32_slurp_symbol_table,
2756 bfd_elf32_swap_dyn_in,
2757 bfd_elf32_swap_dyn_out,
2758 bfd_elf32_swap_reloc_in,
2759 bfd_elf32_swap_reloc_out,
2760 bfd_elf32_swap_reloca_in,
2761 bfd_elf32_swap_reloca_out
2762};
2763
2764#define elf_backend_size_info arc_elf32_size_info
2765
2766static struct bfd_link_hash_table *
2767arc_elf_link_hash_table_create (bfd *abfd)
2768{
2769 struct elf_link_hash_table *htab;
2770
2771 htab = bfd_zmalloc (sizeof (*htab));
2772 if (htab == NULL)
2773 return NULL;
2774
2775 if (!_bfd_elf_link_hash_table_init (htab, abfd,
2776 _bfd_elf_link_hash_newfunc,
2777 sizeof (struct elf_link_hash_entry),
2778 GENERIC_ELF_DATA))
2779 {
2780 free (htab);
2781 return NULL;
2782 }
2783
2784 htab->init_got_refcount.refcount = 0;
2785 htab->init_got_refcount.glist = NULL;
2786 htab->init_got_offset.offset = 0;
2787 htab->init_got_offset.glist = NULL;
2788 return (struct bfd_link_hash_table *) htab;
2789}
2790
2791/* Hook called by the linker routine which adds symbols from an object
2792 file. */
2793
2794static bfd_boolean
2795elf_arc_add_symbol_hook (bfd * abfd,
2796 struct bfd_link_info * info,
2797 Elf_Internal_Sym * sym,
2798 const char ** namep ATTRIBUTE_UNUSED,
2799 flagword * flagsp ATTRIBUTE_UNUSED,
2800 asection ** secp ATTRIBUTE_UNUSED,
2801 bfd_vma * valp ATTRIBUTE_UNUSED)
2802{
a43942db 2803 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
34e967a5
MC
2804 && (abfd->flags & DYNAMIC) == 0
2805 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
a43942db 2806 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
34e967a5
MC
2807
2808 return TRUE;
2809}
886a2506 2810
6d00b590 2811#define TARGET_LITTLE_SYM arc_elf32_le_vec
47b0e7ad 2812#define TARGET_LITTLE_NAME "elf32-littlearc"
886a2506
NC
2813#define TARGET_BIG_SYM arc_elf32_be_vec
2814#define TARGET_BIG_NAME "elf32-bigarc"
2815#define ELF_ARCH bfd_arch_arc
2816#define ELF_MACHINE_CODE EM_ARC_COMPACT
2817#define ELF_MACHINE_ALT1 EM_ARC_COMPACT2
2818#define ELF_MAXPAGESIZE 0x2000
2819
34e967a5
MC
2820#define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create
2821
2822#define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data
2823#define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup
2824#define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags
2825#define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data
2826#define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data
2827
886a2506
NC
2828#define elf_info_to_howto_rel arc_info_to_howto_rel
2829#define elf_backend_object_p arc_elf_object_p
2830#define elf_backend_final_write_processing arc_elf_final_write_processing
2831
2832#define elf_backend_relocate_section elf_arc_relocate_section
2833#define elf_backend_check_relocs elf_arc_check_relocs
2834#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2835
0f7f3789
CM
2836#define elf_backend_reloc_type_class elf32_arc_reloc_type_class
2837
886a2506
NC
2838#define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol
2839#define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol
2840
2841#define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections
2842#define elf_backend_size_dynamic_sections elf_arc_size_dynamic_sections
34e967a5 2843#define elf_backend_add_symbol_hook elf_arc_add_symbol_hook
886a2506
NC
2844
2845#define elf_backend_can_gc_sections 1
2846#define elf_backend_want_got_plt 1
2847#define elf_backend_plt_readonly 1
34e967a5 2848#define elf_backend_rela_plts_and_copies_p 1
886a2506
NC
2849#define elf_backend_want_plt_sym 0
2850#define elf_backend_got_header_size 12
2851
2852#define elf_backend_may_use_rel_p 0
2853#define elf_backend_may_use_rela_p 1
2854#define elf_backend_default_use_rela_p 1
252b5132 2855
3c8adaca
CZ
2856#define elf_backend_default_execstack 0
2857
252b5132 2858#include "elf32-target.h"
This page took 0.986796 seconds and 4 git commands to generate.