1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "bfd_stdint.h"
29 #include "libiberty.h"
30 #include "bfd_stdint.h"
31 #include "elf/nds32.h"
32 #include "opcode/nds32.h"
33 #include "elf32-nds32.h"
34 #include "opcode/cgen.h"
35 #include "../opcodes/nds32-opc.h"
37 /* Relocation HOWTO functions. */
38 static bfd_reloc_status_type nds32_elf_ignore_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type nds32_elf_hi20_reloc
43 (bfd *, arelent *, asymbol *, void *,
44 asection *, bfd *, char **);
45 static bfd_reloc_status_type nds32_elf_lo12_reloc
46 (bfd *, arelent *, asymbol *, void *,
47 asection *, bfd *, char **);
48 static bfd_reloc_status_type nds32_elf_generic_reloc
49 (bfd *, arelent *, asymbol *, void *,
50 asection *, bfd *, char **);
51 static bfd_reloc_status_type nds32_elf_sda15_reloc
52 (bfd *, arelent *, asymbol *, void *,
53 asection *, bfd *, char **);
55 /* Helper functions for HOWTO. */
56 static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
57 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
58 asection *, bfd_vma, bfd_vma);
59 static void nds32_elf_relocate_hi20
60 (bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *, bfd_vma);
61 static reloc_howto_type *bfd_elf32_bfd_reloc_type_table_lookup
62 (enum elf_nds32_reloc_type);
63 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
64 (bfd *, bfd_reloc_code_real_type);
67 static void nds32_info_to_howto_rel
68 (bfd *, arelent *, Elf_Internal_Rela *dst);
69 static void nds32_info_to_howto
70 (bfd *, arelent *, Elf_Internal_Rela *dst);
71 static bfd_boolean nds32_elf_add_symbol_hook
72 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, const char **,
73 flagword *, asection **, bfd_vma *);
74 static bfd_boolean nds32_elf_relocate_section
75 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
76 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
77 static bfd_boolean nds32_elf_object_p (bfd *);
78 static void nds32_elf_final_write_processing (bfd *, bfd_boolean);
79 static bfd_boolean nds32_elf_set_private_flags (bfd *, flagword);
80 static bfd_boolean nds32_elf_merge_private_bfd_data
81 (bfd *, struct bfd_link_info *);
82 static bfd_boolean nds32_elf_print_private_bfd_data (bfd *, void *);
83 static bfd_boolean nds32_elf_check_relocs
84 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
85 static asection *nds32_elf_gc_mark_hook
86 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
87 struct elf_link_hash_entry *, Elf_Internal_Sym *);
88 static bfd_boolean nds32_elf_adjust_dynamic_symbol
89 (struct bfd_link_info *, struct elf_link_hash_entry *);
90 static bfd_boolean nds32_elf_size_dynamic_sections
91 (bfd *, struct bfd_link_info *);
92 static bfd_boolean nds32_elf_create_dynamic_sections
93 (bfd *, struct bfd_link_info *);
94 static bfd_boolean nds32_elf_finish_dynamic_sections
95 (bfd *, struct bfd_link_info *info);
96 static bfd_boolean nds32_elf_finish_dynamic_symbol
97 (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
99 static bfd_boolean nds32_elf_mkobject (bfd *);
101 /* Nds32 helper functions. */
102 static bfd_reloc_status_type nds32_elf_final_sda_base
103 (bfd *, struct bfd_link_info *, bfd_vma *, bfd_boolean);
104 static bfd_boolean allocate_dynrelocs (struct elf_link_hash_entry *, void *);
105 static bfd_boolean readonly_dynrelocs (struct elf_link_hash_entry *, void *);
106 static Elf_Internal_Rela *find_relocs_at_address
107 (Elf_Internal_Rela *, Elf_Internal_Rela *,
108 Elf_Internal_Rela *, enum elf_nds32_reloc_type);
109 static bfd_vma calculate_memory_address
110 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
111 static int nds32_get_section_contents (bfd *, asection *,
112 bfd_byte **, bfd_boolean);
113 static bfd_boolean nds32_elf_ex9_build_hash_table
114 (bfd *, asection *, struct bfd_link_info *);
115 static bfd_boolean nds32_elf_ex9_itb_base (struct bfd_link_info *);
116 static void nds32_elf_ex9_import_table (struct bfd_link_info *);
117 static void nds32_elf_ex9_finish (struct bfd_link_info *);
118 static void nds32_elf_ex9_reloc_jmp (struct bfd_link_info *);
119 static void nds32_elf_get_insn_with_reg
120 (Elf_Internal_Rela *, uint32_t, uint32_t *);
121 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
122 Elf_Internal_Sym **);
123 static bfd_boolean nds32_elf_ex9_replace_instruction
124 (struct bfd_link_info *, bfd *, asection *);
125 static bfd_boolean nds32_elf_ifc_calc (struct bfd_link_info *, bfd *,
127 static bfd_boolean nds32_elf_ifc_finish (struct bfd_link_info *);
128 static bfd_boolean nds32_elf_ifc_replace (struct bfd_link_info *);
129 static bfd_boolean nds32_elf_ifc_reloc (void);
130 static bfd_boolean nds32_relax_fp_as_gp
131 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
132 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
133 Elf_Internal_Sym *isymbuf);
134 static bfd_boolean nds32_fag_remove_unused_fpbase
135 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
136 Elf_Internal_Rela *irelend);
138 nds32_elf_get_relocated_section_contents (bfd *abfd,
139 struct bfd_link_info *link_info,
140 struct bfd_link_order *link_order,
142 bfd_boolean relocatable,
147 MACH_V1 = bfd_mach_n1h,
148 MACH_V2 = bfd_mach_n1h_v2,
149 MACH_V3 = bfd_mach_n1h_v3,
150 MACH_V3M = bfd_mach_n1h_v3m
153 #define MIN(a, b) ((a) > (b) ? (b) : (a))
154 #define MAX(a, b) ((a) > (b) ? (a) : (b))
156 /* The name of the dynamic interpreter. This is put in the .interp
158 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
160 /* The nop opcode we use. */
161 #define NDS32_NOP32 0x40000009
162 #define NDS32_NOP16 0x9200
164 /* The size in bytes of an entry in the procedure linkage table. */
165 #define PLT_ENTRY_SIZE 24
166 #define PLT_HEADER_SIZE 24
168 /* The first entry in a procedure linkage table are reserved,
169 and the initial contents are unimportant (we zero them out).
170 Subsequent entries look like this. */
171 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
172 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
173 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
174 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
175 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
177 /* $ta is change to $r15 (from $r25). */
178 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
179 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
180 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
181 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
182 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
183 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
185 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
186 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
187 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
188 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
189 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
191 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
192 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
193 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
194 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
195 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
196 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
198 /* These are macros used to get the relocation accurate value. */
199 #define ACCURATE_8BIT_S1 (0x100)
200 #define ACCURATE_U9BIT_S1 (0x400)
201 #define ACCURATE_12BIT_S1 (0x2000)
202 #define ACCURATE_14BIT_S1 (0x4000)
203 #define ACCURATE_19BIT (0x40000)
205 /* These are macros used to get the relocation conservative value. */
206 #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
207 #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
208 #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
209 #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
210 /* These must be more conservative because the address may be in
211 different segment. */
212 #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
213 #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
214 #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
215 #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
216 #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
218 /* Size of small data/bss sections, used to calculate SDA_BASE. */
219 static long got_size = 0;
220 static int is_SDA_BASE_set = 0;
221 static int is_ITB_BASE_set = 0;
223 /* Convert ELF-VER in eflags to string for debugging purpose. */
224 static const char *const nds32_elfver_strtab[] =
231 /* The nds32 linker needs to keep track of the number of relocs that it
232 decides to copy in check_relocs for each symbol. This is so that
233 it can discard PC relative relocs if it doesn't need them when
234 linking with -Bsymbolic. We store the information in a field
235 extending the regular ELF linker hash table. */
237 /* This structure keeps track of the number of PC relative relocs we
238 have copied for a given symbol. */
240 struct elf_nds32_pcrel_relocs_copied
243 struct elf_nds32_pcrel_relocs_copied *next;
244 /* A section in dynobj. */
246 /* Number of relocs copied in this section. */
250 /* The sh linker needs to keep track of the number of relocs that it
251 decides to copy as dynamic relocs in check_relocs for each symbol.
252 This is so that it can later discard them if they are found to be
253 unnecessary. We store the information in a field extending the
254 regular ELF linker hash table. */
256 struct elf_nds32_dyn_relocs
258 struct elf_nds32_dyn_relocs *next;
260 /* The input section of the reloc. */
263 /* Total number of relocs copied for the input section. */
266 /* Number of pc-relative relocs copied for the input section. */
267 bfd_size_type pc_count;
270 /* Nds32 ELF linker hash entry. */
272 struct elf_nds32_link_hash_entry
274 struct elf_link_hash_entry root;
276 /* Track dynamic relocs copied for this symbol. */
277 struct elf_nds32_dyn_relocs *dyn_relocs;
279 /* For checking relocation type. */
280 #define GOT_UNKNOWN 0
283 unsigned int tls_type;
286 /* Get the nds32 ELF linker hash table from a link_info structure. */
288 #define FP_BASE_NAME "_FP_BASE_"
289 static int check_start_export_sym = 0;
290 static size_t ex9_relax_size = 0; /* Save ex9 predicted reducing size. */
292 /* The offset for executable tls relaxation. */
293 #define TP_OFFSET 0x0
295 struct elf_nds32_obj_tdata
297 struct elf_obj_tdata root;
299 /* tls_type for each local got entry. */
300 char *local_got_tls_type;
303 #define elf_nds32_tdata(bfd) \
304 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
306 #define elf32_nds32_local_got_tls_type(bfd) \
307 (elf_nds32_tdata (bfd)->local_got_tls_type)
309 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
312 nds32_elf_mkobject (bfd *abfd)
314 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
318 /* Relocations used for relocation. */
319 static reloc_howto_type nds32_elf_howto_table[] =
321 /* This reloc does nothing. */
322 HOWTO (R_NDS32_NONE, /* type */
324 3, /* size (0 = byte, 1 = short, 2 = long) */
326 FALSE, /* pc_relative */
328 complain_overflow_dont, /* complain_on_overflow */
329 bfd_elf_generic_reloc, /* special_function */
330 "R_NDS32_NONE", /* name */
331 FALSE, /* partial_inplace */
334 FALSE), /* pcrel_offset */
336 /* A 16 bit absolute relocation. */
337 HOWTO (R_NDS32_16, /* type */
339 1, /* size (0 = byte, 1 = short, 2 = long) */
341 FALSE, /* pc_relative */
343 complain_overflow_bitfield, /* complain_on_overflow */
344 nds32_elf_generic_reloc, /* special_function */
345 "R_NDS32_16", /* name */
346 FALSE, /* partial_inplace */
347 0xffff, /* src_mask */
348 0xffff, /* dst_mask */
349 FALSE), /* pcrel_offset */
351 /* A 32 bit absolute relocation. */
352 HOWTO (R_NDS32_32, /* type */
354 2, /* size (0 = byte, 1 = short, 2 = long) */
356 FALSE, /* pc_relative */
358 complain_overflow_bitfield, /* complain_on_overflow */
359 nds32_elf_generic_reloc, /* special_function */
360 "R_NDS32_32", /* name */
361 FALSE, /* partial_inplace */
362 0xffffffff, /* src_mask */
363 0xffffffff, /* dst_mask */
364 FALSE), /* pcrel_offset */
366 /* A 20 bit address. */
367 HOWTO (R_NDS32_20, /* type */
369 2, /* size (0 = byte, 1 = short, 2 = long) */
371 FALSE, /* pc_relative */
373 complain_overflow_unsigned, /* complain_on_overflow */
374 nds32_elf_generic_reloc, /* special_function */
375 "R_NDS32_20", /* name */
376 FALSE, /* partial_inplace */
377 0xfffff, /* src_mask */
378 0xfffff, /* dst_mask */
379 FALSE), /* pcrel_offset */
381 /* An PC Relative 9-bit relocation, shifted by 2.
382 This reloc is complicated because relocations are relative to pc & -4.
383 i.e. branches in the right insn slot use the address of the left insn
385 /* ??? It's not clear whether this should have partial_inplace set or not.
386 Branch relaxing in the assembler can store the addend in the insn,
387 and if bfd_install_relocation gets called the addend may get added
389 HOWTO (R_NDS32_9_PCREL, /* type */
391 1, /* size (0 = byte, 1 = short, 2 = long) */
393 TRUE, /* pc_relative */
395 complain_overflow_signed, /* complain_on_overflow */
396 nds32_elf_9_pcrel_reloc, /* special_function */
397 "R_NDS32_9_PCREL", /* name */
398 FALSE, /* partial_inplace */
401 TRUE), /* pcrel_offset */
403 /* A relative 15 bit relocation, right shifted by 1. */
404 HOWTO (R_NDS32_15_PCREL, /* type */
406 2, /* size (0 = byte, 1 = short, 2 = long) */
408 TRUE, /* pc_relative */
410 complain_overflow_signed, /* complain_on_overflow */
411 bfd_elf_generic_reloc, /* special_function */
412 "R_NDS32_15_PCREL", /* name */
413 FALSE, /* partial_inplace */
414 0x3fff, /* src_mask */
415 0x3fff, /* dst_mask */
416 TRUE), /* pcrel_offset */
418 /* A relative 17 bit relocation, right shifted by 1. */
419 HOWTO (R_NDS32_17_PCREL, /* type */
421 2, /* size (0 = byte, 1 = short, 2 = long) */
423 TRUE, /* pc_relative */
425 complain_overflow_signed, /* complain_on_overflow */
426 bfd_elf_generic_reloc, /* special_function */
427 "R_NDS32_17_PCREL", /* name */
428 FALSE, /* partial_inplace */
429 0xffff, /* src_mask */
430 0xffff, /* dst_mask */
431 TRUE), /* pcrel_offset */
433 /* A relative 25 bit relocation, right shifted by 1. */
434 /* ??? It's not clear whether this should have partial_inplace set or not.
435 Branch relaxing in the assembler can store the addend in the insn,
436 and if bfd_install_relocation gets called the addend may get added
438 HOWTO (R_NDS32_25_PCREL, /* type */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
442 TRUE, /* pc_relative */
444 complain_overflow_signed, /* complain_on_overflow */
445 bfd_elf_generic_reloc, /* special_function */
446 "R_NDS32_25_PCREL", /* name */
447 FALSE, /* partial_inplace */
448 0xffffff, /* src_mask */
449 0xffffff, /* dst_mask */
450 TRUE), /* pcrel_offset */
452 /* High 20 bits of address when lower 12 is or'd in. */
453 HOWTO (R_NDS32_HI20, /* type */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
457 FALSE, /* pc_relative */
459 complain_overflow_dont,/* complain_on_overflow */
460 nds32_elf_hi20_reloc, /* special_function */
461 "R_NDS32_HI20", /* name */
462 FALSE, /* partial_inplace */
463 0x000fffff, /* src_mask */
464 0x000fffff, /* dst_mask */
465 FALSE), /* pcrel_offset */
467 /* Lower 12 bits of address. */
468 HOWTO (R_NDS32_LO12S3, /* type */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
472 FALSE, /* pc_relative */
474 complain_overflow_dont,/* complain_on_overflow */
475 nds32_elf_lo12_reloc, /* special_function */
476 "R_NDS32_LO12S3", /* name */
477 FALSE, /* partial_inplace */
478 0x000001ff, /* src_mask */
479 0x000001ff, /* dst_mask */
480 FALSE), /* pcrel_offset */
482 /* Lower 12 bits of address. */
483 HOWTO (R_NDS32_LO12S2, /* type */
485 2, /* size (0 = byte, 1 = short, 2 = long) */
487 FALSE, /* pc_relative */
489 complain_overflow_dont,/* complain_on_overflow */
490 nds32_elf_lo12_reloc, /* special_function */
491 "R_NDS32_LO12S2", /* name */
492 FALSE, /* partial_inplace */
493 0x000003ff, /* src_mask */
494 0x000003ff, /* dst_mask */
495 FALSE), /* pcrel_offset */
497 /* Lower 12 bits of address. */
498 HOWTO (R_NDS32_LO12S1, /* type */
500 2, /* size (0 = byte, 1 = short, 2 = long) */
502 FALSE, /* pc_relative */
504 complain_overflow_dont,/* complain_on_overflow */
505 nds32_elf_lo12_reloc, /* special_function */
506 "R_NDS32_LO12S1", /* name */
507 FALSE, /* partial_inplace */
508 0x000007ff, /* src_mask */
509 0x000007ff, /* dst_mask */
510 FALSE), /* pcrel_offset */
512 /* Lower 12 bits of address. */
513 HOWTO (R_NDS32_LO12S0, /* type */
515 2, /* size (0 = byte, 1 = short, 2 = long) */
517 FALSE, /* pc_relative */
519 complain_overflow_dont,/* complain_on_overflow */
520 nds32_elf_lo12_reloc, /* special_function */
521 "R_NDS32_LO12S0", /* name */
522 FALSE, /* partial_inplace */
523 0x00000fff, /* src_mask */
524 0x00000fff, /* dst_mask */
525 FALSE), /* pcrel_offset */
527 /* Small data area 15 bits offset. */
528 HOWTO (R_NDS32_SDA15S3, /* type */
530 2, /* size (0 = byte, 1 = short, 2 = long) */
532 FALSE, /* pc_relative */
534 complain_overflow_signed, /* complain_on_overflow */
535 nds32_elf_sda15_reloc, /* special_function */
536 "R_NDS32_SDA15S3", /* name */
537 FALSE, /* partial_inplace */
538 0x00007fff, /* src_mask */
539 0x00007fff, /* dst_mask */
540 FALSE), /* pcrel_offset */
542 /* Small data area 15 bits offset. */
543 HOWTO (R_NDS32_SDA15S2, /* type */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
547 FALSE, /* pc_relative */
549 complain_overflow_signed, /* complain_on_overflow */
550 nds32_elf_sda15_reloc, /* special_function */
551 "R_NDS32_SDA15S2", /* name */
552 FALSE, /* partial_inplace */
553 0x00007fff, /* src_mask */
554 0x00007fff, /* dst_mask */
555 FALSE), /* pcrel_offset */
557 /* Small data area 15 bits offset. */
558 HOWTO (R_NDS32_SDA15S1, /* type */
560 2, /* size (0 = byte, 1 = short, 2 = long) */
562 FALSE, /* pc_relative */
564 complain_overflow_signed, /* complain_on_overflow */
565 nds32_elf_sda15_reloc, /* special_function */
566 "R_NDS32_SDA15S1", /* name */
567 FALSE, /* partial_inplace */
568 0x00007fff, /* src_mask */
569 0x00007fff, /* dst_mask */
570 FALSE), /* pcrel_offset */
572 /* Small data area 15 bits offset. */
573 HOWTO (R_NDS32_SDA15S0, /* type */
575 2, /* size (0 = byte, 1 = short, 2 = long) */
577 FALSE, /* pc_relative */
579 complain_overflow_signed, /* complain_on_overflow */
580 nds32_elf_sda15_reloc, /* special_function */
581 "R_NDS32_SDA15S0", /* name */
582 FALSE, /* partial_inplace */
583 0x00007fff, /* src_mask */
584 0x00007fff, /* dst_mask */
585 FALSE), /* pcrel_offset */
587 /* GNU extension to record C++ vtable hierarchy */
588 HOWTO (R_NDS32_GNU_VTINHERIT, /* type */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
592 FALSE, /* pc_relative */
594 complain_overflow_dont,/* complain_on_overflow */
595 NULL, /* special_function */
596 "R_NDS32_GNU_VTINHERIT", /* name */
597 FALSE, /* partial_inplace */
600 FALSE), /* pcrel_offset */
602 /* GNU extension to record C++ vtable member usage */
603 HOWTO (R_NDS32_GNU_VTENTRY, /* type */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
607 FALSE, /* pc_relative */
609 complain_overflow_dont,/* complain_on_overflow */
610 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
611 "R_NDS32_GNU_VTENTRY", /* name */
612 FALSE, /* partial_inplace */
615 FALSE), /* pcrel_offset */
617 /* A 16 bit absolute relocation. */
618 HOWTO (R_NDS32_16_RELA, /* type */
620 1, /* size (0 = byte, 1 = short, 2 = long) */
622 FALSE, /* pc_relative */
624 complain_overflow_bitfield, /* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
626 "R_NDS32_16_RELA", /* name */
627 FALSE, /* partial_inplace */
628 0xffff, /* src_mask */
629 0xffff, /* dst_mask */
630 FALSE), /* pcrel_offset */
632 /* A 32 bit absolute relocation. */
633 HOWTO (R_NDS32_32_RELA, /* type */
635 2, /* size (0 = byte, 1 = short, 2 = long) */
637 FALSE, /* pc_relative */
639 complain_overflow_bitfield, /* complain_on_overflow */
640 bfd_elf_generic_reloc, /* special_function */
641 "R_NDS32_32_RELA", /* name */
642 FALSE, /* partial_inplace */
643 0xffffffff, /* src_mask */
644 0xffffffff, /* dst_mask */
645 FALSE), /* pcrel_offset */
647 /* A 20 bit address. */
648 HOWTO (R_NDS32_20_RELA, /* type */
650 2, /* size (0 = byte, 1 = short, 2 = long) */
652 FALSE, /* pc_relative */
654 complain_overflow_signed, /* complain_on_overflow */
655 bfd_elf_generic_reloc, /* special_function */
656 "R_NDS32_20_RELA", /* name */
657 FALSE, /* partial_inplace */
658 0xfffff, /* src_mask */
659 0xfffff, /* dst_mask */
660 FALSE), /* pcrel_offset */
662 HOWTO (R_NDS32_9_PCREL_RELA, /* type */
664 1, /* size (0 = byte, 1 = short, 2 = long) */
666 TRUE, /* pc_relative */
668 complain_overflow_signed, /* complain_on_overflow */
669 bfd_elf_generic_reloc, /* special_function */
670 "R_NDS32_9_PCREL_RELA",/* name */
671 FALSE, /* partial_inplace */
674 TRUE), /* pcrel_offset */
676 /* A relative 15 bit relocation, right shifted by 1. */
677 HOWTO (R_NDS32_15_PCREL_RELA, /* type */
679 2, /* size (0 = byte, 1 = short, 2 = long) */
681 TRUE, /* pc_relative */
683 complain_overflow_signed, /* complain_on_overflow */
684 bfd_elf_generic_reloc, /* special_function */
685 "R_NDS32_15_PCREL_RELA", /* name */
686 FALSE, /* partial_inplace */
687 0x3fff, /* src_mask */
688 0x3fff, /* dst_mask */
689 TRUE), /* pcrel_offset */
691 /* A relative 17 bit relocation, right shifted by 1. */
692 HOWTO (R_NDS32_17_PCREL_RELA, /* type */
694 2, /* size (0 = byte, 1 = short, 2 = long) */
696 TRUE, /* pc_relative */
698 complain_overflow_signed, /* complain_on_overflow */
699 bfd_elf_generic_reloc, /* special_function */
700 "R_NDS32_17_PCREL_RELA", /* name */
701 FALSE, /* partial_inplace */
702 0xffff, /* src_mask */
703 0xffff, /* dst_mask */
704 TRUE), /* pcrel_offset */
706 /* A relative 25 bit relocation, right shifted by 2. */
707 HOWTO (R_NDS32_25_PCREL_RELA, /* type */
709 2, /* size (0 = byte, 1 = short, 2 = long) */
711 TRUE, /* pc_relative */
713 complain_overflow_signed, /* complain_on_overflow */
714 bfd_elf_generic_reloc, /* special_function */
715 "R_NDS32_25_PCREL_RELA", /* name */
716 FALSE, /* partial_inplace */
717 0xffffff, /* src_mask */
718 0xffffff, /* dst_mask */
719 TRUE), /* pcrel_offset */
721 /* High 20 bits of address when lower 16 is or'd in. */
722 HOWTO (R_NDS32_HI20_RELA, /* type */
724 2, /* size (0 = byte, 1 = short, 2 = long) */
726 FALSE, /* pc_relative */
728 complain_overflow_dont,/* complain_on_overflow */
729 bfd_elf_generic_reloc, /* special_function */
730 "R_NDS32_HI20_RELA", /* name */
731 FALSE, /* partial_inplace */
732 0x000fffff, /* src_mask */
733 0x000fffff, /* dst_mask */
734 FALSE), /* pcrel_offset */
736 /* Lower 12 bits of address. */
737 HOWTO (R_NDS32_LO12S3_RELA, /* type */
739 2, /* size (0 = byte, 1 = short, 2 = long) */
741 FALSE, /* pc_relative */
743 complain_overflow_dont,/* complain_on_overflow */
744 bfd_elf_generic_reloc, /* special_function */
745 "R_NDS32_LO12S3_RELA", /* name */
746 FALSE, /* partial_inplace */
747 0x000001ff, /* src_mask */
748 0x000001ff, /* dst_mask */
749 FALSE), /* pcrel_offset */
751 /* Lower 12 bits of address. */
752 HOWTO (R_NDS32_LO12S2_RELA, /* type */
754 2, /* size (0 = byte, 1 = short, 2 = long) */
756 FALSE, /* pc_relative */
758 complain_overflow_dont,/* complain_on_overflow */
759 bfd_elf_generic_reloc, /* special_function */
760 "R_NDS32_LO12S2_RELA", /* name */
761 FALSE, /* partial_inplace */
762 0x000003ff, /* src_mask */
763 0x000003ff, /* dst_mask */
764 FALSE), /* pcrel_offset */
766 /* Lower 12 bits of address. */
767 HOWTO (R_NDS32_LO12S1_RELA, /* type */
769 2, /* size (0 = byte, 1 = short, 2 = long) */
771 FALSE, /* pc_relative */
773 complain_overflow_dont,/* complain_on_overflow */
774 bfd_elf_generic_reloc, /* special_function */
775 "R_NDS32_LO12S1_RELA", /* name */
776 FALSE, /* partial_inplace */
777 0x000007ff, /* src_mask */
778 0x000007ff, /* dst_mask */
779 FALSE), /* pcrel_offset */
781 /* Lower 12 bits of address. */
782 HOWTO (R_NDS32_LO12S0_RELA, /* type */
784 2, /* size (0 = byte, 1 = short, 2 = long) */
786 FALSE, /* pc_relative */
788 complain_overflow_dont,/* complain_on_overflow */
789 bfd_elf_generic_reloc, /* special_function */
790 "R_NDS32_LO12S0_RELA", /* name */
791 FALSE, /* partial_inplace */
792 0x00000fff, /* src_mask */
793 0x00000fff, /* dst_mask */
794 FALSE), /* pcrel_offset */
796 /* Small data area 15 bits offset. */
797 HOWTO (R_NDS32_SDA15S3_RELA, /* type */
799 2, /* size (0 = byte, 1 = short, 2 = long) */
801 FALSE, /* pc_relative */
803 complain_overflow_signed, /* complain_on_overflow */
804 bfd_elf_generic_reloc, /* special_function */
805 "R_NDS32_SDA15S3_RELA",/* name */
806 FALSE, /* partial_inplace */
807 0x00007fff, /* src_mask */
808 0x00007fff, /* dst_mask */
809 FALSE), /* pcrel_offset */
811 /* Small data area 15 bits offset. */
812 HOWTO (R_NDS32_SDA15S2_RELA, /* type */
814 2, /* size (0 = byte, 1 = short, 2 = long) */
816 FALSE, /* pc_relative */
818 complain_overflow_signed, /* complain_on_overflow */
819 bfd_elf_generic_reloc, /* special_function */
820 "R_NDS32_SDA15S2_RELA",/* name */
821 FALSE, /* partial_inplace */
822 0x00007fff, /* src_mask */
823 0x00007fff, /* dst_mask */
824 FALSE), /* pcrel_offset */
826 HOWTO (R_NDS32_SDA15S1_RELA, /* type */
828 2, /* size (0 = byte, 1 = short, 2 = long) */
830 FALSE, /* pc_relative */
832 complain_overflow_signed, /* complain_on_overflow */
833 bfd_elf_generic_reloc, /* special_function */
834 "R_NDS32_SDA15S1_RELA",/* name */
835 FALSE, /* partial_inplace */
836 0x00007fff, /* src_mask */
837 0x00007fff, /* dst_mask */
838 FALSE), /* pcrel_offset */
840 HOWTO (R_NDS32_SDA15S0_RELA, /* type */
842 2, /* size (0 = byte, 1 = short, 2 = long) */
844 FALSE, /* pc_relative */
846 complain_overflow_signed, /* complain_on_overflow */
847 bfd_elf_generic_reloc, /* special_function */
848 "R_NDS32_SDA15S0_RELA",/* name */
849 FALSE, /* partial_inplace */
850 0x00007fff, /* src_mask */
851 0x00007fff, /* dst_mask */
852 FALSE), /* pcrel_offset */
854 /* GNU extension to record C++ vtable hierarchy */
855 HOWTO (R_NDS32_RELA_GNU_VTINHERIT, /* type */
857 2, /* size (0 = byte, 1 = short, 2 = long) */
859 FALSE, /* pc_relative */
861 complain_overflow_dont,/* complain_on_overflow */
862 NULL, /* special_function */
863 "R_NDS32_RELA_GNU_VTINHERIT", /* name */
864 FALSE, /* partial_inplace */
867 FALSE), /* pcrel_offset */
869 /* GNU extension to record C++ vtable member usage */
870 HOWTO (R_NDS32_RELA_GNU_VTENTRY, /* type */
872 2, /* size (0 = byte, 1 = short, 2 = long) */
874 FALSE, /* pc_relative */
876 complain_overflow_dont,/* complain_on_overflow */
877 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
878 "R_NDS32_RELA_GNU_VTENTRY", /* name */
879 FALSE, /* partial_inplace */
882 FALSE), /* pcrel_offset */
884 /* Like R_NDS32_20, but referring to the GOT table entry for
886 HOWTO (R_NDS32_GOT20, /* type */
888 2, /* size (0 = byte, 1 = short, 2 = long) */
890 FALSE, /* pc_relative */
892 complain_overflow_signed, /* complain_on_overflow */
893 bfd_elf_generic_reloc, /* special_function */
894 "R_NDS32_GOT20", /* name */
895 FALSE, /* partial_inplace */
896 0xfffff, /* src_mask */
897 0xfffff, /* dst_mask */
898 FALSE), /* pcrel_offset */
900 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
901 entry for the symbol. */
902 HOWTO (R_NDS32_25_PLTREL, /* type */
904 2, /* size (0 = byte, 1 = short, 2 = long) */
906 TRUE, /* pc_relative */
908 complain_overflow_signed, /* complain_on_overflow */
909 bfd_elf_generic_reloc, /* special_function */
910 "R_NDS32_25_PLTREL", /* name */
911 FALSE, /* partial_inplace */
912 0xffffff, /* src_mask */
913 0xffffff, /* dst_mask */
914 TRUE), /* pcrel_offset */
916 /* This is used only by the dynamic linker. The symbol should exist
917 both in the object being run and in some shared library. The
918 dynamic linker copies the data addressed by the symbol from the
919 shared library into the object, because the object being
920 run has to have the data at some particular address. */
921 HOWTO (R_NDS32_COPY, /* type */
923 2, /* size (0 = byte, 1 = short, 2 = long) */
925 FALSE, /* pc_relative */
927 complain_overflow_bitfield, /* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_NDS32_COPY", /* name */
930 FALSE, /* partial_inplace */
931 0xffffffff, /* src_mask */
932 0xffffffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
935 /* Like R_NDS32_20, but used when setting global offset table
937 HOWTO (R_NDS32_GLOB_DAT, /* type */
939 2, /* size (0 = byte, 1 = short, 2 = long) */
941 FALSE, /* pc_relative */
943 complain_overflow_bitfield, /* complain_on_overflow */
944 bfd_elf_generic_reloc, /* special_function */
945 "R_NDS32_GLOB_DAT", /* name */
946 FALSE, /* partial_inplace */
947 0xffffffff, /* src_mask */
948 0xffffffff, /* dst_mask */
949 FALSE), /* pcrel_offset */
951 /* Marks a procedure linkage table entry for a symbol. */
952 HOWTO (R_NDS32_JMP_SLOT, /* type */
954 2, /* size (0 = byte, 1 = short, 2 = long) */
956 FALSE, /* pc_relative */
958 complain_overflow_bitfield, /* complain_on_overflow */
959 bfd_elf_generic_reloc, /* special_function */
960 "R_NDS32_JMP_SLOT", /* name */
961 FALSE, /* partial_inplace */
962 0xffffffff, /* src_mask */
963 0xffffffff, /* dst_mask */
964 FALSE), /* pcrel_offset */
966 /* Used only by the dynamic linker. When the object is run, this
967 longword is set to the load address of the object, plus the
969 HOWTO (R_NDS32_RELATIVE, /* type */
971 2, /* size (0 = byte, 1 = short, 2 = long) */
973 FALSE, /* pc_relative */
975 complain_overflow_bitfield, /* complain_on_overflow */
976 bfd_elf_generic_reloc, /* special_function */
977 "R_NDS32_RELATIVE", /* name */
978 FALSE, /* partial_inplace */
979 0xffffffff, /* src_mask */
980 0xffffffff, /* dst_mask */
981 FALSE), /* pcrel_offset */
983 HOWTO (R_NDS32_GOTOFF, /* type */
985 2, /* size (0 = byte, 1 = short, 2 = long) */
987 FALSE, /* pc_relative */
989 complain_overflow_signed, /* complain_on_overflow */
990 bfd_elf_generic_reloc, /* special_function */
991 "R_NDS32_GOTOFF", /* name */
992 FALSE, /* partial_inplace */
993 0xfffff, /* src_mask */
994 0xfffff, /* dst_mask */
995 FALSE), /* pcrel_offset */
997 /* An PC Relative 20-bit relocation used when setting PIC offset
999 HOWTO (R_NDS32_GOTPC20, /* type */
1001 2, /* size (0 = byte, 1 = short, 2 = long) */
1003 TRUE, /* pc_relative */
1005 complain_overflow_signed, /* complain_on_overflow */
1006 bfd_elf_generic_reloc, /* special_function */
1007 "R_NDS32_GOTPC20", /* name */
1008 FALSE, /* partial_inplace */
1009 0xfffff, /* src_mask */
1010 0xfffff, /* dst_mask */
1011 TRUE), /* pcrel_offset */
1013 /* Like R_NDS32_HI20, but referring to the GOT table entry for
1015 HOWTO (R_NDS32_GOT_HI20, /* type */
1016 12, /* rightshift */
1017 2, /* size (0 = byte, 1 = short, 2 = long) */
1019 FALSE, /* pc_relative */
1021 complain_overflow_dont,/* complain_on_overflow */
1022 bfd_elf_generic_reloc, /* special_function */
1023 "R_NDS32_GOT_HI20", /* name */
1024 FALSE, /* partial_inplace */
1025 0x000fffff, /* src_mask */
1026 0x000fffff, /* dst_mask */
1027 FALSE), /* pcrel_offset */
1028 HOWTO (R_NDS32_GOT_LO12, /* type */
1030 2, /* size (0 = byte, 1 = short, 2 = long) */
1032 FALSE, /* pc_relative */
1034 complain_overflow_dont,/* complain_on_overflow */
1035 bfd_elf_generic_reloc, /* special_function */
1036 "R_NDS32_GOT_LO12", /* name */
1037 FALSE, /* partial_inplace */
1038 0x00000fff, /* src_mask */
1039 0x00000fff, /* dst_mask */
1040 FALSE), /* pcrel_offset */
1042 /* An PC Relative relocation used when setting PIC offset table register.
1043 Like R_NDS32_HI20, but referring to the GOT table entry for
1045 HOWTO (R_NDS32_GOTPC_HI20, /* type */
1046 12, /* rightshift */
1047 2, /* size (0 = byte, 1 = short, 2 = long) */
1049 FALSE, /* pc_relative */
1051 complain_overflow_dont,/* complain_on_overflow */
1052 bfd_elf_generic_reloc, /* special_function */
1053 "R_NDS32_GOTPC_HI20", /* name */
1054 FALSE, /* partial_inplace */
1055 0x000fffff, /* src_mask */
1056 0x000fffff, /* dst_mask */
1057 TRUE), /* pcrel_offset */
1058 HOWTO (R_NDS32_GOTPC_LO12, /* type */
1060 2, /* size (0 = byte, 1 = short, 2 = long) */
1062 FALSE, /* pc_relative */
1064 complain_overflow_dont, /* complain_on_overflow */
1065 bfd_elf_generic_reloc, /* special_function */
1066 "R_NDS32_GOTPC_LO12", /* name */
1067 FALSE, /* partial_inplace */
1068 0x00000fff, /* src_mask */
1069 0x00000fff, /* dst_mask */
1070 TRUE), /* pcrel_offset */
1072 HOWTO (R_NDS32_GOTOFF_HI20, /* type */
1073 12, /* rightshift */
1074 2, /* size (0 = byte, 1 = short, 2 = long) */
1076 FALSE, /* pc_relative */
1078 complain_overflow_dont,/* complain_on_overflow */
1079 bfd_elf_generic_reloc, /* special_function */
1080 "R_NDS32_GOTOFF_HI20", /* name */
1081 FALSE, /* partial_inplace */
1082 0x000fffff, /* src_mask */
1083 0x000fffff, /* dst_mask */
1084 FALSE), /* pcrel_offset */
1085 HOWTO (R_NDS32_GOTOFF_LO12, /* type */
1087 2, /* size (0 = byte, 1 = short, 2 = long) */
1089 FALSE, /* pc_relative */
1091 complain_overflow_dont,/* complain_on_overflow */
1092 bfd_elf_generic_reloc, /* special_function */
1093 "R_NDS32_GOTOFF_LO12", /* name */
1094 FALSE, /* partial_inplace */
1095 0x00000fff, /* src_mask */
1096 0x00000fff, /* dst_mask */
1097 FALSE), /* pcrel_offset */
1099 /* Alignment hint for relaxable instruction. This is used with
1100 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1101 in order to make next label aligned on word boundary. */
1102 HOWTO (R_NDS32_INSN16, /* type */
1104 2, /* size (0 = byte, 1 = short, 2 = long) */
1106 FALSE, /* pc_relative */
1108 complain_overflow_dont,/* complain_on_overflow */
1109 nds32_elf_ignore_reloc,/* special_function */
1110 "R_NDS32_INSN16", /* name */
1111 FALSE, /* partial_inplace */
1112 0x00000fff, /* src_mask */
1113 0x00000fff, /* dst_mask */
1114 FALSE), /* pcrel_offset */
1116 /* Alignment hint for label. */
1117 HOWTO (R_NDS32_LABEL, /* type */
1119 2, /* size (0 = byte, 1 = short, 2 = long) */
1121 FALSE, /* pc_relative */
1123 complain_overflow_dont,/* complain_on_overflow */
1124 nds32_elf_ignore_reloc,/* special_function */
1125 "R_NDS32_LABEL", /* name */
1126 FALSE, /* partial_inplace */
1127 0xffffffff, /* src_mask */
1128 0xffffffff, /* dst_mask */
1129 FALSE), /* pcrel_offset */
1131 /* Relax hint for unconditional call sequence */
1132 HOWTO (R_NDS32_LONGCALL1, /* type */
1134 2, /* size (0 = byte, 1 = short, 2 = long) */
1136 FALSE, /* pc_relative */
1138 complain_overflow_dont,/* complain_on_overflow */
1139 nds32_elf_ignore_reloc,/* special_function */
1140 "R_NDS32_LONGCALL1", /* name */
1141 FALSE, /* partial_inplace */
1142 0xffffffff, /* src_mask */
1143 0xffffffff, /* dst_mask */
1144 FALSE), /* pcrel_offset */
1146 /* Relax hint for conditional call sequence. */
1147 HOWTO (R_NDS32_LONGCALL2, /* type */
1149 2, /* size (0 = byte, 1 = short, 2 = long) */
1151 FALSE, /* pc_relative */
1153 complain_overflow_dont,/* complain_on_overflow */
1154 nds32_elf_ignore_reloc,/* special_function */
1155 "R_NDS32_LONGCALL2", /* name */
1156 FALSE, /* partial_inplace */
1157 0xffffffff, /* src_mask */
1158 0xffffffff, /* dst_mask */
1159 FALSE), /* pcrel_offset */
1161 /* Relax hint for conditional call sequence. */
1162 HOWTO (R_NDS32_LONGCALL3, /* type */
1164 2, /* size (0 = byte, 1 = short, 2 = long) */
1166 FALSE, /* pc_relative */
1168 complain_overflow_dont,/* complain_on_overflow */
1169 nds32_elf_ignore_reloc,/* special_function */
1170 "R_NDS32_LONGCALL3", /* name */
1171 FALSE, /* partial_inplace */
1172 0xffffffff, /* src_mask */
1173 0xffffffff, /* dst_mask */
1174 FALSE), /* pcrel_offset */
1176 /* Relax hint for unconditional branch sequence. */
1177 HOWTO (R_NDS32_LONGJUMP1, /* type */
1179 2, /* size (0 = byte, 1 = short, 2 = long) */
1181 FALSE, /* pc_relative */
1183 complain_overflow_dont,/* complain_on_overflow */
1184 nds32_elf_ignore_reloc,/* special_function */
1185 "R_NDS32_LONGJUMP1", /* name */
1186 FALSE, /* partial_inplace */
1187 0xffffffff, /* src_mask */
1188 0xffffffff, /* dst_mask */
1189 FALSE), /* pcrel_offset */
1191 /* Relax hint for conditional branch sequence. */
1192 HOWTO (R_NDS32_LONGJUMP2, /* type */
1194 2, /* size (0 = byte, 1 = short, 2 = long) */
1196 FALSE, /* pc_relative */
1198 complain_overflow_dont,/* complain_on_overflow */
1199 nds32_elf_ignore_reloc,/* special_function */
1200 "R_NDS32_LONGJUMP2", /* name */
1201 FALSE, /* partial_inplace */
1202 0xffffffff, /* src_mask */
1203 0xffffffff, /* dst_mask */
1204 FALSE), /* pcrel_offset */
1206 /* Relax hint for conditional branch sequence. */
1207 HOWTO (R_NDS32_LONGJUMP3, /* type */
1209 2, /* size (0 = byte, 1 = short, 2 = long) */
1211 FALSE, /* pc_relative */
1213 complain_overflow_dont,/* complain_on_overflow */
1214 nds32_elf_ignore_reloc,/* special_function */
1215 "R_NDS32_LONGJUMP3", /* name */
1216 FALSE, /* partial_inplace */
1217 0xffffffff, /* src_mask */
1218 0xffffffff, /* dst_mask */
1219 FALSE), /* pcrel_offset */
1221 /* Relax hint for load/store sequence. */
1222 HOWTO (R_NDS32_LOADSTORE, /* type */
1224 2, /* size (0 = byte, 1 = short, 2 = long) */
1226 FALSE, /* pc_relative */
1228 complain_overflow_dont,/* complain_on_overflow */
1229 nds32_elf_ignore_reloc,/* special_function */
1230 "R_NDS32_LOADSTORE", /* name */
1231 FALSE, /* partial_inplace */
1232 0xffffffff, /* src_mask */
1233 0xffffffff, /* dst_mask */
1234 FALSE), /* pcrel_offset */
1236 /* Relax hint for load/store sequence. */
1237 HOWTO (R_NDS32_9_FIXED_RELA, /* type */
1239 1, /* size (0 = byte, 1 = short, 2 = long) */
1241 FALSE, /* pc_relative */
1243 complain_overflow_dont,/* complain_on_overflow */
1244 nds32_elf_ignore_reloc,/* special_function */
1245 "R_NDS32_9_FIXED_RELA",/* name */
1246 FALSE, /* partial_inplace */
1247 0x000000ff, /* src_mask */
1248 0x000000ff, /* dst_mask */
1249 FALSE), /* pcrel_offset */
1251 /* Relax hint for load/store sequence. */
1252 HOWTO (R_NDS32_15_FIXED_RELA, /* type */
1254 2, /* size (0 = byte, 1 = short, 2 = long) */
1256 FALSE, /* pc_relative */
1258 complain_overflow_dont,/* complain_on_overflow */
1259 nds32_elf_ignore_reloc,/* special_function */
1260 "R_NDS32_15_FIXED_RELA", /* name */
1261 FALSE, /* partial_inplace */
1262 0x00003fff, /* src_mask */
1263 0x00003fff, /* dst_mask */
1264 FALSE), /* pcrel_offset */
1266 /* Relax hint for load/store sequence. */
1267 HOWTO (R_NDS32_17_FIXED_RELA, /* type */
1269 2, /* size (0 = byte, 1 = short, 2 = long) */
1271 FALSE, /* pc_relative */
1273 complain_overflow_dont,/* complain_on_overflow */
1274 nds32_elf_ignore_reloc,/* special_function */
1275 "R_NDS32_17_FIXED_RELA", /* name */
1276 FALSE, /* partial_inplace */
1277 0x0000ffff, /* src_mask */
1278 0x0000ffff, /* dst_mask */
1279 FALSE), /* pcrel_offset */
1281 /* Relax hint for load/store sequence. */
1282 HOWTO (R_NDS32_25_FIXED_RELA, /* type */
1284 2, /* size (0 = byte, 1 = short, 2 = long) */
1286 FALSE, /* pc_relative */
1288 complain_overflow_dont,/* complain_on_overflow */
1289 nds32_elf_ignore_reloc,/* special_function */
1290 "R_NDS32_25_FIXED_RELA", /* name */
1291 FALSE, /* partial_inplace */
1292 0x00ffffff, /* src_mask */
1293 0x00ffffff, /* dst_mask */
1294 FALSE), /* pcrel_offset */
1296 /* High 20 bits of PLT symbol offset relative to PC. */
1297 HOWTO (R_NDS32_PLTREL_HI20, /* type */
1298 12, /* rightshift */
1299 2, /* size (0 = byte, 1 = short, 2 = long) */
1301 FALSE, /* pc_relative */
1303 complain_overflow_dont,/* complain_on_overflow */
1304 bfd_elf_generic_reloc, /* special_function */
1305 "R_NDS32_PLTREL_HI20", /* name */
1306 FALSE, /* partial_inplace */
1307 0x000fffff, /* src_mask */
1308 0x000fffff, /* dst_mask */
1309 FALSE), /* pcrel_offset */
1311 /* Low 12 bits of PLT symbol offset relative to PC. */
1312 HOWTO (R_NDS32_PLTREL_LO12, /* type */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1316 FALSE, /* pc_relative */
1318 complain_overflow_dont,/* complain_on_overflow */
1319 bfd_elf_generic_reloc, /* special_function */
1320 "R_NDS32_PLTREL_LO12", /* name */
1321 FALSE, /* partial_inplace */
1322 0x00000fff, /* src_mask */
1323 0x00000fff, /* dst_mask */
1324 FALSE), /* pcrel_offset */
1326 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1327 HOWTO (R_NDS32_PLT_GOTREL_HI20, /* type */
1328 12, /* rightshift */
1329 2, /* size (0 = byte, 1 = short, 2 = long) */
1331 FALSE, /* pc_relative */
1333 complain_overflow_dont,/* complain_on_overflow */
1334 bfd_elf_generic_reloc, /* special_function */
1335 "R_NDS32_PLT_GOTREL_HI20", /* name */
1336 FALSE, /* partial_inplace */
1337 0x000fffff, /* src_mask */
1338 0x000fffff, /* dst_mask */
1339 FALSE), /* pcrel_offset */
1341 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1342 HOWTO (R_NDS32_PLT_GOTREL_LO12, /* type */
1344 2, /* size (0 = byte, 1 = short, 2 = long) */
1346 FALSE, /* pc_relative */
1348 complain_overflow_dont,/* complain_on_overflow */
1349 bfd_elf_generic_reloc, /* special_function */
1350 "R_NDS32_PLT_GOTREL_LO12", /* name */
1351 FALSE, /* partial_inplace */
1352 0x00000fff, /* src_mask */
1353 0x00000fff, /* dst_mask */
1354 FALSE), /* pcrel_offset */
1356 /* Small data area 12 bits offset. */
1357 HOWTO (R_NDS32_SDA12S2_DP_RELA, /* type */
1359 2, /* size (0 = byte, 1 = short, 2 = long) */
1361 FALSE, /* pc_relative */
1363 complain_overflow_signed, /* complain_on_overflow */
1364 bfd_elf_generic_reloc, /* special_function */
1365 "R_NDS32_SDA12S2_DP_RELA", /* name */
1366 FALSE, /* partial_inplace */
1367 0x00000fff, /* src_mask */
1368 0x00000fff, /* dst_mask */
1369 FALSE), /* pcrel_offset */
1371 /* Small data area 12 bits offset. */
1372 HOWTO (R_NDS32_SDA12S2_SP_RELA, /* type */
1374 2, /* size (0 = byte, 1 = short, 2 = long) */
1376 FALSE, /* pc_relative */
1378 complain_overflow_signed, /* complain_on_overflow */
1379 bfd_elf_generic_reloc, /* special_function */
1380 "R_NDS32_SDA12S2_SP_RELA", /* name */
1381 FALSE, /* partial_inplace */
1382 0x00000fff, /* src_mask */
1383 0x00000fff, /* dst_mask */
1384 FALSE), /* pcrel_offset */
1385 /* Lower 12 bits of address. */
1387 HOWTO (R_NDS32_LO12S2_DP_RELA, /* type */
1389 2, /* size (0 = byte, 1 = short, 2 = long) */
1391 FALSE, /* pc_relative */
1393 complain_overflow_dont,/* complain_on_overflow */
1394 bfd_elf_generic_reloc, /* special_function */
1395 "R_NDS32_LO12S2_DP_RELA", /* name */
1396 FALSE, /* partial_inplace */
1397 0x000003ff, /* src_mask */
1398 0x000003ff, /* dst_mask */
1399 FALSE), /* pcrel_offset */
1401 /* Lower 12 bits of address. */
1402 HOWTO (R_NDS32_LO12S2_SP_RELA,/* type */
1404 2, /* size (0 = byte, 1 = short, 2 = long) */
1406 FALSE, /* pc_relative */
1408 complain_overflow_dont,/* complain_on_overflow */
1409 bfd_elf_generic_reloc, /* special_function */
1410 "R_NDS32_LO12S2_SP_RELA", /* name */
1411 FALSE, /* partial_inplace */
1412 0x000003ff, /* src_mask */
1413 0x000003ff, /* dst_mask */
1414 FALSE), /* pcrel_offset */
1415 /* Lower 12 bits of address. Special identity for or case. */
1416 HOWTO (R_NDS32_LO12S0_ORI_RELA, /* type */
1418 2, /* size (0 = byte, 1 = short, 2 = long) */
1420 FALSE, /* pc_relative */
1422 complain_overflow_dont,/* complain_on_overflow */
1423 bfd_elf_generic_reloc, /* special_function */
1424 "R_NDS32_LO12S0_ORI_RELA", /* name */
1425 FALSE, /* partial_inplace */
1426 0x00000fff, /* src_mask */
1427 0x00000fff, /* dst_mask */
1428 FALSE), /* pcrel_offset */
1429 /* Small data area 19 bits offset. */
1430 HOWTO (R_NDS32_SDA16S3_RELA, /* type */
1432 2, /* size (0 = byte, 1 = short, 2 = long) */
1434 FALSE, /* pc_relative */
1436 complain_overflow_signed, /* complain_on_overflow */
1437 bfd_elf_generic_reloc, /* special_function */
1438 "R_NDS32_SDA16S3_RELA",/* name */
1439 FALSE, /* partial_inplace */
1440 0x0000ffff, /* src_mask */
1441 0x0000ffff, /* dst_mask */
1442 FALSE), /* pcrel_offset */
1444 /* Small data area 15 bits offset. */
1445 HOWTO (R_NDS32_SDA17S2_RELA, /* type */
1447 2, /* size (0 = byte, 1 = short, 2 = long) */
1449 FALSE, /* pc_relative */
1451 complain_overflow_signed, /* complain_on_overflow */
1452 bfd_elf_generic_reloc, /* special_function */
1453 "R_NDS32_SDA17S2_RELA",/* name */
1454 FALSE, /* partial_inplace */
1455 0x0001ffff, /* src_mask */
1456 0x0001ffff, /* dst_mask */
1457 FALSE), /* pcrel_offset */
1459 HOWTO (R_NDS32_SDA18S1_RELA, /* type */
1461 2, /* size (0 = byte, 1 = short, 2 = long) */
1463 FALSE, /* pc_relative */
1465 complain_overflow_signed, /* complain_on_overflow */
1466 bfd_elf_generic_reloc, /* special_function */
1467 "R_NDS32_SDA18S1_RELA",/* name */
1468 FALSE, /* partial_inplace */
1469 0x0003ffff, /* src_mask */
1470 0x0003ffff, /* dst_mask */
1471 FALSE), /* pcrel_offset */
1473 HOWTO (R_NDS32_SDA19S0_RELA, /* type */
1475 2, /* size (0 = byte, 1 = short, 2 = long) */
1477 FALSE, /* pc_relative */
1479 complain_overflow_signed, /* complain_on_overflow */
1480 bfd_elf_generic_reloc, /* special_function */
1481 "R_NDS32_SDA19S0_RELA",/* name */
1482 FALSE, /* partial_inplace */
1483 0x0007ffff, /* src_mask */
1484 0x0007ffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1486 HOWTO (R_NDS32_DWARF2_OP1_RELA, /* type */
1488 0, /* size (0 = byte, 1 = short, 2 = long) */
1490 FALSE, /* pc_relative */
1492 complain_overflow_dont,/* complain_on_overflow */
1493 nds32_elf_ignore_reloc,/* special_function */
1494 "R_NDS32_DWARF2_OP1_RELA", /* name */
1495 FALSE, /* partial_inplace */
1496 0xff, /* src_mask */
1497 0xff, /* dst_mask */
1498 FALSE), /* pcrel_offset */
1499 HOWTO (R_NDS32_DWARF2_OP2_RELA, /* type */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1503 FALSE, /* pc_relative */
1505 complain_overflow_dont,/* complain_on_overflow */
1506 nds32_elf_ignore_reloc,/* special_function */
1507 "R_NDS32_DWARF2_OP2_RELA", /* name */
1508 FALSE, /* partial_inplace */
1509 0xffff, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512 HOWTO (R_NDS32_DWARF2_LEB_RELA, /* type */
1514 2, /* size (0 = byte, 1 = short, 2 = long) */
1516 FALSE, /* pc_relative */
1518 complain_overflow_dont,/* complain_on_overflow */
1519 nds32_elf_ignore_reloc,/* special_function */
1520 "R_NDS32_DWARF2_LEB_RELA", /* name */
1521 FALSE, /* partial_inplace */
1522 0xffffffff, /* src_mask */
1523 0xffffffff, /* dst_mask */
1524 FALSE), /* pcrel_offset */
1525 HOWTO (R_NDS32_UPDATE_TA_RELA,/* type */
1527 1, /* size (0 = byte, 1 = short, 2 = long) */
1529 FALSE, /* pc_relative */
1531 complain_overflow_dont,/* complain_on_overflow */
1532 nds32_elf_ignore_reloc,/* special_function */
1533 "R_NDS32_UPDATE_TA_RELA", /* name */
1534 FALSE, /* partial_inplace */
1535 0xffff, /* src_mask */
1536 0xffff, /* dst_mask */
1537 FALSE), /* pcrel_offset */
1538 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1539 entry for the symbol. */
1540 HOWTO (R_NDS32_9_PLTREL, /* type */
1542 1, /* size (0 = byte, 1 = short, 2 = long) */
1544 TRUE, /* pc_relative */
1546 complain_overflow_signed, /* complain_on_overflow */
1547 bfd_elf_generic_reloc, /* special_function */
1548 "R_NDS32_9_PLTREL", /* name */
1549 FALSE, /* partial_inplace */
1550 0xff, /* src_mask */
1551 0xff, /* dst_mask */
1552 TRUE), /* pcrel_offset */
1553 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1554 HOWTO (R_NDS32_PLT_GOTREL_LO20, /* type */
1556 2, /* size (0 = byte, 1 = short, 2 = long) */
1558 FALSE, /* pc_relative */
1560 complain_overflow_dont,/* complain_on_overflow */
1561 bfd_elf_generic_reloc, /* special_function */
1562 "R_NDS32_PLT_GOTREL_LO20", /* name */
1563 FALSE, /* partial_inplace */
1564 0x000fffff, /* src_mask */
1565 0x000fffff, /* dst_mask */
1566 FALSE), /* pcrel_offset */
1567 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1568 HOWTO (R_NDS32_PLT_GOTREL_LO15, /* type */
1570 2, /* size (0 = byte, 1 = short, 2 = long) */
1572 FALSE, /* pc_relative */
1574 complain_overflow_dont,/* complain_on_overflow */
1575 bfd_elf_generic_reloc, /* special_function */
1576 "R_NDS32_PLT_GOTREL_LO15", /* name */
1577 FALSE, /* partial_inplace */
1578 0x00007fff, /* src_mask */
1579 0x00007fff, /* dst_mask */
1580 FALSE), /* pcrel_offset */
1581 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1582 HOWTO (R_NDS32_PLT_GOTREL_LO19, /* type */
1584 2, /* size (0 = byte, 1 = short, 2 = long) */
1586 FALSE, /* pc_relative */
1588 complain_overflow_dont,/* complain_on_overflow */
1589 bfd_elf_generic_reloc, /* special_function */
1590 "R_NDS32_PLT_GOTREL_LO19", /* name */
1591 FALSE, /* partial_inplace */
1592 0x0007ffff, /* src_mask */
1593 0x0007ffff, /* dst_mask */
1594 FALSE), /* pcrel_offset */
1595 HOWTO (R_NDS32_GOT_LO15, /* type */
1597 2, /* size (0 = byte, 1 = short, 2 = long) */
1599 FALSE, /* pc_relative */
1601 complain_overflow_dont,/* complain_on_overflow */
1602 bfd_elf_generic_reloc, /* special_function */
1603 "R_NDS32_GOT_LO15", /* name */
1604 FALSE, /* partial_inplace */
1605 0x00007fff, /* src_mask */
1606 0x00007fff, /* dst_mask */
1607 FALSE), /* pcrel_offset */
1608 HOWTO (R_NDS32_GOT_LO19, /* type */
1610 2, /* size (0 = byte, 1 = short, 2 = long) */
1612 FALSE, /* pc_relative */
1614 complain_overflow_dont,/* complain_on_overflow */
1615 bfd_elf_generic_reloc, /* special_function */
1616 "R_NDS32_GOT_LO19", /* name */
1617 FALSE, /* partial_inplace */
1618 0x0007ffff, /* src_mask */
1619 0x0007ffff, /* dst_mask */
1620 FALSE), /* pcrel_offset */
1621 HOWTO (R_NDS32_GOTOFF_LO15, /* type */
1623 2, /* size (0 = byte, 1 = short, 2 = long) */
1625 FALSE, /* pc_relative */
1627 complain_overflow_dont,/* complain_on_overflow */
1628 bfd_elf_generic_reloc, /* special_function */
1629 "R_NDS32_GOTOFF_LO15", /* name */
1630 FALSE, /* partial_inplace */
1631 0x00007fff, /* src_mask */
1632 0x00007fff, /* dst_mask */
1633 FALSE), /* pcrel_offset */
1634 HOWTO (R_NDS32_GOTOFF_LO19, /* type */
1636 2, /* size (0 = byte, 1 = short, 2 = long) */
1638 FALSE, /* pc_relative */
1640 complain_overflow_dont,/* complain_on_overflow */
1641 bfd_elf_generic_reloc, /* special_function */
1642 "R_NDS32_GOTOFF_LO19", /* name */
1643 FALSE, /* partial_inplace */
1644 0x0007ffff, /* src_mask */
1645 0x0007ffff, /* dst_mask */
1646 FALSE), /* pcrel_offset */
1647 /* GOT 15 bits offset. */
1648 HOWTO (R_NDS32_GOT15S2_RELA, /* type */
1650 2, /* size (0 = byte, 1 = short, 2 = long) */
1652 FALSE, /* pc_relative */
1654 complain_overflow_signed, /* complain_on_overflow */
1655 bfd_elf_generic_reloc, /* special_function */
1656 "R_NDS32_GOT15S2_RELA",/* name */
1657 FALSE, /* partial_inplace */
1658 0x00007fff, /* src_mask */
1659 0x00007fff, /* dst_mask */
1660 FALSE), /* pcrel_offset */
1661 /* GOT 17 bits offset. */
1662 HOWTO (R_NDS32_GOT17S2_RELA, /* type */
1664 2, /* size (0 = byte, 1 = short, 2 = long) */
1666 FALSE, /* pc_relative */
1668 complain_overflow_signed, /* complain_on_overflow */
1669 bfd_elf_generic_reloc, /* special_function */
1670 "R_NDS32_GOT17S2_RELA",/* name */
1671 FALSE, /* partial_inplace */
1672 0x0001ffff, /* src_mask */
1673 0x0001ffff, /* dst_mask */
1674 FALSE), /* pcrel_offset */
1675 /* A 5 bit address. */
1676 HOWTO (R_NDS32_5_RELA, /* type */
1678 1, /* size (0 = byte, 1 = short, 2 = long) */
1680 FALSE, /* pc_relative */
1682 complain_overflow_signed, /* complain_on_overflow */
1683 bfd_elf_generic_reloc, /* special_function */
1684 "R_NDS32_5_RELA", /* name */
1685 FALSE, /* partial_inplace */
1686 0x1f, /* src_mask */
1687 0x1f, /* dst_mask */
1688 FALSE), /* pcrel_offset */
1689 HOWTO (R_NDS32_10_UPCREL_RELA,/* type */
1691 1, /* size (0 = byte, 1 = short, 2 = long) */
1693 TRUE, /* pc_relative */
1695 complain_overflow_unsigned, /* complain_on_overflow */
1696 bfd_elf_generic_reloc, /* special_function */
1697 "R_NDS32_10_UPCREL_RELA", /* name */
1698 FALSE, /* partial_inplace */
1699 0x1ff, /* src_mask */
1700 0x1ff, /* dst_mask */
1701 TRUE), /* pcrel_offset */
1702 HOWTO (R_NDS32_SDA_FP7U2_RELA,/* type */
1704 1, /* size (0 = byte, 1 = short, 2 = long) */
1706 FALSE, /* pc_relative */
1708 complain_overflow_unsigned, /* complain_on_overflow */
1709 bfd_elf_generic_reloc, /* special_function */
1710 "R_NDS32_SDA_FP7U2_RELA", /* name */
1711 FALSE, /* partial_inplace */
1712 0x0000007f, /* src_mask */
1713 0x0000007f, /* dst_mask */
1714 FALSE), /* pcrel_offset */
1715 HOWTO (R_NDS32_WORD_9_PCREL_RELA, /* type */
1717 2, /* size (0 = byte, 1 = short, 2 = long) */
1719 TRUE, /* pc_relative */
1721 complain_overflow_signed, /* complain_on_overflow */
1722 bfd_elf_generic_reloc, /* special_function */
1723 "R_NDS32_WORD_9_PCREL_RELA", /* name */
1724 FALSE, /* partial_inplace */
1725 0xff, /* src_mask */
1726 0xff, /* dst_mask */
1727 TRUE), /* pcrel_offset */
1728 HOWTO (R_NDS32_25_ABS_RELA, /* type */
1730 2, /* size (0 = byte, 1 = short, 2 = long) */
1732 FALSE, /* pc_relative */
1734 complain_overflow_dont,/* complain_on_overflow */
1735 bfd_elf_generic_reloc, /* special_function */
1736 "R_NDS32_25_ABS_RELA", /* name */
1737 FALSE, /* partial_inplace */
1738 0xffffff, /* src_mask */
1739 0xffffff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1742 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1743 HOWTO (R_NDS32_17IFC_PCREL_RELA, /* type */
1745 2, /* size (0 = byte, 1 = short, 2 = long) */
1747 TRUE, /* pc_relative */
1749 complain_overflow_signed, /* complain_on_overflow */
1750 bfd_elf_generic_reloc, /* special_function */
1751 "R_NDS32_17IFC_PCREL_RELA", /* name */
1752 FALSE, /* partial_inplace */
1753 0xffff, /* src_mask */
1754 0xffff, /* dst_mask */
1755 TRUE), /* pcrel_offset */
1757 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1758 HOWTO (R_NDS32_10IFCU_PCREL_RELA, /* type */
1760 1, /* size (0 = byte, 1 = short, 2 = long) */
1762 TRUE, /* pc_relative */
1764 complain_overflow_unsigned, /* complain_on_overflow */
1765 bfd_elf_generic_reloc, /* special_function */
1766 "R_NDS32_10IFCU_PCREL_RELA", /* name */
1767 FALSE, /* partial_inplace */
1768 0x1ff, /* src_mask */
1769 0x1ff, /* dst_mask */
1770 TRUE), /* pcrel_offset */
1772 /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol. */
1773 HOWTO (R_NDS32_TLS_LE_HI20, /* type */
1774 12, /* rightshift */
1775 2, /* size (0 = byte, 1 = short, 2 = long) */
1777 FALSE, /* pc_relative */
1779 complain_overflow_dont, /* complain_on_overflow */
1780 bfd_elf_generic_reloc, /* special_function */
1781 "R_NDS32_TLS_LE_HI20", /* name */
1782 FALSE, /* partial_inplace */
1783 0x000fffff, /* src_mask */
1784 0x000fffff, /* dst_mask */
1785 FALSE), /* pcrel_offset */
1786 HOWTO (R_NDS32_TLS_LE_LO12, /* type */
1788 2, /* size (0 = byte, 1 = short, 2 = long) */
1790 FALSE, /* pc_relative */
1792 complain_overflow_dont, /* complain_on_overflow */
1793 bfd_elf_generic_reloc, /* special_function */
1794 "R_NDS32_TLS_LE_LO12", /* name */
1795 FALSE, /* partial_inplace */
1796 0x00000fff, /* src_mask */
1797 0x00000fff, /* dst_mask */
1798 FALSE), /* pcrel_offset */
1800 /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol. */
1801 HOWTO (R_NDS32_TLS_IE_HI20, /* type */
1802 12, /* rightshift */
1803 2, /* size (0 = byte, 1 = short, 2 = long) */
1805 FALSE, /* pc_relative */
1807 complain_overflow_dont, /* complain_on_overflow */
1808 bfd_elf_generic_reloc, /* special_function */
1809 "R_NDS32_TLS_IE_HI20", /* name */
1810 FALSE, /* partial_inplace */
1811 0x000fffff, /* src_mask */
1812 0x000fffff, /* dst_mask */
1813 FALSE), /* pcrel_offset */
1814 HOWTO (R_NDS32_TLS_IE_LO12S2, /* type */
1816 2, /* size (0 = byte, 1 = short, 2 = long) */
1818 FALSE, /* pc_relative */
1820 complain_overflow_dont, /* complain_on_overflow */
1821 bfd_elf_generic_reloc, /* special_function */
1822 "R_NDS32_TLS_IE_LO12S2", /* name */
1823 FALSE, /* partial_inplace */
1824 0x000003ff, /* src_mask */
1825 0x000003ff, /* dst_mask */
1826 FALSE), /* pcrel_offset */
1827 /* Mark a TLS IE entry in GOT. */
1828 HOWTO (R_NDS32_TLS_TPOFF, /* type */
1830 2, /* size (0 = byte, 1 = short, 2 = long) */
1832 FALSE, /* pc_relative */
1834 complain_overflow_bitfield, /* complain_on_overflow */
1835 bfd_elf_generic_reloc, /* special_function */
1836 "R_NDS32_TLS_TPOFF", /* name */
1837 FALSE, /* partial_inplace */
1838 0xffffffff, /* src_mask */
1839 0xffffffff, /* dst_mask */
1840 FALSE), /* pcrel_offset */
1841 /* A 20 bit address. */
1842 HOWTO (R_NDS32_TLS_LE_20, /* type */
1844 2, /* size (0 = byte, 1 = short, 2 = long) */
1846 FALSE, /* pc_relative */
1848 complain_overflow_signed, /* complain_on_overflow */
1849 bfd_elf_generic_reloc, /* special_function */
1850 "R_NDS32_TLS_LE_20", /* name */
1851 FALSE, /* partial_inplace */
1852 0xfffff, /* src_mask */
1853 0xfffff, /* dst_mask */
1854 FALSE), /* pcrel_offset */
1855 HOWTO (R_NDS32_TLS_LE_15S0, /* type */
1857 2, /* size (0 = byte, 1 = short, 2 = long) */
1859 FALSE, /* pc_relative */
1861 complain_overflow_signed, /* complain_on_overflow */
1862 bfd_elf_generic_reloc, /* special_function */
1863 "R_NDS32_TLS_LE_15S0", /* name */
1864 FALSE, /* partial_inplace */
1865 0x7fff, /* src_mask */
1866 0x7fff, /* dst_mask */
1867 FALSE), /* pcrel_offset */
1868 HOWTO (R_NDS32_TLS_LE_15S1, /* type */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1872 FALSE, /* pc_relative */
1874 complain_overflow_signed, /* complain_on_overflow */
1875 bfd_elf_generic_reloc, /* special_function */
1876 "R_NDS32_TLS_LE_15S1", /* name */
1877 FALSE, /* partial_inplace */
1878 0x7fff, /* src_mask */
1879 0x7fff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1881 HOWTO (R_NDS32_TLS_LE_15S2, /* type */
1883 2, /* size (0 = byte, 1 = short, 2 = long) */
1885 FALSE, /* pc_relative */
1887 complain_overflow_signed, /* complain_on_overflow */
1888 bfd_elf_generic_reloc, /* special_function */
1889 "R_NDS32_TLS_LE_15S2", /* name */
1890 FALSE, /* partial_inplace */
1891 0x7fff, /* src_mask */
1892 0x7fff, /* dst_mask */
1893 FALSE), /* pcrel_offset */
1895 /* Relax hint for unconditional call sequence */
1896 HOWTO (R_NDS32_LONGCALL4, /* type */
1898 2, /* size (0 = byte, 1 = short, 2 = long) */
1900 FALSE, /* pc_relative */
1902 complain_overflow_dont, /* complain_on_overflow */
1903 nds32_elf_ignore_reloc, /* special_function */
1904 "R_NDS32_LONGCALL4", /* name */
1905 FALSE, /* partial_inplace */
1906 0xffffffff, /* src_mask */
1907 0xffffffff, /* dst_mask */
1908 FALSE), /* pcrel_offset */
1910 /* Relax hint for conditional call sequence. */
1911 HOWTO (R_NDS32_LONGCALL5, /* type */
1913 2, /* size (0 = byte, 1 = short, 2 = long) */
1915 FALSE, /* pc_relative */
1917 complain_overflow_dont, /* complain_on_overflow */
1918 nds32_elf_ignore_reloc, /* special_function */
1919 "R_NDS32_LONGCALL5", /* name */
1920 FALSE, /* partial_inplace */
1921 0xffffffff, /* src_mask */
1922 0xffffffff, /* dst_mask */
1923 FALSE), /* pcrel_offset */
1925 /* Relax hint for conditional call sequence. */
1926 HOWTO (R_NDS32_LONGCALL6, /* type */
1928 2, /* size (0 = byte, 1 = short, 2 = long) */
1930 FALSE, /* pc_relative */
1932 complain_overflow_dont, /* complain_on_overflow */
1933 nds32_elf_ignore_reloc, /* special_function */
1934 "R_NDS32_LONGCALL6", /* name */
1935 FALSE, /* partial_inplace */
1936 0xffffffff, /* src_mask */
1937 0xffffffff, /* dst_mask */
1938 FALSE), /* pcrel_offset */
1940 /* Relax hint for unconditional branch sequence. */
1941 HOWTO (R_NDS32_LONGJUMP4, /* type */
1943 2, /* size (0 = byte, 1 = short, 2 = long) */
1945 FALSE, /* pc_relative */
1947 complain_overflow_dont, /* complain_on_overflow */
1948 nds32_elf_ignore_reloc, /* special_function */
1949 "R_NDS32_LONGJUMP4", /* name */
1950 FALSE, /* partial_inplace */
1951 0xffffffff, /* src_mask */
1952 0xffffffff, /* dst_mask */
1953 FALSE), /* pcrel_offset */
1955 /* Relax hint for conditional branch sequence. */
1956 HOWTO (R_NDS32_LONGJUMP5, /* type */
1958 2, /* size (0 = byte, 1 = short, 2 = long) */
1960 FALSE, /* pc_relative */
1962 complain_overflow_dont, /* complain_on_overflow */
1963 nds32_elf_ignore_reloc, /* special_function */
1964 "R_NDS32_LONGJUMP5", /* name */
1965 FALSE, /* partial_inplace */
1966 0xffffffff, /* src_mask */
1967 0xffffffff, /* dst_mask */
1968 FALSE), /* pcrel_offset */
1970 /* Relax hint for conditional branch sequence. */
1971 HOWTO (R_NDS32_LONGJUMP6, /* type */
1973 2, /* size (0 = byte, 1 = short, 2 = long) */
1975 FALSE, /* pc_relative */
1977 complain_overflow_dont, /* complain_on_overflow */
1978 nds32_elf_ignore_reloc, /* special_function */
1979 "R_NDS32_LONGJUMP6", /* name */
1980 FALSE, /* partial_inplace */
1981 0xffffffff, /* src_mask */
1982 0xffffffff, /* dst_mask */
1983 FALSE), /* pcrel_offset */
1985 /* Relax hint for conditional branch sequence. */
1986 HOWTO (R_NDS32_LONGJUMP7, /* type */
1988 2, /* size (0 = byte, 1 = short, 2 = long) */
1990 FALSE, /* pc_relative */
1992 complain_overflow_dont, /* complain_on_overflow */
1993 nds32_elf_ignore_reloc, /* special_function */
1994 "R_NDS32_LONGJUMP7", /* name */
1995 FALSE, /* partial_inplace */
1996 0xffffffff, /* src_mask */
1997 0xffffffff, /* dst_mask */
1998 FALSE), /* pcrel_offset */
2001 /* Relocations used for relaxation. */
2002 static reloc_howto_type nds32_elf_relax_howto_table[] =
2004 HOWTO (R_NDS32_RELAX_ENTRY, /* type */
2006 2, /* size (0 = byte, 1 = short, 2 = long) */
2008 FALSE, /* pc_relative */
2010 complain_overflow_dont,/* complain_on_overflow */
2011 nds32_elf_ignore_reloc,/* special_function */
2012 "R_NDS32_RELAX_ENTRY", /* name */
2013 FALSE, /* partial_inplace */
2014 0xffffffff, /* src_mask */
2015 0xffffffff, /* dst_mask */
2016 FALSE), /* pcrel_offset */
2017 HOWTO (R_NDS32_GOT_SUFF, /* type */
2019 2, /* size (0 = byte, 1 = short, 2 = long) */
2021 FALSE, /* pc_relative */
2023 complain_overflow_dont,/* complain_on_overflow */
2024 nds32_elf_ignore_reloc,/* special_function */
2025 "R_NDS32_GOT_SUFF", /* name */
2026 FALSE, /* partial_inplace */
2027 0xffffffff, /* src_mask */
2028 0xffffffff, /* dst_mask */
2029 FALSE), /* pcrel_offset */
2030 HOWTO (R_NDS32_GOTOFF_SUFF, /* type */
2032 2, /* size (0 = byte, 1 = short, 2 = long) */
2034 FALSE, /* pc_relative */
2036 complain_overflow_bitfield, /* complain_on_overflow */
2037 nds32_elf_ignore_reloc,/* special_function */
2038 "R_NDS32_GOTOFF_SUFF", /* name */
2039 FALSE, /* partial_inplace */
2040 0xffffffff, /* src_mask */
2041 0xffffffff, /* dst_mask */
2042 FALSE), /* pcrel_offset */
2043 HOWTO (R_NDS32_PLT_GOT_SUFF, /* type */
2045 2, /* size (0 = byte, 1 = short, 2 = long) */
2047 FALSE, /* pc_relative */
2049 complain_overflow_dont,/* complain_on_overflow */
2050 nds32_elf_ignore_reloc,/* special_function */
2051 "R_NDS32_PLT_GOT_SUFF",/* name */
2052 FALSE, /* partial_inplace */
2053 0xffffffff, /* src_mask */
2054 0xffffffff, /* dst_mask */
2055 FALSE), /* pcrel_offset */
2056 HOWTO (R_NDS32_MULCALL_SUFF, /* type */
2058 2, /* size (0 = byte, 1 = short, 2 = long) */
2060 FALSE, /* pc_relative */
2062 complain_overflow_dont,/* complain_on_overflow */
2063 nds32_elf_ignore_reloc,/* special_function */
2064 "R_NDS32_MULCALL_SUFF",/* name */
2065 FALSE, /* partial_inplace */
2066 0xffffffff, /* src_mask */
2067 0xffffffff, /* dst_mask */
2068 FALSE), /* pcrel_offset */
2069 HOWTO (R_NDS32_PTR, /* type */
2071 2, /* size (0 = byte, 1 = short, 2 = long) */
2073 FALSE, /* pc_relative */
2075 complain_overflow_dont,/* complain_on_overflow */
2076 nds32_elf_ignore_reloc,/* special_function */
2077 "R_NDS32_PTR", /* name */
2078 FALSE, /* partial_inplace */
2079 0xffffffff, /* src_mask */
2080 0xffffffff, /* dst_mask */
2081 FALSE), /* pcrel_offset */
2082 HOWTO (R_NDS32_PTR_COUNT, /* type */
2084 2, /* size (0 = byte, 1 = short, 2 = long) */
2086 FALSE, /* pc_relative */
2088 complain_overflow_dont,/* complain_on_overflow */
2089 nds32_elf_ignore_reloc,/* special_function */
2090 "R_NDS32_PTR_COUNT", /* name */
2091 FALSE, /* partial_inplace */
2092 0xffffffff, /* src_mask */
2093 0xffffffff, /* dst_mask */
2094 FALSE), /* pcrel_offset */
2095 HOWTO (R_NDS32_PTR_RESOLVED, /* type */
2097 2, /* size (0 = byte, 1 = short, 2 = long) */
2099 FALSE, /* pc_relative */
2101 complain_overflow_dont,/* complain_on_overflow */
2102 nds32_elf_ignore_reloc,/* special_function */
2103 "R_NDS32_PTR_RESOLVED",/* name */
2104 FALSE, /* partial_inplace */
2105 0xffffffff, /* src_mask */
2106 0xffffffff, /* dst_mask */
2107 FALSE), /* pcrel_offset */
2108 HOWTO (R_NDS32_PLTBLOCK, /* type */
2110 2, /* size (0 = byte, 1 = short, 2 = long) */
2112 FALSE, /* pc_relative */
2114 complain_overflow_dont,/* complain_on_overflow */
2115 nds32_elf_ignore_reloc,/* special_function */
2116 "R_NDS32_PLTBLOCK", /* name */
2117 FALSE, /* partial_inplace */
2118 0xffffffff, /* src_mask */
2119 0xffffffff, /* dst_mask */
2120 FALSE), /* pcrel_offset */
2121 HOWTO (R_NDS32_RELAX_REGION_BEGIN, /* type */
2123 2, /* size (0 = byte, 1 = short, 2 = long) */
2125 FALSE, /* pc_relative */
2127 complain_overflow_dont,/* complain_on_overflow */
2128 nds32_elf_ignore_reloc,/* special_function */
2129 "R_NDS32_RELAX_REGION_BEGIN", /* name */
2130 FALSE, /* partial_inplace */
2131 0xffffffff, /* src_mask */
2132 0xffffffff, /* dst_mask */
2133 FALSE), /* pcrel_offset */
2134 HOWTO (R_NDS32_RELAX_REGION_END, /* type */
2136 2, /* size (0 = byte, 1 = short, 2 = long) */
2138 FALSE, /* pc_relative */
2140 complain_overflow_dont,/* complain_on_overflow */
2141 nds32_elf_ignore_reloc,/* special_function */
2142 "R_NDS32_RELAX_REGION_END", /* name */
2143 FALSE, /* partial_inplace */
2144 0xffffffff, /* src_mask */
2145 0xffffffff, /* dst_mask */
2146 FALSE), /* pcrel_offset */
2147 HOWTO (R_NDS32_MINUEND, /* type */
2149 2, /* size (0 = byte, 1 = short, 2 = long) */
2151 FALSE, /* pc_relative */
2153 complain_overflow_dont,/* complain_on_overflow */
2154 nds32_elf_ignore_reloc,/* special_function */
2155 "R_NDS32_MINUEND", /* name */
2156 FALSE, /* partial_inplace */
2157 0xffffffff, /* src_mask */
2158 0xffffffff, /* dst_mask */
2159 FALSE), /* pcrel_offset */
2160 HOWTO (R_NDS32_SUBTRAHEND, /* type */
2162 2, /* size (0 = byte, 1 = short, 2 = long) */
2164 FALSE, /* pc_relative */
2166 complain_overflow_dont,/* complain_on_overflow */
2167 nds32_elf_ignore_reloc,/* special_function */
2168 "R_NDS32_SUBTRAHEND", /* name */
2169 FALSE, /* partial_inplace */
2170 0xffffffff, /* src_mask */
2171 0xffffffff, /* dst_mask */
2172 FALSE), /* pcrel_offset */
2173 HOWTO (R_NDS32_DIFF8, /* type */
2175 0, /* size (0 = byte, 1 = short, 2 = long) */
2177 FALSE, /* pc_relative */
2179 complain_overflow_dont,/* complain_on_overflow */
2180 nds32_elf_ignore_reloc,/* special_function */
2181 "R_NDS32_DIFF8", /* name */
2182 FALSE, /* partial_inplace */
2183 0x000000ff, /* src_mask */
2184 0x000000ff, /* dst_mask */
2185 FALSE), /* pcrel_offset */
2186 HOWTO (R_NDS32_DIFF16, /* type */
2188 1, /* size (0 = byte, 1 = short, 2 = long) */
2190 FALSE, /* pc_relative */
2192 complain_overflow_dont,/* complain_on_overflow */
2193 nds32_elf_ignore_reloc,/* special_function */
2194 "R_NDS32_DIFF16", /* name */
2195 FALSE, /* partial_inplace */
2196 0x0000ffff, /* src_mask */
2197 0x0000ffff, /* dst_mask */
2198 FALSE), /* pcrel_offset */
2199 HOWTO (R_NDS32_DIFF32, /* type */
2201 2, /* size (0 = byte, 1 = short, 2 = long) */
2203 FALSE, /* pc_relative */
2205 complain_overflow_dont,/* complain_on_overflow */
2206 nds32_elf_ignore_reloc,/* special_function */
2207 "R_NDS32_DIFF32", /* name */
2208 FALSE, /* partial_inplace */
2209 0xffffffff, /* src_mask */
2210 0xffffffff, /* dst_mask */
2211 FALSE), /* pcrel_offset */
2212 HOWTO (R_NDS32_DIFF_ULEB128, /* type */
2214 0, /* size (0 = byte, 1 = short, 2 = long) */
2216 FALSE, /* pc_relative */
2218 complain_overflow_dont,/* complain_on_overflow */
2219 nds32_elf_ignore_reloc,/* special_function */
2220 "R_NDS32_DIFF_ULEB128",/* name */
2221 FALSE, /* partial_inplace */
2222 0xffffffff, /* src_mask */
2223 0xffffffff, /* dst_mask */
2224 FALSE), /* pcrel_offset */
2225 HOWTO (R_NDS32_DATA, /* type */
2227 2, /* size (0 = byte, 1 = short, 2 = long) */
2229 FALSE, /* pc_relative */
2231 complain_overflow_dont,/* complain_on_overflow */
2232 nds32_elf_ignore_reloc,/* special_function */
2233 "R_NDS32_DATA", /* name */
2234 FALSE, /* partial_inplace */
2235 0xffffffff, /* src_mask */
2236 0xffffffff, /* dst_mask */
2237 FALSE), /* pcrel_offset */
2238 HOWTO (R_NDS32_TRAN, /* type */
2240 2, /* size (0 = byte, 1 = short, 2 = long) */
2242 FALSE, /* pc_relative */
2244 complain_overflow_dont,/* complain_on_overflow */
2245 nds32_elf_ignore_reloc,/* special_function */
2246 "R_NDS32_TRAN", /* name */
2247 FALSE, /* partial_inplace */
2248 0xffffffff, /* src_mask */
2249 0xffffffff, /* dst_mask */
2250 FALSE), /* pcrel_offset */
2251 HOWTO (R_NDS32_TLS_LE_ADD, /* type */
2253 2, /* size (0 = byte, 1 = short, 2 = long) */
2255 FALSE, /* pc_relative */
2257 complain_overflow_dont, /* complain_on_overflow */
2258 nds32_elf_ignore_reloc, /* special_function */
2259 "R_NDS32_TLS_LE_ADD", /* name */
2260 FALSE, /* partial_inplace */
2261 0xffffffff, /* src_mask */
2262 0xffffffff, /* dst_mask */
2263 FALSE), /* pcrel_offset */
2264 HOWTO (R_NDS32_TLS_LE_LS, /* type */
2266 2, /* size (0 = byte, 1 = short, 2 = long) */
2268 FALSE, /* pc_relative */
2270 complain_overflow_dont, /* complain_on_overflow */
2271 nds32_elf_ignore_reloc, /* special_function */
2272 "R_NDS32_TLS_LE_LS", /* name */
2273 FALSE, /* partial_inplace */
2274 0xffffffff, /* src_mask */
2275 0xffffffff, /* dst_mask */
2276 FALSE), /* pcrel_offset */
2277 HOWTO (R_NDS32_EMPTY, /* type */
2279 2, /* size (0 = byte, 1 = short, 2 = long) */
2281 FALSE, /* pc_relative */
2283 complain_overflow_dont, /* complain_on_overflow */
2284 nds32_elf_ignore_reloc, /* special_function */
2285 "R_NDS32_EMPTY", /* name */
2286 FALSE, /* partial_inplace */
2287 0xffffffff, /* src_mask */
2288 0xffffffff, /* dst_mask */
2289 FALSE), /* pcrel_offset */
2293 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2294 This prototype is the same as qsort (). */
2297 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2298 int (*compar) (const void *lhs, const void *rhs))
2300 char *ptr = (char *) base;
2302 char *tmp = xmalloc (size);
2304 /* If i is less than j, i is inserted before j.
2306 |---- j ----- i --------------|
2311 for (i = 1; i < (int) nmemb; i++)
2313 for (j = (i - 1); j >= 0; j--)
2314 if (compar (ptr + i * size, ptr + j * size) >= 0)
2320 continue; /* i is in order. */
2322 memcpy (tmp, ptr + i * size, size);
2323 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2324 memcpy (ptr + j * size, tmp, size);
2329 /* Sort relocation by r_offset.
2331 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2332 algorithm. Relocations at the same r_offset must keep their order.
2333 For example, RELAX_ENTRY must be the very first relocation entry.
2335 Currently, this function implements insertion-sort.
2337 FIXME: If we already sort them in assembler, why bother sort them
2341 compar_reloc (const void *lhs, const void *rhs)
2343 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2344 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2346 if (l->r_offset > r->r_offset)
2348 else if (l->r_offset == r->r_offset)
2354 /* Functions listed below are only used for old relocs.
2355 * nds32_elf_9_pcrel_reloc
2356 * nds32_elf_do_9_pcrel_reloc
2357 * nds32_elf_hi20_reloc
2358 * nds32_elf_relocate_hi20
2359 * nds32_elf_lo12_reloc
2360 * nds32_elf_sda15_reloc
2361 * nds32_elf_generic_reloc
2364 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2366 static bfd_reloc_status_type
2367 nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2368 void *data, asection *input_section, bfd *output_bfd,
2369 char **error_message ATTRIBUTE_UNUSED)
2371 /* This part is from bfd_elf_generic_reloc. */
2372 if (output_bfd != (bfd *) NULL
2373 && (symbol->flags & BSF_SECTION_SYM) == 0
2374 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2376 reloc_entry->address += input_section->output_offset;
2377 return bfd_reloc_ok;
2380 if (output_bfd != NULL)
2382 /* FIXME: See bfd_perform_relocation. Is this right? */
2383 return bfd_reloc_continue;
2386 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2388 data, reloc_entry->address,
2391 + symbol->section->output_section->vma
2392 + symbol->section->output_offset),
2393 reloc_entry->addend);
2396 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2397 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2399 static bfd_reloc_status_type
2400 nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
2401 asection *input_section, bfd_byte *data,
2403 asection *symbol_section ATTRIBUTE_UNUSED,
2404 bfd_vma symbol_value, bfd_vma addend)
2406 bfd_signed_vma relocation;
2408 bfd_reloc_status_type status;
2410 /* Sanity check the address (offset in section). */
2411 if (offset > bfd_get_section_limit (abfd, input_section))
2412 return bfd_reloc_outofrange;
2414 relocation = symbol_value + addend;
2415 /* Make it pc relative. */
2416 relocation -= (input_section->output_section->vma
2417 + input_section->output_offset);
2418 /* These jumps mask off the lower two bits of the current address
2419 before doing pcrel calculations. */
2420 relocation -= (offset & -(bfd_vma) 2);
2422 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2423 status = bfd_reloc_overflow;
2425 status = bfd_reloc_ok;
2427 x = bfd_getb16 (data + offset);
2429 relocation >>= howto->rightshift;
2430 relocation <<= howto->bitpos;
2431 x = (x & ~howto->dst_mask)
2432 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2434 bfd_putb16 ((bfd_vma) x, data + offset);
2439 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2440 HI20_SLO is for the add3 and load/store with displacement instructions.
2441 HI20 is for the or3 instruction.
2442 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2443 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2444 we must add one to the high 16 bytes (which will get subtracted off when
2445 the low 16 bits are added).
2446 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2447 because there is a carry from the LO12 to the HI20. Here we just save
2448 the information we need; we do the actual relocation when we see the LO12.
2449 This code is copied from the elf32-mips.c. We also support an arbitrary
2450 number of HI20 relocs to be associated with a single LO12 reloc. The
2451 assembler sorts the relocs to ensure each HI20 immediately precedes its
2452 LO12. However if there are multiple copies, the assembler may not find
2453 the real LO12 so it picks the first one it finds. */
2457 struct nds32_hi20 *next;
2462 static struct nds32_hi20 *nds32_hi20_list;
2464 static bfd_reloc_status_type
2465 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2466 asymbol *symbol, void *data, asection *input_section,
2467 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2469 bfd_reloc_status_type ret;
2471 struct nds32_hi20 *n;
2473 /* This part is from bfd_elf_generic_reloc.
2474 If we're relocating, and this an external symbol, we don't want
2475 to change anything. */
2476 if (output_bfd != (bfd *) NULL
2477 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2479 reloc_entry->address += input_section->output_offset;
2480 return bfd_reloc_ok;
2483 /* Sanity check the address (offset in section). */
2484 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2485 return bfd_reloc_outofrange;
2488 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2489 ret = bfd_reloc_undefined;
2491 if (bfd_is_com_section (symbol->section))
2494 relocation = symbol->value;
2496 relocation += symbol->section->output_section->vma;
2497 relocation += symbol->section->output_offset;
2498 relocation += reloc_entry->addend;
2500 /* Save the information, and let LO12 do the actual relocation. */
2501 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2503 return bfd_reloc_outofrange;
2505 n->addr = (bfd_byte *) data + reloc_entry->address;
2506 n->addend = relocation;
2507 n->next = nds32_hi20_list;
2508 nds32_hi20_list = n;
2510 if (output_bfd != (bfd *) NULL)
2511 reloc_entry->address += input_section->output_offset;
2516 /* Handle an NDS32 ELF HI20 reloc. */
2519 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2520 int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
2521 Elf_Internal_Rela *rello, bfd_byte *contents,
2527 insn = bfd_getb32 (contents + relhi->r_offset);
2529 addlo = bfd_getb32 (contents + rello->r_offset);
2532 addend += ((insn & 0xfffff) << 20) + addlo;
2534 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2535 bfd_putb32 (insn, contents + relhi->r_offset);
2538 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2539 inplace relocation; this function exists in order to do the
2540 R_NDS32_HI20_[SU]LO relocation described above. */
2542 static bfd_reloc_status_type
2543 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2544 void *data, asection *input_section, bfd *output_bfd,
2545 char **error_message)
2547 /* This part is from bfd_elf_generic_reloc.
2548 If we're relocating, and this an external symbol, we don't want
2549 to change anything. */
2550 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2551 && reloc_entry->addend == 0)
2553 reloc_entry->address += input_section->output_offset;
2554 return bfd_reloc_ok;
2557 if (nds32_hi20_list != NULL)
2559 struct nds32_hi20 *l;
2561 l = nds32_hi20_list;
2566 unsigned long vallo;
2567 struct nds32_hi20 *next;
2569 /* Do the HI20 relocation. Note that we actually don't need
2570 to know anything about the LO12 itself, except where to
2571 find the low 12 bits of the addend needed by the LO12. */
2572 insn = bfd_getb32 (l->addr);
2573 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2575 switch (reloc_entry->howto->type)
2577 case R_NDS32_LO12S3:
2581 case R_NDS32_LO12S2:
2585 case R_NDS32_LO12S1:
2589 case R_NDS32_LO12S0:
2594 val = ((insn & 0xfffff) << 12) + vallo;
2597 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2598 bfd_putb32 ((bfd_vma) insn, l->addr);
2605 nds32_hi20_list = NULL;
2608 /* Now do the LO12 reloc in the usual way.
2609 ??? It would be nice to call bfd_elf_generic_reloc here,
2610 but we have partial_inplace set. bfd_elf_generic_reloc will
2611 pass the handling back to bfd_install_relocation which will install
2612 a section relative addend which is wrong. */
2613 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2614 input_section, output_bfd, error_message);
2617 /* Do generic partial_inplace relocation.
2618 This is a local replacement for bfd_elf_generic_reloc. */
2620 static bfd_reloc_status_type
2621 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2622 asymbol *symbol, void *data, asection *input_section,
2623 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2625 bfd_reloc_status_type ret;
2627 bfd_byte *inplace_address;
2629 /* This part is from bfd_elf_generic_reloc.
2630 If we're relocating, and this an external symbol, we don't want
2631 to change anything. */
2632 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2633 && reloc_entry->addend == 0)
2635 reloc_entry->address += input_section->output_offset;
2636 return bfd_reloc_ok;
2639 /* Now do the reloc in the usual way.
2640 ??? It would be nice to call bfd_elf_generic_reloc here,
2641 but we have partial_inplace set. bfd_elf_generic_reloc will
2642 pass the handling back to bfd_install_relocation which will install
2643 a section relative addend which is wrong. */
2645 /* Sanity check the address (offset in section). */
2646 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2647 return bfd_reloc_outofrange;
2650 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2651 ret = bfd_reloc_undefined;
2653 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2656 relocation = symbol->value;
2658 /* Only do this for a final link. */
2659 if (output_bfd == (bfd *) NULL)
2661 relocation += symbol->section->output_section->vma;
2662 relocation += symbol->section->output_offset;
2665 relocation += reloc_entry->addend;
2666 switch (reloc_entry->howto->type)
2668 case R_NDS32_LO12S3:
2672 case R_NDS32_LO12S2:
2676 case R_NDS32_LO12S1:
2680 case R_NDS32_LO12S0:
2686 inplace_address = (bfd_byte *) data + reloc_entry->address;
2689 x = ((x & ~reloc_entry->howto->dst_mask) | \
2690 (((x & reloc_entry->howto->src_mask) + relocation) & \
2691 reloc_entry->howto->dst_mask))
2693 switch (reloc_entry->howto->size)
2697 short x = bfd_getb16 (inplace_address);
2700 bfd_putb16 ((bfd_vma) x, inplace_address);
2705 unsigned long x = bfd_getb32 (inplace_address);
2708 bfd_putb32 ((bfd_vma) x, inplace_address);
2715 if (output_bfd != (bfd *) NULL)
2716 reloc_entry->address += input_section->output_offset;
2721 /* Handle the R_NDS32_SDA15 reloc.
2722 This reloc is used to compute the address of objects in the small data area
2723 and to perform loads and stores from that area.
2724 The lower 15 bits are sign extended and added to the register specified
2725 in the instruction, which is assumed to point to _SDA_BASE_.
2727 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2728 the access size, this must be taken care of. */
2730 static bfd_reloc_status_type
2731 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2732 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2733 asection *input_section, bfd *output_bfd,
2734 char **error_message ATTRIBUTE_UNUSED)
2736 /* This part is from bfd_elf_generic_reloc. */
2737 if (output_bfd != (bfd *) NULL
2738 && (symbol->flags & BSF_SECTION_SYM) == 0
2739 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2741 reloc_entry->address += input_section->output_offset;
2742 return bfd_reloc_ok;
2745 if (output_bfd != NULL)
2747 /* FIXME: See bfd_perform_relocation. Is this right? */
2748 return bfd_reloc_continue;
2751 /* FIXME: not sure what to do here yet. But then again, the linker
2752 may never call us. */
2756 /* nds32_elf_ignore_reloc is the special function for
2757 relocation types which don't need to be relocated
2758 like relaxation relocation types.
2759 This function simply return bfd_reloc_ok when it is
2762 static bfd_reloc_status_type
2763 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2764 asymbol *symbol ATTRIBUTE_UNUSED,
2765 void *data ATTRIBUTE_UNUSED, asection *input_section,
2766 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2768 if (output_bfd != NULL)
2769 reloc_entry->address += input_section->output_offset;
2771 return bfd_reloc_ok;
2775 /* Map BFD reloc types to NDS32 ELF reloc types. */
2777 struct nds32_reloc_map_entry
2779 bfd_reloc_code_real_type bfd_reloc_val;
2780 unsigned char elf_reloc_val;
2783 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
2785 {BFD_RELOC_NONE, R_NDS32_NONE},
2786 {BFD_RELOC_16, R_NDS32_16_RELA},
2787 {BFD_RELOC_32, R_NDS32_32_RELA},
2788 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
2789 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
2790 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
2791 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
2792 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
2793 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
2794 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
2795 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
2796 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
2797 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
2798 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
2799 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
2800 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
2801 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
2802 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
2803 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
2804 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
2805 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
2806 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
2807 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
2809 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
2810 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
2811 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
2812 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
2813 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
2814 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
2815 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
2816 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
2817 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
2818 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
2819 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
2820 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
2821 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
2822 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
2823 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
2824 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
2825 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
2826 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
2827 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
2828 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
2829 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
2830 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
2831 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
2832 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
2833 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
2834 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
2835 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
2836 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
2837 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
2838 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
2839 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
2840 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
2841 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
2842 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
2843 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
2844 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
2845 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
2846 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
2847 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
2848 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
2849 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
2850 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
2851 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
2852 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
2853 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
2854 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
2855 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
2856 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
2857 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
2858 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
2859 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
2860 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
2861 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
2862 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
2863 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
2864 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
2865 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
2866 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
2867 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
2868 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
2869 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
2870 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
2871 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
2872 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
2873 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
2874 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
2875 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
2876 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
2877 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
2878 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
2879 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
2880 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
2881 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
2882 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
2883 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
2885 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
2886 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
2887 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
2888 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
2889 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
2890 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
2891 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
2892 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
2893 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
2894 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
2895 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
2896 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
2897 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
2898 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
2899 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
2900 {BFD_RELOC_NDS32_TLS_TPOFF, R_NDS32_TLS_TPOFF},
2901 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
2902 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
2903 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
2904 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
2909 static reloc_howto_type *
2910 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2915 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
2916 if (nds32_elf_howto_table[i].name != NULL
2917 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
2918 return &nds32_elf_howto_table[i];
2920 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
2921 if (nds32_elf_relax_howto_table[i].name != NULL
2922 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
2923 return &nds32_elf_relax_howto_table[i];
2928 static reloc_howto_type *
2929 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
2931 if (code < R_NDS32_RELAX_ENTRY)
2933 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
2934 return &nds32_elf_howto_table[code];
2938 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
2939 < ARRAY_SIZE (nds32_elf_relax_howto_table));
2940 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
2944 static reloc_howto_type *
2945 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2946 bfd_reloc_code_real_type code)
2950 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
2952 if (nds32_reloc_map[i].bfd_reloc_val == code)
2953 return bfd_elf32_bfd_reloc_type_table_lookup
2954 (nds32_reloc_map[i].elf_reloc_val);
2960 /* Set the howto pointer for an NDS32 ELF reloc. */
2963 nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2964 Elf_Internal_Rela *dst)
2966 enum elf_nds32_reloc_type r_type;
2968 r_type = ELF32_R_TYPE (dst->r_info);
2969 if (r_type > R_NDS32_GNU_VTENTRY)
2971 /* xgettext:c-format */
2972 _bfd_error_handler (_("%B: invalid NDS32 reloc number: %d"), abfd, r_type);
2975 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
2979 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2980 Elf_Internal_Rela *dst)
2982 BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
2983 || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
2984 && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
2985 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
2988 /* Support for core dump NOTE sections.
2989 Reference to include/linux/elfcore.h in Linux. */
2992 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2997 switch (note->descsz)
3000 /* Linux/NDS32 32-bit, ABI1 */
3003 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3006 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3014 /* Linux/NDS32 32-bit */
3017 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3020 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3031 /* Make a ".reg" section. */
3032 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3033 size, note->descpos + offset);
3037 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3039 switch (note->descsz)
3044 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3045 elf_tdata (abfd)->core->program =
3046 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3047 elf_tdata (abfd)->core->command =
3048 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3055 /* Note that for some reason, a spurious space is tacked
3056 onto the end of the args in some (at least one anyway)
3057 implementations, so strip it off if it exists. */
3059 char *command = elf_tdata (abfd)->core->command;
3060 int n = strlen (command);
3062 if (0 < n && command[n - 1] == ' ')
3063 command[n - 1] = '\0';
3069 /* Hook called by the linker routine which adds symbols from an object
3070 file. We must handle the special NDS32 section numbers here.
3071 We also keep watching for whether we need to create the sdata special
3075 nds32_elf_add_symbol_hook (bfd *abfd,
3076 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3077 Elf_Internal_Sym *sym,
3078 const char **namep ATTRIBUTE_UNUSED,
3079 flagword *flagsp ATTRIBUTE_UNUSED,
3080 asection **secp, bfd_vma *valp)
3082 switch (sym->st_shndx)
3085 /* Common symbols less than the GP size are automatically
3086 treated as SHN_MIPS_SCOMMON symbols. */
3087 if (sym->st_size > elf_gp_size (abfd)
3088 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3091 /* st_value is the alignemnt constraint.
3092 That might be its actual size if it is an array or structure. */
3093 switch (sym->st_value)
3096 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3099 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3102 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3105 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3111 (*secp)->flags |= SEC_IS_COMMON;
3112 *valp = sym->st_size;
3120 /* This function can figure out the best location for a base register to access
3121 data relative to this base register
3123 sda_d0: size of first DOUBLE WORD data section
3124 sda_w0: size of first WORD data section
3125 sda_h0: size of first HALF WORD data section
3126 sda_b : size of BYTE data section
3127 sda_hi: size of second HALF WORD data section
3128 sda_w1: size of second WORD data section
3129 sda_d1: size of second DOUBLE WORD data section
3131 offset (always positive) from the beginning of sda_d0 if OK
3132 a negative error value if fail
3134 these 7 sections have to be located back to back if exist
3135 a pass in 0 value for non-existing section */
3137 /* Due to the interpretation of simm15 field of load/store depending on
3138 data accessing size, the organization of base register relative data shall
3139 like the following figure
3140 -------------------------------------------
3141 | DOUBLE WORD sized data (range +/- 128K)
3142 -------------------------------------------
3143 | WORD sized data (range +/- 64K)
3144 -------------------------------------------
3145 | HALF WORD sized data (range +/- 32K)
3146 -------------------------------------------
3147 | BYTE sized data (range +/- 16K)
3148 -------------------------------------------
3149 | HALF WORD sized data (range +/- 32K)
3150 -------------------------------------------
3151 | WORD sized data (range +/- 64K)
3152 -------------------------------------------
3153 | DOUBLE WORD sized data (range +/- 128K)
3154 -------------------------------------------
3155 Its base register shall be set to access these data freely. */
3157 /* We have to figure out the SDA_BASE value, so that we can adjust the
3158 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3159 BFD. If we can't find it, we're stuck. We cache it in the ELF
3160 target data. We don't need to adjust the symbol value for an
3161 external symbol if we are producing relocatable output. */
3163 static asection *sda_rela_sec = NULL;
3165 #define SDA_SECTION_NUM 10
3167 static bfd_reloc_status_type
3168 nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
3169 bfd_vma *psb, bfd_boolean add_symbol)
3172 struct elf_nds32_link_hash_table *table;
3173 struct bfd_link_hash_entry *h, *h2;
3174 long unsigned int total = 0;
3176 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3177 if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
3179 asection *first = NULL, *final = NULL, *temp;
3181 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3182 4 byte-aligned. Therefore, it has to set the first section ".data"
3184 static const char sec_name[SDA_SECTION_NUM][10] =
3186 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3187 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3191 if (output_bfd->sections == NULL)
3193 *psb = elf_gp (output_bfd);
3194 return bfd_reloc_ok;
3197 /* Get the first and final section. */
3198 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3200 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3201 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3203 if (temp && (temp->size != 0 || temp->rawsize != 0))
3206 /* Summarize the sections in order to check if joining .bss. */
3207 if (temp && temp->size != 0)
3208 total += temp->size;
3209 else if (temp && temp->rawsize != 0)
3210 total += temp->rawsize;
3215 /* Check .bss size. */
3216 temp = bfd_get_section_by_name (output_bfd, ".bss");
3219 if (temp->size != 0)
3220 total += temp->size;
3221 else if (temp->rawsize != 0)
3222 total += temp->rawsize;
3224 if (total < 0x80000)
3226 if (!first && (temp->size != 0 || temp->rawsize != 0))
3228 if ((temp->size != 0 || temp->rawsize != 0))
3235 /* The middle of data region. */
3236 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3238 /* Find the section sda_base located. */
3240 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3242 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3243 if (final && (final->size != 0 || final->rawsize != 0)
3244 && sda_base >= final->vma)
3255 /* There is not any data section in output bfd, and set _SDA_BASE_ in
3256 first output section. */
3257 first = output_bfd->sections;
3258 while (first && first->size == 0 && first->rawsize == 0)
3259 first = first->next;
3262 *psb = elf_gp (output_bfd);
3263 return bfd_reloc_ok;
3265 sda_base = first->vma + first->rawsize;
3268 sda_base -= first->vma;
3269 sda_base = sda_base & (~7);
3271 if (!_bfd_generic_link_add_one_symbol
3272 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3273 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3274 get_elf_backend_data (output_bfd)->collect, &h))
3277 sda_rela_sec = first;
3279 table = nds32_elf_hash_table (info);
3280 relax_fp_as_gp = table->relax_fp_as_gp;
3283 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
3284 FALSE, FALSE, FALSE);
3285 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3286 And set FP equal to SDA_BASE to do relaxation for
3287 la $fp, _FP_BASE_. */
3288 if (!_bfd_generic_link_add_one_symbol
3289 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3290 first, (bfd_vma) sda_base, (const char *) NULL,
3291 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3301 elf_gp (output_bfd) = (h->u.def.value
3302 + h->u.def.section->output_section->vma
3303 + h->u.def.section->output_offset);
3307 _bfd_error_handler (_("error: Can't find symbol: _SDA_BASE_."));
3308 return bfd_reloc_dangerous;
3312 *psb = h->u.def.value + h->u.def.section->output_section->vma
3313 + h->u.def.section->output_offset;
3314 return bfd_reloc_ok;
3318 /* Return size of a PLT entry. */
3319 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3322 /* Create an entry in an nds32 ELF linker hash table. */
3324 static struct bfd_hash_entry *
3325 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3326 struct bfd_hash_table *table,
3329 struct elf_nds32_link_hash_entry *ret;
3331 ret = (struct elf_nds32_link_hash_entry *) entry;
3333 /* Allocate the structure if it has not already been allocated by a
3336 ret = (struct elf_nds32_link_hash_entry *)
3337 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3340 return (struct bfd_hash_entry *) ret;
3342 /* Call the allocation method of the superclass. */
3343 ret = (struct elf_nds32_link_hash_entry *)
3344 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3348 struct elf_nds32_link_hash_entry *eh;
3350 eh = (struct elf_nds32_link_hash_entry *) ret;
3351 eh->dyn_relocs = NULL;
3352 eh->tls_type = GOT_UNKNOWN;
3355 return (struct bfd_hash_entry *) ret;
3358 /* Create an nds32 ELF linker hash table. */
3360 static struct bfd_link_hash_table *
3361 nds32_elf_link_hash_table_create (bfd *abfd)
3363 struct elf_nds32_link_hash_table *ret;
3365 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3367 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3372 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3373 nds32_elf_link_hash_newfunc,
3374 sizeof (struct elf_nds32_link_hash_entry),
3381 return &ret->root.root;
3384 /* Create dynamic sections when linking against a dynamic object. */
3387 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3389 struct elf_nds32_link_hash_table *htab;
3390 flagword flags, pltflags;
3391 register asection *s;
3392 const struct elf_backend_data *bed;
3393 int ptralign = 2; /* 32-bit */
3395 bed = get_elf_backend_data (abfd);
3397 htab = nds32_elf_hash_table (info);
3399 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3400 .rel[a].bss sections. */
3402 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3403 | SEC_LINKER_CREATED);
3406 pltflags |= SEC_CODE;
3407 if (bed->plt_not_loaded)
3408 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3409 if (bed->plt_readonly)
3410 pltflags |= SEC_READONLY;
3412 s = bfd_make_section (abfd, ".plt");
3413 htab->root.splt = s;
3415 || !bfd_set_section_flags (abfd, s, pltflags)
3416 || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3419 if (bed->want_plt_sym)
3421 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3423 struct bfd_link_hash_entry *bh = NULL;
3424 struct elf_link_hash_entry *h;
3426 if (!(_bfd_generic_link_add_one_symbol
3427 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3428 (bfd_vma) 0, (const char *) NULL, FALSE,
3429 get_elf_backend_data (abfd)->collect, &bh)))
3432 h = (struct elf_link_hash_entry *) bh;
3434 h->type = STT_OBJECT;
3436 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3440 s = bfd_make_section (abfd,
3441 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3442 htab->root.srelplt = s;
3444 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3445 || !bfd_set_section_alignment (abfd, s, ptralign))
3448 if (htab->root.sgot == NULL && !_bfd_elf_create_got_section (abfd, info))
3452 const char *secname;
3457 for (sec = abfd->sections; sec; sec = sec->next)
3459 secflags = bfd_get_section_flags (abfd, sec);
3460 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3461 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3463 secname = bfd_get_section_name (abfd, sec);
3464 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3465 strcpy (relname, ".rela");
3466 strcat (relname, secname);
3467 if (bfd_get_section_by_name (abfd, secname))
3469 s = bfd_make_section (abfd, relname);
3471 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3472 || !bfd_set_section_alignment (abfd, s, ptralign))
3477 if (bed->want_dynbss)
3479 /* The .dynbss section is a place to put symbols which are defined
3480 by dynamic objects, are referenced by regular objects, and are
3481 not functions. We must allocate space for them in the process
3482 image and use a R_*_COPY reloc to tell the dynamic linker to
3483 initialize them at run time. The linker script puts the .dynbss
3484 section into the .bss section of the final image. */
3485 s = bfd_make_section (abfd, ".dynbss");
3488 || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3490 /* The .rel[a].bss section holds copy relocs. This section is not
3491 normally needed. We need to create it here, though, so that the
3492 linker will map it to an output section. We can't just create it
3493 only if we need it, because we will not know whether we need it
3494 until we have seen all the input files, and the first time the
3495 main linker code calls BFD after examining all the input files
3496 (size_dynamic_sections) the input sections have already been
3497 mapped to the output sections. If the section turns out not to
3498 be needed, we can discard it later. We will never need this
3499 section when generating a shared object, since they do not use
3501 if (!bfd_link_pic (info))
3503 s = bfd_make_section (abfd, (bed->default_use_rela_p
3504 ? ".rela.bss" : ".rel.bss"));
3507 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3508 || !bfd_set_section_alignment (abfd, s, ptralign))
3516 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3518 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3519 struct elf_link_hash_entry *dir,
3520 struct elf_link_hash_entry *ind)
3522 struct elf_nds32_link_hash_entry *edir, *eind;
3524 edir = (struct elf_nds32_link_hash_entry *) dir;
3525 eind = (struct elf_nds32_link_hash_entry *) ind;
3527 if (eind->dyn_relocs != NULL)
3529 if (edir->dyn_relocs != NULL)
3531 struct elf_nds32_dyn_relocs **pp;
3532 struct elf_nds32_dyn_relocs *p;
3534 if (ind->root.type == bfd_link_hash_indirect)
3537 /* Add reloc counts against the weak sym to the strong sym
3538 list. Merge any entries against the same section. */
3539 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3541 struct elf_nds32_dyn_relocs *q;
3543 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3544 if (q->sec == p->sec)
3546 q->pc_count += p->pc_count;
3547 q->count += p->count;
3554 *pp = edir->dyn_relocs;
3557 edir->dyn_relocs = eind->dyn_relocs;
3558 eind->dyn_relocs = NULL;
3561 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3565 /* Adjust a symbol defined by a dynamic object and referenced by a
3566 regular object. The current definition is in some section of the
3567 dynamic object, but we're not including those sections. We have to
3568 change the definition to something the rest of the link can
3572 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3573 struct elf_link_hash_entry *h)
3575 struct elf_nds32_link_hash_table *htab;
3576 struct elf_nds32_link_hash_entry *eh;
3577 struct elf_nds32_dyn_relocs *p;
3580 unsigned int power_of_two;
3582 dynobj = elf_hash_table (info)->dynobj;
3584 /* Make sure we know what is going on here. */
3585 BFD_ASSERT (dynobj != NULL
3587 || h->u.weakdef != NULL
3588 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3591 /* If this is a function, put it in the procedure linkage table. We
3592 will fill in the contents of the procedure linkage table later,
3593 when we know the address of the .got section. */
3594 if (h->type == STT_FUNC || h->needs_plt)
3596 if (!bfd_link_pic (info)
3599 && h->root.type != bfd_link_hash_undefweak
3600 && h->root.type != bfd_link_hash_undefined)
3602 /* This case can occur if we saw a PLT reloc in an input
3603 file, but the symbol was never referred to by a dynamic
3604 object. In such a case, we don't actually need to build
3605 a procedure linkage table, and we can just do a PCREL
3607 h->plt.offset = (bfd_vma) - 1;
3614 h->plt.offset = (bfd_vma) - 1;
3616 /* If this is a weak symbol, and there is a real definition, the
3617 processor independent code will have arranged for us to see the
3618 real definition first, and we can just use the same value. */
3619 if (h->u.weakdef != NULL)
3621 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3622 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3623 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3624 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3628 /* This is a reference to a symbol defined by a dynamic object which
3629 is not a function. */
3631 /* If we are creating a shared library, we must presume that the
3632 only references to the symbol are via the global offset table.
3633 For such cases we need not do anything here; the relocations will
3634 be handled correctly by relocate_section. */
3635 if (bfd_link_pic (info))
3638 /* If there are no references to this symbol that do not use the
3639 GOT, we don't need to generate a copy reloc. */
3640 if (!h->non_got_ref)
3643 /* If -z nocopyreloc was given, we won't generate them either. */
3644 if (info->nocopyreloc)
3650 eh = (struct elf_nds32_link_hash_entry *) h;
3651 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3653 s = p->sec->output_section;
3654 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3658 /* If we didn't find any dynamic relocs in sections which needs the
3659 copy reloc, then we'll be keeping the dynamic relocs and avoiding
3667 /* We must allocate the symbol in our .dynbss section, which will
3668 become part of the .bss section of the executable. There will be
3669 an entry for this symbol in the .dynsym section. The dynamic
3670 object will contain position independent code, so all references
3671 from the dynamic object to this symbol will go through the global
3672 offset table. The dynamic linker will use the .dynsym entry to
3673 determine the address it must put in the global offset table, so
3674 both the dynamic object and the regular object will refer to the
3675 same memory location for the variable. */
3677 htab = nds32_elf_hash_table (info);
3679 BFD_ASSERT (s != NULL);
3681 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3682 to copy the initial value out of the dynamic object and into the
3683 runtime process image. We need to remember the offset into the
3684 .rela.bss section we are going to use. */
3685 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3689 srel = htab->srelbss;
3690 BFD_ASSERT (srel != NULL);
3691 srel->size += sizeof (Elf32_External_Rela);
3695 /* We need to figure out the alignment required for this symbol. I
3696 have no idea how ELF linkers handle this. */
3697 power_of_two = bfd_log2 (h->size);
3698 if (power_of_two > 3)
3701 /* Apply the required alignment. */
3702 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3703 if (power_of_two > bfd_get_section_alignment (dynobj, s))
3705 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
3709 /* Define the symbol as being at this point in the section. */
3710 h->root.u.def.section = s;
3711 h->root.u.def.value = s->size;
3713 /* Increment the section size to make room for the symbol. */
3719 /* Allocate space in .plt, .got and associated reloc sections for
3723 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3725 struct bfd_link_info *info;
3726 struct elf_nds32_link_hash_table *htab;
3727 struct elf_nds32_link_hash_entry *eh;
3728 struct elf_nds32_dyn_relocs *p;
3730 if (h->root.type == bfd_link_hash_indirect)
3733 if (h->root.type == bfd_link_hash_warning)
3734 /* When warning symbols are created, they **replace** the "real"
3735 entry in the hash table, thus we never get to see the real
3736 symbol in a hash traversal. So look at it now. */
3737 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3739 info = (struct bfd_link_info *) inf;
3740 htab = nds32_elf_hash_table (info);
3742 eh = (struct elf_nds32_link_hash_entry *) h;
3744 if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
3746 /* Make sure this symbol is output as a dynamic symbol.
3747 Undefined weak syms won't yet be marked as dynamic. */
3748 if (h->dynindx == -1 && !h->forced_local)
3750 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3754 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
3756 asection *s = htab->root.splt;
3758 /* If this is the first .plt entry, make room for the special
3761 s->size += PLT_ENTRY_SIZE;
3763 h->plt.offset = s->size;
3765 /* If this symbol is not defined in a regular file, and we are
3766 not generating a shared library, then set the symbol to this
3767 location in the .plt. This is required to make function
3768 pointers compare as equal between the normal executable and
3769 the shared library. */
3770 if (!bfd_link_pic (info) && !h->def_regular)
3772 h->root.u.def.section = s;
3773 h->root.u.def.value = h->plt.offset;
3776 /* Make room for this entry. */
3777 s->size += PLT_ENTRY_SIZE;
3779 /* We also need to make an entry in the .got.plt section, which
3780 will be placed in the .got section by the linker script. */
3781 htab->root.sgotplt->size += 4;
3783 /* We also need to make an entry in the .rel.plt section. */
3784 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3788 h->plt.offset = (bfd_vma) - 1;
3794 h->plt.offset = (bfd_vma) - 1;
3798 if (h->got.refcount > 0)
3802 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
3804 /* Make sure this symbol is output as a dynamic symbol.
3805 Undefined weak syms won't yet be marked as dynamic. */
3806 if (h->dynindx == -1 && !h->forced_local)
3808 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3812 s = htab->root.sgot;
3813 h->got.offset = s->size;
3815 if (tls_type == GOT_UNKNOWN)
3817 else if (tls_type == GOT_NORMAL
3818 || tls_type == GOT_TLS_IE)
3819 /* Need a GOT slot. */
3822 dyn = htab->root.dynamic_sections_created;
3823 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
3824 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3827 h->got.offset = (bfd_vma) - 1;
3829 if (eh->dyn_relocs == NULL)
3832 /* In the shared -Bsymbolic case, discard space allocated for
3833 dynamic pc-relative relocs against symbols which turn out to be
3834 defined in regular objects. For the normal shared case, discard
3835 space for pc-relative relocs that have become local due to symbol
3836 visibility changes. */
3838 if (bfd_link_pic (info))
3840 if (h->def_regular && (h->forced_local || info->symbolic))
3842 struct elf_nds32_dyn_relocs **pp;
3844 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
3846 p->count -= p->pc_count;
3857 /* For the non-shared case, discard space for relocs against
3858 symbols which turn out to need copy relocs or are not dynamic. */
3863 || (htab->root.dynamic_sections_created
3864 && (h->root.type == bfd_link_hash_undefweak
3865 || h->root.type == bfd_link_hash_undefined))))
3867 /* Make sure this symbol is output as a dynamic symbol.
3868 Undefined weak syms won't yet be marked as dynamic. */
3869 if (h->dynindx == -1 && !h->forced_local)
3871 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3875 /* If that succeeded, we know we'll be keeping all the
3877 if (h->dynindx != -1)
3881 eh->dyn_relocs = NULL;
3886 /* Finally, allocate space. */
3887 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3889 asection *sreloc = elf_section_data (p->sec)->sreloc;
3890 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3896 /* Find any dynamic relocs that apply to read-only sections. */
3899 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3901 struct elf_nds32_link_hash_entry *eh;
3902 struct elf_nds32_dyn_relocs *p;
3904 if (h->root.type == bfd_link_hash_warning)
3905 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3907 eh = (struct elf_nds32_link_hash_entry *) h;
3908 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3910 asection *s = p->sec->output_section;
3912 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3914 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3916 info->flags |= DF_TEXTREL;
3918 /* Not an error, just cut short the traversal. */
3925 /* Set the sizes of the dynamic sections. */
3928 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3929 struct bfd_link_info *info)
3931 struct elf_nds32_link_hash_table *htab;
3937 htab = nds32_elf_hash_table (info);
3938 dynobj = htab->root.dynobj;
3939 BFD_ASSERT (dynobj != NULL);
3941 if (htab->root.dynamic_sections_created)
3943 /* Set the contents of the .interp section to the interpreter. */
3944 if (bfd_link_executable (info) && !info->nointerp)
3946 s = bfd_get_section_by_name (dynobj, ".interp");
3947 BFD_ASSERT (s != NULL);
3948 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3949 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3953 /* Set up .got offsets for local syms, and space for local dynamic
3955 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3957 bfd_signed_vma *local_got;
3958 bfd_signed_vma *end_local_got;
3959 bfd_size_type locsymcount;
3960 Elf_Internal_Shdr *symtab_hdr;
3963 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3966 for (s = ibfd->sections; s != NULL; s = s->next)
3968 struct elf_nds32_dyn_relocs *p;
3970 for (p = ((struct elf_nds32_dyn_relocs *)
3971 elf_section_data (s)->local_dynrel);
3972 p != NULL; p = p->next)
3974 if (!bfd_is_abs_section (p->sec)
3975 && bfd_is_abs_section (p->sec->output_section))
3977 /* Input section has been discarded, either because
3978 it is a copy of a linkonce section or due to
3979 linker script /DISCARD/, so we'll be discarding
3982 else if (p->count != 0)
3984 srel = elf_section_data (p->sec)->sreloc;
3985 srel->size += p->count * sizeof (Elf32_External_Rela);
3986 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3987 info->flags |= DF_TEXTREL;
3992 local_got = elf_local_got_refcounts (ibfd);
3996 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3997 locsymcount = symtab_hdr->sh_info;
3998 end_local_got = local_got + locsymcount;
3999 s = htab->root.sgot;
4000 srel = htab->root.srelgot;
4001 for (; local_got < end_local_got; ++local_got)
4005 *local_got = s->size;
4007 if (bfd_link_pic (info))
4008 srel->size += sizeof (Elf32_External_Rela);
4011 *local_got = (bfd_vma) - 1;
4015 /* Allocate global sym .plt and .got entries, and space for global
4016 sym dynamic relocs. */
4017 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4019 /* We now have determined the sizes of the various dynamic sections.
4020 Allocate memory for them. */
4022 for (s = dynobj->sections; s != NULL; s = s->next)
4024 if ((s->flags & SEC_LINKER_CREATED) == 0)
4027 if (s == htab->root.splt)
4029 /* Strip this section if we don't need it; see the
4032 else if (s == htab->root.sgot)
4034 got_size += s->size;
4036 else if (s == htab->root.sgotplt)
4038 got_size += s->size;
4040 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4042 if (s->size != 0 && s != htab->root.srelplt)
4045 /* We use the reloc_count field as a counter if we need
4046 to copy relocs into the output file. */
4051 /* It's not one of our sections, so don't allocate space. */
4057 /* If we don't need this section, strip it from the
4058 output file. This is mostly to handle .rela.bss and
4059 .rela.plt. We must create both sections in
4060 create_dynamic_sections, because they must be created
4061 before the linker maps input sections to output
4062 sections. The linker does that before
4063 adjust_dynamic_symbol is called, and it is that
4064 function which decides whether anything needs to go
4065 into these sections. */
4066 s->flags |= SEC_EXCLUDE;
4070 /* Allocate memory for the section contents. We use bfd_zalloc
4071 here in case unused entries are not reclaimed before the
4072 section's contents are written out. This should not happen,
4073 but this way if it does, we get a R_NDS32_NONE reloc instead
4075 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4076 if (s->contents == NULL)
4081 if (htab->root.dynamic_sections_created)
4083 /* Add some entries to the .dynamic section. We fill in the
4084 values later, in nds32_elf_finish_dynamic_sections, but we
4085 must add the entries now so that we get the correct size for
4086 the .dynamic section. The DT_DEBUG entry is filled in by the
4087 dynamic linker and used by the debugger. */
4088 #define add_dynamic_entry(TAG, VAL) \
4089 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4091 if (!bfd_link_pic (info))
4093 if (!add_dynamic_entry (DT_DEBUG, 0))
4097 if (htab->root.splt->size != 0)
4099 if (!add_dynamic_entry (DT_PLTGOT, 0)
4100 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4101 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4102 || !add_dynamic_entry (DT_JMPREL, 0))
4108 if (!add_dynamic_entry (DT_RELA, 0)
4109 || !add_dynamic_entry (DT_RELASZ, 0)
4110 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4113 /* If any dynamic relocs apply to a read-only section,
4114 then we need a DT_TEXTREL entry. */
4115 if ((info->flags & DF_TEXTREL) == 0)
4116 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
4119 if ((info->flags & DF_TEXTREL) != 0)
4121 if (!add_dynamic_entry (DT_TEXTREL, 0))
4126 #undef add_dynamic_entry
4131 static bfd_reloc_status_type
4132 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4133 bfd_vma relocation, bfd_byte *location)
4137 bfd_reloc_status_type flag;
4138 unsigned int rightshift = howto->rightshift;
4139 unsigned int bitpos = howto->bitpos;
4141 /* If the size is negative, negate RELOCATION. This isn't very
4143 if (howto->size < 0)
4144 relocation = -relocation;
4146 /* Get the value we are going to relocate. */
4147 size = bfd_get_reloc_size (howto);
4154 return bfd_reloc_ok;
4156 x = bfd_getb16 (location);
4159 x = bfd_getb32 (location);
4163 /* Check for overflow. FIXME: We may drop bits during the addition
4164 which we don't check for. We must either check at every single
4165 operation, which would be tedious, or we must do the computations
4166 in a type larger than bfd_vma, which would be inefficient. */
4167 flag = bfd_reloc_ok;
4168 if (howto->complain_on_overflow != complain_overflow_dont)
4170 bfd_vma addrmask, fieldmask, signmask, ss;
4173 /* Get the values to be added together. For signed and unsigned
4174 relocations, we assume that all values should be truncated to
4175 the size of an address. For bitfields, all the bits matter.
4176 See also bfd_check_overflow. */
4177 fieldmask = N_ONES (howto->bitsize);
4178 signmask = ~fieldmask;
4179 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4180 a = (relocation & addrmask) >> rightshift;
4181 b = (x & howto->src_mask & addrmask) >> bitpos;
4183 switch (howto->complain_on_overflow)
4185 case complain_overflow_signed:
4186 /* If any sign bits are set, all sign bits must be set.
4187 That is, A must be a valid negative address after
4189 signmask = ~(fieldmask >> 1);
4192 case complain_overflow_bitfield:
4193 /* Much like the signed check, but for a field one bit
4194 wider. We allow a bitfield to represent numbers in the
4195 range -2**n to 2**n-1, where n is the number of bits in the
4196 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4197 can't overflow, which is exactly what we want. */
4199 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4200 flag = bfd_reloc_overflow;
4202 /* We only need this next bit of code if the sign bit of B
4203 is below the sign bit of A. This would only happen if
4204 SRC_MASK had fewer bits than BITSIZE. Note that if
4205 SRC_MASK has more bits than BITSIZE, we can get into
4206 trouble; we would need to verify that B is in range, as
4207 we do for A above. */
4208 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4211 /* Set all the bits above the sign bit. */
4214 /* Now we can do the addition. */
4217 /* See if the result has the correct sign. Bits above the
4218 sign bit are junk now; ignore them. If the sum is
4219 positive, make sure we did not have all negative inputs;
4220 if the sum is negative, make sure we did not have all
4221 positive inputs. The test below looks only at the sign
4222 bits, and it really just
4223 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4225 We mask with addrmask here to explicitly allow an address
4226 wrap-around. The Linux kernel relies on it, and it is
4227 the only way to write assembler code which can run when
4228 loaded at a location 0x80000000 away from the location at
4229 which it is linked. */
4230 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4231 flag = bfd_reloc_overflow;
4235 case complain_overflow_unsigned:
4236 /* Checking for an unsigned overflow is relatively easy:
4237 trim the addresses and add, and trim the result as well.
4238 Overflow is normally indicated when the result does not
4239 fit in the field. However, we also need to consider the
4240 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4241 input is 0x80000000, and bfd_vma is only 32 bits; then we
4242 will get sum == 0, but there is an overflow, since the
4243 inputs did not fit in the field. Instead of doing a
4244 separate test, we can check for this by or-ing in the
4245 operands when testing for the sum overflowing its final
4247 sum = (a + b) & addrmask;
4248 if ((a | b | sum) & signmask)
4249 flag = bfd_reloc_overflow;
4257 /* Put RELOCATION in the right bits. */
4258 relocation >>= (bfd_vma) rightshift;
4259 relocation <<= (bfd_vma) bitpos;
4261 /* Add RELOCATION to the right bits of X. */
4263 Because the relaxation may generate duplicate relocation at one address,
4264 an addition to immediate in the instruction may cause the relocation added
4266 This bug should be fixed in assembler, but a check is also needed here. */
4267 if (howto->partial_inplace)
4268 x = ((x & ~howto->dst_mask)
4269 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4271 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4274 /* Put the relocated value back in the object file. */
4284 bfd_putb16 (x, location);
4287 bfd_putb32 (x, location);
4294 static bfd_reloc_status_type
4295 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4296 asection *input_section, bfd_byte *contents,
4297 bfd_vma address, bfd_vma value, bfd_vma addend)
4301 /* Sanity check the address. */
4302 if (address > bfd_get_section_limit (input_bfd, input_section))
4303 return bfd_reloc_outofrange;
4305 /* This function assumes that we are dealing with a basic relocation
4306 against a symbol. We want to compute the value of the symbol to
4307 relocate to. This is just VALUE, the value of the symbol, plus
4308 ADDEND, any addend associated with the reloc. */
4309 relocation = value + addend;
4311 /* If the relocation is PC relative, we want to set RELOCATION to
4312 the distance between the symbol (currently in RELOCATION) and the
4313 location we are relocating. Some targets (e.g., i386-aout)
4314 arrange for the contents of the section to be the negative of the
4315 offset of the location within the section; for such targets
4316 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4317 simply leave the contents of the section as zero; for such
4318 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4319 need to subtract out the offset of the location within the
4320 section (which is just ADDRESS). */
4321 if (howto->pc_relative)
4323 relocation -= (input_section->output_section->vma
4324 + input_section->output_offset);
4325 if (howto->pcrel_offset)
4326 relocation -= address;
4329 return nds32_relocate_contents (howto, input_bfd, relocation,
4330 contents + address);
4334 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4336 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4337 asection *input_sec,
4338 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4341 FILE *sym_ld_script = NULL;
4342 struct elf_nds32_link_hash_table *table;
4344 table = nds32_elf_hash_table (info);
4345 sym_ld_script = table->sym_ld_script;
4349 if (!h || !name || *name == '\0')
4352 if (input_sec->flags & SEC_EXCLUDE)
4355 if (!check_start_export_sym)
4357 fprintf (sym_ld_script, "SECTIONS\n{\n");
4358 check_start_export_sym = 1;
4361 if (h->root.type == bfd_link_hash_defined
4362 || h->root.type == bfd_link_hash_defweak)
4364 if (!h->root.u.def.section->output_section)
4367 if (bfd_is_const_section (input_sec))
4368 source = input_sec->name;
4370 source = input_sec->owner->filename;
4372 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4373 h->root.root.string,
4374 (long) (h->root.u.def.value
4375 + h->root.u.def.section->output_section->vma
4376 + h->root.u.def.section->output_offset), source);
4382 /* Relocate an NDS32/D ELF section.
4383 There is some attempt to make this function usable for many architectures,
4384 both for RELA and REL type relocs, if only to serve as a learning tool.
4386 The RELOCATE_SECTION function is called by the new ELF backend linker
4387 to handle the relocations for a section.
4389 The relocs are always passed as Rela structures; if the section
4390 actually uses Rel structures, the r_addend field will always be
4393 This function is responsible for adjust the section contents as
4394 necessary, and (if using Rela relocs and generating a
4395 relocatable output file) adjusting the reloc addend as
4398 This function does not have to worry about setting the reloc
4399 address or the reloc symbol index.
4401 LOCAL_SYMS is a pointer to the swapped in local symbols.
4403 LOCAL_SECTIONS is an array giving the section in the input file
4404 corresponding to the st_shndx field of each local symbol.
4406 The global hash table entry for the global symbols can be found
4407 via elf_sym_hashes (input_bfd).
4409 When generating relocatable output, this function must handle
4410 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4411 going to be the section symbol corresponding to the output
4412 section, which means that the addend must be adjusted
4416 dtpoff_base (struct bfd_link_info *info)
4418 /* If tls_sec is NULL, we should have signalled an error already. */
4419 if (elf_hash_table (info)->tls_sec == NULL)
4421 return elf_hash_table (info)->tls_sec->vma;
4425 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
4426 struct bfd_link_info * info,
4428 asection * input_section,
4429 bfd_byte * contents,
4430 Elf_Internal_Rela * relocs,
4431 Elf_Internal_Sym * local_syms,
4432 asection ** local_sections)
4434 Elf_Internal_Shdr *symtab_hdr;
4435 struct elf_link_hash_entry **sym_hashes;
4436 Elf_Internal_Rela *rel, *relend;
4437 bfd_boolean ret = TRUE; /* Assume success. */
4439 bfd_reloc_status_type r;
4440 const char *errmsg = NULL;
4442 struct elf_nds32_link_hash_table *htab;
4444 bfd_vma *local_got_offsets;
4445 asection *sgot, *splt, *sreloc;
4446 bfd_vma high_address;
4447 struct elf_nds32_link_hash_table *table;
4448 int eliminate_gc_relocs;
4449 bfd_vma fpbase_addr;
4451 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4452 sym_hashes = elf_sym_hashes (input_bfd);
4453 htab = nds32_elf_hash_table (info);
4454 high_address = bfd_get_section_limit (input_bfd, input_section);
4456 dynobj = htab->root.dynobj;
4457 local_got_offsets = elf_local_got_offsets (input_bfd);
4459 sgot = htab->root.sgot;
4460 splt = htab->root.splt;
4464 relend = relocs + input_section->reloc_count;
4466 table = nds32_elf_hash_table (info);
4467 eliminate_gc_relocs = table->eliminate_gc_relocs;
4468 /* By this time, we can adjust the value of _SDA_BASE_. */
4469 if ((!bfd_link_relocatable (info)))
4471 is_SDA_BASE_set = 1;
4472 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
4473 if (r != bfd_reloc_ok)
4477 if (is_ITB_BASE_set == 0)
4479 /* Set the _ITB_BASE_. */
4480 if (!nds32_elf_ex9_itb_base (info))
4482 _bfd_error_handler (_("%B: error: Cannot set _ITB_BASE_"),
4484 bfd_set_error (bfd_error_bad_value);
4488 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
4489 if (!nds32_elf_ifc_reloc ())
4490 _bfd_error_handler (_("error: IFC relocation error."));
4492 /* Relocation for .ex9.itable. */
4493 if (table->target_optimize & NDS32_RELAX_EX9_ON
4494 || (table->ex9_import_file && table->update_ex9_table))
4495 nds32_elf_ex9_reloc_jmp (info);
4497 /* Use gp as fp to prevent truncated fit. Because in relaxation time
4498 the fp value is set as gp, and it has be reverted for instruction
4500 fpbase_addr = elf_gp (output_bfd);
4502 for (rel = relocs; rel < relend; rel++)
4504 enum elf_nds32_reloc_type r_type;
4505 reloc_howto_type *howto = NULL;
4506 unsigned long r_symndx;
4507 struct elf_link_hash_entry *h = NULL;
4508 Elf_Internal_Sym *sym = NULL;
4512 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
4513 ensure it's zero (we use REL relocs, not RELA). Therefore this
4514 should be assigning zero to `addend', but for clarity we use
4517 bfd_vma addend = rel->r_addend;
4518 bfd_vma offset = rel->r_offset;
4520 r_type = ELF32_R_TYPE (rel->r_info);
4521 if (r_type >= R_NDS32_max)
4523 /* xgettext:c-format */
4524 _bfd_error_handler (_("%B: error: unknown relocation type %d."),
4526 bfd_set_error (bfd_error_bad_value);
4531 if (r_type == R_NDS32_GNU_VTENTRY
4532 || r_type == R_NDS32_GNU_VTINHERIT
4533 || r_type == R_NDS32_NONE
4534 || r_type == R_NDS32_RELA_GNU_VTENTRY
4535 || r_type == R_NDS32_RELA_GNU_VTINHERIT
4536 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
4537 || r_type == R_NDS32_DATA
4538 || r_type == R_NDS32_TRAN
4539 || (r_type >= R_NDS32_LONGCALL4 && r_type <= R_NDS32_LONGJUMP7))
4542 /* If we enter the fp-as-gp region. Resolve the address
4544 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
4545 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4549 /* Distance to relocation of best fp-base is encoded in R_SYM. */
4550 dist = rel->r_addend >> 16;
4551 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
4552 local_syms, symtab_hdr);
4554 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
4555 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4557 fpbase_addr = elf_gp (output_bfd);
4560 if (((r_type >= R_NDS32_DWARF2_OP1_RELA
4561 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4562 || r_type >= R_NDS32_RELAX_ENTRY) && !bfd_link_relocatable (info))
4565 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
4566 r_symndx = ELF32_R_SYM (rel->r_info);
4568 /* This is a final link. */
4573 if (r_symndx < symtab_hdr->sh_info)
4576 sym = local_syms + r_symndx;
4577 sec = local_sections[r_symndx];
4579 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4580 addend = rel->r_addend;
4584 /* External symbol. */
4585 bfd_boolean warned, ignored, unresolved_reloc;
4586 int symndx = r_symndx - symtab_hdr->sh_info;
4588 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4589 r_symndx, symtab_hdr, sym_hashes, h, sec,
4590 relocation, unresolved_reloc, warned,
4593 /* la $fp, _FP_BASE_ is per-function (region).
4594 Handle it specially. */
4595 switch ((int) r_type)
4597 case R_NDS32_SDA19S0_RELA:
4598 case R_NDS32_SDA15S0_RELA:
4599 case R_NDS32_20_RELA:
4600 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
4603 relocation = fpbase_addr;
4610 if (bfd_link_relocatable (info))
4612 /* This is a relocatable link. We don't have to change
4613 anything, unless the reloc is against a section symbol,
4614 in which case we have to adjust according to where the
4615 section symbol winds up in the output section. */
4616 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4617 rel->r_addend += sec->output_offset + sym->st_value;
4622 /* Sanity check the address. */
4623 if (offset > high_address)
4625 r = bfd_reloc_outofrange;
4629 if ((r_type >= R_NDS32_DWARF2_OP1_RELA
4630 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4631 || r_type >= R_NDS32_RELAX_ENTRY)
4634 switch ((int) r_type)
4636 case R_NDS32_GOTOFF:
4637 /* Relocation is relative to the start of the global offset
4638 table (for ld24 rx, #uimm24), e.g. access at label+addend
4640 ld24 rx. #label@GOTOFF + addend
4642 case R_NDS32_GOTOFF_HI20:
4643 case R_NDS32_GOTOFF_LO12:
4644 case R_NDS32_GOTOFF_LO15:
4645 case R_NDS32_GOTOFF_LO19:
4646 BFD_ASSERT (sgot != NULL);
4648 relocation -= elf_gp (output_bfd);
4651 case R_NDS32_9_PLTREL:
4652 case R_NDS32_25_PLTREL:
4653 /* Relocation is to the entry for this symbol in the
4654 procedure linkage table. */
4656 /* The native assembler will generate a 25_PLTREL reloc
4657 for a local symbol if you assemble a call from one
4658 section to another when using -K pic. */
4662 if (h->forced_local)
4665 /* We didn't make a PLT entry for this symbol. This
4666 happens when statically linking PIC code, or when
4667 using -Bsymbolic. */
4668 if (h->plt.offset == (bfd_vma) - 1)
4671 relocation = (splt->output_section->vma
4672 + splt->output_offset + h->plt.offset);
4675 case R_NDS32_PLT_GOTREL_HI20:
4676 case R_NDS32_PLT_GOTREL_LO12:
4677 case R_NDS32_PLT_GOTREL_LO15:
4678 case R_NDS32_PLT_GOTREL_LO19:
4679 case R_NDS32_PLT_GOTREL_LO20:
4680 if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
4682 /* We didn't make a PLT entry for this symbol. This
4683 happens when statically linking PIC code, or when
4684 using -Bsymbolic. */
4685 relocation -= elf_gp (output_bfd);
4689 relocation = (splt->output_section->vma
4690 + splt->output_offset + h->plt.offset);
4692 relocation -= elf_gp (output_bfd);
4695 case R_NDS32_PLTREL_HI20:
4696 case R_NDS32_PLTREL_LO12:
4698 /* Relocation is to the entry for this symbol in the
4699 procedure linkage table. */
4701 /* The native assembler will generate a 25_PLTREL reloc
4702 for a local symbol if you assemble a call from one
4703 section to another when using -K pic. */
4707 if (h->forced_local)
4710 if (h->plt.offset == (bfd_vma) - 1)
4711 /* We didn't make a PLT entry for this symbol. This
4712 happens when statically linking PIC code, or when
4713 using -Bsymbolic. */
4719 relocation = (splt->output_section->vma
4720 + splt->output_offset
4721 + h->plt.offset + 4)
4722 - (input_section->output_section->vma
4723 + input_section->output_offset
4728 case R_NDS32_GOTPC20:
4729 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4730 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
4731 relocation = elf_gp (output_bfd);
4734 case R_NDS32_GOTPC_HI20:
4735 case R_NDS32_GOTPC_LO12:
4737 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4739 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
4740 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4743 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
4744 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4746 relocation = elf_gp (output_bfd);
4747 relocation -= (input_section->output_section->vma
4748 + input_section->output_offset + rel->r_offset);
4754 case R_NDS32_GOT_HI20:
4755 case R_NDS32_GOT_LO12:
4756 case R_NDS32_GOT_LO15:
4757 case R_NDS32_GOT_LO19:
4758 /* Relocation is to the entry for this symbol in the global
4760 BFD_ASSERT (sgot != NULL);
4767 off = h->got.offset;
4768 BFD_ASSERT (off != (bfd_vma) - 1);
4769 dyn = htab->root.dynamic_sections_created;
4770 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4771 bfd_link_pic (info),
4773 || (bfd_link_pic (info)
4776 || h->forced_local) && h->def_regular))
4778 /* This is actually a static link, or it is a
4779 -Bsymbolic link and the symbol is defined
4780 locally, or the symbol was forced to be local
4781 because of a version file. We must initialize
4782 this entry in the global offset table. Since the
4783 offset must always be a multiple of 4, we use the
4784 least significant bit to record whether we have
4785 initialized it already.
4787 When doing a dynamic link, we create a .rela.got
4788 relocation entry to initialize the value. This
4789 is done in the finish_dynamic_symbol routine. */
4794 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4798 relocation = sgot->output_section->vma + sgot->output_offset + off
4799 - elf_gp (output_bfd);
4806 BFD_ASSERT (local_got_offsets != NULL
4807 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4809 off = local_got_offsets[r_symndx];
4811 /* The offset must always be a multiple of 4. We use
4812 the least significant bit to record whether we have
4813 already processed this entry. */
4818 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4820 if (bfd_link_pic (info))
4823 Elf_Internal_Rela outrel;
4825 /* We need to generate a R_NDS32_RELATIVE reloc
4826 for the dynamic linker. */
4827 srelgot = htab->root.srelgot;
4828 BFD_ASSERT (srelgot != NULL);
4830 outrel.r_offset = (elf_gp (output_bfd)
4831 + sgot->output_offset + off);
4832 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4833 outrel.r_addend = relocation;
4834 loc = srelgot->contents;
4836 srelgot->reloc_count * sizeof (Elf32_External_Rela);
4837 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4838 ++srelgot->reloc_count;
4840 local_got_offsets[r_symndx] |= 1;
4842 relocation = sgot->output_section->vma + sgot->output_offset + off
4843 - elf_gp (output_bfd);
4848 case R_NDS32_16_RELA:
4849 case R_NDS32_20_RELA:
4850 case R_NDS32_5_RELA:
4851 case R_NDS32_32_RELA:
4852 case R_NDS32_9_PCREL_RELA:
4853 case R_NDS32_WORD_9_PCREL_RELA:
4854 case R_NDS32_10_UPCREL_RELA:
4855 case R_NDS32_15_PCREL_RELA:
4856 case R_NDS32_17_PCREL_RELA:
4857 case R_NDS32_25_PCREL_RELA:
4858 case R_NDS32_HI20_RELA:
4859 case R_NDS32_LO12S3_RELA:
4860 case R_NDS32_LO12S2_RELA:
4861 case R_NDS32_LO12S2_DP_RELA:
4862 case R_NDS32_LO12S2_SP_RELA:
4863 case R_NDS32_LO12S1_RELA:
4864 case R_NDS32_LO12S0_RELA:
4865 case R_NDS32_LO12S0_ORI_RELA:
4866 if (bfd_link_pic (info) && r_symndx != 0
4867 && (input_section->flags & SEC_ALLOC) != 0
4868 && (eliminate_gc_relocs == 0
4869 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
4870 && ((r_type != R_NDS32_9_PCREL_RELA
4871 && r_type != R_NDS32_WORD_9_PCREL_RELA
4872 && r_type != R_NDS32_10_UPCREL_RELA
4873 && r_type != R_NDS32_15_PCREL_RELA
4874 && r_type != R_NDS32_17_PCREL_RELA
4875 && r_type != R_NDS32_25_PCREL_RELA
4876 && !(r_type == R_NDS32_32_RELA
4877 && strcmp (input_section->name, ".eh_frame") == 0))
4878 || (h != NULL && h->dynindx != -1
4879 && (!info->symbolic || !h->def_regular))))
4881 Elf_Internal_Rela outrel;
4882 bfd_boolean skip, relocate;
4885 /* When generating a shared object, these relocations
4886 are copied into the output file to be resolved at run
4893 name = bfd_elf_string_from_elf_section
4894 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
4895 elf_section_data (input_section)->rela.hdr->sh_name);
4899 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4900 && strcmp (bfd_get_section_name (input_bfd,
4904 sreloc = bfd_get_section_by_name (dynobj, name);
4905 BFD_ASSERT (sreloc != NULL);
4911 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4915 if (outrel.r_offset == (bfd_vma) - 1)
4917 else if (outrel.r_offset == (bfd_vma) - 2)
4918 skip = TRUE, relocate = TRUE;
4919 outrel.r_offset += (input_section->output_section->vma
4920 + input_section->output_offset);
4923 memset (&outrel, 0, sizeof outrel);
4924 else if (r_type == R_NDS32_17_PCREL_RELA
4925 || r_type == R_NDS32_15_PCREL_RELA
4926 || r_type == R_NDS32_25_PCREL_RELA)
4928 BFD_ASSERT (h != NULL && h->dynindx != -1);
4929 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4930 outrel.r_addend = rel->r_addend;
4934 /* h->dynindx may be -1 if this symbol was marked to
4937 || ((info->symbolic || h->dynindx == -1)
4941 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4942 outrel.r_addend = relocation + rel->r_addend;
4946 BFD_ASSERT (h->dynindx != -1);
4947 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4948 outrel.r_addend = rel->r_addend;
4952 loc = sreloc->contents;
4953 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
4954 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4955 ++sreloc->reloc_count;
4957 /* If this reloc is against an external symbol, we do
4958 not want to fiddle with the addend. Otherwise, we
4959 need to include the symbol value so that it becomes
4960 an addend for the dynamic reloc. */
4966 case R_NDS32_25_ABS_RELA:
4967 if (bfd_link_pic (info))
4970 (_("%B: warning: cannot deal R_NDS32_25_ABS_RELA in shared "
4971 "mode."), input_bfd);
4976 case R_NDS32_9_PCREL:
4977 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
4979 sec, relocation, addend);
4984 Elf_Internal_Rela *lorel;
4986 /* We allow an arbitrary number of HI20 relocs before the
4987 LO12 reloc. This permits gcc to emit the HI and LO relocs
4989 for (lorel = rel + 1;
4991 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
4994 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
4995 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
4996 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
4997 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
4999 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5000 contents, relocation + addend);
5004 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5005 contents, offset, relocation,
5011 case R_NDS32_GOT17S2_RELA:
5012 case R_NDS32_GOT15S2_RELA:
5016 BFD_ASSERT (sgot != NULL);
5022 off = h->got.offset;
5023 BFD_ASSERT (off != (bfd_vma) - 1);
5025 dyn = htab->root.dynamic_sections_created;
5026 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5027 (dyn, bfd_link_pic (info), h)
5028 || (bfd_link_pic (info)
5034 /* This is actually a static link, or it is a
5035 -Bsymbolic link and the symbol is defined
5036 locally, or the symbol was forced to be local
5037 because of a version file. We must initialize
5038 this entry in the global offset table. Since the
5039 offset must always be a multiple of 4, we use the
5040 least significant bit to record whether we have
5041 initialized it already.
5043 When doing a dynamic link, we create a .rela.got
5044 relocation entry to initialize the value. This
5045 is done in the finish_dynamic_symbol routine. */
5050 bfd_put_32 (output_bfd, relocation,
5051 sgot->contents + off);
5060 BFD_ASSERT (local_got_offsets != NULL
5061 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5063 off = local_got_offsets[r_symndx];
5065 /* The offset must always be a multiple of 4. We use
5066 the least significant bit to record whether we have
5067 already processed this entry. */
5072 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5074 if (bfd_link_pic (info))
5077 Elf_Internal_Rela outrel;
5079 /* We need to generate a R_NDS32_RELATIVE reloc
5080 for the dynamic linker. */
5081 srelgot = htab->root.srelgot;
5082 BFD_ASSERT (srelgot != NULL);
5084 outrel.r_offset = (elf_gp (output_bfd)
5085 + sgot->output_offset + off);
5086 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5087 outrel.r_addend = relocation;
5088 loc = srelgot->contents;
5090 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5091 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5092 ++srelgot->reloc_count;
5094 local_got_offsets[r_symndx] |= 1;
5097 relocation = sgot->output_section->vma + sgot->output_offset + off
5098 - elf_gp (output_bfd);
5100 if (relocation & align)
5102 /* Incorrect alignment. */
5104 (_("%B: warning: unaligned access to GOT entry."), input_bfd);
5106 r = bfd_reloc_dangerous;
5111 case R_NDS32_SDA16S3_RELA:
5112 case R_NDS32_SDA15S3_RELA:
5113 case R_NDS32_SDA15S3:
5117 case R_NDS32_SDA17S2_RELA:
5118 case R_NDS32_SDA15S2_RELA:
5119 case R_NDS32_SDA12S2_SP_RELA:
5120 case R_NDS32_SDA12S2_DP_RELA:
5121 case R_NDS32_SDA15S2:
5122 case R_NDS32_SDA_FP7U2_RELA:
5126 case R_NDS32_SDA18S1_RELA:
5127 case R_NDS32_SDA15S1_RELA:
5128 case R_NDS32_SDA15S1:
5132 case R_NDS32_SDA19S0_RELA:
5133 case R_NDS32_SDA15S0_RELA:
5134 case R_NDS32_SDA15S0:
5138 BFD_ASSERT (sec != NULL);
5140 /* If the symbol is in the abs section, the out_bfd will be null.
5141 This happens when the relocation has a symbol@GOTOFF. */
5142 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5143 if (r != bfd_reloc_ok)
5146 (_("%B: warning: relocate SDA_BASE failed."), input_bfd);
5151 /* At this point `relocation' contains the object's
5153 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5155 relocation -= fpbase_addr;
5159 /* Now it contains the offset from _SDA_BASE_. */
5161 /* Make sure alignment is correct. */
5163 if (relocation & align)
5165 /* Incorrect alignment. */
5167 /* xgettext:c-format */
5168 (_("%B(%A): warning: unaligned small data access of type %d."),
5169 input_bfd, input_section, r_type);
5176 case R_NDS32_17IFC_PCREL_RELA:
5177 case R_NDS32_10IFCU_PCREL_RELA:
5181 case R_NDS32_TLS_LE_HI20:
5182 case R_NDS32_TLS_LE_LO12:
5183 case R_NDS32_TLS_LE_20:
5184 case R_NDS32_TLS_LE_15S0:
5185 case R_NDS32_TLS_LE_15S1:
5186 case R_NDS32_TLS_LE_15S2:
5187 if (elf_hash_table (info)->tls_sec != NULL)
5188 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5190 case R_NDS32_TLS_IE_HI20:
5191 case R_NDS32_TLS_IE_LO12S2:
5193 /* Relocation is to the entry for this symbol in the global
5195 unsigned int tls_type;
5197 Elf_Internal_Rela outrel;
5202 BFD_ASSERT (sgot != NULL);
5207 off = h->got.offset;
5208 BFD_ASSERT (off != (bfd_vma) - 1);
5209 dyn = htab->root.dynamic_sections_created;
5210 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5211 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5212 && (!bfd_link_pic (info)
5213 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5218 /* Never happen currently. */
5219 BFD_ASSERT (local_got_offsets != NULL
5220 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5222 off = local_got_offsets[r_symndx];
5224 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5226 relocation = sgot->output_section->vma + sgot->output_offset + off;
5228 if (r_type == R_NDS32_TLS_IE_LO12S2)
5231 /* The offset must always be a multiple of 4. We use
5232 the least significant bit to record whether we have
5233 already processed this entry. */
5238 bfd_boolean need_relocs = FALSE;
5239 srelgot = htab->root.srelgot;
5240 if ((bfd_link_pic (info) || indx != 0)
5242 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5243 || h->root.type != bfd_link_hash_undefweak))
5246 BFD_ASSERT (srelgot != NULL);
5248 if (tls_type & GOT_TLS_IE)
5252 if (h->dynindx == 0)
5253 outrel.r_addend = relocation - dtpoff_base (info);
5255 outrel.r_addend = 0;
5256 outrel.r_offset = (sgot->output_section->vma
5257 + sgot->output_offset
5260 ELF32_R_INFO (h->dynindx, R_NDS32_TLS_TPOFF);
5262 loc = srelgot->contents;
5264 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5265 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5266 ++srelgot->reloc_count;
5269 bfd_put_32 (output_bfd, h->root.u.def.value - TP_OFFSET,
5270 sgot->contents + off);
5276 /* DON'T fall through. */
5279 /* OLD_NDS32_RELOC. */
5281 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5282 contents, offset, relocation, addend);
5286 switch ((int) r_type)
5288 case R_NDS32_20_RELA:
5289 case R_NDS32_5_RELA:
5290 case R_NDS32_9_PCREL_RELA:
5291 case R_NDS32_WORD_9_PCREL_RELA:
5292 case R_NDS32_10_UPCREL_RELA:
5293 case R_NDS32_15_PCREL_RELA:
5294 case R_NDS32_17_PCREL_RELA:
5295 case R_NDS32_25_PCREL_RELA:
5296 case R_NDS32_25_ABS_RELA:
5297 case R_NDS32_HI20_RELA:
5298 case R_NDS32_LO12S3_RELA:
5299 case R_NDS32_LO12S2_RELA:
5300 case R_NDS32_LO12S2_DP_RELA:
5301 case R_NDS32_LO12S2_SP_RELA:
5302 case R_NDS32_LO12S1_RELA:
5303 case R_NDS32_LO12S0_RELA:
5304 case R_NDS32_LO12S0_ORI_RELA:
5305 case R_NDS32_SDA16S3_RELA:
5306 case R_NDS32_SDA17S2_RELA:
5307 case R_NDS32_SDA18S1_RELA:
5308 case R_NDS32_SDA19S0_RELA:
5309 case R_NDS32_SDA15S3_RELA:
5310 case R_NDS32_SDA15S2_RELA:
5311 case R_NDS32_SDA12S2_DP_RELA:
5312 case R_NDS32_SDA12S2_SP_RELA:
5313 case R_NDS32_SDA15S1_RELA:
5314 case R_NDS32_SDA15S0_RELA:
5315 case R_NDS32_SDA_FP7U2_RELA:
5316 case R_NDS32_9_PLTREL:
5317 case R_NDS32_25_PLTREL:
5319 case R_NDS32_GOT_HI20:
5320 case R_NDS32_GOT_LO12:
5321 case R_NDS32_GOT_LO15:
5322 case R_NDS32_GOT_LO19:
5323 case R_NDS32_GOT15S2_RELA:
5324 case R_NDS32_GOT17S2_RELA:
5325 case R_NDS32_GOTPC20:
5326 case R_NDS32_GOTPC_HI20:
5327 case R_NDS32_GOTPC_LO12:
5328 case R_NDS32_GOTOFF:
5329 case R_NDS32_GOTOFF_HI20:
5330 case R_NDS32_GOTOFF_LO12:
5331 case R_NDS32_GOTOFF_LO15:
5332 case R_NDS32_GOTOFF_LO19:
5333 case R_NDS32_PLTREL_HI20:
5334 case R_NDS32_PLTREL_LO12:
5335 case R_NDS32_PLT_GOTREL_HI20:
5336 case R_NDS32_PLT_GOTREL_LO12:
5337 case R_NDS32_PLT_GOTREL_LO15:
5338 case R_NDS32_PLT_GOTREL_LO19:
5339 case R_NDS32_PLT_GOTREL_LO20:
5340 case R_NDS32_17IFC_PCREL_RELA:
5341 case R_NDS32_10IFCU_PCREL_RELA:
5342 case R_NDS32_TLS_LE_HI20:
5343 case R_NDS32_TLS_LE_LO12:
5344 case R_NDS32_TLS_IE_HI20:
5345 case R_NDS32_TLS_IE_LO12S2:
5346 case R_NDS32_TLS_LE_20:
5347 case R_NDS32_TLS_LE_15S0:
5348 case R_NDS32_TLS_LE_15S1:
5349 case R_NDS32_TLS_LE_15S2:
5350 /* Instruction related relocs must handle endian properly. */
5351 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
5352 r = nds32_elf_final_link_relocate (howto, input_bfd,
5353 input_section, contents,
5354 rel->r_offset, relocation,
5359 /* All other relocs can use default handler. */
5360 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5361 contents, rel->r_offset,
5362 relocation, rel->r_addend);
5368 if (r != bfd_reloc_ok)
5370 /* FIXME: This should be generic enough to go in a utility. */
5374 name = h->root.root.string;
5377 name = bfd_elf_string_from_elf_section
5378 (input_bfd, symtab_hdr->sh_link, sym->st_name);
5379 if (name == NULL || *name == '\0')
5380 name = bfd_section_name (input_bfd, sec);
5388 case bfd_reloc_overflow:
5389 (*info->callbacks->reloc_overflow)
5390 (info, (h ? &h->root : NULL), name, howto->name,
5391 (bfd_vma) 0, input_bfd, input_section, offset);
5394 case bfd_reloc_undefined:
5395 (*info->callbacks->undefined_symbol)
5396 (info, name, input_bfd, input_section, offset, TRUE);
5399 case bfd_reloc_outofrange:
5400 errmsg = _("internal error: out of range error");
5403 case bfd_reloc_notsupported:
5404 errmsg = _("internal error: unsupported relocation error");
5407 case bfd_reloc_dangerous:
5408 errmsg = _("internal error: dangerous error");
5412 errmsg = _("internal error: unknown error");
5416 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
5417 input_section, offset);
5426 /* Finish up dynamic symbol handling. We set the contents of various
5427 dynamic sections here. */
5430 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5431 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
5433 struct elf_nds32_link_hash_table *htab;
5436 htab = nds32_elf_hash_table (info);
5438 if (h->plt.offset != (bfd_vma) - 1)
5446 bfd_vma local_plt_offset;
5447 Elf_Internal_Rela rela;
5449 /* This symbol has an entry in the procedure linkage table. Set
5452 BFD_ASSERT (h->dynindx != -1);
5454 splt = htab->root.splt;
5455 sgot = htab->root.sgotplt;
5456 srela = htab->root.srelplt;
5457 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
5459 /* Get the index in the procedure linkage table which
5460 corresponds to this symbol. This is the index of this symbol
5461 in all the symbols for which we are making plt entries. The
5462 first entry in the procedure linkage table is reserved. */
5463 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
5465 /* Get the offset into the .got table of the entry that
5466 corresponds to this function. Each .got entry is 4 bytes.
5467 The first three are reserved. */
5468 got_offset = (plt_index + 3) * 4;
5470 /* Fill in the entry in the procedure linkage table. */
5471 if (!bfd_link_pic (info))
5475 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
5476 + sgot->output_offset + got_offset) >> 12)
5478 bfd_putb32 (insn, splt->contents + h->plt.offset);
5480 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
5481 + sgot->output_offset + got_offset) & 0x0fff)
5483 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5485 insn = PLT_ENTRY_WORD2;
5486 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5488 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
5489 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5491 insn = PLT_ENTRY_WORD4
5492 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
5493 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5494 local_plt_offset = 12;
5498 /* sda_base must be set at this time. */
5502 /* FIXME, sda_base is 65536, it will damage opcode. */
5503 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5504 offset = sgot->output_section->vma + sgot->output_offset + got_offset
5505 - elf_gp (output_bfd);
5506 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
5507 bfd_putb32 (insn, splt->contents + h->plt.offset);
5509 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
5510 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5512 insn = PLT_PIC_ENTRY_WORD2;
5513 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5515 insn = PLT_PIC_ENTRY_WORD3;
5516 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5518 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
5519 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5521 insn = PLT_PIC_ENTRY_WORD5
5522 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
5523 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
5525 local_plt_offset = 16;
5528 /* Fill in the entry in the global offset table,
5529 so it will fall through to the next instruction for the first time. */
5530 bfd_put_32 (output_bfd,
5531 (splt->output_section->vma + splt->output_offset
5532 + h->plt.offset + local_plt_offset),
5533 sgot->contents + got_offset);
5535 /* Fill in the entry in the .rela.plt section. */
5536 rela.r_offset = (sgot->output_section->vma
5537 + sgot->output_offset + got_offset);
5538 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
5540 loc = srela->contents;
5541 loc += plt_index * sizeof (Elf32_External_Rela);
5542 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5544 if (!h->def_regular)
5546 /* Mark the symbol as undefined, rather than as defined in
5547 the .plt section. Leave the value alone. */
5548 sym->st_shndx = SHN_UNDEF;
5549 if (!h->ref_regular_nonweak)
5554 if (h->got.offset != (bfd_vma) - 1)
5558 Elf_Internal_Rela rela;
5560 /* This symbol has an entry in the global offset table.
5563 sgot = htab->root.sgot;
5564 srela = htab->root.srelgot;
5565 BFD_ASSERT (sgot != NULL && srela != NULL);
5567 rela.r_offset = (sgot->output_section->vma
5568 + sgot->output_offset + (h->got.offset & ~1));
5570 /* If this is a -Bsymbolic link, and the symbol is defined
5571 locally, we just want to emit a RELATIVE reloc. Likewise if
5572 the symbol was forced to be local because of a version file.
5573 The entry in the global offset table will already have been
5574 initialized in the relocate_section function. */
5575 if (bfd_link_pic (info)
5577 || h->dynindx == -1 || h->forced_local) && h->def_regular)
5579 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5580 rela.r_addend = (h->root.u.def.value
5581 + h->root.u.def.section->output_section->vma
5582 + h->root.u.def.section->output_offset);
5586 BFD_ASSERT ((h->got.offset & 1) == 0);
5587 bfd_put_32 (output_bfd, (bfd_vma) 0,
5588 sgot->contents + h->got.offset);
5589 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
5593 loc = srela->contents;
5594 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
5595 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5596 ++srela->reloc_count;
5602 Elf_Internal_Rela rela;
5604 /* This symbols needs a copy reloc. Set it up. */
5606 BFD_ASSERT (h->dynindx != -1
5607 && (h->root.type == bfd_link_hash_defined
5608 || h->root.type == bfd_link_hash_defweak));
5610 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
5611 BFD_ASSERT (s != NULL);
5613 rela.r_offset = (h->root.u.def.value
5614 + h->root.u.def.section->output_section->vma
5615 + h->root.u.def.section->output_offset);
5616 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
5619 loc += s->reloc_count * sizeof (Elf32_External_Rela);
5620 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5624 /* Mark some specially defined symbols as absolute. */
5625 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5626 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5627 sym->st_shndx = SHN_ABS;
5633 /* Finish up the dynamic sections. */
5636 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
5638 struct elf_nds32_link_hash_table *htab;
5643 htab = nds32_elf_hash_table (info);
5644 dynobj = htab->root.dynobj;
5646 sgot = htab->root.sgotplt;
5647 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5649 if (htab->root.dynamic_sections_created)
5652 Elf32_External_Dyn *dyncon, *dynconend;
5654 BFD_ASSERT (sgot != NULL && sdyn != NULL);
5656 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5657 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5659 for (; dyncon < dynconend; dyncon++)
5661 Elf_Internal_Dyn dyn;
5664 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5672 s = htab->root.sgotplt;
5675 s = htab->root.srelplt;
5677 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5678 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5682 s = htab->root.srelplt;
5683 dyn.d_un.d_val = s->size;
5684 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5689 /* Fill in the first entry in the procedure linkage table. */
5690 splt = htab->root.splt;
5691 if (splt && splt->size > 0)
5693 if (bfd_link_pic (info))
5698 /* FIXME, sda_base is 65536, it will damage opcode. */
5699 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5700 offset = sgot->output_section->vma + sgot->output_offset + 4
5701 - elf_gp (output_bfd);
5702 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
5703 bfd_putb32 (insn, splt->contents);
5705 /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
5706 /* here has a typo? */
5707 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
5708 bfd_putb32 (insn, splt->contents + 4);
5710 insn = PLT0_PIC_ENTRY_WORD2;
5711 bfd_putb32 (insn, splt->contents + 8);
5713 insn = PLT0_PIC_ENTRY_WORD3;
5714 bfd_putb32 (insn, splt->contents + 12);
5716 insn = PLT0_PIC_ENTRY_WORD4;
5717 bfd_putb32 (insn, splt->contents + 16);
5719 insn = PLT0_PIC_ENTRY_WORD5;
5720 bfd_putb32 (insn, splt->contents + 20);
5727 /* addr = .got + 4 */
5728 addr = sgot->output_section->vma + sgot->output_offset + 4;
5729 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
5730 bfd_putb32 (insn, splt->contents);
5732 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
5733 bfd_putb32 (insn, splt->contents + 4);
5735 insn = PLT0_ENTRY_WORD2;
5736 bfd_putb32 (insn, splt->contents + 8);
5738 insn = PLT0_ENTRY_WORD3;
5739 bfd_putb32 (insn, splt->contents + 12);
5741 insn = PLT0_ENTRY_WORD4;
5742 bfd_putb32 (insn, splt->contents + 16);
5745 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
5750 /* Fill in the first three entries in the global offset table. */
5751 if (sgot && sgot->size > 0)
5754 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5756 bfd_put_32 (output_bfd,
5757 sdyn->output_section->vma + sdyn->output_offset,
5759 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5760 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5762 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5769 /* Set the right machine number. */
5772 nds32_elf_object_p (bfd *abfd)
5774 static unsigned int cur_arch = 0;
5776 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
5778 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
5779 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
5786 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
5789 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
5791 case E_NDS_ARCH_STAR_V2_0:
5792 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
5794 case E_NDS_ARCH_STAR_V3_0:
5795 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
5797 case E_NDS_ARCH_STAR_V3_M:
5798 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
5805 /* Store the machine number in the flags field. */
5808 nds32_elf_final_write_processing (bfd *abfd,
5809 bfd_boolean linker ATTRIBUTE_UNUSED)
5812 static unsigned int cur_mach = 0;
5814 if (bfd_mach_n1 != bfd_get_mach (abfd))
5816 cur_mach = bfd_get_mach (abfd);
5822 /* Only happen when object is empty, since the case is abandon. */
5824 val |= E_NDS_ABI_AABI;
5825 val |= E_NDS32_ELF_VER_1_4;
5830 case bfd_mach_n1h_v2:
5831 val = E_NDS_ARCH_STAR_V2_0;
5833 case bfd_mach_n1h_v3:
5834 val = E_NDS_ARCH_STAR_V3_0;
5836 case bfd_mach_n1h_v3m:
5837 val = E_NDS_ARCH_STAR_V3_M;
5844 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
5845 elf_elfheader (abfd)->e_flags |= val;
5848 /* Function to keep NDS32 specific file flags. */
5851 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
5853 BFD_ASSERT (!elf_flags_init (abfd)
5854 || elf_elfheader (abfd)->e_flags == flags);
5856 elf_elfheader (abfd)->e_flags = flags;
5857 elf_flags_init (abfd) = TRUE;
5862 convert_e_flags (unsigned int e_flags, unsigned int arch)
5864 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
5866 /* From 0.9 to 1.0. */
5867 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
5869 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5870 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5871 if (arch == E_NDS_ARCH_STAR_V1_0)
5878 /* From 1.0 to 2.0. */
5879 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
5881 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
5882 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
5884 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5885 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5890 nds32_check_vec_size (bfd *ibfd)
5892 static unsigned int nds32_vec_size = 0;
5894 asection *sec_t = NULL;
5895 bfd_byte *contents = NULL;
5897 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
5899 if (sec_t && sec_t->size >= 4)
5901 /* Get vec_size in file. */
5902 unsigned int flag_t;
5904 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
5905 flag_t = bfd_get_32 (ibfd, contents);
5907 /* The value could only be 4 or 16. */
5909 if (!nds32_vec_size)
5910 /* Set if not set yet. */
5911 nds32_vec_size = (flag_t & 0x3);
5912 else if (nds32_vec_size != (flag_t & 0x3))
5915 /* xgettext:c-format */
5916 (_("%B: ISR vector size mismatch"
5917 " with previous modules, previous %u-byte, current %u-byte"),
5919 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
5920 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
5924 /* Only keep the first vec_size section. */
5925 sec_t->flags |= SEC_EXCLUDE;
5931 /* Merge backend specific data from an object file to the output
5932 object file when linking. */
5935 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5937 bfd *obfd = info->output_bfd;
5940 flagword out_16regs;
5942 flagword out_no_mac;
5944 flagword out_version;
5945 flagword in_version;
5946 flagword out_fpu_config;
5947 flagword in_fpu_config;
5949 /* TODO: Revise to use object-attributes instead. */
5950 if (!nds32_check_vec_size (ibfd))
5953 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5954 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5957 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
5960 (_("%B: warning: Endian mismatch with previous modules."), ibfd);
5962 bfd_set_error (bfd_error_bad_value);
5966 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
5967 if (in_version == E_NDS32_ELF_VER_1_2)
5970 (_("%B: warning: Older version of object file encountered, "
5971 "Please recompile with current tool chain."), ibfd);
5974 /* We may need to merge V1 and V2 arch object files to V2. */
5975 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5976 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5978 /* Need to convert version. */
5979 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5980 == E_NDS_ARCH_STAR_RESERVED)
5982 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5984 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
5985 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5986 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5988 elf_elfheader (obfd)->e_flags =
5989 convert_e_flags (elf_elfheader (obfd)->e_flags,
5990 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
5994 elf_elfheader (ibfd)->e_flags =
5995 convert_e_flags (elf_elfheader (ibfd)->e_flags,
5996 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6000 /* Extract some flags. */
6001 in_flags = elf_elfheader (ibfd)->e_flags
6002 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6003 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6005 /* The following flags need special treatment. */
6006 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6007 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6008 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6010 /* Extract some flags. */
6011 out_flags = elf_elfheader (obfd)->e_flags
6012 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6013 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6015 /* The following flags need special treatment. */
6016 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6017 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6018 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6019 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6020 if (!elf_flags_init (obfd))
6022 /* If the input is the default architecture then do not
6023 bother setting the flags for the output architecture,
6024 instead allow future merges to do this. If no future
6025 merges ever set these flags then they will retain their
6026 unitialised values, which surprise surprise, correspond
6027 to the default values. */
6028 if (bfd_get_arch_info (ibfd)->the_default)
6031 elf_flags_init (obfd) = TRUE;
6032 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6034 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6035 && bfd_get_arch_info (obfd)->the_default)
6037 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6038 bfd_get_mach (ibfd));
6044 /* Check flag compatibility. */
6045 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6048 (_("%B: error: ABI mismatch with previous modules."), ibfd);
6050 bfd_set_error (bfd_error_bad_value);
6054 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6056 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6059 (_("%B: error: Instruction set mismatch with previous modules."), ibfd);
6061 bfd_set_error (bfd_error_bad_value);
6066 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6067 and perf ext1 and DIV are mergerd to perf ext1. */
6068 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6070 elf_elfheader (obfd)->e_flags =
6071 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6072 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6073 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6074 ? E_NDS32_HAS_EXT_INST : 0)
6075 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6076 ? E_NDS32_HAS_EXT_INST : 0)
6077 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6078 | ((in_version > out_version) ? out_version : in_version);
6082 if (in_version != out_version)
6084 /* xgettext:c-format */
6085 (_("%B: warning: Incompatible elf-versions %s and %s."),
6086 ibfd, nds32_elfver_strtab[out_version],
6087 nds32_elfver_strtab[in_version]);
6089 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6090 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6091 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6092 | (in_version > out_version ? out_version : in_version);
6098 /* Display the flags field. */
6101 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6103 FILE *file = (FILE *) ptr;
6105 BFD_ASSERT (abfd != NULL && ptr != NULL);
6107 _bfd_elf_print_private_bfd_data (abfd, ptr);
6109 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6111 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6115 fprintf (file, _(": n1 instructions"));
6118 fprintf (file, _(": n1h instructions"));
6128 nds32_elf_action_discarded (asection *sec)
6132 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6135 return _bfd_elf_default_action_discarded (sec);
6139 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6140 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6141 Elf_Internal_Sym *sym)
6144 switch (ELF32_R_TYPE (rel->r_info))
6146 case R_NDS32_GNU_VTINHERIT:
6147 case R_NDS32_GNU_VTENTRY:
6148 case R_NDS32_RELA_GNU_VTINHERIT:
6149 case R_NDS32_RELA_GNU_VTENTRY:
6153 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6156 /* Look through the relocs for a section during the first phase.
6157 Since we don't do .gots or .plts, we just need to consider the
6158 virtual table relocs for gc. */
6161 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6162 asection *sec, const Elf_Internal_Rela *relocs)
6164 Elf_Internal_Shdr *symtab_hdr;
6165 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6166 const Elf_Internal_Rela *rel;
6167 const Elf_Internal_Rela *rel_end;
6168 struct elf_nds32_link_hash_table *htab;
6170 asection *sreloc = NULL;
6172 if (bfd_link_relocatable (info))
6175 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6176 sym_hashes = elf_sym_hashes (abfd);
6178 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6179 if (!elf_bad_symtab (abfd))
6180 sym_hashes_end -= symtab_hdr->sh_info;
6182 htab = nds32_elf_hash_table (info);
6183 dynobj = htab->root.dynobj;
6185 rel_end = relocs + sec->reloc_count;
6186 for (rel = relocs; rel < rel_end; rel++)
6188 enum elf_nds32_reloc_type r_type;
6189 struct elf_link_hash_entry *h;
6190 unsigned long r_symndx;
6191 int tls_type, old_tls_type;
6193 r_symndx = ELF32_R_SYM (rel->r_info);
6194 r_type = ELF32_R_TYPE (rel->r_info);
6195 if (r_symndx < symtab_hdr->sh_info)
6199 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6200 while (h->root.type == bfd_link_hash_indirect
6201 || h->root.type == bfd_link_hash_warning)
6202 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6205 /* Some relocs require a global offset table. We create
6206 got section here, since these relocation need got section
6207 and it is not created yet. */
6208 if (htab->root.sgot == NULL)
6212 case R_NDS32_GOT_HI20:
6213 case R_NDS32_GOT_LO12:
6214 case R_NDS32_GOT_LO15:
6215 case R_NDS32_GOT_LO19:
6216 case R_NDS32_GOT17S2_RELA:
6217 case R_NDS32_GOT15S2_RELA:
6218 case R_NDS32_GOTOFF:
6219 case R_NDS32_GOTOFF_HI20:
6220 case R_NDS32_GOTOFF_LO12:
6221 case R_NDS32_GOTOFF_LO15:
6222 case R_NDS32_GOTOFF_LO19:
6223 case R_NDS32_GOTPC20:
6224 case R_NDS32_GOTPC_HI20:
6225 case R_NDS32_GOTPC_LO12:
6227 case R_NDS32_TLS_IE_HI20:
6228 case R_NDS32_TLS_IE_LO12S2:
6230 htab->root.dynobj = dynobj = abfd;
6231 if (!_bfd_elf_create_got_section (dynobj, info))
6240 switch ((int) r_type)
6242 case R_NDS32_GOT_HI20:
6243 case R_NDS32_GOT_LO12:
6244 case R_NDS32_GOT_LO15:
6245 case R_NDS32_GOT_LO19:
6247 case R_NDS32_TLS_IE_HI20:
6248 case R_NDS32_TLS_IE_LO12S2:
6251 case R_NDS32_TLS_IE_HI20:
6252 case R_NDS32_TLS_IE_LO12S2:
6253 tls_type = GOT_TLS_IE;
6256 tls_type = GOT_NORMAL;
6261 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
6262 h->got.refcount += 1;
6266 bfd_signed_vma *local_got_refcounts;
6268 /* This is a global offset table entry for a local
6270 local_got_refcounts = elf_local_got_refcounts (abfd);
6271 if (local_got_refcounts == NULL)
6275 size = symtab_hdr->sh_info;
6276 size *= sizeof (bfd_signed_vma);
6277 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
6278 if (local_got_refcounts == NULL)
6280 elf_local_got_refcounts (abfd) = local_got_refcounts;
6282 local_got_refcounts[r_symndx] += 1;
6283 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
6286 /* We will already have issued an error message if there
6287 is a TLS/non-TLS mismatch, based on the symbol
6288 type. So just combine any TLS types needed. */
6289 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
6290 && tls_type != GOT_NORMAL)
6291 tls_type |= old_tls_type;
6293 if (old_tls_type != tls_type)
6296 elf32_nds32_hash_entry (h)->tls_type = tls_type;
6298 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
6301 case R_NDS32_9_PLTREL:
6302 case R_NDS32_25_PLTREL:
6303 case R_NDS32_PLTREL_HI20:
6304 case R_NDS32_PLTREL_LO12:
6305 case R_NDS32_PLT_GOTREL_HI20:
6306 case R_NDS32_PLT_GOTREL_LO12:
6307 case R_NDS32_PLT_GOTREL_LO15:
6308 case R_NDS32_PLT_GOTREL_LO19:
6309 case R_NDS32_PLT_GOTREL_LO20:
6311 /* This symbol requires a procedure linkage table entry. We
6312 actually build the entry in adjust_dynamic_symbol,
6313 because this might be a case of linking PIC code without
6314 linking in any dynamic objects, in which case we don't
6315 need to generate a procedure linkage table after all. */
6317 /* If this is a local symbol, we resolve it directly without
6318 creating a procedure linkage table entry. */
6322 if (h->forced_local)
6325 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
6327 h->plt.refcount += 1;
6330 case R_NDS32_16_RELA:
6331 case R_NDS32_20_RELA:
6332 case R_NDS32_5_RELA:
6333 case R_NDS32_32_RELA:
6334 case R_NDS32_HI20_RELA:
6335 case R_NDS32_LO12S3_RELA:
6336 case R_NDS32_LO12S2_RELA:
6337 case R_NDS32_LO12S2_DP_RELA:
6338 case R_NDS32_LO12S2_SP_RELA:
6339 case R_NDS32_LO12S1_RELA:
6340 case R_NDS32_LO12S0_RELA:
6341 case R_NDS32_LO12S0_ORI_RELA:
6342 case R_NDS32_SDA16S3_RELA:
6343 case R_NDS32_SDA17S2_RELA:
6344 case R_NDS32_SDA18S1_RELA:
6345 case R_NDS32_SDA19S0_RELA:
6346 case R_NDS32_SDA15S3_RELA:
6347 case R_NDS32_SDA15S2_RELA:
6348 case R_NDS32_SDA12S2_DP_RELA:
6349 case R_NDS32_SDA12S2_SP_RELA:
6350 case R_NDS32_SDA15S1_RELA:
6351 case R_NDS32_SDA15S0_RELA:
6352 case R_NDS32_SDA_FP7U2_RELA:
6353 case R_NDS32_15_PCREL_RELA:
6354 case R_NDS32_17_PCREL_RELA:
6355 case R_NDS32_25_PCREL_RELA:
6357 if (h != NULL && !bfd_link_pic (info))
6360 h->plt.refcount += 1;
6363 /* If we are creating a shared library, and this is a reloc against
6364 a global symbol, or a non PC relative reloc against a local
6365 symbol, then we need to copy the reloc into the shared library.
6366 However, if we are linking with -Bsymbolic, we do not need to
6367 copy a reloc against a global symbol which is defined in an
6368 object we are including in the link (i.e., DEF_REGULAR is set).
6369 At this point we have not seen all the input files, so it is
6370 possible that DEF_REGULAR is not set now but will be set later
6371 (it is never cleared). We account for that possibility below by
6372 storing information in the dyn_relocs field of the hash table
6373 entry. A similar situation occurs when creating shared libraries
6374 and symbol visibility changes render the symbol local.
6376 If on the other hand, we are creating an executable, we may need
6377 to keep relocations for symbols satisfied by a dynamic library
6378 if we manage to avoid copy relocs for the symbol. */
6379 if ((bfd_link_pic (info)
6380 && (sec->flags & SEC_ALLOC) != 0
6381 && ((r_type != R_NDS32_25_PCREL_RELA
6382 && r_type != R_NDS32_15_PCREL_RELA
6383 && r_type != R_NDS32_17_PCREL_RELA
6384 && !(r_type == R_NDS32_32_RELA
6385 && strcmp (sec->name, ".eh_frame") == 0))
6388 || h->root.type == bfd_link_hash_defweak
6389 || !h->def_regular))))
6390 || (!bfd_link_pic (info)
6391 && (sec->flags & SEC_ALLOC) != 0
6393 && (h->root.type == bfd_link_hash_defweak
6394 || !h->def_regular)))
6396 struct elf_nds32_dyn_relocs *p;
6397 struct elf_nds32_dyn_relocs **head;
6400 htab->root.dynobj = dynobj = abfd;
6402 /* When creating a shared object, we must copy these
6403 relocs into the output file. We create a reloc
6404 section in dynobj and make room for the reloc. */
6409 name = bfd_elf_string_from_elf_section
6410 (abfd, elf_elfheader (abfd)->e_shstrndx,
6411 elf_section_data (sec)->rela.hdr->sh_name);
6415 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6416 && strcmp (bfd_get_section_name (abfd, sec),
6419 sreloc = bfd_get_section_by_name (dynobj, name);
6424 sreloc = bfd_make_section (dynobj, name);
6425 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6426 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6427 if ((sec->flags & SEC_ALLOC) != 0)
6428 flags |= SEC_ALLOC | SEC_LOAD;
6430 || !bfd_set_section_flags (dynobj, sreloc, flags)
6431 || !bfd_set_section_alignment (dynobj, sreloc, 2))
6434 elf_section_type (sreloc) = SHT_RELA;
6436 elf_section_data (sec)->sreloc = sreloc;
6439 /* If this is a global symbol, we count the number of
6440 relocations we need for this symbol. */
6442 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
6448 Elf_Internal_Sym *isym;
6449 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
6453 /* Track dynamic relocs needed for local syms too. */
6454 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6458 vpp = &elf_section_data (s)->local_dynrel;
6459 head = (struct elf_nds32_dyn_relocs **) vpp;
6463 if (p == NULL || p->sec != sec)
6465 bfd_size_type amt = sizeof (*p);
6466 p = (struct elf_nds32_dyn_relocs *) bfd_alloc (dynobj, amt);
6477 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
6478 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6479 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
6484 /* This relocation describes the C++ object vtable hierarchy.
6485 Reconstruct it for later use during GC. */
6486 case R_NDS32_RELA_GNU_VTINHERIT:
6487 case R_NDS32_GNU_VTINHERIT:
6488 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6492 /* This relocation describes which C++ vtable entries are actually
6493 used. Record for later use during GC. */
6494 case R_NDS32_GNU_VTENTRY:
6495 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6498 case R_NDS32_RELA_GNU_VTENTRY:
6499 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6508 /* Write VAL in uleb128 format to P, returning a pointer to the
6510 This code is copied from elf-attr.c. */
6513 write_uleb128 (bfd_byte *p, unsigned int val)
6528 static bfd_signed_vma
6529 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
6530 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
6531 int *pic_ext_target)
6533 bfd_signed_vma foff;
6534 bfd_vma symval, addend;
6537 /* Get the value of the symbol referred to by the reloc. */
6538 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6540 Elf_Internal_Sym *isym;
6542 /* A local symbol. */
6543 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6545 if (isym->st_shndx == SHN_UNDEF)
6546 sym_sec = bfd_und_section_ptr;
6547 else if (isym->st_shndx == SHN_ABS)
6548 sym_sec = bfd_abs_section_ptr;
6549 else if (isym->st_shndx == SHN_COMMON)
6550 sym_sec = bfd_com_section_ptr;
6552 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6553 symval = isym->st_value + sym_sec->output_section->vma
6554 + sym_sec->output_offset;
6559 struct elf_link_hash_entry *h;
6562 /* An external symbol. */
6563 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6564 h = elf_sym_hashes (abfd)[indx];
6565 BFD_ASSERT (h != NULL);
6567 if (h->root.type != bfd_link_hash_defined
6568 && h->root.type != bfd_link_hash_defweak)
6569 /* This appears to be a reference to an undefined
6570 symbol. Just ignore it--it will be caught by the
6571 regular reloc processing. */
6573 owner = h->root.u.def.section->owner;
6574 if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
6575 *pic_ext_target = 1;
6577 if (h->root.u.def.section->flags & SEC_MERGE)
6579 sym_sec = h->root.u.def.section;
6580 symval = _bfd_merged_section_offset (abfd, &sym_sec,
6581 elf_section_data (sym_sec)->sec_info,
6582 h->root.u.def.value);
6583 symval = symval + sym_sec->output_section->vma
6584 + sym_sec->output_offset;
6587 symval = (h->root.u.def.value
6588 + h->root.u.def.section->output_section->vma
6589 + h->root.u.def.section->output_offset);
6592 addend = irel->r_addend;
6594 foff = (symval + addend
6595 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
6600 calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
6601 Elf_Internal_Sym *isymbuf,
6602 Elf_Internal_Rela *irel,
6603 Elf_Internal_Shdr *symtab_hdr)
6607 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6609 Elf_Internal_Sym *isym;
6611 /* A local symbol. */
6612 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6614 if (isym->st_shndx == SHN_UNDEF)
6615 sym_sec = bfd_und_section_ptr;
6616 else if (isym->st_shndx == SHN_ABS)
6617 sym_sec = bfd_abs_section_ptr;
6618 else if (isym->st_shndx == SHN_COMMON)
6619 sym_sec = bfd_com_section_ptr;
6621 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6622 symval = isym->st_value + sym_sec->output_section->vma
6623 + sym_sec->output_offset;
6628 struct elf_link_hash_entry *h;
6629 struct elf_nds32_link_hash_table *htab;
6632 /* An external symbol. */
6633 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6634 h = elf_sym_hashes (abfd)[indx];
6635 BFD_ASSERT (h != NULL);
6636 htab = nds32_elf_hash_table (link_info);
6637 splt = htab->root.splt;
6639 while (h->root.type == bfd_link_hash_indirect
6640 || h->root.type == bfd_link_hash_warning)
6641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6643 if (h->plt.offset == (bfd_vma) - 1)
6645 if (h->root.type != bfd_link_hash_defined
6646 && h->root.type != bfd_link_hash_defweak)
6647 /* This appears to be a reference to an undefined
6648 * symbol. Just ignore it--it will be caught by the
6649 * regular reloc processing. */
6651 symval = (h->root.u.def.value
6652 + h->root.u.def.section->output_section->vma
6653 + h->root.u.def.section->output_offset);
6656 symval = splt->output_section->vma + h->plt.offset;
6662 static bfd_signed_vma
6663 calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
6664 Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
6665 Elf_Internal_Shdr *symtab_hdr)
6668 if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
6672 return foff - (irel->r_offset
6673 + sec->output_section->vma + sec->output_offset);
6676 /* Convert a 32-bit instruction to 16-bit one.
6677 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
6678 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
6679 type of INSN16. Return 1 if successful. */
6682 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6685 uint16_t insn16 = 0;
6687 unsigned long mach = bfd_get_mach (abfd);
6689 if (N32_SH5 (insn) != 0)
6692 switch (N32_SUB5 (insn))
6694 case N32_ALU1_ADD_SLLI:
6695 case N32_ALU1_ADD_SRLI:
6696 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6698 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
6700 insn_type = NDS32_INSN_ADD333;
6702 else if (N32_IS_RT4 (insn))
6704 if (N32_RT5 (insn) == N32_RA5 (insn))
6705 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
6706 else if (N32_RT5 (insn) == N32_RB5 (insn))
6707 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
6708 insn_type = NDS32_INSN_ADD45;
6712 case N32_ALU1_SUB_SLLI:
6713 case N32_ALU1_SUB_SRLI:
6714 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6716 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
6718 insn_type = NDS32_INSN_SUB333;
6720 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6722 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
6723 insn_type = NDS32_INSN_SUB45;
6727 case N32_ALU1_AND_SLLI:
6728 case N32_ALU1_AND_SRLI:
6729 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
6730 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6731 && N32_IS_RB3 (insn))
6733 if (N32_RT5 (insn) == N32_RA5 (insn))
6734 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
6735 else if (N32_RT5 (insn) == N32_RB5 (insn))
6736 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
6738 insn_type = NDS32_INSN_AND33;
6742 case N32_ALU1_XOR_SLLI:
6743 case N32_ALU1_XOR_SRLI:
6744 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
6745 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6746 && N32_IS_RB3 (insn))
6748 if (N32_RT5 (insn) == N32_RA5 (insn))
6749 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
6750 else if (N32_RT5 (insn) == N32_RB5 (insn))
6751 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
6753 insn_type = NDS32_INSN_XOR33;
6757 case N32_ALU1_OR_SLLI:
6758 case N32_ALU1_OR_SRLI:
6759 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
6760 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6761 && N32_IS_RB3 (insn))
6763 if (N32_RT5 (insn) == N32_RA5 (insn))
6764 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
6765 else if (N32_RT5 (insn) == N32_RB5 (insn))
6766 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
6768 insn_type = NDS32_INSN_OR33;
6772 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
6773 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
6774 && N32_RA5 (insn) == N32_RB5 (insn))
6776 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
6777 insn_type = NDS32_INSN_NOT33;
6781 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6783 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
6784 insn_type = NDS32_INSN_SRAI45;
6789 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6791 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
6792 insn_type = NDS32_INSN_SRLI45;
6797 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
6799 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
6801 insn_type = NDS32_INSN_SLLI333;
6806 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6808 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
6809 insn_type = NDS32_INSN_ZEH33;
6814 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6816 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
6817 insn_type = NDS32_INSN_SEB33;
6822 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6824 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
6825 insn_type = NDS32_INSN_SEH33;
6830 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6833 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
6834 insn_type = NDS32_INSN_SLT45;
6839 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6842 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
6843 insn_type = NDS32_INSN_SLTS45;
6848 if ((insn16 & 0x8000) == 0)
6854 *pinsn_type = insn_type;
6859 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6862 uint16_t insn16 = 0;
6864 unsigned long mach = bfd_get_mach (abfd);
6866 /* TODO: bset, bclr, btgl, btst. */
6867 if (__GF (insn, 6, 4) != 0)
6870 switch (N32_IMMU (insn, 6))
6873 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6874 && N32_IS_RB3 (insn))
6876 if (N32_RT5 (insn) == N32_RA5 (insn))
6877 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
6878 else if (N32_RT5 (insn) == N32_RB5 (insn))
6879 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
6881 insn_type = NDS32_INSN_MUL33;
6885 if ((insn16 & 0x8000) == 0)
6891 *pinsn_type = insn_type;
6896 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6900 uint16_t insn16 = 0;
6902 unsigned long mach = bfd_get_mach (abfd);
6904 /* Decode 32-bit instruction. */
6905 if (insn & 0x80000000)
6907 /* Not 32-bit insn. */
6911 op6 = N32_OP6 (insn);
6913 /* Convert it to 16-bit instruction. */
6917 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
6919 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
6920 insn_type = NDS32_INSN_MOVI55;
6922 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
6923 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
6925 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
6926 N32_IMM20S (insn) - 16);
6927 insn_type = NDS32_INSN_MOVPI45;
6932 if (N32_IMM15S (insn) == 0)
6934 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
6935 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
6937 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
6939 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
6940 insn_type = NDS32_INSN_MOV55;
6943 else if (N32_IMM15S (insn) > 0)
6945 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
6947 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
6949 insn_type = NDS32_INSN_ADDI333;
6951 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6952 && N32_IMM15S (insn) < 32)
6954 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
6955 insn_type = NDS32_INSN_ADDI45;
6957 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6958 && N32_RT5 (insn) == N32_RA5 (insn)
6959 && N32_IMM15S (insn) < 512)
6961 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6962 insn_type = NDS32_INSN_ADDI10_SP;
6964 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
6965 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
6966 && (N32_IMM15S (insn) % 4 == 0))
6968 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
6969 N32_IMM15S (insn) >> 2);
6970 insn_type = NDS32_INSN_ADDRI36_SP;
6976 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
6978 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
6979 0 - N32_IMM15S (insn));
6980 insn_type = NDS32_INSN_SUBI333;
6982 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6983 && N32_IMM15S (insn) > -32)
6985 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
6986 0 - N32_IMM15S (insn));
6987 insn_type = NDS32_INSN_SUBI45;
6989 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6990 && N32_RT5 (insn) == N32_RA5 (insn)
6991 && N32_IMM15S (insn) >= -512)
6993 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6994 insn_type = NDS32_INSN_ADDI10_SP;
7000 if (N32_IMM15S (insn) == 0)
7002 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7003 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7005 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7007 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7008 insn_type = NDS32_INSN_MOV55;
7014 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7015 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7017 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7018 insn_type = NDS32_INSN_NEG33;
7023 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7025 if (N32_IMM15U (insn) == 1)
7027 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7028 insn_type = NDS32_INSN_XLSB33;
7030 else if (N32_IMM15U (insn) == 0x7ff)
7032 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7033 insn_type = NDS32_INSN_X11B33;
7035 else if (N32_IMM15U (insn) == 0xff)
7037 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7038 insn_type = NDS32_INSN_ZEB33;
7040 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7041 && N32_IMM15U (insn) < 256)
7043 int imm15u = N32_IMM15U (insn);
7045 if (__builtin_popcount (imm15u) == 1)
7048 int imm3u = __builtin_ctz (imm15u);
7050 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7051 insn_type = NDS32_INSN_BMSKI33;
7053 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7056 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7058 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7059 insn_type = NDS32_INSN_FEXTI33;
7066 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7067 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7069 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7070 insn_type = NDS32_INSN_SLTI45;
7075 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7076 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7078 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7079 insn_type = NDS32_INSN_SLTSI45;
7084 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7086 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7087 insn_type = NDS32_INSN_LWI450;
7089 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7090 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7092 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7094 insn_type = NDS32_INSN_LWI333;
7096 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7097 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7099 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7100 insn_type = NDS32_INSN_LWI37;
7102 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7103 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7105 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7106 insn_type = NDS32_INSN_LWI37_SP;
7108 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7109 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7111 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7112 N32_IMM15S (insn) + 32);
7113 insn_type = NDS32_INSN_LWI45_FE;
7118 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7120 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7121 insn_type = NDS32_INSN_SWI450;
7123 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7124 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7126 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7128 insn_type = NDS32_INSN_SWI333;
7130 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7131 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7133 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7134 insn_type = NDS32_INSN_SWI37;
7136 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7137 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7139 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7140 insn_type = NDS32_INSN_SWI37_SP;
7144 case N32_OP6_LWI_BI:
7145 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7146 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7148 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7150 insn_type = NDS32_INSN_LWI333_BI;
7154 case N32_OP6_SWI_BI:
7155 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7156 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7158 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7160 insn_type = NDS32_INSN_SWI333_BI;
7165 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7166 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7168 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7170 insn_type = NDS32_INSN_LHI333;
7175 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7176 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7178 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
7180 insn_type = NDS32_INSN_SHI333;
7185 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7186 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7188 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
7190 insn_type = NDS32_INSN_LBI333;
7195 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7196 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7198 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
7200 insn_type = NDS32_INSN_SBI333;
7205 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
7208 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
7211 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
7214 if ((insn & N32_BIT (14)) == 0)
7217 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7218 && N32_RT5 (insn) != REG_R5)
7219 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
7220 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7221 && N32_RA5 (insn) != REG_R5)
7222 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
7223 insn_type = NDS32_INSN_BEQS38;
7229 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7230 && N32_RT5 (insn) != REG_R5)
7231 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
7232 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7233 && N32_RA5 (insn) != REG_R5)
7234 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
7235 insn_type = NDS32_INSN_BNES38;
7241 switch (N32_BR2_SUB (insn))
7244 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7246 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
7247 insn_type = NDS32_INSN_BEQZ38;
7249 else if (N32_RT5 (insn) == REG_R15
7250 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7252 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
7253 insn_type = NDS32_INSN_BEQZS8;
7258 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7260 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
7261 insn_type = NDS32_INSN_BNEZ38;
7263 else if (N32_RT5 (insn) == REG_R15
7264 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7266 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
7267 insn_type = NDS32_INSN_BNEZS8;
7271 case N32_BR2_IFCALL:
7272 if (IS_WITHIN_U (N32_IMM16S (insn), 9))
7274 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
7275 insn_type = NDS32_INSN_IFCALL9;
7282 if ((insn & N32_BIT (24)) == 0)
7285 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
7287 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
7288 insn_type = NDS32_INSN_J8;
7294 if (__GF (insn, 8, 2) != 0)
7297 switch (N32_IMMU (insn, 5))
7300 if (N32_JREG_HINT (insn) == 0)
7303 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
7304 insn_type = NDS32_INSN_JR5;
7306 else if (N32_JREG_HINT (insn) == 1)
7309 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
7310 insn_type = NDS32_INSN_RET5;
7312 else if (N32_JREG_HINT (insn) == 3)
7314 /* ifret = mov55 $sp, $sp */
7315 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
7316 insn_type = NDS32_INSN_IFRET;
7321 /* It's convertible when return rt5 is $lp and address
7322 translation is kept. */
7323 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
7325 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
7326 insn_type = NDS32_INSN_JRAL5;
7333 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
7335 /* For v3, swid above 31 are used for ex9.it. */
7336 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
7337 insn_type = NDS32_INSN_BREAK16;
7342 /* This instruction has no 16-bit variant. */
7347 /* Bit-15 of insn16 should be set for a valid instruction. */
7348 if ((insn16 & 0x8000) == 0)
7354 *pinsn_type = insn_type;
7359 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
7360 Elf_Internal_Rela *reloc)
7362 uint16_t insn16 = 0;
7364 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
7365 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
7368 if (!N32_IS_RT3 (insn))
7371 switch (N32_OP6 (insn))
7374 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7375 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7378 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7379 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7382 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
7385 if (__GF (insn, 17, 3) == 6)
7386 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
7387 else if (__GF (insn, 17, 3) == 7)
7388 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
7392 if ((insn16 & 0x8000) == 0)
7399 /* Convert a 16-bit instruction to 32-bit one.
7400 INSN16 it the input and PINSN it the point to output.
7401 Return non-zero on successful. Otherwise 0 is returned. */
7404 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
7406 uint32_t insn = 0xffffffff;
7407 unsigned long mach = bfd_get_mach (abfd);
7409 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
7411 switch (__GF (insn16, 9, 6))
7413 case 0x4: /* add45 */
7414 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
7417 case 0x5: /* sub45 */
7418 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
7421 case 0x6: /* addi45 */
7422 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7423 N16_IMM5U (insn16));
7425 case 0x7: /* subi45 */
7426 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7427 -N16_IMM5U (insn16));
7429 case 0x8: /* srai45 */
7430 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
7431 N16_IMM5U (insn16));
7433 case 0x9: /* srli45 */
7434 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
7435 N16_IMM5U (insn16));
7437 case 0xa: /* slli333 */
7438 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
7439 N16_IMM3U (insn16));
7441 case 0xc: /* add333 */
7442 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
7445 case 0xd: /* sub333 */
7446 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
7449 case 0xe: /* addi333 */
7450 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7451 N16_IMM3U (insn16));
7453 case 0xf: /* subi333 */
7454 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7455 -N16_IMM3U (insn16));
7457 case 0x10: /* lwi333 */
7458 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
7459 N16_IMM3U (insn16));
7461 case 0x12: /* lhi333 */
7462 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
7463 N16_IMM3U (insn16));
7465 case 0x13: /* lbi333 */
7466 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
7467 N16_IMM3U (insn16));
7469 case 0x11: /* lwi333.bi */
7470 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7471 N16_IMM3U (insn16));
7473 case 0x14: /* swi333 */
7474 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
7475 N16_IMM3U (insn16));
7477 case 0x16: /* shi333 */
7478 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
7479 N16_IMM3U (insn16));
7481 case 0x17: /* sbi333 */
7482 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
7483 N16_IMM3U (insn16));
7485 case 0x15: /* swi333.bi */
7486 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7487 N16_IMM3U (insn16));
7489 case 0x18: /* addri36.sp */
7490 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
7491 N16_IMM6U (insn16) << 2);
7493 case 0x19: /* lwi45.fe */
7494 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
7495 (N16_IMM5U (insn16) - 32));
7497 case 0x1a: /* lwi450 */
7498 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7500 case 0x1b: /* swi450 */
7501 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7504 /* These are r15 implied instructions. */
7505 case 0x30: /* slts45 */
7506 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7508 case 0x31: /* slt45 */
7509 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7511 case 0x32: /* sltsi45 */
7512 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7514 case 0x33: /* slti45 */
7515 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7517 case 0x34: /* beqzs8, bnezs8 */
7518 if (insn16 & N32_BIT (8))
7519 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
7521 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
7524 case 0x35: /* break16, ex9.it */
7525 /* Only consider range of v3 break16. */
7526 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
7529 case 0x3c: /* ifcall9 */
7530 insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
7532 case 0x3d: /* movpi45 */
7533 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
7536 case 0x3f: /* MISC33 */
7537 switch (insn16 & 0x7)
7540 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7543 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
7547 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
7551 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
7555 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
7559 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
7566 switch (insn16 & 0x7)
7569 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
7572 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7575 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7578 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7580 case 4: /* xlsb33 */
7581 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
7583 case 5: /* x11b33 */
7584 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
7586 case 6: /* bmski33 */
7587 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7588 1 << __GF (insn16, 3, 3));
7590 case 7: /* fexti33 */
7591 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7592 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
7598 switch (__GF (insn16, 10, 5))
7600 case 0x0: /* mov55 or ifret16 */
7601 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
7602 && N16_RT5 (insn16) == N16_RA5 (insn16))
7603 insn = N32_JREG (JR, 0, 0, 0, 3);
7605 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
7607 case 0x1: /* movi55 */
7608 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
7610 case 0x1b: /* addi10s (V2) */
7611 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
7615 switch (__GF (insn16, 11, 4))
7617 case 0x7: /* lwi37.fp/swi37.fp */
7618 if (insn16 & N32_BIT (7)) /* swi37.fp */
7619 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7621 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7623 case 0x8: /* beqz38 */
7624 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7626 case 0x9: /* bnez38 */
7627 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7629 case 0xa: /* beqs38/j8, implied r5 */
7630 if (N16_RT38 (insn16) == 5)
7631 insn = N32_JI (J, N16_IMM8S (insn16));
7633 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7635 case 0xb: /* bnes38 and others */
7636 if (N16_RT38 (insn16) == 5)
7638 switch (__GF (insn16, 5, 3))
7641 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
7644 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
7647 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
7649 case 2: /* ex9.it imm5 */
7650 /* ex9.it had no 32-bit variantl. */
7652 case 5: /* add5.pc */
7653 /* add5.pc had no 32-bit variantl. */
7658 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7660 case 0xe: /* lwi37/swi37 */
7661 if (insn16 & (1 << 7)) /* swi37.sp */
7662 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7664 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7669 if (insn & 0x80000000)
7678 is_sda_access_insn (unsigned long insn)
7680 switch (N32_OP6 (insn))
7701 static unsigned long
7702 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
7708 case R_NDS32_GOT_LO12:
7709 case R_NDS32_GOTOFF_LO12:
7710 case R_NDS32_PLTREL_LO12:
7711 case R_NDS32_PLT_GOTREL_LO12:
7712 case R_NDS32_LO12S0_RELA:
7713 switch (N32_OP6 (insn))
7717 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
7721 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
7725 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
7729 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
7734 case R_NDS32_LO12S1_RELA:
7735 switch (N32_OP6 (insn))
7739 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
7743 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
7747 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
7752 case R_NDS32_LO12S2_RELA:
7753 switch (N32_OP6 (insn))
7757 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
7761 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
7766 case R_NDS32_LO12S2_DP_RELA:
7767 case R_NDS32_LO12S2_SP_RELA:
7768 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
7778 /* Linker hasn't found the correct merge section for non-section symbol
7779 in relax time, this work is left to the function elf_link_input_bfd().
7780 So for non-section symbol, _bfd_merged_section_offset is also needed
7781 to find the correct symbol address. */
7784 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
7785 asection **psec, Elf_Internal_Rela *rel)
7787 asection *sec = *psec;
7790 relocation = (sec->output_section->vma
7791 + sec->output_offset + sym->st_value);
7792 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
7794 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7796 _bfd_merged_section_offset (abfd, psec,
7797 elf_section_data (sec)->sec_info,
7798 sym->st_value + rel->r_addend);
7801 _bfd_merged_section_offset (abfd, psec,
7802 elf_section_data (sec)->sec_info,
7803 sym->st_value) + rel->r_addend;
7807 /* If we have changed the section, and our original section is
7808 marked with SEC_EXCLUDE, it means that the original
7809 SEC_MERGE section has been completely subsumed in some
7810 other SEC_MERGE section. In this case, we need to leave
7811 some info around for --emit-relocs. */
7812 if ((sec->flags & SEC_EXCLUDE) != 0)
7813 sec->kept_section = *psec;
7816 rel->r_addend -= relocation;
7817 rel->r_addend += sec->output_section->vma + sec->output_offset;
7823 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
7824 Elf_Internal_Sym *isymbuf,
7825 Elf_Internal_Shdr *symtab_hdr)
7827 bfd_signed_vma foff;
7828 bfd_vma symval, addend;
7829 Elf_Internal_Rela irel_fn;
7830 Elf_Internal_Sym *isym;
7833 /* Get the value of the symbol referred to by the reloc. */
7834 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7836 /* A local symbol. */
7837 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7839 if (isym->st_shndx == SHN_UNDEF)
7840 sym_sec = bfd_und_section_ptr;
7841 else if (isym->st_shndx == SHN_ABS)
7842 sym_sec = bfd_abs_section_ptr;
7843 else if (isym->st_shndx == SHN_COMMON)
7844 sym_sec = bfd_com_section_ptr;
7846 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7847 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
7848 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
7849 addend = irel_fn.r_addend;
7854 struct elf_link_hash_entry *h;
7856 /* An external symbol. */
7857 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7858 h = elf_sym_hashes (abfd)[indx];
7859 BFD_ASSERT (h != NULL);
7861 while (h->root.type == bfd_link_hash_indirect
7862 || h->root.type == bfd_link_hash_warning)
7863 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7865 if (h->root.type != bfd_link_hash_defined
7866 && h->root.type != bfd_link_hash_defweak)
7867 /* This appears to be a reference to an undefined
7868 symbol. Just ignore it--it will be caught by the
7869 regular reloc processing. */
7872 if (h->root.u.def.section->flags & SEC_MERGE)
7874 sym_sec = h->root.u.def.section;
7875 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
7876 (sym_sec)->sec_info, h->root.u.def.value);
7877 symval = symval + sym_sec->output_section->vma
7878 + sym_sec->output_offset;
7881 symval = (h->root.u.def.value
7882 + h->root.u.def.section->output_section->vma
7883 + h->root.u.def.section->output_offset);
7884 addend = irel->r_addend;
7887 foff = symval + addend;
7893 calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
7894 Elf_Internal_Rela *irel,
7895 Elf_Internal_Shdr *symtab_hdr)
7898 bfd_vma *local_got_offsets;
7899 /* Get the value of the symbol referred to by the reloc. */
7900 struct elf_link_hash_entry *h;
7901 struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);
7903 /* An external symbol. */
7904 symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7905 h = elf_sym_hashes (abfd)[symndx];
7906 while (h->root.type == bfd_link_hash_indirect
7907 || h->root.type == bfd_link_hash_warning)
7908 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7912 BFD_ASSERT (h != NULL);
7913 return (htab->root.sgot->output_section->vma
7914 + htab->root.sgot->output_offset
7919 local_got_offsets = elf_local_got_offsets (abfd);
7920 BFD_ASSERT (local_got_offsets != NULL);
7921 return (htab->root.sgot->output_section->vma
7922 + htab->root.sgot->output_offset
7923 + local_got_offsets[ELF32_R_SYM (irel->r_info)]);
7926 /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?). */
7927 /* The check of h->root.type is passed. */
7931 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
7932 asection *sec, Elf_Internal_Rela *rel)
7935 unsigned short insn16;
7937 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
7939 contents = elf_section_data (sec)->this_hdr.contents;
7940 insn16 = bfd_getb16 (contents + rel->r_offset);
7941 if (insn16 == NDS32_NOP16)
7946 /* It checks whether the instruction could be converted to
7947 16-bit form and returns the converted one.
7949 `internal_relocs' is supposed to be sorted. */
7952 is_convert_32_to_16 (bfd *abfd, asection *sec,
7953 Elf_Internal_Rela *reloc,
7954 Elf_Internal_Rela *internal_relocs,
7955 Elf_Internal_Rela *irelend,
7958 #define NORMAL_32_TO_16 (1 << 0)
7959 #define SPECIAL_32_TO_16 (1 << 1)
7960 bfd_byte *contents = NULL;
7964 Elf_Internal_Rela *pc_rel;
7965 int pic_ext_target = 0;
7966 Elf_Internal_Shdr *symtab_hdr;
7967 Elf_Internal_Sym *isymbuf = NULL;
7971 if (reloc->r_offset + 4 > sec->size)
7974 offset = reloc->r_offset;
7976 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
7978 insn = bfd_getb32 (contents + offset);
7980 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
7981 convert_type = NORMAL_32_TO_16;
7982 else if (special_convert_32_to_16 (insn, insn16, reloc))
7983 convert_type = SPECIAL_32_TO_16;
7987 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7988 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
7991 /* Find the first relocation of the same relocation-type,
7992 so we iteratie them forward. */
7994 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
7997 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
7999 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8000 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8001 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8002 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8004 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
8006 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8011 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8013 /* movi => movi55 */
8014 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8016 /* mem_addr is unsigned, but the value should
8017 be between [-16, 15]. */
8018 if ((mem_addr + 0x10) >> 5)
8022 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8023 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8025 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8026 because it can be relaxed to addi for TLS_LE_ADD. */
8029 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8030 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8031 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8032 && convert_type == SPECIAL_32_TO_16)
8035 We've selected a best fp-base for this access, so we can
8036 always resolve it anyway. Do nothing. */
8039 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8040 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8041 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8042 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8043 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8044 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8046 /* Prevent unresolved addi instruction translate
8047 to addi45 or addi333. */
8050 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8052 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
8054 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8064 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8065 Elf_Internal_Rela *reloc,
8066 Elf_Internal_Rela *internal_relocs,
8067 Elf_Internal_Rela *irelend,
8068 unsigned short insn16)
8070 Elf_Internal_Rela *pc_rel;
8073 offset = reloc->r_offset;
8074 bfd_putb16 (insn16, contents + offset);
8075 /* Find the first relocation of the same relocation-type,
8076 so we iteratie them forward. */
8078 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8081 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8083 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8084 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8085 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8088 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8090 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8092 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8093 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8095 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8096 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8097 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8099 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8100 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8102 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8106 /* Find a relocation of type specified by `reloc_type'
8107 of the same r_offset with reloc.
8108 If not found, return irelend.
8110 Assuming relocations are sorted by r_offset,
8111 we find the relocation from `reloc' backward untill relocs,
8112 or find it from `reloc' forward untill irelend. */
8114 static Elf_Internal_Rela *
8115 find_relocs_at_address (Elf_Internal_Rela *reloc,
8116 Elf_Internal_Rela *relocs,
8117 Elf_Internal_Rela *irelend,
8118 enum elf_nds32_reloc_type reloc_type)
8120 Elf_Internal_Rela *rel_t;
8122 /* Find backward. */
8124 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8126 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8129 /* We didn't find it backward. Try find it forward. */
8131 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8133 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8139 /* Find a relocation of specified type and offset.
8140 `reloc' is just a refence point to find a relocation at specified offset.
8141 If not found, return irelend.
8143 Assuming relocations are sorted by r_offset,
8144 we find the relocation from `reloc' backward untill relocs,
8145 or find it from `reloc' forward untill irelend. */
8147 static Elf_Internal_Rela *
8148 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8149 Elf_Internal_Rela *relocs,
8150 Elf_Internal_Rela *irelend,
8151 enum elf_nds32_reloc_type reloc_type,
8154 Elf_Internal_Rela *rel_t = NULL;
8156 /* First, we try to find a relocation of offset `offset_p',
8157 and then we use find_relocs_at_address to find specific type. */
8159 if (reloc->r_offset > offset_p)
8161 /* Find backward. */
8163 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8166 else if (reloc->r_offset < offset_p)
8170 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8177 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8180 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8184 nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
8185 Elf_Internal_Rela *internal_relocs,
8186 Elf_Internal_Rela *irelend,
8187 unsigned char reloc_type)
8189 Elf_Internal_Rela *rel_t;
8192 rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
8194 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8196 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8197 && rel_t->r_addend == reloc->r_addend)
8202 for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8204 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8206 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8207 && rel_t->r_addend == reloc->r_addend)
8215 typedef struct nds32_elf_blank nds32_elf_blank_t;
8216 struct nds32_elf_blank
8218 /* Where the blank begins. */
8220 /* The size of the blank. */
8222 /* The accumulative size before this blank. */
8224 nds32_elf_blank_t *next;
8225 nds32_elf_blank_t *prev;
8228 static nds32_elf_blank_t *blank_free_list = NULL;
8230 static nds32_elf_blank_t *
8231 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8233 nds32_elf_blank_t *blank_t;
8235 if (blank_free_list)
8237 blank_t = blank_free_list;
8238 blank_free_list = blank_free_list->next;
8241 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
8243 if (blank_t == NULL)
8246 blank_t->offset = offset_p;
8247 blank_t->size = size_p;
8248 blank_t->total_size = 0;
8249 blank_t->next = NULL;
8250 blank_t->prev = NULL;
8256 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
8258 if (blank_free_list)
8260 blank_free_list->prev = blank_p;
8261 blank_p->next = blank_free_list;
8264 blank_p->next = NULL;
8266 blank_p->prev = NULL;
8267 blank_free_list = blank_p;
8271 clean_nds32_elf_blank (void)
8273 nds32_elf_blank_t *blank_t;
8275 while (blank_free_list)
8277 blank_t = blank_free_list;
8278 blank_free_list = blank_free_list->next;
8283 static nds32_elf_blank_t *
8284 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
8286 nds32_elf_blank_t *blank_t;
8292 while (blank_t && addr < blank_t->offset)
8293 blank_t = blank_t->prev;
8294 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
8295 blank_t = blank_t->next;
8301 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8304 nds32_elf_blank_t *blank_t;
8306 blank_t = search_nds32_elf_blank (*blank_p, addr);
8313 if (addr < blank_t->offset + blank_t->size)
8314 return blank_t->total_size + (addr - blank_t->offset);
8316 return blank_t->total_size + blank_t->size;
8320 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
8322 nds32_elf_blank_t *blank_t, *blank_t2;
8326 *blank_p = create_nds32_elf_blank (addr, len);
8327 return *blank_p ? TRUE : FALSE;
8330 blank_t = search_nds32_elf_blank (*blank_p, addr);
8332 if (blank_t == NULL)
8334 blank_t = create_nds32_elf_blank (addr, len);
8337 while ((*blank_p)->prev != NULL)
8338 *blank_p = (*blank_p)->prev;
8339 blank_t->next = *blank_p;
8340 (*blank_p)->prev = blank_t;
8341 (*blank_p) = blank_t;
8345 if (addr < blank_t->offset + blank_t->size)
8347 if (addr > blank_t->offset + blank_t->size)
8348 blank_t->size = addr - blank_t->offset;
8352 blank_t2 = create_nds32_elf_blank (addr, len);
8357 blank_t->next->prev = blank_t2;
8358 blank_t2->next = blank_t->next;
8360 blank_t2->prev = blank_t;
8361 blank_t->next = blank_t2;
8362 *blank_p = blank_t2;
8369 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8372 nds32_elf_blank_t *blank_t;
8374 if (!insert_nds32_elf_blank (blank_p, addr, len))
8381 blank_t->total_size = 0;
8382 blank_t = blank_t->next;
8387 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
8388 blank_t = blank_t->next;
8395 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
8397 nds32_elf_blank_t *blank_t;
8398 bfd_vma total_size = 0;
8404 while (blank_t->prev)
8405 blank_t = blank_t->prev;
8408 blank_t->total_size = total_size;
8409 total_size += blank_t->size;
8410 blank_t = blank_t->next;
8415 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
8416 nds32_elf_blank_t *blank_p)
8418 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
8419 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
8420 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
8421 unsigned int sec_shndx; /* The section the be relaxed. */
8422 bfd_byte *contents; /* Contents data of iterating section. */
8423 Elf_Internal_Rela *internal_relocs;
8424 Elf_Internal_Rela *irel;
8425 Elf_Internal_Rela *irelend;
8426 struct elf_link_hash_entry **sym_hashes;
8427 struct elf_link_hash_entry **end_hashes;
8428 unsigned int symcount;
8430 nds32_elf_blank_t *blank_t;
8431 nds32_elf_blank_t *blank_t2;
8432 nds32_elf_blank_t *blank_head;
8434 blank_head = blank_t = blank_p;
8435 while (blank_head->prev != NULL)
8436 blank_head = blank_head->prev;
8437 while (blank_t->next != NULL)
8438 blank_t = blank_t->next;
8440 if (blank_t->offset + blank_t->size <= sec->size)
8442 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
8443 blank_t->next->prev = blank_t;
8445 if (blank_head->offset > 0)
8447 blank_head->prev = create_nds32_elf_blank (0, 0);
8448 blank_head->prev->next = blank_head;
8449 blank_head = blank_head->prev;
8452 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8454 /* The deletion must stop at the next ALIGN reloc for an alignment
8455 power larger than the number of bytes we are deleting. */
8457 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8458 if (!nds32_get_local_syms (abfd, sec, &isym))
8463 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8464 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
8465 symtab_hdr->contents = (bfd_byte *) isym;
8468 if (isym == NULL || symtab_hdr->sh_info == 0)
8471 blank_t = blank_head;
8472 calc_nds32_blank_total (blank_head);
8474 for (sect = abfd->sections; sect != NULL; sect = sect->next)
8476 /* Adjust all the relocs. */
8478 /* Relocations MUST be kept in memory, because relaxation adjust them. */
8479 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
8480 TRUE /* keep_memory */);
8481 irelend = internal_relocs + sect->reloc_count;
8483 blank_t = blank_head;
8484 blank_t2 = blank_head;
8486 if (!(sect->flags & SEC_RELOC))
8489 nds32_get_section_contents (abfd, sect, &contents, TRUE);
8491 for (irel = internal_relocs; irel < irelend; irel++)
8495 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
8496 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
8497 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8499 unsigned long val = 0;
8501 long before, between;
8504 switch (ELF32_R_TYPE (irel->r_info))
8507 offset = bfd_get_8 (abfd, contents + irel->r_offset);
8509 case R_NDS32_DIFF16:
8510 offset = bfd_get_16 (abfd, contents + irel->r_offset);
8512 case R_NDS32_DIFF32:
8513 val = bfd_get_32 (abfd, contents + irel->r_offset);
8514 /* Get the signed bit and mask for the high part. The
8515 gcc will alarm when right shift 32-bit since the
8516 type size of long may be 32-bit. */
8517 mask = 0 - (val >> 31);
8519 offset = (val | (mask - 0xffffffff));
8528 0 |encoded in location|
8529 |------------|-------------------|---------
8531 -- before ---| *****************
8532 --------------------- between ---|
8534 We only care how much data are relax between DIFF,
8537 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8538 between = get_nds32_elf_blank_total (&blank_t,
8539 irel->r_addend + offset, 0);
8540 if (between == before)
8541 goto done_adjust_diff;
8543 switch (ELF32_R_TYPE (irel->r_info))
8546 bfd_put_8 (abfd, offset - (between - before),
8547 contents + irel->r_offset);
8549 case R_NDS32_DIFF16:
8550 bfd_put_16 (abfd, offset - (between - before),
8551 contents + irel->r_offset);
8553 case R_NDS32_DIFF32:
8554 bfd_put_32 (abfd, offset - (between - before),
8555 contents + irel->r_offset);
8559 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
8560 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8563 unsigned int len = 0;
8564 unsigned long before, between;
8567 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
8570 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8571 between = get_nds32_elf_blank_total (&blank_t,
8572 irel->r_addend + val, 0);
8573 if (between == before)
8574 goto done_adjust_diff;
8576 p = contents + irel->r_offset;
8578 memset (p, 0x80, len);
8580 p = write_uleb128 (p, val - (between - before)) - 1;
8588 raddr = irel->r_offset;
8589 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
8592 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
8594 if (blank_t2 && blank_t2->next
8595 && (blank_t2->offset > raddr
8596 || blank_t2->next->offset <= raddr))
8598 (_("%B: Error: search_nds32_elf_blank reports wrong node\n"), abfd);
8600 /* Mark reloc in deleted portion as NONE.
8601 For some relocs like R_NDS32_LABEL that doesn't modify the
8602 content in the section. R_NDS32_LABEL doesn't belong to the
8603 instruction in the section, so we should preserve it. */
8604 if (raddr >= blank_t2->offset
8605 && raddr < blank_t2->offset + blank_t2->size
8606 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
8607 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
8608 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
8609 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
8610 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
8611 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
8613 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
8619 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
8620 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
8621 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
8624 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
8625 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
8626 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
8628 if (irel->r_addend <= sec->size)
8630 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
8635 /* Adjust the local symbols defined in this section. */
8636 blank_t = blank_head;
8637 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
8639 if (isym->st_shndx == sec_shndx)
8641 if (isym->st_value <= sec->size)
8644 bfd_vma orig_addr = isym->st_value;
8646 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
8647 isym->st_value -= ahead;
8649 /* Adjust function size. */
8650 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
8651 && isym->st_size > 0)
8653 get_nds32_elf_blank_total
8654 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
8659 /* Now adjust the global symbols defined in this section. */
8660 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
8661 - symtab_hdr->sh_info);
8662 sym_hashes = elf_sym_hashes (abfd);
8663 end_hashes = sym_hashes + symcount;
8664 blank_t = blank_head;
8665 for (; sym_hashes < end_hashes; sym_hashes++)
8667 struct elf_link_hash_entry *sym_hash = *sym_hashes;
8669 if ((sym_hash->root.type == bfd_link_hash_defined
8670 || sym_hash->root.type == bfd_link_hash_defweak)
8671 && sym_hash->root.u.def.section == sec)
8673 if (sym_hash->root.u.def.value <= sec->size)
8676 bfd_vma orig_addr = sym_hash->root.u.def.value;
8678 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
8679 sym_hash->root.u.def.value -= ahead;
8681 /* Adjust function size. */
8682 if (sym_hash->type == STT_FUNC)
8684 get_nds32_elf_blank_total
8685 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
8691 contents = elf_section_data (sec)->this_hdr.contents;
8692 blank_t = blank_head;
8693 while (blank_t->next)
8695 /* Actually delete the bytes. */
8697 /* If current blank is the last blank overlap with current section,
8698 go to finish process. */
8699 if (sec->size <= (blank_t->next->offset))
8702 memmove (contents + blank_t->offset - blank_t->total_size,
8703 contents + blank_t->offset + blank_t->size,
8704 blank_t->next->offset - (blank_t->offset + blank_t->size));
8706 blank_t = blank_t->next;
8709 if (sec->size > (blank_t->offset + blank_t->size))
8711 /* There are remaining code between blank and section boundary.
8712 Move the remaining code to appropriate location. */
8713 memmove (contents + blank_t->offset - blank_t->total_size,
8714 contents + blank_t->offset + blank_t->size,
8715 sec->size - (blank_t->offset + blank_t->size));
8716 sec->size -= blank_t->total_size + blank_t->size;
8719 /* This blank is not entirely included in the section,
8720 reduce the section size by only part of the blank size. */
8721 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
8725 blank_t = blank_head;
8726 blank_head = blank_head->next;
8727 remove_nds32_elf_blank (blank_t);
8733 /* Get the contents of a section. */
8736 nds32_get_section_contents (bfd *abfd, asection *sec,
8737 bfd_byte **contents_p, bfd_boolean cache)
8739 /* Get the section contents. */
8740 if (elf_section_data (sec)->this_hdr.contents != NULL)
8741 *contents_p = elf_section_data (sec)->this_hdr.contents;
8744 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
8747 elf_section_data (sec)->this_hdr.contents = *contents_p;
8753 /* Get the contents of the internal symbol of abfd. */
8756 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
8757 Elf_Internal_Sym **isymbuf_p)
8759 Elf_Internal_Shdr *symtab_hdr;
8760 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8762 /* Read this BFD's local symbols if we haven't done so already. */
8763 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
8765 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
8766 if (*isymbuf_p == NULL)
8768 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8769 symtab_hdr->sh_info, 0,
8771 if (*isymbuf_p == NULL)
8775 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
8780 /* Range of small data. */
8781 static bfd_vma sdata_range[2][2];
8782 static bfd_vma const sdata_init_range[2] =
8783 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
8786 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
8787 bfd_byte *contents, bfd_vma addr)
8789 unsigned long insn = bfd_getb32 (contents + addr);
8791 if (insn & 0x80000000)
8797 /* Set the gp relax range. We have to measure the safe range
8798 to do gp relaxation. */
8801 relax_range_measurement (bfd *abfd)
8803 asection *sec_f, *sec_b;
8804 /* For upper bound. */
8805 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
8807 static int decide_relax_range = 0;
8809 int range_number = sizeof (sdata_init_range) / sizeof (sdata_init_range[0]);
8811 if (decide_relax_range)
8813 decide_relax_range = 1;
8815 if (sda_rela_sec == NULL)
8817 /* Since there is no data sections, we assume the range is page size. */
8818 for (i = 0; i < range_number; i++)
8820 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
8821 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
8826 /* Get the biggest alignment power after the gp located section. */
8827 sec_f = sda_rela_sec->output_section;
8828 sec_b = sec_f->next;
8830 while (sec_b != NULL)
8832 if ((unsigned)(1 << sec_b->alignment_power) > align)
8833 align = (1 << sec_b->alignment_power);
8834 sec_b = sec_b->next;
8837 /* I guess we can not determine the section before
8838 gp located section, so we assume the align is max page size. */
8839 for (i = 0; i < range_number; i++)
8841 sdata_range[i][1] = sdata_init_range[i] - align;
8842 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
8843 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
8844 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
8848 /* These are macros used to check flags encoded in r_addend.
8849 They are only used by nds32_elf_relax_section (). */
8850 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
8851 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
8852 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
8853 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
8855 static const char * unrecognized_reloc_msg =
8856 /* xgettext:c-format */
8857 N_("%B: warning: %s points to unrecognized reloc at %#Lx");
8859 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
8862 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
8863 Elf_Internal_Rela *internal_relocs, int *insn_len,
8864 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
8865 Elf_Internal_Shdr *symtab_hdr)
8867 /* There are 3 variations for LONGCALL1
8868 case 4-4-2; 16-bit on, optimize off or optimize for space
8869 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8870 ori ta, ta, lo12(symbol) ; LO12S0
8873 case 4-4-4; 16-bit off, optimize don't care
8874 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8875 ori ta, ta, lo12(symbol) ; LO12S0
8878 case 4-4-4; 16-bit on, optimize for speed
8879 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8880 ori ta, ta, lo12(symbol) ; LO12S0
8882 Check code for -mlong-calls output. */
8884 /* Get the reloc for the address from which the register is
8885 being loaded. This reloc will tell us which function is
8886 actually being called. */
8889 int seq_len; /* Original length of instruction sequence. */
8891 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
8892 int pic_ext_target = 0;
8893 bfd_signed_vma foff;
8896 irelend = internal_relocs + sec->reloc_count;
8897 seq_len = GET_SEQ_LEN (irel->r_addend);
8898 laddr = irel->r_offset;
8899 *insn_len = seq_len;
8901 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8902 R_NDS32_HI20_RELA, laddr);
8903 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8904 R_NDS32_LO12S0_ORI_RELA,
8907 if (hi_irelfn == irelend || lo_irelfn == irelend)
8909 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
8914 /* Get the value of the symbol referred to by the reloc. */
8915 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
8918 /* This condition only happened when symbol is undefined. */
8919 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
8920 || foff >= CONSERVATIVE_24BIT_S1)
8923 /* Relax to: jal symbol; 25_PCREL */
8924 /* For simplicity of coding, we are going to modify the section
8925 contents, the section relocs, and the BFD symbol table. We
8926 must tell the rest of the code not to free up this
8927 information. It would be possible to instead create a table
8928 of changes which have to be made, as is done in coff-mips.c;
8929 that would be more work, but would require less memory when
8930 the linker is run. */
8932 /* Replace the long call with a jal. */
8933 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8934 R_NDS32_25_PCREL_RELA);
8935 irel->r_addend = hi_irelfn->r_addend;
8937 /* We don't resolve this here but resolve it in relocate_section. */
8939 bfd_putb32 (insn, contents + irel->r_offset);
8942 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
8944 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
8949 insn16 = NDS32_NOP16;
8950 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
8952 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
8953 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
8959 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
8960 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
8963 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
8964 Elf_Internal_Rela *internal_relocs, int *insn_len,
8965 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
8966 Elf_Internal_Shdr *symtab_hdr)
8968 /* bltz rt, .L1 ; LONGCALL2
8969 jal symbol ; 25_PCREL
8972 /* Get the reloc for the address from which the register is
8973 being loaded. This reloc will tell us which function is
8974 actually being called. */
8978 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
8979 int pic_ext_target = 0;
8980 bfd_signed_vma foff;
8982 irelend = internal_relocs + sec->reloc_count;
8983 laddr = irel->r_offset;
8985 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8986 R_NDS32_25_PCREL_RELA, laddr + 4);
8988 if (i1_irelfn == irelend)
8990 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
8995 insn = bfd_getb32 (contents + laddr);
8997 /* Get the value of the symbol referred to by the reloc. */
8998 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
9001 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9002 || foff >= CONSERVATIVE_16BIT_S1)
9005 /* Relax to bgezal rt, label ; 17_PCREL
9006 or bltzal rt, label ; 17_PCREL */
9008 /* Convert to complimentary conditional call. */
9009 insn = CONVERT_CONDITION_CALL (insn);
9011 /* For simplicity of coding, we are going to modify the section
9012 contents, the section relocs, and the BFD symbol table. We
9013 must tell the rest of the code not to free up this
9014 information. It would be possible to instead create a table
9015 of changes which have to be made, as is done in coff-mips.c;
9016 that would be more work, but would require less memory when
9017 the linker is run. */
9019 /* Clean unnessary relocations. */
9021 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9023 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9024 R_NDS32_17_PCREL_RELA, laddr);
9025 if (cond_irelfn != irelend)
9026 cond_irelfn->r_info =
9027 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9029 /* Replace the long call with a bgezal. */
9030 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9031 R_NDS32_17_PCREL_RELA);
9032 irel->r_addend = i1_irelfn->r_addend;
9034 bfd_putb32 (insn, contents + irel->r_offset);
9040 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9043 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9044 Elf_Internal_Rela *internal_relocs, int *insn_len,
9045 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9046 Elf_Internal_Shdr *symtab_hdr)
9048 /* There are 3 variations for LONGCALL3
9049 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9050 bltz rt, $1 ; LONGCALL3
9051 sethi ta, hi20(symbol) ; HI20
9052 ori ta, ta, lo12(symbol) ; LO12S0
9056 case 4-4-4-4; 16-bit off, optimize don't care
9057 bltz rt, $1 ; LONGCALL3
9058 sethi ta, hi20(symbol) ; HI20
9059 ori ta, ta, lo12(symbol) ; LO12S0
9063 case 4-4-4-4; 16-bit on, optimize for speed
9064 bltz rt, $1 ; LONGCALL3
9065 sethi ta, hi20(symbol) ; HI20
9066 ori ta, ta, lo12(symbol) ; LO12S0
9070 /* Get the reloc for the address from which the register is
9071 being loaded. This reloc will tell us which function is
9072 actually being called. */
9075 int seq_len; /* Original length of instruction sequence. */
9077 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9078 int pic_ext_target = 0;
9079 bfd_signed_vma foff;
9082 irelend = internal_relocs + sec->reloc_count;
9083 seq_len = GET_SEQ_LEN (irel->r_addend);
9084 laddr = irel->r_offset;
9085 *insn_len = seq_len;
9088 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9089 R_NDS32_HI20_RELA, laddr + 4);
9091 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9092 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9094 if (hi_irelfn == irelend || lo_irelfn == irelend)
9096 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9101 /* Get the value of the symbol referred to by the reloc. */
9102 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9105 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9106 || foff >= CONSERVATIVE_24BIT_S1)
9109 insn = bfd_getb32 (contents + laddr);
9110 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9112 /* Relax to bgezal rt, label ; 17_PCREL
9113 or bltzal rt, label ; 17_PCREL */
9115 /* Convert to complimentary conditional call. */
9116 insn = CONVERT_CONDITION_CALL (insn);
9117 bfd_putb32 (insn, contents + irel->r_offset);
9121 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9123 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9125 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9128 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9129 R_NDS32_17_PCREL_RELA, laddr);
9130 if (cond_irelfn != irelend)
9132 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9133 R_NDS32_17_PCREL_RELA);
9134 cond_irelfn->r_addend = hi_irelfn->r_addend;
9139 insn16 = NDS32_NOP16;
9140 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9141 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9143 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9147 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9149 /* Relax to the following instruction sequence
9150 bltz rt, $1 ; LONGCALL2
9151 jal symbol ; 25_PCREL
9155 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9157 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9158 R_NDS32_25_PCREL_RELA);
9160 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9163 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9167 insn16 = NDS32_NOP16;
9168 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9170 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9171 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9178 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
9181 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9182 Elf_Internal_Rela *internal_relocs, int *insn_len,
9183 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9184 Elf_Internal_Shdr *symtab_hdr)
9186 /* There are 3 variations for LONGJUMP1
9187 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9188 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9189 ori ta, ta, lo12(symbol) ; LO12S0
9192 case 4-4-4; 16-bit off, optimize don't care
9193 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9194 ori ta, ta, lo12(symbol) ; LO12S0
9197 case 4-4-4; 16-bit on, optimize for speed
9198 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9199 ori ta, ta, lo12(symbol) ; LO12S0
9202 /* Get the reloc for the address from which the register is
9203 being loaded. This reloc will tell us which function is
9204 actually being called. */
9207 int seq_len; /* Original length of instruction sequence. */
9208 int insn16_on; /* 16-bit on/off. */
9210 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9211 int pic_ext_target = 0;
9212 bfd_signed_vma foff;
9214 unsigned long reloc;
9216 irelend = internal_relocs + sec->reloc_count;
9217 seq_len = GET_SEQ_LEN (irel->r_addend);
9218 laddr = irel->r_offset;
9219 *insn_len = seq_len;
9220 insn16_on = IS_16BIT_ON (irel->r_addend);
9223 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9224 R_NDS32_HI20_RELA, laddr);
9226 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9227 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9228 if (hi_irelfn == irelend || lo_irelfn == irelend)
9230 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
9235 /* Get the value of the symbol referred to by the reloc. */
9236 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9239 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
9240 || foff < -CONSERVATIVE_24BIT_S1)
9243 if (insn16_on && foff >= -ACCURATE_8BIT_S1
9244 && foff < ACCURATE_8BIT_S1 && (seq_len & 0x2))
9247 /* 16-bit on, but not optimized for speed. */
9248 reloc = R_NDS32_9_PCREL_RELA;
9250 bfd_putb16 (insn16, contents + irel->r_offset);
9253 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9258 reloc = R_NDS32_25_PCREL_RELA;
9260 bfd_putb32 (insn, contents + irel->r_offset);
9263 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9268 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9270 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9272 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
9274 insn16 = NDS32_NOP16;
9275 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9277 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9279 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9285 /* Revert condition branch. This function does not check if the input
9286 instruction is condition branch or not. */
9289 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
9290 uint16_t *re_insn16, uint32_t *re_insn)
9292 uint32_t comp_insn = 0;
9293 uint16_t comp_insn16 = 0;
9297 if (N32_OP6 (insn) == N32_OP6_BR1)
9300 comp_insn = (insn ^ 0x4000) & 0xffffc000;
9301 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
9303 /* Insn can be contracted to 16-bit implied r5. */
9305 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
9306 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9309 else if (N32_OP6 (insn) == N32_OP6_BR3)
9311 /* bnec $ta, imm11, label. */
9312 comp_insn = (insn ^ 0x80000) & 0xffffff00;
9316 comp_insn = (insn ^ 0x10000) & 0xffffc000;
9317 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
9318 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
9320 if (N32_IS_RT3 (insn))
9322 /* Insn can be contracted to 16-bit. */
9324 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
9325 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9327 else if (N32_RT5 (insn) == REG_R15)
9329 /* Insn can be contracted to 16-bit. */
9331 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
9338 switch ((insn16 & 0xf000) >> 12)
9341 /* beqz38 or bnez38 */
9342 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9343 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
9344 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
9348 /* beqs38 or bnes38 */
9349 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9350 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
9351 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
9356 /* beqzS8 or bnezS8 */
9357 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
9358 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
9359 comp_insn |= REG_R15 << 20;
9366 if (comp_insn && re_insn)
9367 *re_insn = comp_insn;
9368 if (comp_insn16 && re_insn16)
9369 *re_insn16 = comp_insn16;
9372 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
9375 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9376 Elf_Internal_Rela *internal_relocs, int *insn_len,
9377 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9378 Elf_Internal_Shdr *symtab_hdr)
9380 /* There are 3 variations for LONGJUMP2
9381 case 2-4; 1st insn convertible, 16-bit on,
9382 optimize off or optimize for space
9383 bnes38 rt, ra, $1 ; LONGJUMP2
9387 case 4-4; 1st insn not convertible
9388 bne rt, ra, $1 ; LONGJUMP2
9392 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9393 bne rt, ra, $1 ; LONGJUMP2
9397 /* Get the reloc for the address from which the register is
9398 being loaded. This reloc will tell us which function is
9399 actually being called. */
9402 int seq_len; /* Original length of instruction sequence. */
9403 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
9404 int pic_ext_target = 0, first_size;
9406 bfd_signed_vma foff;
9407 uint32_t insn, re_insn = 0;
9408 uint16_t insn16, re_insn16 = 0;
9409 unsigned long reloc, cond_reloc;
9411 enum elf_nds32_reloc_type checked_types[] =
9412 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9414 irelend = internal_relocs + sec->reloc_count;
9415 seq_len = GET_SEQ_LEN (irel->r_addend);
9416 laddr = irel->r_offset;
9417 *insn_len = seq_len;
9418 first_size = (seq_len == 6) ? 2 : 4;
9421 find_relocs_at_address_addr (irel, internal_relocs,
9422 irelend, R_NDS32_25_PCREL_RELA,
9423 laddr + first_size);
9425 for (i = 0; i < sizeof (checked_types) / sizeof(checked_types[0]); i++)
9428 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9429 checked_types[i], laddr);
9430 if (cond_irelfn != irelend)
9434 if (i2_irelfn == irelend || cond_irelfn == irelend)
9436 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
9441 /* Get the value of the symbol referred to by the reloc. */
9443 calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
9445 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9446 || foff >= CONSERVATIVE_16BIT_S1)
9449 /* Get the all corresponding instructions. */
9450 if (first_size == 4)
9452 insn = bfd_getb32 (contents + laddr);
9453 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9457 insn16 = bfd_getb16 (contents + laddr);
9458 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9461 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
9462 && foff < ACCURATE_8BIT_S1 - first_size)
9464 if (first_size == 4)
9466 /* Don't convert it to 16-bit now, keep this as relaxable for
9467 ``label reloc; INSN16''. */
9469 /* Save comp_insn32 to buffer. */
9470 bfd_putb32 (re_insn, contents + irel->r_offset);
9472 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9473 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9474 cond_reloc = R_NDS32_INSN16;
9478 bfd_putb16 (re_insn16, contents + irel->r_offset);
9480 reloc = R_NDS32_9_PCREL_RELA;
9481 cond_reloc = R_NDS32_NONE;
9484 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9485 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9486 && foff < ACCURATE_14BIT_S1 - first_size))
9488 /* beqs label ; 15_PCREL */
9489 bfd_putb32 (re_insn, contents + irel->r_offset);
9491 reloc = R_NDS32_15_PCREL_RELA;
9492 cond_reloc = R_NDS32_NONE;
9494 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9495 && foff >= -CONSERVATIVE_16BIT_S1
9496 && foff < CONSERVATIVE_16BIT_S1)
9498 /* beqz label ; 17_PCREL */
9499 bfd_putb32 (re_insn, contents + irel->r_offset);
9501 reloc = R_NDS32_17_PCREL_RELA;
9502 cond_reloc = R_NDS32_NONE;
9507 /* Set all relocations. */
9508 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
9509 irel->r_addend = i2_irelfn->r_addend;
9511 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9513 cond_irelfn->r_addend = 0;
9515 if ((seq_len ^ *insn_len ) & 0x2)
9517 insn16 = NDS32_NOP16;
9518 bfd_putb16 (insn16, contents + irel->r_offset + 4);
9519 i2_irelfn->r_offset = 4;
9520 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9522 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9526 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9531 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
9534 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9535 Elf_Internal_Rela *internal_relocs, int *insn_len,
9536 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9537 Elf_Internal_Shdr *symtab_hdr)
9539 /* There are 5 variations for LONGJUMP3
9540 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
9541 optimize off or optimize for space
9542 bnes38 rt, ra, $1 ; LONGJUMP3
9543 sethi ta, hi20(symbol) ; HI20
9544 ori ta, ta, lo12(symbol) ; LO12S0
9548 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
9549 bnes38 rt, ra, $1 ; LONGJUMP3
9550 sethi ta, hi20(symbol) ; HI20
9551 ori ta, ta, lo12(symbol) ; LO12S0
9555 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
9556 optimize off or optimize for space
9557 bne rt, ra, $1 ; LONGJUMP3
9558 sethi ta, hi20(symbol) ; HI20
9559 ori ta, ta, lo12(symbol) ; LO12S0
9563 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
9564 16-bit off if no INSN16
9565 bne rt, ra, $1 ; LONGJUMP3
9566 sethi ta, hi20(symbol) ; HI20
9567 ori ta, ta, lo12(symbol) ; LO12S0
9571 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
9572 16-bit off if no INSN16
9573 bne rt, ra, $1 ; LONGJUMP3
9574 sethi ta, hi20(symbol) ; HI20
9575 ori ta, ta, lo12(symbol) ; LO12S0
9579 /* Get the reloc for the address from which the register is
9580 being loaded. This reloc will tell us which function is
9581 actually being called. */
9582 enum elf_nds32_reloc_type checked_types[] =
9583 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9585 int reloc_off = 0, cond_removed = 0, convertible;
9587 int seq_len; /* Original length of instruction sequence. */
9588 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9589 int pic_ext_target = 0, first_size;
9591 bfd_signed_vma foff;
9592 uint32_t insn, re_insn = 0;
9593 uint16_t insn16, re_insn16 = 0;
9594 unsigned long reloc, cond_reloc;
9596 irelend = internal_relocs + sec->reloc_count;
9597 seq_len = GET_SEQ_LEN (irel->r_addend);
9598 laddr = irel->r_offset;
9599 *insn_len = seq_len;
9601 convertible = IS_1ST_CONVERT (irel->r_addend);
9608 /* Get all needed relocations. */
9610 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9611 R_NDS32_HI20_RELA, laddr + first_size);
9613 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9614 R_NDS32_LO12S0_ORI_RELA,
9615 laddr + first_size + 4);
9617 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
9620 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9621 checked_types[i], laddr);
9622 if (cond_irelfn != irelend)
9626 if (hi_irelfn == irelend || lo_irelfn == irelend || cond_irelfn == irelend)
9628 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
9633 /* Get the value of the symbol referred to by the reloc. */
9634 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9637 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9638 || foff >= CONSERVATIVE_24BIT_S1)
9641 /* Get the all corresponding instructions. */
9642 if (first_size == 4)
9644 insn = bfd_getb32 (contents + laddr);
9645 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9649 insn16 = bfd_getb16 (contents + laddr);
9650 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9653 /* For simplicity of coding, we are going to modify the section
9654 contents, the section relocs, and the BFD symbol table. We
9655 must tell the rest of the code not to free up this
9656 information. It would be possible to instead create a table
9657 of changes which have to be made, as is done in coff-mips.c;
9658 that would be more work, but would require less memory when
9659 the linker is run. */
9661 if (re_insn16 && foff >= -ACCURATE_8BIT_S1 - first_size
9662 && foff < ACCURATE_8BIT_S1 - first_size)
9664 if (!(seq_len & 0x2))
9666 /* Don't convert it to 16-bit now, keep this as relaxable
9667 for ``label reloc; INSN1a''6. */
9668 /* Save comp_insn32 to buffer. */
9669 bfd_putb32 (re_insn, contents + irel->r_offset);
9671 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9672 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9673 cond_reloc = R_NDS32_INSN16;
9677 /* Not optimize for speed; convert sequence to 16-bit. */
9678 /* Save comp_insn16 to buffer. */
9679 bfd_putb16 (re_insn16, contents + irel->r_offset);
9681 reloc = R_NDS32_9_PCREL_RELA;
9682 cond_reloc = R_NDS32_NONE;
9686 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9687 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9688 && foff < ACCURATE_14BIT_S1 - first_size))
9690 /* beqs label ; 15_PCREL */
9691 bfd_putb32 (re_insn, contents + irel->r_offset);
9693 reloc = R_NDS32_15_PCREL_RELA;
9694 cond_reloc = R_NDS32_NONE;
9697 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9698 && foff >= -CONSERVATIVE_16BIT_S1
9699 && foff < CONSERVATIVE_16BIT_S1)
9701 /* beqz label ; 17_PCREL */
9702 bfd_putb32 (re_insn, contents + irel->r_offset);
9704 reloc = R_NDS32_17_PCREL_RELA;
9705 cond_reloc = R_NDS32_NONE;
9708 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
9709 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
9711 /* Relax to one of the following 3 variations
9713 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
9715 bnes38 rt, $1 ; LONGJUMP2
9719 case 4-4; 1st insn not convertible, others don't care
9720 bne rt, ra, $1 ; LONGJUMP2
9724 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9725 bne rt, ra, $1 ; LONGJUMP2
9729 /* Offset for first instruction. */
9731 /* Use j label as second instruction. */
9732 *insn_len = 4 + first_size;
9734 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9735 reloc = R_NDS32_LONGJUMP2;
9736 cond_reloc = R_NDS32_25_PLTREL;
9741 if (cond_removed == 1)
9743 /* Set all relocations. */
9744 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9745 irel->r_addend = hi_irelfn->r_addend;
9747 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9749 cond_irelfn->r_addend = 0;
9750 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9755 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
9756 irel->r_addend = irel->r_addend;
9757 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9761 if ((seq_len ^ *insn_len ) & 0x2)
9763 insn16 = NDS32_NOP16;
9764 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9765 lo_irelfn->r_offset = *insn_len;
9766 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9768 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9772 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9777 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
9780 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9781 Elf_Internal_Rela *internal_relocs, int *insn_len,
9782 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9783 Elf_Internal_Shdr *symtab_hdr)
9785 /* The pattern for LONGCALL4. Support for function cse.
9786 sethi ta, hi20(symbol) ; LONGCALL4/HI20
9787 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
9788 jral ta ; PTR_RES/EMPTY/INSN16 */
9792 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
9793 Elf_Internal_Rela *irelend;
9794 int pic_ext_target = 0;
9795 bfd_signed_vma foff;
9797 irelend = internal_relocs + sec->reloc_count;
9798 laddr = irel->r_offset;
9800 /* Get the reloc for the address from which the register is
9801 being loaded. This reloc will tell us which function is
9802 actually being called. */
9803 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9804 R_NDS32_HI20_RELA, laddr);
9806 if (hi_irel == irelend)
9808 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9813 /* Get the value of the symbol referred to by the reloc. */
9814 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
9817 /* This condition only happened when symbol is undefined. */
9818 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9819 || foff >= CONSERVATIVE_24BIT_S1)
9822 /* Relax to: jal symbol; 25_PCREL */
9823 /* For simplicity of coding, we are going to modify the section
9824 contents, the section relocs, and the BFD symbol table. We
9825 must tell the rest of the code not to free up this
9826 information. It would be possible to instead create a table
9827 of changes which have to be made, as is done in coff-mips.c;
9828 that would be more work, but would require less memory when
9829 the linker is run. */
9831 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9832 R_NDS32_PTR_RESOLVED, irel->r_addend);
9833 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9834 R_NDS32_EMPTY, irel->r_addend);
9836 if (ptr_irel == irelend || em_irel == irelend)
9838 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9842 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
9843 insn = bfd_getb32 (contents + irel->r_addend);
9844 if (insn & 0x80000000)
9847 /* Replace the long call with a jal. */
9848 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
9849 R_NDS32_25_PCREL_RELA);
9850 ptr_irel->r_addend = 1;
9852 /* We don't resolve this here but resolve it in relocate_section. */
9854 bfd_putb32 (insn, contents + em_irel->r_offset);
9857 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9859 /* If there is function cse, HI20 can not remove now. */
9860 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9861 R_NDS32_LONGCALL4, laddr);
9862 if (call_irel == irelend)
9866 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
9869 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9870 R_NDS32_INSN16, irel->r_addend);
9871 if (insn_irel != irelend)
9873 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9878 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
9881 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9882 Elf_Internal_Rela *internal_relocs, int *insn_len,
9883 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9884 Elf_Internal_Shdr *symtab_hdr)
9886 /* The pattern for LONGCALL5.
9887 bltz rt, .L1 ; LONGCALL5/17_PCREL
9888 jal symbol ; 25_PCREL
9893 Elf_Internal_Rela *cond_irel, *irelend;
9894 int pic_ext_target = 0;
9895 bfd_signed_vma foff;
9897 irelend = internal_relocs + sec->reloc_count;
9898 laddr = irel->r_offset;
9899 insn = bfd_getb32 (contents + laddr);
9901 /* Get the reloc for the address from which the register is
9902 being loaded. This reloc will tell us which function is
9903 actually being called. */
9905 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9906 R_NDS32_25_PCREL_RELA, irel->r_addend);
9907 if (cond_irel == irelend)
9909 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
9914 /* Get the value of the symbol referred to by the reloc. */
9915 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
9918 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9919 || foff >= CONSERVATIVE_16BIT_S1)
9922 /* Relax to bgezal rt, label ; 17_PCREL
9923 or bltzal rt, label ; 17_PCREL */
9925 /* Convert to complimentary conditional call. */
9926 insn = CONVERT_CONDITION_CALL (insn);
9928 /* For simplicity of coding, we are going to modify the section
9929 contents, the section relocs, and the BFD symbol table. We
9930 must tell the rest of the code not to free up this
9931 information. It would be possible to instead create a table
9932 of changes which have to be made, as is done in coff-mips.c;
9933 that would be more work, but would require less memory when
9934 the linker is run. */
9936 /* Modify relocation and contents. */
9938 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
9940 /* Replace the long call with a bgezal. */
9941 bfd_putb32 (insn, contents + cond_irel->r_offset);
9944 /* Clean unnessary relocations. */
9945 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9947 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9948 R_NDS32_17_PCREL_RELA, laddr);
9950 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
9955 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
9958 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9959 Elf_Internal_Rela *internal_relocs, int *insn_len,
9960 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9961 Elf_Internal_Shdr *symtab_hdr)
9963 /* The pattern for LONGCALL6.
9964 bltz rt, .L1 ; LONGCALL6/17_PCREL
9965 sethi ta, hi20(symbol) ; HI20/PTR
9966 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
9967 jral ta ; PTR_RES/EMPTY/INSN16
9972 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
9973 int pic_ext_target = 0;
9974 bfd_signed_vma foff;
9976 irelend = internal_relocs + sec->reloc_count;
9977 laddr = irel->r_offset;
9979 /* Get the reloc for the address from which the register is
9980 being loaded. This reloc will tell us which function is
9981 actually being called. */
9982 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9983 R_NDS32_EMPTY, irel->r_addend);
9985 if (em_irel == irelend)
9987 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
9992 /* Get the value of the symbol referred to by the reloc. */
9993 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
9996 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9997 || foff >= CONSERVATIVE_24BIT_S1)
10000 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10001 insn = bfd_getb32 (contents + irel->r_addend);
10002 if (insn & 0x80000000)
10005 insn = bfd_getb32 (contents + laddr);
10006 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10008 /* Relax to bgezal rt, label ; 17_PCREL
10009 or bltzal rt, label ; 17_PCREL */
10011 /* Convert to complimentary conditional call. */
10013 insn = CONVERT_CONDITION_CALL (insn);
10014 bfd_putb32 (insn, contents + em_irel->r_offset);
10017 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10019 /* Set resolved relocation. */
10021 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10022 R_NDS32_PTR_RESOLVED, irel->r_addend);
10023 if (cond_irel == irelend)
10025 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10026 "R_NDS32_LONGCALL6", irel->r_offset);
10029 cond_irel->r_addend = 1;
10031 /* Clear relocations. */
10034 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10037 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10038 R_NDS32_17_PCREL_RELA, laddr);
10039 if (cond_irel != irelend)
10040 cond_irel->r_info =
10041 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10044 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10045 R_NDS32_INSN16, irel->r_addend);
10046 if (cond_irel != irelend)
10047 cond_irel->r_info =
10048 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10051 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10053 /* Relax to the following instruction sequence
10054 bltz rt, .L1 ; LONGCALL2/17_PCREL
10055 jal symbol ; 25_PCREL/PTR_RES
10058 /* Convert instruction. */
10060 bfd_putb32 (insn, contents + em_irel->r_offset);
10062 /* Convert relocations. */
10063 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10064 R_NDS32_25_PCREL_RELA);
10066 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10068 /* Set resolved relocation. */
10070 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10071 R_NDS32_PTR_RESOLVED, irel->r_addend);
10072 if (cond_irel == irelend)
10074 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10075 "R_NDS32_LONGCALL6", irel->r_offset);
10078 cond_irel->r_addend = 1;
10081 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10082 R_NDS32_INSN16, irel->r_addend);
10083 if (cond_irel != irelend)
10084 cond_irel->r_info =
10085 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10090 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10093 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10094 Elf_Internal_Rela *internal_relocs, int *insn_len,
10095 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10096 Elf_Internal_Shdr *symtab_hdr)
10098 /* The pattern for LONGJUMP4.
10099 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10100 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10101 jr ta ; PTR_RES/INSN16/EMPTY */
10104 int seq_len; /* Original length of instruction sequence. */
10106 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10107 int pic_ext_target = 0;
10108 bfd_signed_vma foff;
10110 irelend = internal_relocs + sec->reloc_count;
10111 seq_len = GET_SEQ_LEN (irel->r_addend);
10112 laddr = irel->r_offset;
10113 *insn_len = seq_len;
10115 /* Get the reloc for the address from which the register is
10116 being loaded. This reloc will tell us which function is
10117 actually being called. */
10119 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10120 R_NDS32_HI20_RELA, laddr);
10122 if (hi_irel == irelend)
10124 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10129 /* Get the value of the symbol referred to by the reloc. */
10130 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
10133 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
10134 || foff < -CONSERVATIVE_24BIT_S1)
10137 /* Convert it to "j label", it may be converted to j8 in the final
10138 pass of relaxation. Therefore, we do not consider this currently. */
10139 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10140 R_NDS32_PTR_RESOLVED, irel->r_addend);
10141 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10142 R_NDS32_EMPTY, irel->r_addend);
10144 if (ptr_irel == irelend || em_irel == irelend)
10146 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10152 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10153 ptr_irel->r_addend = 1;
10155 /* Write instruction. */
10157 bfd_putb32 (insn, contents + em_irel->r_offset);
10159 /* Clear relocations. */
10160 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10162 /* If there is function cse, HI20 can not remove now. */
10163 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10164 R_NDS32_LONGJUMP4, laddr);
10165 if (call_irel == irelend)
10169 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10175 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
10178 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10179 Elf_Internal_Rela *internal_relocs, int *insn_len,
10180 int *seq_len, bfd_byte *contents,
10181 Elf_Internal_Sym *isymbuf,
10182 Elf_Internal_Shdr *symtab_hdr)
10184 /* There are 2 variations for LONGJUMP5
10185 case 2-4; 1st insn convertible, 16-bit on.
10186 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10187 j label ; 25_PCREL/INSN16
10190 case 4-4; 1st insn not convertible
10191 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10192 j label ; 25_PCREL/INSN16
10196 Elf_Internal_Rela *cond_irel, *irelend;
10197 int pic_ext_target = 0;
10199 bfd_signed_vma foff;
10200 uint32_t insn, re_insn = 0;
10201 uint16_t insn16, re_insn16 = 0;
10202 unsigned long reloc;
10204 enum elf_nds32_reloc_type checked_types[] =
10205 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10206 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10208 irelend = internal_relocs + sec->reloc_count;
10209 laddr = irel->r_offset;
10211 /* Get the reloc for the address from which the register is
10212 being loaded. This reloc will tell us which function is
10213 actually being called. */
10216 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10217 R_NDS32_25_PCREL_RELA, irel->r_addend);
10218 if (cond_irel == irelend)
10220 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10225 /* Get the value of the symbol referred to by the reloc. */
10226 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10229 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
10230 || foff >= CONSERVATIVE_16BIT_S1)
10233 /* Get the all corresponding instructions. */
10234 insn = bfd_getb32 (contents + laddr);
10235 /* Check instruction size. */
10236 if (insn & 0x80000000)
10239 insn16 = insn >> 16;
10240 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10243 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10245 if (N32_OP6 (re_insn) == N32_OP6_BR1
10246 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10248 /* beqs label ; 15_PCREL. */
10249 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10250 reloc = R_NDS32_15_PCREL_RELA;
10252 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10253 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10255 /* beqz label ; 17_PCREL. */
10256 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10257 reloc = R_NDS32_17_PCREL_RELA;
10259 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
10260 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
10262 /* beqc label ; 9_PCREL. */
10263 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10264 reloc = R_NDS32_WORD_9_PCREL_RELA;
10269 /* Set all relocations. */
10270 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
10272 /* Clean relocations. */
10273 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10274 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10276 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10277 checked_types[i], laddr);
10278 if (cond_irel != irelend)
10281 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10283 /* If the branch instruction is 2 byte, it cannot remove
10284 directly. Only convert it to nop16 and remove it after
10285 checking alignment issue. */
10286 insn16 = NDS32_NOP16;
10287 bfd_putb16 (insn16, contents + laddr);
10288 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10291 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10300 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
10303 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10304 Elf_Internal_Rela *internal_relocs, int *insn_len,
10305 int *seq_len, bfd_byte *contents,
10306 Elf_Internal_Sym *isymbuf,
10307 Elf_Internal_Shdr *symtab_hdr)
10309 /* There are 5 variations for LONGJUMP6
10310 case : 2-4-4-4; 1st insn convertible, 16-bit on.
10311 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10312 sethi ta, hi20(symbol) ; HI20/PTR
10313 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10314 jr ta ; PTR_RES/INSN16/EMPTY
10317 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
10318 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10319 sethi ta, hi20(symbol) ; HI20/PTR
10320 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10321 jr ta ; PTR_RES/INSN16/EMPTY
10324 enum elf_nds32_reloc_type checked_types[] =
10325 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10326 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10328 int reloc_off = 0, cond_removed = 0;
10330 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
10331 int pic_ext_target = 0;
10333 bfd_signed_vma foff;
10334 uint32_t insn, re_insn = 0;
10335 uint16_t insn16, re_insn16 = 0;
10336 unsigned long reloc;
10338 irelend = internal_relocs + sec->reloc_count;
10339 laddr = irel->r_offset;
10341 /* Get the reloc for the address from which the register is
10342 being loaded. This reloc will tell us which function is
10343 actually being called. */
10344 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10345 R_NDS32_EMPTY, irel->r_addend);
10347 if (em_irel == irelend)
10349 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
10354 /* Get the value of the symbol referred to by the reloc. */
10355 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
10358 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
10359 || foff >= CONSERVATIVE_24BIT_S1)
10362 insn = bfd_getb32 (contents + laddr);
10363 /* Check instruction size. */
10364 if (insn & 0x80000000)
10367 insn16 = insn >> 16;
10368 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10371 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10373 /* For simplicity of coding, we are going to modify the section
10374 contents, the section relocs, and the BFD symbol table. We
10375 must tell the rest of the code not to free up this
10376 information. It would be possible to instead create a table
10377 of changes which have to be made, as is done in coff-mips.c;
10378 that would be more work, but would require less memory when
10379 the linker is run. */
10381 if (N32_OP6 (re_insn) == N32_OP6_BR1
10382 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10384 /* beqs label ; 15_PCREL */
10385 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10386 reloc = R_NDS32_15_PCREL_RELA;
10389 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10390 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10392 /* beqz label ; 17_PCREL */
10393 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10394 reloc = R_NDS32_17_PCREL_RELA;
10397 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10398 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10400 /* Relax to one of the following 2 variations
10402 case 2-4; 1st insn convertible, 16-bit on.
10403 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10404 j label ; 25_PCREL/INSN16
10407 case 4-4; 1st insn not convertible
10408 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10409 j label ; 25_PCREL/INSN16
10412 /* Use j label as second instruction. */
10414 reloc = R_NDS32_25_PCREL_RELA;
10415 bfd_putb32 (insn, contents + em_irel->r_offset);
10420 /* Set all relocations. */
10421 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
10424 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10425 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
10426 cond_irel->r_addend = 1;
10428 /* Use INSN16 of first branch instruction to distinguish if keeping
10429 INSN16 of final instruction or not. */
10430 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10431 R_NDS32_INSN16, irel->r_offset);
10432 if (insn_irel == irelend)
10434 /* Clean the final INSN16. */
10436 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10437 R_NDS32_INSN16, em_irel->r_offset);
10438 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10442 if (cond_removed == 1)
10446 /* Clear relocations. */
10447 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10449 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10452 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10453 checked_types[i], laddr);
10454 if (cond_irel != irelend)
10457 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10459 /* If the branch instruction is 2 byte, it cannot remove
10460 directly. Only convert it to nop16 and remove it after
10461 checking alignment issue. */
10462 insn16 = NDS32_NOP16;
10463 bfd_putb16 (insn16, contents + laddr);
10464 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10467 cond_irel->r_info =
10468 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10474 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10475 R_NDS32_LONGJUMP5);
10481 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
10484 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10485 Elf_Internal_Rela *internal_relocs, int *insn_len,
10486 int *seq_len, bfd_byte *contents,
10487 Elf_Internal_Sym *isymbuf,
10488 Elf_Internal_Shdr *symtab_hdr)
10490 /* There are 2 variations for LONGJUMP5
10491 case 2-4; 1st insn convertible, 16-bit on.
10492 movi55 ta, imm11 ; LONGJUMP7/INSN16
10493 beq rt, ta, label ; 15_PCREL
10495 case 4-4; 1st insn not convertible
10496 movi55 ta, imm11 ; LONGJUMP7/INSN16
10497 beq rt, ta, label ; 15_PCREL */
10500 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
10501 int pic_ext_target = 0;
10502 bfd_signed_vma foff;
10503 uint32_t insn, re_insn = 0;
10507 irelend = internal_relocs + sec->reloc_count;
10508 laddr = irel->r_offset;
10510 /* Get the reloc for the address from which the register is
10511 being loaded. This reloc will tell us which function is
10512 actually being called. */
10515 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10516 R_NDS32_15_PCREL_RELA, irel->r_addend);
10517 if (cond_irel == irelend)
10519 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
10524 /* Get the value of the symbol referred to by the reloc. */
10525 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10528 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_8BIT_S1
10529 || foff >= CONSERVATIVE_8BIT_S1)
10532 /* Get the first instruction for its size. */
10533 insn = bfd_getb32 (contents + laddr);
10534 if (insn & 0x80000000)
10537 /* Get the immediate from movi55. */
10538 imm11 = N16_IMM5S (insn >> 16);
10542 /* Get the immediate from movi. */
10543 imm11 = N32_IMM20S (insn);
10546 /* Get the branch instruction. */
10547 insn = bfd_getb32 (contents + irel->r_addend);
10548 /* Convert instruction to BR3. */
10549 if ((insn >> 14) & 0x1)
10550 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
10552 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
10554 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10556 /* Set all relocations. */
10557 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10558 R_NDS32_WORD_9_PCREL_RELA);
10560 /* Clean relocations. */
10561 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10562 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10563 R_NDS32_INSN16, irel->r_offset);
10564 if (insn_irel != irelend)
10568 /* If the first insntruction is 16bit, convert it to nop16. */
10569 insn16 = NDS32_NOP16;
10570 bfd_putb16 (insn16, contents + laddr);
10571 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10574 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10582 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
10584 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
10587 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
10588 asection *sec, Elf_Internal_Rela *irel,
10589 Elf_Internal_Rela *internal_relocs, int *insn_len,
10590 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10591 Elf_Internal_Shdr *symtab_hdr, int load_store_relax)
10593 int eliminate_sethi = 0, range_type;
10595 bfd_vma local_sda, laddr;
10596 int seq_len; /* Original length of instruction sequence. */
10598 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
10599 bfd_vma access_addr = 0;
10600 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10601 enum elf_nds32_reloc_type checked_types[] =
10602 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
10603 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
10604 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
10605 R_NDS32_TLS_LE_HI20
10608 irelend = internal_relocs + sec->reloc_count;
10609 seq_len = GET_SEQ_LEN (irel->r_addend);
10610 laddr = irel->r_offset;
10611 *insn_len = seq_len;
10613 /* Get the high part relocation. */
10614 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10616 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10617 checked_types[i], laddr);
10618 if (hi_irelfn != irelend)
10622 if (hi_irelfn == irelend)
10624 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
10629 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
10630 nds32_elf_final_sda_base (sec->output_section->owner,
10631 link_info, &local_sda, FALSE);
10633 switch (ELF32_R_TYPE (hi_irelfn->r_info))
10635 case R_NDS32_HI20_RELA:
10636 insn = bfd_getb32 (contents + laddr);
10638 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10640 if (range_type == NDS32_LOADSTORE_IMM)
10642 struct elf_link_hash_entry *h = NULL;
10645 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
10647 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
10648 h = elf_sym_hashes (abfd)[indx];
10651 if ((access_addr < CONSERVATIVE_20BIT)
10652 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10654 eliminate_sethi = 1;
10658 /* This is avoid to relax symbol address which is fixed
10659 relocations. Ex: _stack. */
10660 if (h && bfd_is_abs_section (h->root.u.def.section))
10664 if (!load_store_relax)
10667 /* Case for set gp register. */
10668 if (N32_RT5 (insn) == REG_GP)
10671 if (range_type == NDS32_LOADSTORE_FLOAT_S
10672 || range_type == NDS32_LOADSTORE_FLOAT_S)
10674 range_l = sdata_range[0][0];
10675 range_h = sdata_range[0][1];
10679 range_l = sdata_range[1][0];
10680 range_h = sdata_range[1][1];
10684 case R_NDS32_GOT_HI20:
10686 calculate_got_memory_address (abfd, link_info, hi_irelfn, symtab_hdr);
10688 /* If this symbol is not in .got, the return value will be -1.
10689 Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
10690 a negative offset is allowed. */
10691 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10692 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10693 eliminate_sethi = 1;
10696 case R_NDS32_PLT_GOTREL_HI20:
10697 access_addr = calculate_plt_memory_address (abfd, link_info, isymbuf,
10698 hi_irelfn, symtab_hdr);
10700 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10701 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10702 eliminate_sethi = 1;
10705 case R_NDS32_GOTOFF_HI20:
10707 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10709 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10710 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10711 eliminate_sethi = 1;
10714 case R_NDS32_GOTPC_HI20:
10715 /* The access_addr must consider r_addend of hi_irel. */
10716 access_addr = sec->output_section->vma + sec->output_offset
10717 + irel->r_offset + hi_irelfn->r_addend;
10719 if ((bfd_signed_vma) (local_sda - access_addr) < CONSERVATIVE_20BIT
10720 && (bfd_signed_vma) (local_sda - access_addr) >= -CONSERVATIVE_20BIT)
10721 eliminate_sethi = 1;
10724 case R_NDS32_TLS_LE_HI20:
10726 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10727 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10728 access_addr -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10729 if ((range_type == NDS32_LOADSTORE_IMM)
10730 && (bfd_signed_vma) (access_addr) < CONSERVATIVE_20BIT
10731 && (bfd_signed_vma) (access_addr) >= -CONSERVATIVE_20BIT)
10732 eliminate_sethi = 1;
10739 /* Delete sethi instruction. */
10740 if (eliminate_sethi == 1
10741 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
10742 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10744 hi_irelfn->r_info =
10745 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
10747 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10753 /* Relax LO12 relocation for nds32_elf_relax_section. */
10756 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
10757 asection *sec, Elf_Internal_Rela *irel,
10758 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
10759 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
10762 bfd_vma local_sda, laddr;
10763 unsigned long reloc;
10764 bfd_vma access_addr;
10765 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10766 Elf_Internal_Rela *irelfn = NULL, *irelend;
10767 struct elf_link_hash_entry *h = NULL;
10770 /* For SDA base relative relaxation. */
10771 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10772 &local_sda, FALSE);
10774 irelend = internal_relocs + sec->reloc_count;
10775 laddr = irel->r_offset;
10776 insn = bfd_getb32 (contents + laddr);
10778 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
10781 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10783 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
10785 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
10786 h = elf_sym_hashes (abfd)[indx];
10789 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
10790 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10792 reloc = R_NDS32_20_RELA;
10793 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10794 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10795 bfd_putb32 (insn, contents + laddr);
10797 /* This is avoid to relax symbol address which is fixed
10798 relocations. Ex: _stack. */
10799 else if (N32_OP6 (insn) == N32_OP6_ORI
10800 && h && bfd_is_abs_section (h->root.u.def.section))
10804 range_l = sdata_range[1][0];
10805 range_h = sdata_range[1][1];
10806 switch (ELF32_R_TYPE (irel->r_info))
10808 case R_NDS32_LO12S0_RELA:
10809 reloc = R_NDS32_SDA19S0_RELA;
10811 case R_NDS32_LO12S1_RELA:
10812 reloc = R_NDS32_SDA18S1_RELA;
10814 case R_NDS32_LO12S2_RELA:
10815 reloc = R_NDS32_SDA17S2_RELA;
10817 case R_NDS32_LO12S2_DP_RELA:
10818 range_l = sdata_range[0][0];
10819 range_h = sdata_range[0][1];
10820 reloc = R_NDS32_SDA12S2_DP_RELA;
10822 case R_NDS32_LO12S2_SP_RELA:
10823 range_l = sdata_range[0][0];
10824 range_h = sdata_range[0][1];
10825 reloc = R_NDS32_SDA12S2_SP_RELA;
10831 /* There are range_h and range_l because linker has to promise
10832 all sections move cross one page together. */
10833 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
10834 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10836 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
10838 /* Maybe we should add R_NDS32_INSN16 reloc type here
10839 or manually do some optimization. sethi can't be
10840 eliminated when updating $gp so the relative ori
10841 needs to be preserved. */
10844 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
10847 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10848 bfd_putb32 (insn, contents + laddr);
10850 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10852 /* SDA17 must keep INSN16 for converting fp_as_gp. */
10853 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
10855 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
10862 /* Relax low part of PIC instruction pattern. */
10865 nds32_elf_relax_piclo12 (struct bfd_link_info *link_info, bfd *abfd,
10866 asection *sec, Elf_Internal_Rela *irel,
10867 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10868 Elf_Internal_Shdr *symtab_hdr)
10871 bfd_vma local_sda, laddr;
10872 bfd_signed_vma foff;
10873 unsigned long reloc;
10875 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10876 &local_sda, FALSE);
10877 laddr = irel->r_offset;
10878 insn = bfd_getb32 (contents + laddr);
10880 if (N32_OP6 (insn) != N32_OP6_ORI)
10883 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
10885 foff = calculate_got_memory_address (abfd, link_info, irel,
10886 symtab_hdr) - local_sda;
10887 reloc = R_NDS32_GOT20;
10889 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
10891 foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
10892 symtab_hdr) - local_sda;
10893 reloc = R_NDS32_PLT_GOTREL_LO20;
10895 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
10897 foff = calculate_memory_address (abfd, irel, isymbuf,
10898 symtab_hdr) - local_sda;
10899 reloc = R_NDS32_GOTOFF;
10901 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12)
10903 foff = local_sda - sec->output_section->vma + sec->output_offset
10904 + irel->r_offset + irel->r_addend;
10905 reloc = R_NDS32_GOTPC20;
10910 if ((foff < CONSERVATIVE_20BIT) && (foff >= -CONSERVATIVE_20BIT))
10912 /* Turn into MOVI. */
10913 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10914 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10915 bfd_putb32 (insn, contents + laddr);
10919 /* Relax low part of LE TLS instruction pattern. */
10922 nds32_elf_relax_letlslo12 (struct bfd_link_info *link_info, bfd *abfd,
10923 Elf_Internal_Rela *irel,
10924 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10925 Elf_Internal_Shdr *symtab_hdr)
10929 bfd_signed_vma foff;
10930 unsigned long reloc;
10932 laddr = irel->r_offset;
10933 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10934 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10935 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10936 insn = bfd_getb32 (contents + laddr);
10938 if ( (bfd_signed_vma) (foff) < CONSERVATIVE_20BIT
10939 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_20BIT)
10941 /* Pattern sethi-ori transform to movi. */
10942 reloc = R_NDS32_TLS_LE_20;
10943 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10944 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10945 bfd_putb32 (insn, contents + laddr);
10949 /* Relax LE TLS calculate address instruction pattern. */
10952 nds32_elf_relax_letlsadd (struct bfd_link_info *link_info, bfd *abfd,
10953 asection *sec, Elf_Internal_Rela *irel,
10954 Elf_Internal_Rela *internal_relocs,
10955 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10956 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
10958 /* Local TLS non-pic
10959 sethi ta, hi20(symbol@tpoff) ; TLS_LE_HI20
10960 ori ta, ta, lo12(symbol@tpoff) ; TLS_LE_LO12
10961 add ra, ta, tp ; TLS_LE_ADD */
10965 bfd_signed_vma foff;
10966 Elf_Internal_Rela *i1_irelfn, *irelend;
10968 irelend = internal_relocs + sec->reloc_count;
10969 laddr = irel->r_offset;
10970 insn = bfd_getb32 (contents + laddr);
10971 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10972 R_NDS32_PTR_RESOLVED);
10973 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10974 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10975 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10977 /* The range is +/-16k. */
10978 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
10979 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
10981 /* Transform add to addi. */
10982 insn = N32_TYPE2 (ADDI, N32_RT5 (insn), N32_RB5 (insn), 0);
10984 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
10986 bfd_putb32 (insn, contents + laddr);
10987 if (i1_irelfn != irelend)
10989 i1_irelfn->r_addend |= 1;
10995 /* Relax LE TLS load store instruction pattern. */
10998 nds32_elf_relax_letlsls (struct bfd_link_info *link_info, bfd *abfd,
10999 asection *sec, Elf_Internal_Rela *irel,
11000 Elf_Internal_Rela *internal_relocs,
11001 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11002 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11007 bfd_signed_vma foff;
11008 Elf_Internal_Rela *i1_irelfn, *irelend;
11011 irelend = internal_relocs + sec->reloc_count;
11012 laddr = irel->r_offset;
11013 insn = bfd_getb32 (contents + laddr);
11014 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11015 R_NDS32_PTR_RESOLVED);
11016 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11017 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
11018 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
11020 switch ((N32_OP6 (insn) << 8) | (insn & 0xff))
11022 case (N32_OP6_MEM << 8) | N32_MEM_LB:
11023 case (N32_OP6_MEM << 8) | N32_MEM_SB:
11024 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11025 /* The range is +/-16k. */
11026 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
11027 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
11030 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11032 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
11036 /* Fall through. */
11037 case (N32_OP6_MEM << 8) | N32_MEM_LH:
11038 case (N32_OP6_MEM << 8) | N32_MEM_SH:
11039 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11040 /* The range is +/-32k. */
11041 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S1
11042 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S1)
11045 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11047 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S1);
11051 /* Fall through. */
11052 case (N32_OP6_MEM << 8) | N32_MEM_LW:
11053 case (N32_OP6_MEM << 8) | N32_MEM_SW:
11054 /* The range is +/-64k. */
11055 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S2
11056 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S2)
11059 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11061 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S2);
11065 /* Fall through. */
11072 bfd_putb32 (insn, contents + laddr);
11073 if (i1_irelfn != irelend)
11075 i1_irelfn->r_addend |= 1;
11081 /* Relax PTR relocation for nds32_elf_relax_section. */
11084 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11085 Elf_Internal_Rela *internal_relocs, int *insn_len,
11086 int *seq_len, bfd_byte *contents)
11088 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11090 irelend = internal_relocs + sec->reloc_count;
11093 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11094 R_NDS32_PTR_RESOLVED, irel->r_addend);
11096 if (re_irel == irelend)
11098 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11103 if (re_irel->r_addend != 1)
11106 /* Pointed target is relaxed and no longer needs this void *,
11107 change the type to NONE. */
11108 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11110 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11111 not exist, it means only count 1 and remove it directly. */
11112 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11113 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11114 R_NDS32_PTR_COUNT);
11115 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11117 if (count_irel != irelend)
11119 if (--count_irel->r_addend > 0)
11123 if (ptr_irel != irelend)
11126 /* If the PTR_COUNT is already 0, remove current instruction. */
11127 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11132 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11135 nds32_elf_relax_pltgot_suff (struct bfd_link_info *link_info, bfd *abfd,
11136 asection *sec, Elf_Internal_Rela *irel,
11137 Elf_Internal_Rela *internal_relocs,
11138 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11139 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11142 bfd_signed_vma foff;
11143 Elf_Internal_Rela *i1_irelfn, *irelend;
11144 bfd_vma local_sda, laddr;
11146 irelend = internal_relocs + sec->reloc_count;
11147 laddr = irel->r_offset;
11148 insn = bfd_getb32 (contents + laddr);
11150 /* FIXME: It's a little trouble to turn JRAL5 to JAL since
11151 we need additional space. It might be help if we could
11152 borrow some space from instructions to be eliminated
11153 such as sethi, ori, add. */
11154 if (insn & 0x80000000)
11157 if (nds32_elf_check_dup_relocs
11158 (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
11162 find_relocs_at_address (irel, internal_relocs, irelend,
11163 R_NDS32_PTR_RESOLVED);
11166 The boundary should be reduced since the .plt section hasn't
11167 been created and the address of specific entry is still unknown
11168 Maybe the range between the function call and the begin of the
11169 .text section can be used to decide if the .plt is in the range
11170 of function call. */
11172 if (N32_OP6 (insn) == N32_OP6_ALU1
11173 && N32_SUB5 (insn) == N32_ALU1_ADD)
11175 /* Get the value of the symbol referred to by the reloc. */
11176 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11177 &local_sda, FALSE);
11178 foff = (bfd_signed_vma) (calculate_plt_memory_address
11179 (abfd, link_info, isymbuf, irel,
11180 symtab_hdr) - local_sda);
11181 /* This condition only happened when symbol is undefined. */
11185 if (foff < -CONSERVATIVE_19BIT || foff >= CONSERVATIVE_19BIT)
11187 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11188 R_NDS32_PLT_GOTREL_LO19);
11190 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
11192 else if (N32_OP6 (insn) == N32_OP6_JREG
11193 && N32_SUB5 (insn) == N32_JREG_JRAL)
11195 /* Get the value of the symbol referred to by the reloc. */
11197 calculate_plt_offset (abfd, sec, link_info, isymbuf, irel, symtab_hdr);
11198 /* This condition only happened when symbol is undefined. */
11201 if (foff < -CONSERVATIVE_24BIT_S1 || foff >= CONSERVATIVE_24BIT_S1)
11203 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
11209 bfd_putb32 (insn, contents + laddr);
11210 if (i1_irelfn != irelend)
11212 i1_irelfn->r_addend |= 1;
11217 /* Relax GOT_SUFF relocation for nds32_elf_relax_section. */
11220 nds32_elf_relax_got_suff (struct bfd_link_info *link_info, bfd *abfd,
11221 asection *sec, Elf_Internal_Rela *irel,
11222 Elf_Internal_Rela *internal_relocs,
11223 bfd_byte *contents, Elf_Internal_Shdr *symtab_hdr,
11224 bfd_boolean *again)
11227 bfd_signed_vma foff;
11228 Elf_Internal_Rela *i1_irelfn, *irelend;
11229 bfd_vma local_sda, laddr;
11231 irelend = internal_relocs + sec->reloc_count;
11232 laddr = irel->r_offset;
11233 insn = bfd_getb32 (contents + laddr);
11234 if (insn & 0x80000000)
11237 if (nds32_elf_check_dup_relocs
11238 (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
11241 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11242 R_NDS32_PTR_RESOLVED);
11244 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11245 &local_sda, FALSE);
11246 foff = calculate_got_memory_address (abfd, link_info, irel,
11247 symtab_hdr) - local_sda;
11249 if (foff < CONSERVATIVE_19BIT && foff >= -CONSERVATIVE_19BIT)
11251 /* Turn LW to LWI.GP. Change relocation type to R_NDS32_GOT_REL. */
11252 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11254 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
11255 bfd_putb32 (insn, contents + laddr);
11256 if (i1_irelfn != irelend)
11258 i1_irelfn->r_addend |= 1;
11264 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11267 nds32_elf_relax_gotoff_suff (struct bfd_link_info *link_info, bfd *abfd,
11268 asection *sec, Elf_Internal_Rela *irel,
11269 Elf_Internal_Rela *internal_relocs,
11270 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11271 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11273 int opc_insn_gotoff;
11275 bfd_signed_vma foff;
11276 Elf_Internal_Rela *i1_irelfn, *i2_irelfn, *irelend;
11277 bfd_vma local_sda, laddr;
11279 irelend = internal_relocs + sec->reloc_count;
11280 laddr = irel->r_offset;
11281 insn = bfd_getb32 (contents + laddr);
11283 if (insn & 0x80000000)
11286 if (nds32_elf_check_dup_relocs
11287 (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
11290 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11291 R_NDS32_PTR_RESOLVED);
11292 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11293 &local_sda, FALSE);
11294 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11295 foff = foff - local_sda;
11297 if (foff >= CONSERVATIVE_19BIT || foff < -CONSERVATIVE_19BIT)
11300 /* Concatenate opcode and sub-opcode for switch case.
11301 It may be MEM or ALU1. */
11302 opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
11303 switch (opc_insn_gotoff)
11305 case (N32_OP6_MEM << 8) | N32_MEM_LW:
11306 /* 4-byte aligned. */
11307 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11309 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11311 case (N32_OP6_MEM << 8) | N32_MEM_SW:
11312 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
11314 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11316 case (N32_OP6_MEM << 8) | N32_MEM_LH:
11317 /* 2-byte aligned. */
11318 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
11320 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11322 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11323 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
11325 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11327 case (N32_OP6_MEM << 8) | N32_MEM_SH:
11328 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
11330 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11332 case (N32_OP6_MEM << 8) | N32_MEM_LB:
11333 /* 1-byte aligned. */
11334 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
11336 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11338 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11339 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
11341 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11343 case (N32_OP6_MEM << 8) | N32_MEM_SB:
11344 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
11346 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11348 case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD:
11349 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
11351 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11357 bfd_putb32 (insn, contents + laddr);
11358 if (i1_irelfn != irelend)
11360 i1_irelfn->r_addend |= 1;
11363 if ((i2_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11364 R_NDS32_INSN16)) != irelend)
11365 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11370 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11371 Elf_Internal_Rela *internal_relocs,
11372 bfd_byte *contents,
11373 nds32_elf_blank_t **relax_blank_list,
11374 int optimize, int opt_size)
11376 /* This code block is used to adjust 4-byte alignment by relax a pair
11377 of instruction a time.
11379 It recognizes three types of relocations.
11380 1. R_NDS32_LABEL - a alignment.
11381 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11382 3. is_16bit_NOP () - remove a 16-bit instruction. */
11384 /* TODO: It seems currently implementation only support 4-byte alignment.
11385 We should handle any-alignment. */
11387 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11388 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11389 Elf_Internal_Rela rel_temp;
11390 Elf_Internal_Rela *irelend;
11394 /* Checking for branch relaxation relies on the relocations to
11395 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11396 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11397 sizeof (Elf_Internal_Rela), compar_reloc);
11399 irelend = internal_relocs + sec->reloc_count;
11401 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11402 /* FIXME: Can we generate the right order in assembler?
11403 So we don't have to swapping them here. */
11405 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11406 label_rel < irelend; label_rel++)
11408 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11411 /* Find the first reloc has the same offset with label_rel. */
11412 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11415 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11417 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11419 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11422 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11423 && insn_rel < label_rel)
11425 /* Swap the two reloc if the R_NDS32_INSN16 is
11426 before R_NDS32_LABEL. */
11427 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11428 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11429 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11435 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11436 or higher, remove other R_NDS32_LABEL with lower alignment.
11437 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11438 then the R_NDS32_LABEL sequence is broke. */
11439 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11441 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11443 if (label_rel == NULL)
11445 if (tmp_rel->r_addend < 2)
11446 label_rel = tmp_rel;
11449 else if (tmp_rel->r_addend > 1)
11451 /* Remove all LABEL relocation from label_rel to tmp_rel
11452 including relocations with same offset as tmp_rel. */
11453 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel
11454 || tmp2_rel->r_offset == tmp_rel->r_offset; tmp2_rel++)
11456 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11457 && tmp2_rel->r_addend < 2)
11459 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11465 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11467 /* A new INSN16 which can be converted, so clear label_rel. */
11468 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11470 || is_16bit_NOP (abfd, sec, tmp_rel))
11477 /* Optimized for speed and nothing has not been relaxed.
11478 It's time to align labels.
11479 We may convert a 16-bit instruction right before a label to
11480 32-bit, in order to align the label if necessary
11481 all reloc entries has been sorted by r_offset. */
11482 for (irel = internal_relocs; irel < irelend; irel++)
11484 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11485 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11488 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11490 /* A new INSN16 found, resize the old one. */
11491 if (is_convert_32_to_16
11492 (abfd, sec, irel, internal_relocs, irelend, &insn16)
11493 || is_16bit_NOP (abfd, sec, irel))
11497 /* Previous INSN16 reloc exists, reduce its
11499 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11502 nds32_elf_write_16 (abfd, contents, insn_rel,
11503 internal_relocs, irelend, insn16);
11505 if (!insert_nds32_elf_blank_recalc_total
11506 (relax_blank_list, insn_rel->r_offset + 2, 2))
11509 else if (is_16bit_NOP (abfd, sec, insn_rel))
11511 if (!insert_nds32_elf_blank_recalc_total
11512 (relax_blank_list, insn_rel->r_offset, 2))
11516 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11518 /* Save the new one for later use. */
11522 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11525 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11527 /* Search for label. */
11528 int force_relax = 0;
11530 /* Label on 16-bit instruction or optimization
11531 needless, just reset this reloc. */
11532 insn16 = bfd_getb16 (contents + irel->r_offset);
11533 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
11536 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11541 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
11542 irel->r_offset, 1);
11546 /* Check if there is case which can not be aligned. */
11547 if (irel->r_addend == 2 && address & 0x2)
11552 /* Try to align this label. */
11554 if ((irel->r_addend & 0x1f) < 2)
11556 /* Check if there is a INSN16 at the same address.
11557 Label_rel always seats before insn_rel after
11560 /* Search for INSN16 at LABEL location. If INSN16 is at
11561 same location and this LABEL alignment is lower than 2,
11562 the INSN16 can be converted to 2-byte. */
11563 for (tmp_rel = irel;
11564 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
11567 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
11568 && (is_convert_32_to_16
11569 (abfd, sec, tmp_rel, internal_relocs,
11571 || is_16bit_NOP (abfd, sec, tmp_rel)))
11579 if (force_relax || irel->r_addend == 1 || address & 0x2)
11581 /* Label not aligned. */
11582 /* Previous reloc exists, reduce its size to 16-bit. */
11583 if (is_convert_32_to_16 (abfd, sec, insn_rel,
11584 internal_relocs, irelend, &insn16))
11586 nds32_elf_write_16 (abfd, contents, insn_rel,
11587 internal_relocs, irelend, insn16);
11589 if (!insert_nds32_elf_blank_recalc_total
11590 (relax_blank_list, insn_rel->r_offset + 2, 2))
11593 else if (is_16bit_NOP (abfd, sec, insn_rel))
11595 if (!insert_nds32_elf_blank_recalc_total
11596 (relax_blank_list, insn_rel->r_offset, 2))
11601 /* INSN16 reloc is used. */
11607 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
11608 if (insn_rel && (address & 0x2 || opt_size))
11610 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11613 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
11615 if (!insert_nds32_elf_blank_recalc_total
11616 (relax_blank_list, insn_rel->r_offset + 2, 2))
11618 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11621 else if (is_16bit_NOP (abfd, sec, insn_rel))
11623 if (!insert_nds32_elf_blank_recalc_total
11624 (relax_blank_list, insn_rel->r_offset, 2))
11626 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11634 /* Pick relaxation round. */
11637 nds32_elf_pick_relax (bfd_boolean init, asection *sec, bfd_boolean *again,
11638 struct elf_nds32_link_hash_table *table,
11639 struct bfd_link_info *link_info)
11641 static asection *final_sec, *first_sec = NULL;
11642 static bfd_boolean normal_again = FALSE;
11643 static bfd_boolean set = FALSE;
11644 static bfd_boolean first = TRUE;
11645 int round_table[] = {
11646 NDS32_RELAX_NORMAL_ROUND,
11647 NDS32_RELAX_JUMP_IFC_ROUND,
11648 NDS32_RELAX_EX9_BUILD_ROUND,
11649 NDS32_RELAX_EX9_REPLACE_ROUND,
11651 static int pass = 0;
11652 static int relax_round;
11654 /* The new round. */
11655 if (init && first_sec == sec)
11658 normal_again = FALSE;
11663 /* Run an empty run to get the final section. */
11664 relax_round = NDS32_RELAX_EMPTY_ROUND;
11666 /* It has to enter relax again because we can
11667 not make sure what the final turn is. */
11676 /* Not reenter yet. */
11678 return relax_round;
11681 relax_round = round_table[pass];
11683 if (!init && relax_round == NDS32_RELAX_NORMAL_ROUND && *again)
11684 normal_again = TRUE;
11686 if (!init && final_sec == sec)
11688 switch (relax_round)
11690 case NDS32_RELAX_NORMAL_ROUND:
11693 /* Normal relaxation done. */
11694 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11699 else if (table->target_optimize & NDS32_RELAX_EX9_ON)
11701 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11704 else if (table->ex9_import_file)
11706 /* Import ex9 table. */
11707 if (table->update_ex9_table)
11708 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11710 pass += 3; /* NDS32_RELAX_EX9_REPLACE_ROUND */
11711 nds32_elf_ex9_import_table (link_info);
11716 case NDS32_RELAX_JUMP_IFC_ROUND:
11717 if (!nds32_elf_ifc_finish (link_info))
11718 _bfd_error_handler (_("error: Jump IFC Fail."));
11719 if (table->target_optimize & NDS32_RELAX_EX9_ON)
11725 case NDS32_RELAX_EX9_BUILD_ROUND:
11726 nds32_elf_ex9_finish (link_info);
11730 case NDS32_RELAX_EX9_REPLACE_ROUND:
11731 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11733 /* Do jump IFC optimization again. */
11734 if (!nds32_elf_ifc_finish (link_info))
11735 _bfd_error_handler (_("error: Jump IFC Fail."));
11743 return relax_round;
11747 nds32_elf_relax_section (bfd *abfd, asection *sec,
11748 struct bfd_link_info *link_info, bfd_boolean *again)
11750 nds32_elf_blank_t *relax_blank_list = NULL;
11751 Elf_Internal_Shdr *symtab_hdr;
11752 Elf_Internal_Rela *internal_relocs;
11753 Elf_Internal_Rela *irel;
11754 Elf_Internal_Rela *irelend;
11755 Elf_Internal_Sym *isymbuf = NULL;
11756 bfd_byte *contents = NULL;
11757 bfd_boolean result = TRUE;
11763 /* Target dependnet option. */
11764 struct elf_nds32_link_hash_table *table;
11765 int load_store_relax;
11768 relax_blank_list = NULL;
11772 /* Nothing to do for
11773 * relocatable link or
11774 * non-relocatable section or
11775 * non-code section or
11777 * no reloc entry. */
11778 if (bfd_link_relocatable (link_info)
11779 || (sec->flags & SEC_RELOC) == 0
11780 || (sec->flags & SEC_EXCLUDE) != 0
11781 || (sec->flags & SEC_CODE) == 0
11785 /* 09.12.11 Workaround. */
11786 /* We have to adjust align for R_NDS32_LABEL if needed.
11787 The adjust approach only can fix 2-byte align once. */
11788 if (sec->alignment_power > 2)
11791 /* The optimization type to do. */
11793 table = nds32_elf_hash_table (link_info);
11794 relax_round = nds32_elf_pick_relax (TRUE, sec, again, table, link_info);
11795 switch (relax_round)
11797 case NDS32_RELAX_JUMP_IFC_ROUND:
11798 /* Here is the entrance of ifc jump relaxation. */
11799 if (!nds32_elf_ifc_calc (link_info, abfd, sec))
11801 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11804 case NDS32_RELAX_EX9_BUILD_ROUND:
11805 /* Here is the entrance of ex9 relaxation. There are two pass of
11806 ex9 relaxation. The one is to traverse all instructions and build
11807 the hash table. The other one is to compare instructions and replace
11809 if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
11811 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11814 case NDS32_RELAX_EX9_REPLACE_ROUND:
11815 if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
11819 case NDS32_RELAX_EMPTY_ROUND:
11820 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11823 case NDS32_RELAX_NORMAL_ROUND:
11825 if (sec->reloc_count == 0)
11830 /* The begining of general relaxation. */
11832 if (is_SDA_BASE_set == 0)
11835 is_SDA_BASE_set = 1;
11836 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11838 relax_range_measurement (abfd);
11841 if (is_ITB_BASE_set == 0)
11843 /* Set the _ITB_BASE_. */
11844 if (!nds32_elf_ex9_itb_base (link_info))
11846 _bfd_error_handler (_("%B: error: Cannot set _ITB_BASE_"), abfd);
11847 bfd_set_error (bfd_error_bad_value);
11851 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11852 /* Relocations MUST be kept in memory, because relaxation adjust them. */
11853 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11854 TRUE /* keep_memory */);
11855 if (internal_relocs == NULL)
11858 irelend = internal_relocs + sec->reloc_count;
11859 irel = find_relocs_at_address (internal_relocs, internal_relocs,
11860 irelend, R_NDS32_RELAX_ENTRY);
11862 if (irel == irelend)
11865 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
11867 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
11869 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11873 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
11876 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
11880 load_store_relax = table->load_store_relax;
11882 /* Get symbol table and section content. */
11883 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
11884 || !nds32_get_local_syms (abfd, sec, &isymbuf))
11887 /* Do relax loop only when finalize is not done.
11888 Take care of relaxable relocs except INSN16. */
11889 for (irel = internal_relocs; irel < irelend; irel++)
11891 int seq_len; /* Original length of instruction sequence. */
11892 int insn_len = 0; /* Final length of instruction sequence. */
11893 bfd_boolean removed;
11896 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
11897 && (irel->r_addend & 0x1f) >= 2)
11900 /* Relocation Types
11901 R_NDS32_LONGCALL1 53
11902 R_NDS32_LONGCALL2 54
11903 R_NDS32_LONGCALL3 55
11904 R_NDS32_LONGJUMP1 56
11905 R_NDS32_LONGJUMP2 57
11906 R_NDS32_LONGJUMP3 58
11907 R_NDS32_LOADSTORE 59 */
11908 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
11909 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
11910 seq_len = GET_SEQ_LEN (irel->r_addend);
11912 /* Relocation Types
11913 R_NDS32_LONGCALL4 107
11914 R_NDS32_LONGCALL5 108
11915 R_NDS32_LONGCALL6 109
11916 R_NDS32_LONGJUMP4 110
11917 R_NDS32_LONGJUMP5 111
11918 R_NDS32_LONGJUMP6 112
11919 R_NDS32_LONGJUMP7 113 */
11920 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
11921 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
11924 /* Relocation Types
11925 R_NDS32_LO12S0_RELA 30
11926 R_NDS32_LO12S1_RELA 29
11927 R_NDS32_LO12S2_RELA 28
11928 R_NDS32_LO12S2_SP_RELA 71
11929 R_NDS32_LO12S2_DP_RELA 70
11930 R_NDS32_GOT_LO12 46
11931 R_NDS32_GOTOFF_LO12 50
11932 R_NDS32_PLTREL_LO12 65
11933 R_NDS32_PLT_GOTREL_LO12 67
11934 R_NDS32_17IFC_PCREL_RELA 96
11935 R_NDS32_GOT_SUFF 193
11936 R_NDS32_GOTOFF_SUFF 194
11937 R_NDS32_PLT_GOT_SUFF 195
11938 R_NDS32_MULCALL_SUFF 196
11940 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
11941 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
11942 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
11943 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
11944 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
11945 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
11946 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
11947 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
11948 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
11949 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
11950 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
11951 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
11952 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
11953 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
11954 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS)
11959 insn_len = seq_len;
11962 switch (ELF32_R_TYPE (irel->r_info))
11964 case R_NDS32_LONGCALL1:
11965 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
11966 &insn_len, contents, isymbuf,
11969 case R_NDS32_LONGCALL2:
11970 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
11971 &insn_len, contents, isymbuf,
11974 case R_NDS32_LONGCALL3:
11975 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
11976 &insn_len, contents, isymbuf,
11979 case R_NDS32_LONGJUMP1:
11980 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
11981 &insn_len, contents, isymbuf,
11984 case R_NDS32_LONGJUMP2:
11985 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
11986 &insn_len, contents, isymbuf,
11989 case R_NDS32_LONGJUMP3:
11990 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
11991 &insn_len, contents, isymbuf,
11994 case R_NDS32_LONGCALL4:
11995 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
11996 &insn_len, contents, isymbuf,
11999 case R_NDS32_LONGCALL5:
12000 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12001 &insn_len, contents, isymbuf,
12004 case R_NDS32_LONGCALL6:
12005 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12006 &insn_len, contents, isymbuf,
12009 case R_NDS32_LONGJUMP4:
12010 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12011 &insn_len, contents, isymbuf,
12014 case R_NDS32_LONGJUMP5:
12015 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12016 &insn_len, &seq_len, contents,
12017 isymbuf, symtab_hdr);
12019 case R_NDS32_LONGJUMP6:
12020 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12021 &insn_len, &seq_len, contents,
12022 isymbuf, symtab_hdr);
12024 case R_NDS32_LONGJUMP7:
12025 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12026 &insn_len, &seq_len, contents,
12027 isymbuf, symtab_hdr);
12029 case R_NDS32_LOADSTORE:
12030 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12031 internal_relocs, &insn_len,
12032 contents, isymbuf, symtab_hdr,
12035 case R_NDS32_LO12S0_RELA:
12036 case R_NDS32_LO12S1_RELA:
12037 case R_NDS32_LO12S2_DP_RELA:
12038 case R_NDS32_LO12S2_SP_RELA:
12039 case R_NDS32_LO12S2_RELA:
12040 /* Relax for low part. */
12041 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12042 contents, isymbuf, symtab_hdr);
12044 /* It is impossible to delete blank, so just continue. */
12046 case R_NDS32_GOT_LO12:
12047 case R_NDS32_GOTOFF_LO12:
12048 case R_NDS32_PLTREL_LO12:
12049 case R_NDS32_PLT_GOTREL_LO12:
12050 case R_NDS32_GOTPC_LO12:
12051 /* Relax for PIC gp-relative low part. */
12052 nds32_elf_relax_piclo12 (link_info, abfd, sec, irel, contents,
12053 isymbuf, symtab_hdr);
12055 /* It is impossible to delete blank, so just continue. */
12057 case R_NDS32_TLS_LE_LO12:
12058 /* Relax for LE TLS low part. */
12059 nds32_elf_relax_letlslo12 (link_info, abfd, irel, contents,
12060 isymbuf, symtab_hdr);
12062 /* It is impossible to delete blank, so just continue. */
12064 case R_NDS32_TLS_LE_ADD:
12065 nds32_elf_relax_letlsadd (link_info, abfd, sec, irel, internal_relocs,
12066 contents, isymbuf, symtab_hdr, again);
12067 /* It is impossible to delete blank, so just continue. */
12069 case R_NDS32_TLS_LE_LS:
12070 nds32_elf_relax_letlsls (link_info, abfd, sec, irel, internal_relocs,
12071 contents, isymbuf, symtab_hdr, again);
12074 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12075 &insn_len, &seq_len, contents);
12077 case R_NDS32_PLT_GOT_SUFF:
12078 nds32_elf_relax_pltgot_suff (link_info, abfd, sec, irel,
12079 internal_relocs, contents,
12080 isymbuf, symtab_hdr, again);
12081 /* It is impossible to delete blank, so just continue. */
12083 case R_NDS32_GOT_SUFF:
12084 nds32_elf_relax_got_suff (link_info, abfd, sec, irel,
12085 internal_relocs, contents,
12086 symtab_hdr, again);
12087 /* It is impossible to delete blank, so just continue. */
12089 case R_NDS32_GOTOFF_SUFF:
12090 nds32_elf_relax_gotoff_suff (link_info, abfd, sec, irel,
12091 internal_relocs, contents,
12092 isymbuf, symtab_hdr, again);
12093 /* It is impossible to delete blank, so just continue. */
12099 if (removed && seq_len - insn_len > 0)
12101 if (!insert_nds32_elf_blank
12102 (&relax_blank_list, irel->r_offset + insn_len,
12103 seq_len - insn_len))
12109 calc_nds32_blank_total (relax_blank_list);
12111 if (table->relax_fp_as_gp)
12113 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12119 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12125 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
12129 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12130 &relax_blank_list, optimize, opt_size))
12134 /* It doesn't matter optimize_for_space_no_align anymore.
12135 If object file is assembled with flag '-Os',
12136 the we don't adjust jump-destination on 4-byte boundary. */
12138 if (relax_blank_list)
12140 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12141 relax_blank_list = NULL;
12146 /* Closing the section, so we don't relax it anymore. */
12147 bfd_vma sec_size_align;
12148 Elf_Internal_Rela *tmp_rel;
12150 /* Pad to alignment boundary. Only handle current section alignment. */
12151 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12152 & ((-1U) << sec->alignment_power);
12153 if ((sec_size_align - sec->size) & 0x2)
12155 insn16 = NDS32_NOP16;
12156 bfd_putb16 (insn16, contents + sec->size);
12160 while (sec_size_align != sec->size)
12162 insn = NDS32_NOP32;
12163 bfd_putb32 (insn, contents + sec->size);
12167 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12168 irelend, R_NDS32_RELAX_ENTRY);
12169 if (tmp_rel != irelend)
12170 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12172 clean_nds32_elf_blank ();
12176 if (internal_relocs != NULL
12177 && elf_section_data (sec)->relocs != internal_relocs)
12178 free (internal_relocs);
12180 if (contents != NULL
12181 && elf_section_data (sec)->this_hdr.contents != contents)
12184 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12194 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12196 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12197 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12202 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12203 struct bfd_link_info *info,
12204 void *finfo ATTRIBUTE_UNUSED,
12205 bfd_boolean (*func) (void *, const char *,
12206 Elf_Internal_Sym *,
12208 struct elf_link_hash_entry *)
12211 FILE *sym_ld_script = NULL;
12212 struct elf_nds32_link_hash_table *table;
12214 table = nds32_elf_hash_table (info);
12215 sym_ld_script = table->sym_ld_script;
12217 if (check_start_export_sym)
12218 fprintf (sym_ld_script, "}\n");
12223 static enum elf_reloc_type_class
12224 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12225 const asection *rel_sec ATTRIBUTE_UNUSED,
12226 const Elf_Internal_Rela *rela)
12228 switch ((int) ELF32_R_TYPE (rela->r_info))
12230 case R_NDS32_RELATIVE:
12231 return reloc_class_relative;
12232 case R_NDS32_JMP_SLOT:
12233 return reloc_class_plt;
12235 return reloc_class_copy;
12237 return reloc_class_normal;
12241 /* Put target dependent option into info hash table. */
12243 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12244 int relax_fp_as_gp,
12245 int eliminate_gc_relocs,
12246 FILE * sym_ld_script, int load_store_relax,
12247 int target_optimize, int relax_status,
12248 int relax_round, FILE * ex9_export_file,
12249 FILE * ex9_import_file,
12250 int update_ex9_table, int ex9_limit,
12251 bfd_boolean ex9_loop_aware,
12252 bfd_boolean ifc_loop_aware)
12254 struct elf_nds32_link_hash_table *table;
12256 table = nds32_elf_hash_table (link_info);
12260 table->relax_fp_as_gp = relax_fp_as_gp;
12261 table->eliminate_gc_relocs = eliminate_gc_relocs;
12262 table->sym_ld_script = sym_ld_script;
12263 table ->load_store_relax = load_store_relax;
12264 table->target_optimize = target_optimize;
12265 table->relax_status = relax_status;
12266 table->relax_round = relax_round;
12267 table->ex9_export_file = ex9_export_file;
12268 table->ex9_import_file = ex9_import_file;
12269 table->update_ex9_table = update_ex9_table;
12270 table->ex9_limit = ex9_limit;
12271 table->ex9_loop_aware = ex9_loop_aware;
12272 table->ifc_loop_aware = ifc_loop_aware;
12275 /* These functions and data-structures are used for fp-as-gp
12278 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12279 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12280 the read-only section and read-write section. */
12281 #define FAG_WINDOW (508 - 32)
12283 /* An nds32_fag represent a gp-relative access.
12284 We find best fp-base by using a sliding window
12285 to find a base address which can cover most gp-access. */
12288 struct nds32_fag *next; /* NULL-teminated linked list. */
12289 bfd_vma addr; /* The address of this fag. */
12290 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12291 It is used for applying FP7U2_FLAG. */
12292 int count; /* How many times this address is referred.
12293 There should be exactly `count' relocations
12295 int relas_capcity; /* The buffer size of relas.
12296 We use an array instead of linked-list,
12297 and realloc is used to adjust buffer size. */
12301 nds32_fag_init (struct nds32_fag *head)
12303 memset (head, 0, sizeof (struct nds32_fag));
12307 nds32_fag_verify (struct nds32_fag *head)
12309 struct nds32_fag *iter;
12310 struct nds32_fag *prev;
12316 if (prev && prev->addr >= iter->addr)
12317 puts ("Bug in fp-as-gp insertion.");
12323 /* Insert a fag in ascending order.
12324 If a fag of the same address already exists,
12325 they are chained by relas array. */
12328 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12329 Elf_Internal_Rela * rel)
12331 struct nds32_fag *iter;
12332 struct nds32_fag *new_fag;
12333 const int INIT_RELAS_CAP = 4;
12336 iter->next && iter->next->addr <= addr;
12338 /* Find somewhere to insert. */ ;
12340 /* `iter' will be equal to `head' if the list is empty. */
12341 if (iter != head && iter->addr == addr)
12343 /* The address exists in the list.
12344 Insert `rel' into relocation list, relas. */
12346 /* Check whether relas is big enough. */
12347 if (iter->count >= iter->relas_capcity)
12349 iter->relas_capcity *= 2;
12350 iter->relas = bfd_realloc
12351 (iter->relas, iter->relas_capcity * sizeof (void *));
12353 iter->relas[iter->count++] = rel;
12357 /* This is a new address. Create a fag node for it. */
12358 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12359 memset (new_fag, 0, sizeof (*new_fag));
12360 new_fag->addr = addr;
12361 new_fag->count = 1;
12362 new_fag->next = iter->next;
12363 new_fag->relas_capcity = INIT_RELAS_CAP;
12364 new_fag->relas = (Elf_Internal_Rela **)
12365 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12366 new_fag->relas[0] = rel;
12367 iter->next = new_fag;
12369 nds32_fag_verify (head);
12373 nds32_fag_free_list (struct nds32_fag *head)
12375 struct nds32_fag *iter;
12380 struct nds32_fag *tmp = iter;
12388 /* Find the best fp-base address.
12389 The relocation associated with that address is returned,
12390 so we can track the symbol instead of a fixed address.
12392 When relaxation, the address of an datum may change,
12393 because a text section is shrinked, so the data section
12394 moves forward. If the aligments of text and data section
12395 are different, their distance may change too.
12396 Therefore, tracking a fixed address is not appriate. */
12399 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12401 struct nds32_fag *base; /* First fag in the window. */
12402 struct nds32_fag *last; /* First fag outside the window. */
12403 int accu = 0; /* Usage accumulation. */
12404 struct nds32_fag *best; /* Best fag. */
12405 int baccu = 0; /* Best accumulation. */
12407 /* Use first fag for initial, and find the last fag in the window.
12409 In each iteration, we could simply subtract previous fag
12410 and accumulate following fags which are inside the window,
12411 untill we each the end. */
12413 if (head->next == NULL)
12419 /* Initialize base. */
12423 last && last->addr < base->addr + FAG_WINDOW;
12425 accu += last->count;
12429 /* Record the best base in each iteration. */
12432 accu -= base->count;
12434 /* Account fags in window. */
12435 for (/* Nothing. */;
12436 last && last->addr < base->addr + FAG_WINDOW;
12438 accu += last->count;
12440 /* A better fp-base? */
12453 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12454 so we can convert it fo fp-relative access later.
12455 `best_fag' is the best fp-base. Only those inside the window
12456 of best_fag is applied the flag. */
12459 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12460 bfd *abfd, struct nds32_fag *best_fag,
12461 Elf_Internal_Rela *internal_relocs,
12462 Elf_Internal_Rela *irelend)
12464 struct nds32_fag *ifag;
12465 bfd_vma best_fpbase, gp;
12468 output_bfd = abfd->sections->output_section->owner;
12469 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12470 best_fpbase = best_fag->addr;
12472 if (best_fpbase > gp + sdata_range[1][1]
12473 || best_fpbase < gp - sdata_range[1][0])
12476 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12477 so we know they can be converted to lwi37.fp. */
12478 for (ifag = best_fag;
12479 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12483 for (i = 0; i < ifag->count; i++)
12485 Elf_Internal_Rela *insn16_rel;
12486 Elf_Internal_Rela *fag_rel;
12488 fag_rel = ifag->relas[i];
12490 /* Only if this is within the WINDOWS, FP7U2_FLAG
12493 insn16_rel = find_relocs_at_address
12494 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12496 if (insn16_rel != irelend)
12497 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12503 /* Reset INSN16 to clean fp as gp. */
12506 nds32_fag_unmark_relax (struct nds32_fag *fag,
12507 Elf_Internal_Rela *internal_relocs,
12508 Elf_Internal_Rela *irelend)
12510 struct nds32_fag *ifag;
12512 Elf_Internal_Rela *insn16_rel;
12513 Elf_Internal_Rela *fag_rel;
12515 for (ifag = fag; ifag; ifag = ifag->next)
12517 for (i = 0; i < ifag->count; i++)
12519 fag_rel = ifag->relas[i];
12521 /* Restore the INSN16 relocation. */
12522 insn16_rel = find_relocs_at_address
12523 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12525 if (insn16_rel != irelend)
12526 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12531 /* This is the main function of fp-as-gp optimization.
12532 It should be called by relax_section. */
12535 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12536 bfd *abfd, asection *sec,
12537 Elf_Internal_Rela *internal_relocs,
12538 Elf_Internal_Rela *irelend,
12539 Elf_Internal_Sym *isymbuf)
12541 Elf_Internal_Rela *begin_rel = NULL;
12542 Elf_Internal_Rela *irel;
12543 struct nds32_fag fag_head;
12544 Elf_Internal_Shdr *symtab_hdr;
12545 bfd_byte *contents;
12546 bfd_boolean ifc_inside = FALSE;
12548 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12550 /* Per-function fp-base selection.
12551 1. Create a list for all the gp-relative access.
12552 2. Base on those gp-relative address,
12553 find a fp-base which can cover most access.
12554 3. Use the fp-base for fp-as-gp relaxation.
12556 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12558 1. delete the `la $fp, _FP_BASE_' instruction and
12559 2. not convert lwi.gp to lwi37.fp.
12561 To delete the _FP_BASE_ instruction, we simply apply
12562 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12564 To suppress the conversion, we simply NOT to apply
12565 R_NDS32_INSN16_FP7U2_FLAG flag. */
12567 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12569 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12570 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12573 /* Check whether it is worth for fp-as-gp optimization,
12574 i.e., at least 3 gp-load.
12576 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12577 apply this optimization. */
12579 for (irel = internal_relocs; irel < irelend; irel++)
12581 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12582 One we enter the begin of the region, we track all the LW/ST
12583 instructions, so when we leave the region, we try to find
12584 the best fp-base address for those LW/ST instructions. */
12586 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12587 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12589 /* Begin of the region. */
12591 /* xgettext:c-format */
12592 _bfd_error_handler (_("%B: Nested OMIT_FP in %A."), abfd, sec);
12595 nds32_fag_init (&fag_head);
12596 ifc_inside = FALSE;
12598 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12599 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12602 struct nds32_fag *best_fag, *tmp_fag;
12605 /* End of the region.
12606 Check whether it is worth to do fp-as-gp. */
12608 if (begin_rel == NULL)
12610 /* xgettext:c-format */
12611 _bfd_error_handler (_("%B: Unmatched OMIT_FP in %A."), abfd, sec);
12615 accu = nds32_fag_find_base (&fag_head, &best_fag);
12617 /* Clean FP7U2_FLAG because they may set ever. */
12618 tmp_fag = fag_head.next;
12619 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12621 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12622 if (accu < FAG_THRESHOLD
12623 || !nds32_fag_mark_relax (link_info, abfd, best_fag,
12624 internal_relocs, irelend))
12626 /* Not worth to do fp-as-gp. */
12627 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12628 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12629 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12630 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12631 nds32_fag_free_list (&fag_head);
12636 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12637 so we use it to record the distance to the reloction of best
12639 dist = best_fag->relas[0] - begin_rel;
12640 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12641 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12642 relocation. And get the base value when relocating. */
12643 begin_rel->r_addend &= (0x1 << 16) - 1;
12644 begin_rel->r_addend |= dist << 16;
12646 nds32_fag_free_list (&fag_head);
12650 if (begin_rel == NULL || ifc_inside)
12651 /* Skip if we are not in the region of fp-as-gp. */
12654 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12655 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12660 /* A gp-relative access is found. Insert it to the fag-list. */
12662 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
12663 insn = bfd_getb32 (contents + irel->r_offset);
12664 if (!N32_IS_RT3 (insn))
12667 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12668 nds32_fag_insert (&fag_head, addr, irel);
12670 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12674 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12675 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12677 /* Suppress fp as gp when encounter ifc. */
12685 /* Remove unused `la $fp, _FD_BASE_' instruction. */
12688 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12689 Elf_Internal_Rela *internal_relocs,
12690 Elf_Internal_Rela *irelend)
12692 Elf_Internal_Rela *irel;
12693 Elf_Internal_Shdr *symtab_hdr;
12694 bfd_byte *contents = NULL;
12695 nds32_elf_blank_t *relax_blank_list = NULL;
12696 bfd_boolean result = TRUE;
12697 bfd_boolean unused_region = FALSE;
12700 NOTE: Disable fp-as-gp if we encounter ifcall relocations.
12701 * R_NDS32_17IFC_PCREL_RELA
12702 * R_NDS32_10IFCU_PCREL_RELA
12707 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12708 nds32_get_section_contents (abfd, sec, &contents, TRUE);
12710 for (irel = internal_relocs; irel < irelend; irel++)
12712 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
12713 we marked to in previous pass.
12714 DO NOT scan relocations again, since we've alreadly decided it
12715 and set the flag. */
12716 const char *syname;
12720 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12721 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12722 unused_region = TRUE;
12723 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12724 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12725 unused_region = FALSE;
12727 /* We're not in the region. */
12728 if (!unused_region)
12731 /* _FP_BASE_ must be a GLOBAL symbol. */
12732 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
12733 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
12736 /* The symbol name must be _FP_BASE_. */
12737 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
12738 if (strcmp (syname, FP_BASE_NAME) != 0)
12741 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
12743 /* addi.gp $fp, -256 */
12744 insn = bfd_getb32 (contents + irel->r_offset);
12745 if (insn != INSN_ADDIGP_TO_FP)
12748 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
12750 /* addi $fp, $gp, -256 */
12751 insn = bfd_getb32 (contents + irel->r_offset);
12752 if (insn != INSN_ADDI_GP_TO_FP)
12755 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
12757 /* movi $fp, FP_BASE */
12758 insn = bfd_getb32 (contents + irel->r_offset);
12759 if (insn != INSN_MOVI_TO_FP)
12765 /* We got here because a FP_BASE instruction is found. */
12766 if (!insert_nds32_elf_blank_recalc_total
12767 (&relax_blank_list, irel->r_offset, 4))
12772 if (relax_blank_list)
12774 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12775 relax_blank_list = NULL;
12784 /* This is a version of bfd_generic_get_relocated_section_contents.
12785 We need this variety because relaxation will modify the dwarf
12786 infomation. When there is undefined symbol reference error mesage,
12787 linker need to dump line number where the symbol be used. However
12788 the address is be relaxed, it can not get the original dwarf contents.
12789 The variety only modify function call for reading in the section. */
12792 nds32_elf_get_relocated_section_contents (bfd *abfd,
12793 struct bfd_link_info *link_info,
12794 struct bfd_link_order *link_order,
12796 bfd_boolean relocatable,
12799 bfd *input_bfd = link_order->u.indirect.section->owner;
12800 asection *input_section = link_order->u.indirect.section;
12802 arelent **reloc_vector;
12805 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12806 if (reloc_size < 0)
12809 /* Read in the section. */
12810 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
12813 if (reloc_size == 0)
12816 reloc_vector = (arelent **) bfd_malloc (reloc_size);
12817 if (reloc_vector == NULL)
12820 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
12821 reloc_vector, symbols);
12822 if (reloc_count < 0)
12825 if (reloc_count > 0)
12828 for (parent = reloc_vector; *parent != NULL; parent++)
12830 char *error_message = NULL;
12832 bfd_reloc_status_type r;
12834 symbol = *(*parent)->sym_ptr_ptr;
12835 if (symbol->section && discarded_section (symbol->section))
12838 static reloc_howto_type none_howto
12839 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
12840 "unused", FALSE, 0, 0, FALSE);
12842 p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
12843 _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
12845 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
12846 (*parent)->addend = 0;
12847 (*parent)->howto = &none_howto;
12851 r = bfd_perform_relocation (input_bfd, *parent, data,
12853 relocatable ? abfd : NULL,
12858 asection *os = input_section->output_section;
12860 /* A partial link, so keep the relocs. */
12861 os->orelocation[os->reloc_count] = *parent;
12865 if (r != bfd_reloc_ok)
12869 case bfd_reloc_undefined:
12870 (*link_info->callbacks->undefined_symbol)
12871 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12872 input_bfd, input_section, (*parent)->address, TRUE);
12874 case bfd_reloc_dangerous:
12875 BFD_ASSERT (error_message != NULL);
12876 (*link_info->callbacks->reloc_dangerous)
12877 (link_info, error_message,
12878 input_bfd, input_section, (*parent)->address);
12880 case bfd_reloc_overflow:
12881 (*link_info->callbacks->reloc_overflow)
12883 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12884 (*parent)->howto->name, (*parent)->addend,
12885 input_bfd, input_section, (*parent)->address);
12887 case bfd_reloc_outofrange:
12889 This error can result when processing some partially
12890 complete binaries. Do not abort, but issue an error
12891 message instead. */
12892 link_info->callbacks->einfo
12893 /* xgettext:c-format */
12894 (_("%X%P: %B(%A): relocation \"%R\" goes out of range\n"),
12895 abfd, input_section, * parent);
12906 free (reloc_vector);
12910 free (reloc_vector);
12914 /* Link-time IFC relaxation.
12915 In this optimization, we chains jump instructions
12916 of the same destination with ifcall. */
12919 /* List to save jal and j relocation. */
12920 struct elf_nds32_ifc_symbol_entry
12923 struct elf_link_hash_entry *h;
12924 struct elf_nds32_ifc_irel_list *irel_head;
12925 unsigned long insn;
12927 int enable; /* Apply ifc. */
12928 int ex9_enable; /* Apply ifc after ex9. */
12929 struct elf_nds32_ifc_symbol_entry *next;
12932 struct elf_nds32_ifc_irel_list
12934 Elf_Internal_Rela *irel;
12937 /* If this is set, then it is the last instruction for
12938 ifc-chain, so it must be keep for the actual branching. */
12940 struct elf_nds32_ifc_irel_list *next;
12943 static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;
12945 /* Insert symbol of jal and j for ifc. */
12948 nds32_elf_ifc_insert_symbol (asection *sec,
12949 struct elf_link_hash_entry *h,
12950 Elf_Internal_Rela *irel,
12951 unsigned long insn)
12953 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
12955 /* Check there is target of existing entry the same as the new one. */
12956 while (ptr != NULL)
12958 if (((h == NULL && ptr->sec == sec
12959 && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
12960 && ptr->irel_head->irel->r_addend == irel->r_addend)
12963 && ptr->insn == insn)
12965 /* The same target exist, so insert into list. */
12966 struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;
12968 while (irel_list->next != NULL)
12969 irel_list = irel_list->next;
12970 irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
12971 irel_list = irel_list->next;
12972 irel_list->irel = irel;
12973 irel_list->keep = 1;
12976 irel_list->sec = NULL;
12978 irel_list->sec = sec;
12979 irel_list->next = NULL;
12982 if (ptr->next == NULL)
12987 /* There is no same target entry, so build a new one. */
12988 if (ifc_symbol_head == NULL)
12990 ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
12991 ptr = ifc_symbol_head;
12995 ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
13000 ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
13001 ptr->irel_head->irel = irel;
13003 ptr->irel_head->keep = 1;
13007 /* Local symbols. */
13009 ptr->irel_head->sec = NULL;
13013 /* Global symbol. */
13015 ptr->irel_head->sec = sec;
13018 ptr->irel_head->next = NULL;
13021 ptr->ex9_enable = 0;
13025 /* Gather all jal and j instructions. */
13028 nds32_elf_ifc_calc (struct bfd_link_info *info,
13029 bfd *abfd, asection *sec)
13031 Elf_Internal_Rela *internal_relocs;
13032 Elf_Internal_Rela *irelend;
13033 Elf_Internal_Rela *irel;
13034 Elf_Internal_Shdr *symtab_hdr;
13035 bfd_byte *contents = NULL;
13036 uint32_t insn, insn_with_reg;
13037 unsigned long r_symndx;
13038 struct elf_link_hash_entry *h;
13039 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
13040 struct elf_nds32_link_hash_table *table;
13041 bfd_boolean ifc_loop_aware;
13043 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13044 TRUE /* keep_memory */);
13045 irelend = internal_relocs + sec->reloc_count;
13046 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13048 /* Check if the object enable ifc. */
13049 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
13050 R_NDS32_RELAX_ENTRY);
13054 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
13055 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
13056 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
13059 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
13062 table = nds32_elf_hash_table (info);
13063 ifc_loop_aware = table->ifc_loop_aware;
13064 while (irel != NULL && irel < irelend)
13066 /* Traverse all relocation and gather all of them to build the list. */
13068 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
13070 if (ifc_loop_aware == 1
13071 && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
13073 /* Check the region if loop or not. If it is true and
13074 ifc-loop-aware is true, ignore the region till region end. */
13075 while (irel != NULL
13077 && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
13078 || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
13083 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
13085 insn = bfd_getb32 (contents + irel->r_offset);
13086 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
13087 r_symndx = ELF32_R_SYM (irel->r_info);
13088 if (r_symndx < symtab_hdr->sh_info)
13090 /* Local symbol. */
13091 nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
13095 /* External symbol. */
13096 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13097 nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
13105 /* Determine whether j and jal should be substituted. */
13108 nds32_elf_ifc_filter (struct bfd_link_info *info)
13110 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13111 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13112 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13113 struct elf_nds32_link_hash_table *table;
13114 int target_optimize;
13117 table = nds32_elf_hash_table (info);
13118 target_optimize = table->target_optimize;
13121 irel_ptr = ptr->irel_head;
13122 if (ptr->h == NULL)
13124 /* Local symbol. */
13125 irel_keeper = irel_ptr;
13126 while (irel_ptr && irel_ptr->next)
13128 /* Check there is jump target can be used. */
13129 if ((irel_ptr->next->irel->r_offset
13130 - irel_keeper->irel->r_offset) > 1022)
13131 irel_keeper = irel_ptr->next;
13135 irel_ptr->keep = 0;
13137 irel_ptr = irel_ptr->next;
13142 /* Global symbol. */
13143 /* We have to get the absolute address and decide
13144 whether to keep it or not. */
13147 address = (irel_ptr->irel->r_offset
13148 + irel_ptr->sec->output_section->vma
13149 + irel_ptr->sec->output_offset);
13150 irel_ptr->addr = address;
13151 irel_ptr = irel_ptr->next;
13154 irel_ptr = ptr->irel_head;
13157 /* Sort by address. */
13158 struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
13159 struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
13160 struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
13161 struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;
13163 /* Get the smallest one. */
13164 while (irel_temp->next)
13166 if (irel_temp->next->addr < irel_dest->addr)
13168 irel_dest_prev = irel_temp;
13169 irel_dest = irel_temp->next;
13171 irel_temp = irel_temp->next;
13174 if (irel_dest != irel_ptr)
13177 irel_ptr_prev->next = irel_dest;
13178 if (irel_dest_prev)
13179 irel_dest_prev->next = irel_ptr;
13180 irel_temp = irel_ptr->next;
13181 irel_ptr->next = irel_dest->next;
13182 irel_dest->next = irel_temp;
13184 irel_ptr_prev = irel_ptr;
13185 irel_ptr = irel_ptr->next;
13188 irel_ptr = ptr->irel_head;
13189 irel_keeper = irel_ptr;
13190 while (irel_ptr && irel_ptr->next)
13192 if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
13193 irel_keeper = irel_ptr->next;
13197 irel_ptr->keep = 0;
13199 irel_ptr = irel_ptr->next;
13203 /* Ex9 enable. Reserve it for ex9. */
13204 if ((target_optimize & NDS32_RELAX_EX9_ON)
13205 && ptr->irel_head != irel_keeper)
13211 /* Determine whether j and jal should be substituted after ex9 done. */
13214 nds32_elf_ifc_filter_after_ex9 (void)
13216 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13217 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13221 if (ptr->enable == 0)
13223 /* Check whether ifc is applied or not. */
13224 irel_ptr = ptr->irel_head;
13225 ptr->ex9_enable = 1;
13228 if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
13231 ptr->ex9_enable = 0;
13234 irel_ptr = irel_ptr->next;
13241 /* Wrapper to do ifc relaxation. */
13244 nds32_elf_ifc_finish (struct bfd_link_info *info)
13247 struct elf_nds32_link_hash_table *table;
13249 table = nds32_elf_hash_table (info);
13250 relax_status = table->relax_status;
13252 if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
13253 nds32_elf_ifc_filter (info);
13255 nds32_elf_ifc_filter_after_ex9 ();
13257 if (!nds32_elf_ifc_replace (info))
13261 table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
13265 /* Traverse the result of ifc filter and replace it with ifcall9. */
13268 nds32_elf_ifc_replace (struct bfd_link_info *info)
13270 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13271 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13272 nds32_elf_blank_t *relax_blank_list = NULL;
13273 bfd_byte *contents = NULL;
13274 Elf_Internal_Rela *internal_relocs;
13275 Elf_Internal_Rela *irel;
13276 Elf_Internal_Rela *irelend;
13277 unsigned short insn16 = INSN_IFCALL9;
13278 struct elf_nds32_link_hash_table *table;
13281 table = nds32_elf_hash_table (info);
13282 relax_status = table->relax_status;
13286 /* Traverse the ifc gather list, and replace the
13287 filter entries by ifcall9. */
13288 if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
13289 || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE)
13290 && ptr->ex9_enable == 1))
13292 irel_ptr = ptr->irel_head;
13293 if (ptr->h == NULL)
13295 /* Local symbol. */
13296 internal_relocs = _bfd_elf_link_read_relocs
13297 (ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
13298 irelend = internal_relocs + ptr->sec->reloc_count;
13300 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13306 if (irel_ptr->keep == 0 && irel_ptr->next)
13308 /* The one can be replaced. We have to check whether
13309 there is any alignment point in the region. */
13310 irel = irel_ptr->irel;
13311 while (((irel_ptr->next->keep == 0
13312 && irel < irel_ptr->next->irel)
13313 || (irel_ptr->next->keep == 1 && irel < irelend))
13314 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13315 && (irel->r_addend & 0x1f) == 2))
13317 if (irel >= irelend
13318 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13319 && (irel->r_addend & 0x1f) == 2
13320 && ((irel->r_offset - get_nds32_elf_blank_total
13321 (&relax_blank_list, irel->r_offset, 1))
13324 /* Replace by ifcall9. */
13325 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13326 if (!insert_nds32_elf_blank_recalc_total
13327 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13329 irel_ptr->irel->r_info =
13330 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13331 R_NDS32_10IFCU_PCREL_RELA);
13334 irel_ptr = irel_ptr->next;
13337 /* Delete the redundant code. */
13338 if (relax_blank_list)
13340 nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
13342 relax_blank_list = NULL;
13347 /* Global symbol. */
13350 if (irel_ptr->keep == 0 && irel_ptr->next)
13352 /* The one can be replaced, and we have to check
13353 whether there is any alignment point in the region. */
13354 internal_relocs = _bfd_elf_link_read_relocs
13355 (irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
13356 TRUE /* keep_memory */);
13357 irelend = internal_relocs + irel_ptr->sec->reloc_count;
13358 if (!nds32_get_section_contents (irel_ptr->sec->owner,
13359 irel_ptr->sec, &contents,
13363 irel = irel_ptr->irel;
13364 while (((irel_ptr->sec == irel_ptr->next->sec
13365 && irel_ptr->next->keep == 0
13366 && irel < irel_ptr->next->irel)
13367 || ((irel_ptr->sec != irel_ptr->next->sec
13368 || irel_ptr->next->keep == 1)
13369 && irel < irelend))
13370 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13371 && (irel->r_addend & 0x1f) == 2))
13373 if (irel >= irelend
13374 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13375 && (irel->r_addend & 0x1f) == 2
13376 && ((irel->r_offset
13377 - get_nds32_elf_blank_total (&relax_blank_list,
13378 irel->r_offset, 1)) & 0x02) == 0))
13380 /* Replace by ifcall9. */
13381 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13382 if (!insert_nds32_elf_blank_recalc_total
13383 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13386 /* Delete the redundant code, and clear the relocation. */
13387 nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
13390 irel_ptr->irel->r_info =
13391 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13392 R_NDS32_10IFCU_PCREL_RELA);
13393 relax_blank_list = NULL;
13397 irel_ptr = irel_ptr->next;
13407 /* Relocate ifcall. */
13410 nds32_elf_ifc_reloc (void)
13412 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13413 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13414 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13415 bfd_vma relocation, address;
13416 unsigned short insn16;
13417 bfd_byte *contents = NULL;
13418 static bfd_boolean done = FALSE;
13427 /* Check the entry is enable ifcall. */
13428 if (ptr->enable == 1 || ptr->ex9_enable == 1)
13430 /* Get the reserve jump. */
13431 irel_ptr = ptr->irel_head;
13434 if (irel_ptr->keep == 1)
13436 irel_keeper = irel_ptr;
13439 irel_ptr = irel_ptr->next;
13442 irel_ptr = ptr->irel_head;
13443 if (ptr->h == NULL)
13445 /* Local symbol. */
13446 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13452 if (irel_ptr->keep == 0
13453 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13455 relocation = irel_keeper->irel->r_offset;
13456 relocation = relocation - irel_ptr->irel->r_offset;
13457 while (irel_keeper && relocation > 1022)
13459 irel_keeper = irel_keeper->next;
13460 if (irel_keeper && irel_keeper->keep == 1)
13462 relocation = irel_keeper->irel->r_offset;
13463 relocation = relocation - irel_ptr->irel->r_offset;
13466 if (relocation > 1022)
13468 /* Double check. */
13469 irel_keeper = ptr->irel_head;
13470 while (irel_keeper)
13472 if (irel_keeper->keep == 1)
13474 relocation = irel_keeper->irel->r_offset;
13475 relocation = relocation - irel_ptr->irel->r_offset;
13477 if (relocation <= 1022)
13479 irel_keeper = irel_keeper->next;
13484 irel_ptr->irel->r_info =
13485 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13487 insn16 = INSN_IFCALL9 | (relocation >> 1);
13488 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13490 irel_ptr = irel_ptr->next;
13495 /* Global symbol. */
13498 if (irel_ptr->keep == 0
13499 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13501 /* Get the distance between ifcall and jump. */
13502 relocation = (irel_keeper->irel->r_offset
13503 + irel_keeper->sec->output_section->vma
13504 + irel_keeper->sec->output_offset);
13505 address = (irel_ptr->irel->r_offset
13506 + irel_ptr->sec->output_section->vma
13507 + irel_ptr->sec->output_offset);
13508 relocation = relocation - address;
13510 /* The distance is over ragne, find callee again. */
13511 while (irel_keeper && relocation > 1022)
13513 irel_keeper = irel_keeper->next;
13514 if (irel_keeper && irel_keeper->keep ==1)
13516 relocation = (irel_keeper->irel->r_offset
13517 + irel_keeper->sec->output_section->vma
13518 + irel_keeper->sec->output_offset);
13519 relocation = relocation - address;
13523 if (relocation > 1022)
13525 /* Double check. */
13526 irel_keeper = ptr->irel_head;
13527 while (irel_keeper)
13529 if (irel_keeper->keep == 1)
13532 relocation = (irel_keeper->irel->r_offset
13533 + irel_keeper->sec->output_section->vma
13534 + irel_keeper->sec->output_offset);
13535 relocation = relocation - address;
13537 if (relocation <= 1022)
13539 irel_keeper = irel_keeper->next;
13544 if (!nds32_get_section_contents
13545 (irel_ptr->sec->owner, irel_ptr->sec, &contents, TRUE))
13547 insn16 = INSN_IFCALL9 | (relocation >> 1);
13548 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13549 irel_ptr->irel->r_info =
13550 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13553 irel_ptr =irel_ptr->next;
13563 /* End of IFC relaxation. */
13565 /* EX9 Instruction Table Relaxation. */
13567 /* Global hash list. */
13568 struct elf_link_hash_entry_list
13570 struct elf_link_hash_entry *h;
13571 struct elf_link_hash_entry_list *next;
13574 /* Save different destination but same insn. */
13575 struct elf_link_hash_entry_mul_list
13577 /* Global symbol times. */
13579 /* Save relocation for each global symbol but useful?? */
13580 Elf_Internal_Rela *irel;
13581 /* For sethi, two sethi may have the same high-part but different low-parts. */
13582 Elf_Internal_Rela rel_backup;
13583 struct elf_link_hash_entry_list *h_list;
13584 struct elf_link_hash_entry_mul_list *next;
13587 /* Instruction hash table. */
13588 struct elf_nds32_code_hash_entry
13590 struct bfd_hash_entry root;
13592 /* For insn that can use relocation or constant ex: sethi. */
13595 struct elf_link_hash_entry_mul_list *m_list;
13596 /* Using r_addend. */
13597 Elf_Internal_Rela *irel;
13598 /* Using r_info. */
13599 Elf_Internal_Rela rel_backup;
13602 /* Instruction count list. */
13603 struct elf_nds32_insn_times_entry
13605 const char *string;
13609 struct elf_link_hash_entry_mul_list *m_list;
13610 Elf_Internal_Rela *irel;
13611 Elf_Internal_Rela rel_backup;
13612 struct elf_nds32_insn_times_entry *next;
13615 /* J and JAL symbol list. */
13616 struct elf_nds32_symbol_entry
13619 unsigned long insn;
13620 struct elf_nds32_symbol_entry *next;
13623 /* Relocation list. */
13624 struct elf_nds32_irel_entry
13626 Elf_Internal_Rela *irel;
13627 struct elf_nds32_irel_entry *next;
13630 /* ex9.it insn need to be fixed. */
13631 struct elf_nds32_ex9_refix
13633 Elf_Internal_Rela *irel;
13635 struct elf_link_hash_entry *h;
13637 struct elf_nds32_ex9_refix *next;
13640 static struct bfd_hash_table ex9_code_table;
13641 static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
13642 static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;
13644 /* EX9 hash function. */
13646 static struct bfd_hash_entry *
13647 nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
13648 struct bfd_hash_table *table,
13649 const char *string)
13651 struct elf_nds32_code_hash_entry *ret;
13653 /* Allocate the structure if it has not already been allocated by a
13657 entry = (struct bfd_hash_entry *)
13658 bfd_hash_allocate (table, sizeof (*ret));
13663 /* Call the allocation method of the superclass. */
13664 entry = bfd_hash_newfunc (entry, table, string);
13668 ret = (struct elf_nds32_code_hash_entry*) entry;
13670 ret->const_insn = 0;
13671 ret->m_list = NULL;
13677 /* Insert ex9 entry
13678 this insert must be stable sorted by times. */
13681 nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
13683 struct elf_nds32_insn_times_entry *temp;
13684 struct elf_nds32_insn_times_entry *temp2;
13686 if (ex9_insn_head == NULL)
13688 ex9_insn_head = ptr;
13693 temp = ex9_insn_head;
13694 temp2 = ex9_insn_head;
13695 while (temp->next &&
13696 (temp->next->times >= ptr->times
13697 || temp->times == -1))
13699 if (temp->times == -1)
13703 if (ptr->times > temp->times && temp->times != -1)
13706 if (temp2->times == -1)
13709 ex9_insn_head = ptr;
13711 else if (temp->next == NULL)
13718 ptr->next = temp->next;
13724 /* Examine each insn times in hash table.
13725 Handle multi-link hash entry.
13727 TODO: This function doesn't assign so much info since it is fake. */
13730 nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
13732 struct elf_nds32_insn_times_entry *ptr;
13735 if (h->m_list == NULL)
13737 /* Local symbol insn or insn without relocation. */
13741 ptr = (struct elf_nds32_insn_times_entry *)
13742 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13743 ptr->times = h->times;
13744 ptr->string = h->root.string;
13745 ptr->m_list = NULL;
13747 ptr->irel = h->irel;
13748 ptr->rel_backup = h->rel_backup;
13749 nds32_elf_ex9_insert_entry (ptr);
13753 /* Global symbol insn. */
13754 /* Only sethi insn has multiple m_list. */
13755 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13760 times += m_list->times;
13761 m_list = m_list->next;
13765 m_list = h->m_list;
13766 ptr = (struct elf_nds32_insn_times_entry *)
13767 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13768 ptr->times = times; /* Use the total times. */
13769 ptr->string = h->root.string;
13770 ptr->m_list = m_list;
13772 ptr->irel = m_list->irel;
13773 ptr->rel_backup = m_list->rel_backup;
13774 nds32_elf_ex9_insert_entry (ptr);
13776 if (h->const_insn == 1)
13778 /* sethi with constant value. */
13782 ptr = (struct elf_nds32_insn_times_entry *)
13783 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13784 ptr->times = h->times;
13785 ptr->string = h->root.string;
13786 ptr->m_list = NULL;
13789 ptr->rel_backup = h->rel_backup;
13790 nds32_elf_ex9_insert_entry (ptr);
13796 /* Count each insn times in hash table.
13797 Handle multi-link hash entry. */
13800 nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
13802 int reservation, times;
13803 unsigned long relocation, min_relocation;
13804 struct elf_nds32_insn_times_entry *ptr;
13806 if (h->m_list == NULL)
13808 /* Local symbol insn or insn without relocation. */
13811 ptr = (struct elf_nds32_insn_times_entry *)
13812 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13813 ptr->times = h->times;
13814 ptr->string = h->root.string;
13815 ptr->m_list = NULL;
13817 ptr->irel = h->irel;
13818 ptr->rel_backup = h->rel_backup;
13819 nds32_elf_ex9_insert_entry (ptr);
13823 /* Global symbol insn. */
13824 /* Only sethi insn has multiple m_list. */
13825 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13827 if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
13828 && m_list->next != NULL)
13830 /* Sethi insn has different symbol or addend but has same hi20. */
13834 min_relocation = 0xffffffff;
13837 /* Get the minimum sethi address
13838 and calculate how many entry the sethi-list have to use. */
13839 if ((m_list->h_list->h->root.type == bfd_link_hash_defined
13840 || m_list->h_list->h->root.type == bfd_link_hash_defweak)
13841 && (m_list->h_list->h->root.u.def.section != NULL
13842 && m_list->h_list->h->root.u.def.section->output_section != NULL))
13844 relocation = (m_list->h_list->h->root.u.def.value +
13845 m_list->h_list->h->root.u.def.section->output_section->vma +
13846 m_list->h_list->h->root.u.def.section->output_offset);
13847 relocation += m_list->irel->r_addend;
13851 if (relocation < min_relocation)
13852 min_relocation = relocation;
13853 times += m_list->times;
13854 m_list = m_list->next;
13856 if (min_relocation < ex9_relax_size)
13857 reservation = (min_relocation >> 12) + 1;
13859 reservation = (min_relocation >> 12)
13860 - ((min_relocation - ex9_relax_size) >> 12) + 1;
13861 if (reservation < (times / 3))
13863 /* Efficient enough to use ex9. */
13866 for (i = reservation ; i > 0; i--)
13868 /* Allocate number of reservation ex9 entry. */
13869 ptr = (struct elf_nds32_insn_times_entry *)
13870 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13871 ptr->times = h->m_list->times / reservation;
13872 ptr->string = h->root.string;
13873 ptr->m_list = h->m_list;
13875 ptr->irel = h->m_list->irel;
13876 ptr->rel_backup = h->m_list->rel_backup;
13877 nds32_elf_ex9_insert_entry (ptr);
13883 /* Normal global symbol that means no different address symbol
13884 using same ex9 entry. */
13885 if (m_list->times >= 3)
13887 ptr = (struct elf_nds32_insn_times_entry *)
13888 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13889 ptr->times = m_list->times;
13890 ptr->string = h->root.string;
13891 ptr->m_list = h->m_list;
13893 ptr->irel = h->m_list->irel;
13894 ptr->rel_backup = h->m_list->rel_backup;
13895 nds32_elf_ex9_insert_entry (ptr);
13899 if (h->const_insn == 1)
13901 /* sethi with constant value. */
13905 ptr = (struct elf_nds32_insn_times_entry *)
13906 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13907 ptr->times = h->times;
13908 ptr->string = h->root.string;
13909 ptr->m_list = NULL;
13912 ptr->rel_backup = h->rel_backup;
13913 nds32_elf_ex9_insert_entry (ptr);
13920 /* Hash table traverse function. */
13923 nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
13927 ex9_code_table.frozen = 1;
13928 for (i = 0; i < ex9_code_table.size; i++)
13930 struct bfd_hash_entry *p;
13932 for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
13933 if (!func ((struct elf_nds32_code_hash_entry *) p))
13937 ex9_code_table.frozen = 0;
13941 /* Give order number to insn list. */
13944 nds32_elf_order_insn_times (struct bfd_link_info *info)
13946 struct elf_nds32_insn_times_entry *ex9_insn;
13947 struct elf_nds32_insn_times_entry *temp = NULL;
13948 struct elf_nds32_link_hash_table *table;
13952 if (ex9_insn_head == NULL)
13955 /* The max number of entries is 512. */
13956 ex9_insn = ex9_insn_head;
13957 table = nds32_elf_hash_table (info);
13958 ex9_limit = table->ex9_limit;
13960 ex9_insn = ex9_insn_head;
13962 while (ex9_insn != NULL && number < ex9_limit)
13964 ex9_insn->order = number;
13967 ex9_insn = ex9_insn->next;
13970 if (ex9_insn && temp)
13973 while (ex9_insn != NULL)
13975 /* Free useless entry. */
13977 ex9_insn = ex9_insn->next;
13982 /* Build .ex9.itable section. */
13985 nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
13987 asection *table_sec;
13988 struct elf_nds32_insn_times_entry *ptr;
13991 bfd_byte *contents = NULL;
13993 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
13994 it_abfd = it_abfd->link.next)
13996 /* Find the section .ex9.itable, and put all entries into it. */
13997 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
13998 if (table_sec != NULL)
14000 if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
14003 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
14006 table_sec->size = number * 4;
14011 elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
14013 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
14017 val = strtol (ptr->string, NULL, 16);
14018 bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
14026 /* Get insn with regs according to relocation type. */
14029 nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
14030 uint32_t insn, uint32_t *insn_with_reg)
14032 reloc_howto_type *howto = NULL;
14035 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
14036 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
14037 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
14039 *insn_with_reg = insn;
14043 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14044 *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
14047 /* Mask number of address bits according to relocation. */
14049 static unsigned long
14050 nds32_elf_irel_mask (Elf_Internal_Rela *irel)
14052 reloc_howto_type *howto = NULL;
14055 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
14056 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
14057 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
14060 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14061 return howto->dst_mask;
14065 nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
14066 struct elf_nds32_irel_entry *irel_ptr)
14068 if (*irel_list == NULL)
14070 *irel_list = irel_ptr;
14071 irel_ptr->next = NULL;
14075 irel_ptr->next = *irel_list;
14076 *irel_list = irel_ptr;
14081 nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
14082 struct elf_link_hash_entry *h, int order)
14084 struct elf_nds32_ex9_refix *ptr;
14086 ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
14090 ptr->order = order;
14093 if (ex9_refix_head == NULL)
14094 ex9_refix_head = ptr;
14097 struct elf_nds32_ex9_refix *temp = ex9_refix_head;
14099 while (temp->next != NULL)
14108 CLEAN_PRE = 1 << 1,
14112 /* Check relocation type if supporting for ex9. */
14115 nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
14116 Elf_Internal_Rela **irel,
14117 Elf_Internal_Rela *irelend,
14118 nds32_elf_blank_t *relax_blank_list,
14119 asection *sec,bfd_vma *off,
14120 bfd_byte *contents)
14122 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
14123 bfd_boolean nested_ex9, nested_loop;
14124 bfd_boolean ex9_loop_aware;
14125 /* We use the highest 1 byte of result to record
14126 how many bytes location counter has to move. */
14128 Elf_Internal_Rela *irel_save = NULL;
14129 struct elf_nds32_link_hash_table *table;
14131 table = nds32_elf_hash_table (info);
14132 ex9_loop_aware = table->ex9_loop_aware;
14134 while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
14136 switch (ELF32_R_TYPE ((*irel)->r_info))
14138 case R_NDS32_RELAX_REGION_BEGIN:
14139 /* Ignore code block. */
14140 nested_ex9 = FALSE;
14141 nested_loop = FALSE;
14142 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
14144 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
14146 /* Check the region if loop or not. If it is true and
14147 ex9-loop-aware is true, ignore the region till region end. */
14148 /* To save the status for in .no_relax ex9 region and
14149 loop region to conform the block can do ex9 relaxation. */
14150 nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
14151 nested_loop = (ex9_loop_aware
14152 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
14153 while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
14156 if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
14158 /* There may be nested region. */
14159 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14161 else if (ex9_loop_aware
14162 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14163 nested_loop = TRUE;
14165 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
14167 /* The end of region. */
14168 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14169 nested_ex9 = FALSE;
14170 else if (ex9_loop_aware
14171 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14172 nested_loop = FALSE;
14174 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
14175 && ((*irel)->r_addend & 0x1f) == 2)
14177 /* Alignment exist in the region. */
14178 result |= CLEAN_PRE;
14179 if (((*irel)->r_offset -
14180 get_nds32_elf_blank_total (&relax_blank_list,
14181 (*irel)->r_offset, 0)) & 0x02)
14182 result |= PUSH_PRE;
14185 if ((*irel) >= irelend)
14188 *off = (*irel)->r_offset;
14190 /* The final instruction in the region, regard this one as data to ignore it. */
14191 result |= DATA_EXIST;
14196 case R_NDS32_LABEL:
14197 if (((*irel)->r_addend & 0x1f) == 2)
14199 /* Check this point is align and decide to do ex9 or not. */
14200 result |= CLEAN_PRE;
14201 if (((*irel)->r_offset -
14202 get_nds32_elf_blank_total (&relax_blank_list,
14203 (*irel)->r_offset, 0)) & 0x02)
14204 result |= PUSH_PRE;
14207 case R_NDS32_32_RELA:
14209 result |= (4 << 24);
14210 result |= DATA_EXIST;
14212 case R_NDS32_16_RELA:
14214 result |= (2 << 24);
14215 result |= DATA_EXIST;
14219 /* The least code alignment is 2. If the data is only one byte,
14220 we have to shift one more byte. */
14221 if ((*irel)->r_addend == 1)
14222 result |= ((*irel)->r_addend << 25) ;
14224 result |= ((*irel)->r_addend << 24) ;
14226 result |= DATA_EXIST;
14229 case R_NDS32_25_PCREL_RELA:
14230 case R_NDS32_SDA16S3_RELA:
14231 case R_NDS32_SDA15S3_RELA:
14232 case R_NDS32_SDA15S3:
14233 case R_NDS32_SDA17S2_RELA:
14234 case R_NDS32_SDA15S2_RELA:
14235 case R_NDS32_SDA12S2_SP_RELA:
14236 case R_NDS32_SDA12S2_DP_RELA:
14237 case R_NDS32_SDA15S2:
14238 case R_NDS32_SDA18S1_RELA:
14239 case R_NDS32_SDA15S1_RELA:
14240 case R_NDS32_SDA15S1:
14241 case R_NDS32_SDA19S0_RELA:
14242 case R_NDS32_SDA15S0_RELA:
14243 case R_NDS32_SDA15S0:
14244 case R_NDS32_HI20_RELA:
14245 case R_NDS32_LO12S0_ORI_RELA:
14246 case R_NDS32_LO12S0_RELA:
14247 case R_NDS32_LO12S1_RELA:
14248 case R_NDS32_LO12S2_RELA:
14249 /* These relocation is supported ex9 relaxation currently. */
14250 /* We have to save the relocation for using later, since we have
14251 to check there is any alignment in the same address. */
14255 /* Not support relocations. */
14256 if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
14257 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE
14258 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_INSN16)
14260 /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
14261 But we have to consider if there is any side-effect. */
14262 if (!(result & DATA_EXIST))
14264 /* We have to confirm there is no data relocation in the
14265 same address. In general case, this won't happen. */
14266 /* We have to do ex9 conservative, for those relocation not
14267 considerd we ignore instruction. */
14268 result |= DATA_EXIST;
14269 if (*(contents + *off) & 0x80)
14270 result |= (2 << 24);
14272 result |= (4 << 24);
14277 if ((*irel) < irelend
14278 && ((*irel) + 1) < irelend
14279 && (*irel)->r_offset == ((*irel) + 1)->r_offset)
14280 /* There are relocations pointing to the same address, we have to
14281 check all of them. */
14293 /* Replace with ex9 instruction. */
14296 nds32_elf_ex9_push_insn (uint16_t insn16, bfd_byte *contents, bfd_vma pre_off,
14297 nds32_elf_blank_t **relax_blank_list,
14298 struct elf_nds32_irel_entry *pre_irel_ptr,
14299 struct elf_nds32_irel_entry **irel_list)
14303 /* Implement the ex9 relaxation. */
14304 bfd_putb16 (insn16, contents + pre_off);
14305 if (!insert_nds32_elf_blank_recalc_total (relax_blank_list,
14308 if (pre_irel_ptr != NULL)
14309 nds32_elf_insert_irel_entry (irel_list, pre_irel_ptr);
14314 /* Replace input file instruction which is in ex9 itable. */
14317 nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
14319 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14320 bfd_byte *contents = NULL;
14322 uint16_t insn16, insn_ex9;
14323 /* `pre_*' are used to track previous instruction that can use ex9.it. */
14324 bfd_vma pre_off = -1;
14325 uint16_t pre_insn16 = 0;
14326 struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
14327 Elf_Internal_Rela *internal_relocs;
14328 Elf_Internal_Rela *irel;
14329 Elf_Internal_Rela *irelend;
14330 Elf_Internal_Shdr *symtab_hdr;
14331 Elf_Internal_Sym *isym = NULL;
14332 nds32_elf_blank_t *relax_blank_list = NULL;
14334 uint32_t insn_with_reg = 0;
14336 uint32_t it_insn_with_reg;
14337 unsigned long r_symndx;
14339 struct elf_nds32_irel_entry *irel_list = NULL;
14340 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
14341 int data_flag, do_replace, save_irel;
14342 struct elf_link_hash_entry_list *h_list;
14345 /* Load section instructions, relocations, and symbol table. */
14346 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
14347 || !nds32_get_local_syms (abfd, sec, &isym))
14350 _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, TRUE /* keep_memory */);
14351 irelend = internal_relocs + sec->reloc_count;
14352 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14356 /* Check if the object enable ex9. */
14357 irel = find_relocs_at_address (internal_relocs, internal_relocs,
14358 irelend, R_NDS32_RELAX_ENTRY);
14360 /* Check this section trigger ex9 relaxation. */
14363 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
14364 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
14365 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
14368 irel = internal_relocs;
14370 /* Check alignment and fetch proper relocation. */
14371 while (off < sec->size)
14373 struct elf_link_hash_entry *h = NULL;
14374 struct elf_nds32_irel_entry *irel_ptr = NULL;
14376 /* Syn the instruction and the relocation. */
14377 while (irel != NULL && irel < irelend && irel->r_offset < off)
14380 data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
14381 relax_blank_list, sec,
14383 if (data_flag & PUSH_PRE)
14384 if (!nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14385 &relax_blank_list, pre_irel_ptr,
14389 if (data_flag & CLEAN_PRE)
14393 pre_irel_ptr = NULL;
14395 if (data_flag & DATA_EXIST)
14397 /* We save the move offset in the highest byte. */
14398 off += (data_flag >> 24);
14402 if (*(contents + off) & 0x80)
14404 /* 2-byte instruction. */
14409 /* Load the instruction and its opcode with register for comparing. */
14410 ex9_insn = ex9_insn_head;
14411 insn = bfd_getb32 (contents + off);
14415 it_insn = strtol (ex9_insn->string, NULL, 16);
14416 it_insn_with_reg = 0;
14420 if (irel != NULL && irel < irelend && irel->r_offset == off)
14422 /* Insn with relocation. */
14423 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
14425 if (ex9_insn->irel != NULL)
14426 nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn,
14427 &it_insn_with_reg);
14429 if (ex9_insn->irel != NULL
14430 && (ELF32_R_TYPE (irel->r_info) ==
14431 ELF32_R_TYPE (ex9_insn->irel->r_info))
14432 && (insn_with_reg == it_insn_with_reg))
14434 /* Insn relocation and format is the same as table entry. */
14436 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
14437 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
14438 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
14439 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
14440 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
14441 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
14442 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
14443 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
14444 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
14445 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
14446 && ELF32_R_TYPE (irel->r_info) <=
14447 R_NDS32_SDA12S2_SP_RELA)
14448 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
14449 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
14451 r_symndx = ELF32_R_SYM (irel->r_info);
14452 if (r_symndx < symtab_hdr->sh_info)
14454 /* Local symbol. */
14455 int shndx = isym[r_symndx].st_shndx;
14457 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14458 if (ex9_insn->sec == isec
14459 && ex9_insn->irel->r_addend == irel->r_addend
14460 && ex9_insn->irel->r_info == irel->r_info)
14468 /* External symbol. */
14469 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14470 if (ex9_insn->m_list)
14472 h_list = ex9_insn->m_list->h_list;
14476 && (ex9_insn->m_list->irel->r_addend ==
14483 h_list = h_list->next;
14488 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
14490 r_symndx = ELF32_R_SYM (irel->r_info);
14491 if (r_symndx < symtab_hdr->sh_info)
14493 /* Local symbols. Compare its base symbol and offset. */
14494 int shndx = isym[r_symndx].st_shndx;
14496 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14497 if (ex9_insn->sec == isec
14498 && ex9_insn->irel->r_addend == irel->r_addend
14499 && ex9_insn->irel->r_info == irel->r_info)
14507 /* External symbol. */
14508 struct elf_link_hash_entry_mul_list *m_list;
14510 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14511 m_list = ex9_insn->m_list;
14515 h_list = m_list->h_list;
14520 && (m_list->irel->r_addend
14521 == irel->r_addend))
14526 && ex9_insn->m_list
14527 && ex9_insn->m_list == ex9_insn->next->m_list)
14529 /* sethi multiple entry must be fixed */
14530 nds32_elf_ex9_insert_fix (sec, irel,
14531 h, ex9_insn->order);
14535 h_list = h_list->next;
14537 m_list = m_list->next;
14543 /* Import table: Check the symbol hash table and the
14544 jump target. Only R_NDS32_25_PCREL_RELA now. */
14545 else if (ex9_insn->times == -1
14546 && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
14548 nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
14549 if (insn_with_reg == it_insn_with_reg)
14552 bfd_vma relocation;
14554 r_symndx = ELF32_R_SYM (irel->r_info);
14555 if (r_symndx >= symtab_hdr->sh_info)
14557 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14558 if ((h->root.type == bfd_link_hash_defined
14559 || h->root.type == bfd_link_hash_defweak)
14560 && h->root.u.def.section != NULL
14561 && h->root.u.def.section->output_section != NULL
14562 && h->root.u.def.section->gc_mark == 1
14563 && bfd_is_abs_section (h->root.u.def.section)
14564 && h->root.u.def.value > sec->size)
14566 relocation = h->root.u.def.value +
14567 h->root.u.def.section->output_section->vma +
14568 h->root.u.def.section->output_offset;
14569 relocation += irel->r_addend;
14570 insn = insn_with_reg
14571 | ((relocation >> 1) & 0xffffff);
14572 snprintf (code, sizeof (code), "%08x", insn);
14573 if (strcmp (code, ex9_insn->string) == 0)
14582 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
14583 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
14584 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
14586 /* These relocations do not have to relocate contens, so it can
14587 be regard as instruction without relocation. */
14588 if (insn == it_insn && ex9_insn->irel == NULL)
14594 /* Instruction without relocation, we only
14595 have to compare their byte code. */
14596 if (insn == it_insn && ex9_insn->irel == NULL)
14600 /* Insntruction match so replacing the code here. */
14601 if (do_replace == 1)
14603 /* There are two formats of ex9 instruction. */
14604 if (ex9_insn->order < 32)
14605 insn_ex9 = INSN_EX9_IT_2;
14607 insn_ex9 = INSN_EX9_IT_1;
14608 insn16 = insn_ex9 | ex9_insn->order;
14610 /* Insert ex9 instruction. */
14611 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14612 &relax_blank_list, pre_irel_ptr,
14615 pre_insn16 = insn16;
14619 /* For instuction with relocation do relax. */
14620 irel_ptr = (struct elf_nds32_irel_entry *)
14621 bfd_malloc (sizeof (struct elf_nds32_irel_entry));
14622 irel_ptr->irel = irel;
14623 irel_ptr->next = NULL;
14624 pre_irel_ptr = irel_ptr;
14627 pre_irel_ptr = NULL;
14630 ex9_insn = ex9_insn->next;
14635 /* Insert ex9 instruction. */
14636 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14637 &relax_blank_list, pre_irel_ptr,
14640 /* Delete the redundant code. */
14641 if (relax_blank_list)
14643 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
14644 relax_blank_list = NULL;
14647 /* Clear the relocation that is replaced by ex9. */
14650 struct elf_nds32_irel_entry *irel_ptr;
14652 irel_ptr = irel_list;
14653 irel_list = irel_ptr->next;
14654 irel_ptr->irel->r_info =
14655 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
14661 /* Initialize ex9 hash table. */
14664 nds32_elf_ex9_init (void)
14666 if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
14667 sizeof (struct elf_nds32_code_hash_entry),
14670 _bfd_error_handler (_("Linker: cannot init ex9 hash table error \n"));
14676 /* Predict how many bytes will be relaxed with ex9 and ifc. */
14679 nds32_elf_ex9_total_relax (struct bfd_link_info *info)
14681 struct elf_nds32_insn_times_entry *ex9_insn;
14682 struct elf_nds32_insn_times_entry *temp;
14683 int target_optimize;
14684 struct elf_nds32_link_hash_table *table;
14686 if (ex9_insn_head == NULL)
14689 table = nds32_elf_hash_table (info);
14690 target_optimize = table->target_optimize;
14691 ex9_insn = ex9_insn_head;
14694 ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
14696 ex9_insn = ex9_insn->next;
14699 ex9_insn_head = NULL;
14701 if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
14703 /* Examine ifc reduce size. */
14704 struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
14705 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
14710 if (ifc_ent->enable == 0)
14713 irel_ptr = ifc_ent->irel_head;
14717 irel_ptr = irel_ptr->next;
14721 ifc_ent = ifc_ent->next;
14723 ex9_relax_size += size;
14727 /* Finish ex9 table. */
14730 nds32_elf_ex9_finish (struct bfd_link_info *link_info)
14732 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14733 nds32_elf_order_insn_times (link_info);
14734 nds32_elf_ex9_total_relax (link_info);
14735 /* Traverse the hash table and count its times. */
14736 nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
14737 nds32_elf_order_insn_times (link_info);
14738 nds32_elf_ex9_build_itable (link_info);
14741 /* Relocate the entries in ex9 table. */
14744 nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
14745 struct bfd_link_info *link_info)
14747 Elf_Internal_Sym *isym = NULL;
14748 bfd_vma relocation = -1;
14749 struct elf_link_hash_entry *h;
14751 if (ptr->m_list != NULL)
14753 /* Global symbol. */
14754 h = ptr->m_list->h_list->h;
14755 if ((h->root.type == bfd_link_hash_defined
14756 || h->root.type == bfd_link_hash_defweak)
14757 && h->root.u.def.section != NULL
14758 && h->root.u.def.section->output_section != NULL)
14761 relocation = h->root.u.def.value +
14762 h->root.u.def.section->output_section->vma +
14763 h->root.u.def.section->output_offset;
14764 relocation += ptr->m_list->irel->r_addend;
14769 else if (ptr->sec !=NULL)
14771 /* Local symbol. */
14772 Elf_Internal_Sym sym;
14773 asection *sec = NULL;
14775 asection *isec_ptr = &isec;
14776 Elf_Internal_Rela irel_backup = *(ptr->irel);
14777 asection *sec_backup = ptr->sec;
14778 bfd *abfd = ptr->sec->owner;
14780 if (!nds32_get_local_syms (abfd, sec, &isym))
14782 isym = isym + ELF32_R_SYM (ptr->irel->r_info);
14784 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14789 /* The purpose is same as elf_link_input_bfd. */
14790 if (isec_ptr != NULL
14791 && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
14792 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
14795 _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
14796 elf_section_data (isec_ptr)->sec_info,
14799 relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
14800 &ptr->sec, ptr->irel);
14801 if (ptr->irel != NULL)
14802 relocation += ptr->irel->r_addend;
14804 /* Restore origin value since there may be some insntructions that
14805 could not be replaced with ex9.it. */
14806 *(ptr->irel) = irel_backup;
14807 ptr->sec = sec_backup;
14813 /* Import ex9 table and build list. */
14816 nds32_elf_ex9_import_table (struct bfd_link_info *info)
14819 bfd_byte *contents;
14820 FILE *ex9_import_file;
14821 int update_ex9_table;
14822 struct elf_nds32_link_hash_table *table;
14824 table = nds32_elf_hash_table (info);
14825 ex9_import_file = table->ex9_import_file;
14826 rewind (table->ex9_import_file);
14828 contents = bfd_malloc (sizeof (bfd_byte) * 4);
14830 /* Read instructions from the input file and build the list. */
14831 while (!feof (ex9_import_file))
14833 unsigned long insn;
14835 struct elf_nds32_insn_times_entry *ptr;
14838 nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
14839 /* Ignore the final byte 0x0a. */
14842 insn = bfd_getb32 (contents);
14843 code = bfd_malloc (sizeof (char) * 9);
14844 snprintf (code, 9, "%08lx", (insn & 0xffffffff));
14845 ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14846 ptr->string = code;
14850 ptr->m_list = NULL;
14851 ptr->rel_backup.r_offset = 0;
14852 ptr->rel_backup.r_info = 0;
14853 ptr->rel_backup.r_addend = 0;
14856 nds32_elf_ex9_insert_entry (ptr);
14860 update_ex9_table = table->update_ex9_table;
14861 if (update_ex9_table == 1)
14863 /* It has to consider of sethi need to use multiple page
14864 but it not be done yet. */
14865 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14866 nds32_elf_order_insn_times (info);
14870 /* Export ex9 table. */
14873 nds32_elf_ex9_export (struct bfd_link_info *info,
14874 bfd_byte *contents, int size)
14876 FILE *ex9_export_file;
14877 struct elf_nds32_link_hash_table *table;
14879 table = nds32_elf_hash_table (info);
14880 ex9_export_file = table->ex9_export_file;
14881 fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
14882 fclose (ex9_export_file);
14885 /* Adjust relocations of J and JAL in ex9.itable.
14886 Export ex9 table. */
14889 nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
14891 asection *table_sec = NULL;
14892 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14893 struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
14895 uint32_t insn, insn_with_reg, source_insn;
14896 bfd_byte *contents = NULL, *source_contents = NULL;
14899 int shift, update_ex9_table, offset = 0;
14900 reloc_howto_type *howto = NULL;
14901 Elf_Internal_Rela rel_backup;
14902 unsigned short insn_ex9;
14903 struct elf_nds32_link_hash_table *table;
14904 FILE *ex9_export_file;
14905 static bfd_boolean done = FALSE;
14912 table = nds32_elf_hash_table (link_info);
14914 table->relax_status |= NDS32_RELAX_EX9_DONE;
14917 update_ex9_table = table->update_ex9_table;
14918 /* Generated ex9.itable exactly. */
14919 if (update_ex9_table == 0)
14921 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
14922 it_abfd = it_abfd->link.next)
14924 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
14925 if (table_sec != NULL)
14929 if (table_sec != NULL)
14933 output_bfd = table_sec->output_section->owner;
14934 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
14935 if (table_sec->size == 0)
14938 if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
14947 output_bfd = link_info->input_bfds->sections->output_section->owner;
14948 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
14949 contents = bfd_malloc (sizeof (bfd_byte) * 2048);
14952 /* Relocate instruction. */
14955 bfd_vma relocation, min_relocation = 0xffffffff;
14957 insn = strtol (ex9_insn->string, NULL, 16);
14959 if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
14961 if (ex9_insn->m_list)
14962 rel_backup = ex9_insn->m_list->rel_backup;
14964 rel_backup = ex9_insn->rel_backup;
14966 nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
14968 bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
14969 (rel_backup.r_info));
14970 shift = howto->rightshift;
14971 if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
14972 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
14973 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
14974 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
14975 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
14977 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
14979 insn_with_reg | ((relocation >> shift) &
14980 nds32_elf_irel_mask (&rel_backup));
14981 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14983 else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
14984 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
14985 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
14986 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
14987 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
14988 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
14989 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
14990 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
14992 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
14994 insn_with_reg | (((relocation - gp) >> shift) &
14995 nds32_elf_irel_mask (&rel_backup));
14996 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14998 else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
15000 /* Sethi may be multiple entry for one insn. */
15001 if (ex9_insn->next && ex9_insn->m_list
15002 && ex9_insn->m_list == ex9_insn->next->m_list)
15004 struct elf_link_hash_entry_mul_list *m_list;
15005 struct elf_nds32_ex9_refix *fix_ptr;
15006 struct elf_link_hash_entry *h;
15008 temp_ptr = ex9_insn;
15009 temp_ptr2 = ex9_insn;
15010 m_list = ex9_insn->m_list;
15013 h = m_list->h_list->h;
15014 relocation = h->root.u.def.value +
15015 h->root.u.def.section->output_section->vma +
15016 h->root.u.def.section->output_offset;
15017 relocation += m_list->irel->r_addend;
15019 if (relocation < min_relocation)
15020 min_relocation = relocation;
15021 m_list = m_list->next;
15023 relocation = min_relocation;
15025 /* Put insntruction into ex9 table. */
15026 insn = insn_with_reg
15027 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15028 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15029 relocation = relocation + 0x1000; /* hi20 */
15031 while (ex9_insn->next && ex9_insn->m_list
15032 && ex9_insn->m_list == ex9_insn->next->m_list)
15034 /* Multiple sethi. */
15035 ex9_insn = ex9_insn->next;
15038 insn_with_reg | ((relocation >> shift) &
15039 nds32_elf_irel_mask (&rel_backup));
15040 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15041 relocation = relocation + 0x1000; /* hi20 */
15044 fix_ptr = ex9_refix_head;
15047 /* Fix ex9 insn. */
15048 /* temp_ptr2 points to the head of multiple sethi. */
15049 temp_ptr = temp_ptr2;
15050 while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
15052 fix_ptr = fix_ptr->next;
15054 if (fix_ptr->order != temp_ptr->order)
15057 /* Set source insn. */
15059 fix_ptr->h->root.u.def.value +
15060 fix_ptr->h->root.u.def.section->output_section->vma +
15061 fix_ptr->h->root.u.def.section->output_offset;
15062 relocation += fix_ptr->irel->r_addend;
15063 /* sethi imm is imm20s. */
15064 source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);
15068 /* Match entry and source code. */
15069 insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
15070 if (insn == source_insn)
15072 /* Fix the ex9 insn. */
15073 if (temp_ptr->order != fix_ptr->order)
15075 if (!nds32_get_section_contents
15076 (fix_ptr->sec->owner, fix_ptr->sec,
15077 &source_contents, TRUE))
15079 (_("Linker: error cannot fixed ex9 relocation \n"));
15080 if (temp_ptr->order < 32)
15081 insn_ex9 = INSN_EX9_IT_2;
15083 insn_ex9 = INSN_EX9_IT_1;
15084 insn_ex9 = insn_ex9 | temp_ptr->order;
15085 bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
15091 if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
15093 (_("Linker: error cannot fixed ex9 relocation \n"));
15095 temp_ptr = temp_ptr->next;
15098 fix_ptr = fix_ptr->next;
15103 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15104 insn = insn_with_reg
15105 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15106 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15112 /* Insn without relocation does not have to be fixed
15113 if need to update export table. */
15114 if (update_ex9_table == 1)
15115 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15117 ex9_insn = ex9_insn->next;
15121 ex9_export_file = table->ex9_export_file;
15122 if (ex9_export_file != NULL)
15123 nds32_elf_ex9_export (link_info, contents, table_sec->size);
15124 else if (update_ex9_table == 1)
15126 table->ex9_export_file = table->ex9_import_file;
15127 rewind (table->ex9_export_file);
15128 nds32_elf_ex9_export (link_info, contents, size);
15132 /* Generate ex9 hash table. */
15135 nds32_elf_ex9_build_hash_table (bfd *abfd, asection *sec,
15136 struct bfd_link_info *link_info)
15138 Elf_Internal_Rela *internal_relocs;
15139 Elf_Internal_Rela *irelend;
15140 Elf_Internal_Rela *irel;
15141 Elf_Internal_Rela *jrel;
15142 Elf_Internal_Rela rel_backup;
15143 Elf_Internal_Shdr *symtab_hdr;
15144 Elf_Internal_Sym *isym = NULL;
15146 struct elf_link_hash_entry **sym_hashes;
15147 bfd_byte *contents = NULL;
15149 unsigned long r_symndx;
15150 uint32_t insn, insn_with_reg;
15151 struct elf_link_hash_entry *h;
15152 int data_flag, shift, align;
15153 bfd_vma relocation;
15154 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
15155 reloc_howto_type *howto = NULL;
15157 sym_hashes = elf_sym_hashes (abfd);
15158 /* Load section instructions, relocations, and symbol table. */
15159 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
15162 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
15163 TRUE /* keep_memory */);
15164 irelend = internal_relocs + sec->reloc_count;
15165 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
15166 if (!nds32_get_local_syms (abfd, sec, &isym))
15169 /* Check the object if enable ex9. */
15170 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
15171 R_NDS32_RELAX_ENTRY);
15173 /* Check this section trigger ex9 relaxation. */
15176 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
15177 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
15178 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
15181 irel = internal_relocs;
15183 /* Push each insn into hash table. */
15184 while (off < sec->size)
15187 struct elf_nds32_code_hash_entry *entry;
15189 while (irel != NULL && irel < irelend && irel->r_offset < off)
15192 data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend,
15193 NULL, sec, &off, contents);
15194 if (data_flag & DATA_EXIST)
15196 /* We save the move offset in the highest byte. */
15197 off += (data_flag >> 24);
15201 if (*(contents + off) & 0x80)
15210 rel_backup.r_info = 0;
15211 rel_backup.r_offset = 0;
15212 rel_backup.r_addend = 0;
15213 /* Load the instruction and its opcode with register for comparing. */
15214 insn = bfd_getb32 (contents + off);
15216 if (irel != NULL && irel < irelend && irel->r_offset == off)
15218 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
15219 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
15220 shift = howto->rightshift;
15221 align = (1 << shift) - 1;
15222 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
15223 || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
15224 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
15225 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
15226 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
15227 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
15228 ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15229 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15230 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15231 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15232 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15233 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15234 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15235 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15237 r_symndx = ELF32_R_SYM (irel->r_info);
15239 rel_backup = *irel;
15240 if (r_symndx < symtab_hdr->sh_info)
15242 /* Local symbol. */
15243 int shndx = isym[r_symndx].st_shndx;
15245 bfd_vma st_value = (isym + r_symndx)->st_value;
15246 isec = elf_elfsections (abfd)[shndx]->bfd_section;
15247 relocation = (isec->output_section->vma + isec->output_offset
15248 + st_value + irel->r_addend);
15252 /* External symbol. */
15253 bfd_boolean warned ATTRIBUTE_UNUSED;
15254 bfd_boolean ignored ATTRIBUTE_UNUSED;
15255 bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
15258 /* Maybe there is a better way to get h and relocation */
15259 RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
15260 r_symndx, symtab_hdr, sym_hashes,
15261 h, sym_sec, relocation,
15262 unresolved_reloc, warned, ignored);
15263 relocation += irel->r_addend;
15264 if ((h->root.type != bfd_link_hash_defined
15265 && h->root.type != bfd_link_hash_defweak)
15266 || strcmp (h->root.root.string, "_FP_BASE_") == 0)
15273 /* Check for gp relative instruction alignment. */
15274 if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15275 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15276 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15277 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15278 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15279 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15280 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15281 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15284 bfd *output_bfd = sec->output_section->owner;
15285 bfd_reloc_status_type r;
15287 /* If the symbol is in the abs section, the out_bfd will be null.
15288 This happens when the relocation has a symbol@GOTOFF. */
15289 r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15290 if (r != bfd_reloc_ok)
15298 /* Make sure alignment is correct. */
15299 if (relocation & align)
15301 /* Incorrect alignment. */
15303 /* xgettext:c-format */
15304 (_("%B: warning: unaligned small data access "
15305 "for entry: {%Ld, %Ld, %Ld}, addr = %#Lx, align = %#x"),
15306 abfd, irel->r_offset,
15307 irel->r_info, irel->r_addend, relocation, align);
15313 insn = insn_with_reg
15314 | ((relocation >> shift) & nds32_elf_irel_mask (irel));
15316 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
15317 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
15318 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
15320 /* These relocations do not have to relocate contens, so it can
15321 be regard as instruction without relocation. */
15330 snprintf (code, sizeof (code), "%08x", insn);
15332 entry = (struct elf_nds32_code_hash_entry*)
15333 bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
15337 (_("failed creating ex9.it %s hash table entry"), code);
15342 if (h->root.type == bfd_link_hash_undefined)
15344 /* Global symbol. */
15345 /* In order to do sethi with different symbol but same value. */
15346 if (entry->m_list == NULL)
15348 struct elf_link_hash_entry_mul_list *m_list_new;
15349 struct elf_link_hash_entry_list *h_list_new;
15351 m_list_new = (struct elf_link_hash_entry_mul_list *)
15352 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15353 h_list_new = (struct elf_link_hash_entry_list *)
15354 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15355 entry->m_list = m_list_new;
15356 m_list_new->h_list = h_list_new;
15357 m_list_new->rel_backup = rel_backup;
15358 m_list_new->times = 1;
15359 m_list_new->irel = jrel;
15360 m_list_new->next = NULL;
15362 h_list_new->next = NULL;
15366 struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
15367 struct elf_link_hash_entry_list *h_list;
15371 /* Build the different symbols that point to the same address. */
15372 h_list = m_list->h_list;
15373 if (h_list->h->root.u.def.value == h->root.u.def.value
15374 && h_list->h->root.u.def.section->output_section->vma
15375 == h->root.u.def.section->output_section->vma
15376 && h_list->h->root.u.def.section->output_offset
15377 == h->root.u.def.section->output_offset
15378 && m_list->rel_backup.r_addend == rel_backup.r_addend)
15381 m_list->irel = jrel;
15382 while (h_list->h != h && h_list->next)
15383 h_list = h_list->next;
15384 if (h_list->h != h)
15386 struct elf_link_hash_entry_list *h_list_new;
15388 h_list_new = (struct elf_link_hash_entry_list *)
15389 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15390 h_list->next = h_list_new;
15392 h_list_new->next = NULL;
15396 /* The sethi case may have different address but the
15397 hi20 is the same. */
15398 else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
15399 && m_list->next == NULL)
15401 struct elf_link_hash_entry_mul_list *m_list_new;
15402 struct elf_link_hash_entry_list *h_list_new;
15404 m_list_new = (struct elf_link_hash_entry_mul_list *)
15405 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15406 h_list_new = (struct elf_link_hash_entry_list *)
15407 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15408 m_list->next = m_list_new;
15409 m_list_new->h_list = h_list_new;
15410 m_list_new->rel_backup = rel_backup;
15411 m_list_new->times = 1;
15412 m_list_new->irel = jrel;
15413 m_list_new->next = NULL;
15415 h_list_new->next = NULL;
15418 m_list = m_list->next;
15429 /* Local symbol and insn without relocation*/
15431 entry->rel_backup = rel_backup;
15434 /* Use in sethi insn with constant and global symbol in same format. */
15436 entry->const_insn = 1;
15438 entry->irel = jrel;
15446 /* Set the _ITB_BASE, and point it to ex9 table. */
15449 nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
15453 bfd *output_bfd = NULL;
15454 struct bfd_link_hash_entry *bh = NULL;
15456 if (is_ITB_BASE_set == 1)
15459 is_ITB_BASE_set = 1;
15461 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_", FALSE, FALSE, TRUE);
15463 if (bh && (bh->type == bfd_link_hash_defined
15464 || bh->type == bfd_link_hash_defweak))
15467 for (abfd = link_info->input_bfds; abfd != NULL;
15468 abfd = abfd->link.next)
15470 sec = bfd_get_section_by_name (abfd, ".ex9.itable");
15473 output_bfd = sec->output_section->owner;
15477 if (output_bfd == NULL)
15479 output_bfd = link_info->output_bfd;
15480 if (output_bfd->sections == NULL)
15483 sec = bfd_abs_section_ptr;
15485 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
15486 FALSE, FALSE, TRUE);
15487 return (_bfd_generic_link_add_one_symbol
15488 (link_info, output_bfd, "_ITB_BASE_",
15489 BSF_GLOBAL | BSF_WEAK, sec, 0,
15490 (const char *) NULL, FALSE, get_elf_backend_data
15491 (output_bfd)->collect, &bh));
15495 #define ELF_ARCH bfd_arch_nds32
15496 #define ELF_MACHINE_CODE EM_NDS32
15497 #define ELF_MAXPAGESIZE 0x1000
15498 #define ELF_TARGET_ID NDS32_ELF_DATA
15500 #define TARGET_BIG_SYM nds32_elf32_be_vec
15501 #define TARGET_BIG_NAME "elf32-nds32be"
15502 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
15503 #define TARGET_LITTLE_NAME "elf32-nds32le"
15505 #define elf_info_to_howto nds32_info_to_howto
15506 #define elf_info_to_howto_rel nds32_info_to_howto_rel
15508 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
15509 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
15510 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
15511 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
15512 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
15514 #define bfd_elf32_mkobject nds32_elf_mkobject
15515 #define elf_backend_action_discarded nds32_elf_action_discarded
15516 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
15517 #define elf_backend_check_relocs nds32_elf_check_relocs
15518 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
15519 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
15520 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
15521 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
15522 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
15523 #define elf_backend_relocate_section nds32_elf_relocate_section
15524 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
15525 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
15526 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
15527 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
15528 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
15529 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
15530 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
15531 #define elf_backend_object_p nds32_elf_object_p
15532 #define elf_backend_final_write_processing nds32_elf_final_write_processing
15533 #define elf_backend_special_sections nds32_elf_special_sections
15534 #define bfd_elf32_bfd_get_relocated_section_contents \
15535 nds32_elf_get_relocated_section_contents
15537 #define elf_backend_can_gc_sections 1
15538 #define elf_backend_can_refcount 1
15539 #define elf_backend_want_got_plt 1
15540 #define elf_backend_plt_readonly 1
15541 #define elf_backend_want_plt_sym 0
15542 #define elf_backend_got_header_size 12
15543 #define elf_backend_may_use_rel_p 1
15544 #define elf_backend_default_use_rela_p 1
15545 #define elf_backend_may_use_rela_p 1
15546 #define elf_backend_dtrel_excludes_plt 1
15548 #include "elf32-target.h"
15550 #undef ELF_MAXPAGESIZE
15551 #define ELF_MAXPAGESIZE 0x2000
15553 #undef TARGET_BIG_SYM
15554 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
15555 #undef TARGET_BIG_NAME
15556 #define TARGET_BIG_NAME "elf32-nds32be-linux"
15557 #undef TARGET_LITTLE_SYM
15558 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
15559 #undef TARGET_LITTLE_NAME
15560 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
15562 #define elf32_bed elf32_nds32_lin_bed
15564 #include "elf32-target.h"