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