1 /* KVX-specific support for NN-bit ELF.
2 Copyright (C) 2009-2023 Free Software Foundation, Inc.
3 Contributed by Kalray SA.
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; see the file COPYING3. If not,
19 see <http://www.gnu.org/licenses/>. */
23 #include "libiberty.h"
29 #include "elfxx-kvx.h"
34 #define LOG_FILE_ALIGN 3
38 #define LOG_FILE_ALIGN 2
41 #define IS_KVX_TLS_RELOC(R_TYPE) \
42 ((R_TYPE) == BFD_RELOC_KVX_S37_TLS_LE_LO10 \
43 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LE_UP27 \
44 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_LO10 \
45 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_UP27 \
46 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_EX6 \
47 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_DTPOFF_LO10 \
48 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_DTPOFF_UP27 \
49 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_LO10 \
50 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_UP27 \
51 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_EX6 \
52 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_IE_LO10 \
53 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_IE_UP27 \
54 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_LO10 \
55 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_UP27 \
56 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_EX6 \
57 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_GD_LO10 \
58 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_GD_UP27 \
59 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_LO10 \
60 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_UP27 \
61 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_EX6 \
62 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LD_LO10 \
63 || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LD_UP27 \
64 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_LO10 \
65 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_UP27 \
66 || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_EX6 \
69 #define IS_KVX_TLS_RELAX_RELOC(R_TYPE) 0
71 #define ELIMINATE_COPY_RELOCS 0
73 /* Return size of a relocation entry. HTAB is the bfd's
74 elf_kvx_link_hash_entry. */
75 #define RELOC_SIZE(HTAB) (sizeof (ElfNN_External_Rela))
77 /* GOT Entry size - 8 bytes in ELF64 and 4 bytes in ELF32. */
78 #define GOT_ENTRY_SIZE (ARCH_SIZE / 8)
79 #define PLT_ENTRY_SIZE (32)
81 #define PLT_SMALL_ENTRY_SIZE (4*4)
83 /* Encoding of the nop instruction */
84 #define INSN_NOP 0x00f0037f
86 #define kvx_compute_jump_table_size(htab) \
87 (((htab)->root.srelplt == NULL) ? 0 \
88 : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE)
90 static const bfd_byte elfNN_kvx_small_plt0_entry
[PLT_ENTRY_SIZE
] =
92 /* FIXME KVX: no first entry, not used yet */
96 /* Per function entry in a procedure linkage table looks like this
97 if the distance between the PLTGOT and the PLT is < 4GB use
99 static const bfd_byte elfNN_kvx_small_plt_entry
[PLT_SMALL_ENTRY_SIZE
] =
101 0x10, 0x00, 0xc4, 0x0f, /* get $r16 = $pc ;; */
103 0x10, 0x00, 0x40, 0xb0, /* lwz $r16 = 0[$r16] ;; */
105 0x10, 0x00, 0x40, 0xb8, /* ld $r16 = 0[$r16] ;; */
107 0x00, 0x00, 0x00, 0x18, /* upper 27 bits for LSU */
108 0x10, 0x00, 0xd8, 0x0f, /* igoto $r16 ;; */
111 /* Long stub use 43bits format of make. */
112 static const uint32_t elfNN_kvx_long_branch_stub
[] =
114 0xe0400000, /* make $r16 = LO10<emm43> EX6<imm43> */
115 0x00000000, /* UP27<imm43> ;; */
116 0x0fd80010, /* igoto "r16 ;; */
119 #define elf_info_to_howto elfNN_kvx_info_to_howto
120 #define elf_info_to_howto_rel elfNN_kvx_info_to_howto
122 #define KVX_ELF_ABI_VERSION 0
124 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
125 #define ALL_ONES (~ (bfd_vma) 0)
127 /* Indexed by the bfd interal reloc enumerators.
128 Therefore, the table needs to be synced with BFD_RELOC_KVX_*
131 #define KVX_KV3_V1_KV3_V2_KV4_V1
132 #include "elfxx-kvx-relocs.h"
133 #undef KVX_KV3_V1_KV3_V2_KV4_V1
135 /* Given HOWTO, return the bfd internal relocation enumerator. */
137 static bfd_reloc_code_real_type
138 elfNN_kvx_bfd_reloc_from_howto (reloc_howto_type
*howto
)
140 const int size
= (int) ARRAY_SIZE (elf_kvx_howto_table
);
141 const ptrdiff_t offset
= howto
- elf_kvx_howto_table
;
143 if (offset
>= 0 && offset
< size
)
144 return BFD_RELOC_KVX_RELOC_START
+ offset
+ 1;
146 return BFD_RELOC_KVX_RELOC_START
+ 1;
149 /* Given R_TYPE, return the bfd internal relocation enumerator. */
151 static bfd_reloc_code_real_type
152 elfNN_kvx_bfd_reloc_from_type (bfd
*abfd ATTRIBUTE_UNUSED
, unsigned int r_type
)
154 static bool initialized_p
= false;
155 /* Indexed by R_TYPE, values are offsets in the howto_table. */
156 static unsigned int offsets
[R_KVX_end
];
162 for (i
= 0; i
< ARRAY_SIZE (elf_kvx_howto_table
); ++i
)
163 offsets
[elf_kvx_howto_table
[i
].type
] = i
;
165 initialized_p
= true;
168 /* PR 17512: file: b371e70a. */
169 if (r_type
>= R_KVX_end
)
171 bfd_set_error (bfd_error_bad_value
);
172 return BFD_RELOC_KVX_RELOC_END
;
175 return (BFD_RELOC_KVX_RELOC_START
+ 1) + offsets
[r_type
];
178 struct elf_kvx_reloc_map
180 bfd_reloc_code_real_type from
;
181 bfd_reloc_code_real_type to
;
184 /* Map bfd generic reloc to KVX-specific reloc. */
185 static const struct elf_kvx_reloc_map elf_kvx_reloc_map
[] =
187 {BFD_RELOC_NONE
, BFD_RELOC_KVX_NONE
},
189 /* Basic data relocations. */
190 {BFD_RELOC_CTOR
, BFD_RELOC_KVX_NN
},
191 {BFD_RELOC_64
, BFD_RELOC_KVX_64
},
192 {BFD_RELOC_32
, BFD_RELOC_KVX_32
},
193 {BFD_RELOC_16
, BFD_RELOC_KVX_16
},
194 {BFD_RELOC_8
, BFD_RELOC_KVX_8
},
196 {BFD_RELOC_64_PCREL
, BFD_RELOC_KVX_64_PCREL
},
197 {BFD_RELOC_32_PCREL
, BFD_RELOC_KVX_32_PCREL
},
200 /* Given the bfd internal relocation enumerator in CODE, return the
201 corresponding howto entry. */
203 static reloc_howto_type
*
204 elfNN_kvx_howto_from_bfd_reloc (bfd_reloc_code_real_type code
)
208 /* Convert bfd generic reloc to KVX-specific reloc. */
209 if (code
< BFD_RELOC_KVX_RELOC_START
|| code
> BFD_RELOC_KVX_RELOC_END
)
210 for (i
= 0; i
< ARRAY_SIZE (elf_kvx_reloc_map
) ; i
++)
211 if (elf_kvx_reloc_map
[i
].from
== code
)
213 code
= elf_kvx_reloc_map
[i
].to
;
217 if (code
> BFD_RELOC_KVX_RELOC_START
&& code
< BFD_RELOC_KVX_RELOC_END
)
218 return &elf_kvx_howto_table
[code
- (BFD_RELOC_KVX_RELOC_START
+ 1)];
223 static reloc_howto_type
*
224 elfNN_kvx_howto_from_type (bfd
*abfd
, unsigned int r_type
)
226 bfd_reloc_code_real_type val
;
227 reloc_howto_type
*howto
;
232 bfd_set_error (bfd_error_bad_value
);
237 val
= elfNN_kvx_bfd_reloc_from_type (abfd
, r_type
);
238 howto
= elfNN_kvx_howto_from_bfd_reloc (val
);
243 bfd_set_error (bfd_error_bad_value
);
248 elfNN_kvx_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*bfd_reloc
,
249 Elf_Internal_Rela
*elf_reloc
)
253 r_type
= ELFNN_R_TYPE (elf_reloc
->r_info
);
254 bfd_reloc
->howto
= elfNN_kvx_howto_from_type (abfd
, r_type
);
256 if (bfd_reloc
->howto
== NULL
)
258 /* xgettext:c-format */
259 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd
, r_type
);
265 static reloc_howto_type
*
266 elfNN_kvx_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
267 bfd_reloc_code_real_type code
)
269 reloc_howto_type
*howto
= elfNN_kvx_howto_from_bfd_reloc (code
);
274 bfd_set_error (bfd_error_bad_value
);
278 static reloc_howto_type
*
279 elfNN_kvx_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
284 for (i
= 0; i
< ARRAY_SIZE (elf_kvx_howto_table
); ++i
)
285 if (elf_kvx_howto_table
[i
].name
!= NULL
286 && strcasecmp (elf_kvx_howto_table
[i
].name
, r_name
) == 0)
287 return &elf_kvx_howto_table
[i
];
292 #define TARGET_LITTLE_SYM kvx_elfNN_vec
293 #define TARGET_LITTLE_NAME "elfNN-kvx"
295 /* The linker script knows the section names for placement.
296 The entry_names are used to do simple name mangling on the stubs.
297 Given a function name, and its type, the stub can be found. The
298 name can be changed. The only requirement is the %s be present. */
299 #define STUB_ENTRY_NAME "__%s_veneer"
301 /* The name of the dynamic interpreter. This is put in the .interp
303 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
306 /* PCREL 27 is signed-extended and scaled by 4 */
307 #define KVX_MAX_FWD_CALL_OFFSET \
308 (((1 << 26) - 1) << 2)
309 #define KVX_MAX_BWD_CALL_OFFSET \
312 /* Check that the destination of the call is within the PCREL27
315 kvx_valid_call_p (bfd_vma value
, bfd_vma place
)
317 bfd_signed_vma offset
= (bfd_signed_vma
) (value
- place
);
318 return (offset
<= KVX_MAX_FWD_CALL_OFFSET
319 && offset
>= KVX_MAX_BWD_CALL_OFFSET
);
322 /* Section name for stubs is the associated section name plus this
324 #define STUB_SUFFIX ".stub"
326 enum elf_kvx_stub_type
329 kvx_stub_long_branch
,
332 struct elf_kvx_stub_hash_entry
334 /* Base hash table entry structure. */
335 struct bfd_hash_entry root
;
337 /* The stub section. */
340 /* Offset within stub_sec of the beginning of this stub. */
343 /* Given the symbol's value and its section we can determine its final
344 value when building the stubs (so the stub knows where to jump). */
345 bfd_vma target_value
;
346 asection
*target_section
;
348 enum elf_kvx_stub_type stub_type
;
350 /* The symbol table entry, if any, that this was derived from. */
351 struct elf_kvx_link_hash_entry
*h
;
353 /* Destination symbol type */
354 unsigned char st_type
;
356 /* Where this stub is being called from, or, in the case of combined
357 stub sections, the first input section in the group. */
360 /* The name for the local symbol at the start of this stub. The
361 stub name in the hash table has to be unique; this does not, so
362 it can be friendlier. */
366 /* Used to build a map of a section. This is required for mixed-endian
369 typedef struct elf_elf_section_map
377 typedef struct _kvx_elf_section_data
379 struct bfd_elf_section_data elf
;
380 unsigned int mapcount
;
381 unsigned int mapsize
;
382 elf_kvx_section_map
*map
;
384 _kvx_elf_section_data
;
386 #define elf_kvx_section_data(sec) \
387 ((_kvx_elf_section_data *) elf_section_data (sec))
389 struct elf_kvx_local_symbol
391 unsigned int got_type
;
392 bfd_signed_vma got_refcount
;
396 struct elf_kvx_obj_tdata
398 struct elf_obj_tdata root
;
400 /* local symbol descriptors */
401 struct elf_kvx_local_symbol
*locals
;
403 /* Zero to warn when linking objects with incompatible enum sizes. */
404 int no_enum_size_warning
;
406 /* Zero to warn when linking objects with incompatible wchar_t sizes. */
407 int no_wchar_size_warning
;
410 #define elf_kvx_tdata(bfd) \
411 ((struct elf_kvx_obj_tdata *) (bfd)->tdata.any)
413 #define elf_kvx_locals(bfd) (elf_kvx_tdata (bfd)->locals)
415 #define is_kvx_elf(bfd) \
416 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
417 && elf_tdata (bfd) != NULL \
418 && elf_object_id (bfd) == KVX_ELF_DATA)
421 elfNN_kvx_mkobject (bfd
*abfd
)
423 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_kvx_obj_tdata
),
427 #define elf_kvx_hash_entry(ent) \
428 ((struct elf_kvx_link_hash_entry *)(ent))
430 #define GOT_UNKNOWN 0
437 /* KVX ELF linker hash entry. */
438 struct elf_kvx_link_hash_entry
440 struct elf_link_hash_entry root
;
442 /* Since PLT entries have variable size, we need to record the
443 index into .got.plt instead of recomputing it from the PLT
445 bfd_signed_vma plt_got_offset
;
447 /* Bit mask representing the type of GOT entry(s) if any required by
449 unsigned int got_type
;
451 /* A pointer to the most recently used stub hash entry against this
453 struct elf_kvx_stub_hash_entry
*stub_cache
;
456 /* Get the KVX elf linker hash table from a link_info structure. */
457 #define elf_kvx_hash_table(info) \
458 ((struct elf_kvx_link_hash_table *) ((info)->hash))
460 #define kvx_stub_hash_lookup(table, string, create, copy) \
461 ((struct elf_kvx_stub_hash_entry *) \
462 bfd_hash_lookup ((table), (string), (create), (copy)))
464 /* KVX ELF linker hash table. */
465 struct elf_kvx_link_hash_table
467 /* The main hash table. */
468 struct elf_link_hash_table root
;
470 /* Nonzero to force PIC branch veneers. */
473 /* The number of bytes in the initial entry in the PLT. */
474 bfd_size_type plt_header_size
;
476 /* The number of bytes in the subsequent PLT etries. */
477 bfd_size_type plt_entry_size
;
479 /* The bytes of the subsequent PLT entry. */
480 const bfd_byte
*plt_entry
;
482 /* Short-cuts to get to dynamic linker sections. */
486 /* Small local sym cache. */
487 struct sym_cache sym_cache
;
489 /* For convenience in allocate_dynrelocs. */
492 /* The amount of space used by the reserved portion of the sgotplt
493 section, plus whatever space is used by the jump slots. */
494 bfd_vma sgotplt_jump_table_size
;
496 /* The stub hash table. */
497 struct bfd_hash_table stub_hash_table
;
499 /* Linker stub bfd. */
502 /* Linker call-backs. */
503 asection
*(*add_stub_section
) (const char *, asection
*);
504 void (*layout_sections_again
) (void);
506 /* Array to keep track of which stub sections have been created, and
507 information on stub grouping. */
510 /* This is the section to which stubs in the group will be
513 /* The stub section. */
517 /* Assorted information used by elfNN_kvx_size_stubs. */
518 unsigned int bfd_count
;
519 unsigned int top_index
;
520 asection
**input_list
;
523 /* Create an entry in an KVX ELF linker hash table. */
525 static struct bfd_hash_entry
*
526 elfNN_kvx_link_hash_newfunc (struct bfd_hash_entry
*entry
,
527 struct bfd_hash_table
*table
,
530 struct elf_kvx_link_hash_entry
*ret
=
531 (struct elf_kvx_link_hash_entry
*) entry
;
533 /* Allocate the structure if it has not already been allocated by a
536 ret
= bfd_hash_allocate (table
,
537 sizeof (struct elf_kvx_link_hash_entry
));
539 return (struct bfd_hash_entry
*) ret
;
541 /* Call the allocation method of the superclass. */
542 ret
= ((struct elf_kvx_link_hash_entry
*)
543 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
547 ret
->got_type
= GOT_UNKNOWN
;
548 ret
->plt_got_offset
= (bfd_vma
) - 1;
549 ret
->stub_cache
= NULL
;
552 return (struct bfd_hash_entry
*) ret
;
555 /* Initialize an entry in the stub hash table. */
557 static struct bfd_hash_entry
*
558 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
559 struct bfd_hash_table
*table
, const char *string
)
561 /* Allocate the structure if it has not already been allocated by a
565 entry
= bfd_hash_allocate (table
,
567 elf_kvx_stub_hash_entry
));
572 /* Call the allocation method of the superclass. */
573 entry
= bfd_hash_newfunc (entry
, table
, string
);
576 struct elf_kvx_stub_hash_entry
*eh
;
578 /* Initialize the local fields. */
579 eh
= (struct elf_kvx_stub_hash_entry
*) entry
;
582 eh
->target_value
= 0;
583 eh
->target_section
= NULL
;
584 eh
->stub_type
= kvx_stub_none
;
592 /* Copy the extra info we tack onto an elf_link_hash_entry. */
595 elfNN_kvx_copy_indirect_symbol (struct bfd_link_info
*info
,
596 struct elf_link_hash_entry
*dir
,
597 struct elf_link_hash_entry
*ind
)
599 struct elf_kvx_link_hash_entry
*edir
, *eind
;
601 edir
= (struct elf_kvx_link_hash_entry
*) dir
;
602 eind
= (struct elf_kvx_link_hash_entry
*) ind
;
604 if (ind
->root
.type
== bfd_link_hash_indirect
)
606 /* Copy over PLT info. */
607 if (dir
->got
.refcount
<= 0)
609 edir
->got_type
= eind
->got_type
;
610 eind
->got_type
= GOT_UNKNOWN
;
614 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
617 /* Destroy a KVX elf linker hash table. */
620 elfNN_kvx_link_hash_table_free (bfd
*obfd
)
622 struct elf_kvx_link_hash_table
*ret
623 = (struct elf_kvx_link_hash_table
*) obfd
->link
.hash
;
625 bfd_hash_table_free (&ret
->stub_hash_table
);
626 _bfd_elf_link_hash_table_free (obfd
);
629 /* Create a KVX elf linker hash table. */
631 static struct bfd_link_hash_table
*
632 elfNN_kvx_link_hash_table_create (bfd
*abfd
)
634 struct elf_kvx_link_hash_table
*ret
;
635 bfd_size_type amt
= sizeof (struct elf_kvx_link_hash_table
);
637 ret
= bfd_zmalloc (amt
);
641 if (!_bfd_elf_link_hash_table_init
642 (&ret
->root
, abfd
, elfNN_kvx_link_hash_newfunc
,
643 sizeof (struct elf_kvx_link_hash_entry
), KVX_ELF_DATA
))
649 ret
->plt_header_size
= PLT_ENTRY_SIZE
;
650 ret
->plt_entry_size
= PLT_SMALL_ENTRY_SIZE
;
651 ret
->plt_entry
= elfNN_kvx_small_plt_entry
;
655 if (!bfd_hash_table_init (&ret
->stub_hash_table
, stub_hash_newfunc
,
656 sizeof (struct elf_kvx_stub_hash_entry
)))
658 _bfd_elf_link_hash_table_free (abfd
);
662 ret
->root
.root
.hash_table_free
= elfNN_kvx_link_hash_table_free
;
664 return &ret
->root
.root
;
667 static bfd_reloc_status_type
668 kvx_relocate (unsigned int r_type
, bfd
*input_bfd
, asection
*input_section
,
669 bfd_vma offset
, bfd_vma value
)
671 reloc_howto_type
*howto
;
674 howto
= elfNN_kvx_howto_from_type (input_bfd
, r_type
);
675 place
= (input_section
->output_section
->vma
+ input_section
->output_offset
678 r_type
= elfNN_kvx_bfd_reloc_from_type (input_bfd
, r_type
);
679 value
= _bfd_kvx_elf_resolve_relocation (r_type
, place
, value
, 0, false);
680 return _bfd_kvx_elf_put_addend (input_bfd
,
681 input_section
->contents
+ offset
, r_type
,
685 /* Determine the type of stub needed, if any, for a call. */
687 static enum elf_kvx_stub_type
688 kvx_type_of_stub (asection
*input_sec
,
689 const Elf_Internal_Rela
*rel
,
691 unsigned char st_type
,
695 bfd_signed_vma branch_offset
;
697 enum elf_kvx_stub_type stub_type
= kvx_stub_none
;
699 if (st_type
!= STT_FUNC
700 && (sym_sec
== input_sec
))
703 /* Determine where the call point is. */
704 location
= (input_sec
->output_offset
705 + input_sec
->output_section
->vma
+ rel
->r_offset
);
707 branch_offset
= (bfd_signed_vma
) (destination
- location
);
709 r_type
= ELFNN_R_TYPE (rel
->r_info
);
711 /* We don't want to redirect any old unconditional jump in this way,
712 only one which is being used for a sibcall, where it is
713 acceptable for the R16 and R17 registers to be clobbered. */
714 if (r_type
== R_KVX_PCREL27
715 && (branch_offset
> KVX_MAX_FWD_CALL_OFFSET
716 || branch_offset
< KVX_MAX_BWD_CALL_OFFSET
))
718 stub_type
= kvx_stub_long_branch
;
724 /* Build a name for an entry in the stub hash table. */
727 elfNN_kvx_stub_name (const asection
*input_section
,
728 const asection
*sym_sec
,
729 const struct elf_kvx_link_hash_entry
*hash
,
730 const Elf_Internal_Rela
*rel
)
737 len
= 8 + 1 + strlen (hash
->root
.root
.root
.string
) + 1 + 16 + 1;
738 stub_name
= bfd_malloc (len
);
739 if (stub_name
!= NULL
)
740 snprintf (stub_name
, len
, "%08x_%s+%" PRIx64
"x",
741 (unsigned int) input_section
->id
,
742 hash
->root
.root
.root
.string
,
747 len
= 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
748 stub_name
= bfd_malloc (len
);
749 if (stub_name
!= NULL
)
750 snprintf (stub_name
, len
, "%08x_%x:%x+%" PRIx64
"x",
751 (unsigned int) input_section
->id
,
752 (unsigned int) sym_sec
->id
,
753 (unsigned int) ELFNN_R_SYM (rel
->r_info
),
760 /* Return true if symbol H should be hashed in the `.gnu.hash' section. For
761 executable PLT slots where the executable never takes the address of those
762 functions, the function symbols are not added to the hash table. */
765 elf_kvx_hash_symbol (struct elf_link_hash_entry
*h
)
767 if (h
->plt
.offset
!= (bfd_vma
) -1
769 && !h
->pointer_equality_needed
)
772 return _bfd_elf_hash_symbol (h
);
776 /* Look up an entry in the stub hash. Stub entries are cached because
777 creating the stub name takes a bit of time. */
779 static struct elf_kvx_stub_hash_entry
*
780 elfNN_kvx_get_stub_entry (const asection
*input_section
,
781 const asection
*sym_sec
,
782 struct elf_link_hash_entry
*hash
,
783 const Elf_Internal_Rela
*rel
,
784 struct elf_kvx_link_hash_table
*htab
)
786 struct elf_kvx_stub_hash_entry
*stub_entry
;
787 struct elf_kvx_link_hash_entry
*h
=
788 (struct elf_kvx_link_hash_entry
*) hash
;
789 const asection
*id_sec
;
791 if ((input_section
->flags
& SEC_CODE
) == 0)
794 /* If this input section is part of a group of sections sharing one
795 stub section, then use the id of the first section in the group.
796 Stub names need to include a section id, as there may well be
797 more than one stub used to reach say, printf, and we need to
798 distinguish between them. */
799 id_sec
= htab
->stub_group
[input_section
->id
].link_sec
;
801 if (h
!= NULL
&& h
->stub_cache
!= NULL
802 && h
->stub_cache
->h
== h
&& h
->stub_cache
->id_sec
== id_sec
)
804 stub_entry
= h
->stub_cache
;
810 stub_name
= elfNN_kvx_stub_name (id_sec
, sym_sec
, h
, rel
);
811 if (stub_name
== NULL
)
814 stub_entry
= kvx_stub_hash_lookup (&htab
->stub_hash_table
,
815 stub_name
, false, false);
817 h
->stub_cache
= stub_entry
;
826 /* Create a stub section. */
829 _bfd_kvx_create_stub_section (asection
*section
,
830 struct elf_kvx_link_hash_table
*htab
)
837 namelen
= strlen (section
->name
);
838 len
= namelen
+ sizeof (STUB_SUFFIX
);
839 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
843 memcpy (s_name
, section
->name
, namelen
);
844 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
845 return (*htab
->add_stub_section
) (s_name
, section
);
849 /* Find or create a stub section for a link section.
851 Fix or create the stub section used to collect stubs attached to
852 the specified link section. */
855 _bfd_kvx_get_stub_for_link_section (asection
*link_section
,
856 struct elf_kvx_link_hash_table
*htab
)
858 if (htab
->stub_group
[link_section
->id
].stub_sec
== NULL
)
859 htab
->stub_group
[link_section
->id
].stub_sec
860 = _bfd_kvx_create_stub_section (link_section
, htab
);
861 return htab
->stub_group
[link_section
->id
].stub_sec
;
865 /* Find or create a stub section in the stub group for an input
869 _bfd_kvx_create_or_find_stub_sec (asection
*section
,
870 struct elf_kvx_link_hash_table
*htab
)
872 asection
*link_sec
= htab
->stub_group
[section
->id
].link_sec
;
873 return _bfd_kvx_get_stub_for_link_section (link_sec
, htab
);
877 /* Add a new stub entry in the stub group associated with an input
878 section to the stub hash. Not all fields of the new stub entry are
881 static struct elf_kvx_stub_hash_entry
*
882 _bfd_kvx_add_stub_entry_in_group (const char *stub_name
,
884 struct elf_kvx_link_hash_table
*htab
)
888 struct elf_kvx_stub_hash_entry
*stub_entry
;
890 link_sec
= htab
->stub_group
[section
->id
].link_sec
;
891 stub_sec
= _bfd_kvx_create_or_find_stub_sec (section
, htab
);
893 /* Enter this entry into the linker stub hash table. */
894 stub_entry
= kvx_stub_hash_lookup (&htab
->stub_hash_table
, stub_name
,
896 if (stub_entry
== NULL
)
898 /* xgettext:c-format */
899 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
900 section
->owner
, stub_name
);
904 stub_entry
->stub_sec
= stub_sec
;
905 stub_entry
->stub_offset
= 0;
906 stub_entry
->id_sec
= link_sec
;
912 kvx_build_one_stub (struct bfd_hash_entry
*gen_entry
,
915 struct elf_kvx_stub_hash_entry
*stub_entry
;
920 unsigned int template_size
;
921 const uint32_t *template;
923 struct bfd_link_info
*info
;
925 /* Massage our args to the form they really have. */
926 stub_entry
= (struct elf_kvx_stub_hash_entry
*) gen_entry
;
928 info
= (struct bfd_link_info
*) in_arg
;
930 /* Fail if the target section could not be assigned to an output
931 section. The user should fix his linker script. */
932 if (stub_entry
->target_section
->output_section
== NULL
933 && info
->non_contiguous_regions
)
934 info
->callbacks
->einfo (_("%F%P: Could not assign '%pA' to an output section. "
936 "--enable-non-contiguous-regions.\n"),
937 stub_entry
->target_section
);
939 stub_sec
= stub_entry
->stub_sec
;
941 /* Make a note of the offset within the stubs for this entry. */
942 stub_entry
->stub_offset
= stub_sec
->size
;
943 loc
= stub_sec
->contents
+ stub_entry
->stub_offset
;
945 stub_bfd
= stub_sec
->owner
;
947 /* This is the address of the stub destination. */
948 sym_value
= (stub_entry
->target_value
949 + stub_entry
->target_section
->output_offset
950 + stub_entry
->target_section
->output_section
->vma
);
952 switch (stub_entry
->stub_type
)
954 case kvx_stub_long_branch
:
955 template = elfNN_kvx_long_branch_stub
;
956 template_size
= sizeof (elfNN_kvx_long_branch_stub
);
962 for (i
= 0; i
< (template_size
/ sizeof template[0]); i
++)
964 bfd_putl32 (template[i
], loc
);
968 stub_sec
->size
+= template_size
;
970 switch (stub_entry
->stub_type
)
972 case kvx_stub_long_branch
:
974 The stub uses a make insn with 43bits immediate.
975 We need to apply 3 relocations:
976 BFD_RELOC_KVX_S43_LO10
977 BFD_RELOC_KVX_S43_UP27
978 BFD_RELOC_KVX_S43_EX6
980 if (kvx_relocate (R_KVX_S43_LO10
, stub_bfd
, stub_sec
,
981 stub_entry
->stub_offset
, sym_value
) != bfd_reloc_ok
)
983 if (kvx_relocate (R_KVX_S43_EX6
, stub_bfd
, stub_sec
,
984 stub_entry
->stub_offset
, sym_value
) != bfd_reloc_ok
)
986 if (kvx_relocate (R_KVX_S43_UP27
, stub_bfd
, stub_sec
,
987 stub_entry
->stub_offset
+ 4, sym_value
) != bfd_reloc_ok
)
997 /* As above, but don't actually build the stub. Just bump offset so
998 we know stub section sizes. */
1001 kvx_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
1003 struct elf_kvx_stub_hash_entry
*stub_entry
;
1006 /* Massage our args to the form they really have. */
1007 stub_entry
= (struct elf_kvx_stub_hash_entry
*) gen_entry
;
1009 switch (stub_entry
->stub_type
)
1011 case kvx_stub_long_branch
:
1012 size
= sizeof (elfNN_kvx_long_branch_stub
);
1018 stub_entry
->stub_sec
->size
+= size
;
1022 /* External entry points for sizing and building linker stubs. */
1024 /* Set up various things so that we can make a list of input sections
1025 for each output section included in the link. Returns -1 on error,
1026 0 when no stubs will be needed, and 1 on success. */
1029 elfNN_kvx_setup_section_lists (bfd
*output_bfd
,
1030 struct bfd_link_info
*info
)
1033 unsigned int bfd_count
;
1034 unsigned int top_id
, top_index
;
1036 asection
**input_list
, **list
;
1038 struct elf_kvx_link_hash_table
*htab
=
1039 elf_kvx_hash_table (info
);
1041 if (!is_elf_hash_table ((const struct bfd_link_hash_table
*)htab
))
1044 /* Count the number of input BFDs and find the top input section id. */
1045 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
1046 input_bfd
!= NULL
; input_bfd
= input_bfd
->link
.next
)
1049 for (section
= input_bfd
->sections
;
1050 section
!= NULL
; section
= section
->next
)
1052 if (top_id
< section
->id
)
1053 top_id
= section
->id
;
1056 htab
->bfd_count
= bfd_count
;
1058 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
1059 htab
->stub_group
= bfd_zmalloc (amt
);
1060 if (htab
->stub_group
== NULL
)
1063 /* We can't use output_bfd->section_count here to find the top output
1064 section index as some sections may have been removed, and
1065 _bfd_strip_section_from_output doesn't renumber the indices. */
1066 for (section
= output_bfd
->sections
, top_index
= 0;
1067 section
!= NULL
; section
= section
->next
)
1069 if (top_index
< section
->index
)
1070 top_index
= section
->index
;
1073 htab
->top_index
= top_index
;
1074 amt
= sizeof (asection
*) * (top_index
+ 1);
1075 input_list
= bfd_malloc (amt
);
1076 htab
->input_list
= input_list
;
1077 if (input_list
== NULL
)
1080 /* For sections we aren't interested in, mark their entries with a
1081 value we can check later. */
1082 list
= input_list
+ top_index
;
1084 *list
= bfd_abs_section_ptr
;
1085 while (list
-- != input_list
);
1087 for (section
= output_bfd
->sections
;
1088 section
!= NULL
; section
= section
->next
)
1090 if ((section
->flags
& SEC_CODE
) != 0)
1091 input_list
[section
->index
] = NULL
;
1097 /* Used by elfNN_kvx_next_input_section and group_sections. */
1098 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
1100 /* The linker repeatedly calls this function for each input section,
1101 in the order that input sections are linked into output sections.
1102 Build lists of input sections to determine groupings between which
1103 we may insert linker stubs. */
1106 elfNN_kvx_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
1108 struct elf_kvx_link_hash_table
*htab
=
1109 elf_kvx_hash_table (info
);
1111 if (isec
->output_section
->index
<= htab
->top_index
)
1113 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
1115 if (*list
!= bfd_abs_section_ptr
)
1117 /* Steal the link_sec pointer for our list. */
1118 /* This happens to make the list in reverse order,
1119 which is what we want. */
1120 PREV_SEC (isec
) = *list
;
1126 /* See whether we can group stub sections together. Grouping stub
1127 sections may result in fewer stubs. More importantly, we need to
1128 put all .init* and .fini* stubs at the beginning of the .init or
1129 .fini output sections respectively, because glibc splits the
1130 _init and _fini functions into multiple parts. Putting a stub in
1131 the middle of a function is not a good idea. */
1134 group_sections (struct elf_kvx_link_hash_table
*htab
,
1135 bfd_size_type stub_group_size
,
1136 bool stubs_always_after_branch
)
1138 asection
**list
= htab
->input_list
;
1142 asection
*tail
= *list
;
1145 if (tail
== bfd_abs_section_ptr
)
1148 /* Reverse the list: we must avoid placing stubs at the
1149 beginning of the section because the beginning of the text
1150 section may be required for an interrupt vector in bare metal
1152 #define NEXT_SEC PREV_SEC
1154 while (tail
!= NULL
)
1156 /* Pop from tail. */
1157 asection
*item
= tail
;
1158 tail
= PREV_SEC (item
);
1161 NEXT_SEC (item
) = head
;
1165 while (head
!= NULL
)
1169 bfd_vma stub_group_start
= head
->output_offset
;
1170 bfd_vma end_of_next
;
1173 while (NEXT_SEC (curr
) != NULL
)
1175 next
= NEXT_SEC (curr
);
1176 end_of_next
= next
->output_offset
+ next
->size
;
1177 if (end_of_next
- stub_group_start
>= stub_group_size
)
1178 /* End of NEXT is too far from start, so stop. */
1180 /* Add NEXT to the group. */
1184 /* OK, the size from the start to the start of CURR is less
1185 than stub_group_size and thus can be handled by one stub
1186 section. (Or the head section is itself larger than
1187 stub_group_size, in which case we may be toast.)
1188 We should really be keeping track of the total size of
1189 stubs added here, as stubs contribute to the final output
1193 next
= NEXT_SEC (head
);
1194 /* Set up this stub group. */
1195 htab
->stub_group
[head
->id
].link_sec
= curr
;
1197 while (head
!= curr
&& (head
= next
) != NULL
);
1199 /* But wait, there's more! Input sections up to stub_group_size
1200 bytes after the stub section can be handled by it too. */
1201 if (!stubs_always_after_branch
)
1203 stub_group_start
= curr
->output_offset
+ curr
->size
;
1205 while (next
!= NULL
)
1207 end_of_next
= next
->output_offset
+ next
->size
;
1208 if (end_of_next
- stub_group_start
>= stub_group_size
)
1209 /* End of NEXT is too far from stubs, so stop. */
1211 /* Add NEXT to the stub group. */
1213 next
= NEXT_SEC (head
);
1214 htab
->stub_group
[head
->id
].link_sec
= curr
;
1220 while (list
++ != htab
->input_list
+ htab
->top_index
);
1222 free (htab
->input_list
);
1226 _bfd_kvx_resize_stubs (struct elf_kvx_link_hash_table
*htab
)
1230 /* OK, we've added some stubs. Find out the new size of the
1232 for (section
= htab
->stub_bfd
->sections
;
1233 section
!= NULL
; section
= section
->next
)
1235 /* Ignore non-stub sections. */
1236 if (!strstr (section
->name
, STUB_SUFFIX
))
1241 bfd_hash_traverse (&htab
->stub_hash_table
, kvx_size_one_stub
, htab
);
1244 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
1247 kvx_elfNN_init_stub_bfd (struct bfd_link_info
*info
,
1250 struct elf_kvx_link_hash_table
*htab
;
1252 elf_elfheader (stub_bfd
)->e_ident
[EI_CLASS
] = ELFCLASSNN
;
1254 /* Always hook our dynamic sections into the first bfd, which is the
1255 linker created stub bfd. This ensures that the GOT header is at
1256 the start of the output TOC section. */
1257 htab
= elf_kvx_hash_table (info
);
1264 /* Determine and set the size of the stub section for a final link.
1266 The basic idea here is to examine all the relocations looking for
1267 PC-relative calls to a target that is unreachable with a 27bits
1268 immediate (found in call and goto). */
1271 elfNN_kvx_size_stubs (bfd
*output_bfd
,
1273 struct bfd_link_info
*info
,
1274 bfd_signed_vma group_size
,
1275 asection
* (*add_stub_section
) (const char *,
1277 void (*layout_sections_again
) (void))
1279 bfd_size_type stub_group_size
;
1280 bool stubs_always_before_branch
;
1281 bool stub_changed
= false;
1282 struct elf_kvx_link_hash_table
*htab
= elf_kvx_hash_table (info
);
1284 /* Propagate mach to stub bfd, because it may not have been
1285 finalized when we created stub_bfd. */
1286 bfd_set_arch_mach (stub_bfd
, bfd_get_arch (output_bfd
),
1287 bfd_get_mach (output_bfd
));
1289 /* Stash our params away. */
1290 htab
->stub_bfd
= stub_bfd
;
1291 htab
->add_stub_section
= add_stub_section
;
1292 htab
->layout_sections_again
= layout_sections_again
;
1293 stubs_always_before_branch
= group_size
< 0;
1295 stub_group_size
= -group_size
;
1297 stub_group_size
= group_size
;
1299 if (stub_group_size
== 1)
1301 /* Default values. */
1302 /* KVX branch range is +-256MB. The value used is 1MB less. */
1303 stub_group_size
= 255 * 1024 * 1024;
1306 group_sections (htab
, stub_group_size
, stubs_always_before_branch
);
1308 (*htab
->layout_sections_again
) ();
1314 for (input_bfd
= info
->input_bfds
;
1315 input_bfd
!= NULL
; input_bfd
= input_bfd
->link
.next
)
1317 Elf_Internal_Shdr
*symtab_hdr
;
1319 Elf_Internal_Sym
*local_syms
= NULL
;
1321 if (!is_kvx_elf (input_bfd
)
1322 || (input_bfd
->flags
& BFD_LINKER_CREATED
) != 0)
1325 /* We'll need the symbol table in a second. */
1326 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1327 if (symtab_hdr
->sh_info
== 0)
1330 /* Walk over each section attached to the input bfd. */
1331 for (section
= input_bfd
->sections
;
1332 section
!= NULL
; section
= section
->next
)
1334 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
1336 /* If there aren't any relocs, then there's nothing more
1338 if ((section
->flags
& SEC_RELOC
) == 0
1339 || section
->reloc_count
== 0
1340 || (section
->flags
& SEC_CODE
) == 0)
1343 /* If this section is a link-once section that will be
1344 discarded, then don't create any stubs. */
1345 if (section
->output_section
== NULL
1346 || section
->output_section
->owner
!= output_bfd
)
1349 /* Get the relocs. */
1351 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
,
1352 NULL
, info
->keep_memory
);
1353 if (internal_relocs
== NULL
)
1354 goto error_ret_free_local
;
1356 /* Now examine each relocation. */
1357 irela
= internal_relocs
;
1358 irelaend
= irela
+ section
->reloc_count
;
1359 for (; irela
< irelaend
; irela
++)
1361 unsigned int r_type
, r_indx
;
1362 enum elf_kvx_stub_type stub_type
;
1363 struct elf_kvx_stub_hash_entry
*stub_entry
;
1366 bfd_vma destination
;
1367 struct elf_kvx_link_hash_entry
*hash
;
1368 const char *sym_name
;
1370 const asection
*id_sec
;
1371 unsigned char st_type
;
1374 r_type
= ELFNN_R_TYPE (irela
->r_info
);
1375 r_indx
= ELFNN_R_SYM (irela
->r_info
);
1377 if (r_type
>= (unsigned int) R_KVX_end
)
1379 bfd_set_error (bfd_error_bad_value
);
1380 error_ret_free_internal
:
1381 if (elf_section_data (section
)->relocs
== NULL
)
1382 free (internal_relocs
);
1383 goto error_ret_free_local
;
1386 /* Only look for stubs on unconditional branch and
1387 branch and link instructions. */
1388 /* This catches CALL and GOTO insn */
1389 if (r_type
!= (unsigned int) R_KVX_PCREL27
)
1392 /* Now determine the call target, its name, value,
1399 if (r_indx
< symtab_hdr
->sh_info
)
1401 /* It's a local symbol. */
1402 Elf_Internal_Sym
*sym
;
1403 Elf_Internal_Shdr
*hdr
;
1405 if (local_syms
== NULL
)
1408 = (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1409 if (local_syms
== NULL
)
1411 = bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1412 symtab_hdr
->sh_info
, 0,
1414 if (local_syms
== NULL
)
1415 goto error_ret_free_internal
;
1418 sym
= local_syms
+ r_indx
;
1419 hdr
= elf_elfsections (input_bfd
)[sym
->st_shndx
];
1420 sym_sec
= hdr
->bfd_section
;
1422 /* This is an undefined symbol. It can never
1426 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
1427 sym_value
= sym
->st_value
;
1428 destination
= (sym_value
+ irela
->r_addend
1429 + sym_sec
->output_offset
1430 + sym_sec
->output_section
->vma
);
1431 st_type
= ELF_ST_TYPE (sym
->st_info
);
1433 = bfd_elf_string_from_elf_section (input_bfd
,
1434 symtab_hdr
->sh_link
,
1441 e_indx
= r_indx
- symtab_hdr
->sh_info
;
1442 hash
= ((struct elf_kvx_link_hash_entry
*)
1443 elf_sym_hashes (input_bfd
)[e_indx
]);
1445 while (hash
->root
.root
.type
== bfd_link_hash_indirect
1446 || hash
->root
.root
.type
== bfd_link_hash_warning
)
1447 hash
= ((struct elf_kvx_link_hash_entry
*)
1448 hash
->root
.root
.u
.i
.link
);
1450 if (hash
->root
.root
.type
== bfd_link_hash_defined
1451 || hash
->root
.root
.type
== bfd_link_hash_defweak
)
1453 struct elf_kvx_link_hash_table
*globals
=
1454 elf_kvx_hash_table (info
);
1455 sym_sec
= hash
->root
.root
.u
.def
.section
;
1456 sym_value
= hash
->root
.root
.u
.def
.value
;
1457 /* For a destination in a shared library,
1458 use the PLT stub as target address to
1459 decide whether a branch stub is
1461 if (globals
->root
.splt
!= NULL
&& hash
!= NULL
1462 && hash
->root
.plt
.offset
!= (bfd_vma
) - 1)
1464 sym_sec
= globals
->root
.splt
;
1465 sym_value
= hash
->root
.plt
.offset
;
1466 if (sym_sec
->output_section
!= NULL
)
1467 destination
= (sym_value
1468 + sym_sec
->output_offset
1470 sym_sec
->output_section
->vma
);
1472 else if (sym_sec
->output_section
!= NULL
)
1473 destination
= (sym_value
+ irela
->r_addend
1474 + sym_sec
->output_offset
1475 + sym_sec
->output_section
->vma
);
1477 else if (hash
->root
.root
.type
== bfd_link_hash_undefined
1478 || (hash
->root
.root
.type
1479 == bfd_link_hash_undefweak
))
1481 /* For a shared library, use the PLT stub as
1482 target address to decide whether a long
1483 branch stub is needed.
1484 For absolute code, they cannot be handled. */
1485 struct elf_kvx_link_hash_table
*globals
=
1486 elf_kvx_hash_table (info
);
1488 if (globals
->root
.splt
!= NULL
&& hash
!= NULL
1489 && hash
->root
.plt
.offset
!= (bfd_vma
) - 1)
1491 sym_sec
= globals
->root
.splt
;
1492 sym_value
= hash
->root
.plt
.offset
;
1493 if (sym_sec
->output_section
!= NULL
)
1494 destination
= (sym_value
1495 + sym_sec
->output_offset
1497 sym_sec
->output_section
->vma
);
1504 bfd_set_error (bfd_error_bad_value
);
1505 goto error_ret_free_internal
;
1507 st_type
= ELF_ST_TYPE (hash
->root
.type
);
1508 sym_name
= hash
->root
.root
.root
.string
;
1511 /* Determine what (if any) linker stub is needed. */
1512 stub_type
= kvx_type_of_stub (section
, irela
, sym_sec
,
1513 st_type
, destination
);
1514 if (stub_type
== kvx_stub_none
)
1517 /* Support for grouping stub sections. */
1518 id_sec
= htab
->stub_group
[section
->id
].link_sec
;
1520 /* Get the name of this stub. */
1521 stub_name
= elfNN_kvx_stub_name (id_sec
, sym_sec
, hash
,
1524 goto error_ret_free_internal
;
1527 kvx_stub_hash_lookup (&htab
->stub_hash_table
,
1528 stub_name
, false, false);
1529 if (stub_entry
!= NULL
)
1531 /* The proper stub has already been created. */
1533 /* Always update this stub's target since it may have
1534 changed after layout. */
1535 stub_entry
->target_value
= sym_value
+ irela
->r_addend
;
1539 stub_entry
= _bfd_kvx_add_stub_entry_in_group
1540 (stub_name
, section
, htab
);
1541 if (stub_entry
== NULL
)
1544 goto error_ret_free_internal
;
1547 stub_entry
->target_value
= sym_value
+ irela
->r_addend
;
1548 stub_entry
->target_section
= sym_sec
;
1549 stub_entry
->stub_type
= stub_type
;
1550 stub_entry
->h
= hash
;
1551 stub_entry
->st_type
= st_type
;
1553 if (sym_name
== NULL
)
1554 sym_name
= "unnamed";
1555 len
= sizeof (STUB_ENTRY_NAME
) + strlen (sym_name
);
1556 stub_entry
->output_name
= bfd_alloc (htab
->stub_bfd
, len
);
1557 if (stub_entry
->output_name
== NULL
)
1560 goto error_ret_free_internal
;
1563 snprintf (stub_entry
->output_name
, len
, STUB_ENTRY_NAME
,
1566 stub_changed
= true;
1569 /* We're done with the internal relocs, free them. */
1570 if (elf_section_data (section
)->relocs
== NULL
)
1571 free (internal_relocs
);
1578 _bfd_kvx_resize_stubs (htab
);
1580 /* Ask the linker to do its stuff. */
1581 (*htab
->layout_sections_again
) ();
1582 stub_changed
= false;
1587 error_ret_free_local
:
1592 /* Build all the stubs associated with the current output file. The
1593 stubs are kept in a hash table attached to the main linker hash
1594 table. We also set up the .plt entries for statically linked PIC
1595 functions here. This function is called via kvx_elf_finish in the
1599 elfNN_kvx_build_stubs (struct bfd_link_info
*info
)
1602 struct bfd_hash_table
*table
;
1603 struct elf_kvx_link_hash_table
*htab
;
1605 htab
= elf_kvx_hash_table (info
);
1607 for (stub_sec
= htab
->stub_bfd
->sections
;
1608 stub_sec
!= NULL
; stub_sec
= stub_sec
->next
)
1612 /* Ignore non-stub sections. */
1613 if (!strstr (stub_sec
->name
, STUB_SUFFIX
))
1616 /* Allocate memory to hold the linker stubs. */
1617 size
= stub_sec
->size
;
1618 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
1619 if (stub_sec
->contents
== NULL
&& size
!= 0)
1624 /* Build the stubs as directed by the stub hash table. */
1625 table
= &htab
->stub_hash_table
;
1626 bfd_hash_traverse (table
, kvx_build_one_stub
, info
);
1632 kvx_calculate_got_entry_vma (struct elf_link_hash_entry
*h
,
1633 struct elf_kvx_link_hash_table
1634 *globals
, struct bfd_link_info
*info
,
1635 bfd_vma value
, bfd
*output_bfd
,
1636 bool *unresolved_reloc_p
)
1638 bfd_vma off
= (bfd_vma
) - 1;
1639 asection
*basegot
= globals
->root
.sgot
;
1640 bool dyn
= globals
->root
.dynamic_sections_created
;
1644 BFD_ASSERT (basegot
!= NULL
);
1645 off
= h
->got
.offset
;
1646 BFD_ASSERT (off
!= (bfd_vma
) - 1);
1647 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1648 || (bfd_link_pic (info
)
1649 && SYMBOL_REFERENCES_LOCAL (info
, h
))
1650 || (ELF_ST_VISIBILITY (h
->other
)
1651 && h
->root
.type
== bfd_link_hash_undefweak
))
1653 /* This is actually a static link, or it is a -Bsymbolic link
1654 and the symbol is defined locally. We must initialize this
1655 entry in the global offset table. Since the offset must
1656 always be a multiple of 8 (4 in the case of ILP32), we use
1657 the least significant bit to record whether we have
1658 initialized it already.
1659 When doing a dynamic link, we create a .rel(a).got relocation
1660 entry to initialize the value. This is done in the
1661 finish_dynamic_symbol routine. */
1666 bfd_put_NN (output_bfd
, value
, basegot
->contents
+ off
);
1671 *unresolved_reloc_p
= false;
1678 kvx_reloc_got_type (bfd_reloc_code_real_type r_type
)
1683 awk 'match ($0, /HOWTO.*R_(KVX.*_GOT(OFF)?(64)?_.*),/,ary) {print "case BFD_RELOC_" ary[1] ":";}' elfxx-kvxc.def
1685 case BFD_RELOC_KVX_S37_GOTOFF_LO10
:
1686 case BFD_RELOC_KVX_S37_GOTOFF_UP27
:
1688 case BFD_RELOC_KVX_S37_GOT_LO10
:
1689 case BFD_RELOC_KVX_S37_GOT_UP27
:
1691 case BFD_RELOC_KVX_S43_GOTOFF_LO10
:
1692 case BFD_RELOC_KVX_S43_GOTOFF_UP27
:
1693 case BFD_RELOC_KVX_S43_GOTOFF_EX6
:
1695 case BFD_RELOC_KVX_S43_GOT_LO10
:
1696 case BFD_RELOC_KVX_S43_GOT_UP27
:
1697 case BFD_RELOC_KVX_S43_GOT_EX6
:
1700 case BFD_RELOC_KVX_S37_TLS_GD_LO10
:
1701 case BFD_RELOC_KVX_S37_TLS_GD_UP27
:
1702 case BFD_RELOC_KVX_S43_TLS_GD_LO10
:
1703 case BFD_RELOC_KVX_S43_TLS_GD_UP27
:
1704 case BFD_RELOC_KVX_S43_TLS_GD_EX6
:
1707 case BFD_RELOC_KVX_S37_TLS_LD_LO10
:
1708 case BFD_RELOC_KVX_S37_TLS_LD_UP27
:
1709 case BFD_RELOC_KVX_S43_TLS_LD_LO10
:
1710 case BFD_RELOC_KVX_S43_TLS_LD_UP27
:
1711 case BFD_RELOC_KVX_S43_TLS_LD_EX6
:
1714 case BFD_RELOC_KVX_S37_TLS_IE_LO10
:
1715 case BFD_RELOC_KVX_S37_TLS_IE_UP27
:
1716 case BFD_RELOC_KVX_S43_TLS_IE_LO10
:
1717 case BFD_RELOC_KVX_S43_TLS_IE_UP27
:
1718 case BFD_RELOC_KVX_S43_TLS_IE_EX6
:
1728 kvx_can_relax_tls (bfd
*input_bfd ATTRIBUTE_UNUSED
,
1729 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1730 bfd_reloc_code_real_type r_type ATTRIBUTE_UNUSED
,
1731 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
1732 unsigned long r_symndx ATTRIBUTE_UNUSED
)
1734 if (! IS_KVX_TLS_RELAX_RELOC (r_type
))
1737 /* Relaxing hook. Disabled on KVX. */
1738 /* See elfnn-aarch64.c */
1742 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
1745 static bfd_reloc_code_real_type
1746 kvx_tls_transition (bfd
*input_bfd
,
1747 struct bfd_link_info
*info
,
1748 unsigned int r_type
,
1749 struct elf_link_hash_entry
*h
,
1750 unsigned long r_symndx
)
1752 bfd_reloc_code_real_type bfd_r_type
1753 = elfNN_kvx_bfd_reloc_from_type (input_bfd
, r_type
);
1755 if (! kvx_can_relax_tls (input_bfd
, info
, bfd_r_type
, h
, r_symndx
))
1761 /* Return the base VMA address which should be subtracted from real addresses
1762 when resolving R_KVX_*_TLS_GD_* and R_KVX_*_TLS_LD_* relocation. */
1765 dtpoff_base (struct bfd_link_info
*info
)
1767 /* If tls_sec is NULL, we should have signalled an error already. */
1768 BFD_ASSERT (elf_hash_table (info
)->tls_sec
!= NULL
);
1769 return elf_hash_table (info
)->tls_sec
->vma
;
1772 /* Return the base VMA address which should be subtracted from real addresses
1773 when resolving R_KVX_*_TLS_IE_* and R_KVX_*_TLS_LE_* relocations. */
1776 tpoff_base (struct bfd_link_info
*info
)
1778 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1780 /* If tls_sec is NULL, we should have signalled an error already. */
1781 BFD_ASSERT (htab
->tls_sec
!= NULL
);
1783 bfd_vma base
= align_power ((bfd_vma
) 0,
1784 htab
->tls_sec
->alignment_power
);
1785 return htab
->tls_sec
->vma
- base
;
1789 symbol_got_offset_ref (bfd
*input_bfd
, struct elf_link_hash_entry
*h
,
1790 unsigned long r_symndx
)
1792 /* Calculate the address of the GOT entry for symbol
1793 referred to in h. */
1795 return &h
->got
.offset
;
1799 struct elf_kvx_local_symbol
*l
;
1801 l
= elf_kvx_locals (input_bfd
);
1802 return &l
[r_symndx
].got_offset
;
1807 symbol_got_offset_mark (bfd
*input_bfd
, struct elf_link_hash_entry
*h
,
1808 unsigned long r_symndx
)
1811 p
= symbol_got_offset_ref (input_bfd
, h
, r_symndx
);
1816 symbol_got_offset_mark_p (bfd
*input_bfd
, struct elf_link_hash_entry
*h
,
1817 unsigned long r_symndx
)
1820 value
= * symbol_got_offset_ref (input_bfd
, h
, r_symndx
);
1825 symbol_got_offset (bfd
*input_bfd
, struct elf_link_hash_entry
*h
,
1826 unsigned long r_symndx
)
1829 value
= * symbol_got_offset_ref (input_bfd
, h
, r_symndx
);
1834 /* N_ONES produces N one bits, without overflowing machine arithmetic. */
1835 #define N_ONES(n) (((((bfd_vma) 1 << ((n) -1)) - 1) << 1) | 1)
1838 * This is a copy/paste + modification from
1839 * reloc.c:_bfd_relocate_contents. Relocations are applied to 32bits
1840 * words, so all overflow checks will overflow for values above
1843 static bfd_reloc_status_type
1844 check_signed_overflow (enum complain_overflow complain_on_overflow
,
1845 bfd_reloc_code_real_type bfd_r_type
, bfd
*input_bfd
,
1848 bfd_reloc_status_type flag
= bfd_reloc_ok
;
1849 bfd_vma addrmask
, fieldmask
, signmask
, ss
;
1853 /* These usually come from howto struct. As we don't check for
1854 * values fitting in bitfields or in subpart of words, we set all
1855 * these to values to check as if the field is starting from first
1858 unsigned int rightshift
= 0;
1859 unsigned int bitpos
= 0;
1860 unsigned int bitsize
= 0;
1861 bfd_vma src_mask
= -1;
1863 /* Only regular symbol relocations are checked here. Others
1864 relocations (GOT, TLS) could be checked if the need is
1865 confirmed. At the moment, we keep previous behavior
1866 (ie. unchecked) for those. */
1869 case BFD_RELOC_KVX_S37_LO10
:
1870 case BFD_RELOC_KVX_S37_UP27
:
1874 case BFD_RELOC_KVX_S32_LO5
:
1875 case BFD_RELOC_KVX_S32_UP27
:
1879 case BFD_RELOC_KVX_S43_LO10
:
1880 case BFD_RELOC_KVX_S43_UP27
:
1881 case BFD_RELOC_KVX_S43_EX6
:
1885 case BFD_RELOC_KVX_S64_LO10
:
1886 case BFD_RELOC_KVX_S64_UP27
:
1887 case BFD_RELOC_KVX_S64_EX27
:
1892 return bfd_reloc_ok
;
1895 /* direct copy/paste from reloc.c below */
1897 /* Get the values to be added together. For signed and unsigned
1898 relocations, we assume that all values should be truncated to
1899 the size of an address. For bitfields, all the bits matter.
1900 See also bfd_check_overflow. */
1901 fieldmask
= N_ONES (bitsize
);
1902 signmask
= ~fieldmask
;
1903 addrmask
= (N_ONES (bfd_arch_bits_per_address (input_bfd
))
1904 | (fieldmask
<< rightshift
));
1905 a
= (relocation
& addrmask
) >> rightshift
;
1906 b
= (x
& src_mask
& addrmask
) >> bitpos
;
1907 addrmask
>>= rightshift
;
1909 switch (complain_on_overflow
)
1911 case complain_overflow_signed
:
1912 /* If any sign bits are set, all sign bits must be set.
1913 That is, A must be a valid negative address after
1915 signmask
= ~(fieldmask
>> 1);
1918 case complain_overflow_bitfield
:
1919 /* Much like the signed check, but for a field one bit
1920 wider. We allow a bitfield to represent numbers in the
1921 range -2**n to 2**n-1, where n is the number of bits in the
1922 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
1923 can't overflow, which is exactly what we want. */
1925 if (ss
!= 0 && ss
!= (addrmask
& signmask
))
1926 flag
= bfd_reloc_overflow
;
1928 /* We only need this next bit of code if the sign bit of B
1929 is below the sign bit of A. This would only happen if
1930 SRC_MASK had fewer bits than BITSIZE. Note that if
1931 SRC_MASK has more bits than BITSIZE, we can get into
1932 trouble; we would need to verify that B is in range, as
1933 we do for A above. */
1934 ss
= ((~src_mask
) >> 1) & src_mask
;
1937 /* Set all the bits above the sign bit. */
1940 /* Now we can do the addition. */
1943 /* See if the result has the correct sign. Bits above the
1944 sign bit are junk now; ignore them. If the sum is
1945 positive, make sure we did not have all negative inputs;
1946 if the sum is negative, make sure we did not have all
1947 positive inputs. The test below looks only at the sign
1948 bits, and it really just
1949 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
1951 We mask with addrmask here to explicitly allow an address
1952 wrap-around. The Linux kernel relies on it, and it is
1953 the only way to write assembler code which can run when
1954 loaded at a location 0x80000000 away from the location at
1955 which it is linked. */
1956 if (((~(a
^ b
)) & (a
^ sum
)) & signmask
& addrmask
)
1957 flag
= bfd_reloc_overflow
;
1960 case complain_overflow_unsigned
:
1961 /* Checking for an unsigned overflow is relatively easy:
1962 trim the addresses and add, and trim the result as well.
1963 Overflow is normally indicated when the result does not
1964 fit in the field. However, we also need to consider the
1965 case when, e.g., fieldmask is 0x7fffffff or smaller, an
1966 input is 0x80000000, and bfd_vma is only 32 bits; then we
1967 will get sum == 0, but there is an overflow, since the
1968 inputs did not fit in the field. Instead of doing a
1969 separate test, we can check for this by or-ing in the
1970 operands when testing for the sum overflowing its final
1972 sum
= (a
+ b
) & addrmask
;
1973 if ((a
| b
| sum
) & signmask
)
1974 flag
= bfd_reloc_overflow
;
1983 /* Perform a relocation as part of a final link. */
1984 static bfd_reloc_status_type
1985 elfNN_kvx_final_link_relocate (reloc_howto_type
*howto
,
1988 asection
*input_section
,
1990 Elf_Internal_Rela
*rel
,
1992 struct bfd_link_info
*info
,
1994 struct elf_link_hash_entry
*h
,
1995 bool *unresolved_reloc_p
,
1997 bfd_vma
*saved_addend
,
1998 Elf_Internal_Sym
*sym
)
2000 Elf_Internal_Shdr
*symtab_hdr
;
2001 unsigned int r_type
= howto
->type
;
2002 bfd_reloc_code_real_type bfd_r_type
2003 = elfNN_kvx_bfd_reloc_from_howto (howto
);
2004 bfd_reloc_code_real_type new_bfd_r_type
;
2005 unsigned long r_symndx
;
2006 bfd_byte
*hit_data
= contents
+ rel
->r_offset
;
2008 bfd_signed_vma signed_addend
;
2009 struct elf_kvx_link_hash_table
*globals
;
2012 bfd_reloc_status_type rret
= bfd_reloc_ok
;
2013 bool resolved_to_zero
;
2014 globals
= elf_kvx_hash_table (info
);
2016 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2018 BFD_ASSERT (is_kvx_elf (input_bfd
));
2020 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2022 /* It is possible to have linker relaxations on some TLS access
2023 models. Update our information here. */
2024 new_bfd_r_type
= kvx_tls_transition (input_bfd
, info
, r_type
, h
, r_symndx
);
2025 if (new_bfd_r_type
!= bfd_r_type
)
2027 bfd_r_type
= new_bfd_r_type
;
2028 howto
= elfNN_kvx_howto_from_bfd_reloc (bfd_r_type
);
2029 BFD_ASSERT (howto
!= NULL
);
2030 r_type
= howto
->type
;
2033 place
= input_section
->output_section
->vma
2034 + input_section
->output_offset
+ rel
->r_offset
;
2036 /* Get addend, accumulating the addend for consecutive relocs
2037 which refer to the same offset. */
2038 signed_addend
= saved_addend
? *saved_addend
: 0;
2039 signed_addend
+= rel
->r_addend
;
2041 weak_undef_p
= (h
? h
->root
.type
== bfd_link_hash_undefweak
2042 : bfd_is_und_section (sym_sec
));
2043 resolved_to_zero
= (h
!= NULL
2044 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2048 case BFD_RELOC_KVX_NN
:
2050 case BFD_RELOC_KVX_32
:
2052 case BFD_RELOC_KVX_S37_LO10
:
2053 case BFD_RELOC_KVX_S37_UP27
:
2055 case BFD_RELOC_KVX_S32_LO5
:
2056 case BFD_RELOC_KVX_S32_UP27
:
2058 case BFD_RELOC_KVX_S43_LO10
:
2059 case BFD_RELOC_KVX_S43_UP27
:
2060 case BFD_RELOC_KVX_S43_EX6
:
2062 case BFD_RELOC_KVX_S64_LO10
:
2063 case BFD_RELOC_KVX_S64_UP27
:
2064 case BFD_RELOC_KVX_S64_EX27
:
2065 /* When generating a shared object or relocatable executable, these
2066 relocations are copied into the output file to be resolved at
2068 if (((bfd_link_pic (info
) == true)
2069 || globals
->root
.is_relocatable_executable
)
2070 && (input_section
->flags
& SEC_ALLOC
)
2072 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
&& !resolved_to_zero
)
2073 || h
->root
.type
!= bfd_link_hash_undefweak
))
2075 Elf_Internal_Rela outrel
;
2077 bool skip
, relocate
;
2080 *unresolved_reloc_p
= false;
2085 outrel
.r_addend
= signed_addend
;
2087 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2089 if (outrel
.r_offset
== (bfd_vma
) - 1)
2091 else if (outrel
.r_offset
== (bfd_vma
) - 2)
2097 outrel
.r_offset
+= (input_section
->output_section
->vma
2098 + input_section
->output_offset
);
2101 memset (&outrel
, 0, sizeof outrel
);
2104 && (!bfd_link_pic (info
) || !info
->symbolic
|| !h
->def_regular
))
2105 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2106 else if (bfd_r_type
== BFD_RELOC_KVX_32
2107 || bfd_r_type
== BFD_RELOC_KVX_64
)
2111 /* On SVR4-ish systems, the dynamic loader cannot
2112 relocate the text and data segments independently,
2113 so the symbol does not matter. */
2115 outrel
.r_info
= ELFNN_R_INFO (symbol
, R_KVX_RELATIVE
);
2116 outrel
.r_addend
+= value
;
2118 else if (bfd_link_pic (info
) && info
->symbolic
)
2120 goto skip_because_pic
;
2124 /* We may endup here from bad input code trying to
2125 insert relocation on symbols within code. We do not
2126 want that currently, and such code should use GOT +
2127 KVX_32/64 reloc that translate in KVX_RELATIVE
2130 if (h
&& h
->root
.root
.string
)
2131 name
= h
->root
.root
.string
;
2133 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
,
2136 (*_bfd_error_handler
)
2137 /* xgettext:c-format */
2138 (_("%pB(%pA+%#" PRIx64
"): "
2139 "unresolvable %s relocation in section `%s'"),
2140 input_bfd
, input_section
, (uint64_t) rel
->r_offset
, howto
->name
,
2142 return bfd_reloc_notsupported
;
2145 sreloc
= elf_section_data (input_section
)->sreloc
;
2146 if (sreloc
== NULL
|| sreloc
->contents
== NULL
)
2147 return bfd_reloc_notsupported
;
2149 loc
= sreloc
->contents
+ sreloc
->reloc_count
++ * RELOC_SIZE (globals
);
2150 bfd_elfNN_swap_reloca_out (output_bfd
, &outrel
, loc
);
2152 if (sreloc
->reloc_count
* RELOC_SIZE (globals
) > sreloc
->size
)
2154 /* Sanity to check that we have previously allocated
2155 sufficient space in the relocation section for the
2156 number of relocations we actually want to emit. */
2160 /* If this reloc is against an external symbol, we do not want to
2161 fiddle with the addend. Otherwise, we need to include the symbol
2162 value so that it becomes an addend for the dynamic reloc. */
2164 return bfd_reloc_ok
;
2166 rret
= check_signed_overflow (complain_overflow_signed
, bfd_r_type
,
2167 input_bfd
, value
+ signed_addend
);
2168 if (rret
!= bfd_reloc_ok
)
2171 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2172 contents
, rel
->r_offset
, value
,
2177 rret
= check_signed_overflow (complain_overflow_signed
, bfd_r_type
,
2178 input_bfd
, value
+ signed_addend
);
2179 if (rret
!= bfd_reloc_ok
)
2182 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2183 contents
, rel
->r_offset
, value
,
2187 case BFD_RELOC_KVX_PCREL17
:
2188 case BFD_RELOC_KVX_PCREL27
:
2191 * BCU insn are always first in a bundle, so there is no need
2192 * to correct the address using offset within bundle
2195 asection
*splt
= globals
->root
.splt
;
2197 splt
!= NULL
&& h
!= NULL
&& h
->plt
.offset
!= (bfd_vma
) - 1;
2199 /* A call to an undefined weak symbol is converted to a jump to
2200 the next instruction unless a PLT entry will be created.
2201 The jump to the next instruction is optimized as a NOP.
2202 Do the same for local undefined symbols. */
2203 if (weak_undef_p
&& ! via_plt_p
)
2205 bfd_putl32 (INSN_NOP
, hit_data
);
2206 return bfd_reloc_ok
;
2209 /* If the call goes through a PLT entry, make sure to
2210 check distance to the right destination address. */
2212 value
= (splt
->output_section
->vma
2213 + splt
->output_offset
+ h
->plt
.offset
);
2215 /* Check if a stub has to be inserted because the destination
2217 struct elf_kvx_stub_hash_entry
*stub_entry
= NULL
;
2219 /* If the target symbol is global and marked as a function the
2220 relocation applies a function call or a tail call. In this
2221 situation we can veneer out of range branches. The veneers
2222 use R16 and R17 hence cannot be used arbitrary out of range
2223 branches that occur within the body of a function. */
2225 /* Check if a stub has to be inserted because the destination
2227 if (! kvx_valid_call_p (value
, place
))
2229 /* The target is out of reach, so redirect the branch to
2230 the local stub for this function. */
2231 stub_entry
= elfNN_kvx_get_stub_entry (input_section
,
2234 if (stub_entry
!= NULL
)
2235 value
= (stub_entry
->stub_offset
2236 + stub_entry
->stub_sec
->output_offset
2237 + stub_entry
->stub_sec
->output_section
->vma
);
2238 /* We have redirected the destination to stub entry address,
2239 so ignore any addend record in the original rela entry. */
2243 *unresolved_reloc_p
= false;
2247 /* PCREL 32 are used in dwarf2 table for exception handling */
2248 case BFD_RELOC_KVX_32_PCREL
:
2249 case BFD_RELOC_KVX_S64_PCREL_LO10
:
2250 case BFD_RELOC_KVX_S64_PCREL_UP27
:
2251 case BFD_RELOC_KVX_S64_PCREL_EX27
:
2252 case BFD_RELOC_KVX_S37_PCREL_LO10
:
2253 case BFD_RELOC_KVX_S37_PCREL_UP27
:
2254 case BFD_RELOC_KVX_S43_PCREL_LO10
:
2255 case BFD_RELOC_KVX_S43_PCREL_UP27
:
2256 case BFD_RELOC_KVX_S43_PCREL_EX6
:
2257 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2258 contents
, rel
->r_offset
, value
,
2262 case BFD_RELOC_KVX_S37_TLS_LE_LO10
:
2263 case BFD_RELOC_KVX_S37_TLS_LE_UP27
:
2265 case BFD_RELOC_KVX_S43_TLS_LE_LO10
:
2266 case BFD_RELOC_KVX_S43_TLS_LE_UP27
:
2267 case BFD_RELOC_KVX_S43_TLS_LE_EX6
:
2268 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2269 contents
, rel
->r_offset
, value
- tpoff_base (info
),
2273 case BFD_RELOC_KVX_S37_TLS_DTPOFF_LO10
:
2274 case BFD_RELOC_KVX_S37_TLS_DTPOFF_UP27
:
2276 case BFD_RELOC_KVX_S43_TLS_DTPOFF_LO10
:
2277 case BFD_RELOC_KVX_S43_TLS_DTPOFF_UP27
:
2278 case BFD_RELOC_KVX_S43_TLS_DTPOFF_EX6
:
2279 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2280 contents
, rel
->r_offset
, value
- dtpoff_base (info
),
2283 case BFD_RELOC_KVX_S37_TLS_GD_UP27
:
2284 case BFD_RELOC_KVX_S37_TLS_GD_LO10
:
2286 case BFD_RELOC_KVX_S43_TLS_GD_UP27
:
2287 case BFD_RELOC_KVX_S43_TLS_GD_EX6
:
2288 case BFD_RELOC_KVX_S43_TLS_GD_LO10
:
2290 case BFD_RELOC_KVX_S37_TLS_IE_UP27
:
2291 case BFD_RELOC_KVX_S37_TLS_IE_LO10
:
2293 case BFD_RELOC_KVX_S43_TLS_IE_UP27
:
2294 case BFD_RELOC_KVX_S43_TLS_IE_EX6
:
2295 case BFD_RELOC_KVX_S43_TLS_IE_LO10
:
2297 case BFD_RELOC_KVX_S37_TLS_LD_UP27
:
2298 case BFD_RELOC_KVX_S37_TLS_LD_LO10
:
2300 case BFD_RELOC_KVX_S43_TLS_LD_UP27
:
2301 case BFD_RELOC_KVX_S43_TLS_LD_EX6
:
2302 case BFD_RELOC_KVX_S43_TLS_LD_LO10
:
2304 if (globals
->root
.sgot
== NULL
)
2305 return bfd_reloc_notsupported
;
2306 value
= symbol_got_offset (input_bfd
, h
, r_symndx
);
2308 _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2309 contents
, rel
->r_offset
, value
,
2311 *unresolved_reloc_p
= false;
2314 case BFD_RELOC_KVX_S37_GOTADDR_UP27
:
2315 case BFD_RELOC_KVX_S37_GOTADDR_LO10
:
2317 case BFD_RELOC_KVX_S43_GOTADDR_UP27
:
2318 case BFD_RELOC_KVX_S43_GOTADDR_EX6
:
2319 case BFD_RELOC_KVX_S43_GOTADDR_LO10
:
2321 case BFD_RELOC_KVX_S64_GOTADDR_UP27
:
2322 case BFD_RELOC_KVX_S64_GOTADDR_EX27
:
2323 case BFD_RELOC_KVX_S64_GOTADDR_LO10
:
2325 if (globals
->root
.sgot
== NULL
)
2326 BFD_ASSERT (h
!= NULL
);
2328 value
= globals
->root
.sgot
->output_section
->vma
2329 + globals
->root
.sgot
->output_offset
;
2331 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2332 contents
, rel
->r_offset
, value
,
2337 case BFD_RELOC_KVX_S37_GOTOFF_LO10
:
2338 case BFD_RELOC_KVX_S37_GOTOFF_UP27
:
2340 case BFD_RELOC_KVX_32_GOTOFF
:
2341 case BFD_RELOC_KVX_64_GOTOFF
:
2343 case BFD_RELOC_KVX_S43_GOTOFF_LO10
:
2344 case BFD_RELOC_KVX_S43_GOTOFF_UP27
:
2345 case BFD_RELOC_KVX_S43_GOTOFF_EX6
:
2348 asection
*basegot
= globals
->root
.sgot
;
2349 /* BFD_ASSERT(h == NULL); */
2350 BFD_ASSERT(globals
->root
.sgot
!= NULL
);
2351 value
-= basegot
->output_section
->vma
+ basegot
->output_offset
;
2352 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2353 contents
, rel
->r_offset
, value
,
2358 case BFD_RELOC_KVX_S37_GOT_LO10
:
2359 case BFD_RELOC_KVX_S37_GOT_UP27
:
2361 case BFD_RELOC_KVX_32_GOT
:
2362 case BFD_RELOC_KVX_64_GOT
:
2364 case BFD_RELOC_KVX_S43_GOT_LO10
:
2365 case BFD_RELOC_KVX_S43_GOT_UP27
:
2366 case BFD_RELOC_KVX_S43_GOT_EX6
:
2368 if (globals
->root
.sgot
== NULL
)
2369 BFD_ASSERT (h
!= NULL
);
2373 value
= kvx_calculate_got_entry_vma (h
, globals
, info
, value
,
2375 unresolved_reloc_p
);
2377 printf("GOT_LO/HI for %s, value %x\n", h
->root
.root
.string
, value
);
2380 /* value = _bfd_kvx_elf_resolve_relocation (bfd_r_type, place, value, */
2381 /* 0, weak_undef_p); */
2382 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2383 contents
, rel
->r_offset
, value
,
2389 printf("GOT_LO/HI with h NULL, initial value %x\n", value
);
2393 struct elf_kvx_local_symbol
*locals
2394 = elf_kvx_locals (input_bfd
);
2398 int howto_index
= bfd_r_type
- BFD_RELOC_KVX_RELOC_START
;
2400 /* xgettext:c-format */
2401 (_("%pB: local symbol descriptor table be NULL when applying "
2402 "relocation %s against local symbol"),
2403 input_bfd
, elf_kvx_howto_table
[howto_index
].name
);
2407 off
= symbol_got_offset (input_bfd
, h
, r_symndx
);
2408 base_got
= globals
->root
.sgot
;
2409 bfd_vma got_entry_addr
= (base_got
->output_section
->vma
2410 + base_got
->output_offset
+ off
);
2412 if (!symbol_got_offset_mark_p (input_bfd
, h
, r_symndx
))
2414 bfd_put_64 (output_bfd
, value
, base_got
->contents
+ off
);
2416 if (bfd_link_pic (info
))
2419 Elf_Internal_Rela outrel
;
2421 /* For local symbol, we have done absolute relocation in static
2422 linking stageh. While for share library, we need to update
2423 the content of GOT entry according to the share objects
2424 loading base address. So we need to generate a
2425 R_AARCH64_RELATIVE reloc for dynamic linker. */
2426 s
= globals
->root
.srelgot
;
2430 outrel
.r_offset
= got_entry_addr
;
2431 outrel
.r_info
= ELFNN_R_INFO (0, R_KVX_RELATIVE
);
2432 outrel
.r_addend
= value
;
2433 elf_append_rela (output_bfd
, s
, &outrel
);
2436 symbol_got_offset_mark (input_bfd
, h
, r_symndx
);
2439 /* Update the relocation value to GOT entry addr as we have transformed
2440 the direct data access into indirect data access through GOT. */
2441 value
= got_entry_addr
;
2443 return _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2444 contents
, rel
->r_offset
, off
,
2450 return bfd_reloc_notsupported
;
2454 *saved_addend
= value
;
2456 /* Only apply the final relocation in a sequence. */
2458 return bfd_reloc_continue
;
2460 return _bfd_kvx_elf_put_addend (input_bfd
, hit_data
, bfd_r_type
,
2466 /* Relocate a KVX ELF section. */
2469 elfNN_kvx_relocate_section (bfd
*output_bfd
,
2470 struct bfd_link_info
*info
,
2472 asection
*input_section
,
2474 Elf_Internal_Rela
*relocs
,
2475 Elf_Internal_Sym
*local_syms
,
2476 asection
**local_sections
)
2478 Elf_Internal_Shdr
*symtab_hdr
;
2479 struct elf_link_hash_entry
**sym_hashes
;
2480 Elf_Internal_Rela
*rel
;
2481 Elf_Internal_Rela
*relend
;
2483 struct elf_kvx_link_hash_table
*globals
;
2484 bool save_addend
= false;
2487 globals
= elf_kvx_hash_table (info
);
2489 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2490 sym_hashes
= elf_sym_hashes (input_bfd
);
2493 relend
= relocs
+ input_section
->reloc_count
;
2494 for (; rel
< relend
; rel
++)
2496 unsigned int r_type
;
2497 bfd_reloc_code_real_type bfd_r_type
;
2498 reloc_howto_type
*howto
;
2499 unsigned long r_symndx
;
2500 Elf_Internal_Sym
*sym
;
2502 struct elf_link_hash_entry
*h
;
2504 bfd_reloc_status_type r
;
2507 bool unresolved_reloc
= false;
2508 char *error_message
= NULL
;
2510 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2511 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2513 bfd_reloc
.howto
= elfNN_kvx_howto_from_type (input_bfd
, r_type
);
2514 howto
= bfd_reloc
.howto
;
2517 return _bfd_unrecognized_reloc (input_bfd
, input_section
, r_type
);
2519 bfd_r_type
= elfNN_kvx_bfd_reloc_from_howto (howto
);
2525 if (r_symndx
< symtab_hdr
->sh_info
) /* A local symbol. */
2527 sym
= local_syms
+ r_symndx
;
2528 sym_type
= ELFNN_ST_TYPE (sym
->st_info
);
2529 sec
= local_sections
[r_symndx
];
2531 /* An object file might have a reference to a local
2532 undefined symbol. This is a draft object file, but we
2533 should at least do something about it. */
2534 if (r_type
!= R_KVX_NONE
2535 && r_type
!= R_KVX_S37_GOTADDR_LO10
2536 && r_type
!= R_KVX_S37_GOTADDR_UP27
2537 && r_type
!= R_KVX_S64_GOTADDR_LO10
2538 && r_type
!= R_KVX_S64_GOTADDR_UP27
2539 && r_type
!= R_KVX_S64_GOTADDR_EX27
2540 && r_type
!= R_KVX_S43_GOTADDR_LO10
2541 && r_type
!= R_KVX_S43_GOTADDR_UP27
2542 && r_type
!= R_KVX_S43_GOTADDR_EX6
2543 && bfd_is_und_section (sec
)
2544 && ELF_ST_BIND (sym
->st_info
) != STB_WEAK
)
2545 (*info
->callbacks
->undefined_symbol
)
2546 (info
, bfd_elf_string_from_elf_section
2547 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
),
2548 input_bfd
, input_section
, rel
->r_offset
, true);
2550 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2554 bool warned
, ignored
;
2556 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2557 r_symndx
, symtab_hdr
, sym_hashes
,
2559 unresolved_reloc
, warned
, ignored
);
2564 if (sec
!= NULL
&& discarded_section (sec
))
2565 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2566 rel
, 1, relend
, howto
, 0, contents
);
2568 if (bfd_link_relocatable (info
))
2572 name
= h
->root
.root
.string
;
2575 name
= (bfd_elf_string_from_elf_section
2576 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2577 if (name
== NULL
|| *name
== '\0')
2578 name
= bfd_section_name (sec
);
2582 && r_type
!= R_KVX_NONE
2584 || h
->root
.type
== bfd_link_hash_defined
2585 || h
->root
.type
== bfd_link_hash_defweak
)
2586 && IS_KVX_TLS_RELOC (bfd_r_type
) != (sym_type
== STT_TLS
))
2588 (*_bfd_error_handler
)
2589 ((sym_type
== STT_TLS
2590 /* xgettext:c-format */
2591 ? _("%pB(%pA+%#lx): %s used with TLS symbol %s")
2592 /* xgettext:c-format */
2593 : _("%pB(%pA+%#lx): %s used with non-TLS symbol %s")),
2595 input_section
, (long) rel
->r_offset
, howto
->name
, name
);
2598 /* Original aarch64 has relaxation handling for TLS here. */
2599 r
= bfd_reloc_continue
;
2601 /* There may be multiple consecutive relocations for the
2602 same offset. In that case we are supposed to treat the
2603 output of each relocation as the addend for the next. */
2604 if (rel
+ 1 < relend
2605 && rel
->r_offset
== rel
[1].r_offset
2606 && ELFNN_R_TYPE (rel
[1].r_info
) != R_KVX_NONE
)
2610 save_addend
= false;
2612 if (r
== bfd_reloc_continue
)
2613 r
= elfNN_kvx_final_link_relocate (howto
, input_bfd
, output_bfd
,
2614 input_section
, contents
, rel
,
2615 relocation
, info
, sec
,
2616 h
, &unresolved_reloc
,
2617 save_addend
, &addend
, sym
);
2619 switch (elfNN_kvx_bfd_reloc_from_type (input_bfd
, r_type
))
2621 case BFD_RELOC_KVX_S37_TLS_GD_LO10
:
2622 case BFD_RELOC_KVX_S37_TLS_GD_UP27
:
2624 case BFD_RELOC_KVX_S43_TLS_GD_LO10
:
2625 case BFD_RELOC_KVX_S43_TLS_GD_UP27
:
2626 case BFD_RELOC_KVX_S43_TLS_GD_EX6
:
2628 case BFD_RELOC_KVX_S37_TLS_LD_LO10
:
2629 case BFD_RELOC_KVX_S37_TLS_LD_UP27
:
2631 case BFD_RELOC_KVX_S43_TLS_LD_LO10
:
2632 case BFD_RELOC_KVX_S43_TLS_LD_UP27
:
2633 case BFD_RELOC_KVX_S43_TLS_LD_EX6
:
2635 if (! symbol_got_offset_mark_p (input_bfd
, h
, r_symndx
))
2637 bool need_relocs
= false;
2642 off
= symbol_got_offset (input_bfd
, h
, r_symndx
);
2643 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
2646 (bfd_link_pic (info
) || indx
!= 0) &&
2648 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2649 || h
->root
.type
!= bfd_link_hash_undefweak
);
2651 BFD_ASSERT (globals
->root
.srelgot
!= NULL
);
2655 Elf_Internal_Rela rela
;
2656 rela
.r_info
= ELFNN_R_INFO (indx
, R_KVX_64_DTPMOD
);
2658 rela
.r_offset
= globals
->root
.sgot
->output_section
->vma
+
2659 globals
->root
.sgot
->output_offset
+ off
;
2661 loc
= globals
->root
.srelgot
->contents
;
2662 loc
+= globals
->root
.srelgot
->reloc_count
++
2663 * RELOC_SIZE (htab
);
2664 bfd_elfNN_swap_reloca_out (output_bfd
, &rela
, loc
);
2666 bfd_reloc_code_real_type real_type
=
2667 elfNN_kvx_bfd_reloc_from_type (input_bfd
, r_type
);
2669 if (real_type
== BFD_RELOC_KVX_S37_TLS_LD_LO10
2670 || real_type
== BFD_RELOC_KVX_S37_TLS_LD_UP27
2671 || real_type
== BFD_RELOC_KVX_S43_TLS_LD_LO10
2672 || real_type
== BFD_RELOC_KVX_S43_TLS_LD_UP27
2673 || real_type
== BFD_RELOC_KVX_S43_TLS_LD_EX6
)
2675 /* For local dynamic, don't generate DTPOFF in any case.
2676 Initialize the DTPOFF slot into zero, so we get module
2677 base address when invoke runtime TLS resolver. */
2678 bfd_put_NN (output_bfd
, 0,
2679 globals
->root
.sgot
->contents
+ off
2684 bfd_put_NN (output_bfd
,
2685 relocation
- dtpoff_base (info
),
2686 globals
->root
.sgot
->contents
+ off
2691 /* This TLS symbol is global. We emit a
2692 relocation to fixup the tls offset at load
2695 ELFNN_R_INFO (indx
, R_KVX_64_DTPOFF
);
2698 (globals
->root
.sgot
->output_section
->vma
2699 + globals
->root
.sgot
->output_offset
+ off
2702 loc
= globals
->root
.srelgot
->contents
;
2703 loc
+= globals
->root
.srelgot
->reloc_count
++
2704 * RELOC_SIZE (globals
);
2705 bfd_elfNN_swap_reloca_out (output_bfd
, &rela
, loc
);
2706 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2707 globals
->root
.sgot
->contents
+ off
2713 bfd_put_NN (output_bfd
, (bfd_vma
) 1,
2714 globals
->root
.sgot
->contents
+ off
);
2715 bfd_put_NN (output_bfd
,
2716 relocation
- dtpoff_base (info
),
2717 globals
->root
.sgot
->contents
+ off
2721 symbol_got_offset_mark (input_bfd
, h
, r_symndx
);
2725 case BFD_RELOC_KVX_S37_TLS_IE_LO10
:
2726 case BFD_RELOC_KVX_S37_TLS_IE_UP27
:
2728 case BFD_RELOC_KVX_S43_TLS_IE_LO10
:
2729 case BFD_RELOC_KVX_S43_TLS_IE_UP27
:
2730 case BFD_RELOC_KVX_S43_TLS_IE_EX6
:
2731 if (! symbol_got_offset_mark_p (input_bfd
, h
, r_symndx
))
2733 bool need_relocs
= false;
2738 off
= symbol_got_offset (input_bfd
, h
, r_symndx
);
2740 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
2743 (bfd_link_pic (info
) || indx
!= 0) &&
2745 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2746 || h
->root
.type
!= bfd_link_hash_undefweak
);
2748 BFD_ASSERT (globals
->root
.srelgot
!= NULL
);
2752 Elf_Internal_Rela rela
;
2755 rela
.r_addend
= relocation
- dtpoff_base (info
);
2759 rela
.r_info
= ELFNN_R_INFO (indx
, R_KVX_64_TPOFF
);
2760 rela
.r_offset
= globals
->root
.sgot
->output_section
->vma
+
2761 globals
->root
.sgot
->output_offset
+ off
;
2763 loc
= globals
->root
.srelgot
->contents
;
2764 loc
+= globals
->root
.srelgot
->reloc_count
++
2765 * RELOC_SIZE (htab
);
2767 bfd_elfNN_swap_reloca_out (output_bfd
, &rela
, loc
);
2769 bfd_put_NN (output_bfd
, rela
.r_addend
,
2770 globals
->root
.sgot
->contents
+ off
);
2773 bfd_put_NN (output_bfd
, relocation
- tpoff_base (info
),
2774 globals
->root
.sgot
->contents
+ off
);
2776 symbol_got_offset_mark (input_bfd
, h
, r_symndx
);
2784 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2785 because such sections are not SEC_ALLOC and thus ld.so will
2786 not process them. */
2787 if (unresolved_reloc
2788 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2790 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2791 +rel
->r_offset
) != (bfd_vma
) - 1)
2793 (*_bfd_error_handler
)
2794 /* xgettext:c-format */
2795 (_("%pB(%pA+%#" PRIx64
"): "
2796 "unresolvable %s relocation against symbol `%s'"),
2797 input_bfd
, input_section
, (uint64_t) rel
->r_offset
, howto
->name
,
2798 h
->root
.root
.string
);
2802 if (r
!= bfd_reloc_ok
&& r
!= bfd_reloc_continue
)
2806 case bfd_reloc_overflow
:
2807 (*info
->callbacks
->reloc_overflow
)
2808 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
2809 input_bfd
, input_section
, rel
->r_offset
);
2811 /* Original aarch64 code had a check for alignement correctness */
2814 case bfd_reloc_undefined
:
2815 (*info
->callbacks
->undefined_symbol
)
2816 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, true);
2819 case bfd_reloc_outofrange
:
2820 error_message
= _("out of range");
2823 case bfd_reloc_notsupported
:
2824 error_message
= _("unsupported relocation");
2827 case bfd_reloc_dangerous
:
2828 /* error_message should already be set. */
2832 error_message
= _("unknown error");
2836 BFD_ASSERT (error_message
!= NULL
);
2837 (*info
->callbacks
->reloc_dangerous
)
2838 (info
, error_message
, input_bfd
, input_section
, rel
->r_offset
);
2850 /* Set the right machine number. */
2853 elfNN_kvx_object_p (bfd
*abfd
)
2855 /* must be coherent with default arch in cpu-kvx.c */
2856 int e_set
= bfd_mach_kv3_1
;
2858 if (elf_elfheader (abfd
)->e_machine
== EM_KVX
)
2860 int e_core
= elf_elfheader (abfd
)->e_flags
& ELF_KVX_CORE_MASK
;
2864 case ELF_KVX_CORE_KV3_1
: e_set
= bfd_mach_kv3_1_64
; break;
2865 case ELF_KVX_CORE_KV3_2
: e_set
= bfd_mach_kv3_2_64
; break;
2866 case ELF_KVX_CORE_KV4_1
: e_set
= bfd_mach_kv4_1_64
; break;
2868 case ELF_KVX_CORE_KV3_1
: e_set
= bfd_mach_kv3_1
; break;
2869 case ELF_KVX_CORE_KV3_2
: e_set
= bfd_mach_kv3_2
; break;
2870 case ELF_KVX_CORE_KV4_1
: e_set
= bfd_mach_kv4_1
; break;
2873 (*_bfd_error_handler
)(_("%s: Bad ELF id: `%d'"),
2874 abfd
->filename
, e_core
);
2877 return bfd_default_set_arch_mach (abfd
, bfd_arch_kvx
, e_set
);
2881 /* Function to keep KVX specific flags in the ELF header. */
2884 elfNN_kvx_set_private_flags (bfd
*abfd
, flagword flags
)
2886 if (elf_flags_init (abfd
) && elf_elfheader (abfd
)->e_flags
!= flags
)
2891 elf_elfheader (abfd
)->e_flags
= flags
;
2892 elf_flags_init (abfd
) = true;
2898 /* Merge backend specific data from an object file to the output
2899 object file when linking. */
2902 elfNN_kvx_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
2904 bfd
*obfd
= info
->output_bfd
;
2907 bool flags_compatible
= true;
2910 /* Check if we have the same endianess. */
2911 if (!_bfd_generic_verify_endian_match (ibfd
, info
))
2914 if (!is_kvx_elf (ibfd
) || !is_kvx_elf (obfd
))
2917 /* The input BFD must have had its flags initialised. */
2918 /* The following seems bogus to me -- The flags are initialized in
2919 the assembler but I don't think an elf_flags_init field is
2920 written into the object. */
2921 /* BFD_ASSERT (elf_flags_init (ibfd)); */
2923 if (bfd_get_arch_size (ibfd
) != bfd_get_arch_size (obfd
))
2927 if (bfd_get_arch_size (ibfd
) == 32
2928 && bfd_get_arch_size (obfd
) == 64)
2929 msg
= _("%s: compiled as 32-bit object and %s is 64-bit");
2930 else if (bfd_get_arch_size (ibfd
) == 64
2931 && bfd_get_arch_size (obfd
) == 32)
2932 msg
= _("%s: compiled as 64-bit object and %s is 32-bit");
2934 msg
= _("%s: object size does not match that of target %s");
2936 (*_bfd_error_handler
) (msg
, bfd_get_filename (ibfd
),
2937 bfd_get_filename (obfd
));
2938 bfd_set_error (bfd_error_wrong_format
);
2942 in_flags
= elf_elfheader (ibfd
)->e_flags
;
2943 out_flags
= elf_elfheader (obfd
)->e_flags
;
2945 if (!elf_flags_init (obfd
))
2947 /* If the input is the default architecture and had the default
2948 flags then do not bother setting the flags for the output
2949 architecture, instead allow future merges to do this. If no
2950 future merges ever set these flags then they will retain their
2951 uninitialised values, which surprise surprise, correspond
2952 to the default values. */
2953 if (bfd_get_arch_info (ibfd
)->the_default
2954 && elf_elfheader (ibfd
)->e_flags
== 0)
2957 elf_flags_init (obfd
) = true;
2958 elf_elfheader (obfd
)->e_flags
= in_flags
;
2960 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
2961 && bfd_get_arch_info (obfd
)->the_default
)
2962 return bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
2963 bfd_get_mach (ibfd
));
2968 /* Identical flags must be compatible. */
2969 if (in_flags
== out_flags
)
2972 /* Check to see if the input BFD actually contains any sections. If
2973 not, its flags may not have been initialised either, but it
2974 cannot actually cause any incompatiblity. Do not short-circuit
2975 dynamic objects; their section list may be emptied by
2976 elf_link_add_object_symbols.
2978 Also check to see if there are no code sections in the input.
2979 In this case there is no need to check for code specific flags.
2980 XXX - do we need to worry about floating-point format compatability
2981 in data sections ? */
2982 if (!(ibfd
->flags
& DYNAMIC
))
2984 bool null_input_bfd
= true;
2985 bool only_data_sections
= true;
2987 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2989 if ((bfd_section_flags (sec
)
2990 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
2991 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
2992 only_data_sections
= false;
2994 null_input_bfd
= false;
2998 if (null_input_bfd
|| only_data_sections
)
3001 return flags_compatible
;
3004 /* Display the flags field. */
3007 elfNN_kvx_print_private_bfd_data (bfd
*abfd
, void *ptr
)
3009 FILE *file
= (FILE *) ptr
;
3010 unsigned long flags
;
3012 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
3014 /* Print normal ELF private data. */
3015 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
3017 flags
= elf_elfheader (abfd
)->e_flags
;
3018 /* Ignore init flag - it may not be set, despite the flags field
3019 containing valid data. */
3021 /* xgettext:c-format */
3022 fprintf (file
, _("Private flags = 0x%lx : "), elf_elfheader (abfd
)->e_flags
);
3023 if((flags
& ELF_KVX_ABI_64B_ADDR_BIT
) == ELF_KVX_ABI_64B_ADDR_BIT
)
3025 if (ELF_KVX_CHECK_CORE(flags
,ELF_KVX_CORE_KV3_1
))
3026 fprintf (file
, _("Coolidge (kv3) V1 64 bits"));
3027 else if (ELF_KVX_CHECK_CORE(flags
,ELF_KVX_CORE_KV3_2
))
3028 fprintf (file
, _("Coolidge (kv3) V2 64 bits"));
3029 else if (ELF_KVX_CHECK_CORE(flags
,ELF_KVX_CORE_KV4_1
))
3030 fprintf (file
, _("Coolidge (kv4) V1 64 bits"));
3034 if (ELF_KVX_CHECK_CORE(flags
,ELF_KVX_CORE_KV3_1
))
3035 fprintf (file
, _("Coolidge (kv3) V1 32 bits"));
3036 else if (ELF_KVX_CHECK_CORE(flags
,ELF_KVX_CORE_KV3_2
))
3037 fprintf (file
, _("Coolidge (kv3) V2 32 bits"));
3038 else if (ELF_KVX_CHECK_CORE(flags
,ELF_KVX_CORE_KV4_1
))
3039 fprintf (file
, _("Coolidge (kv4) V1 32 bits"));
3047 /* Adjust a symbol defined by a dynamic object and referenced by a
3048 regular object. The current definition is in some section of the
3049 dynamic object, but we're not including those sections. We have to
3050 change the definition to something the rest of the link can
3054 elfNN_kvx_adjust_dynamic_symbol (struct bfd_link_info
*info
,
3055 struct elf_link_hash_entry
*h
)
3057 struct elf_kvx_link_hash_table
*htab
;
3060 /* If this is a function, put it in the procedure linkage table. We
3061 will fill in the contents of the procedure linkage table later,
3062 when we know the address of the .got section. */
3063 if (h
->type
== STT_FUNC
|| h
->needs_plt
)
3065 if (h
->plt
.refcount
<= 0
3066 || ((SYMBOL_CALLS_LOCAL (info
, h
)
3067 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3068 && h
->root
.type
== bfd_link_hash_undefweak
))))
3070 /* This case can occur if we saw a CALL26 reloc in
3071 an input file, but the symbol wasn't referred to
3072 by a dynamic object or all references were
3073 garbage collected. In which case we can end up
3075 h
->plt
.offset
= (bfd_vma
) - 1;
3082 /* Otherwise, reset to -1. */
3083 h
->plt
.offset
= (bfd_vma
) - 1;
3086 /* If this is a weak symbol, and there is a real definition, the
3087 processor independent code will have arranged for us to see the
3088 real definition first, and we can just use the same value. */
3089 if (h
->is_weakalias
)
3091 struct elf_link_hash_entry
*def
= weakdef (h
);
3092 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
3093 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
3094 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
3095 if (ELIMINATE_COPY_RELOCS
|| info
->nocopyreloc
)
3096 h
->non_got_ref
= def
->non_got_ref
;
3100 /* If we are creating a shared library, we must presume that the
3101 only references to the symbol are via the global offset table.
3102 For such cases we need not do anything here; the relocations will
3103 be handled correctly by relocate_section. */
3104 if (bfd_link_pic (info
))
3107 /* If there are no references to this symbol that do not use the
3108 GOT, we don't need to generate a copy reloc. */
3109 if (!h
->non_got_ref
)
3112 /* If -z nocopyreloc was given, we won't generate them either. */
3113 if (info
->nocopyreloc
)
3119 /* We must allocate the symbol in our .dynbss section, which will
3120 become part of the .bss section of the executable. There will be
3121 an entry for this symbol in the .dynsym section. The dynamic
3122 object will contain position independent code, so all references
3123 from the dynamic object to this symbol will go through the global
3124 offset table. The dynamic linker will use the .dynsym entry to
3125 determine the address it must put in the global offset table, so
3126 both the dynamic object and the regular object will refer to the
3127 same memory location for the variable. */
3129 htab
= elf_kvx_hash_table (info
);
3131 /* We must generate a R_KVX_COPY reloc to tell the dynamic linker
3132 to copy the initial value out of the dynamic object and into the
3133 runtime process image. */
3134 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
3136 htab
->srelbss
->size
+= RELOC_SIZE (htab
);
3142 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
3147 elfNN_kvx_allocate_local_symbols (bfd
*abfd
, unsigned number
)
3149 struct elf_kvx_local_symbol
*locals
;
3150 locals
= elf_kvx_locals (abfd
);
3153 locals
= (struct elf_kvx_local_symbol
*)
3154 bfd_zalloc (abfd
, number
* sizeof (struct elf_kvx_local_symbol
));
3157 elf_kvx_locals (abfd
) = locals
;
3162 /* Create the .got section to hold the global offset table. */
3165 kvx_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
3167 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3170 struct elf_link_hash_entry
*h
;
3171 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3173 /* This function may be called more than once. */
3174 s
= bfd_get_linker_section (abfd
, ".got");
3178 flags
= bed
->dynamic_sec_flags
;
3180 s
= bfd_make_section_anyway_with_flags (abfd
,
3181 (bed
->rela_plts_and_copies_p
3182 ? ".rela.got" : ".rel.got"),
3183 (bed
->dynamic_sec_flags
3186 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
3191 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
3193 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
3196 htab
->sgot
->size
+= GOT_ENTRY_SIZE
;
3198 if (bed
->want_got_sym
)
3200 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
3201 (or .got.plt) section. We don't do this in the linker script
3202 because we don't want to define the symbol if we are not creating
3203 a global offset table. */
3204 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
3205 "_GLOBAL_OFFSET_TABLE_");
3206 elf_hash_table (info
)->hgot
= h
;
3211 if (bed
->want_got_plt
)
3213 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
3215 || !bfd_set_section_alignment (s
,
3216 bed
->s
->log_file_align
))
3221 /* The first bit of the global offset table is the header. */
3222 s
->size
+= bed
->got_header_size
;
3224 /* we still need to handle got content when doing static link with PIC */
3225 if (bfd_link_executable (info
) && !bfd_link_pic (info
)) {
3226 htab
->dynobj
= abfd
;
3232 /* Look through the relocs for a section during the first phase. */
3235 elfNN_kvx_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
3236 asection
*sec
, const Elf_Internal_Rela
*relocs
)
3238 Elf_Internal_Shdr
*symtab_hdr
;
3239 struct elf_link_hash_entry
**sym_hashes
;
3240 const Elf_Internal_Rela
*rel
;
3241 const Elf_Internal_Rela
*rel_end
;
3244 struct elf_kvx_link_hash_table
*htab
;
3246 if (bfd_link_relocatable (info
))
3249 BFD_ASSERT (is_kvx_elf (abfd
));
3251 htab
= elf_kvx_hash_table (info
);
3254 symtab_hdr
= &elf_symtab_hdr (abfd
);
3255 sym_hashes
= elf_sym_hashes (abfd
);
3257 rel_end
= relocs
+ sec
->reloc_count
;
3258 for (rel
= relocs
; rel
< rel_end
; rel
++)
3260 struct elf_link_hash_entry
*h
;
3261 unsigned int r_symndx
;
3262 unsigned int r_type
;
3263 bfd_reloc_code_real_type bfd_r_type
;
3264 Elf_Internal_Sym
*isym
;
3266 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
3267 r_type
= ELFNN_R_TYPE (rel
->r_info
);
3269 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
3271 /* xgettext:c-format */
3272 _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd
, r_symndx
);
3276 if (r_symndx
< symtab_hdr
->sh_info
)
3278 /* A local symbol. */
3279 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
3288 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3289 while (h
->root
.type
== bfd_link_hash_indirect
3290 || h
->root
.type
== bfd_link_hash_warning
)
3291 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3294 /* Could be done earlier, if h were already available. */
3295 bfd_r_type
= kvx_tls_transition (abfd
, info
, r_type
, h
, r_symndx
);
3299 /* Create the ifunc sections for static executables. If we
3300 never see an indirect function symbol nor we are building
3301 a static executable, those sections will be empty and
3302 won't appear in output. */
3309 /* It is referenced by a non-shared object. */
3316 case BFD_RELOC_KVX_S43_LO10
:
3317 case BFD_RELOC_KVX_S43_UP27
:
3318 case BFD_RELOC_KVX_S43_EX6
:
3320 case BFD_RELOC_KVX_S37_LO10
:
3321 case BFD_RELOC_KVX_S37_UP27
:
3323 case BFD_RELOC_KVX_S64_LO10
:
3324 case BFD_RELOC_KVX_S64_UP27
:
3325 case BFD_RELOC_KVX_S64_EX27
:
3327 case BFD_RELOC_KVX_32
:
3328 case BFD_RELOC_KVX_64
:
3330 /* We don't need to handle relocs into sections not going into
3331 the "real" output. */
3332 if ((sec
->flags
& SEC_ALLOC
) == 0)
3337 if (!bfd_link_pic (info
))
3340 h
->plt
.refcount
+= 1;
3341 h
->pointer_equality_needed
= 1;
3344 /* No need to do anything if we're not creating a shared
3346 if (! bfd_link_pic (info
))
3350 struct elf_dyn_relocs
*p
;
3351 struct elf_dyn_relocs
**head
;
3353 /* We must copy these reloc types into the output file.
3354 Create a reloc section in dynobj and make room for
3358 if (htab
->root
.dynobj
== NULL
)
3359 htab
->root
.dynobj
= abfd
;
3361 sreloc
= _bfd_elf_make_dynamic_reloc_section
3362 (sec
, htab
->root
.dynobj
, LOG_FILE_ALIGN
, abfd
, /*rela? */ true);
3368 /* If this is a global symbol, we count the number of
3369 relocations we need for this symbol. */
3372 head
= &h
->dyn_relocs
;
3376 /* Track dynamic relocs needed for local syms too.
3377 We really need local syms available to do this
3383 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
3388 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3392 /* Beware of type punned pointers vs strict aliasing
3394 vpp
= &(elf_section_data (s
)->local_dynrel
);
3395 head
= (struct elf_dyn_relocs
**) vpp
;
3399 if (p
== NULL
|| p
->sec
!= sec
)
3401 bfd_size_type amt
= sizeof *p
;
3402 p
= ((struct elf_dyn_relocs
*)
3403 bfd_zalloc (htab
->root
.dynobj
, amt
));
3416 case BFD_RELOC_KVX_S37_GOT_LO10
:
3417 case BFD_RELOC_KVX_S37_GOT_UP27
:
3419 case BFD_RELOC_KVX_S37_GOTOFF_LO10
:
3420 case BFD_RELOC_KVX_S37_GOTOFF_UP27
:
3422 case BFD_RELOC_KVX_S43_GOT_LO10
:
3423 case BFD_RELOC_KVX_S43_GOT_UP27
:
3424 case BFD_RELOC_KVX_S43_GOT_EX6
:
3426 case BFD_RELOC_KVX_S43_GOTOFF_LO10
:
3427 case BFD_RELOC_KVX_S43_GOTOFF_UP27
:
3428 case BFD_RELOC_KVX_S43_GOTOFF_EX6
:
3430 case BFD_RELOC_KVX_S37_TLS_GD_LO10
:
3431 case BFD_RELOC_KVX_S37_TLS_GD_UP27
:
3433 case BFD_RELOC_KVX_S43_TLS_GD_LO10
:
3434 case BFD_RELOC_KVX_S43_TLS_GD_UP27
:
3435 case BFD_RELOC_KVX_S43_TLS_GD_EX6
:
3437 case BFD_RELOC_KVX_S37_TLS_IE_LO10
:
3438 case BFD_RELOC_KVX_S37_TLS_IE_UP27
:
3440 case BFD_RELOC_KVX_S43_TLS_IE_LO10
:
3441 case BFD_RELOC_KVX_S43_TLS_IE_UP27
:
3442 case BFD_RELOC_KVX_S43_TLS_IE_EX6
:
3444 case BFD_RELOC_KVX_S37_TLS_LD_LO10
:
3445 case BFD_RELOC_KVX_S37_TLS_LD_UP27
:
3447 case BFD_RELOC_KVX_S43_TLS_LD_LO10
:
3448 case BFD_RELOC_KVX_S43_TLS_LD_UP27
:
3449 case BFD_RELOC_KVX_S43_TLS_LD_EX6
:
3452 unsigned old_got_type
;
3454 got_type
= kvx_reloc_got_type (bfd_r_type
);
3458 h
->got
.refcount
+= 1;
3459 old_got_type
= elf_kvx_hash_entry (h
)->got_type
;
3463 struct elf_kvx_local_symbol
*locals
;
3465 if (!elfNN_kvx_allocate_local_symbols
3466 (abfd
, symtab_hdr
->sh_info
))
3469 locals
= elf_kvx_locals (abfd
);
3470 BFD_ASSERT (r_symndx
< symtab_hdr
->sh_info
);
3471 locals
[r_symndx
].got_refcount
+= 1;
3472 old_got_type
= locals
[r_symndx
].got_type
;
3475 /* We will already have issued an error message if there
3476 is a TLS/non-TLS mismatch, based on the symbol type.
3477 So just combine any TLS types needed. */
3478 if (old_got_type
!= GOT_UNKNOWN
&& old_got_type
!= GOT_NORMAL
3479 && got_type
!= GOT_NORMAL
)
3480 got_type
|= old_got_type
;
3482 /* If the symbol is accessed by both IE and GD methods, we
3483 are able to relax. Turn off the GD flag, without
3484 messing up with any other kind of TLS types that may be
3486 /* Disabled untested and unused TLS */
3487 /* if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type)) */
3488 /* got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD); */
3490 if (old_got_type
!= got_type
)
3493 elf_kvx_hash_entry (h
)->got_type
= got_type
;
3496 struct elf_kvx_local_symbol
*locals
;
3497 locals
= elf_kvx_locals (abfd
);
3498 BFD_ASSERT (r_symndx
< symtab_hdr
->sh_info
);
3499 locals
[r_symndx
].got_type
= got_type
;
3503 if (htab
->root
.dynobj
== NULL
)
3504 htab
->root
.dynobj
= abfd
;
3505 if (! kvx_elf_create_got_section (htab
->root
.dynobj
, info
))
3510 case BFD_RELOC_KVX_S64_GOTADDR_LO10
:
3511 case BFD_RELOC_KVX_S64_GOTADDR_UP27
:
3512 case BFD_RELOC_KVX_S64_GOTADDR_EX27
:
3514 case BFD_RELOC_KVX_S43_GOTADDR_LO10
:
3515 case BFD_RELOC_KVX_S43_GOTADDR_UP27
:
3516 case BFD_RELOC_KVX_S43_GOTADDR_EX6
:
3518 case BFD_RELOC_KVX_S37_GOTADDR_LO10
:
3519 case BFD_RELOC_KVX_S37_GOTADDR_UP27
:
3521 if (htab
->root
.dynobj
== NULL
)
3522 htab
->root
.dynobj
= abfd
;
3523 if (! kvx_elf_create_got_section (htab
->root
.dynobj
, info
))
3527 case BFD_RELOC_KVX_PCREL27
:
3528 case BFD_RELOC_KVX_PCREL17
:
3529 /* If this is a local symbol then we resolve it
3530 directly without creating a PLT entry. */
3535 if (h
->plt
.refcount
<= 0)
3536 h
->plt
.refcount
= 1;
3538 h
->plt
.refcount
+= 1;
3550 elfNN_kvx_init_file_header (bfd
*abfd
, struct bfd_link_info
*link_info
)
3552 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
3554 if (!_bfd_elf_init_file_header (abfd
, link_info
))
3557 i_ehdrp
= elf_elfheader (abfd
);
3558 i_ehdrp
->e_ident
[EI_ABIVERSION
] = KVX_ELF_ABI_VERSION
;
3562 static enum elf_reloc_type_class
3563 elfNN_kvx_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3564 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3565 const Elf_Internal_Rela
*rela
)
3567 switch ((int) ELFNN_R_TYPE (rela
->r_info
))
3569 case R_KVX_RELATIVE
:
3570 return reloc_class_relative
;
3571 case R_KVX_JMP_SLOT
:
3572 return reloc_class_plt
;
3574 return reloc_class_copy
;
3576 return reloc_class_normal
;
3580 /* A structure used to record a list of sections, independently
3581 of the next and prev fields in the asection structure. */
3582 typedef struct section_list
3585 struct section_list
*next
;
3586 struct section_list
*prev
;
3593 struct bfd_link_info
*info
;
3596 int (*func
) (void *, const char *, Elf_Internal_Sym
*,
3597 asection
*, struct elf_link_hash_entry
*);
3598 } output_arch_syminfo
;
3600 /* Output a single local symbol for a generated stub. */
3603 elfNN_kvx_output_stub_sym (output_arch_syminfo
*osi
, const char *name
,
3604 bfd_vma offset
, bfd_vma size
)
3606 Elf_Internal_Sym sym
;
3608 sym
.st_value
= (osi
->sec
->output_section
->vma
3609 + osi
->sec
->output_offset
+ offset
);
3612 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FUNC
);
3613 sym
.st_shndx
= osi
->sec_shndx
;
3614 return osi
->func (osi
->finfo
, name
, &sym
, osi
->sec
, NULL
) == 1;
3618 kvx_map_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg
)
3620 struct elf_kvx_stub_hash_entry
*stub_entry
;
3624 output_arch_syminfo
*osi
;
3626 /* Massage our args to the form they really have. */
3627 stub_entry
= (struct elf_kvx_stub_hash_entry
*) gen_entry
;
3628 osi
= (output_arch_syminfo
*) in_arg
;
3630 stub_sec
= stub_entry
->stub_sec
;
3632 /* Ensure this stub is attached to the current section being
3634 if (stub_sec
!= osi
->sec
)
3637 addr
= (bfd_vma
) stub_entry
->stub_offset
;
3639 stub_name
= stub_entry
->output_name
;
3641 switch (stub_entry
->stub_type
)
3643 case kvx_stub_long_branch
:
3644 if (!elfNN_kvx_output_stub_sym
3645 (osi
, stub_name
, addr
, sizeof (elfNN_kvx_long_branch_stub
)))
3656 /* Output mapping symbols for linker generated sections. */
3659 elfNN_kvx_output_arch_local_syms (bfd
*output_bfd
,
3660 struct bfd_link_info
*info
,
3662 int (*func
) (void *, const char *,
3665 struct elf_link_hash_entry
3668 output_arch_syminfo osi
;
3669 struct elf_kvx_link_hash_table
*htab
;
3671 htab
= elf_kvx_hash_table (info
);
3677 /* Long calls stubs. */
3678 if (htab
->stub_bfd
&& htab
->stub_bfd
->sections
)
3682 for (stub_sec
= htab
->stub_bfd
->sections
;
3683 stub_sec
!= NULL
; stub_sec
= stub_sec
->next
)
3685 /* Ignore non-stub sections. */
3686 if (!strstr (stub_sec
->name
, STUB_SUFFIX
))
3691 osi
.sec_shndx
= _bfd_elf_section_from_bfd_section
3692 (output_bfd
, osi
.sec
->output_section
);
3694 bfd_hash_traverse (&htab
->stub_hash_table
, kvx_map_one_stub
,
3699 /* Finally, output mapping symbols for the PLT. */
3700 if (!htab
->root
.splt
|| htab
->root
.splt
->size
== 0)
3703 osi
.sec_shndx
= _bfd_elf_section_from_bfd_section
3704 (output_bfd
, htab
->root
.splt
->output_section
);
3705 osi
.sec
= htab
->root
.splt
;
3711 /* Allocate target specific section data. */
3714 elfNN_kvx_new_section_hook (bfd
*abfd
, asection
*sec
)
3716 if (!sec
->used_by_bfd
)
3718 _kvx_elf_section_data
*sdata
;
3719 bfd_size_type amt
= sizeof (*sdata
);
3721 sdata
= bfd_zalloc (abfd
, amt
);
3724 sec
->used_by_bfd
= sdata
;
3727 return _bfd_elf_new_section_hook (abfd
, sec
);
3730 /* Create dynamic sections. This is different from the ARM backend in that
3731 the got, plt, gotplt and their relocation sections are all created in the
3732 standard part of the bfd elf backend. */
3735 elfNN_kvx_create_dynamic_sections (bfd
*dynobj
,
3736 struct bfd_link_info
*info
)
3738 struct elf_kvx_link_hash_table
*htab
;
3740 /* We need to create .got section. */
3741 if (!kvx_elf_create_got_section (dynobj
, info
))
3744 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
3747 htab
= elf_kvx_hash_table (info
);
3748 htab
->sdynbss
= bfd_get_linker_section (dynobj
, ".dynbss");
3749 if (!bfd_link_pic (info
))
3750 htab
->srelbss
= bfd_get_linker_section (dynobj
, ".rela.bss");
3752 if (!htab
->sdynbss
|| (!bfd_link_pic (info
) && !htab
->srelbss
))
3759 /* Allocate space in .plt, .got and associated reloc sections for
3763 elfNN_kvx_allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
3765 struct bfd_link_info
*info
;
3766 struct elf_kvx_link_hash_table
*htab
;
3767 struct elf_dyn_relocs
*p
;
3769 /* An example of a bfd_link_hash_indirect symbol is versioned
3770 symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
3771 -> __gxx_personality_v0(bfd_link_hash_defined)
3773 There is no need to process bfd_link_hash_indirect symbols here
3774 because we will also be presented with the concrete instance of
3775 the symbol and elfNN_kvx_copy_indirect_symbol () will have been
3776 called to copy all relevant data from the generic to the concrete
3779 if (h
->root
.type
== bfd_link_hash_indirect
)
3782 if (h
->root
.type
== bfd_link_hash_warning
)
3783 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3785 info
= (struct bfd_link_info
*) inf
;
3786 htab
= elf_kvx_hash_table (info
);
3788 if (htab
->root
.dynamic_sections_created
&& h
->plt
.refcount
> 0)
3790 /* Make sure this symbol is output as a dynamic symbol.
3791 Undefined weak syms won't yet be marked as dynamic. */
3792 if (h
->dynindx
== -1 && !h
->forced_local
)
3794 if (!bfd_elf_link_record_dynamic_symbol (info
, h
))
3798 if (bfd_link_pic (info
) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
3800 asection
*s
= htab
->root
.splt
;
3802 /* If this is the first .plt entry, make room for the special
3805 s
->size
+= htab
->plt_header_size
;
3807 h
->plt
.offset
= s
->size
;
3809 /* If this symbol is not defined in a regular file, and we are
3810 not generating a shared library, then set the symbol to this
3811 location in the .plt. This is required to make function
3812 pointers compare as equal between the normal executable and
3813 the shared library. */
3814 if (!bfd_link_pic (info
) && !h
->def_regular
)
3816 h
->root
.u
.def
.section
= s
;
3817 h
->root
.u
.def
.value
= h
->plt
.offset
;
3820 /* Make room for this entry. For now we only create the
3821 small model PLT entries. We later need to find a way
3822 of relaxing into these from the large model PLT entries. */
3823 s
->size
+= PLT_SMALL_ENTRY_SIZE
;
3825 /* We also need to make an entry in the .got.plt section, which
3826 will be placed in the .got section by the linker script. */
3827 htab
->root
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
3829 /* We also need to make an entry in the .rela.plt section. */
3830 htab
->root
.srelplt
->size
+= RELOC_SIZE (htab
);
3832 /* We need to ensure that all GOT entries that serve the PLT
3833 are consecutive with the special GOT slots [0] [1] and
3834 [2]. Any addtional relocations must be placed after the
3835 PLT related entries. We abuse the reloc_count such that
3836 during sizing we adjust reloc_count to indicate the
3837 number of PLT related reserved entries. In subsequent
3838 phases when filling in the contents of the reloc entries,
3839 PLT related entries are placed by computing their PLT
3840 index (0 .. reloc_count). While other none PLT relocs are
3841 placed at the slot indicated by reloc_count and
3842 reloc_count is updated. */
3844 htab
->root
.srelplt
->reloc_count
++;
3848 h
->plt
.offset
= (bfd_vma
) - 1;
3854 h
->plt
.offset
= (bfd_vma
) - 1;
3858 if (h
->got
.refcount
> 0)
3861 unsigned got_type
= elf_kvx_hash_entry (h
)->got_type
;
3863 h
->got
.offset
= (bfd_vma
) - 1;
3865 dyn
= htab
->root
.dynamic_sections_created
;
3867 /* Make sure this symbol is output as a dynamic symbol.
3868 Undefined weak syms won't yet be marked as dynamic. */
3869 if (dyn
&& h
->dynindx
== -1 && !h
->forced_local
)
3871 if (!bfd_elf_link_record_dynamic_symbol (info
, h
))
3875 if (got_type
== GOT_UNKNOWN
)
3877 (*_bfd_error_handler
)
3878 (_("relocation against `%s' has faulty GOT type "),
3879 (h
) ? h
->root
.root
.string
: "a local symbol");
3880 bfd_set_error (bfd_error_bad_value
);
3883 else if (got_type
== GOT_NORMAL
)
3885 h
->got
.offset
= htab
->root
.sgot
->size
;
3886 htab
->root
.sgot
->size
+= GOT_ENTRY_SIZE
;
3887 if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3888 || h
->root
.type
!= bfd_link_hash_undefweak
)
3889 && (bfd_link_pic (info
)
3890 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
3892 htab
->root
.srelgot
->size
+= RELOC_SIZE (htab
);
3899 /* Any of these will require 2 GOT slots because
3900 * they use __tls_get_addr() */
3901 if (got_type
& (GOT_TLS_GD
| GOT_TLS_LD
))
3903 h
->got
.offset
= htab
->root
.sgot
->size
;
3904 htab
->root
.sgot
->size
+= GOT_ENTRY_SIZE
* 2;
3907 if (got_type
& GOT_TLS_IE
)
3909 h
->got
.offset
= htab
->root
.sgot
->size
;
3910 htab
->root
.sgot
->size
+= GOT_ENTRY_SIZE
;
3913 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
3914 if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3915 || h
->root
.type
!= bfd_link_hash_undefweak
)
3916 && (bfd_link_pic (info
)
3918 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
3920 /* Only the GD case requires 2 relocations. */
3921 if (got_type
& GOT_TLS_GD
)
3922 htab
->root
.srelgot
->size
+= RELOC_SIZE (htab
) * 2;
3924 /* LD needs a DTPMOD reloc, IE needs a DTPOFF. */
3925 if (got_type
& (GOT_TLS_LD
| GOT_TLS_IE
))
3926 htab
->root
.srelgot
->size
+= RELOC_SIZE (htab
);
3932 h
->got
.offset
= (bfd_vma
) - 1;
3935 if (h
->dyn_relocs
== NULL
)
3938 /* In the shared -Bsymbolic case, discard space allocated for
3939 dynamic pc-relative relocs against symbols which turn out to be
3940 defined in regular objects. For the normal shared case, discard
3941 space for pc-relative relocs that have become local due to symbol
3942 visibility changes. */
3944 if (bfd_link_pic (info
))
3946 /* Relocs that use pc_count are those that appear on a call
3947 insn, or certain REL relocs that can generated via assembly.
3948 We want calls to protected symbols to resolve directly to the
3949 function rather than going via the plt. If people want
3950 function pointer comparisons to work as expected then they
3951 should avoid writing weird assembly. */
3952 if (SYMBOL_CALLS_LOCAL (info
, h
))
3954 struct elf_dyn_relocs
**pp
;
3956 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
;)
3958 p
->count
-= p
->pc_count
;
3967 /* Also discard relocs on undefined weak syms with non-default
3969 if (h
->dyn_relocs
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
)
3971 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3972 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
3973 h
->dyn_relocs
= NULL
;
3975 /* Make sure undefined weak symbols are output as a dynamic
3977 else if (h
->dynindx
== -1
3979 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
3984 else if (ELIMINATE_COPY_RELOCS
)
3986 /* For the non-shared case, discard space for relocs against
3987 symbols which turn out to need copy relocs or are not
3993 || (htab
->root
.dynamic_sections_created
3994 && (h
->root
.type
== bfd_link_hash_undefweak
3995 || h
->root
.type
== bfd_link_hash_undefined
))))
3997 /* Make sure this symbol is output as a dynamic symbol.
3998 Undefined weak syms won't yet be marked as dynamic. */
3999 if (h
->dynindx
== -1
4001 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
4004 /* If that succeeded, we know we'll be keeping all the
4006 if (h
->dynindx
!= -1)
4010 h
->dyn_relocs
= NULL
;
4015 /* Finally, allocate space. */
4016 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
4020 sreloc
= elf_section_data (p
->sec
)->sreloc
;
4022 BFD_ASSERT (sreloc
!= NULL
);
4024 sreloc
->size
+= p
->count
* RELOC_SIZE (htab
);
4030 /* Find any dynamic relocs that apply to read-only sections. */
4033 kvx_readonly_dynrelocs (struct elf_link_hash_entry
* h
, void * inf
)
4035 struct elf_dyn_relocs
* p
;
4037 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
4039 asection
*s
= p
->sec
;
4041 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
4043 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
4045 info
->flags
|= DF_TEXTREL
;
4046 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' in "
4047 "read-only section `%pA'\n"),
4048 s
->owner
, h
->root
.root
.string
, s
);
4050 /* Not an error, just cut short the traversal. */
4057 /* This is the most important function of all . Innocuosly named
4060 elfNN_kvx_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
4061 struct bfd_link_info
*info
)
4063 struct elf_kvx_link_hash_table
*htab
;
4069 htab
= elf_kvx_hash_table ((info
));
4070 dynobj
= htab
->root
.dynobj
;
4072 BFD_ASSERT (dynobj
!= NULL
);
4074 if (htab
->root
.dynamic_sections_created
)
4076 if (bfd_link_executable (info
) && !info
->nointerp
)
4078 s
= bfd_get_linker_section (dynobj
, ".interp");
4081 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4082 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4086 /* Set up .got offsets for local syms, and space for local dynamic
4088 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
4090 struct elf_kvx_local_symbol
*locals
= NULL
;
4091 Elf_Internal_Shdr
*symtab_hdr
;
4095 if (!is_kvx_elf (ibfd
))
4098 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
4100 struct elf_dyn_relocs
*p
;
4102 for (p
= (struct elf_dyn_relocs
*)
4103 (elf_section_data (s
)->local_dynrel
); p
!= NULL
; p
= p
->next
)
4105 if (!bfd_is_abs_section (p
->sec
)
4106 && bfd_is_abs_section (p
->sec
->output_section
))
4108 /* Input section has been discarded, either because
4109 it is a copy of a linkonce section or due to
4110 linker script /DISCARD/, so we'll be discarding
4113 else if (p
->count
!= 0)
4115 srel
= elf_section_data (p
->sec
)->sreloc
;
4116 srel
->size
+= p
->count
* RELOC_SIZE (htab
);
4117 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
4118 info
->flags
|= DF_TEXTREL
;
4123 locals
= elf_kvx_locals (ibfd
);
4127 symtab_hdr
= &elf_symtab_hdr (ibfd
);
4128 srel
= htab
->root
.srelgot
;
4129 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
4131 locals
[i
].got_offset
= (bfd_vma
) - 1;
4132 if (locals
[i
].got_refcount
> 0)
4134 unsigned got_type
= locals
[i
].got_type
;
4135 if (got_type
& (GOT_TLS_GD
| GOT_TLS_LD
))
4137 locals
[i
].got_offset
= htab
->root
.sgot
->size
;
4138 htab
->root
.sgot
->size
+= GOT_ENTRY_SIZE
* 2;
4141 if (got_type
& (GOT_NORMAL
| GOT_TLS_IE
))
4143 locals
[i
].got_offset
= htab
->root
.sgot
->size
;
4144 htab
->root
.sgot
->size
+= GOT_ENTRY_SIZE
;
4147 if (got_type
== GOT_UNKNOWN
)
4151 if (bfd_link_pic (info
))
4153 if (got_type
& GOT_TLS_GD
)
4154 htab
->root
.srelgot
->size
+= RELOC_SIZE (htab
) * 2;
4156 if (got_type
& GOT_TLS_IE
4157 || got_type
& GOT_TLS_LD
4158 || got_type
& GOT_NORMAL
)
4159 htab
->root
.srelgot
->size
+= RELOC_SIZE (htab
);
4164 locals
[i
].got_refcount
= (bfd_vma
) - 1;
4170 /* Allocate global sym .plt and .got entries, and space for global
4171 sym dynamic relocs. */
4172 elf_link_hash_traverse (&htab
->root
, elfNN_kvx_allocate_dynrelocs
,
4175 /* For every jump slot reserved in the sgotplt, reloc_count is
4176 incremented. However, when we reserve space for TLS descriptors,
4177 it's not incremented, so in order to compute the space reserved
4178 for them, it suffices to multiply the reloc count by the jump
4181 if (htab
->root
.srelplt
)
4182 htab
->sgotplt_jump_table_size
= kvx_compute_jump_table_size (htab
);
4184 /* We now have determined the sizes of the various dynamic sections.
4185 Allocate memory for them. */
4187 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4189 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
4192 if (s
== htab
->root
.splt
4193 || s
== htab
->root
.sgot
4194 || s
== htab
->root
.sgotplt
4195 || s
== htab
->root
.iplt
4196 || s
== htab
->root
.igotplt
|| s
== htab
->sdynbss
)
4198 /* Strip this section if we don't need it; see the
4201 else if (startswith (bfd_section_name (s
), ".rela"))
4203 if (s
->size
!= 0 && s
!= htab
->root
.srelplt
)
4206 /* We use the reloc_count field as a counter if we need
4207 to copy relocs into the output file. */
4208 if (s
!= htab
->root
.srelplt
)
4213 /* It's not one of our sections, so don't allocate space. */
4219 /* If we don't need this section, strip it from the
4220 output file. This is mostly to handle .rela.bss and
4221 .rela.plt. We must create both sections in
4222 create_dynamic_sections, because they must be created
4223 before the linker maps input sections to output
4224 sections. The linker does that before
4225 adjust_dynamic_symbol is called, and it is that
4226 function which decides whether anything needs to go
4227 into these sections. */
4229 s
->flags
|= SEC_EXCLUDE
;
4233 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
4236 /* Allocate memory for the section contents. We use bfd_zalloc
4237 here in case unused entries are not reclaimed before the
4238 section's contents are written out. This should not happen,
4239 but this way if it does, we get a R_KVX_NONE reloc instead
4241 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
4242 if (s
->contents
== NULL
)
4246 if (htab
->root
.dynamic_sections_created
)
4248 /* Add some entries to the .dynamic section. We fill in the
4249 values later, in elfNN_kvx_finish_dynamic_sections, but we
4250 must add the entries now so that we get the correct size for
4251 the .dynamic section. The DT_DEBUG entry is filled in by the
4252 dynamic linker and used by the debugger. */
4253 #define add_dynamic_entry(TAG, VAL) \
4254 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4256 if (bfd_link_executable (info
))
4258 if (!add_dynamic_entry (DT_DEBUG
, 0))
4262 if (htab
->root
.splt
->size
!= 0)
4264 if (!add_dynamic_entry (DT_PLTGOT
, 0)
4265 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
4266 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
4267 || !add_dynamic_entry (DT_JMPREL
, 0))
4273 if (!add_dynamic_entry (DT_RELA
, 0)
4274 || !add_dynamic_entry (DT_RELASZ
, 0)
4275 || !add_dynamic_entry (DT_RELAENT
, RELOC_SIZE (htab
)))
4278 /* If any dynamic relocs apply to a read-only section,
4279 then we need a DT_TEXTREL entry. */
4280 if ((info
->flags
& DF_TEXTREL
) == 0)
4281 elf_link_hash_traverse (&htab
->root
, kvx_readonly_dynrelocs
,
4284 if ((info
->flags
& DF_TEXTREL
) != 0)
4286 if (!add_dynamic_entry (DT_TEXTREL
, 0))
4291 #undef add_dynamic_entry
4297 elf_kvx_update_plt_entry (bfd
*output_bfd
,
4298 bfd_reloc_code_real_type r_type
,
4299 bfd_byte
*plt_entry
, bfd_vma value
)
4301 reloc_howto_type
*howto
= elfNN_kvx_howto_from_bfd_reloc (r_type
);
4302 BFD_ASSERT(howto
!= NULL
);
4303 _bfd_kvx_elf_put_addend (output_bfd
, plt_entry
, r_type
, howto
, value
);
4307 elfNN_kvx_create_small_pltn_entry (struct elf_link_hash_entry
*h
,
4308 struct elf_kvx_link_hash_table
4309 *htab
, bfd
*output_bfd
)
4311 bfd_byte
*plt_entry
;
4314 bfd_vma gotplt_entry_address
;
4315 bfd_vma plt_entry_address
;
4316 Elf_Internal_Rela rela
;
4318 asection
*plt
, *gotplt
, *relplt
;
4320 plt
= htab
->root
.splt
;
4321 gotplt
= htab
->root
.sgotplt
;
4322 relplt
= htab
->root
.srelplt
;
4324 /* Get the index in the procedure linkage table which
4325 corresponds to this symbol. This is the index of this symbol
4326 in all the symbols for which we are making plt entries. The
4327 first entry in the procedure linkage table is reserved.
4329 Get the offset into the .got table of the entry that
4330 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
4331 bytes. The first three are reserved for the dynamic linker.
4333 For static executables, we don't reserve anything. */
4335 if (plt
== htab
->root
.splt
)
4337 plt_index
= (h
->plt
.offset
- htab
->plt_header_size
) / htab
->plt_entry_size
;
4338 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
4342 plt_index
= h
->plt
.offset
/ htab
->plt_entry_size
;
4343 got_offset
= plt_index
* GOT_ENTRY_SIZE
;
4346 plt_entry
= plt
->contents
+ h
->plt
.offset
;
4347 plt_entry_address
= plt
->output_section
->vma
4348 + plt
->output_offset
+ h
->plt
.offset
;
4349 gotplt_entry_address
= gotplt
->output_section
->vma
+
4350 gotplt
->output_offset
+ got_offset
;
4352 /* Copy in the boiler-plate for the PLTn entry. */
4353 memcpy (plt_entry
, elfNN_kvx_small_plt_entry
, PLT_SMALL_ENTRY_SIZE
);
4355 /* Patch the loading of the GOT entry, relative to the PLT entry
4359 /* Use 37bits offset for both 32 and 64bits mode */
4360 /* Fill the LO10 of of lw $r9 = 0[$r14] */
4361 elf_kvx_update_plt_entry(output_bfd
, BFD_RELOC_KVX_S37_LO10
,
4363 gotplt_entry_address
- plt_entry_address
);
4365 /* Fill the UP27 of of lw $r9 = 0[$r14] */
4366 elf_kvx_update_plt_entry(output_bfd
, BFD_RELOC_KVX_S37_UP27
,
4368 gotplt_entry_address
- plt_entry_address
);
4370 rela
.r_offset
= gotplt_entry_address
;
4372 /* Fill in the entry in the .rela.plt section. */
4373 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_KVX_JMP_SLOT
);
4376 /* Compute the relocation entry to used based on PLT index and do
4377 not adjust reloc_count. The reloc_count has already been adjusted
4378 to account for this entry. */
4379 loc
= relplt
->contents
+ plt_index
* RELOC_SIZE (htab
);
4380 bfd_elfNN_swap_reloca_out (output_bfd
, &rela
, loc
);
4383 /* Size sections even though they're not dynamic. We use it to setup
4384 _TLS_MODULE_BASE_, if needed. */
4387 elfNN_kvx_always_size_sections (bfd
*output_bfd
,
4388 struct bfd_link_info
*info
)
4392 if (bfd_link_relocatable (info
))
4395 tls_sec
= elf_hash_table (info
)->tls_sec
;
4399 struct elf_link_hash_entry
*tlsbase
;
4401 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
4402 "_TLS_MODULE_BASE_", true, true, false);
4406 struct bfd_link_hash_entry
*h
= NULL
;
4407 const struct elf_backend_data
*bed
=
4408 get_elf_backend_data (output_bfd
);
4410 if (!(_bfd_generic_link_add_one_symbol
4411 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
4412 tls_sec
, 0, NULL
, false, bed
->collect
, &h
)))
4415 tlsbase
->type
= STT_TLS
;
4416 tlsbase
= (struct elf_link_hash_entry
*) h
;
4417 tlsbase
->def_regular
= 1;
4418 tlsbase
->other
= STV_HIDDEN
;
4419 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, true);
4426 /* Finish up dynamic symbol handling. We set the contents of various
4427 dynamic sections here. */
4429 elfNN_kvx_finish_dynamic_symbol (bfd
*output_bfd
,
4430 struct bfd_link_info
*info
,
4431 struct elf_link_hash_entry
*h
,
4432 Elf_Internal_Sym
*sym
)
4434 struct elf_kvx_link_hash_table
*htab
;
4435 htab
= elf_kvx_hash_table (info
);
4437 if (h
->plt
.offset
!= (bfd_vma
) - 1)
4439 asection
*plt
= NULL
, *gotplt
= NULL
, *relplt
= NULL
;
4441 /* This symbol has an entry in the procedure linkage table. Set
4444 if (htab
->root
.splt
!= NULL
)
4446 plt
= htab
->root
.splt
;
4447 gotplt
= htab
->root
.sgotplt
;
4448 relplt
= htab
->root
.srelplt
;
4451 /* This symbol has an entry in the procedure linkage table. Set
4453 if ((h
->dynindx
== -1
4454 && !((h
->forced_local
|| bfd_link_executable (info
))
4456 && h
->type
== STT_GNU_IFUNC
))
4462 elfNN_kvx_create_small_pltn_entry (h
, htab
, output_bfd
);
4463 if (!h
->def_regular
)
4465 /* Mark the symbol as undefined, rather than as defined in
4466 the .plt section. */
4467 sym
->st_shndx
= SHN_UNDEF
;
4468 /* If the symbol is weak we need to clear the value.
4469 Otherwise, the PLT entry would provide a definition for
4470 the symbol even if the symbol wasn't defined anywhere,
4471 and so the symbol would never be NULL. Leave the value if
4472 there were any relocations where pointer equality matters
4473 (this is a clue for the dynamic linker, to make function
4474 pointer comparisons work between an application and shared
4476 if (!h
->ref_regular_nonweak
|| !h
->pointer_equality_needed
)
4481 if (h
->got
.offset
!= (bfd_vma
) - 1
4482 && elf_kvx_hash_entry (h
)->got_type
== GOT_NORMAL
)
4484 Elf_Internal_Rela rela
;
4487 /* This symbol has an entry in the global offset table. Set it
4489 if (htab
->root
.sgot
== NULL
|| htab
->root
.srelgot
== NULL
)
4492 rela
.r_offset
= (htab
->root
.sgot
->output_section
->vma
4493 + htab
->root
.sgot
->output_offset
4494 + (h
->got
.offset
& ~(bfd_vma
) 1));
4497 printf("setting rela at offset 0x%x(0x%x + 0x%x + 0x%x) for %s\n",
4499 htab
->root
.sgot
->output_section
->vma
,
4500 htab
->root
.sgot
->output_offset
,
4502 h
->root
.root
.string
);
4505 if (bfd_link_pic (info
) && SYMBOL_REFERENCES_LOCAL (info
, h
))
4507 if (!h
->def_regular
)
4510 /* in case of PLT related GOT entry, it is not clear who is
4511 supposed to set the LSB of GOT entry...
4512 kvx_calculate_got_entry_vma() would be a good candidate,
4513 but it is not called currently
4514 So we are commenting it ATM
4516 // BFD_ASSERT ((h->got.offset & 1) != 0);
4517 rela
.r_info
= ELFNN_R_INFO (0, R_KVX_RELATIVE
);
4518 rela
.r_addend
= (h
->root
.u
.def
.value
4519 + h
->root
.u
.def
.section
->output_section
->vma
4520 + h
->root
.u
.def
.section
->output_offset
);
4524 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
4525 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
4526 htab
->root
.sgot
->contents
+ h
->got
.offset
);
4527 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_KVX_GLOB_DAT
);
4531 loc
= htab
->root
.srelgot
->contents
;
4532 loc
+= htab
->root
.srelgot
->reloc_count
++ * RELOC_SIZE (htab
);
4533 bfd_elfNN_swap_reloca_out (output_bfd
, &rela
, loc
);
4538 Elf_Internal_Rela rela
;
4541 /* This symbol needs a copy reloc. Set it up. */
4543 if (h
->dynindx
== -1
4544 || (h
->root
.type
!= bfd_link_hash_defined
4545 && h
->root
.type
!= bfd_link_hash_defweak
)
4546 || htab
->srelbss
== NULL
)
4549 rela
.r_offset
= (h
->root
.u
.def
.value
4550 + h
->root
.u
.def
.section
->output_section
->vma
4551 + h
->root
.u
.def
.section
->output_offset
);
4552 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_KVX_COPY
);
4554 loc
= htab
->srelbss
->contents
;
4555 loc
+= htab
->srelbss
->reloc_count
++ * RELOC_SIZE (htab
);
4556 bfd_elfNN_swap_reloca_out (output_bfd
, &rela
, loc
);
4559 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
4560 be NULL for local symbols. */
4562 && (h
== elf_hash_table (info
)->hdynamic
4563 || h
== elf_hash_table (info
)->hgot
))
4564 sym
->st_shndx
= SHN_ABS
;
4570 elfNN_kvx_init_small_plt0_entry (bfd
*output_bfd ATTRIBUTE_UNUSED
,
4571 struct elf_kvx_link_hash_table
4574 memcpy (htab
->root
.splt
->contents
, elfNN_kvx_small_plt0_entry
,
4576 elf_section_data (htab
->root
.splt
->output_section
)->this_hdr
.sh_entsize
=
4581 elfNN_kvx_finish_dynamic_sections (bfd
*output_bfd
,
4582 struct bfd_link_info
*info
)
4584 struct elf_kvx_link_hash_table
*htab
;
4588 htab
= elf_kvx_hash_table (info
);
4589 dynobj
= htab
->root
.dynobj
;
4590 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
4592 if (htab
->root
.dynamic_sections_created
)
4594 ElfNN_External_Dyn
*dyncon
, *dynconend
;
4596 if (sdyn
== NULL
|| htab
->root
.sgot
== NULL
)
4599 dyncon
= (ElfNN_External_Dyn
*) sdyn
->contents
;
4600 dynconend
= (ElfNN_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
4601 for (; dyncon
< dynconend
; dyncon
++)
4603 Elf_Internal_Dyn dyn
;
4606 bfd_elfNN_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4614 s
= htab
->root
.sgotplt
;
4615 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
4619 dyn
.d_un
.d_ptr
= htab
->root
.srelplt
->output_section
->vma
;
4623 s
= htab
->root
.srelplt
;
4624 dyn
.d_un
.d_val
= s
->size
;
4628 /* The procedure linkage table relocs (DT_JMPREL) should
4629 not be included in the overall relocs (DT_RELA).
4630 Therefore, we override the DT_RELASZ entry here to
4631 make it not include the JMPREL relocs. Since the
4632 linker script arranges for .rela.plt to follow all
4633 other relocation sections, we don't have to worry
4634 about changing the DT_RELA entry. */
4635 if (htab
->root
.srelplt
!= NULL
)
4637 s
= htab
->root
.srelplt
;
4638 dyn
.d_un
.d_val
-= s
->size
;
4643 bfd_elfNN_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4648 /* Fill in the special first entry in the procedure linkage table. */
4649 if (htab
->root
.splt
&& htab
->root
.splt
->size
> 0)
4651 elfNN_kvx_init_small_plt0_entry (output_bfd
, htab
);
4653 elf_section_data (htab
->root
.splt
->output_section
)->
4654 this_hdr
.sh_entsize
= htab
->plt_entry_size
;
4657 if (htab
->root
.sgotplt
)
4659 if (bfd_is_abs_section (htab
->root
.sgotplt
->output_section
))
4661 (*_bfd_error_handler
)
4662 (_("discarded output section: `%pA'"), htab
->root
.sgotplt
);
4666 /* Fill in the first three entries in the global offset table. */
4667 if (htab
->root
.sgotplt
->size
> 0)
4669 bfd_put_NN (output_bfd
, (bfd_vma
) 0, htab
->root
.sgotplt
->contents
);
4671 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4672 bfd_put_NN (output_bfd
,
4674 htab
->root
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
4675 bfd_put_NN (output_bfd
,
4677 htab
->root
.sgotplt
->contents
+ GOT_ENTRY_SIZE
* 2);
4680 if (htab
->root
.sgot
)
4682 if (htab
->root
.sgot
->size
> 0)
4685 sdyn
? sdyn
->output_section
->vma
+ sdyn
->output_offset
: 0;
4686 bfd_put_NN (output_bfd
, addr
, htab
->root
.sgot
->contents
);
4690 elf_section_data (htab
->root
.sgotplt
->output_section
)->
4691 this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
4694 if (htab
->root
.sgot
&& htab
->root
.sgot
->size
> 0)
4695 elf_section_data (htab
->root
.sgot
->output_section
)->this_hdr
.sh_entsize
4701 /* Return address for Ith PLT stub in section PLT, for relocation REL
4702 or (bfd_vma) -1 if it should not be included. */
4705 elfNN_kvx_plt_sym_val (bfd_vma i
, const asection
*plt
,
4706 const arelent
*rel ATTRIBUTE_UNUSED
)
4708 return plt
->vma
+ PLT_ENTRY_SIZE
+ i
* PLT_SMALL_ENTRY_SIZE
;
4711 #define ELF_ARCH bfd_arch_kvx
4712 #define ELF_MACHINE_CODE EM_KVX
4713 #define ELF_MAXPAGESIZE 0x10000
4714 #define ELF_MINPAGESIZE 0x1000
4715 #define ELF_COMMONPAGESIZE 0x1000
4717 #define bfd_elfNN_bfd_link_hash_table_create \
4718 elfNN_kvx_link_hash_table_create
4720 #define bfd_elfNN_bfd_merge_private_bfd_data \
4721 elfNN_kvx_merge_private_bfd_data
4723 #define bfd_elfNN_bfd_print_private_bfd_data \
4724 elfNN_kvx_print_private_bfd_data
4726 #define bfd_elfNN_bfd_reloc_type_lookup \
4727 elfNN_kvx_reloc_type_lookup
4729 #define bfd_elfNN_bfd_reloc_name_lookup \
4730 elfNN_kvx_reloc_name_lookup
4732 #define bfd_elfNN_bfd_set_private_flags \
4733 elfNN_kvx_set_private_flags
4735 #define bfd_elfNN_mkobject \
4738 #define bfd_elfNN_new_section_hook \
4739 elfNN_kvx_new_section_hook
4741 #define elf_backend_adjust_dynamic_symbol \
4742 elfNN_kvx_adjust_dynamic_symbol
4744 #define elf_backend_always_size_sections \
4745 elfNN_kvx_always_size_sections
4747 #define elf_backend_check_relocs \
4748 elfNN_kvx_check_relocs
4750 #define elf_backend_copy_indirect_symbol \
4751 elfNN_kvx_copy_indirect_symbol
4753 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
4754 to them in our hash. */
4755 #define elf_backend_create_dynamic_sections \
4756 elfNN_kvx_create_dynamic_sections
4758 #define elf_backend_init_index_section \
4759 _bfd_elf_init_2_index_sections
4761 #define elf_backend_finish_dynamic_sections \
4762 elfNN_kvx_finish_dynamic_sections
4764 #define elf_backend_finish_dynamic_symbol \
4765 elfNN_kvx_finish_dynamic_symbol
4767 #define elf_backend_object_p \
4770 #define elf_backend_output_arch_local_syms \
4771 elfNN_kvx_output_arch_local_syms
4773 #define elf_backend_plt_sym_val \
4774 elfNN_kvx_plt_sym_val
4776 #define elf_backend_init_file_header \
4777 elfNN_kvx_init_file_header
4779 #define elf_backend_init_process_headers \
4780 elfNN_kvx_init_process_headers
4782 #define elf_backend_relocate_section \
4783 elfNN_kvx_relocate_section
4785 #define elf_backend_reloc_type_class \
4786 elfNN_kvx_reloc_type_class
4788 #define elf_backend_size_dynamic_sections \
4789 elfNN_kvx_size_dynamic_sections
4791 #define elf_backend_can_refcount 1
4792 #define elf_backend_can_gc_sections 1
4793 #define elf_backend_plt_readonly 1
4794 #define elf_backend_want_got_plt 1
4795 #define elf_backend_want_plt_sym 0
4796 #define elf_backend_may_use_rel_p 0
4797 #define elf_backend_may_use_rela_p 1
4798 #define elf_backend_default_use_rela_p 1
4799 #define elf_backend_rela_normal 1
4800 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
4801 #define elf_backend_default_execstack 0
4802 #define elf_backend_extern_protected_data 1
4803 #define elf_backend_hash_symbol elf_kvx_hash_symbol
4805 #include "elfNN-target.h"