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).
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "libiberty.h"
28 #include "opcode/arc-func.h"
32 # define PR_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
34 # define PR_DEBUG(fmt, args...)
37 /* #define ARC_ENABLE_DEBUG 1 */
38 #ifndef ARC_ENABLE_DEBUG
39 #define ARC_DEBUG(...)
42 name_for_global_symbol (struct elf_link_hash_entry
*h
)
44 static char *local_str
= "(local)";
48 return h
->root
.root
.string
;
50 #define ARC_DEBUG(args...) fprintf (stderr, ##args)
54 #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND) \
56 struct elf_link_hash_table *_htab = elf_hash_table (info); \
57 Elf_Internal_Rela _rel; \
60 _loc = _htab->srel##SECTION->contents \
61 + ((_htab->srel##SECTION->reloc_count) \
62 * sizeof (Elf32_External_Rela)); \
63 _htab->srel##SECTION->reloc_count++; \
64 _rel.r_addend = ADDEND; \
65 _rel.r_offset = (_htab->s##SECTION)->output_section->vma \
66 + (_htab->s##SECTION)->output_offset + OFFSET; \
67 BFD_ASSERT ((long) SYM_IDX != -1); \
68 _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE); \
69 bfd_elf32_swap_reloca_out (BFD, &_rel, _loc); \
74 bfd_vma sdata_begin_symbol_vma
;
75 asection
* sdata_output_section
;
76 bfd_vma got_symbol_vma
;
79 struct arc_local_data global_arc_data
=
81 .sdata_begin_symbol_vma
= 0,
82 .sdata_output_section
= NULL
,
86 struct dynamic_sections
88 bfd_boolean initialized
;
92 asection
* srelgotplt
;
98 enum dyn_section_types
106 DYN_SECTION_TYPES_END
109 const char * dyn_section_names
[DYN_SECTION_TYPES_END
] =
138 struct got_entry
*next
;
139 enum tls_type_e type
;
141 bfd_boolean processed
;
142 bfd_boolean created_dyn_relocation
;
143 enum tls_got_entries existing_entries
;
147 new_got_entry_to_list (struct got_entry
**list
,
148 enum tls_type_e type
,
150 enum tls_got_entries existing_entries
)
152 /* Find list end. Avoid having multiple entries of the same
154 struct got_entry
**p
= list
;
157 if ((*p
)->type
== type
)
162 struct got_entry
*entry
=
163 (struct got_entry
*) malloc (sizeof(struct got_entry
));
166 entry
->offset
= offset
;
168 entry
->processed
= FALSE
;
169 entry
->created_dyn_relocation
= FALSE
;
170 entry
->existing_entries
= existing_entries
;
172 /* Add the entry to the end of the list. */
177 symbol_has_entry_of_type (struct got_entry
*list
, enum tls_type_e type
)
181 if (list
->type
== type
)
189 /* The default symbols representing the init and fini dyn values.
190 TODO: Check what is the relation of those strings with arclinux.em
192 #define INIT_SYM_STRING "_init"
193 #define FINI_SYM_STRING "_fini"
195 char * init_str
= INIT_SYM_STRING
;
196 char * fini_str
= FINI_SYM_STRING
;
198 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
203 static ATTRIBUTE_UNUSED
const char *
204 reloc_type_to_name (unsigned int type
)
208 #include "elf/arc-reloc.def"
215 #undef ARC_RELOC_HOWTO
217 /* Try to minimize the amount of space occupied by relocation tables
218 on the ROM (not that the ROM won't be swamped by other ELF overhead). */
222 static ATTRIBUTE_UNUSED bfd_boolean
223 is_reloc_PC_relative (reloc_howto_type
*howto
)
225 return (strstr (howto
->name
, "PC") != NULL
) ? TRUE
: FALSE
;
229 is_reloc_SDA_relative (reloc_howto_type
*howto
)
231 return (strstr (howto
->name
, "SDA") != NULL
) ? TRUE
: FALSE
;
235 is_reloc_for_GOT (reloc_howto_type
* howto
)
237 if (strstr (howto
->name
, "TLS") != NULL
)
239 return (strstr (howto
->name
, "GOT") != NULL
) ? TRUE
: FALSE
;
243 is_reloc_for_PLT (reloc_howto_type
* howto
)
245 return (strstr (howto
->name
, "PLT") != NULL
) ? TRUE
: FALSE
;
249 is_reloc_for_TLS (reloc_howto_type
*howto
)
251 return (strstr (howto
->name
, "TLS") != NULL
) ? TRUE
: FALSE
;
254 #define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
255 #define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
256 #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
257 #define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
258 #define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
259 #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
262 static bfd_reloc_status_type
263 arc_elf_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
264 arelent
*reloc_entry
,
266 void *data ATTRIBUTE_UNUSED
,
267 asection
*input_section
,
269 char ** error_message ATTRIBUTE_UNUSED
)
271 if (output_bfd
!= NULL
)
273 reloc_entry
->address
+= input_section
->output_offset
;
275 /* In case of relocateable link and if the reloc is against a
276 section symbol, the addend needs to be adjusted according to
277 where the section symbol winds up in the output section. */
278 if ((symbol_in
->flags
& BSF_SECTION_SYM
) && symbol_in
->section
)
279 reloc_entry
->addend
+= symbol_in
->section
->output_offset
;
284 return bfd_reloc_continue
;
288 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
292 #include "elf/arc-reloc.def"
295 #undef ARC_RELOC_HOWTO
297 #define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
298 [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, FALSE, 0, complain_overflow_##OVERFLOW, arc_elf_reloc, "R_" #TYPE, FALSE, 0, 0, FALSE),
300 static struct reloc_howto_struct elf_arc_howto_table
[] =
302 #include "elf/arc-reloc.def"
303 /* Example of what is generated by the preprocessor. Currently kept as an
305 HOWTO (R_ARC_NONE, // Type.
307 2, // Size (0 = byte, 1 = short, 2 = long).
309 FALSE, // PC_relative.
311 complain_overflow_bitfield, // Complain_on_overflow.
312 bfd_elf_generic_reloc, // Special_function.
313 "R_ARC_NONE", // Name.
314 TRUE, // Partial_inplace.
317 FALSE), // PCrel_offset.
320 #undef ARC_RELOC_HOWTO
322 static void arc_elf_howto_init (void)
324 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
325 elf_arc_howto_table[TYPE].pc_relative = \
326 (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
327 elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \
328 /* Only 32 bit data relocations should be marked as ME. */ \
329 if (strstr (#FORMULA, " ME ") != NULL) \
331 BFD_ASSERT (SIZE == 2); \
334 #include "elf/arc-reloc.def"
337 #undef ARC_RELOC_HOWTO
340 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
342 const int howto_table_lookup
[] =
344 #include "elf/arc-reloc.def"
346 #undef ARC_RELOC_HOWTO
348 static reloc_howto_type
*
349 arc_elf_howto (unsigned int r_type
)
351 if (elf_arc_howto_table
[R_ARC_32
].dst_mask
== 0)
352 arc_elf_howto_init ();
353 return &elf_arc_howto_table
[r_type
];
356 /* Map BFD reloc types to ARC ELF reloc types. */
360 bfd_reloc_code_real_type bfd_reloc_val
;
361 unsigned char elf_reloc_val
;
364 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
365 { BFD_RELOC_##TYPE, R_##TYPE },
366 static const struct arc_reloc_map arc_reloc_map
[] =
368 #include "elf/arc-reloc.def"
370 {BFD_RELOC_NONE
, R_ARC_NONE
},
371 {BFD_RELOC_8
, R_ARC_8
},
372 {BFD_RELOC_16
, R_ARC_16
},
373 {BFD_RELOC_24
, R_ARC_24
},
374 {BFD_RELOC_32
, R_ARC_32
},
376 #undef ARC_RELOC_HOWTO
378 static reloc_howto_type
*
379 arc_elf32_bfd_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
380 bfd_reloc_code_real_type code
)
384 for (i
= ARRAY_SIZE (arc_reloc_map
); i
--;)
386 if (arc_reloc_map
[i
].bfd_reloc_val
== code
)
387 return arc_elf_howto (arc_reloc_map
[i
].elf_reloc_val
);
393 /* Function to set the ELF flag bits. */
395 arc_elf_set_private_flags (bfd
*abfd
, flagword flags
)
397 elf_elfheader (abfd
)->e_flags
= flags
;
398 elf_flags_init (abfd
) = TRUE
;
402 /* Print private flags. */
404 arc_elf_print_private_bfd_data (bfd
*abfd
, void * ptr
)
406 FILE *file
= (FILE *) ptr
;
409 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
411 /* Print normal ELF private data. */
412 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
414 flags
= elf_elfheader (abfd
)->e_flags
;
415 fprintf (file
, _("private flags = 0x%lx:"), (unsigned long) flags
);
417 switch (flags
& EF_ARC_MACH_MSK
)
419 case EF_ARC_CPU_ARCV2HS
: fprintf (file
, " -mcpu=ARCv2HS"); break;
420 case EF_ARC_CPU_ARCV2EM
: fprintf (file
, " -mcpu=ARCv2EM"); break;
421 case E_ARC_MACH_ARC600
: fprintf (file
, " -mcpu=ARC600"); break;
422 case E_ARC_MACH_ARC601
: fprintf (file
, " -mcpu=ARC601"); break;
423 case E_ARC_MACH_ARC700
: fprintf (file
, " -mcpu=ARC700"); break;
425 fprintf (file
, "-mcpu=unknown");
429 switch (flags
& EF_ARC_OSABI_MSK
)
431 case E_ARC_OSABI_ORIG
: fprintf (file
, " (ABI:legacy)"); break;
432 case E_ARC_OSABI_V2
: fprintf (file
, " (ABI:v2)"); break;
433 case E_ARC_OSABI_V3
: fprintf (file
, " (ABI:v3)"); break;
435 fprintf (file
, "(ABI:unknown)");
443 /* Copy backend specific data from one object module to another. */
446 arc_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
448 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
449 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
452 BFD_ASSERT (!elf_flags_init (obfd
)
453 || elf_elfheader (obfd
)->e_flags
== elf_elfheader (ibfd
)->e_flags
);
455 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
456 elf_flags_init (obfd
) = TRUE
;
458 /* Copy object attributes. */
459 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
461 return _bfd_elf_copy_private_bfd_data (ibfd
, obfd
);
464 static reloc_howto_type
*
465 bfd_elf32_bfd_reloc_name_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
470 for (i
= 0; i
< ARRAY_SIZE (elf_arc_howto_table
); i
++)
471 if (elf_arc_howto_table
[i
].name
!= NULL
472 && strcasecmp (elf_arc_howto_table
[i
].name
, r_name
) == 0)
473 return arc_elf_howto (i
);
478 /* Set the howto pointer for an ARC ELF reloc. */
481 arc_info_to_howto_rel (bfd
* abfd ATTRIBUTE_UNUSED
,
483 Elf_Internal_Rela
* dst
)
487 r_type
= ELF32_R_TYPE (dst
->r_info
);
488 BFD_ASSERT (r_type
< (unsigned int) R_ARC_max
);
489 cache_ptr
->howto
= arc_elf_howto (r_type
);
492 /* Merge backend specific data from an object file to the output
493 object file when linking. */
496 arc_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
498 unsigned short mach_ibfd
;
499 static unsigned short mach_obfd
= EM_NONE
;
504 /* Check if we have the same endianess. */
505 if (! _bfd_generic_verify_endian_match (ibfd
, obfd
))
507 _bfd_error_handler (_("ERROR: Endian Match failed. Attempting to link "
508 "%B with binary %s of opposite endian-ness"),
509 ibfd
, bfd_get_filename (obfd
));
513 /* Collect ELF flags. */
514 in_flags
= elf_elfheader (ibfd
)->e_flags
& EF_ARC_MACH_MSK
;
515 out_flags
= elf_elfheader (obfd
)->e_flags
& EF_ARC_MACH_MSK
;
517 if (!elf_flags_init (obfd
)) /* First call, no flags set. */
519 elf_flags_init (obfd
) = TRUE
;
520 out_flags
= in_flags
;
523 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
524 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
527 /* Check to see if the input BFD actually contains any sections. Do
528 not short-circuit dynamic objects; their section list may be
529 emptied by elf_link_add_object_symbols. */
530 if (!(ibfd
->flags
& DYNAMIC
))
532 bfd_boolean null_input_bfd
= TRUE
;
533 bfd_boolean only_data_sections
= TRUE
;
535 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
537 if ((bfd_get_section_flags (ibfd
, sec
)
538 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
539 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
540 only_data_sections
= FALSE
;
542 null_input_bfd
= FALSE
;
545 if (null_input_bfd
|| only_data_sections
)
549 /* Complain about various flag/architecture mismatches. */
550 mach_ibfd
= elf_elfheader (ibfd
)->e_machine
;
551 if (mach_obfd
== EM_NONE
)
553 mach_obfd
= mach_ibfd
;
557 if (mach_ibfd
!= mach_obfd
)
559 _bfd_error_handler (_("ERROR: Attempting to link %B "
560 "with a binary %s of different architecture"),
561 ibfd
, bfd_get_filename (obfd
));
564 else if (in_flags
!= out_flags
)
566 /* Warn if different flags. */
567 (*_bfd_error_handler
)
568 (_("%s: uses different e_flags (0x%lx) fields than "
569 "previous modules (0x%lx)"),
570 bfd_get_filename (ibfd
), (long)in_flags
, (long)out_flags
);
571 if (in_flags
&& out_flags
)
573 /* MWDT doesnt set the eflags hence make sure we choose the
574 eflags set by gcc. */
575 in_flags
= in_flags
> out_flags
? in_flags
: out_flags
;
579 /* Update the flags. */
580 elf_elfheader (obfd
)->e_flags
= in_flags
;
582 if (bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
584 return bfd_set_arch_mach (obfd
, bfd_arch_arc
, bfd_get_mach (ibfd
));
590 /* Set the right machine number for an ARC ELF file. */
592 arc_elf_object_p (bfd
* abfd
)
594 /* Make sure this is initialised, or you'll have the potential of passing
595 garbage---or misleading values---into the call to
596 bfd_default_set_arch_mach (). */
597 int mach
= bfd_mach_arc_arc700
;
598 unsigned long arch
= elf_elfheader (abfd
)->e_flags
& EF_ARC_MACH_MSK
;
599 unsigned e_machine
= elf_elfheader (abfd
)->e_machine
;
601 if (e_machine
== EM_ARC_COMPACT
|| e_machine
== EM_ARC_COMPACT2
)
605 case E_ARC_MACH_ARC600
:
606 mach
= bfd_mach_arc_arc600
;
608 case E_ARC_MACH_ARC601
:
609 mach
= bfd_mach_arc_arc601
;
611 case E_ARC_MACH_ARC700
:
612 mach
= bfd_mach_arc_arc700
;
614 case E_ARC_MACH_NPS400
:
615 mach
= bfd_mach_arc_nps400
;
617 case EF_ARC_CPU_ARCV2HS
:
618 case EF_ARC_CPU_ARCV2EM
:
619 mach
= bfd_mach_arc_arcv2
;
622 mach
= (e_machine
== EM_ARC_COMPACT
) ?
623 bfd_mach_arc_arc700
: bfd_mach_arc_arcv2
;
629 if (e_machine
== EM_ARC
)
631 (*_bfd_error_handler
)
632 (_("Error: The ARC4 architecture is no longer supported.\n"));
637 (*_bfd_error_handler
)
638 (_("Warning: unset or old architecture flags. \n"
639 " Use default machine.\n"));
643 return bfd_default_set_arch_mach (abfd
, bfd_arch_arc
, mach
);
646 /* The final processing done just before writing out an ARC ELF object file.
647 This gets the ARC architecture right based on the machine number. */
650 arc_elf_final_write_processing (bfd
* abfd
,
651 bfd_boolean linker ATTRIBUTE_UNUSED
)
655 switch (bfd_get_mach (abfd
))
657 case bfd_mach_arc_arc600
:
658 emf
= EM_ARC_COMPACT
;
660 case bfd_mach_arc_arc601
:
661 emf
= EM_ARC_COMPACT
;
663 case bfd_mach_arc_arc700
:
664 emf
= EM_ARC_COMPACT
;
666 case bfd_mach_arc_nps400
:
667 emf
= EM_ARC_COMPACT
;
669 case bfd_mach_arc_arcv2
:
670 emf
= EM_ARC_COMPACT2
;
676 elf_elfheader (abfd
)->e_machine
= emf
;
678 /* Record whatever is the current syscall ABI version. */
679 elf_elfheader (abfd
)->e_flags
|= E_ARC_OSABI_CURRENT
;
682 #define BFD_DEBUG_PIC(...)
684 struct arc_relocation_data
686 bfd_vma reloc_offset
;
687 bfd_vma reloc_addend
;
688 bfd_vma got_offset_value
;
691 asection
* sym_section
;
693 reloc_howto_type
*howto
;
695 asection
* input_section
;
697 bfd_vma sdata_begin_symbol_vma
;
698 bfd_boolean sdata_begin_symbol_vma_set
;
699 bfd_vma got_symbol_vma
;
701 bfd_boolean should_relocate
;
705 debug_arc_reloc (struct arc_relocation_data reloc_data
)
707 PR_DEBUG ("Reloc type=%s, should_relocate = %s\n",
708 reloc_data
.howto
->name
,
709 reloc_data
.should_relocate
? "true" : "false");
710 PR_DEBUG (" offset = 0x%x, addend = 0x%x\n",
711 (unsigned int) reloc_data
.reloc_offset
,
712 (unsigned int) reloc_data
.reloc_addend
);
713 PR_DEBUG (" Symbol:\n");
714 PR_DEBUG (" value = 0x%08x\n",
715 (unsigned int) reloc_data
.sym_value
);
716 if (reloc_data
.sym_section
!= NULL
)
718 PR_DEBUG ("IN IF\n");
720 " section name = %s, output_offset 0x%08x",
721 reloc_data
.sym_section
->name
,
722 (unsigned int) reloc_data
.sym_section
->output_offset
);
723 if (reloc_data
.sym_section
->output_section
!= NULL
)
726 ", output_section->vma = 0x%08x",
727 ((unsigned int) reloc_data
.sym_section
->output_section
->vma
));
734 PR_DEBUG ( " symbol section is NULL\n");
737 PR_DEBUG ( " Input_section:\n");
738 if (reloc_data
.input_section
!= NULL
)
741 " section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
742 reloc_data
.input_section
->name
,
743 (unsigned int) reloc_data
.input_section
->output_offset
,
744 (unsigned int) reloc_data
.input_section
->output_section
->vma
);
745 PR_DEBUG ( " changed_address = 0x%08x\n",
746 (unsigned int) (reloc_data
.input_section
->output_section
->vma
+
747 reloc_data
.input_section
->output_offset
+
748 reloc_data
.reloc_offset
));
752 PR_DEBUG ( " input section is NULL\n");
757 middle_endian_convert (bfd_vma insn
, bfd_boolean do_it
)
762 ((insn
& 0xffff0000) >> 16) |
763 ((insn
& 0xffff) << 16);
768 #define ME(reloc) (reloc)
770 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
771 && (!bfd_big_endian (BFD)))
773 #define S (reloc_data.sym_value \
774 + (reloc_data.sym_section->output_section != NULL ? \
775 (reloc_data.sym_section->output_offset \
776 + reloc_data.sym_section->output_section->vma) : 0) \
778 #define L (reloc_data.sym_value \
779 + (reloc_data.sym_section->output_section != NULL ? \
780 (reloc_data.sym_section->output_offset \
781 + reloc_data.sym_section->output_section->vma) : 0) \
783 #define A (reloc_data.reloc_addend)
785 #define G (reloc_data.got_offset_value)
786 #define GOT (reloc_data.got_symbol_vma)
787 #define GOT_BEGIN (htab->sgot->output_section->vma)
790 /* P: relative offset to PCL The offset should be to the
791 current location aligned to 32 bits. */
794 (reloc_data.input_section->output_section != NULL ? \
795 reloc_data.input_section->output_section->vma : 0) \
796 + reloc_data.input_section->output_offset \
797 + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0)) \
800 (reloc_data.input_section->output_section->vma \
801 + reloc_data.input_section->output_offset \
802 + (reloc_data.reloc_offset) \
804 #define SECTSTAR (reloc_data.input_section->output_offset)
805 #define SECTSTART (reloc_data.input_section->output_offset)
806 #define _SDA_BASE_ (reloc_data.sdata_begin_symbol_vma)
807 #define TLS_REL ((elf_hash_table (info))->tls_sec->output_section->vma)
808 #define _SDA_BASE_ (reloc_data.sdata_begin_symbol_vma)
814 #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA) \
816 asection *sym_section = reloc_data.sym_section; \
817 asection *input_section = reloc_data.input_section; \
818 ARC_DEBUG ("FORMULA = " #FORMULA "\n"); \
819 ARC_DEBUG ("S = 0x%x\n", S); \
820 ARC_DEBUG ("A = 0x%x\n", A); \
821 ARC_DEBUG ("L = 0x%x\n", L); \
822 if (sym_section->output_section != NULL) \
824 ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
825 sym_section->output_section->vma + sym_section->output_offset); \
829 ARC_DEBUG ("symbol_section->vma = NULL\n"); \
831 if (input_section->output_section != NULL) \
833 ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
834 input_section->output_section->vma + input_section->output_offset); \
838 ARC_DEBUG ("symbol_section->vma = NULL\n"); \
840 ARC_DEBUG ("PCL = 0x%x\n", P); \
841 ARC_DEBUG ("P = 0x%x\n", P); \
842 ARC_DEBUG ("G = 0x%x\n", G); \
843 ARC_DEBUG ("SDA_OFFSET = 0x%x\n", _SDA_BASE_); \
844 ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
845 ARC_DEBUG ("GOT_OFFSET = 0x%x\n", GOT); \
846 ARC_DEBUG ("relocation = 0x%08x\n", relocation); \
847 ARC_DEBUG ("before = 0x%08x\n", (unsigned int) insn); \
848 ARC_DEBUG ("data = 0x%08x (%u) (%d)\n", (unsigned int) relocation, (unsigned int) relocation, (int) relocation); \
851 #define PRINT_DEBUG_RELOC_INFO_AFTER \
853 ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \
856 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
859 bfd_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
860 relocation = FORMULA ; \
861 PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA) \
862 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
863 insn = RELOC_FUNCTION (insn, relocation); \
864 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
865 PRINT_DEBUG_RELOC_INFO_AFTER \
869 static bfd_reloc_status_type
870 arc_do_relocation (bfd_byte
* contents
,
871 struct arc_relocation_data reloc_data
,
872 struct bfd_link_info
*info
)
874 bfd_vma relocation
= 0;
876 bfd_vma orig_insn ATTRIBUTE_UNUSED
;
877 bfd
* abfd
= reloc_data
.input_section
->owner
;
878 struct elf_link_hash_table
*htab ATTRIBUTE_UNUSED
= elf_hash_table (info
);
880 if (reloc_data
.should_relocate
== FALSE
)
883 switch (reloc_data
.howto
->size
)
886 insn
= arc_bfd_get_32 (abfd
,
887 contents
+ reloc_data
.reloc_offset
,
888 reloc_data
.input_section
);
891 insn
= arc_bfd_get_16 (abfd
,
892 contents
+ reloc_data
.reloc_offset
,
893 reloc_data
.input_section
);
896 insn
= arc_bfd_get_8 (abfd
,
897 contents
+ reloc_data
.reloc_offset
,
898 reloc_data
.input_section
);
908 switch (reloc_data
.howto
->type
)
910 #include "elf/arc-reloc.def"
917 /* Check for relocation overflow. */
918 if (reloc_data
.howto
->complain_on_overflow
!= complain_overflow_dont
)
920 bfd_reloc_status_type flag
;
921 flag
= bfd_check_overflow (reloc_data
.howto
->complain_on_overflow
,
922 reloc_data
.howto
->bitsize
,
923 reloc_data
.howto
->rightshift
,
924 bfd_arch_bits_per_address (abfd
),
927 #undef DEBUG_ARC_RELOC
928 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
929 if (flag
!= bfd_reloc_ok
)
931 PR_DEBUG ( "Relocation overflows !!!!\n");
933 DEBUG_ARC_RELOC (reloc_data
);
936 "Relocation value = signed -> %d, unsigned -> %u"
937 ", hex -> (0x%08x)\n",
939 (unsigned int) relocation
,
940 (unsigned int) relocation
);
944 #undef DEBUG_ARC_RELOC
945 #define DEBUG_ARC_RELOC(A)
947 switch (reloc_data
.howto
->size
)
950 arc_bfd_put_32 (abfd
, insn
,
951 contents
+ reloc_data
.reloc_offset
,
952 reloc_data
.input_section
);
955 arc_bfd_put_16 (abfd
, insn
,
956 contents
+ reloc_data
.reloc_offset
,
957 reloc_data
.input_section
);
960 arc_bfd_put_8 (abfd
, insn
,
961 contents
+ reloc_data
.reloc_offset
,
962 reloc_data
.input_section
);
965 ARC_DEBUG ("size = %d\n", reloc_data
.howto
->size
);
985 #undef ARC_RELOC_HOWTO
987 static struct got_entry
**
988 arc_get_local_got_ents (bfd
* abfd
)
990 static struct got_entry
**local_got_ents
= NULL
;
992 if (local_got_ents
== NULL
)
995 Elf_Internal_Shdr
*symtab_hdr
= &((elf_tdata (abfd
))->symtab_hdr
);
997 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
998 local_got_ents
= (struct got_entry
**)
999 bfd_alloc (abfd
, sizeof(struct got_entry
*) * size
);
1000 if (local_got_ents
== NULL
)
1003 memset (local_got_ents
, 0, sizeof(struct got_entry
*) * size
);
1004 elf_local_got_ents (abfd
) = local_got_ents
;
1007 return local_got_ents
;
1010 /* Relocate an arc ELF section.
1011 Function : elf_arc_relocate_section
1012 Brief : Relocate an arc section, by handling all the relocations
1013 appearing in that section.
1014 Args : output_bfd : The bfd being written to.
1015 info : Link information.
1016 input_bfd : The input bfd.
1017 input_section : The section being relocated.
1018 contents : contents of the section being relocated.
1019 relocs : List of relocations in the section.
1020 local_syms : is a pointer to the swapped in local symbols.
1021 local_section : is an array giving the section in the input file
1022 corresponding to the st_shndx field of each
1025 elf_arc_relocate_section (bfd
* output_bfd
,
1026 struct bfd_link_info
* info
,
1028 asection
* input_section
,
1029 bfd_byte
* contents
,
1030 Elf_Internal_Rela
* relocs
,
1031 Elf_Internal_Sym
* local_syms
,
1032 asection
** local_sections
)
1034 Elf_Internal_Shdr
* symtab_hdr
;
1035 struct elf_link_hash_entry
** sym_hashes
;
1036 struct got_entry
** local_got_ents
;
1037 Elf_Internal_Rela
* rel
;
1038 Elf_Internal_Rela
* relend
;
1039 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1041 symtab_hdr
= &((elf_tdata (input_bfd
))->symtab_hdr
);
1042 sym_hashes
= elf_sym_hashes (input_bfd
);
1045 relend
= relocs
+ input_section
->reloc_count
;
1046 for (; rel
< relend
; rel
++)
1048 enum elf_arc_reloc_type r_type
;
1049 reloc_howto_type
* howto
;
1050 unsigned long r_symndx
;
1051 struct elf_link_hash_entry
* h
;
1052 Elf_Internal_Sym
* sym
;
1054 struct elf_link_hash_entry
*h2
;
1056 struct arc_relocation_data reloc_data
=
1060 .got_offset_value
= 0,
1062 .sym_section
= NULL
,
1064 .input_section
= NULL
,
1065 .sdata_begin_symbol_vma
= 0,
1066 .sdata_begin_symbol_vma_set
= FALSE
,
1067 .got_symbol_vma
= 0,
1068 .should_relocate
= FALSE
1071 r_type
= ELF32_R_TYPE (rel
->r_info
);
1073 if (r_type
>= (int) R_ARC_max
)
1075 bfd_set_error (bfd_error_bad_value
);
1078 howto
= &elf_arc_howto_table
[r_type
];
1080 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1082 /* If we are generating another .o file and the symbol in not
1083 local, skip this relocation. */
1084 if (bfd_link_relocatable (info
))
1086 /* This is a relocateable link. We don't have to change
1087 anything, unless the reloc is against a section symbol,
1088 in which case we have to adjust according to where the
1089 section symbol winds up in the output section. */
1091 /* Checks if this is a local symbol and thus the reloc
1092 might (will??) be against a section symbol. */
1093 if (r_symndx
< symtab_hdr
->sh_info
)
1095 sym
= local_syms
+ r_symndx
;
1096 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1098 sec
= local_sections
[r_symndx
];
1100 /* for RELA relocs.Just adjust the addend
1101 value in the relocation entry. */
1102 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
1105 PR_DEBUG ("local symbols reloc "
1106 "(section=%d %s) seen in %s\n",
1108 local_sections
[r_symndx
]->name
,
1109 __PRETTY_FUNCTION__
)
1117 h2
= elf_link_hash_lookup (elf_hash_table (info
), "__SDATA_BEGIN__",
1118 FALSE
, FALSE
, TRUE
);
1120 if (reloc_data
.sdata_begin_symbol_vma_set
== FALSE
1121 && h2
!= NULL
&& h2
->root
.type
!= bfd_link_hash_undefined
1122 && h2
->root
.u
.def
.section
->output_section
!= NULL
)
1123 /* TODO: Verify this condition. */
1125 reloc_data
.sdata_begin_symbol_vma
=
1126 (h2
->root
.u
.def
.value
+
1127 h2
->root
.u
.def
.section
->output_section
->vma
);
1128 reloc_data
.sdata_begin_symbol_vma_set
= TRUE
;
1131 reloc_data
.input_section
= input_section
;
1132 reloc_data
.howto
= howto
;
1133 reloc_data
.reloc_offset
= rel
->r_offset
;
1134 reloc_data
.reloc_addend
= rel
->r_addend
;
1136 /* This is a final link. */
1141 if (r_symndx
< symtab_hdr
->sh_info
) /* A local symbol. */
1143 local_got_ents
= arc_get_local_got_ents (output_bfd
);
1144 struct got_entry
*entry
= local_got_ents
[r_symndx
];
1146 sym
= local_syms
+ r_symndx
;
1147 sec
= local_sections
[r_symndx
];
1149 reloc_data
.sym_value
= sym
->st_value
;
1150 reloc_data
.sym_section
= sec
;
1152 /* Mergeable section handling. */
1153 if ((sec
->flags
& SEC_MERGE
)
1154 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1158 rel
->r_addend
= _bfd_elf_rel_local_sym (output_bfd
, sym
,
1159 &msec
, rel
->r_addend
);
1160 rel
->r_addend
-= (sec
->output_section
->vma
1161 + sec
->output_offset
1163 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
1165 reloc_data
.reloc_addend
= rel
->r_addend
;
1168 if ((is_reloc_for_GOT (howto
)
1169 || is_reloc_for_TLS (howto
)) && entry
!= NULL
)
1171 if (is_reloc_for_TLS (howto
))
1172 while (entry
->type
== GOT_NORMAL
&& entry
->next
!= NULL
)
1173 entry
= entry
->next
;
1175 if (is_reloc_for_GOT (howto
))
1176 while (entry
->type
!= GOT_NORMAL
&& entry
->next
!= NULL
)
1177 entry
= entry
->next
;
1179 if (entry
->type
== GOT_TLS_GD
&& entry
->processed
== FALSE
)
1181 bfd_vma sym_vma
= sym
->st_value
1182 + sec
->output_section
->vma
1183 + sec
->output_offset
;
1185 /* Create dynamic relocation for local sym. */
1186 ADD_RELA (output_bfd
, got
, entry
->offset
, 0,
1187 R_ARC_TLS_DTPMOD
, 0);
1188 ADD_RELA (output_bfd
, got
, entry
->offset
+4, 0,
1189 R_ARC_TLS_DTPOFF
, 0);
1191 bfd_vma sec_vma
= sec
->output_section
->vma
1192 + sec
->output_offset
;
1193 bfd_put_32 (output_bfd
, sym_vma
- sec_vma
,
1194 htab
->sgot
->contents
+ entry
->offset
+ 4);
1196 ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_GD value "
1197 "= 0x%x @ 0x%x, for symbol %s\n",
1199 htab
->sgot
->contents
+ entry
->offset
+ 4,
1202 entry
->processed
= TRUE
;
1204 if (entry
->type
== GOT_TLS_IE
&& entry
->processed
== FALSE
)
1206 bfd_vma sym_vma
= sym
->st_value
1207 + sec
->output_section
->vma
1208 + sec
->output_offset
;
1209 bfd_vma sec_vma
= htab
->tls_sec
->output_section
->vma
;
1210 bfd_put_32 (output_bfd
, sym_vma
- sec_vma
,
1211 htab
->sgot
->contents
+ entry
->offset
);
1212 /* TODO: Check if this type of relocs is the cause
1213 for all the ARC_NONE dynamic relocs. */
1215 ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_IE value = "
1216 "0x%x @ 0x%x, for symbol %s\n",
1218 htab
->sgot
->contents
+ entry
->offset
,
1221 entry
->processed
= TRUE
;
1223 if (entry
->type
== GOT_NORMAL
&& entry
->processed
== FALSE
)
1225 bfd_vma sec_vma
= reloc_data
.sym_section
->output_section
->vma
1226 + reloc_data
.sym_section
->output_offset
;
1228 bfd_put_32 (output_bfd
, reloc_data
.sym_value
+ sec_vma
,
1229 htab
->sgot
->contents
+ entry
->offset
);
1231 ARC_DEBUG ("arc_info: PATCHED: 0x%08x @ 0x%08x for "
1232 "sym %s in got offset 0x%x\n",
1233 reloc_data
.sym_value
+ sec_vma
,
1234 htab
->sgot
->output_section
->vma
1235 + htab
->sgot
->output_offset
+ entry
->offset
,
1238 entry
->processed
= TRUE
;
1241 reloc_data
.got_offset_value
= entry
->offset
;
1242 ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
1243 "vma = 0x%x for symbol %s\n",
1244 entry
->type
, entry
->offset
,
1245 htab
->sgot
->output_section
->vma
1246 + htab
->sgot
->output_offset
+ entry
->offset
,
1250 BFD_ASSERT (htab
->sgot
!= NULL
|| !is_reloc_for_GOT (howto
));
1251 if (htab
->sgot
!= NULL
)
1252 reloc_data
.got_symbol_vma
= htab
->sgot
->output_section
->vma
1253 + htab
->sgot
->output_offset
;
1255 reloc_data
.should_relocate
= TRUE
;
1257 else /* Global symbol. */
1259 /* Get the symbol's entry in the symtab. */
1260 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1262 while (h
->root
.type
== bfd_link_hash_indirect
1263 || h
->root
.type
== bfd_link_hash_warning
)
1264 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1266 BFD_ASSERT ((h
->dynindx
== -1) >= (h
->forced_local
!= 0));
1267 /* If we have encountered a definition for this symbol. */
1268 if (h
->root
.type
== bfd_link_hash_defined
1269 || h
->root
.type
== bfd_link_hash_defweak
)
1271 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1272 reloc_data
.sym_section
= h
->root
.u
.def
.section
;
1274 reloc_data
.should_relocate
= TRUE
;
1276 if (is_reloc_for_GOT (howto
) && !bfd_link_pic (info
))
1278 /* TODO: Change it to use arc_do_relocation with
1279 ARC_32 reloc. Try to use ADD_RELA macro. */
1280 bfd_vma relocation
=
1281 reloc_data
.sym_value
+ reloc_data
.reloc_addend
1282 + (reloc_data
.sym_section
->output_section
!= NULL
?
1283 (reloc_data
.sym_section
->output_offset
1284 + reloc_data
.sym_section
->output_section
->vma
)
1287 BFD_ASSERT (h
->got
.glist
);
1288 bfd_vma got_offset
= h
->got
.glist
->offset
;
1289 bfd_put_32 (output_bfd
, relocation
,
1290 htab
->sgot
->contents
+ got_offset
);
1292 if (is_reloc_for_PLT (howto
) && h
->plt
.offset
!= (bfd_vma
) -1)
1294 /* TODO: This is repeated up here. */
1295 reloc_data
.sym_value
= h
->plt
.offset
;
1296 reloc_data
.sym_section
= htab
->splt
;
1299 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1301 /* Is weak symbol and has no definition. */
1302 if (is_reloc_for_GOT (howto
))
1304 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1305 reloc_data
.sym_section
= htab
->sgot
;
1306 reloc_data
.should_relocate
= TRUE
;
1308 else if (is_reloc_for_PLT (howto
)
1309 && h
->plt
.offset
!= (bfd_vma
) -1)
1311 /* TODO: This is repeated up here. */
1312 reloc_data
.sym_value
= h
->plt
.offset
;
1313 reloc_data
.sym_section
= htab
->splt
;
1314 reloc_data
.should_relocate
= TRUE
;
1321 if (is_reloc_for_GOT (howto
))
1323 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1324 reloc_data
.sym_section
= htab
->sgot
;
1326 reloc_data
.should_relocate
= TRUE
;
1328 else if (is_reloc_for_PLT (howto
))
1330 /* Fail if it is linking for PIE and the symbol is
1332 if (bfd_link_executable (info
)
1333 && !(*info
->callbacks
->undefined_symbol
)
1334 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
1335 rel
->r_offset
, TRUE
))
1339 reloc_data
.sym_value
= h
->plt
.offset
;
1340 reloc_data
.sym_section
= htab
->splt
;
1342 reloc_data
.should_relocate
= TRUE
;
1344 else if (!bfd_link_pic (info
)
1345 && !(*info
->callbacks
->undefined_symbol
)
1346 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
1347 rel
->r_offset
, TRUE
))
1353 if (h
->got
.glist
!= NULL
)
1355 struct got_entry
*entry
= h
->got
.glist
;
1357 if (is_reloc_for_GOT (howto
) || is_reloc_for_TLS (howto
))
1359 if (! elf_hash_table (info
)->dynamic_sections_created
1360 || (bfd_link_pic (info
)
1361 && SYMBOL_REFERENCES_LOCAL (info
, h
)))
1363 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1364 reloc_data
.sym_section
= h
->root
.u
.def
.section
;
1366 if (is_reloc_for_TLS (howto
))
1367 while (entry
->type
== GOT_NORMAL
&& entry
->next
!= NULL
)
1368 entry
= entry
->next
;
1370 if (entry
->processed
== FALSE
1371 && (entry
->type
== GOT_TLS_GD
1372 || entry
->type
== GOT_TLS_IE
))
1374 bfd_vma sym_value
= h
->root
.u
.def
.value
1375 + h
->root
.u
.def
.section
->output_section
->vma
1376 + h
->root
.u
.def
.section
->output_offset
;
1379 elf_hash_table (info
)->tls_sec
->output_section
->vma
;
1381 bfd_put_32 (output_bfd
,
1382 sym_value
- sec_vma
,
1383 htab
->sgot
->contents
+ entry
->offset
1384 + (entry
->existing_entries
== MOD_AND_OFF
? 4 : 0));
1386 ARC_DEBUG ("arc_info: FIXED -> %s value = 0x%x "
1387 "@ 0x%x, for symbol %s\n",
1388 (entry
->type
== GOT_TLS_GD
? "GOT_TLS_GD" :
1390 sym_value
- sec_vma
,
1391 htab
->sgot
->contents
+ entry
->offset
1392 + (entry
->existing_entries
== MOD_AND_OFF
? 4 : 0),
1393 h
->root
.root
.string
);
1395 entry
->processed
= TRUE
;
1398 if (entry
->type
== GOT_TLS_IE
&& entry
->processed
== FALSE
)
1400 bfd_vma sec_vma
= htab
->tls_sec
->output_section
->vma
;
1401 bfd_put_32 (output_bfd
,
1402 reloc_data
.sym_value
- sec_vma
,
1403 htab
->sgot
->contents
+ entry
->offset
);
1406 if (entry
->type
== GOT_NORMAL
&& entry
->processed
== FALSE
)
1409 reloc_data
.sym_section
->output_section
->vma
1410 + reloc_data
.sym_section
->output_offset
;
1412 if (h
->root
.type
!= bfd_link_hash_undefweak
)
1414 bfd_put_32 (output_bfd
,
1415 reloc_data
.sym_value
+ sec_vma
,
1416 htab
->sgot
->contents
+ entry
->offset
);
1418 ARC_DEBUG ("arc_info: PATCHED: 0x%08x "
1419 "@ 0x%08x for sym %s in got offset 0x%x\n",
1420 reloc_data
.sym_value
+ sec_vma
,
1421 htab
->sgot
->output_section
->vma
1422 + htab
->sgot
->output_offset
+ entry
->offset
,
1423 h
->root
.root
.string
,
1428 ARC_DEBUG ("arc_info: PATCHED: NOT_PATCHED "
1429 "@ 0x%08x for sym %s in got offset 0x%x "
1431 htab
->sgot
->output_section
->vma
1432 + htab
->sgot
->output_offset
+ entry
->offset
,
1433 h
->root
.root
.string
,
1437 entry
->processed
= TRUE
;
1442 reloc_data
.got_offset_value
= entry
->offset
;
1444 ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
1445 "vma = 0x%x for symbol %s\n",
1446 entry
->type
, entry
->offset
,
1447 htab
->sgot
->output_section
->vma
1448 + htab
->sgot
->output_offset
+ entry
->offset
,
1449 h
->root
.root
.string
);
1452 BFD_ASSERT (htab
->sgot
!= NULL
|| !is_reloc_for_GOT (howto
));
1453 if (htab
->sgot
!= NULL
)
1454 reloc_data
.got_symbol_vma
= htab
->sgot
->output_section
->vma
1455 + htab
->sgot
->output_offset
;
1463 case R_ARC_32_PCREL
:
1464 if (bfd_link_pic (info
) && !bfd_link_pie (info
)
1465 && ((r_type
!= R_ARC_PC32
&& r_type
!= R_ARC_32_PCREL
)
1468 && (!info
->symbolic
|| !h
->def_regular
))))
1470 Elf_Internal_Rela outrel
;
1472 bfd_boolean skip
= FALSE
;
1473 bfd_boolean relocate
= FALSE
;
1474 asection
*sreloc
= _bfd_elf_get_dynamic_reloc_section
1475 (input_bfd
, input_section
,
1478 BFD_ASSERT (sreloc
!= NULL
);
1480 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
1484 if (outrel
.r_offset
== (bfd_vma
) -1)
1487 outrel
.r_addend
= rel
->r_addend
;
1488 outrel
.r_offset
+= (input_section
->output_section
->vma
1489 + input_section
->output_offset
);
1493 memset (&outrel
, 0, sizeof outrel
);
1496 else if (r_type
== R_ARC_PC32
1497 || r_type
== R_ARC_32_PCREL
)
1499 BFD_ASSERT (h
!= NULL
);
1500 if ((input_section
->flags
& SEC_ALLOC
) != 0)
1505 BFD_ASSERT (h
->dynindx
!= -1);
1506 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
1510 /* Handle local symbols, they either do not have a
1511 global hash table entry (h == NULL), or are
1512 forced local due to a version script
1513 (h->forced_local), or the third condition is
1514 legacy, it appears to say something like, for
1515 links where we are pre-binding the symbols, or
1516 there's not an entry for this symbol in the
1517 dynamic symbol table, and it's a regular symbol
1518 not defined in a shared object, then treat the
1519 symbol as local, resolve it now. */
1521 || ((info
->symbolic
|| h
->dynindx
== -1)
1526 /* outrel.r_addend = 0; */
1527 outrel
.r_info
= ELF32_R_INFO (0, R_ARC_RELATIVE
);
1531 BFD_ASSERT (h
->dynindx
!= -1);
1533 /* This type of dynamic relocation cannot be created
1534 for code sections. */
1535 BFD_ASSERT ((input_section
->flags
& SEC_CODE
) == 0);
1537 if ((input_section
->flags
& SEC_ALLOC
) != 0)
1542 BFD_ASSERT (h
->dynindx
!= -1);
1543 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_ARC_32
);
1547 BFD_ASSERT (sreloc
->contents
!= 0);
1549 loc
= sreloc
->contents
;
1550 loc
+= sreloc
->reloc_count
* sizeof (Elf32_External_Rela
);
1551 sreloc
->reloc_count
+= 1;
1553 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1555 if (relocate
== FALSE
)
1563 if (is_reloc_SDA_relative (howto
)
1564 && (reloc_data
.sdata_begin_symbol_vma_set
== FALSE
))
1566 (*_bfd_error_handler
)
1567 ("Error: Linker symbol __SDATA_BEGIN__ not found");
1568 bfd_set_error (bfd_error_bad_value
);
1572 DEBUG_ARC_RELOC (reloc_data
);
1574 if (arc_do_relocation (contents
, reloc_data
, info
) != bfd_reloc_ok
)
1581 static struct dynamic_sections
1582 arc_create_dynamic_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1584 struct elf_link_hash_table
*htab
;
1586 struct dynamic_sections ds
=
1588 .initialized
= FALSE
,
1598 htab
= elf_hash_table (info
);
1601 /* Create dynamic sections for relocatable executables so that we
1602 can copy relocations. */
1603 if (! htab
->dynamic_sections_created
&& bfd_link_pic (info
))
1605 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
1609 dynobj
= (elf_hash_table (info
))->dynobj
;
1613 ds
.sgot
= htab
->sgot
;
1614 ds
.srelgot
= htab
->srelgot
;
1616 ds
.sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
1617 ds
.srelgotplt
= ds
.srelplt
;
1619 ds
.splt
= bfd_get_section_by_name (dynobj
, ".plt");
1620 ds
.srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
1623 if (htab
->dynamic_sections_created
)
1625 ds
.sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1628 ds
.initialized
= TRUE
;
1633 #define ADD_SYMBOL_REF_SEC_AND_RELOC(SECNAME, COND_FOR_RELOC, H) \
1634 htab->s##SECNAME->size; \
1636 if (COND_FOR_RELOC) \
1638 htab->srel##SECNAME->size += sizeof (Elf32_External_Rela); \
1639 ARC_DEBUG ("arc_info: Added reloc space in " \
1640 #SECNAME " section at " __FILE__ \
1641 ":%d for symbol\n", \
1642 __LINE__, name_for_global_symbol (H)); \
1645 if (h->dynindx == -1 && !h->forced_local) \
1646 if (! bfd_elf_link_record_dynamic_symbol (info, H)) \
1648 htab->s##SECNAME->size += 4; \
1652 elf_arc_check_relocs (bfd
* abfd
,
1653 struct bfd_link_info
* info
,
1655 const Elf_Internal_Rela
* relocs
)
1657 Elf_Internal_Shdr
* symtab_hdr
;
1658 struct elf_link_hash_entry
** sym_hashes
;
1659 struct got_entry
** local_got_ents
;
1660 const Elf_Internal_Rela
* rel
;
1661 const Elf_Internal_Rela
* rel_end
;
1663 asection
* sreloc
= NULL
;
1664 struct elf_link_hash_table
* htab
= elf_hash_table (info
);
1666 if (bfd_link_relocatable (info
))
1669 dynobj
= (elf_hash_table (info
))->dynobj
;
1670 symtab_hdr
= &((elf_tdata (abfd
))->symtab_hdr
);
1671 sym_hashes
= elf_sym_hashes (abfd
);
1672 local_got_ents
= arc_get_local_got_ents (abfd
);
1674 rel_end
= relocs
+ sec
->reloc_count
;
1675 for (rel
= relocs
; rel
< rel_end
; rel
++)
1677 enum elf_arc_reloc_type r_type
;
1678 reloc_howto_type
*howto
;
1679 unsigned long r_symndx
;
1680 struct elf_link_hash_entry
*h
;
1682 r_type
= ELF32_R_TYPE (rel
->r_info
);
1684 if (r_type
>= (int) R_ARC_max
)
1686 bfd_set_error (bfd_error_bad_value
);
1689 howto
= &elf_arc_howto_table
[r_type
];
1692 && (is_reloc_for_GOT (howto
) == TRUE
1693 || is_reloc_for_TLS (howto
) == TRUE
))
1695 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
1696 if (! _bfd_elf_create_got_section (abfd
, info
))
1700 /* Load symbol information. */
1701 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1702 if (r_symndx
< symtab_hdr
->sh_info
) /* Is a local symbol. */
1704 else /* Global one. */
1705 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1711 /* During shared library creation, these relocs should not
1712 appear in a shared library (as memory will be read only
1713 and the dynamic linker can not resolve these. However
1714 the error should not occur for e.g. debugging or
1715 non-readonly sections. */
1716 if (bfd_link_dll (info
) && !bfd_link_pie (info
)
1717 && (sec
->flags
& SEC_ALLOC
) != 0
1718 && (sec
->flags
& SEC_READONLY
) != 0)
1722 name
= h
->root
.root
.string
;
1724 /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
1726 (*_bfd_error_handler
)
1728 %B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1730 arc_elf_howto (r_type
)->name
,
1732 bfd_set_error (bfd_error_bad_value
);
1736 /* In some cases we are not setting the 'non_got_ref'
1737 flag, even though the relocations don't require a GOT
1738 access. We should extend the testing in this area to
1739 ensure that no significant cases are being missed. */
1744 case R_ARC_32_PCREL
:
1745 if (bfd_link_pic (info
) && !bfd_link_pie (info
)
1746 && ((r_type
!= R_ARC_PC32
&& r_type
!= R_ARC_32_PCREL
)
1749 && (!info
->symbolic
|| !h
->def_regular
))))
1753 sreloc
= _bfd_elf_make_dynamic_reloc_section (sec
, dynobj
,
1761 sreloc
->size
+= sizeof (Elf32_External_Rela
);
1768 if (is_reloc_for_PLT (howto
) == TRUE
)
1776 if (is_reloc_for_GOT (howto
) == TRUE
)
1781 if (local_got_ents
[r_symndx
] == NULL
)
1784 ADD_SYMBOL_REF_SEC_AND_RELOC (got
,
1785 bfd_link_pic (info
),
1787 new_got_entry_to_list (&(local_got_ents
[r_symndx
]),
1788 GOT_NORMAL
, offset
, NONE
);
1793 /* Global symbol. */
1794 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1795 if (h
->got
.glist
== NULL
)
1798 ADD_SYMBOL_REF_SEC_AND_RELOC (got
, TRUE
, h
);
1799 new_got_entry_to_list (&h
->got
.glist
,
1800 GOT_NORMAL
, offset
, NONE
);
1805 if (is_reloc_for_TLS (howto
) == TRUE
)
1807 enum tls_type_e type
= GOT_UNKNOWN
;
1811 case R_ARC_TLS_GD_GOT
:
1814 case R_ARC_TLS_IE_GOT
:
1821 struct got_entry
**list
= NULL
;
1823 list
= &(h
->got
.glist
);
1825 list
= &(local_got_ents
[r_symndx
]);
1827 if (type
!= GOT_UNKNOWN
&& !symbol_has_entry_of_type (*list
, type
))
1829 enum tls_got_entries entries
= NONE
;
1831 ADD_SYMBOL_REF_SEC_AND_RELOC (got
, TRUE
, h
);
1833 if (type
== GOT_TLS_GD
)
1835 bfd_vma ATTRIBUTE_UNUSED notneeded
=
1836 ADD_SYMBOL_REF_SEC_AND_RELOC (got
, TRUE
, h
);
1837 entries
= MOD_AND_OFF
;
1840 if (entries
== NONE
)
1843 new_got_entry_to_list (list
, type
, offset
, entries
);
1851 #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
1853 static struct plt_version_t
*
1854 arc_get_plt_version (struct bfd_link_info
*info
)
1858 for (i
= 0; i
< 1; i
++)
1860 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i
,
1861 plt_versions
[i
].entry_size
,
1862 plt_versions
[i
].elem_size
);
1865 if (bfd_get_mach (info
->output_bfd
) == bfd_mach_arc_arcv2
)
1867 if (bfd_link_pic (info
))
1868 return &(plt_versions
[ELF_ARCV2_PIC
]);
1870 return &(plt_versions
[ELF_ARCV2_ABS
]);
1874 if (bfd_link_pic (info
))
1875 return &(plt_versions
[ELF_ARC_PIC
]);
1877 return &(plt_versions
[ELF_ARC_ABS
]);
1882 add_symbol_to_plt (struct bfd_link_info
*info
)
1884 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1887 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
1889 /* If this is the first .plt entry, make room for the special first
1891 if (htab
->splt
->size
== 0)
1892 htab
->splt
->size
+= plt_data
->entry_size
;
1894 ret
= htab
->splt
->size
;
1896 htab
->splt
->size
+= plt_data
->elem_size
;
1897 ARC_DEBUG ("PLT_SIZE = %d\n", htab
->splt
->size
);
1899 htab
->sgotplt
->size
+= 4;
1900 htab
->srelplt
->size
+= sizeof (Elf32_External_Rela
);
1905 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
1906 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
1909 plt_do_relocs_for_symbol (bfd
*abfd
,
1910 struct elf_link_hash_table
*htab
,
1911 const struct plt_reloc
*reloc
,
1913 bfd_vma symbol_got_offset
)
1915 while (SYM_ONLY (reloc
->symbol
) != LAST_RELOC
)
1917 bfd_vma relocation
= 0;
1919 switch (SYM_ONLY (reloc
->symbol
))
1923 htab
->sgotplt
->output_section
->vma
+
1924 htab
->sgotplt
->output_offset
+ symbol_got_offset
;
1927 relocation
+= reloc
->addend
;
1929 if (IS_RELATIVE (reloc
->symbol
))
1931 bfd_vma reloc_offset
= reloc
->offset
;
1932 reloc_offset
-= (IS_INSN_32 (reloc
->symbol
)) ? 4 : 0;
1933 reloc_offset
-= (IS_INSN_24 (reloc
->symbol
)) ? 2 : 0;
1935 relocation
-= htab
->splt
->output_section
->vma
1936 + htab
->splt
->output_offset
1937 + plt_offset
+ reloc_offset
;
1940 /* TODO: being ME is not a property of the relocation but of the
1941 section of which is applying the relocation. */
1942 if (IS_MIDDLE_ENDIAN (reloc
->symbol
) || bfd_big_endian (abfd
))
1945 ((relocation
& 0xffff0000) >> 16) |
1946 ((relocation
& 0xffff) << 16);
1949 switch (reloc
->size
)
1952 bfd_put_32 (htab
->splt
->output_section
->owner
,
1954 htab
->splt
->contents
+ plt_offset
+ reloc
->offset
);
1958 reloc
= &(reloc
[1]); /* Jump to next relocation. */
1963 relocate_plt_for_symbol (bfd
*output_bfd
,
1964 struct bfd_link_info
*info
,
1965 struct elf_link_hash_entry
*h
)
1967 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
1968 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1970 bfd_vma plt_index
= (h
->plt
.offset
- plt_data
->entry_size
)
1971 / plt_data
->elem_size
;
1972 bfd_vma got_offset
= (plt_index
+ 3) * 4;
1974 ARC_DEBUG ("arc_info: PLT_OFFSET = 0x%x, PLT_ENTRY_VMA = 0x%x, \
1975 GOT_ENTRY_OFFSET = 0x%x, GOT_ENTRY_VMA = 0x%x, for symbol %s\n",
1977 htab
->splt
->output_section
->vma
1978 + htab
->splt
->output_offset
1981 htab
->sgotplt
->output_section
->vma
1982 + htab
->sgotplt
->output_offset
1984 h
->root
.root
.string
);
1986 memcpy (htab
->splt
->contents
+ h
->plt
.offset
,
1988 plt_data
->elem_size
);
1989 plt_do_relocs_for_symbol (output_bfd
, htab
,
1990 plt_data
->elem_relocs
,
1994 /* Fill in the entry in the global offset table. */
1995 bfd_put_32 (output_bfd
,
1996 (bfd_vma
) (htab
->splt
->output_section
->vma
1997 + htab
->splt
->output_offset
),
1998 htab
->sgotplt
->contents
+ got_offset
);
2000 /* TODO: Fill in the entry in the .rela.plt section. */
2002 Elf_Internal_Rela rel
;
2005 rel
.r_offset
= (htab
->sgotplt
->output_section
->vma
2006 + htab
->sgotplt
->output_offset
2010 BFD_ASSERT (h
->dynindx
!= -1);
2011 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_ARC_JMP_SLOT
);
2013 loc
= htab
->srelplt
->contents
;
2014 loc
+= plt_index
* sizeof (Elf32_External_Rela
); /* relA */
2015 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
2020 relocate_plt_for_entry (bfd
*abfd
,
2021 struct bfd_link_info
*info
)
2023 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
2024 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2026 memcpy (htab
->splt
->contents
, plt_data
->entry
,
2027 plt_data
->entry_size
);
2028 PLT_DO_RELOCS_FOR_ENTRY (abfd
, htab
, plt_data
->entry_relocs
);
2031 /* Desc : Adjust a symbol defined by a dynamic object and referenced
2032 by a regular object. The current definition is in some section of
2033 the dynamic object, but we're not including those sections. We
2034 have to change the definition to something the rest of the link can
2038 elf_arc_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2039 struct elf_link_hash_entry
*h
)
2042 unsigned int power_of_two
;
2043 bfd
*dynobj
= (elf_hash_table (info
))->dynobj
;
2044 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2046 if (h
->type
== STT_FUNC
2047 || h
->type
== STT_GNU_IFUNC
2048 || h
->needs_plt
== 1)
2050 if (!bfd_link_pic (info
) && !h
->def_dynamic
&& !h
->ref_dynamic
)
2052 /* This case can occur if we saw a PLT32 reloc in an input
2053 file, but the symbol was never referred to by a dynamic
2054 object. In such a case, we don't actually need to build
2055 a procedure linkage table, and we can just do a PC32
2057 BFD_ASSERT (h
->needs_plt
);
2061 /* Make sure this symbol is output as a dynamic symbol. */
2062 if (h
->dynindx
== -1 && !h
->forced_local
2063 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
2066 if (bfd_link_pic (info
)
2067 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2069 bfd_vma loc
= add_symbol_to_plt (info
);
2071 if (!bfd_link_pic (info
) && !h
->def_regular
)
2073 h
->root
.u
.def
.section
= htab
->splt
;
2074 h
->root
.u
.def
.value
= loc
;
2076 h
->plt
.offset
= loc
;
2080 h
->plt
.offset
= (bfd_vma
) -1;
2086 /* If this is a weak symbol, and there is a real definition, the
2087 processor independent code will have arranged for us to see the
2088 real definition first, and we can just use the same value. */
2089 if (h
->u
.weakdef
!= NULL
)
2091 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
2092 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
2093 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
2094 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
2098 /* If there are no non-GOT references, we do not need a copy
2100 if (!h
->non_got_ref
)
2103 /* This is a reference to a symbol defined by a dynamic object which
2104 is not a function. */
2106 /* If we are creating a shared library, we must presume that the
2107 only references to the symbol are via the global offset table.
2108 For such cases we need not do anything here; the relocations will
2109 be handled correctly by relocate_section. */
2110 if (bfd_link_pic (info
))
2113 /* We must allocate the symbol in our .dynbss section, which will
2114 become part of the .bss section of the executable. There will be
2115 an entry for this symbol in the .dynsym section. The dynamic
2116 object will contain position independent code, so all references
2117 from the dynamic object to this symbol will go through the global
2118 offset table. The dynamic linker will use the .dynsym entry to
2119 determine the address it must put in the global offset table, so
2120 both the dynamic object and the regular object will refer to the
2121 same memory location for the variable. */
2123 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
2124 BFD_ASSERT (s
!= NULL
);
2126 /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
2127 copy the initial value out of the dynamic object and into the
2128 runtime process image. We need to remember the offset into the
2129 .rela.bss section we are going to use. */
2130 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
2134 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
2135 BFD_ASSERT (srel
!= NULL
);
2136 srel
->size
+= sizeof (Elf32_External_Rela
);
2140 /* We need to figure out the alignment required for this symbol. I
2141 have no idea how ELF linkers handle this. */
2142 power_of_two
= bfd_log2 (h
->size
);
2143 if (power_of_two
> 3)
2146 /* Apply the required alignment. */
2147 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
) (1 << power_of_two
));
2148 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
2150 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
2154 /* Define the symbol as being at this point in the section. */
2155 h
->root
.u
.def
.section
= s
;
2156 h
->root
.u
.def
.value
= s
->size
;
2158 /* Increment the section size to make room for the symbol. */
2164 /* Function : elf_arc_finish_dynamic_symbol
2165 Brief : Finish up dynamic symbol handling. We set the
2166 contents of various dynamic sections here.
2171 Returns : True/False as the return status. */
2174 elf_arc_finish_dynamic_symbol (bfd
* output_bfd
,
2175 struct bfd_link_info
*info
,
2176 struct elf_link_hash_entry
*h
,
2177 Elf_Internal_Sym
* sym
)
2179 if (h
->plt
.offset
!= (bfd_vma
) -1)
2181 relocate_plt_for_symbol (output_bfd
, info
, h
);
2183 if (!h
->def_regular
)
2185 /* Mark the symbol as undefined, rather than as defined in
2186 the .plt section. Leave the value alone. */
2187 sym
->st_shndx
= SHN_UNDEF
;
2191 if (h
->got
.glist
!= NULL
)
2193 struct got_entry
*list
= h
->got
.glist
;
2195 /* Traverse the list of got entries for this symbol. */
2198 bfd_vma got_offset
= h
->got
.glist
->offset
;
2200 if (list
->type
== GOT_NORMAL
2201 && list
->created_dyn_relocation
== FALSE
)
2203 if (bfd_link_pic (info
)
2204 && (info
->symbolic
|| h
->dynindx
== -1)
2207 ADD_RELA (output_bfd
, got
, got_offset
, 0, R_ARC_RELATIVE
, 0);
2209 /* Do not fully understand the side effects of this condition.
2210 The relocation space might still being reserved. Perhaps
2211 I should clear its value. */
2212 else if (h
->dynindx
!= -1)
2214 ADD_RELA (output_bfd
, got
, got_offset
, h
->dynindx
,
2217 list
->created_dyn_relocation
= TRUE
;
2219 else if (list
->existing_entries
!= NONE
)
2221 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2222 enum tls_got_entries e
= list
->existing_entries
;
2224 BFD_ASSERT (list
->type
!= GOT_TLS_GD
2225 || list
->existing_entries
== MOD_AND_OFF
);
2227 bfd_vma dynindx
= h
->dynindx
== -1 ? 0 : h
->dynindx
;
2228 if (e
== MOD_AND_OFF
|| e
== MOD
)
2230 ADD_RELA (output_bfd
, got
, got_offset
, dynindx
,
2231 R_ARC_TLS_DTPMOD
, 0);
2232 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2233 GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2236 htab
->sgot
->output_section
->vma
2237 + htab
->sgot
->output_offset
+ got_offset
,
2240 if (e
== MOD_AND_OFF
|| e
== OFF
)
2243 if (list
->type
== GOT_TLS_IE
)
2244 addend
= bfd_get_32 (output_bfd
,
2245 htab
->sgot
->contents
+ got_offset
);
2247 ADD_RELA (output_bfd
, got
,
2248 got_offset
+ (e
== MOD_AND_OFF
? 4 : 0),
2250 (list
->type
== GOT_TLS_IE
?
2251 R_ARC_TLS_TPOFF
: R_ARC_TLS_DTPOFF
),
2254 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2255 GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2258 htab
->sgot
->output_section
->vma
2259 + htab
->sgot
->output_offset
+ got_offset
,
2267 h
->got
.glist
= NULL
;
2272 bfd_vma rel_offset
= (h
->root
.u
.def
.value
2273 + h
->root
.u
.def
.section
->output_section
->vma
2274 + h
->root
.u
.def
.section
->output_offset
);
2277 bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
2280 bfd_byte
* loc
= srelbss
->contents
2281 + (srelbss
->reloc_count
* sizeof (Elf32_External_Rela
));
2282 srelbss
->reloc_count
++;
2284 Elf_Internal_Rela rel
;
2286 rel
.r_offset
= rel_offset
;
2288 BFD_ASSERT (h
->dynindx
!= -1);
2289 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_ARC_COPY
);
2291 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
2294 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2295 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
2296 || strcmp (h
->root
.root
.string
, "__DYNAMIC") == 0
2297 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
2298 sym
->st_shndx
= SHN_ABS
;
2303 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION, ASSERT) \
2305 if (SYMBOL != NULL) \
2307 h = elf_link_hash_lookup (elf_hash_table (info), \
2308 SYMBOL, FALSE, FALSE, TRUE); \
2310 else if (SECTION != NULL) \
2312 s = bfd_get_section_by_name (output_bfd, SECTION); \
2313 BFD_ASSERT (s != NULL || !ASSERT); \
2318 /* Function : elf_arc_finish_dynamic_sections
2319 Brief : Finish up the dynamic sections handling.
2324 Returns : True/False as the return status. */
2327 elf_arc_finish_dynamic_sections (bfd
* output_bfd
,
2328 struct bfd_link_info
*info
)
2330 struct dynamic_sections ds
= arc_create_dynamic_sections (output_bfd
, info
);
2331 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2332 bfd
*dynobj
= (elf_hash_table (info
))->dynobj
;
2336 Elf32_External_Dyn
*dyncon
, *dynconend
;
2338 dyncon
= (Elf32_External_Dyn
*) ds
.sdyn
->contents
;
2340 (Elf32_External_Dyn
*) (ds
.sdyn
->contents
+ ds
.sdyn
->size
);
2341 for (; dyncon
< dynconend
; dyncon
++)
2343 Elf_Internal_Dyn internal_dyn
;
2344 bfd_boolean do_it
= FALSE
;
2346 struct elf_link_hash_entry
*h
= NULL
;
2349 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &internal_dyn
);
2351 switch (internal_dyn
.d_tag
)
2353 GET_SYMBOL_OR_SECTION (DT_INIT
, "_init", NULL
, TRUE
)
2354 GET_SYMBOL_OR_SECTION (DT_FINI
, "_fini", NULL
, TRUE
)
2355 GET_SYMBOL_OR_SECTION (DT_PLTGOT
, NULL
, ".plt", TRUE
)
2356 GET_SYMBOL_OR_SECTION (DT_JMPREL
, NULL
, ".rela.plt", TRUE
)
2357 GET_SYMBOL_OR_SECTION (DT_PLTRELSZ
, NULL
, ".rela.plt", TRUE
)
2358 GET_SYMBOL_OR_SECTION (DT_RELASZ
, NULL
, ".rela.plt", FALSE
)
2359 GET_SYMBOL_OR_SECTION (DT_VERSYM
, NULL
, ".gnu.version", TRUE
)
2360 GET_SYMBOL_OR_SECTION (DT_VERDEF
, NULL
, ".gnu.version_d", TRUE
)
2361 GET_SYMBOL_OR_SECTION (DT_VERNEED
, NULL
, ".gnu.version_r", TRUE
)
2366 /* In case the dynamic symbols should be updated with a symbol. */
2368 && (h
->root
.type
== bfd_link_hash_defined
2369 || h
->root
.type
== bfd_link_hash_defweak
))
2373 internal_dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
2374 asec_ptr
= h
->root
.u
.def
.section
;
2375 if (asec_ptr
->output_section
!= NULL
)
2377 internal_dyn
.d_un
.d_val
+=
2378 (asec_ptr
->output_section
->vma
+
2379 asec_ptr
->output_offset
);
2383 /* The symbol is imported from another shared
2384 library and does not apply to this one. */
2385 internal_dyn
.d_un
.d_val
= 0;
2389 else if (s
!= NULL
) /* With a section information. */
2391 switch (internal_dyn
.d_tag
)
2398 internal_dyn
.d_un
.d_ptr
= s
->vma
;
2403 internal_dyn
.d_un
.d_val
= s
->size
;
2409 internal_dyn
.d_un
.d_val
-= s
->size
;
2419 bfd_elf32_swap_dyn_out (output_bfd
, &internal_dyn
, dyncon
);
2422 if (htab
->splt
->size
> 0)
2424 relocate_plt_for_entry (output_bfd
, info
);
2427 /* TODO: Validate this. */
2428 elf_section_data (htab
->srelplt
->output_section
)->this_hdr
.sh_entsize
2432 /* Fill in the first three entries in the global offset table. */
2435 if (htab
->sgot
->size
> 0 || htab
->sgotplt
->size
> 0)
2437 if (ds
.sdyn
== NULL
)
2438 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
2439 htab
->sgotplt
->contents
);
2441 bfd_put_32 (output_bfd
,
2442 ds
.sdyn
->output_section
->vma
+ ds
.sdyn
->output_offset
,
2443 htab
->sgotplt
->contents
);
2444 bfd_put_32 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ 4);
2445 bfd_put_32 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ 8);
2452 #define ADD_DYNAMIC_SYMBOL(NAME, TAG) \
2453 h = elf_link_hash_lookup (elf_hash_table (info), \
2454 NAME, FALSE, FALSE, FALSE); \
2455 if ((h != NULL && (h->ref_regular || h->def_regular))) \
2456 if (! _bfd_elf_add_dynamic_entry (info, TAG, 0)) \
2459 /* Set the sizes of the dynamic sections. */
2461 elf_arc_size_dynamic_sections (bfd
* output_bfd
,
2462 struct bfd_link_info
*info
)
2466 bfd_boolean relocs_exist
= FALSE
;
2467 bfd_boolean reltext_exist
= FALSE
;
2468 struct dynamic_sections ds
= arc_create_dynamic_sections (output_bfd
, info
);
2469 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2471 dynobj
= (elf_hash_table (info
))->dynobj
;
2472 BFD_ASSERT (dynobj
!= NULL
);
2474 if ((elf_hash_table (info
))->dynamic_sections_created
)
2476 struct elf_link_hash_entry
*h
;
2478 /* Set the contents of the .interp section to the
2480 if (!bfd_link_pic (info
))
2482 s
= bfd_get_section_by_name (dynobj
, ".interp");
2483 BFD_ASSERT (s
!= NULL
);
2484 s
->size
= sizeof (ELF_DYNAMIC_INTERPRETER
);
2485 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2488 /* Add some entries to the .dynamic section. We fill in some of
2489 the values later, in elf_bfd_final_link, but we must add the
2490 entries now so that we know the final size of the .dynamic
2491 section. Checking if the .init section is present. We also
2492 create DT_INIT and DT_FINI entries if the init_str has been
2493 changed by the user. */
2494 ADD_DYNAMIC_SYMBOL ("init", DT_INIT
);
2495 ADD_DYNAMIC_SYMBOL ("fini", DT_FINI
);
2499 /* We may have created entries in the .rela.got section.
2500 However, if we are not creating the dynamic sections, we will
2501 not actually use these entries. Reset the size of .rela.got,
2502 which will cause it to get stripped from the output file
2504 if (htab
->srelgot
!= NULL
)
2505 htab
->srelgot
->size
= 0;
2508 if (htab
->splt
!= NULL
&& htab
->splt
->size
== 0)
2509 htab
->splt
->flags
|= SEC_EXCLUDE
;
2510 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2512 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2515 if (strncmp (s
->name
, ".rela", 5) == 0)
2519 s
->flags
|= SEC_EXCLUDE
;
2523 if (strcmp (s
->name
, ".rela.plt") != 0)
2525 const char *outname
=
2526 bfd_get_section_name (output_bfd
,
2527 htab
->srelplt
->output_section
);
2529 asection
*target
= bfd_get_section_by_name (output_bfd
,
2532 relocs_exist
= TRUE
;
2533 if (target
!= NULL
&& target
->size
!= 0
2534 && (target
->flags
& SEC_READONLY
) != 0
2535 && (target
->flags
& SEC_ALLOC
) != 0)
2536 reltext_exist
= TRUE
;
2540 /* We use the reloc_count field as a counter if we need to
2541 copy relocs into the output file. */
2545 if (strcmp (s
->name
, ".dynamic") == 0)
2549 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
2551 if (s
->contents
== NULL
&& s
->size
!= 0)
2557 /* TODO: Check if this is needed. */
2558 if (!bfd_link_pic (info
))
2559 if (!_bfd_elf_add_dynamic_entry (info
, DT_DEBUG
, 0))
2562 if (htab
->splt
&& (htab
->splt
->flags
& SEC_EXCLUDE
) == 0)
2563 if (!_bfd_elf_add_dynamic_entry (info
, DT_PLTGOT
, 0)
2564 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
2565 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
2566 || !_bfd_elf_add_dynamic_entry (info
, DT_JMPREL
, 0)
2570 if (relocs_exist
== TRUE
)
2571 if (!_bfd_elf_add_dynamic_entry (info
, DT_RELA
, 0)
2572 || !_bfd_elf_add_dynamic_entry (info
, DT_RELASZ
, 0)
2573 || !_bfd_elf_add_dynamic_entry (info
, DT_RELENT
,
2574 sizeof (Elf32_External_Rela
))
2578 if (reltext_exist
== TRUE
)
2579 if (!_bfd_elf_add_dynamic_entry (info
, DT_TEXTREL
, 0))
2586 const struct elf_size_info arc_elf32_size_info
=
2588 sizeof (Elf32_External_Ehdr
),
2589 sizeof (Elf32_External_Phdr
),
2590 sizeof (Elf32_External_Shdr
),
2591 sizeof (Elf32_External_Rel
),
2592 sizeof (Elf32_External_Rela
),
2593 sizeof (Elf32_External_Sym
),
2594 sizeof (Elf32_External_Dyn
),
2595 sizeof (Elf_External_Note
),
2599 ELFCLASS32
, EV_CURRENT
,
2600 bfd_elf32_write_out_phdrs
,
2601 bfd_elf32_write_shdrs_and_ehdr
,
2602 bfd_elf32_checksum_contents
,
2603 bfd_elf32_write_relocs
,
2604 bfd_elf32_swap_symbol_in
,
2605 bfd_elf32_swap_symbol_out
,
2606 bfd_elf32_slurp_reloc_table
,
2607 bfd_elf32_slurp_symbol_table
,
2608 bfd_elf32_swap_dyn_in
,
2609 bfd_elf32_swap_dyn_out
,
2610 bfd_elf32_swap_reloc_in
,
2611 bfd_elf32_swap_reloc_out
,
2612 bfd_elf32_swap_reloca_in
,
2613 bfd_elf32_swap_reloca_out
2616 #define elf_backend_size_info arc_elf32_size_info
2618 static struct bfd_link_hash_table
*
2619 arc_elf_link_hash_table_create (bfd
*abfd
)
2621 struct elf_link_hash_table
*htab
;
2623 htab
= bfd_zmalloc (sizeof (*htab
));
2627 if (!_bfd_elf_link_hash_table_init (htab
, abfd
,
2628 _bfd_elf_link_hash_newfunc
,
2629 sizeof (struct elf_link_hash_entry
),
2636 htab
->init_got_refcount
.refcount
= 0;
2637 htab
->init_got_refcount
.glist
= NULL
;
2638 htab
->init_got_offset
.offset
= 0;
2639 htab
->init_got_offset
.glist
= NULL
;
2640 return (struct bfd_link_hash_table
*) htab
;
2643 /* Hook called by the linker routine which adds symbols from an object
2647 elf_arc_add_symbol_hook (bfd
* abfd
,
2648 struct bfd_link_info
* info
,
2649 Elf_Internal_Sym
* sym
,
2650 const char ** namep ATTRIBUTE_UNUSED
,
2651 flagword
* flagsp ATTRIBUTE_UNUSED
,
2652 asection
** secp ATTRIBUTE_UNUSED
,
2653 bfd_vma
* valp ATTRIBUTE_UNUSED
)
2655 if ((ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
2656 || ELF_ST_BIND (sym
->st_info
) == STB_GNU_UNIQUE
)
2657 && (abfd
->flags
& DYNAMIC
) == 0
2658 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
2659 elf_tdata (info
->output_bfd
)->has_gnu_symbols
= elf_gnu_symbol_any
;
2664 #define TARGET_LITTLE_SYM arc_elf32_le_vec
2665 #define TARGET_LITTLE_NAME "elf32-littlearc"
2666 #define TARGET_BIG_SYM arc_elf32_be_vec
2667 #define TARGET_BIG_NAME "elf32-bigarc"
2668 #define ELF_ARCH bfd_arch_arc
2669 #define ELF_MACHINE_CODE EM_ARC_COMPACT
2670 #define ELF_MACHINE_ALT1 EM_ARC_COMPACT2
2671 #define ELF_MAXPAGESIZE 0x2000
2673 #define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create
2675 #define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data
2676 #define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup
2677 #define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags
2678 #define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data
2679 #define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data
2681 #define elf_info_to_howto_rel arc_info_to_howto_rel
2682 #define elf_backend_object_p arc_elf_object_p
2683 #define elf_backend_final_write_processing arc_elf_final_write_processing
2685 #define elf_backend_relocate_section elf_arc_relocate_section
2686 #define elf_backend_check_relocs elf_arc_check_relocs
2687 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2689 #define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol
2690 #define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol
2692 #define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections
2693 #define elf_backend_size_dynamic_sections elf_arc_size_dynamic_sections
2694 #define elf_backend_add_symbol_hook elf_arc_add_symbol_hook
2696 #define elf_backend_can_gc_sections 1
2697 #define elf_backend_want_got_plt 1
2698 #define elf_backend_plt_readonly 1
2699 #define elf_backend_rela_plts_and_copies_p 1
2700 #define elf_backend_want_plt_sym 0
2701 #define elf_backend_got_header_size 12
2703 #define elf_backend_may_use_rel_p 0
2704 #define elf_backend_may_use_rela_p 1
2705 #define elf_backend_default_use_rela_p 1
2707 #include "elf32-target.h"