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"
33 # define PR_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
35 # define PR_DEBUG(fmt, args...)
38 /* #define ARC_ENABLE_DEBUG 1 */
39 #ifndef ARC_ENABLE_DEBUG
40 #define ARC_DEBUG(...)
43 name_for_global_symbol (struct elf_link_hash_entry
*h
)
45 static char *local_str
= "(local)";
49 return h
->root
.root
.string
;
51 #define ARC_DEBUG(args...) fprintf (stderr, ##args)
55 #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND) \
57 struct elf_link_hash_table *_htab = elf_hash_table (info); \
58 Elf_Internal_Rela _rel; \
61 _loc = _htab->srel##SECTION->contents \
62 + ((_htab->srel##SECTION->reloc_count) \
63 * sizeof (Elf32_External_Rela)); \
64 _htab->srel##SECTION->reloc_count++; \
65 _rel.r_addend = ADDEND; \
66 _rel.r_offset = (_htab->s##SECTION)->output_section->vma \
67 + (_htab->s##SECTION)->output_offset + OFFSET; \
68 BFD_ASSERT ((long) SYM_IDX != -1); \
69 _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE); \
70 bfd_elf32_swap_reloca_out (BFD, &_rel, _loc); \
73 struct dynamic_sections
75 bfd_boolean initialized
;
79 asection
* srelgotplt
;
85 enum dyn_section_types
96 const char * dyn_section_names
[DYN_SECTION_TYPES_END
] =
125 struct got_entry
*next
;
126 enum tls_type_e type
;
128 bfd_boolean processed
;
129 bfd_boolean created_dyn_relocation
;
130 enum tls_got_entries existing_entries
;
134 new_got_entry_to_list (struct got_entry
**list
,
135 enum tls_type_e type
,
137 enum tls_got_entries existing_entries
)
139 /* Find list end. Avoid having multiple entries of the same
141 struct got_entry
**p
= list
;
144 if ((*p
)->type
== type
)
149 struct got_entry
*entry
=
150 (struct got_entry
*) malloc (sizeof(struct got_entry
));
153 entry
->offset
= offset
;
155 entry
->processed
= FALSE
;
156 entry
->created_dyn_relocation
= FALSE
;
157 entry
->existing_entries
= existing_entries
;
159 /* Add the entry to the end of the list. */
164 symbol_has_entry_of_type (struct got_entry
*list
, enum tls_type_e type
)
168 if (list
->type
== type
)
176 /* The default symbols representing the init and fini dyn values.
177 TODO: Check what is the relation of those strings with arclinux.em
179 #define INIT_SYM_STRING "_init"
180 #define FINI_SYM_STRING "_fini"
182 char * init_str
= INIT_SYM_STRING
;
183 char * fini_str
= FINI_SYM_STRING
;
185 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
190 static ATTRIBUTE_UNUSED
const char *
191 reloc_type_to_name (unsigned int type
)
195 #include "elf/arc-reloc.def"
202 #undef ARC_RELOC_HOWTO
204 /* Try to minimize the amount of space occupied by relocation tables
205 on the ROM (not that the ROM won't be swamped by other ELF overhead). */
209 static ATTRIBUTE_UNUSED bfd_boolean
210 is_reloc_PC_relative (reloc_howto_type
*howto
)
212 return (strstr (howto
->name
, "PC") != NULL
) ? TRUE
: FALSE
;
216 is_reloc_SDA_relative (reloc_howto_type
*howto
)
218 return (strstr (howto
->name
, "SDA") != NULL
) ? TRUE
: FALSE
;
222 is_reloc_for_GOT (reloc_howto_type
* howto
)
224 if (strstr (howto
->name
, "TLS") != NULL
)
226 return (strstr (howto
->name
, "GOT") != NULL
) ? TRUE
: FALSE
;
230 is_reloc_for_PLT (reloc_howto_type
* howto
)
232 return (strstr (howto
->name
, "PLT") != NULL
) ? TRUE
: FALSE
;
236 is_reloc_for_TLS (reloc_howto_type
*howto
)
238 return (strstr (howto
->name
, "TLS") != NULL
) ? TRUE
: FALSE
;
241 #define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
242 #define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
243 #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
244 #define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
245 #define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
246 #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
249 static bfd_reloc_status_type
250 arc_elf_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
251 arelent
*reloc_entry
,
253 void *data ATTRIBUTE_UNUSED
,
254 asection
*input_section
,
256 char ** error_message ATTRIBUTE_UNUSED
)
258 if (output_bfd
!= NULL
)
260 reloc_entry
->address
+= input_section
->output_offset
;
262 /* In case of relocateable link and if the reloc is against a
263 section symbol, the addend needs to be adjusted according to
264 where the section symbol winds up in the output section. */
265 if ((symbol_in
->flags
& BSF_SECTION_SYM
) && symbol_in
->section
)
266 reloc_entry
->addend
+= symbol_in
->section
->output_offset
;
271 return bfd_reloc_continue
;
275 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
279 #include "elf/arc-reloc.def"
282 #undef ARC_RELOC_HOWTO
284 #define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
285 [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, FALSE, 0, complain_overflow_##OVERFLOW, arc_elf_reloc, "R_" #TYPE, FALSE, 0, 0, FALSE),
287 static struct reloc_howto_struct elf_arc_howto_table
[] =
289 #include "elf/arc-reloc.def"
290 /* Example of what is generated by the preprocessor. Currently kept as an
292 HOWTO (R_ARC_NONE, // Type.
294 2, // Size (0 = byte, 1 = short, 2 = long).
296 FALSE, // PC_relative.
298 complain_overflow_bitfield, // Complain_on_overflow.
299 bfd_elf_generic_reloc, // Special_function.
300 "R_ARC_NONE", // Name.
301 TRUE, // Partial_inplace.
304 FALSE), // PCrel_offset.
307 #undef ARC_RELOC_HOWTO
309 static void arc_elf_howto_init (void)
311 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
312 elf_arc_howto_table[TYPE].pc_relative = \
313 (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
314 elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \
315 /* Only 32 bit data relocations should be marked as ME. */ \
316 if (strstr (#FORMULA, " ME ") != NULL) \
318 BFD_ASSERT (SIZE == 2); \
321 #include "elf/arc-reloc.def"
324 #undef ARC_RELOC_HOWTO
327 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
329 const int howto_table_lookup
[] =
331 #include "elf/arc-reloc.def"
333 #undef ARC_RELOC_HOWTO
335 static reloc_howto_type
*
336 arc_elf_howto (unsigned int r_type
)
338 if (elf_arc_howto_table
[R_ARC_32
].dst_mask
== 0)
339 arc_elf_howto_init ();
340 return &elf_arc_howto_table
[r_type
];
343 /* Map BFD reloc types to ARC ELF reloc types. */
347 bfd_reloc_code_real_type bfd_reloc_val
;
348 unsigned char elf_reloc_val
;
351 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
352 { BFD_RELOC_##TYPE, R_##TYPE },
353 static const struct arc_reloc_map arc_reloc_map
[] =
355 #include "elf/arc-reloc.def"
357 {BFD_RELOC_NONE
, R_ARC_NONE
},
358 {BFD_RELOC_8
, R_ARC_8
},
359 {BFD_RELOC_16
, R_ARC_16
},
360 {BFD_RELOC_24
, R_ARC_24
},
361 {BFD_RELOC_32
, R_ARC_32
},
363 #undef ARC_RELOC_HOWTO
365 typedef ATTRIBUTE_UNUSED
bfd_vma (*replace_func
) (unsigned, int ATTRIBUTE_UNUSED
);
367 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
369 func = (void *) RELOC_FUNCTION; \
372 get_replace_function (bfd
*abfd
, unsigned int r_type
)
378 #include "elf/arc-reloc.def"
381 if (func
== replace_bits24
&& bfd_big_endian (abfd
))
382 return (replace_func
) replace_bits24_be
;
384 return (replace_func
) func
;
386 #undef ARC_RELOC_HOWTO
388 static reloc_howto_type
*
389 arc_elf32_bfd_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
390 bfd_reloc_code_real_type code
)
394 for (i
= ARRAY_SIZE (arc_reloc_map
); i
--;)
396 if (arc_reloc_map
[i
].bfd_reloc_val
== code
)
397 return arc_elf_howto (arc_reloc_map
[i
].elf_reloc_val
);
403 /* Function to set the ELF flag bits. */
405 arc_elf_set_private_flags (bfd
*abfd
, flagword flags
)
407 elf_elfheader (abfd
)->e_flags
= flags
;
408 elf_flags_init (abfd
) = TRUE
;
412 /* Print private flags. */
414 arc_elf_print_private_bfd_data (bfd
*abfd
, void * ptr
)
416 FILE *file
= (FILE *) ptr
;
419 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
421 /* Print normal ELF private data. */
422 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
424 flags
= elf_elfheader (abfd
)->e_flags
;
425 fprintf (file
, _("private flags = 0x%lx:"), (unsigned long) flags
);
427 switch (flags
& EF_ARC_MACH_MSK
)
429 case EF_ARC_CPU_ARCV2HS
: fprintf (file
, " -mcpu=ARCv2HS"); break;
430 case EF_ARC_CPU_ARCV2EM
: fprintf (file
, " -mcpu=ARCv2EM"); break;
431 case E_ARC_MACH_ARC600
: fprintf (file
, " -mcpu=ARC600"); break;
432 case E_ARC_MACH_ARC601
: fprintf (file
, " -mcpu=ARC601"); break;
433 case E_ARC_MACH_ARC700
: fprintf (file
, " -mcpu=ARC700"); break;
435 fprintf (file
, "-mcpu=unknown");
439 switch (flags
& EF_ARC_OSABI_MSK
)
441 case E_ARC_OSABI_ORIG
: fprintf (file
, " (ABI:legacy)"); break;
442 case E_ARC_OSABI_V2
: fprintf (file
, " (ABI:v2)"); break;
443 case E_ARC_OSABI_V3
: fprintf (file
, " (ABI:v3)"); break;
445 fprintf (file
, "(ABI:unknown)");
453 /* Copy backend specific data from one object module to another. */
456 arc_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
458 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
459 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
462 BFD_ASSERT (!elf_flags_init (obfd
)
463 || elf_elfheader (obfd
)->e_flags
== elf_elfheader (ibfd
)->e_flags
);
465 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
466 elf_flags_init (obfd
) = TRUE
;
468 /* Copy object attributes. */
469 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
471 return _bfd_elf_copy_private_bfd_data (ibfd
, obfd
);
474 static reloc_howto_type
*
475 bfd_elf32_bfd_reloc_name_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
480 for (i
= 0; i
< ARRAY_SIZE (elf_arc_howto_table
); i
++)
481 if (elf_arc_howto_table
[i
].name
!= NULL
482 && strcasecmp (elf_arc_howto_table
[i
].name
, r_name
) == 0)
483 return arc_elf_howto (i
);
488 /* Set the howto pointer for an ARC ELF reloc. */
491 arc_info_to_howto_rel (bfd
* abfd ATTRIBUTE_UNUSED
,
493 Elf_Internal_Rela
* dst
)
497 r_type
= ELF32_R_TYPE (dst
->r_info
);
498 BFD_ASSERT (r_type
< (unsigned int) R_ARC_max
);
499 cache_ptr
->howto
= arc_elf_howto (r_type
);
502 /* Merge backend specific data from an object file to the output
503 object file when linking. */
506 arc_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
508 unsigned short mach_ibfd
;
509 static unsigned short mach_obfd
= EM_NONE
;
514 /* Check if we have the same endianess. */
515 if (! _bfd_generic_verify_endian_match (ibfd
, obfd
))
517 _bfd_error_handler (_("ERROR: Endian Match failed. Attempting to link "
518 "%B with binary %s of opposite endian-ness"),
519 ibfd
, bfd_get_filename (obfd
));
523 /* Collect ELF flags. */
524 in_flags
= elf_elfheader (ibfd
)->e_flags
& EF_ARC_MACH_MSK
;
525 out_flags
= elf_elfheader (obfd
)->e_flags
& EF_ARC_MACH_MSK
;
527 if (!elf_flags_init (obfd
)) /* First call, no flags set. */
529 elf_flags_init (obfd
) = TRUE
;
530 out_flags
= in_flags
;
533 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
534 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
537 /* Check to see if the input BFD actually contains any sections. Do
538 not short-circuit dynamic objects; their section list may be
539 emptied by elf_link_add_object_symbols. */
540 if (!(ibfd
->flags
& DYNAMIC
))
542 bfd_boolean null_input_bfd
= TRUE
;
543 bfd_boolean only_data_sections
= TRUE
;
545 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
547 if ((bfd_get_section_flags (ibfd
, sec
)
548 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
549 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
550 only_data_sections
= FALSE
;
552 null_input_bfd
= FALSE
;
555 if (null_input_bfd
|| only_data_sections
)
559 /* Complain about various flag/architecture mismatches. */
560 mach_ibfd
= elf_elfheader (ibfd
)->e_machine
;
561 if (mach_obfd
== EM_NONE
)
563 mach_obfd
= mach_ibfd
;
567 if (mach_ibfd
!= mach_obfd
)
569 _bfd_error_handler (_("ERROR: Attempting to link %B "
570 "with a binary %s of different architecture"),
571 ibfd
, bfd_get_filename (obfd
));
574 else if (in_flags
!= out_flags
)
576 /* Warn if different flags. */
577 (*_bfd_error_handler
)
578 (_("%s: uses different e_flags (0x%lx) fields than "
579 "previous modules (0x%lx)"),
580 bfd_get_filename (ibfd
), (long)in_flags
, (long)out_flags
);
581 if (in_flags
&& out_flags
)
583 /* MWDT doesnt set the eflags hence make sure we choose the
584 eflags set by gcc. */
585 in_flags
= in_flags
> out_flags
? in_flags
: out_flags
;
589 /* Update the flags. */
590 elf_elfheader (obfd
)->e_flags
= in_flags
;
592 if (bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
594 return bfd_set_arch_mach (obfd
, bfd_arch_arc
, bfd_get_mach (ibfd
));
600 /* Set the right machine number for an ARC ELF file. */
602 arc_elf_object_p (bfd
* abfd
)
604 /* Make sure this is initialised, or you'll have the potential of passing
605 garbage---or misleading values---into the call to
606 bfd_default_set_arch_mach (). */
607 int mach
= bfd_mach_arc_arc700
;
608 unsigned long arch
= elf_elfheader (abfd
)->e_flags
& EF_ARC_MACH_MSK
;
609 unsigned e_machine
= elf_elfheader (abfd
)->e_machine
;
611 if (e_machine
== EM_ARC_COMPACT
|| e_machine
== EM_ARC_COMPACT2
)
615 case E_ARC_MACH_ARC600
:
616 mach
= bfd_mach_arc_arc600
;
618 case E_ARC_MACH_ARC601
:
619 mach
= bfd_mach_arc_arc601
;
621 case E_ARC_MACH_ARC700
:
622 mach
= bfd_mach_arc_arc700
;
624 case E_ARC_MACH_NPS400
:
625 mach
= bfd_mach_arc_nps400
;
627 case EF_ARC_CPU_ARCV2HS
:
628 case EF_ARC_CPU_ARCV2EM
:
629 mach
= bfd_mach_arc_arcv2
;
632 mach
= (e_machine
== EM_ARC_COMPACT
) ?
633 bfd_mach_arc_arc700
: bfd_mach_arc_arcv2
;
639 if (e_machine
== EM_ARC
)
641 (*_bfd_error_handler
)
642 (_("Error: The ARC4 architecture is no longer supported.\n"));
647 (*_bfd_error_handler
)
648 (_("Warning: unset or old architecture flags. \n"
649 " Use default machine.\n"));
653 return bfd_default_set_arch_mach (abfd
, bfd_arch_arc
, mach
);
656 /* The final processing done just before writing out an ARC ELF object file.
657 This gets the ARC architecture right based on the machine number. */
660 arc_elf_final_write_processing (bfd
* abfd
,
661 bfd_boolean linker ATTRIBUTE_UNUSED
)
665 switch (bfd_get_mach (abfd
))
667 case bfd_mach_arc_arc600
:
668 emf
= EM_ARC_COMPACT
;
670 case bfd_mach_arc_arc601
:
671 emf
= EM_ARC_COMPACT
;
673 case bfd_mach_arc_arc700
:
674 emf
= EM_ARC_COMPACT
;
676 case bfd_mach_arc_nps400
:
677 emf
= EM_ARC_COMPACT
;
679 case bfd_mach_arc_arcv2
:
680 emf
= EM_ARC_COMPACT2
;
686 elf_elfheader (abfd
)->e_machine
= emf
;
688 /* Record whatever is the current syscall ABI version. */
689 elf_elfheader (abfd
)->e_flags
|= E_ARC_OSABI_CURRENT
;
695 #define BFD_DEBUG_PIC(...)
697 struct arc_relocation_data
699 bfd_signed_vma reloc_offset
;
700 bfd_signed_vma reloc_addend
;
701 bfd_signed_vma got_offset_value
;
703 bfd_signed_vma sym_value
;
704 asection
* sym_section
;
706 reloc_howto_type
*howto
;
708 asection
* input_section
;
710 bfd_signed_vma sdata_begin_symbol_vma
;
711 bfd_boolean sdata_begin_symbol_vma_set
;
712 bfd_signed_vma got_symbol_vma
;
714 bfd_boolean should_relocate
;
716 const char * symbol_name
;
720 debug_arc_reloc (struct arc_relocation_data reloc_data
)
722 PR_DEBUG ("Reloc type=%s, should_relocate = %s\n",
723 reloc_data
.howto
->name
,
724 reloc_data
.should_relocate
? "true" : "false");
725 PR_DEBUG (" offset = 0x%x, addend = 0x%x\n",
726 (unsigned int) reloc_data
.reloc_offset
,
727 (unsigned int) reloc_data
.reloc_addend
);
728 PR_DEBUG (" Symbol:\n");
729 PR_DEBUG (" value = 0x%08x\n",
730 (unsigned int) reloc_data
.sym_value
);
731 if (reloc_data
.sym_section
!= NULL
)
733 PR_DEBUG (" Symbol Section:\n");
735 " section name = %s, output_offset 0x%08x",
736 reloc_data
.sym_section
->name
,
737 (unsigned int) reloc_data
.sym_section
->output_offset
);
738 if (reloc_data
.sym_section
->output_section
!= NULL
)
741 ", output_section->vma = 0x%08x",
742 ((unsigned int) reloc_data
.sym_section
->output_section
->vma
));
745 PR_DEBUG (" file: %s\n", reloc_data
.sym_section
->owner
->filename
);
749 PR_DEBUG ( " symbol section is NULL\n");
752 PR_DEBUG ( " Input_section:\n");
753 if (reloc_data
.input_section
!= NULL
)
756 " section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
757 reloc_data
.input_section
->name
,
758 (unsigned int) reloc_data
.input_section
->output_offset
,
759 (unsigned int) reloc_data
.input_section
->output_section
->vma
);
760 PR_DEBUG ( " changed_address = 0x%08x\n",
761 (unsigned int) (reloc_data
.input_section
->output_section
->vma
+
762 reloc_data
.input_section
->output_offset
+
763 reloc_data
.reloc_offset
));
764 PR_DEBUG (" file: %s\n", reloc_data
.input_section
->owner
->filename
);
768 PR_DEBUG ( " input section is NULL\n");
773 middle_endian_convert (bfd_vma insn
, bfd_boolean do_it
)
778 ((insn
& 0xffff0000) >> 16) |
779 ((insn
& 0xffff) << 16);
784 /* This function is called for relocations that are otherwise marked as NOT
785 requiring overflow checks. In here we perform non-standard checks of
786 the relocation value. */
788 static inline bfd_reloc_status_type
789 arc_special_overflow_checks (const struct arc_relocation_data reloc_data
,
790 bfd_signed_vma relocation
,
791 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
793 switch (reloc_data
.howto
->type
)
795 case R_ARC_NPS_CMEM16
:
796 if (((relocation
>> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE
)
798 if (reloc_data
.reloc_addend
== 0)
799 (*_bfd_error_handler
)
800 (_("%B(%A+0x%lx): CMEM relocation to `%s' is invalid, "
801 "16 MSB should be 0x%04x (value is 0x%lx)"),
802 reloc_data
.input_section
->owner
,
803 reloc_data
.input_section
,
804 reloc_data
.reloc_offset
,
805 reloc_data
.symbol_name
,
809 (*_bfd_error_handler
)
810 (_("%B(%A+0x%lx): CMEM relocation to `%s+0x%lx' is invalid, "
811 "16 MSB should be 0x%04x (value is 0x%lx)"),
812 reloc_data
.input_section
->owner
,
813 reloc_data
.input_section
,
814 reloc_data
.reloc_offset
,
815 reloc_data
.symbol_name
,
816 reloc_data
.reloc_addend
,
819 return bfd_reloc_overflow
;
830 #define ME(reloc) (reloc)
832 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
833 && (!bfd_big_endian (BFD)))
835 #define S ((bfd_signed_vma) (reloc_data.sym_value \
836 + (reloc_data.sym_section->output_section != NULL ? \
837 (reloc_data.sym_section->output_offset \
838 + reloc_data.sym_section->output_section->vma) : 0)))
839 #define L ((bfd_signed_vma) (reloc_data.sym_value \
840 + (reloc_data.sym_section->output_section != NULL ? \
841 (reloc_data.sym_section->output_offset \
842 + reloc_data.sym_section->output_section->vma) : 0)))
843 #define A (reloc_data.reloc_addend)
845 #define G (reloc_data.got_offset_value)
846 #define GOT (reloc_data.got_symbol_vma)
847 #define GOT_BEGIN (htab->sgot->output_section->vma)
850 /* P: relative offset to PCL The offset should be to the
851 current location aligned to 32 bits. */
852 #define P ((bfd_signed_vma) ( \
854 (reloc_data.input_section->output_section != NULL ? \
855 reloc_data.input_section->output_section->vma : 0) \
856 + reloc_data.input_section->output_offset \
857 + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \
859 #define PDATA ((bfd_signed_vma) ( \
860 (reloc_data.input_section->output_section->vma \
861 + reloc_data.input_section->output_offset \
862 + (reloc_data.reloc_offset))))
863 #define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
864 + reloc_data.sym_section->output_offset)
866 #define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
867 #define TLS_REL (bfd_signed_vma) \
868 ((elf_hash_table (info))->tls_sec->output_section->vma)
874 #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
876 asection *sym_section = reloc_data.sym_section; \
877 asection *input_section = reloc_data.input_section; \
878 ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \
879 ARC_DEBUG ("FORMULA = " FORMULA "\n"); \
880 ARC_DEBUG ("S = 0x%x\n", S); \
881 ARC_DEBUG ("A = 0x%x\n", A); \
882 ARC_DEBUG ("L = 0x%x\n", L); \
883 if (sym_section->output_section != NULL) \
885 ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
886 sym_section->output_section->vma + sym_section->output_offset); \
890 ARC_DEBUG ("symbol_section->vma = NULL\n"); \
892 if (input_section->output_section != NULL) \
894 ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
895 input_section->output_section->vma + input_section->output_offset); \
899 ARC_DEBUG ("symbol_section->vma = NULL\n"); \
901 ARC_DEBUG ("PCL = 0x%x\n", P); \
902 ARC_DEBUG ("P = 0x%x\n", P); \
903 ARC_DEBUG ("G = 0x%x\n", G); \
904 ARC_DEBUG ("SDA_OFFSET = 0x%x\n", _SDA_BASE_); \
905 ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
906 ARC_DEBUG ("GOT_OFFSET = 0x%x\n", GOT); \
907 ARC_DEBUG ("relocation = 0x%08x\n", relocation); \
908 ARC_DEBUG ("before = 0x%08x\n", (unsigned int) insn); \
909 ARC_DEBUG ("data = 0x%08x (%u) (%d)\n", (unsigned int) relocation, (unsigned int) relocation, (int) relocation); \
912 #define PRINT_DEBUG_RELOC_INFO_AFTER \
914 ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \
917 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
920 bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
921 relocation = FORMULA ; \
922 PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \
923 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
924 insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \
925 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
926 PRINT_DEBUG_RELOC_INFO_AFTER \
930 static bfd_reloc_status_type
931 arc_do_relocation (bfd_byte
* contents
,
932 struct arc_relocation_data reloc_data
,
933 struct bfd_link_info
*info
)
935 bfd_signed_vma relocation
= 0;
937 bfd_vma orig_insn ATTRIBUTE_UNUSED
;
938 bfd
* abfd
= reloc_data
.input_section
->owner
;
939 struct elf_link_hash_table
*htab ATTRIBUTE_UNUSED
= elf_hash_table (info
);
940 bfd_reloc_status_type flag
;
942 if (reloc_data
.should_relocate
== FALSE
)
945 switch (reloc_data
.howto
->size
)
948 insn
= arc_bfd_get_32 (abfd
,
949 contents
+ reloc_data
.reloc_offset
,
950 reloc_data
.input_section
);
953 insn
= arc_bfd_get_16 (abfd
,
954 contents
+ reloc_data
.reloc_offset
,
955 reloc_data
.input_section
);
958 insn
= arc_bfd_get_8 (abfd
,
959 contents
+ reloc_data
.reloc_offset
,
960 reloc_data
.input_section
);
970 switch (reloc_data
.howto
->type
)
972 #include "elf/arc-reloc.def"
979 /* Check for relocation overflow. */
980 if (reloc_data
.howto
->complain_on_overflow
!= complain_overflow_dont
)
981 flag
= bfd_check_overflow (reloc_data
.howto
->complain_on_overflow
,
982 reloc_data
.howto
->bitsize
,
983 reloc_data
.howto
->rightshift
,
984 bfd_arch_bits_per_address (abfd
),
987 flag
= arc_special_overflow_checks (reloc_data
, relocation
, info
);
989 #undef DEBUG_ARC_RELOC
990 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
991 if (flag
!= bfd_reloc_ok
)
993 PR_DEBUG ( "Relocation overflows !!!!\n");
995 DEBUG_ARC_RELOC (reloc_data
);
998 "Relocation value = signed -> %d, unsigned -> %u"
999 ", hex -> (0x%08x)\n",
1001 (unsigned int) relocation
,
1002 (unsigned int) relocation
);
1005 #undef DEBUG_ARC_RELOC
1006 #define DEBUG_ARC_RELOC(A)
1008 /* Write updated instruction back to memory. */
1009 switch (reloc_data
.howto
->size
)
1012 arc_bfd_put_32 (abfd
, insn
,
1013 contents
+ reloc_data
.reloc_offset
,
1014 reloc_data
.input_section
);
1017 arc_bfd_put_16 (abfd
, insn
,
1018 contents
+ reloc_data
.reloc_offset
,
1019 reloc_data
.input_section
);
1022 arc_bfd_put_8 (abfd
, insn
,
1023 contents
+ reloc_data
.reloc_offset
,
1024 reloc_data
.input_section
);
1027 ARC_DEBUG ("size = %d\n", reloc_data
.howto
->size
);
1032 return bfd_reloc_ok
;
1047 #undef ARC_RELOC_HOWTO
1049 static struct got_entry
**
1050 arc_get_local_got_ents (bfd
* abfd
)
1052 static struct got_entry
**local_got_ents
= NULL
;
1054 if (local_got_ents
== NULL
)
1057 Elf_Internal_Shdr
*symtab_hdr
= &((elf_tdata (abfd
))->symtab_hdr
);
1059 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
1060 local_got_ents
= (struct got_entry
**)
1061 bfd_alloc (abfd
, sizeof(struct got_entry
*) * size
);
1062 if (local_got_ents
== NULL
)
1065 memset (local_got_ents
, 0, sizeof(struct got_entry
*) * size
);
1066 elf_local_got_ents (abfd
) = local_got_ents
;
1069 return local_got_ents
;
1072 /* Relocate an arc ELF section.
1073 Function : elf_arc_relocate_section
1074 Brief : Relocate an arc section, by handling all the relocations
1075 appearing in that section.
1076 Args : output_bfd : The bfd being written to.
1077 info : Link information.
1078 input_bfd : The input bfd.
1079 input_section : The section being relocated.
1080 contents : contents of the section being relocated.
1081 relocs : List of relocations in the section.
1082 local_syms : is a pointer to the swapped in local symbols.
1083 local_section : is an array giving the section in the input file
1084 corresponding to the st_shndx field of each
1087 elf_arc_relocate_section (bfd
* output_bfd
,
1088 struct bfd_link_info
* info
,
1090 asection
* input_section
,
1091 bfd_byte
* contents
,
1092 Elf_Internal_Rela
* relocs
,
1093 Elf_Internal_Sym
* local_syms
,
1094 asection
** local_sections
)
1096 Elf_Internal_Shdr
* symtab_hdr
;
1097 struct elf_link_hash_entry
** sym_hashes
;
1098 struct got_entry
** local_got_ents
;
1099 Elf_Internal_Rela
* rel
;
1100 Elf_Internal_Rela
* wrel
;
1101 Elf_Internal_Rela
* relend
;
1102 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1104 symtab_hdr
= &((elf_tdata (input_bfd
))->symtab_hdr
);
1105 sym_hashes
= elf_sym_hashes (input_bfd
);
1107 rel
= wrel
= relocs
;
1108 relend
= relocs
+ input_section
->reloc_count
;
1109 for (; rel
< relend
; wrel
++, rel
++)
1111 enum elf_arc_reloc_type r_type
;
1112 reloc_howto_type
* howto
;
1113 unsigned long r_symndx
;
1114 struct elf_link_hash_entry
* h
;
1115 Elf_Internal_Sym
* sym
;
1117 struct elf_link_hash_entry
*h2
;
1119 struct arc_relocation_data reloc_data
=
1123 .got_offset_value
= 0,
1125 .sym_section
= NULL
,
1127 .input_section
= NULL
,
1128 .sdata_begin_symbol_vma
= 0,
1129 .sdata_begin_symbol_vma_set
= FALSE
,
1130 .got_symbol_vma
= 0,
1131 .should_relocate
= FALSE
1134 r_type
= ELF32_R_TYPE (rel
->r_info
);
1136 if (r_type
>= (int) R_ARC_max
)
1138 bfd_set_error (bfd_error_bad_value
);
1141 howto
= arc_elf_howto (r_type
);
1143 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1145 /* If we are generating another .o file and the symbol in not
1146 local, skip this relocation. */
1147 if (bfd_link_relocatable (info
))
1149 /* This is a relocateable link. We don't have to change
1150 anything, unless the reloc is against a section symbol,
1151 in which case we have to adjust according to where the
1152 section symbol winds up in the output section. */
1154 /* Checks if this is a local symbol and thus the reloc
1155 might (will??) be against a section symbol. */
1156 if (r_symndx
< symtab_hdr
->sh_info
)
1158 sym
= local_syms
+ r_symndx
;
1159 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1161 sec
= local_sections
[r_symndx
];
1163 /* for RELA relocs.Just adjust the addend
1164 value in the relocation entry. */
1165 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
1168 PR_DEBUG ("local symbols reloc "
1169 "(section=%d %s) seen in %s\n",
1171 local_sections
[r_symndx
]->name
,
1172 __PRETTY_FUNCTION__
)
1178 h2
= elf_link_hash_lookup (elf_hash_table (info
), "__SDATA_BEGIN__",
1179 FALSE
, FALSE
, TRUE
);
1181 if (reloc_data
.sdata_begin_symbol_vma_set
== FALSE
1182 && h2
!= NULL
&& h2
->root
.type
!= bfd_link_hash_undefined
1183 && h2
->root
.u
.def
.section
->output_section
!= NULL
)
1184 /* TODO: Verify this condition. */
1186 reloc_data
.sdata_begin_symbol_vma
=
1187 (h2
->root
.u
.def
.value
+
1188 h2
->root
.u
.def
.section
->output_section
->vma
);
1189 reloc_data
.sdata_begin_symbol_vma_set
= TRUE
;
1192 reloc_data
.input_section
= input_section
;
1193 reloc_data
.howto
= howto
;
1194 reloc_data
.reloc_offset
= rel
->r_offset
;
1195 reloc_data
.reloc_addend
= rel
->r_addend
;
1197 /* This is a final link. */
1202 if (r_symndx
< symtab_hdr
->sh_info
) /* A local symbol. */
1204 sym
= local_syms
+ r_symndx
;
1205 sec
= local_sections
[r_symndx
];
1209 /* TODO: This code is repeated from below. We should
1210 clean it and remove duplications.
1211 Sec is used check for discarded sections.
1212 Need to redesign code below. */
1214 /* Get the symbol's entry in the symtab. */
1215 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1217 while (h
->root
.type
== bfd_link_hash_indirect
1218 || h
->root
.type
== bfd_link_hash_warning
)
1219 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1221 /* If we have encountered a definition for this symbol. */
1222 if (h
->root
.type
== bfd_link_hash_defined
1223 || h
->root
.type
== bfd_link_hash_defweak
)
1225 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1226 sec
= h
->root
.u
.def
.section
;
1230 /* Clean relocs for symbols in discarded sections. */
1231 if (sec
!= NULL
&& discarded_section (sec
))
1233 _bfd_clear_contents (howto
, input_bfd
, input_section
,
1234 contents
+ rel
->r_offset
);
1235 rel
->r_offset
= rel
->r_offset
;
1239 /* For ld -r, remove relocations in debug sections against
1240 sections defined in discarded sections. Not done for
1241 eh_frame editing code expects to be present. */
1242 if (bfd_link_relocatable (info
)
1243 && (input_section
->flags
& SEC_DEBUGGING
))
1249 if (bfd_link_relocatable (info
))
1256 if (r_symndx
< symtab_hdr
->sh_info
) /* A local symbol. */
1258 struct got_entry
*entry
;
1260 local_got_ents
= arc_get_local_got_ents (output_bfd
);
1261 entry
= local_got_ents
[r_symndx
];
1263 reloc_data
.sym_value
= sym
->st_value
;
1264 reloc_data
.sym_section
= sec
;
1265 reloc_data
.symbol_name
=
1266 bfd_elf_string_from_elf_section (input_bfd
,
1267 symtab_hdr
->sh_link
,
1270 /* Mergeable section handling. */
1271 if ((sec
->flags
& SEC_MERGE
)
1272 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1276 rel
->r_addend
= _bfd_elf_rel_local_sym (output_bfd
, sym
,
1277 &msec
, rel
->r_addend
);
1278 rel
->r_addend
-= (sec
->output_section
->vma
1279 + sec
->output_offset
1281 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
1283 reloc_data
.reloc_addend
= rel
->r_addend
;
1286 if ((is_reloc_for_GOT (howto
)
1287 || is_reloc_for_TLS (howto
)) && entry
!= NULL
)
1289 if (is_reloc_for_TLS (howto
))
1290 while (entry
->type
== GOT_NORMAL
&& entry
->next
!= NULL
)
1291 entry
= entry
->next
;
1293 if (is_reloc_for_GOT (howto
))
1294 while (entry
->type
!= GOT_NORMAL
&& entry
->next
!= NULL
)
1295 entry
= entry
->next
;
1297 if (entry
->type
== GOT_TLS_GD
&& entry
->processed
== FALSE
)
1299 bfd_vma sym_vma
= sym
->st_value
1300 + sec
->output_section
->vma
1301 + sec
->output_offset
;
1303 /* Create dynamic relocation for local sym. */
1304 ADD_RELA (output_bfd
, got
, entry
->offset
, 0,
1305 R_ARC_TLS_DTPMOD
, 0);
1306 ADD_RELA (output_bfd
, got
, entry
->offset
+4, 0,
1307 R_ARC_TLS_DTPOFF
, 0);
1309 bfd_vma sec_vma
= sec
->output_section
->vma
1310 + sec
->output_offset
;
1311 bfd_put_32 (output_bfd
, sym_vma
- sec_vma
,
1312 htab
->sgot
->contents
+ entry
->offset
+ 4);
1314 ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_GD value "
1315 "= 0x%x @ 0x%x, for symbol %s\n",
1317 htab
->sgot
->contents
+ entry
->offset
+ 4,
1320 entry
->processed
= TRUE
;
1322 if (entry
->type
== GOT_TLS_IE
&& entry
->processed
== FALSE
)
1324 bfd_vma sym_vma
= sym
->st_value
1325 + sec
->output_section
->vma
1326 + sec
->output_offset
;
1327 bfd_vma sec_vma
= htab
->tls_sec
->output_section
->vma
;
1328 bfd_put_32 (output_bfd
, sym_vma
- sec_vma
,
1329 htab
->sgot
->contents
+ entry
->offset
);
1330 /* TODO: Check if this type of relocs is the cause
1331 for all the ARC_NONE dynamic relocs. */
1333 ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_IE value = "
1334 "0x%x @ 0x%x, for symbol %s\n",
1336 htab
->sgot
->contents
+ entry
->offset
,
1339 entry
->processed
= TRUE
;
1341 if (entry
->type
== GOT_NORMAL
&& entry
->processed
== FALSE
)
1343 bfd_vma sec_vma
= reloc_data
.sym_section
->output_section
->vma
1344 + reloc_data
.sym_section
->output_offset
;
1346 bfd_put_32 (output_bfd
, reloc_data
.sym_value
+ sec_vma
,
1347 htab
->sgot
->contents
+ entry
->offset
);
1349 ARC_DEBUG ("arc_info: PATCHED: 0x%08x @ 0x%08x for "
1350 "sym %s in got offset 0x%x\n",
1351 reloc_data
.sym_value
+ sec_vma
,
1352 htab
->sgot
->output_section
->vma
1353 + htab
->sgot
->output_offset
+ entry
->offset
,
1356 entry
->processed
= TRUE
;
1359 reloc_data
.got_offset_value
= entry
->offset
;
1360 ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
1361 "vma = 0x%x for symbol %s\n",
1362 entry
->type
, entry
->offset
,
1363 htab
->sgot
->output_section
->vma
1364 + htab
->sgot
->output_offset
+ entry
->offset
,
1368 BFD_ASSERT (htab
->sgot
!= NULL
|| !is_reloc_for_GOT (howto
));
1369 if (htab
->sgot
!= NULL
)
1370 reloc_data
.got_symbol_vma
= htab
->sgot
->output_section
->vma
1371 + htab
->sgot
->output_offset
;
1373 reloc_data
.should_relocate
= TRUE
;
1375 else /* Global symbol. */
1377 /* Get the symbol's entry in the symtab. */
1378 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1380 while (h
->root
.type
== bfd_link_hash_indirect
1381 || h
->root
.type
== bfd_link_hash_warning
)
1382 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1384 /* TODO: Need to validate what was the intention. */
1385 /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
1386 reloc_data
.symbol_name
= h
->root
.root
.string
;
1388 /* If we have encountered a definition for this symbol. */
1389 if (h
->root
.type
== bfd_link_hash_defined
1390 || h
->root
.type
== bfd_link_hash_defweak
)
1392 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1393 reloc_data
.sym_section
= h
->root
.u
.def
.section
;
1395 reloc_data
.should_relocate
= TRUE
;
1397 if (is_reloc_for_GOT (howto
) && !bfd_link_pic (info
))
1399 /* TODO: Change it to use arc_do_relocation with
1400 ARC_32 reloc. Try to use ADD_RELA macro. */
1401 bfd_vma relocation
=
1402 reloc_data
.sym_value
+ reloc_data
.reloc_addend
1403 + (reloc_data
.sym_section
->output_section
!= NULL
?
1404 (reloc_data
.sym_section
->output_offset
1405 + reloc_data
.sym_section
->output_section
->vma
)
1408 BFD_ASSERT (h
->got
.glist
);
1409 bfd_vma got_offset
= h
->got
.glist
->offset
;
1410 bfd_put_32 (output_bfd
, relocation
,
1411 htab
->sgot
->contents
+ got_offset
);
1413 if (is_reloc_for_PLT (howto
) && h
->plt
.offset
!= (bfd_vma
) -1)
1415 /* TODO: This is repeated up here. */
1416 reloc_data
.sym_value
= h
->plt
.offset
;
1417 reloc_data
.sym_section
= htab
->splt
;
1420 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1422 /* Is weak symbol and has no definition. */
1423 if (is_reloc_for_GOT (howto
))
1425 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1426 reloc_data
.sym_section
= htab
->sgot
;
1427 reloc_data
.should_relocate
= TRUE
;
1429 else if (is_reloc_for_PLT (howto
)
1430 && h
->plt
.offset
!= (bfd_vma
) -1)
1432 /* TODO: This is repeated up here. */
1433 reloc_data
.sym_value
= h
->plt
.offset
;
1434 reloc_data
.sym_section
= htab
->splt
;
1435 reloc_data
.should_relocate
= TRUE
;
1442 if (is_reloc_for_GOT (howto
))
1444 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1445 reloc_data
.sym_section
= htab
->sgot
;
1447 reloc_data
.should_relocate
= TRUE
;
1449 else if (is_reloc_for_PLT (howto
))
1451 /* Fail if it is linking for PIE and the symbol is
1453 if (bfd_link_executable (info
))
1454 (*info
->callbacks
->undefined_symbol
)
1455 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
1456 rel
->r_offset
, TRUE
);
1457 reloc_data
.sym_value
= h
->plt
.offset
;
1458 reloc_data
.sym_section
= htab
->splt
;
1460 reloc_data
.should_relocate
= TRUE
;
1462 else if (!bfd_link_pic (info
))
1463 (*info
->callbacks
->undefined_symbol
)
1464 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
1465 rel
->r_offset
, TRUE
);
1468 if (h
->got
.glist
!= NULL
)
1470 struct got_entry
*entry
= h
->got
.glist
;
1472 if (is_reloc_for_GOT (howto
) || is_reloc_for_TLS (howto
))
1474 if (! elf_hash_table (info
)->dynamic_sections_created
1475 || (bfd_link_pic (info
)
1476 && SYMBOL_REFERENCES_LOCAL (info
, h
)))
1478 reloc_data
.sym_value
= h
->root
.u
.def
.value
;
1479 reloc_data
.sym_section
= h
->root
.u
.def
.section
;
1481 if (is_reloc_for_TLS (howto
))
1482 while (entry
->type
== GOT_NORMAL
&& entry
->next
!= NULL
)
1483 entry
= entry
->next
;
1485 if (entry
->processed
== FALSE
1486 && (entry
->type
== GOT_TLS_GD
1487 || entry
->type
== GOT_TLS_IE
))
1489 bfd_vma sym_value
= h
->root
.u
.def
.value
1490 + h
->root
.u
.def
.section
->output_section
->vma
1491 + h
->root
.u
.def
.section
->output_offset
;
1494 elf_hash_table (info
)->tls_sec
->output_section
->vma
;
1496 bfd_put_32 (output_bfd
,
1497 sym_value
- sec_vma
,
1498 htab
->sgot
->contents
+ entry
->offset
1499 + (entry
->existing_entries
== TLS_GOT_MOD_AND_OFF
? 4 : 0));
1501 ARC_DEBUG ("arc_info: FIXED -> %s value = 0x%x "
1502 "@ 0x%x, for symbol %s\n",
1503 (entry
->type
== GOT_TLS_GD
? "GOT_TLS_GD" :
1505 sym_value
- sec_vma
,
1506 htab
->sgot
->contents
+ entry
->offset
1507 + (entry
->existing_entries
== TLS_GOT_MOD_AND_OFF
? 4 : 0),
1508 h
->root
.root
.string
);
1510 entry
->processed
= TRUE
;
1513 if (entry
->type
== GOT_TLS_IE
&& entry
->processed
== FALSE
)
1515 bfd_vma sec_vma
= htab
->tls_sec
->output_section
->vma
;
1516 bfd_put_32 (output_bfd
,
1517 reloc_data
.sym_value
- sec_vma
,
1518 htab
->sgot
->contents
+ entry
->offset
);
1521 if (entry
->type
== GOT_NORMAL
&& entry
->processed
== FALSE
)
1524 reloc_data
.sym_section
->output_section
->vma
1525 + reloc_data
.sym_section
->output_offset
;
1527 if (h
->root
.type
!= bfd_link_hash_undefweak
)
1529 bfd_put_32 (output_bfd
,
1530 reloc_data
.sym_value
+ sec_vma
,
1531 htab
->sgot
->contents
+ entry
->offset
);
1533 ARC_DEBUG ("arc_info: PATCHED: 0x%08x "
1534 "@ 0x%08x for sym %s in got offset 0x%x\n",
1535 reloc_data
.sym_value
+ sec_vma
,
1536 htab
->sgot
->output_section
->vma
1537 + htab
->sgot
->output_offset
+ entry
->offset
,
1538 h
->root
.root
.string
,
1543 ARC_DEBUG ("arc_info: PATCHED: NOT_PATCHED "
1544 "@ 0x%08x for sym %s in got offset 0x%x "
1546 htab
->sgot
->output_section
->vma
1547 + htab
->sgot
->output_offset
+ entry
->offset
,
1548 h
->root
.root
.string
,
1552 entry
->processed
= TRUE
;
1557 reloc_data
.got_offset_value
= entry
->offset
;
1559 ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
1560 "vma = 0x%x for symbol %s\n",
1561 entry
->type
, entry
->offset
,
1562 htab
->sgot
->output_section
->vma
1563 + htab
->sgot
->output_offset
+ entry
->offset
,
1564 h
->root
.root
.string
);
1567 BFD_ASSERT (htab
->sgot
!= NULL
|| !is_reloc_for_GOT (howto
));
1568 if (htab
->sgot
!= NULL
)
1569 reloc_data
.got_symbol_vma
= htab
->sgot
->output_section
->vma
1570 + htab
->sgot
->output_offset
;
1578 case R_ARC_32_PCREL
:
1579 if ((bfd_link_pic (info
) || bfd_link_pie (info
))
1580 && ((r_type
!= R_ARC_PC32
&& r_type
!= R_ARC_32_PCREL
)
1583 && (!info
->symbolic
|| !h
->def_regular
))))
1585 Elf_Internal_Rela outrel
;
1587 bfd_boolean skip
= FALSE
;
1588 bfd_boolean relocate
= FALSE
;
1589 asection
*sreloc
= _bfd_elf_get_dynamic_reloc_section
1590 (input_bfd
, input_section
,
1593 BFD_ASSERT (sreloc
!= NULL
);
1595 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
1599 if (outrel
.r_offset
== (bfd_vma
) -1)
1602 outrel
.r_addend
= rel
->r_addend
;
1603 outrel
.r_offset
+= (input_section
->output_section
->vma
1604 + input_section
->output_offset
);
1606 #define IS_ARC_PCREL_TYPE(TYPE) \
1607 ( (TYPE == R_ARC_PC32) \
1608 || (TYPE == R_ARC_32_PCREL))
1611 memset (&outrel
, 0, sizeof outrel
);
1616 && ((IS_ARC_PCREL_TYPE (r_type
))
1617 || !(bfd_link_executable (info
)
1618 || SYMBOLIC_BIND (info
, h
))
1619 || ! h
->def_regular
))
1621 BFD_ASSERT (h
!= NULL
);
1622 if ((input_section
->flags
& SEC_ALLOC
) != 0)
1627 BFD_ASSERT (h
->dynindx
!= -1);
1628 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
1632 /* Handle local symbols, they either do not have a
1633 global hash table entry (h == NULL), or are
1634 forced local due to a version script
1635 (h->forced_local), or the third condition is
1636 legacy, it appears to say something like, for
1637 links where we are pre-binding the symbols, or
1638 there's not an entry for this symbol in the
1639 dynamic symbol table, and it's a regular symbol
1640 not defined in a shared object, then treat the
1641 symbol as local, resolve it now. */
1643 /* outrel.r_addend = 0; */
1644 outrel
.r_info
= ELF32_R_INFO (0, R_ARC_RELATIVE
);
1647 BFD_ASSERT (sreloc
->contents
!= 0);
1649 loc
= sreloc
->contents
;
1650 loc
+= sreloc
->reloc_count
* sizeof (Elf32_External_Rela
);
1651 sreloc
->reloc_count
+= 1;
1653 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1655 if (relocate
== FALSE
)
1663 if (is_reloc_SDA_relative (howto
)
1664 && (reloc_data
.sdata_begin_symbol_vma_set
== FALSE
))
1666 (*_bfd_error_handler
)
1667 ("Error: Linker symbol __SDATA_BEGIN__ not found");
1668 bfd_set_error (bfd_error_bad_value
);
1672 DEBUG_ARC_RELOC (reloc_data
);
1674 /* Make sure we have with a dynamic linker. In case of GOT and PLT
1675 the sym_section should point to .got or .plt respectively. */
1676 if (is_reloc_for_GOT (howto
) || is_reloc_for_PLT (howto
))
1678 (*_bfd_error_handler
)
1679 (_("GOT and PLT relocations cannot be fixed with a non dynamic linker."));
1680 bfd_set_error (bfd_error_bad_value
);
1684 if (arc_do_relocation (contents
, reloc_data
, info
) != bfd_reloc_ok
)
1691 static struct dynamic_sections
1692 arc_create_dynamic_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1694 struct elf_link_hash_table
*htab
;
1696 struct dynamic_sections ds
=
1698 .initialized
= FALSE
,
1708 htab
= elf_hash_table (info
);
1711 /* Create dynamic sections for relocatable executables so that we
1712 can copy relocations. */
1713 if (! htab
->dynamic_sections_created
&& bfd_link_pic (info
))
1715 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
1719 dynobj
= (elf_hash_table (info
))->dynobj
;
1723 ds
.sgot
= htab
->sgot
;
1724 ds
.srelgot
= htab
->srelgot
;
1726 ds
.sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
1727 ds
.srelgotplt
= ds
.srelplt
;
1729 ds
.splt
= bfd_get_section_by_name (dynobj
, ".plt");
1730 ds
.srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
1733 if (htab
->dynamic_sections_created
)
1735 ds
.sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1738 ds
.initialized
= TRUE
;
1743 #define ADD_SYMBOL_REF_SEC_AND_RELOC(SECNAME, COND_FOR_RELOC, H) \
1744 htab->s##SECNAME->size; \
1746 if (COND_FOR_RELOC) \
1748 htab->srel##SECNAME->size += sizeof (Elf32_External_Rela); \
1749 ARC_DEBUG ("arc_info: Added reloc space in " \
1750 #SECNAME " section at " __FILE__ \
1751 ":%d for symbol\n", \
1752 __LINE__, name_for_global_symbol (H)); \
1755 if (h->dynindx == -1 && !h->forced_local) \
1756 if (! bfd_elf_link_record_dynamic_symbol (info, H)) \
1758 htab->s##SECNAME->size += 4; \
1762 elf_arc_check_relocs (bfd
* abfd
,
1763 struct bfd_link_info
* info
,
1765 const Elf_Internal_Rela
* relocs
)
1767 Elf_Internal_Shdr
* symtab_hdr
;
1768 struct elf_link_hash_entry
** sym_hashes
;
1769 struct got_entry
** local_got_ents
;
1770 const Elf_Internal_Rela
* rel
;
1771 const Elf_Internal_Rela
* rel_end
;
1773 asection
* sreloc
= NULL
;
1774 struct elf_link_hash_table
* htab
= elf_hash_table (info
);
1776 if (bfd_link_relocatable (info
))
1779 dynobj
= (elf_hash_table (info
))->dynobj
;
1780 symtab_hdr
= &((elf_tdata (abfd
))->symtab_hdr
);
1781 sym_hashes
= elf_sym_hashes (abfd
);
1782 local_got_ents
= arc_get_local_got_ents (abfd
);
1784 rel_end
= relocs
+ sec
->reloc_count
;
1785 for (rel
= relocs
; rel
< rel_end
; rel
++)
1787 enum elf_arc_reloc_type r_type
;
1788 reloc_howto_type
*howto
;
1789 unsigned long r_symndx
;
1790 struct elf_link_hash_entry
*h
;
1792 r_type
= ELF32_R_TYPE (rel
->r_info
);
1794 if (r_type
>= (int) R_ARC_max
)
1796 bfd_set_error (bfd_error_bad_value
);
1799 howto
= arc_elf_howto (r_type
);
1802 && (is_reloc_for_GOT (howto
) == TRUE
1803 || is_reloc_for_TLS (howto
) == TRUE
))
1805 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
1806 if (! _bfd_elf_create_got_section (abfd
, info
))
1810 /* Load symbol information. */
1811 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1812 if (r_symndx
< symtab_hdr
->sh_info
) /* Is a local symbol. */
1814 else /* Global one. */
1815 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1821 /* During shared library creation, these relocs should not
1822 appear in a shared library (as memory will be read only
1823 and the dynamic linker can not resolve these. However
1824 the error should not occur for e.g. debugging or
1825 non-readonly sections. */
1826 if ((bfd_link_dll (info
) && !bfd_link_pie (info
))
1827 && (sec
->flags
& SEC_ALLOC
) != 0
1828 && (sec
->flags
& SEC_READONLY
) != 0
1829 && ((sec
->flags
& SEC_CODE
) != 0
1830 || (sec
->flags
& SEC_DEBUGGING
) != 0))
1834 name
= h
->root
.root
.string
;
1836 /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
1838 (*_bfd_error_handler
)
1840 %B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1842 arc_elf_howto (r_type
)->name
,
1844 bfd_set_error (bfd_error_bad_value
);
1848 /* In some cases we are not setting the 'non_got_ref'
1849 flag, even though the relocations don't require a GOT
1850 access. We should extend the testing in this area to
1851 ensure that no significant cases are being missed. */
1856 case R_ARC_32_PCREL
:
1857 if ((bfd_link_pic (info
) || bfd_link_pie (info
))
1858 && ((r_type
!= R_ARC_PC32
&& r_type
!= R_ARC_32_PCREL
)
1861 && (!info
->symbolic
|| !h
->def_regular
))))
1865 sreloc
= _bfd_elf_make_dynamic_reloc_section (sec
, dynobj
,
1873 sreloc
->size
+= sizeof (Elf32_External_Rela
);
1880 if (is_reloc_for_PLT (howto
) == TRUE
)
1888 if (is_reloc_for_GOT (howto
) == TRUE
)
1893 if (local_got_ents
[r_symndx
] == NULL
)
1896 ADD_SYMBOL_REF_SEC_AND_RELOC (got
,
1897 bfd_link_pic (info
),
1899 new_got_entry_to_list (&(local_got_ents
[r_symndx
]),
1900 GOT_NORMAL
, offset
, TLS_GOT_NONE
);
1905 /* Global symbol. */
1906 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1907 if (h
->got
.glist
== NULL
)
1910 ADD_SYMBOL_REF_SEC_AND_RELOC (got
, TRUE
, h
);
1911 new_got_entry_to_list (&h
->got
.glist
,
1912 GOT_NORMAL
, offset
, TLS_GOT_NONE
);
1917 if (is_reloc_for_TLS (howto
) == TRUE
)
1919 enum tls_type_e type
= GOT_UNKNOWN
;
1923 case R_ARC_TLS_GD_GOT
:
1926 case R_ARC_TLS_IE_GOT
:
1933 struct got_entry
**list
= NULL
;
1935 list
= &(h
->got
.glist
);
1937 list
= &(local_got_ents
[r_symndx
]);
1939 if (type
!= GOT_UNKNOWN
&& !symbol_has_entry_of_type (*list
, type
))
1941 enum tls_got_entries entries
= TLS_GOT_NONE
;
1943 ADD_SYMBOL_REF_SEC_AND_RELOC (got
, TRUE
, h
);
1945 if (type
== GOT_TLS_GD
)
1947 bfd_vma ATTRIBUTE_UNUSED notneeded
=
1948 ADD_SYMBOL_REF_SEC_AND_RELOC (got
, TRUE
, h
);
1949 entries
= TLS_GOT_MOD_AND_OFF
;
1952 if (entries
== TLS_GOT_NONE
)
1953 entries
= TLS_GOT_OFF
;
1955 new_got_entry_to_list (list
, type
, offset
, entries
);
1963 #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
1965 static struct plt_version_t
*
1966 arc_get_plt_version (struct bfd_link_info
*info
)
1970 for (i
= 0; i
< 1; i
++)
1972 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i
,
1973 plt_versions
[i
].entry_size
,
1974 plt_versions
[i
].elem_size
);
1977 if (bfd_get_mach (info
->output_bfd
) == bfd_mach_arc_arcv2
)
1979 if (bfd_link_pic (info
))
1980 return &(plt_versions
[ELF_ARCV2_PIC
]);
1982 return &(plt_versions
[ELF_ARCV2_ABS
]);
1986 if (bfd_link_pic (info
))
1987 return &(plt_versions
[ELF_ARC_PIC
]);
1989 return &(plt_versions
[ELF_ARC_ABS
]);
1994 add_symbol_to_plt (struct bfd_link_info
*info
)
1996 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1999 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
2001 /* If this is the first .plt entry, make room for the special first
2003 if (htab
->splt
->size
== 0)
2004 htab
->splt
->size
+= plt_data
->entry_size
;
2006 ret
= htab
->splt
->size
;
2008 htab
->splt
->size
+= plt_data
->elem_size
;
2009 ARC_DEBUG ("PLT_SIZE = %d\n", htab
->splt
->size
);
2011 htab
->sgotplt
->size
+= 4;
2012 htab
->srelplt
->size
+= sizeof (Elf32_External_Rela
);
2017 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
2018 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
2021 plt_do_relocs_for_symbol (bfd
*abfd
,
2022 struct elf_link_hash_table
*htab
,
2023 const struct plt_reloc
*reloc
,
2025 bfd_vma symbol_got_offset
)
2027 while (SYM_ONLY (reloc
->symbol
) != LAST_RELOC
)
2029 bfd_vma relocation
= 0;
2031 switch (SYM_ONLY (reloc
->symbol
))
2035 htab
->sgotplt
->output_section
->vma
+
2036 htab
->sgotplt
->output_offset
+ symbol_got_offset
;
2039 relocation
+= reloc
->addend
;
2041 if (IS_RELATIVE (reloc
->symbol
))
2043 bfd_vma reloc_offset
= reloc
->offset
;
2044 reloc_offset
-= (IS_INSN_32 (reloc
->symbol
)) ? 4 : 0;
2045 reloc_offset
-= (IS_INSN_24 (reloc
->symbol
)) ? 2 : 0;
2047 relocation
-= htab
->splt
->output_section
->vma
2048 + htab
->splt
->output_offset
2049 + plt_offset
+ reloc_offset
;
2052 /* TODO: being ME is not a property of the relocation but of the
2053 section of which is applying the relocation. */
2054 if (IS_MIDDLE_ENDIAN (reloc
->symbol
) && !bfd_big_endian (abfd
))
2057 ((relocation
& 0xffff0000) >> 16) |
2058 ((relocation
& 0xffff) << 16);
2061 switch (reloc
->size
)
2064 bfd_put_32 (htab
->splt
->output_section
->owner
,
2066 htab
->splt
->contents
+ plt_offset
+ reloc
->offset
);
2070 reloc
= &(reloc
[1]); /* Jump to next relocation. */
2075 relocate_plt_for_symbol (bfd
*output_bfd
,
2076 struct bfd_link_info
*info
,
2077 struct elf_link_hash_entry
*h
)
2079 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
2080 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2082 bfd_vma plt_index
= (h
->plt
.offset
- plt_data
->entry_size
)
2083 / plt_data
->elem_size
;
2084 bfd_vma got_offset
= (plt_index
+ 3) * 4;
2086 ARC_DEBUG ("arc_info: PLT_OFFSET = 0x%x, PLT_ENTRY_VMA = 0x%x, \
2087 GOT_ENTRY_OFFSET = 0x%x, GOT_ENTRY_VMA = 0x%x, for symbol %s\n",
2089 htab
->splt
->output_section
->vma
2090 + htab
->splt
->output_offset
2093 htab
->sgotplt
->output_section
->vma
2094 + htab
->sgotplt
->output_offset
2096 h
->root
.root
.string
);
2101 uint16_t *ptr
= (uint16_t *) plt_data
->elem
;
2102 for (i
= 0; i
< plt_data
->elem_size
/2; i
++)
2104 uint16_t data
= ptr
[i
];
2105 bfd_put_16 (output_bfd
,
2107 htab
->splt
->contents
+ h
->plt
.offset
+ (i
*2));
2111 plt_do_relocs_for_symbol (output_bfd
, htab
,
2112 plt_data
->elem_relocs
,
2116 /* Fill in the entry in the global offset table. */
2117 bfd_put_32 (output_bfd
,
2118 (bfd_vma
) (htab
->splt
->output_section
->vma
2119 + htab
->splt
->output_offset
),
2120 htab
->sgotplt
->contents
+ got_offset
);
2122 /* TODO: Fill in the entry in the .rela.plt section. */
2124 Elf_Internal_Rela rel
;
2127 rel
.r_offset
= (htab
->sgotplt
->output_section
->vma
2128 + htab
->sgotplt
->output_offset
2132 BFD_ASSERT (h
->dynindx
!= -1);
2133 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_ARC_JMP_SLOT
);
2135 loc
= htab
->srelplt
->contents
;
2136 loc
+= plt_index
* sizeof (Elf32_External_Rela
); /* relA */
2137 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
2142 relocate_plt_for_entry (bfd
*abfd
,
2143 struct bfd_link_info
*info
)
2145 struct plt_version_t
*plt_data
= arc_get_plt_version (info
);
2146 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2150 uint16_t *ptr
= (uint16_t *) plt_data
->entry
;
2151 for (i
= 0; i
< plt_data
->entry_size
/2; i
++)
2153 uint16_t data
= ptr
[i
];
2156 htab
->splt
->contents
+ (i
*2));
2159 PLT_DO_RELOCS_FOR_ENTRY (abfd
, htab
, plt_data
->entry_relocs
);
2162 /* Desc : Adjust a symbol defined by a dynamic object and referenced
2163 by a regular object. The current definition is in some section of
2164 the dynamic object, but we're not including those sections. We
2165 have to change the definition to something the rest of the link can
2169 elf_arc_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2170 struct elf_link_hash_entry
*h
)
2173 bfd
*dynobj
= (elf_hash_table (info
))->dynobj
;
2174 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2176 if (h
->type
== STT_FUNC
2177 || h
->type
== STT_GNU_IFUNC
2178 || h
->needs_plt
== 1)
2180 if (!bfd_link_pic (info
) && !h
->def_dynamic
&& !h
->ref_dynamic
)
2182 /* This case can occur if we saw a PLT32 reloc in an input
2183 file, but the symbol was never referred to by a dynamic
2184 object. In such a case, we don't actually need to build
2185 a procedure linkage table, and we can just do a PC32
2187 BFD_ASSERT (h
->needs_plt
);
2191 /* Make sure this symbol is output as a dynamic symbol. */
2192 if (h
->dynindx
== -1 && !h
->forced_local
2193 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
2196 if (bfd_link_pic (info
)
2197 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2199 bfd_vma loc
= add_symbol_to_plt (info
);
2201 if (!bfd_link_pic (info
) && !h
->def_regular
)
2203 h
->root
.u
.def
.section
= htab
->splt
;
2204 h
->root
.u
.def
.value
= loc
;
2206 h
->plt
.offset
= loc
;
2210 h
->plt
.offset
= (bfd_vma
) -1;
2216 /* If this is a weak symbol, and there is a real definition, the
2217 processor independent code will have arranged for us to see the
2218 real definition first, and we can just use the same value. */
2219 if (h
->u
.weakdef
!= NULL
)
2221 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
2222 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
2223 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
2224 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
2228 /* This is a reference to a symbol defined by a dynamic object which
2229 is not a function. */
2231 /* If we are creating a shared library, we must presume that the
2232 only references to the symbol are via the global offset table.
2233 For such cases we need not do anything here; the relocations will
2234 be handled correctly by relocate_section. */
2235 if (!bfd_link_executable (info
))
2238 /* If there are no non-GOT references, we do not need a copy
2240 if (!h
->non_got_ref
)
2243 /* If -z nocopyreloc was given, we won't generate them either. */
2244 if (info
->nocopyreloc
)
2250 /* We must allocate the symbol in our .dynbss section, which will
2251 become part of the .bss section of the executable. There will be
2252 an entry for this symbol in the .dynsym section. The dynamic
2253 object will contain position independent code, so all references
2254 from the dynamic object to this symbol will go through the global
2255 offset table. The dynamic linker will use the .dynsym entry to
2256 determine the address it must put in the global offset table, so
2257 both the dynamic object and the regular object will refer to the
2258 same memory location for the variable. */
2263 /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
2264 copy the initial value out of the dynamic object and into the
2265 runtime process image. We need to remember the offset into the
2266 .rela.bss section we are going to use. */
2267 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
2271 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
2272 BFD_ASSERT (srel
!= NULL
);
2273 srel
->size
+= sizeof (Elf32_External_Rela
);
2277 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
2278 BFD_ASSERT (s
!= NULL
);
2280 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
2283 /* Function : elf_arc_finish_dynamic_symbol
2284 Brief : Finish up dynamic symbol handling. We set the
2285 contents of various dynamic sections here.
2290 Returns : True/False as the return status. */
2293 elf_arc_finish_dynamic_symbol (bfd
* output_bfd
,
2294 struct bfd_link_info
*info
,
2295 struct elf_link_hash_entry
*h
,
2296 Elf_Internal_Sym
* sym
)
2298 if (h
->plt
.offset
!= (bfd_vma
) -1)
2300 relocate_plt_for_symbol (output_bfd
, info
, h
);
2302 if (!h
->def_regular
)
2304 /* Mark the symbol as undefined, rather than as defined in
2305 the .plt section. Leave the value alone. */
2306 sym
->st_shndx
= SHN_UNDEF
;
2310 if (h
->got
.glist
!= NULL
)
2312 struct got_entry
*list
= h
->got
.glist
;
2314 /* Traverse the list of got entries for this symbol. */
2317 bfd_vma got_offset
= h
->got
.glist
->offset
;
2319 if (list
->type
== GOT_NORMAL
2320 && list
->created_dyn_relocation
== FALSE
)
2322 if (bfd_link_pic (info
)
2323 && (info
->symbolic
|| h
->dynindx
== -1)
2326 ADD_RELA (output_bfd
, got
, got_offset
, 0, R_ARC_RELATIVE
, 0);
2328 /* Do not fully understand the side effects of this condition.
2329 The relocation space might still being reserved. Perhaps
2330 I should clear its value. */
2331 else if (h
->dynindx
!= -1)
2333 ADD_RELA (output_bfd
, got
, got_offset
, h
->dynindx
,
2336 list
->created_dyn_relocation
= TRUE
;
2338 else if (list
->existing_entries
!= TLS_GOT_NONE
)
2340 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2341 enum tls_got_entries e
= list
->existing_entries
;
2343 BFD_ASSERT (list
->type
!= GOT_TLS_GD
2344 || list
->existing_entries
== TLS_GOT_MOD_AND_OFF
);
2346 bfd_vma dynindx
= h
->dynindx
== -1 ? 0 : h
->dynindx
;
2347 if (e
== TLS_GOT_MOD_AND_OFF
|| e
== TLS_GOT_MOD
)
2349 ADD_RELA (output_bfd
, got
, got_offset
, dynindx
,
2350 R_ARC_TLS_DTPMOD
, 0);
2351 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2352 GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2355 htab
->sgot
->output_section
->vma
2356 + htab
->sgot
->output_offset
+ got_offset
,
2359 if (e
== TLS_GOT_MOD_AND_OFF
|| e
== TLS_GOT_OFF
)
2362 if (list
->type
== GOT_TLS_IE
)
2363 addend
= bfd_get_32 (output_bfd
,
2364 htab
->sgot
->contents
+ got_offset
);
2366 ADD_RELA (output_bfd
, got
,
2367 got_offset
+ (e
== TLS_GOT_MOD_AND_OFF
? 4 : 0),
2369 (list
->type
== GOT_TLS_IE
?
2370 R_ARC_TLS_TPOFF
: R_ARC_TLS_DTPOFF
),
2373 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2374 GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2377 htab
->sgot
->output_section
->vma
2378 + htab
->sgot
->output_offset
+ got_offset
,
2386 h
->got
.glist
= NULL
;
2391 bfd_vma rel_offset
= (h
->root
.u
.def
.value
2392 + h
->root
.u
.def
.section
->output_section
->vma
2393 + h
->root
.u
.def
.section
->output_offset
);
2396 bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
2399 bfd_byte
* loc
= srelbss
->contents
2400 + (srelbss
->reloc_count
* sizeof (Elf32_External_Rela
));
2401 srelbss
->reloc_count
++;
2403 Elf_Internal_Rela rel
;
2405 rel
.r_offset
= rel_offset
;
2407 BFD_ASSERT (h
->dynindx
!= -1);
2408 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_ARC_COPY
);
2410 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
2413 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2414 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
2415 || strcmp (h
->root
.root
.string
, "__DYNAMIC") == 0
2416 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
2417 sym
->st_shndx
= SHN_ABS
;
2422 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \
2424 if (SYMBOL != NULL) \
2425 h = elf_link_hash_lookup (elf_hash_table (info), \
2426 SYMBOL, FALSE, FALSE, TRUE); \
2427 else if (SECTION != NULL) \
2428 s = bfd_get_linker_section (dynobj, SECTION); \
2431 /* Function : elf_arc_finish_dynamic_sections
2432 Brief : Finish up the dynamic sections handling.
2437 Returns : True/False as the return status. */
2440 elf_arc_finish_dynamic_sections (bfd
* output_bfd
,
2441 struct bfd_link_info
*info
)
2443 struct dynamic_sections ds
= arc_create_dynamic_sections (output_bfd
, info
);
2444 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2445 bfd
*dynobj
= (elf_hash_table (info
))->dynobj
;
2449 Elf32_External_Dyn
*dyncon
, *dynconend
;
2451 dyncon
= (Elf32_External_Dyn
*) ds
.sdyn
->contents
;
2453 (Elf32_External_Dyn
*) (ds
.sdyn
->contents
+ ds
.sdyn
->size
);
2454 for (; dyncon
< dynconend
; dyncon
++)
2456 Elf_Internal_Dyn internal_dyn
;
2457 bfd_boolean do_it
= FALSE
;
2459 struct elf_link_hash_entry
*h
= NULL
;
2462 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &internal_dyn
);
2464 switch (internal_dyn
.d_tag
)
2466 GET_SYMBOL_OR_SECTION (DT_INIT
, "_init", NULL
)
2467 GET_SYMBOL_OR_SECTION (DT_FINI
, "_fini", NULL
)
2468 GET_SYMBOL_OR_SECTION (DT_PLTGOT
, NULL
, ".plt")
2469 GET_SYMBOL_OR_SECTION (DT_JMPREL
, NULL
, ".rela.plt")
2470 GET_SYMBOL_OR_SECTION (DT_PLTRELSZ
, NULL
, ".rela.plt")
2471 GET_SYMBOL_OR_SECTION (DT_RELASZ
, NULL
, ".rela.plt")
2472 GET_SYMBOL_OR_SECTION (DT_VERSYM
, NULL
, ".gnu.version")
2473 GET_SYMBOL_OR_SECTION (DT_VERDEF
, NULL
, ".gnu.version_d")
2474 GET_SYMBOL_OR_SECTION (DT_VERNEED
, NULL
, ".gnu.version_r")
2479 /* In case the dynamic symbols should be updated with a symbol. */
2481 && (h
->root
.type
== bfd_link_hash_defined
2482 || h
->root
.type
== bfd_link_hash_defweak
))
2486 internal_dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
2487 asec_ptr
= h
->root
.u
.def
.section
;
2488 if (asec_ptr
->output_section
!= NULL
)
2490 internal_dyn
.d_un
.d_val
+=
2491 (asec_ptr
->output_section
->vma
+
2492 asec_ptr
->output_offset
);
2496 /* The symbol is imported from another shared
2497 library and does not apply to this one. */
2498 internal_dyn
.d_un
.d_val
= 0;
2502 else if (s
!= NULL
) /* With a section information. */
2504 switch (internal_dyn
.d_tag
)
2511 internal_dyn
.d_un
.d_ptr
= (s
->output_section
->vma
2512 + s
->output_offset
);
2517 internal_dyn
.d_un
.d_val
= s
->size
;
2523 internal_dyn
.d_un
.d_val
-= s
->size
;
2533 bfd_elf32_swap_dyn_out (output_bfd
, &internal_dyn
, dyncon
);
2536 if (htab
->splt
->size
> 0)
2538 relocate_plt_for_entry (output_bfd
, info
);
2541 /* TODO: Validate this. */
2542 elf_section_data (htab
->srelplt
->output_section
)->this_hdr
.sh_entsize
2546 /* Fill in the first three entries in the global offset table. */
2549 struct elf_link_hash_entry
*h
;
2550 h
= elf_link_hash_lookup (elf_hash_table (info
), "_GLOBAL_OFFSET_TABLE_",
2551 FALSE
, FALSE
, TRUE
);
2553 if (h
!= NULL
&& h
->root
.type
!= bfd_link_hash_undefined
2554 && h
->root
.u
.def
.section
!= NULL
)
2556 asection
*sec
= h
->root
.u
.def
.section
;
2558 if (ds
.sdyn
== NULL
)
2559 bfd_put_32 (output_bfd
, (bfd_vma
) 0,
2562 bfd_put_32 (output_bfd
,
2563 ds
.sdyn
->output_section
->vma
+ ds
.sdyn
->output_offset
,
2565 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sec
->contents
+ 4);
2566 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sec
->contents
+ 8);
2573 #define ADD_DYNAMIC_SYMBOL(NAME, TAG) \
2574 h = elf_link_hash_lookup (elf_hash_table (info), \
2575 NAME, FALSE, FALSE, FALSE); \
2576 if ((h != NULL && (h->ref_regular || h->def_regular))) \
2577 if (! _bfd_elf_add_dynamic_entry (info, TAG, 0)) \
2580 /* Set the sizes of the dynamic sections. */
2582 elf_arc_size_dynamic_sections (bfd
* output_bfd
,
2583 struct bfd_link_info
*info
)
2587 bfd_boolean relocs_exist
= FALSE
;
2588 bfd_boolean reltext_exist
= FALSE
;
2589 struct dynamic_sections ds
= arc_create_dynamic_sections (output_bfd
, info
);
2590 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2592 dynobj
= (elf_hash_table (info
))->dynobj
;
2593 BFD_ASSERT (dynobj
!= NULL
);
2595 if ((elf_hash_table (info
))->dynamic_sections_created
)
2597 struct elf_link_hash_entry
*h
;
2599 /* Set the contents of the .interp section to the
2601 if (!bfd_link_pic (info
))
2603 s
= bfd_get_section_by_name (dynobj
, ".interp");
2604 BFD_ASSERT (s
!= NULL
);
2605 s
->size
= sizeof (ELF_DYNAMIC_INTERPRETER
);
2606 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2609 /* Add some entries to the .dynamic section. We fill in some of
2610 the values later, in elf_bfd_final_link, but we must add the
2611 entries now so that we know the final size of the .dynamic
2612 section. Checking if the .init section is present. We also
2613 create DT_INIT and DT_FINI entries if the init_str has been
2614 changed by the user. */
2615 ADD_DYNAMIC_SYMBOL ("init", DT_INIT
);
2616 ADD_DYNAMIC_SYMBOL ("fini", DT_FINI
);
2620 /* We may have created entries in the .rela.got section.
2621 However, if we are not creating the dynamic sections, we will
2622 not actually use these entries. Reset the size of .rela.got,
2623 which will cause it to get stripped from the output file
2625 if (htab
->srelgot
!= NULL
)
2626 htab
->srelgot
->size
= 0;
2629 if (htab
->splt
!= NULL
&& htab
->splt
->size
== 0)
2630 htab
->splt
->flags
|= SEC_EXCLUDE
;
2631 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2633 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2636 if (strncmp (s
->name
, ".rela", 5) == 0)
2640 s
->flags
|= SEC_EXCLUDE
;
2644 if (strcmp (s
->name
, ".rela.plt") != 0)
2646 const char *outname
=
2647 bfd_get_section_name (output_bfd
,
2648 htab
->srelplt
->output_section
);
2650 asection
*target
= bfd_get_section_by_name (output_bfd
,
2653 relocs_exist
= TRUE
;
2654 if (target
!= NULL
&& target
->size
!= 0
2655 && (target
->flags
& SEC_READONLY
) != 0
2656 && (target
->flags
& SEC_ALLOC
) != 0)
2657 reltext_exist
= TRUE
;
2661 /* We use the reloc_count field as a counter if we need to
2662 copy relocs into the output file. */
2666 if (strcmp (s
->name
, ".dynamic") == 0)
2670 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
2672 if (s
->contents
== NULL
&& s
->size
!= 0)
2678 /* TODO: Check if this is needed. */
2679 if (!bfd_link_pic (info
))
2680 if (!_bfd_elf_add_dynamic_entry (info
, DT_DEBUG
, 0))
2683 if (htab
->splt
&& (htab
->splt
->flags
& SEC_EXCLUDE
) == 0)
2684 if (!_bfd_elf_add_dynamic_entry (info
, DT_PLTGOT
, 0)
2685 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
2686 || !_bfd_elf_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
2687 || !_bfd_elf_add_dynamic_entry (info
, DT_JMPREL
, 0)
2691 if (relocs_exist
== TRUE
)
2692 if (!_bfd_elf_add_dynamic_entry (info
, DT_RELA
, 0)
2693 || !_bfd_elf_add_dynamic_entry (info
, DT_RELASZ
, 0)
2694 || !_bfd_elf_add_dynamic_entry (info
, DT_RELAENT
,
2695 sizeof (Elf32_External_Rela
))
2699 if (reltext_exist
== TRUE
)
2700 if (!_bfd_elf_add_dynamic_entry (info
, DT_TEXTREL
, 0))
2708 /* Classify dynamic relocs such that -z combreloc can reorder and combine
2710 static enum elf_reloc_type_class
2711 elf32_arc_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2712 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2713 const Elf_Internal_Rela
*rela
)
2715 switch ((int) ELF32_R_TYPE (rela
->r_info
))
2717 case R_ARC_RELATIVE
:
2718 return reloc_class_relative
;
2719 case R_ARC_JMP_SLOT
:
2720 return reloc_class_plt
;
2722 return reloc_class_copy
;
2723 /* TODO: Needed in future to support ifunc. */
2725 case R_ARC_IRELATIVE:
2726 return reloc_class_ifunc;
2729 return reloc_class_normal
;
2733 const struct elf_size_info arc_elf32_size_info
=
2735 sizeof (Elf32_External_Ehdr
),
2736 sizeof (Elf32_External_Phdr
),
2737 sizeof (Elf32_External_Shdr
),
2738 sizeof (Elf32_External_Rel
),
2739 sizeof (Elf32_External_Rela
),
2740 sizeof (Elf32_External_Sym
),
2741 sizeof (Elf32_External_Dyn
),
2742 sizeof (Elf_External_Note
),
2746 ELFCLASS32
, EV_CURRENT
,
2747 bfd_elf32_write_out_phdrs
,
2748 bfd_elf32_write_shdrs_and_ehdr
,
2749 bfd_elf32_checksum_contents
,
2750 bfd_elf32_write_relocs
,
2751 bfd_elf32_swap_symbol_in
,
2752 bfd_elf32_swap_symbol_out
,
2753 bfd_elf32_slurp_reloc_table
,
2754 bfd_elf32_slurp_symbol_table
,
2755 bfd_elf32_swap_dyn_in
,
2756 bfd_elf32_swap_dyn_out
,
2757 bfd_elf32_swap_reloc_in
,
2758 bfd_elf32_swap_reloc_out
,
2759 bfd_elf32_swap_reloca_in
,
2760 bfd_elf32_swap_reloca_out
2763 #define elf_backend_size_info arc_elf32_size_info
2765 static struct bfd_link_hash_table
*
2766 arc_elf_link_hash_table_create (bfd
*abfd
)
2768 struct elf_link_hash_table
*htab
;
2770 htab
= bfd_zmalloc (sizeof (*htab
));
2774 if (!_bfd_elf_link_hash_table_init (htab
, abfd
,
2775 _bfd_elf_link_hash_newfunc
,
2776 sizeof (struct elf_link_hash_entry
),
2783 htab
->init_got_refcount
.refcount
= 0;
2784 htab
->init_got_refcount
.glist
= NULL
;
2785 htab
->init_got_offset
.offset
= 0;
2786 htab
->init_got_offset
.glist
= NULL
;
2787 return (struct bfd_link_hash_table
*) htab
;
2790 /* Hook called by the linker routine which adds symbols from an object
2794 elf_arc_add_symbol_hook (bfd
* abfd
,
2795 struct bfd_link_info
* info
,
2796 Elf_Internal_Sym
* sym
,
2797 const char ** namep ATTRIBUTE_UNUSED
,
2798 flagword
* flagsp ATTRIBUTE_UNUSED
,
2799 asection
** secp ATTRIBUTE_UNUSED
,
2800 bfd_vma
* valp ATTRIBUTE_UNUSED
)
2802 if (ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
2803 && (abfd
->flags
& DYNAMIC
) == 0
2804 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
2805 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_ifunc
;
2810 #define TARGET_LITTLE_SYM arc_elf32_le_vec
2811 #define TARGET_LITTLE_NAME "elf32-littlearc"
2812 #define TARGET_BIG_SYM arc_elf32_be_vec
2813 #define TARGET_BIG_NAME "elf32-bigarc"
2814 #define ELF_ARCH bfd_arch_arc
2815 #define ELF_MACHINE_CODE EM_ARC_COMPACT
2816 #define ELF_MACHINE_ALT1 EM_ARC_COMPACT2
2817 #define ELF_MAXPAGESIZE 0x2000
2819 #define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create
2821 #define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data
2822 #define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup
2823 #define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags
2824 #define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data
2825 #define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data
2827 #define elf_info_to_howto_rel arc_info_to_howto_rel
2828 #define elf_backend_object_p arc_elf_object_p
2829 #define elf_backend_final_write_processing arc_elf_final_write_processing
2831 #define elf_backend_relocate_section elf_arc_relocate_section
2832 #define elf_backend_check_relocs elf_arc_check_relocs
2833 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2835 #define elf_backend_reloc_type_class elf32_arc_reloc_type_class
2837 #define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol
2838 #define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol
2840 #define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections
2841 #define elf_backend_size_dynamic_sections elf_arc_size_dynamic_sections
2842 #define elf_backend_add_symbol_hook elf_arc_add_symbol_hook
2844 #define elf_backend_can_gc_sections 1
2845 #define elf_backend_want_got_plt 1
2846 #define elf_backend_plt_readonly 1
2847 #define elf_backend_rela_plts_and_copies_p 1
2848 #define elf_backend_want_plt_sym 0
2849 #define elf_backend_got_header_size 12
2851 #define elf_backend_may_use_rel_p 0
2852 #define elf_backend_may_use_rela_p 1
2853 #define elf_backend_default_use_rela_p 1
2855 #define elf_backend_default_execstack 0
2857 #include "elf32-target.h"