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"
29 #include "opcode/arc.h"
32 /* #define ARC_ENABLE_DEBUG 1 */
33 #ifdef ARC_ENABLE_DEBUG
35 name_for_global_symbol (struct elf_link_hash_entry
*h
)
37 static char *local_str
= "(local)";
40 return h
->root
.root
.string
;
42 #define ARC_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
44 #define ARC_DEBUG(...)
48 #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND) \
50 struct elf_link_hash_table *_htab = elf_hash_table (info); \
51 Elf_Internal_Rela _rel; \
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); \
67 struct dynamic_sections
69 bfd_boolean initialized
;
73 asection
* srelgotplt
;
79 enum dyn_section_types
90 const char * dyn_section_names
[DYN_SECTION_TYPES_END
] =
101 /* The default symbols representing the init and fini dyn values.
102 TODO: Check what is the relation of those strings with arclinux.em
104 #define INIT_SYM_STRING "_init"
105 #define FINI_SYM_STRING "_fini"
107 char * init_str
= INIT_SYM_STRING
;
108 char * fini_str
= FINI_SYM_STRING
;
110 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
115 static ATTRIBUTE_UNUSED
const char *
116 reloc_type_to_name (unsigned int type
)
120 #include "elf/arc-reloc.def"
127 #undef ARC_RELOC_HOWTO
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). */
134 static ATTRIBUTE_UNUSED bfd_boolean
135 is_reloc_PC_relative (reloc_howto_type
*howto
)
137 return (strstr (howto
->name
, "PC") != NULL
) ? TRUE
: FALSE
;
141 is_reloc_SDA_relative (reloc_howto_type
*howto
)
143 return (strstr (howto
->name
, "SDA") != NULL
) ? TRUE
: FALSE
;
147 is_reloc_for_GOT (reloc_howto_type
* howto
)
149 if (strstr (howto
->name
, "TLS") != NULL
)
151 return (strstr (howto
->name
, "GOT") != NULL
) ? TRUE
: FALSE
;
155 is_reloc_for_PLT (reloc_howto_type
* howto
)
157 return (strstr (howto
->name
, "PLT") != NULL
) ? TRUE
: FALSE
;
161 is_reloc_for_TLS (reloc_howto_type
*howto
)
163 return (strstr (howto
->name
, "TLS") != NULL
) ? TRUE
: FALSE
;
166 struct arc_relocation_data
168 bfd_signed_vma reloc_offset
;
169 bfd_signed_vma reloc_addend
;
170 bfd_signed_vma got_offset_value
;
172 bfd_signed_vma sym_value
;
173 asection
* sym_section
;
175 reloc_howto_type
*howto
;
177 asection
* input_section
;
179 bfd_signed_vma sdata_begin_symbol_vma
;
180 bfd_boolean sdata_begin_symbol_vma_set
;
181 bfd_signed_vma got_symbol_vma
;
183 bfd_boolean should_relocate
;
185 const char * symbol_name
;
188 /* Should be included at this location due to static declarations
189 * defined before this point. */
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)
200 static bfd_reloc_status_type
201 arc_elf_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
202 arelent
*reloc_entry
,
204 void *data ATTRIBUTE_UNUSED
,
205 asection
*input_section
,
207 char ** error_message ATTRIBUTE_UNUSED
)
209 if (output_bfd
!= NULL
)
211 reloc_entry
->address
+= input_section
->output_offset
;
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
;
222 return bfd_reloc_continue
;
226 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
230 #include "elf/arc-reloc.def"
233 #undef ARC_RELOC_HOWTO
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),
240 static struct reloc_howto_struct elf_arc_howto_table
[] =
242 #include "elf/arc-reloc.def"
243 /* Example of what is generated by the preprocessor. Currently kept as an
245 HOWTO (R_ARC_NONE, // Type.
247 2, // Size (0 = byte, 1 = short, 2 = long).
249 FALSE, // PC_relative.
251 complain_overflow_bitfield, // Complain_on_overflow.
252 bfd_elf_generic_reloc, // Special_function.
253 "R_ARC_NONE", // Name.
254 TRUE, // Partial_inplace.
257 FALSE), // PCrel_offset.
260 #undef ARC_RELOC_HOWTO
262 static void arc_elf_howto_init (void)
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) \
271 BFD_ASSERT (SIZE == 2); \
274 #include "elf/arc-reloc.def"
277 #undef ARC_RELOC_HOWTO
280 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
282 const int howto_table_lookup
[] =
284 #include "elf/arc-reloc.def"
286 #undef ARC_RELOC_HOWTO
288 static reloc_howto_type
*
289 arc_elf_howto (unsigned int r_type
)
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
];
296 /* Map BFD reloc types to ARC ELF reloc types. */
300 bfd_reloc_code_real_type bfd_reloc_val
;
301 unsigned char elf_reloc_val
;
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
[] =
308 #include "elf/arc-reloc.def"
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
},
316 #undef ARC_RELOC_HOWTO
318 typedef ATTRIBUTE_UNUSED
bfd_vma (*replace_func
) (unsigned, int ATTRIBUTE_UNUSED
);
320 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
322 func = (void *) RELOC_FUNCTION; \
325 get_replace_function (bfd
*abfd
, unsigned int r_type
)
331 #include "elf/arc-reloc.def"
334 if (func
== replace_bits24
&& bfd_big_endian (abfd
))
335 return (replace_func
) replace_bits24_be
;
337 return (replace_func
) func
;
339 #undef ARC_RELOC_HOWTO
341 static reloc_howto_type
*
342 arc_elf32_bfd_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
343 bfd_reloc_code_real_type code
)
347 for (i
= ARRAY_SIZE (arc_reloc_map
); i
--;)
349 if (arc_reloc_map
[i
].bfd_reloc_val
== code
)
350 return arc_elf_howto (arc_reloc_map
[i
].elf_reloc_val
);
356 /* Function to set the ELF flag bits. */
358 arc_elf_set_private_flags (bfd
*abfd
, flagword flags
)
360 elf_elfheader (abfd
)->e_flags
= flags
;
361 elf_flags_init (abfd
) = TRUE
;
365 /* Print private flags. */
367 arc_elf_print_private_bfd_data (bfd
*abfd
, void * ptr
)
369 FILE *file
= (FILE *) ptr
;
372 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
374 /* Print normal ELF private data. */
375 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
377 flags
= elf_elfheader (abfd
)->e_flags
;
378 fprintf (file
, _("private flags = 0x%lx:"), (unsigned long) flags
);
380 switch (flags
& EF_ARC_MACH_MSK
)
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;
388 fprintf (file
, "-mcpu=unknown");
392 switch (flags
& EF_ARC_OSABI_MSK
)
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;
398 fprintf (file
, "(ABI:unknown)");
406 /* Copy backend specific data from one object module to another. */
409 arc_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
411 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
412 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
415 BFD_ASSERT (!elf_flags_init (obfd
)
416 || elf_elfheader (obfd
)->e_flags
== elf_elfheader (ibfd
)->e_flags
);
418 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
419 elf_flags_init (obfd
) = TRUE
;
421 /* Copy object attributes. */
422 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
424 return _bfd_elf_copy_private_bfd_data (ibfd
, obfd
);
427 static reloc_howto_type
*
428 bfd_elf32_bfd_reloc_name_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
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
);
441 /* Set the howto pointer for an ARC ELF reloc. */
444 arc_info_to_howto_rel (bfd
* abfd ATTRIBUTE_UNUSED
,
446 Elf_Internal_Rela
* dst
)
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
);
455 /* Merge backend specific data from an object file to the output
456 object file when linking. */
459 arc_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
461 unsigned short mach_ibfd
;
462 static unsigned short mach_obfd
= EM_NONE
;
467 /* Check if we have the same endianess. */
468 if (! _bfd_generic_verify_endian_match (ibfd
, obfd
))
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
));
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
;
480 if (!elf_flags_init (obfd
)) /* First call, no flags set. */
482 elf_flags_init (obfd
) = TRUE
;
483 out_flags
= in_flags
;
486 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
487 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
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
))
495 bfd_boolean null_input_bfd
= TRUE
;
496 bfd_boolean only_data_sections
= TRUE
;
498 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
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
;
505 null_input_bfd
= FALSE
;
508 if (null_input_bfd
|| only_data_sections
)
512 /* Complain about various flag/architecture mismatches. */
513 mach_ibfd
= elf_elfheader (ibfd
)->e_machine
;
514 if (mach_obfd
== EM_NONE
)
516 mach_obfd
= mach_ibfd
;
520 if (mach_ibfd
!= mach_obfd
)
522 _bfd_error_handler (_("ERROR: Attempting to link %B "
523 "with a binary %s of different architecture"),
524 ibfd
, bfd_get_filename (obfd
));
527 else if (in_flags
!= out_flags
)
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
)
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
;
542 /* Update the flags. */
543 elf_elfheader (obfd
)->e_flags
= in_flags
;
545 if (bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
547 return bfd_set_arch_mach (obfd
, bfd_arch_arc
, bfd_get_mach (ibfd
));
553 /* Set the right machine number for an ARC ELF file. */
555 arc_elf_object_p (bfd
* abfd
)
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
;
564 if (e_machine
== EM_ARC_COMPACT
|| e_machine
== EM_ARC_COMPACT2
)
568 case E_ARC_MACH_ARC600
:
569 mach
= bfd_mach_arc_arc600
;
571 case E_ARC_MACH_ARC601
:
572 mach
= bfd_mach_arc_arc601
;
574 case E_ARC_MACH_ARC700
:
575 mach
= bfd_mach_arc_arc700
;
577 case EF_ARC_CPU_ARCV2HS
:
578 case EF_ARC_CPU_ARCV2EM
:
579 mach
= bfd_mach_arc_arcv2
;
582 mach
= (e_machine
== EM_ARC_COMPACT
)
583 ? bfd_mach_arc_arc700
: bfd_mach_arc_arcv2
;
589 if (e_machine
== EM_ARC
)
591 (*_bfd_error_handler
)
592 (_("Error: The ARC4 architecture is no longer supported.\n"));
597 (*_bfd_error_handler
)
598 (_("Warning: unset or old architecture flags. \n"
599 " Use default machine.\n"));
603 return bfd_default_set_arch_mach (abfd
, bfd_arch_arc
, mach
);
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. */
610 arc_elf_final_write_processing (bfd
* abfd
,
611 bfd_boolean linker ATTRIBUTE_UNUSED
)
615 switch (bfd_get_mach (abfd
))
617 case bfd_mach_arc_arc600
:
618 emf
= EM_ARC_COMPACT
;
620 case bfd_mach_arc_arc601
:
621 emf
= EM_ARC_COMPACT
;
623 case bfd_mach_arc_arc700
:
624 emf
= EM_ARC_COMPACT
;
626 case bfd_mach_arc_arcv2
:
627 emf
= EM_ARC_COMPACT2
;
633 elf_elfheader (abfd
)->e_machine
= emf
;
635 /* Record whatever is the current syscall ABI version. */
636 elf_elfheader (abfd
)->e_flags
|= E_ARC_OSABI_CURRENT
;
642 #ifdef ARC_ENABLE_DEBUG
643 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
646 debug_arc_reloc (struct arc_relocation_data reloc_data
)
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
)
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
));
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
);
672 ARC_DEBUG (" symbol section is NULL\n");
675 ARC_DEBUG (" Input_section:\n");
676 if (reloc_data
.input_section
!= NULL
)
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
);
690 ARC_DEBUG (" input section is NULL\n");
694 #define DEBUG_ARC_RELOC(A)
695 #endif /* ARC_ENABLE_DEBUG */
698 middle_endian_convert (bfd_vma insn
, bfd_boolean do_it
)
703 = ((insn
& 0xffff0000) >> 16)
704 | ((insn
& 0xffff) << 16);
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. */
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
)
718 switch (reloc_data
.howto
->type
)
720 case R_ARC_NPS_CMEM16
:
721 if (((relocation
>> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE
)
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
,
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
,
744 return bfd_reloc_overflow
;
755 #define ME(reloc) (reloc)
757 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
758 && (!bfd_big_endian (BFD)))
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)
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)
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) ( \
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))) \
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)
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)
799 #ifdef ARC_ENABLE_DEBUG
800 #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
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); \
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); \
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); \
835 #define PRINT_DEBUG_RELOC_INFO_AFTER \
838 ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \
844 #define PRINT_DEBUG_RELOC_INFO_BEFORE(...)
845 #define PRINT_DEBUG_RELOC_INFO_AFTER
847 #endif /* ARC_ENABLE_DEBUG */
849 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
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; \
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
)
867 bfd_signed_vma relocation
= 0;
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
;
874 if (reloc_data
.should_relocate
== FALSE
)
877 switch (reloc_data
.howto
->size
)
880 insn
= arc_bfd_get_32 (abfd
,
881 contents
+ reloc_data
.reloc_offset
,
882 reloc_data
.input_section
);
885 insn
= arc_bfd_get_16 (abfd
,
886 contents
+ reloc_data
.reloc_offset
,
887 reloc_data
.input_section
);
890 insn
= arc_bfd_get_8 (abfd
,
891 contents
+ reloc_data
.reloc_offset
,
892 reloc_data
.input_section
);
902 switch (reloc_data
.howto
->type
)
904 #include "elf/arc-reloc.def"
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
),
919 flag
= arc_special_overflow_checks (reloc_data
, relocation
, info
);
921 if (flag
!= bfd_reloc_ok
)
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
);
932 /* Write updated instruction back to memory. */
933 switch (reloc_data
.howto
->size
)
936 arc_bfd_put_32 (abfd
, insn
,
937 contents
+ reloc_data
.reloc_offset
,
938 reloc_data
.input_section
);
941 arc_bfd_put_16 (abfd
, insn
,
942 contents
+ reloc_data
.reloc_offset
,
943 reloc_data
.input_section
);
946 arc_bfd_put_8 (abfd
, insn
,
947 contents
+ reloc_data
.reloc_offset
,
948 reloc_data
.input_section
);
951 ARC_DEBUG ("size = %d\n", reloc_data
.howto
->size
);
971 #undef ARC_RELOC_HOWTO
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
989 elf_arc_relocate_section (bfd
* output_bfd
,
990 struct bfd_link_info
* info
,
992 asection
* input_section
,
994 Elf_Internal_Rela
* relocs
,
995 Elf_Internal_Sym
* local_syms
,
996 asection
** local_sections
)
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
);
1005 symtab_hdr
= &((elf_tdata (input_bfd
))->symtab_hdr
);
1006 sym_hashes
= elf_sym_hashes (input_bfd
);
1008 rel
= wrel
= relocs
;
1009 relend
= relocs
+ input_section
->reloc_count
;
1010 for (; rel
< relend
; wrel
++, rel
++)
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
;
1018 struct elf_link_hash_entry
* h2
;
1021 struct arc_relocation_data reloc_data
=
1025 .got_offset_value
= 0,
1027 .sym_section
= 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
1036 r_type
= ELF32_R_TYPE (rel
->r_info
);
1038 if (r_type
>= (int) R_ARC_max
)
1040 bfd_set_error (bfd_error_bad_value
);
1043 howto
= arc_elf_howto (r_type
);
1045 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1047 /* If we are generating another .o file and the symbol in not
1048 local, skip this relocation. */
1049 if (bfd_link_relocatable (info
))
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. */
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
)
1060 sym
= local_syms
+ r_symndx
;
1061 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1063 sec
= local_sections
[r_symndx
];
1065 /* For RELA relocs. Just adjust the addend
1066 value in the relocation entry. */
1067 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
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__
);
1076 h2
= elf_link_hash_lookup (elf_hash_table (info
), "__SDATA_BEGIN__",
1077 FALSE
, FALSE
, TRUE
);
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. */
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
;
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
;
1095 /* This is a final link. */
1100 if (r_symndx
< symtab_hdr
->sh_info
) /* A local symbol. */
1102 sym
= local_syms
+ r_symndx
;
1103 sec
= local_sections
[r_symndx
];
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. */
1112 /* Get the symbol's entry in the symtab. */
1113 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
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
;
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
)
1123 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1124 sec
= h
->root
.u
.def
.section
;
1128 /* Clean relocs for symbols in discarded sections. */
1129 if (sec
!= NULL
&& discarded_section (sec
))
1131 _bfd_clear_contents (howto
, input_bfd
, input_section
,
1132 contents
+ rel
->r_offset
);
1133 rel
->r_offset
= rel
->r_offset
;
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
))
1147 if (bfd_link_relocatable (info
))
1154 if (r_symndx
< symtab_hdr
->sh_info
) /* A local symbol. */
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
,
1163 /* Mergeable section handling. */
1164 if ((sec
->flags
& SEC_MERGE
)
1165 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
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
1174 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
1176 reloc_data
.reloc_addend
= rel
->r_addend
;
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
;
1184 reloc_data
.should_relocate
= TRUE
;
1186 else /* Global symbol. */
1188 /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro
1189 (defined in elf-bfd.h) here. */
1191 /* Get the symbol's entry in the symtab. */
1192 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
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
;
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
;
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
)
1206 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1207 reloc_data
.sym_section
= h
->root
.u
.def
.section
;
1209 reloc_data
.should_relocate
= TRUE
;
1211 if (is_reloc_for_GOT (howto
) && !bfd_link_pic (info
))
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
)
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
);
1227 if (is_reloc_for_PLT (howto
) && h
->plt
.offset
!= (bfd_vma
) -1)
1229 /* TODO: This is repeated up here. */
1230 reloc_data
.sym_value
= h
->plt
.offset
;
1231 reloc_data
.sym_section
= htab
->splt
;
1234 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1236 /* Is weak symbol and has no definition. */
1237 if (is_reloc_for_GOT (howto
))
1239 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1240 reloc_data
.sym_section
= htab
->sgot
;
1241 reloc_data
.should_relocate
= TRUE
;
1243 else if (is_reloc_for_PLT (howto
)
1244 && h
->plt
.offset
!= (bfd_vma
) -1)
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
;
1256 if (is_reloc_for_GOT (howto
))
1258 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1259 reloc_data
.sym_section
= htab
->sgot
;
1261 reloc_data
.should_relocate
= TRUE
;
1263 else if (is_reloc_for_PLT (howto
))
1265 /* Fail if it is linking for PIE and the symbol is
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
;
1274 reloc_data
.should_relocate
= TRUE
;
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
);
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
;
1288 if ((is_reloc_for_GOT (howto
)
1289 || is_reloc_for_TLS (howto
)))
1291 struct got_entry
**list
1292 = get_got_entry_list_for_symbol (output_bfd
, r_symndx
, h
);
1294 reloc_data
.got_offset_value
1295 = relocate_fix_got_relocs_for_got_info (list
,
1296 tls_type_for_reloc (howto
),
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
);
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
)
1324 && (!info
->symbolic
|| !h
->def_regular
))))
1326 Elf_Internal_Rela outrel
;
1328 bfd_boolean skip
= FALSE
;
1329 bfd_boolean relocate
= FALSE
;
1330 asection
*sreloc
= _bfd_elf_get_dynamic_reloc_section
1331 (input_bfd
, input_section
,
1334 BFD_ASSERT (sreloc
!= NULL
);
1336 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
1340 if (outrel
.r_offset
== (bfd_vma
) -1)
1343 outrel
.r_addend
= rel
->r_addend
;
1344 outrel
.r_offset
+= (input_section
->output_section
->vma
1345 + input_section
->output_offset
);
1347 #define IS_ARC_PCREL_TYPE(TYPE) \
1348 ( (TYPE == R_ARC_PC32) \
1349 || (TYPE == R_ARC_32_PCREL))
1353 memset (&outrel
, 0, sizeof outrel
);
1358 && ((IS_ARC_PCREL_TYPE (r_type
))
1359 || !(bfd_link_executable (info
)
1360 || SYMBOLIC_BIND (info
, h
))
1361 || ! h
->def_regular
))
1363 BFD_ASSERT (h
!= NULL
);
1364 if ((input_section
->flags
& SEC_ALLOC
) != 0)
1369 BFD_ASSERT (h
->dynindx
!= -1);
1370 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
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. */
1385 /* outrel.r_addend = 0; */
1386 outrel
.r_info
= ELF32_R_INFO (0, R_ARC_RELATIVE
);
1389 BFD_ASSERT (sreloc
->contents
!= 0);
1391 loc
= sreloc
->contents
;
1392 loc
+= sreloc
->reloc_count
* sizeof (Elf32_External_Rela
);
1393 sreloc
->reloc_count
+= 1;
1395 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1397 if (relocate
== FALSE
)
1405 if (is_reloc_SDA_relative (howto
)
1406 && (reloc_data
.sdata_begin_symbol_vma_set
== FALSE
))
1408 (*_bfd_error_handler
)
1409 ("Error: Linker symbol __SDATA_BEGIN__ not found");
1410 bfd_set_error (bfd_error_bad_value
);
1414 DEBUG_ARC_RELOC (reloc_data
);
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
)
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
);
1428 switch (arc_do_relocation (contents
, reloc_data
, info
))
1431 continue; /* The reloc processing loop. */
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
);
1439 case bfd_reloc_undefined
:
1440 (*info
->callbacks
->undefined_symbol
)
1441 (info
, reloc_data
.symbol_name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1444 case bfd_reloc_other
:
1445 msg
= _("%B(%A): warning: unaligned access to symbol '%s' in the small data area");
1448 case bfd_reloc_outofrange
:
1449 msg
= _("%B(%A): internal error: out of range error");
1452 case bfd_reloc_notsupported
:
1453 msg
= _("%B(%A): internal error: unsupported relocation error");
1456 case bfd_reloc_dangerous
:
1457 msg
= _("%B(%A): internal error: dangerous relocation");
1461 msg
= _("%B(%A): internal error: unknown error");
1466 _bfd_error_handler (msg
, input_bfd
, input_section
, reloc_data
.symbol_name
);
1473 static struct dynamic_sections
1474 arc_create_dynamic_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1476 struct elf_link_hash_table
*htab
;
1478 struct dynamic_sections ds
=
1480 .initialized
= FALSE
,
1490 htab
= elf_hash_table (info
);
1493 /* Create dynamic sections for relocatable executables so that we
1494 can copy relocations. */
1495 if (! htab
->dynamic_sections_created
&& bfd_link_pic (info
))
1497 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
1501 dynobj
= (elf_hash_table (info
))->dynobj
;
1505 ds
.sgot
= htab
->sgot
;
1506 ds
.srelgot
= htab
->srelgot
;
1508 ds
.sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
1509 ds
.srelgotplt
= ds
.srelplt
;
1511 ds
.splt
= bfd_get_section_by_name (dynobj
, ".plt");
1512 ds
.srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
1515 if (htab
->dynamic_sections_created
)
1517 ds
.sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1520 ds
.initialized
= TRUE
;
1526 elf_arc_check_relocs (bfd
* abfd
,
1527 struct bfd_link_info
* info
,
1529 const Elf_Internal_Rela
* relocs
)
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
;
1536 asection
* sreloc
= NULL
;
1538 if (bfd_link_relocatable (info
))
1541 dynobj
= (elf_hash_table (info
))->dynobj
;
1542 symtab_hdr
= &((elf_tdata (abfd
))->symtab_hdr
);
1543 sym_hashes
= elf_sym_hashes (abfd
);
1545 rel_end
= relocs
+ sec
->reloc_count
;
1546 for (rel
= relocs
; rel
< rel_end
; rel
++)
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
;
1553 r_type
= ELF32_R_TYPE (rel
->r_info
);
1555 if (r_type
>= (int) R_ARC_max
)
1557 bfd_set_error (bfd_error_bad_value
);
1560 howto
= arc_elf_howto (r_type
);
1563 && (is_reloc_for_GOT (howto
) == TRUE
1564 || is_reloc_for_TLS (howto
) == TRUE
))
1566 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
1567 if (! _bfd_elf_create_got_section (abfd
, info
))
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. */
1575 else /* Global one. */
1576 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
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))
1595 name
= h
->root
.root
.string
;
1597 /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
1599 (*_bfd_error_handler
)
1601 %B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1603 arc_elf_howto (r_type
)->name
,
1605 bfd_set_error (bfd_error_bad_value
);
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. */
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
)
1622 && (!info
->symbolic
|| !h
->def_regular
))))
1626 sreloc
= _bfd_elf_make_dynamic_reloc_section (sec
, dynobj
,
1634 sreloc
->size
+= sizeof (Elf32_External_Rela
);
1641 if (is_reloc_for_PLT (howto
) == TRUE
)
1649 /* Add info to the symbol got_entry_list. */
1650 if (is_reloc_for_GOT (howto
) == TRUE
1651 || is_reloc_for_TLS (howto
) == TRUE
)
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
),
1664 #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
1666 static struct plt_version_t
*
1667 arc_get_plt_version (struct bfd_link_info
*info
)
1671 for (i
= 0; i
< 1; i
++)
1673 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i
,
1674 (int) plt_versions
[i
].entry_size
,
1675 (int) plt_versions
[i
].elem_size
);
1678 if (bfd_get_mach (info
->output_bfd
) == bfd_mach_arc_arcv2
)
1680 if (bfd_link_pic (info
))
1681 return &(plt_versions
[ELF_ARCV2_PIC
]);
1683 return &(plt_versions
[ELF_ARCV2_ABS
]);
1687 if (bfd_link_pic (info
))
1688 return &(plt_versions
[ELF_ARC_PIC
]);
1690 return &(plt_versions
[ELF_ARC_ABS
]);
1695 add_symbol_to_plt (struct bfd_link_info
*info
)
1697 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1700 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
1702 /* If this is the first .plt entry, make room for the special first
1704 if (htab
->splt
->size
== 0)
1705 htab
->splt
->size
+= plt_data
->entry_size
;
1707 ret
= htab
->splt
->size
;
1709 htab
->splt
->size
+= plt_data
->elem_size
;
1710 ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab
->splt
->size
);
1712 htab
->sgotplt
->size
+= 4;
1713 htab
->srelplt
->size
+= sizeof (Elf32_External_Rela
);
1718 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
1719 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
1722 plt_do_relocs_for_symbol (bfd
*abfd
,
1723 struct elf_link_hash_table
*htab
,
1724 const struct plt_reloc
*reloc
,
1726 bfd_vma symbol_got_offset
)
1728 while (SYM_ONLY (reloc
->symbol
) != LAST_RELOC
)
1730 bfd_vma relocation
= 0;
1732 switch (SYM_ONLY (reloc
->symbol
))
1736 = htab
->sgotplt
->output_section
->vma
1737 + htab
->sgotplt
->output_offset
+ symbol_got_offset
;
1740 relocation
+= reloc
->addend
;
1742 if (IS_RELATIVE (reloc
->symbol
))
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;
1748 relocation
-= htab
->splt
->output_section
->vma
1749 + htab
->splt
->output_offset
1750 + plt_offset
+ reloc_offset
;
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
))
1758 = ((relocation
& 0xffff0000) >> 16)
1759 | ((relocation
& 0xffff) << 16);
1762 switch (reloc
->size
)
1765 bfd_put_32 (htab
->splt
->output_section
->owner
,
1767 htab
->splt
->contents
+ plt_offset
+ reloc
->offset
);
1771 reloc
= &(reloc
[1]); /* Jump to next relocation. */
1776 relocate_plt_for_symbol (bfd
*output_bfd
,
1777 struct bfd_link_info
*info
,
1778 struct elf_link_hash_entry
*h
)
1780 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
1781 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
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;
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
1794 (long) (htab
->sgotplt
->output_section
->vma
1795 + htab
->sgotplt
->output_offset
1797 h
->root
.root
.string
);
1801 uint16_t *ptr
= (uint16_t *) plt_data
->elem
;
1803 for (i
= 0; i
< plt_data
->elem_size
/2; i
++)
1805 uint16_t data
= ptr
[i
];
1806 bfd_put_16 (output_bfd
,
1808 htab
->splt
->contents
+ h
->plt
.offset
+ (i
*2));
1812 plt_do_relocs_for_symbol (output_bfd
, htab
,
1813 plt_data
->elem_relocs
,
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
);
1823 /* TODO: Fill in the entry in the .rela.plt section. */
1825 Elf_Internal_Rela rel
;
1828 rel
.r_offset
= (htab
->sgotplt
->output_section
->vma
1829 + htab
->sgotplt
->output_offset
1833 BFD_ASSERT (h
->dynindx
!= -1);
1834 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_ARC_JMP_SLOT
);
1836 loc
= htab
->srelplt
->contents
;
1837 loc
+= plt_index
* sizeof (Elf32_External_Rela
); /* relA */
1838 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
1843 relocate_plt_for_entry (bfd
*abfd
,
1844 struct bfd_link_info
*info
)
1846 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
1847 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1851 uint16_t *ptr
= (uint16_t *) plt_data
->entry
;
1852 for (i
= 0; i
< plt_data
->entry_size
/2; i
++)
1854 uint16_t data
= ptr
[i
];
1857 htab
->splt
->contents
+ (i
*2));
1860 PLT_DO_RELOCS_FOR_ENTRY (abfd
, htab
, plt_data
->entry_relocs
);
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
1870 elf_arc_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1871 struct elf_link_hash_entry
*h
)
1874 bfd
*dynobj
= (elf_hash_table (info
))->dynobj
;
1875 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1877 if (h
->type
== STT_FUNC
1878 || h
->type
== STT_GNU_IFUNC
1879 || h
->needs_plt
== 1)
1881 if (!bfd_link_pic (info
) && !h
->def_dynamic
&& !h
->ref_dynamic
)
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
1888 BFD_ASSERT (h
->needs_plt
);
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
))
1897 if (bfd_link_pic (info
)
1898 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
1900 bfd_vma loc
= add_symbol_to_plt (info
);
1902 if (bfd_link_executable (info
) && !h
->def_regular
)
1904 h
->root
.u
.def
.section
= htab
->splt
;
1905 h
->root
.u
.def
.value
= loc
;
1907 h
->plt
.offset
= loc
;
1911 h
->plt
.offset
= (bfd_vma
) -1;
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
)
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
;
1929 /* This is a reference to a symbol defined by a dynamic object which
1930 is not a function. */
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
))
1939 /* If there are no non-GOT references, we do not need a copy
1941 if (!h
->non_got_ref
)
1944 /* If -z nocopyreloc was given, we won't generate them either. */
1945 if (info
->nocopyreloc
)
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. */
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)
1972 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
1973 BFD_ASSERT (srel
!= NULL
);
1974 srel
->size
+= sizeof (Elf32_External_Rela
);
1978 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
1979 BFD_ASSERT (s
!= NULL
);
1981 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1984 /* Function : elf_arc_finish_dynamic_symbol
1985 Brief : Finish up dynamic symbol handling. We set the
1986 contents of various dynamic sections here.
1991 Returns : True/False as the return status. */
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
)
1999 if (h
->plt
.offset
!= (bfd_vma
) -1)
2001 relocate_plt_for_symbol (output_bfd
, info
, h
);
2003 if (!h
->def_regular
)
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
;
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
,
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
);
2028 = bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
2031 bfd_byte
* loc
= srelbss
->contents
2032 + (srelbss
->reloc_count
* sizeof (Elf32_External_Rela
));
2033 srelbss
->reloc_count
++;
2035 Elf_Internal_Rela rel
;
2037 rel
.r_offset
= rel_offset
;
2039 BFD_ASSERT (h
->dynindx
!= -1);
2040 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_ARC_COPY
);
2042 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
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
;
2054 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \
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); \
2063 /* Function : elf_arc_finish_dynamic_sections
2064 Brief : Finish up the dynamic sections handling.
2069 Returns : True/False as the return status. */
2072 elf_arc_finish_dynamic_sections (bfd
* output_bfd
,
2073 struct bfd_link_info
*info
)
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
;
2081 Elf32_External_Dyn
*dyncon
, *dynconend
;
2083 dyncon
= (Elf32_External_Dyn
*) ds
.sdyn
->contents
;
2085 = (Elf32_External_Dyn
*) (ds
.sdyn
->contents
+ ds
.sdyn
->size
);
2086 for (; dyncon
< dynconend
; dyncon
++)
2088 Elf_Internal_Dyn internal_dyn
;
2089 bfd_boolean do_it
= FALSE
;
2091 struct elf_link_hash_entry
*h
= NULL
;
2094 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &internal_dyn
);
2096 switch (internal_dyn
.d_tag
)
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")
2111 /* In case the dynamic symbols should be updated with a symbol. */
2113 && (h
->root
.type
== bfd_link_hash_defined
2114 || h
->root
.type
== bfd_link_hash_defweak
))
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
)
2122 internal_dyn
.d_un
.d_val
+=
2123 (asec_ptr
->output_section
->vma
2124 + asec_ptr
->output_offset
);
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;
2134 else if (s
!= NULL
) /* With a section information. */
2136 switch (internal_dyn
.d_tag
)
2143 internal_dyn
.d_un
.d_ptr
= (s
->output_section
->vma
2144 + s
->output_offset
);
2149 internal_dyn
.d_un
.d_val
= s
->size
;
2155 internal_dyn
.d_un
.d_val
-= s
->size
;
2165 bfd_elf32_swap_dyn_out (output_bfd
, &internal_dyn
, dyncon
);
2168 if (htab
->splt
->size
> 0)
2170 relocate_plt_for_entry (output_bfd
, info
);
2173 /* TODO: Validate this. */
2174 elf_section_data (htab
->srelplt
->output_section
)->this_hdr
.sh_entsize
2178 /* Fill in the first three entries in the global offset table. */
2181 struct elf_link_hash_entry
*h
;
2182 h
= elf_link_hash_lookup (elf_hash_table (info
), "_GLOBAL_OFFSET_TABLE_",
2183 FALSE
, FALSE
, TRUE
);
2185 if (h
!= NULL
&& h
->root
.type
!= bfd_link_hash_undefined
2186 && h
->root
.u
.def
.section
!= NULL
)
2188 asection
*sec
= h
->root
.u
.def
.section
;
2190 if (ds
.sdyn
== NULL
)
2191 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
2194 bfd_put_32 (output_bfd
,
2195 ds
.sdyn
->output_section
->vma
+ ds
.sdyn
->output_offset
,
2197 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sec
->contents
+ 4);
2198 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sec
->contents
+ 8);
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)) \
2212 /* Set the sizes of the dynamic sections. */
2214 elf_arc_size_dynamic_sections (bfd
* output_bfd
,
2215 struct bfd_link_info
*info
)
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
);
2224 dynobj
= (elf_hash_table (info
))->dynobj
;
2225 BFD_ASSERT (dynobj
!= NULL
);
2227 if ((elf_hash_table (info
))->dynamic_sections_created
)
2229 struct elf_link_hash_entry
*h
;
2231 /* Set the contents of the .interp section to the
2233 if (!bfd_link_pic (info
))
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
;
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
);
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
2257 if (htab
->srelgot
!= NULL
)
2258 htab
->srelgot
->size
= 0;
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
)
2265 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2268 if (strncmp (s
->name
, ".rela", 5) == 0)
2272 s
->flags
|= SEC_EXCLUDE
;
2276 if (strcmp (s
->name
, ".rela.plt") != 0)
2278 const char *outname
=
2279 bfd_get_section_name (output_bfd
,
2280 htab
->srelplt
->output_section
);
2282 asection
*target
= bfd_get_section_by_name (output_bfd
,
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
;
2293 /* We use the reloc_count field as a counter if we need to
2294 copy relocs into the output file. */
2298 if (strcmp (s
->name
, ".dynamic") == 0)
2302 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
2304 if (s
->contents
== NULL
&& s
->size
!= 0)
2310 /* TODO: Check if this is needed. */
2311 if (!bfd_link_pic (info
))
2312 if (!_bfd_elf_add_dynamic_entry (info
, DT_DEBUG
, 0))
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)
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
))
2331 if (reltext_exist
== TRUE
)
2332 if (!_bfd_elf_add_dynamic_entry (info
, DT_TEXTREL
, 0))
2340 /* Classify dynamic relocs such that -z combreloc can reorder and combine
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
)
2347 switch ((int) ELF32_R_TYPE (rela
->r_info
))
2349 case R_ARC_RELATIVE
:
2350 return reloc_class_relative
;
2351 case R_ARC_JMP_SLOT
:
2352 return reloc_class_plt
;
2354 return reloc_class_copy
;
2355 /* TODO: Needed in future to support ifunc. */
2357 case R_ARC_IRELATIVE:
2358 return reloc_class_ifunc;
2361 return reloc_class_normal
;
2365 const struct elf_size_info arc_elf32_size_info
=
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
),
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
2395 #define elf_backend_size_info arc_elf32_size_info
2397 static struct bfd_link_hash_table
*
2398 arc_elf_link_hash_table_create (bfd
*abfd
)
2400 struct elf_link_hash_table
*htab
;
2402 htab
= bfd_zmalloc (sizeof (*htab
));
2406 if (!_bfd_elf_link_hash_table_init (htab
, abfd
,
2407 _bfd_elf_link_hash_newfunc
,
2408 sizeof (struct elf_link_hash_entry
),
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
;
2422 /* Hook called by the linker routine which adds symbols from an object
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
)
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
;
2442 #define TARGET_LITTLE_SYM arc_elf32_le_vec
2443 #define TARGET_LITTLE_NAME "elf32-littlearc"
2444 #define TARGET_BIG_SYM arc_elf32_be_vec
2445 #define TARGET_BIG_NAME "elf32-bigarc"
2446 #define ELF_ARCH bfd_arch_arc
2447 #define ELF_MACHINE_CODE EM_ARC_COMPACT
2448 #define ELF_MACHINE_ALT1 EM_ARC_COMPACT2
2449 #define ELF_MAXPAGESIZE 0x2000
2451 #define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create
2453 #define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data
2454 #define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup
2455 #define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags
2456 #define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data
2457 #define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data
2459 #define elf_info_to_howto_rel arc_info_to_howto_rel
2460 #define elf_backend_object_p arc_elf_object_p
2461 #define elf_backend_final_write_processing arc_elf_final_write_processing
2463 #define elf_backend_relocate_section elf_arc_relocate_section
2464 #define elf_backend_check_relocs elf_arc_check_relocs
2465 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2467 #define elf_backend_reloc_type_class elf32_arc_reloc_type_class
2469 #define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol
2470 #define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol
2472 #define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections
2473 #define elf_backend_size_dynamic_sections elf_arc_size_dynamic_sections
2474 #define elf_backend_add_symbol_hook elf_arc_add_symbol_hook
2476 #define elf_backend_can_gc_sections 1
2477 #define elf_backend_want_got_plt 1
2478 #define elf_backend_plt_readonly 1
2479 #define elf_backend_rela_plts_and_copies_p 1
2480 #define elf_backend_want_plt_sym 0
2481 #define elf_backend_got_header_size 12
2483 #define elf_backend_may_use_rel_p 0
2484 #define elf_backend_may_use_rela_p 1
2485 #define elf_backend_default_use_rela_p 1
2487 #define elf_backend_default_execstack 0
2489 #include "elf32-target.h"