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