[ARC] Parse NOTE section in core dump files
[deliverable/binutils-gdb.git] / bfd / elf32-arc.c
1 /* ARC-specific support for 32-bit ELF
2 Copyright (C) 1994-2016 Free Software Foundation, Inc.
3 Contributed by Cupertino Miranda (cmiranda@synopsys.com).
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/arc.h"
27 #include "libiberty.h"
28 #include "opcode/arc-func.h"
29 #include "opcode/arc.h"
30 #include "arc-plt.h"
31
32 /* #define ARC_ENABLE_DEBUG 1 */
33 #ifdef ARC_ENABLE_DEBUG
34 static const char *
35 name_for_global_symbol (struct elf_link_hash_entry *h)
36 {
37 static char *local_str = "(local)";
38 if (h == NULL)
39 return local_str;
40 return h->root.root.string;
41 }
42 #define ARC_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
43 #else
44 #define ARC_DEBUG(...)
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; \
52 bfd_byte * _loc; \
53 \
54 BFD_ASSERT (_htab->srel##SECTION &&_htab->srel##SECTION->contents); \
55 _loc = _htab->srel##SECTION->contents \
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; \
62 BFD_ASSERT ((long) SYM_IDX != -1); \
63 _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE); \
64 bfd_elf32_swap_reloca_out (BFD, &_rel, _loc); \
65 }
66
67 struct dynamic_sections
68 {
69 bfd_boolean initialized;
70 asection * sgot;
71 asection * srelgot;
72 asection * sgotplt;
73 asection * srelgotplt;
74 asection * sdyn;
75 asection * splt;
76 asection * srelplt;
77 };
78
79 enum dyn_section_types
80 {
81 got = 0,
82 relgot,
83 gotplt,
84 dyn,
85 plt,
86 relplt,
87 DYN_SECTION_TYPES_END
88 };
89
90 const 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
100
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
107 char * init_str = INIT_SYM_STRING;
108 char * fini_str = FINI_SYM_STRING;
109
110 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
111 case VALUE: \
112 return "R_" #TYPE; \
113 break;
114
115 static ATTRIBUTE_UNUSED const char *
116 reloc_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
128
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). */
131
132 #define USE_REL 1
133
134 static ATTRIBUTE_UNUSED bfd_boolean
135 is_reloc_PC_relative (reloc_howto_type *howto)
136 {
137 return (strstr (howto->name, "PC") != NULL) ? TRUE : FALSE;
138 }
139
140 static bfd_boolean
141 is_reloc_SDA_relative (reloc_howto_type *howto)
142 {
143 return (strstr (howto->name, "SDA") != NULL) ? TRUE : FALSE;
144 }
145
146 static bfd_boolean
147 is_reloc_for_GOT (reloc_howto_type * howto)
148 {
149 if (strstr (howto->name, "TLS") != NULL)
150 return FALSE;
151 return (strstr (howto->name, "GOT") != NULL) ? TRUE : FALSE;
152 }
153
154 static bfd_boolean
155 is_reloc_for_PLT (reloc_howto_type * howto)
156 {
157 return (strstr (howto->name, "PLT") != NULL) ? TRUE : FALSE;
158 }
159
160 static bfd_boolean
161 is_reloc_for_TLS (reloc_howto_type *howto)
162 {
163 return (strstr (howto->name, "TLS") != NULL) ? TRUE : FALSE;
164 }
165
166 struct 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
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)
194 #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
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)
197 #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
198
199
200 static bfd_reloc_status_type
201 arc_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,
228 enum 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) \
236 [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, FALSE, 0, \
237 complain_overflow_##OVERFLOW, arc_elf_reloc, \
238 "R_" #TYPE, FALSE, 0, 0, FALSE),
239
240 static struct reloc_howto_struct elf_arc_howto_table[] =
241 {
242 #include "elf/arc-reloc.def"
243 /* Example of what is generated by the preprocessor. Currently kept as an
244 example.
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
262 static void arc_elf_howto_init (void)
263 {
264 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
265 elf_arc_howto_table[TYPE].pc_relative = \
266 (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
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 }
273
274 #include "elf/arc-reloc.def"
275
276 }
277 #undef ARC_RELOC_HOWTO
278
279
280 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
281 [TYPE] = VALUE,
282 const int howto_table_lookup[] =
283 {
284 #include "elf/arc-reloc.def"
285 };
286 #undef ARC_RELOC_HOWTO
287
288 static reloc_howto_type *
289 arc_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 }
295
296 /* Map BFD reloc types to ARC ELF reloc types. */
297
298 struct arc_reloc_map
299 {
300 bfd_reloc_code_real_type bfd_reloc_val;
301 unsigned char elf_reloc_val;
302 };
303
304 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
305 { BFD_RELOC_##TYPE, R_##TYPE },
306 static const struct arc_reloc_map arc_reloc_map[] =
307 {
308 #include "elf/arc-reloc.def"
309
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},
315 };
316 #undef ARC_RELOC_HOWTO
317
318 typedef 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;
324 static replace_func
325 get_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
341 static reloc_howto_type *
342 arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
343 bfd_reloc_code_real_type code)
344 {
345 unsigned int i;
346
347 for (i = ARRAY_SIZE (arc_reloc_map); i--;)
348 {
349 if (arc_reloc_map[i].bfd_reloc_val == code)
350 return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
351 }
352
353 return NULL;
354 }
355
356 /* Function to set the ELF flag bits. */
357 static bfd_boolean
358 arc_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. */
366 static bfd_boolean
367 arc_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 {
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
408 static bfd_boolean
409 arc_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
427 static reloc_howto_type *
428 bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
429 const char *r_name)
430 {
431 unsigned int i;
432
433 for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
434 if (elf_arc_howto_table[i].name != NULL
435 && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
436 return arc_elf_howto (i);
437
438 return NULL;
439 }
440
441 /* Set the howto pointer for an ARC ELF reloc. */
442
443 static void
444 arc_info_to_howto_rel (bfd * abfd ATTRIBUTE_UNUSED,
445 arelent * cache_ptr,
446 Elf_Internal_Rela * dst)
447 {
448 unsigned int r_type;
449
450 r_type = ELF32_R_TYPE (dst->r_info);
451 BFD_ASSERT (r_type < (unsigned int) R_ARC_max);
452 cache_ptr->howto = arc_elf_howto (r_type);
453 }
454
455 /* Merge backend specific data from an object file to the output
456 object file when linking. */
457
458 static bfd_boolean
459 arc_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
553 /* Set the right machine number for an ARC ELF file. */
554 static bfd_boolean
555 arc_elf_object_p (bfd * abfd)
556 {
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;
563
564 if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
565 {
566 switch (arch)
567 {
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:
582 mach = (e_machine == EM_ARC_COMPACT)
583 ? bfd_mach_arc_arc700 : bfd_mach_arc_arcv2;
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"));
600 }
601 }
602
603 return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
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
609 static void
610 arc_elf_final_write_processing (bfd * abfd,
611 bfd_boolean linker ATTRIBUTE_UNUSED)
612 {
613 unsigned long emf;
614
615 switch (bfd_get_mach (abfd))
616 {
617 case bfd_mach_arc_arc600:
618 emf = EM_ARC_COMPACT;
619 break;
620 case bfd_mach_arc_arc601:
621 emf = EM_ARC_COMPACT;
622 break;
623 case bfd_mach_arc_arc700:
624 emf = EM_ARC_COMPACT;
625 break;
626 case bfd_mach_arc_arcv2:
627 emf = EM_ARC_COMPACT2;
628 break;
629 default:
630 goto DO_NOTHING;
631 }
632
633 elf_elfheader (abfd)->e_machine = emf;
634
635 /* Record whatever is the current syscall ABI version. */
636 elf_elfheader (abfd)->e_flags |= E_ARC_OSABI_CURRENT;
637
638 DO_NOTHING:
639 return;
640 }
641
642 #ifdef ARC_ENABLE_DEBUG
643 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
644
645 static void
646 debug_arc_reloc (struct arc_relocation_data reloc_data)
647 {
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);
657 if (reloc_data.sym_section != NULL)
658 {
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);
663 if (reloc_data.sym_section->output_section != NULL)
664 ARC_DEBUG (", output_section->vma = 0x%08x",
665 ((unsigned int) reloc_data.sym_section->output_section->vma));
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);
669 }
670 else
671 {
672 ARC_DEBUG (" symbol section is NULL\n");
673 }
674
675 ARC_DEBUG (" Input_section:\n");
676 if (reloc_data.input_section != NULL)
677 {
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);
687 }
688 else
689 {
690 ARC_DEBUG (" input section is NULL\n");
691 }
692 }
693 #else
694 #define DEBUG_ARC_RELOC(A)
695 #endif /* ARC_ENABLE_DEBUG */
696
697 static bfd_vma
698 middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
699 {
700 if (do_it)
701 {
702 insn
703 = ((insn & 0xffff0000) >> 16)
704 | ((insn & 0xffff) << 16);
705 }
706 return insn;
707 }
708
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
713 static inline bfd_reloc_status_type
714 arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
715 bfd_signed_vma relocation,
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)
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 }
746 break;
747
748 default:
749 break;
750 }
751
752 return bfd_reloc_ok;
753 }
754
755 #define ME(reloc) (reloc)
756
757 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
758 && (!bfd_big_endian (BFD)))
759
760 #define S ((bfd_signed_vma) (reloc_data.sym_value \
761 + (reloc_data.sym_section->output_section != NULL ? \
762 (reloc_data.sym_section->output_offset \
763 + reloc_data.sym_section->output_section->vma) : 0)))
764 #define L ((bfd_signed_vma) (reloc_data.sym_value \
765 + (reloc_data.sym_section->output_section != NULL ? \
766 (reloc_data.sym_section->output_offset \
767 + reloc_data.sym_section->output_section->vma) : 0)))
768 #define A (reloc_data.reloc_addend)
769 #define B (0)
770 #define G (reloc_data.got_offset_value)
771 #define GOT (reloc_data.got_symbol_vma)
772 #define GOT_BEGIN (htab->sgot->output_section->vma)
773
774 #define MES (0)
775 /* P: relative offset to PCL The offset should be to the
776 current location aligned to 32 bits. */
777 #define P ((bfd_signed_vma) ( \
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 \
782 + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \
783 & ~0x3))
784 #define PDATA ((bfd_signed_vma) ( \
785 (reloc_data.input_section->output_section->vma \
786 + reloc_data.input_section->output_offset \
787 + (reloc_data.reloc_offset))))
788 #define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
789 + reloc_data.sym_section->output_offset)
790
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)
794 #define TLS_TBSS (8)
795 #define TCB_SIZE (8)
796
797 #define none (0)
798
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_); \
826 ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
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)
841
842 #else
843
844 #define PRINT_DEBUG_RELOC_INFO_BEFORE(...)
845 #define PRINT_DEBUG_RELOC_INFO_AFTER
846
847 #endif /* ARC_ENABLE_DEBUG */
848
849 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
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 } \
860 break;
861
862 static bfd_reloc_status_type
863 arc_do_relocation (bfd_byte * contents,
864 struct arc_relocation_data reloc_data,
865 struct bfd_link_info *info)
866 {
867 bfd_signed_vma relocation = 0;
868 bfd_vma insn;
869 bfd_vma orig_insn ATTRIBUTE_UNUSED;
870 bfd * abfd = reloc_data.input_section->owner;
871 struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
872 bfd_reloc_status_type flag;
873
874 if (reloc_data.should_relocate == FALSE)
875 return bfd_reloc_ok;
876
877 switch (reloc_data.howto->size)
878 {
879 case 2:
880 insn = arc_bfd_get_32 (abfd,
881 contents + reloc_data.reloc_offset,
882 reloc_data.input_section);
883 break;
884 case 1:
885 insn = arc_bfd_get_16 (abfd,
886 contents + reloc_data.reloc_offset,
887 reloc_data.input_section);
888 break;
889 case 0:
890 insn = arc_bfd_get_8 (abfd,
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 {
904 #include "elf/arc-reloc.def"
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)
913 flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
914 reloc_data.howto->bitsize,
915 reloc_data.howto->rightshift,
916 bfd_arch_bits_per_address (abfd),
917 relocation);
918 else
919 flag = arc_special_overflow_checks (reloc_data, relocation, info);
920
921 if (flag != bfd_reloc_ok)
922 {
923 ARC_DEBUG ("Relocation overflows !\n");
924 DEBUG_ARC_RELOC (reloc_data);
925 ARC_DEBUG ("Relocation value = signed -> %d, unsigned -> %u"
926 ", hex -> (0x%08x)\n",
927 (int) relocation, (unsigned) relocation, (int) relocation);
928
929 return flag;
930 }
931
932 /* Write updated instruction back to memory. */
933 switch (reloc_data.howto->size)
934 {
935 case 2:
936 arc_bfd_put_32 (abfd, insn,
937 contents + reloc_data.reloc_offset,
938 reloc_data.input_section);
939 break;
940 case 1:
941 arc_bfd_put_16 (abfd, insn,
942 contents + reloc_data.reloc_offset,
943 reloc_data.input_section);
944 break;
945 case 0:
946 arc_bfd_put_8 (abfd, insn,
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
973
974 /* Relocate an arc ELF section.
975 Function : elf_arc_relocate_section
976 Brief : Relocate an arc section, by handling all the relocations
977 appearing in that section.
978 Args : output_bfd : The bfd being written to.
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. */
988 static bfd_boolean
989 elf_arc_relocate_section (bfd * output_bfd,
990 struct bfd_link_info * info,
991 bfd * input_bfd,
992 asection * input_section,
993 bfd_byte * contents,
994 Elf_Internal_Rela * relocs,
995 Elf_Internal_Sym * local_syms,
996 asection ** local_sections)
997 {
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);
1004
1005 symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
1006 sym_hashes = elf_sym_hashes (input_bfd);
1007
1008 rel = wrel = relocs;
1009 relend = relocs + input_section->reloc_count;
1010 for (; rel < relend; wrel++, rel++)
1011 {
1012 enum elf_arc_reloc_type r_type;
1013 reloc_howto_type * howto;
1014 unsigned long r_symndx;
1015 struct elf_link_hash_entry * h;
1016 Elf_Internal_Sym * sym;
1017 asection * sec;
1018 struct elf_link_hash_entry * h2;
1019 const char * msg;
1020
1021 struct arc_relocation_data reloc_data =
1022 {
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
1034 };
1035
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 }
1043 howto = arc_elf_howto (r_type);
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
1065 /* For RELA relocs. Just adjust the addend
1066 value in the relocation entry. */
1067 rel->r_addend += sec->output_offset + sym->st_value;
1068
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__);
1072 }
1073 }
1074 }
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
1080 && h2 != NULL && h2->root.type != bfd_link_hash_undefined
1081 && h2->root.u.def.section->output_section != NULL)
1082 /* TODO: Verify this condition. */
1083 {
1084 reloc_data.sdata_begin_symbol_vma =
1085 (h2->root.u.def.value
1086 + h2->root.u.def.section->output_section->vma);
1087 reloc_data.sdata_begin_symbol_vma_set = TRUE;
1088 }
1089
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
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];
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 {
1156 reloc_data.sym_value = sym->st_value;
1157 reloc_data.sym_section = sec;
1158 reloc_data.symbol_name =
1159 bfd_elf_string_from_elf_section (input_bfd,
1160 symtab_hdr->sh_link,
1161 sym->st_name);
1162
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
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;
1183
1184 reloc_data.should_relocate = TRUE;
1185 }
1186 else /* Global symbol. */
1187 {
1188 /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro
1189 (defined in elf-bfd.h) here. */
1190
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
1198 /* TODO: Need to validate what was the intention. */
1199 /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
1200 reloc_data.symbol_name = h->root.root.string;
1201
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
1211 if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
1212 {
1213 /* TODO: Change it to use arc_do_relocation with
1214 ARC_32 reloc. Try to use ADD_RELA macro. */
1215 bfd_vma relocation =
1216 reloc_data.sym_value + reloc_data.reloc_addend
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;
1232 }
1233 }
1234 else if (h->root.type == bfd_link_hash_undefweak)
1235 {
1236 /* Is weak symbol and has no definition. */
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;
1253 }
1254 else
1255 {
1256 if (is_reloc_for_GOT (howto))
1257 {
1258 reloc_data.sym_value = h->root.u.def.value;
1259 reloc_data.sym_section = htab->sgot;
1260
1261 reloc_data.should_relocate = TRUE;
1262 }
1263 else if (is_reloc_for_PLT (howto))
1264 {
1265 /* Fail if it is linking for PIE and the symbol is
1266 undefined. */
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);
1271 reloc_data.sym_value = h->plt.offset;
1272 reloc_data.sym_section = htab->splt;
1273
1274 reloc_data.should_relocate = TRUE;
1275 }
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);
1280 }
1281
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;
1286 }
1287
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 }
1313
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:
1320 if ((bfd_link_pic (info))// || bfd_link_pie (info))
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
1347 #define IS_ARC_PCREL_TYPE(TYPE) \
1348 ( (TYPE == R_ARC_PC32) \
1349 || (TYPE == R_ARC_32_PCREL))
1350
1351 if (skip)
1352 {
1353 memset (&outrel, 0, sizeof outrel);
1354 relocate = FALSE;
1355 }
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))
1362 {
1363 BFD_ASSERT (h != NULL);
1364 if ((input_section->flags & SEC_ALLOC) != 0)
1365 relocate = FALSE;
1366 else
1367 relocate = TRUE;
1368
1369 BFD_ASSERT (h->dynindx != -1);
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. */
1384 relocate = TRUE;
1385 /* outrel.r_addend = 0; */
1386 outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
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))
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);
1415
1416 /* Make sure we have with a dynamic linker. In case of GOT and PLT
1417 the sym_section should point to .got or .plt respectively. */
1418 if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
1419 && reloc_data.sym_section == NULL)
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
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;
1468 }
1469
1470 return TRUE;
1471 }
1472
1473 static struct dynamic_sections
1474 arc_create_dynamic_sections (bfd * abfd, struct bfd_link_info *info)
1475 {
1476 struct elf_link_hash_table *htab;
1477 bfd *dynobj;
1478 struct dynamic_sections ds =
1479 {
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);
1499 }
1500
1501 dynobj = (elf_hash_table (info))->dynobj;
1502
1503 if (dynobj)
1504 {
1505 ds.sgot = htab->sgot;
1506 ds.srelgot = htab->srelgot;
1507
1508 ds.sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1509 ds.srelgotplt = ds.srelplt;
1510
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 }
1519
1520 ds.initialized = TRUE;
1521
1522 return ds;
1523 }
1524
1525 static bfd_boolean
1526 elf_arc_check_relocs (bfd * abfd,
1527 struct bfd_link_info * info,
1528 asection * sec,
1529 const Elf_Internal_Rela * relocs)
1530 {
1531 Elf_Internal_Shdr * symtab_hdr;
1532 struct elf_link_hash_entry ** sym_hashes;
1533 const Elf_Internal_Rela * rel;
1534 const Elf_Internal_Rela * rel_end;
1535 bfd * dynobj;
1536 asection * sreloc = NULL;
1537
1538 if (bfd_link_relocatable (info))
1539 return TRUE;
1540
1541 dynobj = (elf_hash_table (info))->dynobj;
1542 symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
1543 sym_hashes = elf_sym_hashes (abfd);
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 }
1560 howto = arc_elf_howto (r_type);
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 }
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
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. */
1587 if ((bfd_link_dll (info) && !bfd_link_pie (info))
1588 && (sec->flags & SEC_ALLOC) != 0
1589 && (sec->flags & SEC_READONLY) != 0
1590 && ((sec->flags & SEC_CODE) != 0
1591 || (sec->flags & SEC_DEBUGGING) != 0))
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:
1618 if ((bfd_link_pic (info))// || bfd_link_pie (info))
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
1641 if (is_reloc_for_PLT (howto) == TRUE)
1642 {
1643 if (h == NULL)
1644 continue;
1645 else
1646 h->needs_plt = 1;
1647 }
1648
1649 /* Add info to the symbol got_entry_list. */
1650 if (is_reloc_for_GOT (howto) == TRUE
1651 || is_reloc_for_TLS (howto) == TRUE)
1652 {
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);
1658 }
1659 }
1660
1661 return TRUE;
1662 }
1663
1664 #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
1665
1666 static struct plt_version_t *
1667 arc_get_plt_version (struct bfd_link_info *info)
1668 {
1669 int i;
1670
1671 for (i = 0; i < 1; i++)
1672 {
1673 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
1674 (int) plt_versions[i].entry_size,
1675 (int) plt_versions[i].elem_size);
1676 }
1677
1678 if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
1679 {
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
1686 {
1687 if (bfd_link_pic (info))
1688 return &(plt_versions[ELF_ARC_PIC]);
1689 else
1690 return &(plt_versions[ELF_ARC_ABS]);
1691 }
1692 }
1693
1694 static bfd_vma
1695 add_symbol_to_plt (struct bfd_link_info *info)
1696 {
1697 struct elf_link_hash_table *htab = elf_hash_table (info);
1698 bfd_vma ret;
1699
1700 struct plt_version_t *plt_data = arc_get_plt_version (info);
1701
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;
1706
1707 ret = htab->splt->size;
1708
1709 htab->splt->size += plt_data->elem_size;
1710 ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab->splt->size);
1711
1712 htab->sgotplt->size += 4;
1713 htab->srelplt->size += sizeof (Elf32_External_Rela);
1714
1715 return ret;
1716 }
1717
1718 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
1719 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
1720
1721 static void
1722 plt_do_relocs_for_symbol (bfd *abfd,
1723 struct elf_link_hash_table *htab,
1724 const struct plt_reloc *reloc,
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:
1735 relocation
1736 = htab->sgotplt->output_section->vma
1737 + htab->sgotplt->output_offset + symbol_got_offset;
1738 break;
1739 }
1740 relocation += reloc->addend;
1741
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;
1747
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. */
1755 if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
1756 {
1757 relocation
1758 = ((relocation & 0xffff0000) >> 16)
1759 | ((relocation & 0xffff) << 16);
1760 }
1761
1762 switch (reloc->size)
1763 {
1764 case 32:
1765 bfd_put_32 (htab->splt->output_section->owner,
1766 relocation,
1767 htab->splt->contents + plt_offset + reloc->offset);
1768 break;
1769 }
1770
1771 reloc = &(reloc[1]); /* Jump to next relocation. */
1772 }
1773 }
1774
1775 static void
1776 relocate_plt_for_symbol (bfd *output_bfd,
1777 struct bfd_link_info *info,
1778 struct elf_link_hash_entry *h)
1779 {
1780 struct plt_version_t *plt_data = arc_get_plt_version (info);
1781 struct elf_link_hash_table *htab = elf_hash_table (info);
1782
1783 bfd_vma plt_index = (h->plt.offset - plt_data->entry_size)
1784 / plt_data->elem_size;
1785 bfd_vma got_offset = (plt_index + 3) * 4;
1786
1787 ARC_DEBUG ("arc_info: PLT_OFFSET = %#lx, PLT_ENTRY_VMA = %#lx, \
1788 GOT_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),
1797 h->root.root.string);
1798
1799 {
1800 bfd_vma i = 0;
1801 uint16_t *ptr = (uint16_t *) plt_data->elem;
1802
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
1812 plt_do_relocs_for_symbol (output_bfd, htab,
1813 plt_data->elem_relocs,
1814 h->plt.offset,
1815 got_offset);
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;
1832
1833 BFD_ASSERT (h->dynindx != -1);
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 }
1840 }
1841
1842 static void
1843 relocate_plt_for_entry (bfd *abfd,
1844 struct bfd_link_info *info)
1845 {
1846 struct plt_version_t *plt_data = arc_get_plt_version (info);
1847 struct elf_link_hash_table *htab = elf_hash_table (info);
1848
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 }
1860 PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
1861 }
1862
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
1867 understand. */
1868
1869 static bfd_boolean
1870 elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
1871 struct elf_link_hash_entry *h)
1872 {
1873 asection *s;
1874 bfd *dynobj = (elf_hash_table (info))->dynobj;
1875 struct elf_link_hash_table *htab = elf_hash_table (info);
1876
1877 if (h->type == STT_FUNC
1878 || h->type == STT_GNU_IFUNC
1879 || h->needs_plt == 1)
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
1897 if (bfd_link_pic (info)
1898 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1899 {
1900 bfd_vma loc = add_symbol_to_plt (info);
1901
1902 if (bfd_link_executable (info) && !h->def_regular)
1903 {
1904 h->root.u.def.section = htab->splt;
1905 h->root.u.def.value = loc;
1906 }
1907 h->plt.offset = loc;
1908 }
1909 else
1910 {
1911 h->plt.offset = (bfd_vma) -1;
1912 h->needs_plt = 0;
1913 }
1914 return TRUE;
1915 }
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)
1921 {
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;
1927 }
1928
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. */
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)
1942 return TRUE;
1943
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
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
1961 if (htab == NULL)
1962 return FALSE;
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
1978 s = bfd_get_section_by_name (dynobj, ".dynbss");
1979 BFD_ASSERT (s != NULL);
1980
1981 return _bfd_elf_adjust_dynamic_copy (info, h, s);
1982 }
1983
1984 /* Function : elf_arc_finish_dynamic_symbol
1985 Brief : Finish up dynamic symbol handling. We set the
1986 contents of various dynamic sections here.
1987 Args : output_bfd :
1988 info :
1989 h :
1990 sym :
1991 Returns : True/False as the return status. */
1992
1993 static bfd_boolean
1994 elf_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 {
1999 if (h->plt.offset != (bfd_vma) -1)
2000 {
2001 relocate_plt_for_symbol (output_bfd, info, h);
2002
2003 if (!h->def_regular)
2004 {
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;
2008 }
2009 }
2010
2011
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);
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
2027 asection *srelbss
2028 = bfd_get_section_by_name (h->root.u.def.section->owner,
2029 ".rela.bss");
2030
2031 bfd_byte * loc = srelbss->contents
2032 + (srelbss->reloc_count * sizeof (Elf32_External_Rela));
2033 srelbss->reloc_count++;
2034
2035 Elf_Internal_Rela rel;
2036 rel.r_addend = 0;
2037 rel.r_offset = rel_offset;
2038
2039 BFD_ASSERT (h->dynindx != -1);
2040 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY);
2041
2042 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
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
2054 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \
2055 case TAG: \
2056 if (SYMBOL != NULL) \
2057 h = elf_link_hash_lookup (elf_hash_table (info), \
2058 SYMBOL, FALSE, FALSE, TRUE); \
2059 else if (SECTION != NULL) \
2060 s = bfd_get_linker_section (dynobj, SECTION); \
2061 break;
2062
2063 /* Function : elf_arc_finish_dynamic_sections
2064 Brief : Finish up the dynamic sections handling.
2065 Args : output_bfd :
2066 info :
2067 h :
2068 sym :
2069 Returns : True/False as the return status. */
2070
2071 static bfd_boolean
2072 elf_arc_finish_dynamic_sections (bfd * output_bfd,
2073 struct bfd_link_info *info)
2074 {
2075 struct dynamic_sections ds = arc_create_dynamic_sections (output_bfd, info);
2076 struct elf_link_hash_table *htab = elf_hash_table (info);
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;
2084 dynconend
2085 = (Elf32_External_Dyn *) (ds.sdyn->contents + ds.sdyn->size);
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 {
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")
2107 default:
2108 break;
2109 }
2110
2111 /* In case the dynamic symbols should be updated with a symbol. */
2112 if (h != NULL
2113 && (h->root.type == bfd_link_hash_defined
2114 || h->root.type == bfd_link_hash_defweak))
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 +=
2123 (asec_ptr->output_section->vma
2124 + asec_ptr->output_offset);
2125 }
2126 else
2127 {
2128 /* The symbol is imported from another shared
2129 library and does not apply to this one. */
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:
2140 case DT_VERSYM:
2141 case DT_VERDEF:
2142 case DT_VERNEED:
2143 internal_dyn.d_un.d_ptr = (s->output_section->vma
2144 + s->output_offset);
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:
2154 if (s != NULL)
2155 internal_dyn.d_un.d_val -= s->size;
2156 do_it = TRUE;
2157 break;
2158
2159 default:
2160 break;
2161 }
2162 }
2163
2164 if (do_it)
2165 bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon);
2166 }
2167
2168 if (htab->splt->size > 0)
2169 {
2170 relocate_plt_for_entry (output_bfd, info);
2171 }
2172
2173 /* TODO: Validate this. */
2174 elf_section_data (htab->srelplt->output_section)->this_hdr.sh_entsize
2175 = 0xc;
2176 }
2177
2178 /* Fill in the first three entries in the global offset table. */
2179 if (htab->sgot)
2180 {
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)
2187 {
2188 asection *sec = h->root.u.def.section;
2189
2190 if (ds.sdyn == NULL)
2191 bfd_put_32 (output_bfd, (bfd_vma) 0,
2192 sec->contents);
2193 else
2194 bfd_put_32 (output_bfd,
2195 ds.sdyn->output_section->vma + ds.sdyn->output_offset,
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);
2199 }
2200 }
2201
2202 return TRUE;
2203 }
2204
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)) \
2210 return FALSE;
2211
2212 /* Set the sizes of the dynamic sections. */
2213 static bfd_boolean
2214 elf_arc_size_dynamic_sections (bfd * output_bfd,
2215 struct bfd_link_info *info)
2216 {
2217 bfd * dynobj;
2218 asection * s;
2219 bfd_boolean relocs_exist = FALSE;
2220 bfd_boolean reltext_exist = FALSE;
2221 struct dynamic_sections ds = arc_create_dynamic_sections (output_bfd, info);
2222 struct elf_link_hash_table *htab = elf_hash_table (info);
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
2231 /* Set the contents of the .interp section to the
2232 interpreter. */
2233 if (!bfd_link_pic (info))
2234 {
2235 s = bfd_get_section_by_name (dynobj, ".interp");
2236 BFD_ASSERT (s != NULL);
2237 s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
2238 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2239 }
2240
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. */
2247 ADD_DYNAMIC_SYMBOL ("init", DT_INIT);
2248 ADD_DYNAMIC_SYMBOL ("fini", DT_FINI);
2249 }
2250 else
2251 {
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;
2259 }
2260
2261 if (htab->splt != NULL && htab->splt->size == 0)
2262 htab->splt->flags |= SEC_EXCLUDE;
2263 for (s = dynobj->sections; s != NULL; s = s->next)
2264 {
2265 if ((s->flags & SEC_LINKER_CREATED) == 0)
2266 continue;
2267
2268 if (strncmp (s->name, ".rela", 5) == 0)
2269 {
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 }
2292
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;
2296 }
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;
2306 }
2307
2308 if (ds.sdyn)
2309 {
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)
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)
2326 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
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
2339
2340 /* Classify dynamic relocs such that -z combreloc can reorder and combine
2341 them. */
2342 static enum elf_reloc_type_class
2343 elf32_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
2365 const 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
2397 static struct bfd_link_hash_table *
2398 arc_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
2425 static bfd_boolean
2426 elf_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 {
2434 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
2435 && (abfd->flags & DYNAMIC) == 0
2436 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
2437 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
2438
2439 return TRUE;
2440 }
2441
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
2449 static bfd_boolean
2450 elf32_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
2475 #define TARGET_LITTLE_SYM arc_elf32_le_vec
2476 #define TARGET_LITTLE_NAME "elf32-littlearc"
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
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
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
2500 #define elf_backend_reloc_type_class elf32_arc_reloc_type_class
2501
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
2507 #define elf_backend_add_symbol_hook elf_arc_add_symbol_hook
2508
2509 #define elf_backend_can_gc_sections 1
2510 #define elf_backend_want_got_plt 1
2511 #define elf_backend_plt_readonly 1
2512 #define elf_backend_rela_plts_and_copies_p 1
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
2519
2520 #define elf_backend_grok_prstatus elf32_arc_grok_prstatus
2521
2522 #define elf_backend_default_execstack 0
2523
2524 #include "elf32-target.h"
This page took 0.136765 seconds and 5 git commands to generate.