1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2014 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 (bfd *, bfd *);
81 static bfd_boolean nds32_elf_print_private_bfd_data (bfd *, void *);
82 static bfd_boolean nds32_elf_gc_sweep_hook
83 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
84 static bfd_boolean nds32_elf_check_relocs
85 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
86 static asection *nds32_elf_gc_mark_hook
87 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
88 struct elf_link_hash_entry *, Elf_Internal_Sym *);
89 static bfd_boolean nds32_elf_adjust_dynamic_symbol
90 (struct bfd_link_info *, struct elf_link_hash_entry *);
91 static bfd_boolean nds32_elf_size_dynamic_sections
92 (bfd *, struct bfd_link_info *);
93 static bfd_boolean nds32_elf_create_dynamic_sections
94 (bfd *, struct bfd_link_info *);
95 static bfd_boolean nds32_elf_finish_dynamic_sections
96 (bfd *, struct bfd_link_info *info);
97 static bfd_boolean nds32_elf_finish_dynamic_symbol
98 (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
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 *, bfd_byte **);
112 static bfd_boolean nds32_elf_ex9_build_hash_table
113 (bfd *, asection *, struct bfd_link_info *);
114 static void nds32_elf_get_insn_with_reg
115 (Elf_Internal_Rela *, unsigned long, unsigned long *);
116 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
117 Elf_Internal_Sym **);
118 static bfd_boolean nds32_elf_ex9_replace_instruction
119 (struct bfd_link_info *, bfd *, asection *);
120 static bfd_boolean nds32_elf_ifc_calc (struct bfd_link_info *, bfd *,
122 static bfd_boolean nds32_elf_ifc_replace (struct bfd_link_info *);
123 static bfd_boolean nds32_relax_fp_as_gp
124 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
125 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
126 Elf_Internal_Sym *isymbuf);
127 static bfd_boolean nds32_fag_remove_unused_fpbase
128 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
129 Elf_Internal_Rela *irelend);
133 MACH_V1 = bfd_mach_n1h,
134 MACH_V2 = bfd_mach_n1h_v2,
135 MACH_V3 = bfd_mach_n1h_v3,
136 MACH_V3M = bfd_mach_n1h_v3m
139 #define MIN(a, b) ((a) > (b) ? (b) : (a))
140 #define MAX(a, b) ((a) > (b) ? (a) : (b))
142 /* The name of the dynamic interpreter. This is put in the .interp
144 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
146 /* The nop opcode we use. */
147 #define NDS32_NOP32 0x40000009
148 #define NDS32_NOP16 0x9200
150 /* The size in bytes of an entry in the procedure linkage table. */
151 #define PLT_ENTRY_SIZE 24
152 #define PLT_HEADER_SIZE 24
154 /* The first entry in a procedure linkage table are reserved,
155 and the initial contents are unimportant (we zero them out).
156 Subsequent entries look like this. */
157 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
158 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
159 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
160 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
161 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
163 /* $ta is change to $r15 (from $r25). */
164 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
165 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
166 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
167 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
168 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
169 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
171 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
172 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
173 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
174 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
175 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
177 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
178 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
179 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
180 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
181 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
182 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
184 /* Size of small data/bss sections, used to calculate SDA_BASE. */
185 static long got_size = 0;
186 static int is_SDA_BASE_set = 0;
187 static int is_ITB_BASE_set = 0;
189 static int relax_active = 0;
191 /* Convert ELF-VER in eflags to string for debugging purpose. */
192 static const char *const nds32_elfver_strtab[] =
199 /* The nds32 linker needs to keep track of the number of relocs that it
200 decides to copy in check_relocs for each symbol. This is so that
201 it can discard PC relative relocs if it doesn't need them when
202 linking with -Bsymbolic. We store the information in a field
203 extending the regular ELF linker hash table. */
205 /* This structure keeps track of the number of PC relative relocs we
206 have copied for a given symbol. */
208 struct elf_nds32_pcrel_relocs_copied
211 struct elf_nds32_pcrel_relocs_copied *next;
212 /* A section in dynobj. */
214 /* Number of relocs copied in this section. */
218 /* The sh linker needs to keep track of the number of relocs that it
219 decides to copy as dynamic relocs in check_relocs for each symbol.
220 This is so that it can later discard them if they are found to be
221 unnecessary. We store the information in a field extending the
222 regular ELF linker hash table. */
224 struct elf_nds32_dyn_relocs
226 struct elf_nds32_dyn_relocs *next;
228 /* The input section of the reloc. */
231 /* Total number of relocs copied for the input section. */
234 /* Number of pc-relative relocs copied for the input section. */
235 bfd_size_type pc_count;
238 /* Nds32 ELF linker hash entry. */
240 struct elf_nds32_link_hash_entry
242 struct elf_link_hash_entry root;
244 /* Track dynamic relocs copied for this symbol. */
245 struct elf_nds32_dyn_relocs *dyn_relocs;
248 /* Get the nds32 ELF linker hash table from a link_info structure. */
250 #define FP_BASE_NAME "_FP_BASE_"
251 static int check_start_export_sym = 0;
252 static size_t ex9_relax_size = 0; /* Save ex9 predicted reducing size. */
254 /* Relocations used for relocation. */
255 static reloc_howto_type nds32_elf_howto_table[] =
257 /* This reloc does nothing. */
258 HOWTO (R_NDS32_NONE, /* type */
260 2, /* size (0 = byte, 1 = short, 2 = long) */
262 FALSE, /* pc_relative */
264 complain_overflow_bitfield, /* complain_on_overflow */
265 bfd_elf_generic_reloc, /* special_function */
266 "R_NDS32_NONE", /* name */
267 FALSE, /* partial_inplace */
270 FALSE), /* pcrel_offset */
272 /* A 16 bit absolute relocation. */
273 HOWTO (R_NDS32_16, /* type */
275 1, /* size (0 = byte, 1 = short, 2 = long) */
277 FALSE, /* pc_relative */
279 complain_overflow_bitfield, /* complain_on_overflow */
280 nds32_elf_generic_reloc, /* special_function */
281 "R_NDS32_16", /* name */
282 FALSE, /* partial_inplace */
283 0xffff, /* src_mask */
284 0xffff, /* dst_mask */
285 FALSE), /* pcrel_offset */
287 /* A 32 bit absolute relocation. */
288 HOWTO (R_NDS32_32, /* type */
290 2, /* size (0 = byte, 1 = short, 2 = long) */
292 FALSE, /* pc_relative */
294 complain_overflow_bitfield, /* complain_on_overflow */
295 nds32_elf_generic_reloc, /* special_function */
296 "R_NDS32_32", /* name */
297 FALSE, /* partial_inplace */
298 0xffffffff, /* src_mask */
299 0xffffffff, /* dst_mask */
300 FALSE), /* pcrel_offset */
302 /* A 20 bit address. */
303 HOWTO (R_NDS32_20, /* type */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
307 FALSE, /* pc_relative */
309 complain_overflow_unsigned, /* complain_on_overflow */
310 nds32_elf_generic_reloc, /* special_function */
311 "R_NDS32_20", /* name */
312 FALSE, /* partial_inplace */
313 0xfffff, /* src_mask */
314 0xfffff, /* dst_mask */
315 FALSE), /* pcrel_offset */
317 /* An PC Relative 9-bit relocation, shifted by 2.
318 This reloc is complicated because relocations are relative to pc & -4.
319 i.e. branches in the right insn slot use the address of the left insn
321 /* ??? It's not clear whether this should have partial_inplace set or not.
322 Branch relaxing in the assembler can store the addend in the insn,
323 and if bfd_install_relocation gets called the addend may get added
325 HOWTO (R_NDS32_9_PCREL, /* type */
327 1, /* size (0 = byte, 1 = short, 2 = long) */
329 TRUE, /* pc_relative */
331 complain_overflow_signed, /* complain_on_overflow */
332 nds32_elf_9_pcrel_reloc, /* special_function */
333 "R_NDS32_9_PCREL", /* name */
334 FALSE, /* partial_inplace */
337 TRUE), /* pcrel_offset */
339 /* A relative 15 bit relocation, right shifted by 1. */
340 HOWTO (R_NDS32_15_PCREL, /* type */
342 2, /* size (0 = byte, 1 = short, 2 = long) */
344 TRUE, /* pc_relative */
346 complain_overflow_signed, /* complain_on_overflow */
347 bfd_elf_generic_reloc, /* special_function */
348 "R_NDS32_15_PCREL", /* name */
349 FALSE, /* partial_inplace */
350 0x3fff, /* src_mask */
351 0x3fff, /* dst_mask */
352 TRUE), /* pcrel_offset */
354 /* A relative 17 bit relocation, right shifted by 1. */
355 HOWTO (R_NDS32_17_PCREL, /* type */
357 2, /* size (0 = byte, 1 = short, 2 = long) */
359 TRUE, /* pc_relative */
361 complain_overflow_signed, /* complain_on_overflow */
362 bfd_elf_generic_reloc, /* special_function */
363 "R_NDS32_17_PCREL", /* name */
364 FALSE, /* partial_inplace */
365 0xffff, /* src_mask */
366 0xffff, /* dst_mask */
367 TRUE), /* pcrel_offset */
369 /* A relative 25 bit relocation, right shifted by 1. */
370 /* ??? It's not clear whether this should have partial_inplace set or not.
371 Branch relaxing in the assembler can store the addend in the insn,
372 and if bfd_install_relocation gets called the addend may get added
374 HOWTO (R_NDS32_25_PCREL, /* type */
376 2, /* size (0 = byte, 1 = short, 2 = long) */
378 TRUE, /* pc_relative */
380 complain_overflow_signed, /* complain_on_overflow */
381 bfd_elf_generic_reloc, /* special_function */
382 "R_NDS32_25_PCREL", /* name */
383 FALSE, /* partial_inplace */
384 0xffffff, /* src_mask */
385 0xffffff, /* dst_mask */
386 TRUE), /* pcrel_offset */
388 /* High 20 bits of address when lower 12 is or'd in. */
389 HOWTO (R_NDS32_HI20, /* type */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
393 FALSE, /* pc_relative */
395 complain_overflow_dont,/* complain_on_overflow */
396 nds32_elf_hi20_reloc, /* special_function */
397 "R_NDS32_HI20", /* name */
398 FALSE, /* partial_inplace */
399 0x000fffff, /* src_mask */
400 0x000fffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
403 /* Lower 12 bits of address. */
404 HOWTO (R_NDS32_LO12S3, /* type */
406 2, /* size (0 = byte, 1 = short, 2 = long) */
408 FALSE, /* pc_relative */
410 complain_overflow_dont,/* complain_on_overflow */
411 nds32_elf_lo12_reloc, /* special_function */
412 "R_NDS32_LO12S3", /* name */
413 FALSE, /* partial_inplace */
414 0x000001ff, /* src_mask */
415 0x000001ff, /* dst_mask */
416 FALSE), /* pcrel_offset */
418 /* Lower 12 bits of address. */
419 HOWTO (R_NDS32_LO12S2, /* type */
421 2, /* size (0 = byte, 1 = short, 2 = long) */
423 FALSE, /* pc_relative */
425 complain_overflow_dont,/* complain_on_overflow */
426 nds32_elf_lo12_reloc, /* special_function */
427 "R_NDS32_LO12S2", /* name */
428 FALSE, /* partial_inplace */
429 0x000003ff, /* src_mask */
430 0x000003ff, /* dst_mask */
431 FALSE), /* pcrel_offset */
433 /* Lower 12 bits of address. */
434 HOWTO (R_NDS32_LO12S1, /* type */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
438 FALSE, /* pc_relative */
440 complain_overflow_dont,/* complain_on_overflow */
441 nds32_elf_lo12_reloc, /* special_function */
442 "R_NDS32_LO12S1", /* name */
443 FALSE, /* partial_inplace */
444 0x000007ff, /* src_mask */
445 0x000007ff, /* dst_mask */
446 FALSE), /* pcrel_offset */
448 /* Lower 12 bits of address. */
449 HOWTO (R_NDS32_LO12S0, /* type */
451 2, /* size (0 = byte, 1 = short, 2 = long) */
453 FALSE, /* pc_relative */
455 complain_overflow_dont,/* complain_on_overflow */
456 nds32_elf_lo12_reloc, /* special_function */
457 "R_NDS32_LO12S0", /* name */
458 FALSE, /* partial_inplace */
459 0x00000fff, /* src_mask */
460 0x00000fff, /* dst_mask */
461 FALSE), /* pcrel_offset */
463 /* Small data area 15 bits offset. */
464 HOWTO (R_NDS32_SDA15S3, /* type */
466 2, /* size (0 = byte, 1 = short, 2 = long) */
468 FALSE, /* pc_relative */
470 complain_overflow_signed, /* complain_on_overflow */
471 nds32_elf_sda15_reloc, /* special_function */
472 "R_NDS32_SDA15S3", /* name */
473 FALSE, /* partial_inplace */
474 0x00007fff, /* src_mask */
475 0x00007fff, /* dst_mask */
476 FALSE), /* pcrel_offset */
478 /* Small data area 15 bits offset. */
479 HOWTO (R_NDS32_SDA15S2, /* type */
481 2, /* size (0 = byte, 1 = short, 2 = long) */
483 FALSE, /* pc_relative */
485 complain_overflow_signed, /* complain_on_overflow */
486 nds32_elf_sda15_reloc, /* special_function */
487 "R_NDS32_SDA15S2", /* name */
488 FALSE, /* partial_inplace */
489 0x00007fff, /* src_mask */
490 0x00007fff, /* dst_mask */
491 FALSE), /* pcrel_offset */
493 /* Small data area 15 bits offset. */
494 HOWTO (R_NDS32_SDA15S1, /* type */
496 2, /* size (0 = byte, 1 = short, 2 = long) */
498 FALSE, /* pc_relative */
500 complain_overflow_signed, /* complain_on_overflow */
501 nds32_elf_sda15_reloc, /* special_function */
502 "R_NDS32_SDA15S1", /* name */
503 FALSE, /* partial_inplace */
504 0x00007fff, /* src_mask */
505 0x00007fff, /* dst_mask */
506 FALSE), /* pcrel_offset */
508 /* Small data area 15 bits offset. */
509 HOWTO (R_NDS32_SDA15S0, /* type */
511 2, /* size (0 = byte, 1 = short, 2 = long) */
513 FALSE, /* pc_relative */
515 complain_overflow_signed, /* complain_on_overflow */
516 nds32_elf_sda15_reloc, /* special_function */
517 "R_NDS32_SDA15S0", /* name */
518 FALSE, /* partial_inplace */
519 0x00007fff, /* src_mask */
520 0x00007fff, /* dst_mask */
521 FALSE), /* pcrel_offset */
523 /* GNU extension to record C++ vtable hierarchy */
524 HOWTO (R_NDS32_GNU_VTINHERIT, /* type */
526 2, /* size (0 = byte, 1 = short, 2 = long) */
528 FALSE, /* pc_relative */
530 complain_overflow_dont,/* complain_on_overflow */
531 NULL, /* special_function */
532 "R_NDS32_GNU_VTINHERIT", /* name */
533 FALSE, /* partial_inplace */
536 FALSE), /* pcrel_offset */
538 /* GNU extension to record C++ vtable member usage */
539 HOWTO (R_NDS32_GNU_VTENTRY, /* type */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
543 FALSE, /* pc_relative */
545 complain_overflow_dont,/* complain_on_overflow */
546 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
547 "R_NDS32_GNU_VTENTRY", /* name */
548 FALSE, /* partial_inplace */
551 FALSE), /* pcrel_offset */
553 /* A 16 bit absolute relocation. */
554 HOWTO (R_NDS32_16_RELA, /* type */
556 1, /* size (0 = byte, 1 = short, 2 = long) */
558 FALSE, /* pc_relative */
560 complain_overflow_bitfield, /* complain_on_overflow */
561 bfd_elf_generic_reloc, /* special_function */
562 "R_NDS32_16_RELA", /* name */
563 FALSE, /* partial_inplace */
564 0xffff, /* src_mask */
565 0xffff, /* dst_mask */
566 FALSE), /* pcrel_offset */
568 /* A 32 bit absolute relocation. */
569 HOWTO (R_NDS32_32_RELA, /* type */
571 2, /* size (0 = byte, 1 = short, 2 = long) */
573 FALSE, /* pc_relative */
575 complain_overflow_bitfield, /* complain_on_overflow */
576 bfd_elf_generic_reloc, /* special_function */
577 "R_NDS32_32_RELA", /* name */
578 FALSE, /* partial_inplace */
579 0xffffffff, /* src_mask */
580 0xffffffff, /* dst_mask */
581 FALSE), /* pcrel_offset */
583 /* A 20 bit address. */
584 HOWTO (R_NDS32_20_RELA, /* type */
586 2, /* size (0 = byte, 1 = short, 2 = long) */
588 FALSE, /* pc_relative */
590 complain_overflow_signed, /* complain_on_overflow */
591 bfd_elf_generic_reloc, /* special_function */
592 "R_NDS32_20_RELA", /* name */
593 FALSE, /* partial_inplace */
594 0xfffff, /* src_mask */
595 0xfffff, /* dst_mask */
596 FALSE), /* pcrel_offset */
598 HOWTO (R_NDS32_9_PCREL_RELA, /* type */
600 1, /* size (0 = byte, 1 = short, 2 = long) */
602 TRUE, /* pc_relative */
604 complain_overflow_signed, /* complain_on_overflow */
605 bfd_elf_generic_reloc, /* special_function */
606 "R_NDS32_9_PCREL_RELA",/* name */
607 FALSE, /* partial_inplace */
610 TRUE), /* pcrel_offset */
612 /* A relative 15 bit relocation, right shifted by 1. */
613 HOWTO (R_NDS32_15_PCREL_RELA, /* type */
615 2, /* size (0 = byte, 1 = short, 2 = long) */
617 TRUE, /* pc_relative */
619 complain_overflow_signed, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_NDS32_15_PCREL_RELA", /* name */
622 FALSE, /* partial_inplace */
623 0x3fff, /* src_mask */
624 0x3fff, /* dst_mask */
625 TRUE), /* pcrel_offset */
627 /* A relative 17 bit relocation, right shifted by 1. */
628 HOWTO (R_NDS32_17_PCREL_RELA, /* type */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
632 TRUE, /* pc_relative */
634 complain_overflow_signed, /* complain_on_overflow */
635 bfd_elf_generic_reloc, /* special_function */
636 "R_NDS32_17_PCREL_RELA", /* name */
637 FALSE, /* partial_inplace */
638 0xffff, /* src_mask */
639 0xffff, /* dst_mask */
640 TRUE), /* pcrel_offset */
642 /* A relative 25 bit relocation, right shifted by 2. */
643 HOWTO (R_NDS32_25_PCREL_RELA, /* type */
645 2, /* size (0 = byte, 1 = short, 2 = long) */
647 TRUE, /* pc_relative */
649 complain_overflow_signed, /* complain_on_overflow */
650 bfd_elf_generic_reloc, /* special_function */
651 "R_NDS32_25_PCREL_RELA", /* name */
652 FALSE, /* partial_inplace */
653 0xffffff, /* src_mask */
654 0xffffff, /* dst_mask */
655 TRUE), /* pcrel_offset */
657 /* High 20 bits of address when lower 16 is or'd in. */
658 HOWTO (R_NDS32_HI20_RELA, /* type */
660 2, /* size (0 = byte, 1 = short, 2 = long) */
662 FALSE, /* pc_relative */
664 complain_overflow_dont,/* complain_on_overflow */
665 bfd_elf_generic_reloc, /* special_function */
666 "R_NDS32_HI20_RELA", /* name */
667 FALSE, /* partial_inplace */
668 0x000fffff, /* src_mask */
669 0x000fffff, /* dst_mask */
670 FALSE), /* pcrel_offset */
672 /* Lower 12 bits of address. */
673 HOWTO (R_NDS32_LO12S3_RELA, /* type */
675 2, /* size (0 = byte, 1 = short, 2 = long) */
677 FALSE, /* pc_relative */
679 complain_overflow_dont,/* complain_on_overflow */
680 bfd_elf_generic_reloc, /* special_function */
681 "R_NDS32_LO12S3_RELA", /* name */
682 FALSE, /* partial_inplace */
683 0x000001ff, /* src_mask */
684 0x000001ff, /* dst_mask */
685 FALSE), /* pcrel_offset */
687 /* Lower 12 bits of address. */
688 HOWTO (R_NDS32_LO12S2_RELA, /* type */
690 2, /* size (0 = byte, 1 = short, 2 = long) */
692 FALSE, /* pc_relative */
694 complain_overflow_dont,/* complain_on_overflow */
695 bfd_elf_generic_reloc, /* special_function */
696 "R_NDS32_LO12S2_RELA", /* name */
697 FALSE, /* partial_inplace */
698 0x000003ff, /* src_mask */
699 0x000003ff, /* dst_mask */
700 FALSE), /* pcrel_offset */
702 /* Lower 12 bits of address. */
703 HOWTO (R_NDS32_LO12S1_RELA, /* type */
705 2, /* size (0 = byte, 1 = short, 2 = long) */
707 FALSE, /* pc_relative */
709 complain_overflow_dont,/* complain_on_overflow */
710 bfd_elf_generic_reloc, /* special_function */
711 "R_NDS32_LO12S1_RELA", /* name */
712 FALSE, /* partial_inplace */
713 0x000007ff, /* src_mask */
714 0x000007ff, /* dst_mask */
715 FALSE), /* pcrel_offset */
717 /* Lower 12 bits of address. */
718 HOWTO (R_NDS32_LO12S0_RELA, /* type */
720 2, /* size (0 = byte, 1 = short, 2 = long) */
722 FALSE, /* pc_relative */
724 complain_overflow_dont,/* complain_on_overflow */
725 bfd_elf_generic_reloc, /* special_function */
726 "R_NDS32_LO12S0_RELA", /* name */
727 FALSE, /* partial_inplace */
728 0x00000fff, /* src_mask */
729 0x00000fff, /* dst_mask */
730 FALSE), /* pcrel_offset */
732 /* Small data area 15 bits offset. */
733 HOWTO (R_NDS32_SDA15S3_RELA, /* type */
735 2, /* size (0 = byte, 1 = short, 2 = long) */
737 FALSE, /* pc_relative */
739 complain_overflow_signed, /* complain_on_overflow */
740 bfd_elf_generic_reloc, /* special_function */
741 "R_NDS32_SDA15S3_RELA",/* name */
742 FALSE, /* partial_inplace */
743 0x00007fff, /* src_mask */
744 0x00007fff, /* dst_mask */
745 FALSE), /* pcrel_offset */
747 /* Small data area 15 bits offset. */
748 HOWTO (R_NDS32_SDA15S2_RELA, /* type */
750 2, /* size (0 = byte, 1 = short, 2 = long) */
752 FALSE, /* pc_relative */
754 complain_overflow_signed, /* complain_on_overflow */
755 bfd_elf_generic_reloc, /* special_function */
756 "R_NDS32_SDA15S2_RELA",/* name */
757 FALSE, /* partial_inplace */
758 0x00007fff, /* src_mask */
759 0x00007fff, /* dst_mask */
760 FALSE), /* pcrel_offset */
762 HOWTO (R_NDS32_SDA15S1_RELA, /* type */
764 2, /* size (0 = byte, 1 = short, 2 = long) */
766 FALSE, /* pc_relative */
768 complain_overflow_signed, /* complain_on_overflow */
769 bfd_elf_generic_reloc, /* special_function */
770 "R_NDS32_SDA15S1_RELA",/* name */
771 FALSE, /* partial_inplace */
772 0x00007fff, /* src_mask */
773 0x00007fff, /* dst_mask */
774 FALSE), /* pcrel_offset */
776 HOWTO (R_NDS32_SDA15S0_RELA, /* type */
778 2, /* size (0 = byte, 1 = short, 2 = long) */
780 FALSE, /* pc_relative */
782 complain_overflow_signed, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_NDS32_SDA15S0_RELA",/* name */
785 FALSE, /* partial_inplace */
786 0x00007fff, /* src_mask */
787 0x00007fff, /* dst_mask */
788 FALSE), /* pcrel_offset */
790 /* GNU extension to record C++ vtable hierarchy */
791 HOWTO (R_NDS32_RELA_GNU_VTINHERIT, /* type */
793 2, /* size (0 = byte, 1 = short, 2 = long) */
795 FALSE, /* pc_relative */
797 complain_overflow_dont,/* complain_on_overflow */
798 NULL, /* special_function */
799 "R_NDS32_RELA_GNU_VTINHERIT", /* name */
800 FALSE, /* partial_inplace */
803 FALSE), /* pcrel_offset */
805 /* GNU extension to record C++ vtable member usage */
806 HOWTO (R_NDS32_RELA_GNU_VTENTRY, /* type */
808 2, /* size (0 = byte, 1 = short, 2 = long) */
810 FALSE, /* pc_relative */
812 complain_overflow_dont,/* complain_on_overflow */
813 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
814 "R_NDS32_RELA_GNU_VTENTRY", /* name */
815 FALSE, /* partial_inplace */
818 FALSE), /* pcrel_offset */
820 /* Like R_NDS32_20, but referring to the GOT table entry for
822 HOWTO (R_NDS32_GOT20, /* type */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
826 FALSE, /* pc_relative */
828 complain_overflow_signed, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_NDS32_GOT20", /* name */
831 FALSE, /* partial_inplace */
832 0xfffff, /* src_mask */
833 0xfffff, /* dst_mask */
834 FALSE), /* pcrel_offset */
836 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
837 entry for the symbol. */
838 HOWTO (R_NDS32_25_PLTREL, /* type */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
842 TRUE, /* pc_relative */
844 complain_overflow_signed, /* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_NDS32_25_PLTREL", /* name */
847 FALSE, /* partial_inplace */
848 0xffffff, /* src_mask */
849 0xffffff, /* dst_mask */
850 TRUE), /* pcrel_offset */
852 /* This is used only by the dynamic linker. The symbol should exist
853 both in the object being run and in some shared library. The
854 dynamic linker copies the data addressed by the symbol from the
855 shared library into the object, because the object being
856 run has to have the data at some particular address. */
857 HOWTO (R_NDS32_COPY, /* type */
859 2, /* size (0 = byte, 1 = short, 2 = long) */
861 FALSE, /* pc_relative */
863 complain_overflow_bitfield, /* complain_on_overflow */
864 bfd_elf_generic_reloc, /* special_function */
865 "R_NDS32_COPY", /* name */
866 FALSE, /* partial_inplace */
867 0xffffffff, /* src_mask */
868 0xffffffff, /* dst_mask */
869 FALSE), /* pcrel_offset */
871 /* Like R_NDS32_20, but used when setting global offset table
873 HOWTO (R_NDS32_GLOB_DAT, /* type */
875 2, /* size (0 = byte, 1 = short, 2 = long) */
877 FALSE, /* pc_relative */
879 complain_overflow_bitfield, /* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_NDS32_GLOB_DAT", /* name */
882 FALSE, /* partial_inplace */
883 0xffffffff, /* src_mask */
884 0xffffffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
887 /* Marks a procedure linkage table entry for a symbol. */
888 HOWTO (R_NDS32_JMP_SLOT, /* type */
890 2, /* size (0 = byte, 1 = short, 2 = long) */
892 FALSE, /* pc_relative */
894 complain_overflow_bitfield, /* complain_on_overflow */
895 bfd_elf_generic_reloc, /* special_function */
896 "R_NDS32_JMP_SLOT", /* name */
897 FALSE, /* partial_inplace */
898 0xffffffff, /* src_mask */
899 0xffffffff, /* dst_mask */
900 FALSE), /* pcrel_offset */
902 /* Used only by the dynamic linker. When the object is run, this
903 longword is set to the load address of the object, plus the
905 HOWTO (R_NDS32_RELATIVE, /* type */
907 2, /* size (0 = byte, 1 = short, 2 = long) */
909 FALSE, /* pc_relative */
911 complain_overflow_bitfield, /* complain_on_overflow */
912 bfd_elf_generic_reloc, /* special_function */
913 "R_NDS32_RELATIVE", /* name */
914 FALSE, /* partial_inplace */
915 0xffffffff, /* src_mask */
916 0xffffffff, /* dst_mask */
917 FALSE), /* pcrel_offset */
919 HOWTO (R_NDS32_GOTOFF, /* type */
921 2, /* size (0 = byte, 1 = short, 2 = long) */
923 FALSE, /* pc_relative */
925 complain_overflow_signed, /* complain_on_overflow */
926 bfd_elf_generic_reloc, /* special_function */
927 "R_NDS32_GOTOFF", /* name */
928 FALSE, /* partial_inplace */
929 0xfffff, /* src_mask */
930 0xfffff, /* dst_mask */
931 FALSE), /* pcrel_offset */
933 /* An PC Relative 20-bit relocation used when setting PIC offset
935 HOWTO (R_NDS32_GOTPC20, /* type */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
939 TRUE, /* pc_relative */
941 complain_overflow_signed, /* complain_on_overflow */
942 bfd_elf_generic_reloc, /* special_function */
943 "R_NDS32_GOTPC20", /* name */
944 FALSE, /* partial_inplace */
945 0xfffff, /* src_mask */
946 0xfffff, /* dst_mask */
947 TRUE), /* pcrel_offset */
949 /* Like R_NDS32_HI20, but referring to the GOT table entry for
951 HOWTO (R_NDS32_GOT_HI20, /* type */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
955 FALSE, /* pc_relative */
957 complain_overflow_dont,/* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_NDS32_GOT_HI20", /* name */
960 FALSE, /* partial_inplace */
961 0x000fffff, /* src_mask */
962 0x000fffff, /* dst_mask */
963 FALSE), /* pcrel_offset */
964 HOWTO (R_NDS32_GOT_LO12, /* type */
966 2, /* size (0 = byte, 1 = short, 2 = long) */
968 FALSE, /* pc_relative */
970 complain_overflow_dont,/* complain_on_overflow */
971 bfd_elf_generic_reloc, /* special_function */
972 "R_NDS32_GOT_LO12", /* name */
973 FALSE, /* partial_inplace */
974 0x00000fff, /* src_mask */
975 0x00000fff, /* dst_mask */
976 FALSE), /* pcrel_offset */
978 /* An PC Relative relocation used when setting PIC offset table register.
979 Like R_NDS32_HI20, but referring to the GOT table entry for
981 HOWTO (R_NDS32_GOTPC_HI20, /* type */
983 2, /* size (0 = byte, 1 = short, 2 = long) */
985 FALSE, /* pc_relative */
987 complain_overflow_dont,/* complain_on_overflow */
988 bfd_elf_generic_reloc, /* special_function */
989 "R_NDS32_GOTPC_HI20", /* name */
990 FALSE, /* partial_inplace */
991 0x000fffff, /* src_mask */
992 0x000fffff, /* dst_mask */
993 TRUE), /* pcrel_offset */
994 HOWTO (R_NDS32_GOTPC_LO12, /* type */
996 2, /* size (0 = byte, 1 = short, 2 = long) */
998 FALSE, /* pc_relative */
1000 complain_overflow_dont, /* complain_on_overflow */
1001 bfd_elf_generic_reloc, /* special_function */
1002 "R_NDS32_GOTPC_LO12", /* name */
1003 FALSE, /* partial_inplace */
1004 0x00000fff, /* src_mask */
1005 0x00000fff, /* dst_mask */
1006 TRUE), /* pcrel_offset */
1008 HOWTO (R_NDS32_GOTOFF_HI20, /* type */
1009 12, /* rightshift */
1010 2, /* size (0 = byte, 1 = short, 2 = long) */
1012 FALSE, /* pc_relative */
1014 complain_overflow_dont,/* complain_on_overflow */
1015 bfd_elf_generic_reloc, /* special_function */
1016 "R_NDS32_GOTOFF_HI20", /* name */
1017 FALSE, /* partial_inplace */
1018 0x000fffff, /* src_mask */
1019 0x000fffff, /* dst_mask */
1020 FALSE), /* pcrel_offset */
1021 HOWTO (R_NDS32_GOTOFF_LO12, /* type */
1023 2, /* size (0 = byte, 1 = short, 2 = long) */
1025 FALSE, /* pc_relative */
1027 complain_overflow_dont,/* complain_on_overflow */
1028 bfd_elf_generic_reloc, /* special_function */
1029 "R_NDS32_GOTOFF_LO12", /* name */
1030 FALSE, /* partial_inplace */
1031 0x00000fff, /* src_mask */
1032 0x00000fff, /* dst_mask */
1033 FALSE), /* pcrel_offset */
1035 /* Alignment hint for relaxable instruction. This is used with
1036 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1037 in order to make next label aligned on word boundary. */
1038 HOWTO (R_NDS32_INSN16, /* type */
1040 2, /* size (0 = byte, 1 = short, 2 = long) */
1042 FALSE, /* pc_relative */
1044 complain_overflow_dont,/* complain_on_overflow */
1045 nds32_elf_ignore_reloc,/* special_function */
1046 "R_NDS32_INSN16", /* name */
1047 FALSE, /* partial_inplace */
1048 0x00000fff, /* src_mask */
1049 0x00000fff, /* dst_mask */
1050 FALSE), /* pcrel_offset */
1052 /* Alignment hint for label. */
1053 HOWTO (R_NDS32_LABEL, /* type */
1055 2, /* size (0 = byte, 1 = short, 2 = long) */
1057 FALSE, /* pc_relative */
1059 complain_overflow_dont,/* complain_on_overflow */
1060 nds32_elf_ignore_reloc,/* special_function */
1061 "R_NDS32_LABEL", /* name */
1062 FALSE, /* partial_inplace */
1063 0xffffffff, /* src_mask */
1064 0xffffffff, /* dst_mask */
1065 FALSE), /* pcrel_offset */
1067 /* Relax hint for unconditional call sequence */
1068 HOWTO (R_NDS32_LONGCALL1, /* type */
1070 2, /* size (0 = byte, 1 = short, 2 = long) */
1072 FALSE, /* pc_relative */
1074 complain_overflow_dont,/* complain_on_overflow */
1075 nds32_elf_ignore_reloc,/* special_function */
1076 "R_NDS32_LONGCALL1", /* name */
1077 FALSE, /* partial_inplace */
1078 0xffffffff, /* src_mask */
1079 0xffffffff, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1082 /* Relax hint for conditional call sequence. */
1083 HOWTO (R_NDS32_LONGCALL2, /* type */
1085 2, /* size (0 = byte, 1 = short, 2 = long) */
1087 FALSE, /* pc_relative */
1089 complain_overflow_dont,/* complain_on_overflow */
1090 nds32_elf_ignore_reloc,/* special_function */
1091 "R_NDS32_LONGCALL2", /* name */
1092 FALSE, /* partial_inplace */
1093 0xffffffff, /* src_mask */
1094 0xffffffff, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1097 /* Relax hint for conditional call sequence. */
1098 HOWTO (R_NDS32_LONGCALL3, /* type */
1100 2, /* size (0 = byte, 1 = short, 2 = long) */
1102 FALSE, /* pc_relative */
1104 complain_overflow_dont,/* complain_on_overflow */
1105 nds32_elf_ignore_reloc,/* special_function */
1106 "R_NDS32_LONGCALL3", /* name */
1107 FALSE, /* partial_inplace */
1108 0xffffffff, /* src_mask */
1109 0xffffffff, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1112 /* Relax hint for unconditional branch sequence. */
1113 HOWTO (R_NDS32_LONGJUMP1, /* type */
1115 2, /* size (0 = byte, 1 = short, 2 = long) */
1117 FALSE, /* pc_relative */
1119 complain_overflow_dont,/* complain_on_overflow */
1120 nds32_elf_ignore_reloc,/* special_function */
1121 "R_NDS32_LONGJUMP1", /* name */
1122 FALSE, /* partial_inplace */
1123 0xffffffff, /* src_mask */
1124 0xffffffff, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1127 /* Relax hint for conditional branch sequence. */
1128 HOWTO (R_NDS32_LONGJUMP2, /* type */
1130 2, /* size (0 = byte, 1 = short, 2 = long) */
1132 FALSE, /* pc_relative */
1134 complain_overflow_dont,/* complain_on_overflow */
1135 nds32_elf_ignore_reloc,/* special_function */
1136 "R_NDS32_LONGJUMP2", /* name */
1137 FALSE, /* partial_inplace */
1138 0xffffffff, /* src_mask */
1139 0xffffffff, /* dst_mask */
1140 FALSE), /* pcrel_offset */
1142 /* Relax hint for conditional branch sequence. */
1143 HOWTO (R_NDS32_LONGJUMP3, /* type */
1145 2, /* size (0 = byte, 1 = short, 2 = long) */
1147 FALSE, /* pc_relative */
1149 complain_overflow_dont,/* complain_on_overflow */
1150 nds32_elf_ignore_reloc,/* special_function */
1151 "R_NDS32_LONGJUMP3", /* name */
1152 FALSE, /* partial_inplace */
1153 0xffffffff, /* src_mask */
1154 0xffffffff, /* dst_mask */
1155 FALSE), /* pcrel_offset */
1157 /* Relax hint for load/store sequence. */
1158 HOWTO (R_NDS32_LOADSTORE, /* type */
1160 2, /* size (0 = byte, 1 = short, 2 = long) */
1162 FALSE, /* pc_relative */
1164 complain_overflow_dont,/* complain_on_overflow */
1165 nds32_elf_ignore_reloc,/* special_function */
1166 "R_NDS32_LOADSTORE", /* name */
1167 FALSE, /* partial_inplace */
1168 0xffffffff, /* src_mask */
1169 0xffffffff, /* dst_mask */
1170 FALSE), /* pcrel_offset */
1172 /* Relax hint for load/store sequence. */
1173 HOWTO (R_NDS32_9_FIXED_RELA, /* type */
1175 1, /* size (0 = byte, 1 = short, 2 = long) */
1177 FALSE, /* pc_relative */
1179 complain_overflow_dont,/* complain_on_overflow */
1180 nds32_elf_ignore_reloc,/* special_function */
1181 "R_NDS32_9_FIXED_RELA",/* name */
1182 FALSE, /* partial_inplace */
1183 0x000000ff, /* src_mask */
1184 0x000000ff, /* dst_mask */
1185 FALSE), /* pcrel_offset */
1187 /* Relax hint for load/store sequence. */
1188 HOWTO (R_NDS32_15_FIXED_RELA, /* type */
1190 2, /* size (0 = byte, 1 = short, 2 = long) */
1192 FALSE, /* pc_relative */
1194 complain_overflow_dont,/* complain_on_overflow */
1195 nds32_elf_ignore_reloc,/* special_function */
1196 "R_NDS32_15_FIXED_RELA", /* name */
1197 FALSE, /* partial_inplace */
1198 0x00003fff, /* src_mask */
1199 0x00003fff, /* dst_mask */
1200 FALSE), /* pcrel_offset */
1202 /* Relax hint for load/store sequence. */
1203 HOWTO (R_NDS32_17_FIXED_RELA, /* type */
1205 2, /* size (0 = byte, 1 = short, 2 = long) */
1207 FALSE, /* pc_relative */
1209 complain_overflow_dont,/* complain_on_overflow */
1210 nds32_elf_ignore_reloc,/* special_function */
1211 "R_NDS32_17_FIXED_RELA", /* name */
1212 FALSE, /* partial_inplace */
1213 0x0000ffff, /* src_mask */
1214 0x0000ffff, /* dst_mask */
1215 FALSE), /* pcrel_offset */
1217 /* Relax hint for load/store sequence. */
1218 HOWTO (R_NDS32_25_FIXED_RELA, /* type */
1220 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 FALSE, /* pc_relative */
1224 complain_overflow_dont,/* complain_on_overflow */
1225 nds32_elf_ignore_reloc,/* special_function */
1226 "R_NDS32_25_FIXED_RELA", /* name */
1227 FALSE, /* partial_inplace */
1228 0x00ffffff, /* src_mask */
1229 0x00ffffff, /* dst_mask */
1230 FALSE), /* pcrel_offset */
1232 /* High 20 bits of PLT symbol offset relative to PC. */
1233 HOWTO (R_NDS32_PLTREL_HI20, /* type */
1234 12, /* rightshift */
1235 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 FALSE, /* pc_relative */
1239 complain_overflow_dont,/* complain_on_overflow */
1240 bfd_elf_generic_reloc, /* special_function */
1241 "R_NDS32_PLTREL_HI20", /* name */
1242 FALSE, /* partial_inplace */
1243 0x000fffff, /* src_mask */
1244 0x000fffff, /* dst_mask */
1245 FALSE), /* pcrel_offset */
1247 /* Low 12 bits of PLT symbol offset relative to PC. */
1248 HOWTO (R_NDS32_PLTREL_LO12, /* type */
1250 2, /* size (0 = byte, 1 = short, 2 = long) */
1252 FALSE, /* pc_relative */
1254 complain_overflow_dont,/* complain_on_overflow */
1255 bfd_elf_generic_reloc, /* special_function */
1256 "R_NDS32_PLTREL_LO12", /* name */
1257 FALSE, /* partial_inplace */
1258 0x00000fff, /* src_mask */
1259 0x00000fff, /* dst_mask */
1260 FALSE), /* pcrel_offset */
1262 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1263 HOWTO (R_NDS32_PLT_GOTREL_HI20, /* type */
1264 12, /* rightshift */
1265 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 FALSE, /* pc_relative */
1269 complain_overflow_dont,/* complain_on_overflow */
1270 bfd_elf_generic_reloc, /* special_function */
1271 "R_NDS32_PLT_GOTREL_HI20", /* name */
1272 FALSE, /* partial_inplace */
1273 0x000fffff, /* src_mask */
1274 0x000fffff, /* dst_mask */
1275 FALSE), /* pcrel_offset */
1277 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1278 HOWTO (R_NDS32_PLT_GOTREL_LO12, /* type */
1280 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 FALSE, /* pc_relative */
1284 complain_overflow_dont,/* complain_on_overflow */
1285 bfd_elf_generic_reloc, /* special_function */
1286 "R_NDS32_PLT_GOTREL_LO12", /* name */
1287 FALSE, /* partial_inplace */
1288 0x00000fff, /* src_mask */
1289 0x00000fff, /* dst_mask */
1290 FALSE), /* pcrel_offset */
1292 /* Small data area 12 bits offset. */
1293 HOWTO (R_NDS32_SDA12S2_DP_RELA, /* type */
1295 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 FALSE, /* pc_relative */
1299 complain_overflow_signed, /* complain_on_overflow */
1300 bfd_elf_generic_reloc, /* special_function */
1301 "R_NDS32_SDA12S2_DP_RELA", /* name */
1302 FALSE, /* partial_inplace */
1303 0x00000fff, /* src_mask */
1304 0x00000fff, /* dst_mask */
1305 FALSE), /* pcrel_offset */
1307 /* Small data area 12 bits offset. */
1308 HOWTO (R_NDS32_SDA12S2_SP_RELA, /* type */
1310 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 FALSE, /* pc_relative */
1314 complain_overflow_signed, /* complain_on_overflow */
1315 bfd_elf_generic_reloc, /* special_function */
1316 "R_NDS32_SDA12S2_SP_RELA", /* name */
1317 FALSE, /* partial_inplace */
1318 0x00000fff, /* src_mask */
1319 0x00000fff, /* dst_mask */
1320 FALSE), /* pcrel_offset */
1321 /* Lower 12 bits of address. */
1323 HOWTO (R_NDS32_LO12S2_DP_RELA, /* type */
1325 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 FALSE, /* pc_relative */
1329 complain_overflow_dont,/* complain_on_overflow */
1330 bfd_elf_generic_reloc, /* special_function */
1331 "R_NDS32_LO12S2_DP_RELA", /* name */
1332 FALSE, /* partial_inplace */
1333 0x000003ff, /* src_mask */
1334 0x000003ff, /* dst_mask */
1335 FALSE), /* pcrel_offset */
1337 /* Lower 12 bits of address. */
1338 HOWTO (R_NDS32_LO12S2_SP_RELA,/* type */
1340 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 FALSE, /* pc_relative */
1344 complain_overflow_dont,/* complain_on_overflow */
1345 bfd_elf_generic_reloc, /* special_function */
1346 "R_NDS32_LO12S2_SP_RELA", /* name */
1347 FALSE, /* partial_inplace */
1348 0x000003ff, /* src_mask */
1349 0x000003ff, /* dst_mask */
1350 FALSE), /* pcrel_offset */
1351 /* Lower 12 bits of address. Special identity for or case. */
1352 HOWTO (R_NDS32_LO12S0_ORI_RELA, /* type */
1354 2, /* size (0 = byte, 1 = short, 2 = long) */
1356 FALSE, /* pc_relative */
1358 complain_overflow_dont,/* complain_on_overflow */
1359 bfd_elf_generic_reloc, /* special_function */
1360 "R_NDS32_LO12S0_ORI_RELA", /* name */
1361 FALSE, /* partial_inplace */
1362 0x00000fff, /* src_mask */
1363 0x00000fff, /* dst_mask */
1364 FALSE), /* pcrel_offset */
1365 /* Small data area 19 bits offset. */
1366 HOWTO (R_NDS32_SDA16S3_RELA, /* type */
1368 2, /* size (0 = byte, 1 = short, 2 = long) */
1370 FALSE, /* pc_relative */
1372 complain_overflow_signed, /* complain_on_overflow */
1373 bfd_elf_generic_reloc, /* special_function */
1374 "R_NDS32_SDA16S3_RELA",/* name */
1375 FALSE, /* partial_inplace */
1376 0x0000ffff, /* src_mask */
1377 0x0000ffff, /* dst_mask */
1378 FALSE), /* pcrel_offset */
1380 /* Small data area 15 bits offset. */
1381 HOWTO (R_NDS32_SDA17S2_RELA, /* type */
1383 2, /* size (0 = byte, 1 = short, 2 = long) */
1385 FALSE, /* pc_relative */
1387 complain_overflow_signed, /* complain_on_overflow */
1388 bfd_elf_generic_reloc, /* special_function */
1389 "R_NDS32_SDA17S2_RELA",/* name */
1390 FALSE, /* partial_inplace */
1391 0x0001ffff, /* src_mask */
1392 0x0001ffff, /* dst_mask */
1393 FALSE), /* pcrel_offset */
1395 HOWTO (R_NDS32_SDA18S1_RELA, /* type */
1397 2, /* size (0 = byte, 1 = short, 2 = long) */
1399 FALSE, /* pc_relative */
1401 complain_overflow_signed, /* complain_on_overflow */
1402 bfd_elf_generic_reloc, /* special_function */
1403 "R_NDS32_SDA18S1_RELA",/* name */
1404 FALSE, /* partial_inplace */
1405 0x0003ffff, /* src_mask */
1406 0x0003ffff, /* dst_mask */
1407 FALSE), /* pcrel_offset */
1409 HOWTO (R_NDS32_SDA19S0_RELA, /* type */
1411 2, /* size (0 = byte, 1 = short, 2 = long) */
1413 FALSE, /* pc_relative */
1415 complain_overflow_signed, /* complain_on_overflow */
1416 bfd_elf_generic_reloc, /* special_function */
1417 "R_NDS32_SDA19S0_RELA",/* name */
1418 FALSE, /* partial_inplace */
1419 0x0007ffff, /* src_mask */
1420 0x0007ffff, /* dst_mask */
1421 FALSE), /* pcrel_offset */
1422 HOWTO (R_NDS32_DWARF2_OP1_RELA, /* type */
1424 0, /* size (0 = byte, 1 = short, 2 = long) */
1426 FALSE, /* pc_relative */
1428 complain_overflow_dont,/* complain_on_overflow */
1429 nds32_elf_ignore_reloc,/* special_function */
1430 "R_NDS32_DWARF2_OP1_RELA", /* name */
1431 FALSE, /* partial_inplace */
1432 0xff, /* src_mask */
1433 0xff, /* dst_mask */
1434 FALSE), /* pcrel_offset */
1435 HOWTO (R_NDS32_DWARF2_OP2_RELA, /* type */
1437 1, /* size (0 = byte, 1 = short, 2 = long) */
1439 FALSE, /* pc_relative */
1441 complain_overflow_dont,/* complain_on_overflow */
1442 nds32_elf_ignore_reloc,/* special_function */
1443 "R_NDS32_DWARF2_OP2_RELA", /* name */
1444 FALSE, /* partial_inplace */
1445 0xffff, /* src_mask */
1446 0xffff, /* dst_mask */
1447 FALSE), /* pcrel_offset */
1448 HOWTO (R_NDS32_DWARF2_LEB_RELA, /* type */
1450 2, /* size (0 = byte, 1 = short, 2 = long) */
1452 FALSE, /* pc_relative */
1454 complain_overflow_dont,/* complain_on_overflow */
1455 nds32_elf_ignore_reloc,/* special_function */
1456 "R_NDS32_DWARF2_LEB_RELA", /* name */
1457 FALSE, /* partial_inplace */
1458 0xffffffff, /* src_mask */
1459 0xffffffff, /* dst_mask */
1460 FALSE), /* pcrel_offset */
1461 HOWTO (R_NDS32_UPDATE_TA_RELA,/* type */
1463 1, /* size (0 = byte, 1 = short, 2 = long) */
1465 FALSE, /* pc_relative */
1467 complain_overflow_dont,/* complain_on_overflow */
1468 nds32_elf_ignore_reloc,/* special_function */
1469 "R_NDS32_UPDATE_TA_RELA", /* name */
1470 FALSE, /* partial_inplace */
1471 0xffff, /* src_mask */
1472 0xffff, /* dst_mask */
1473 FALSE), /* pcrel_offset */
1474 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1475 entry for the symbol. */
1476 HOWTO (R_NDS32_9_PLTREL, /* type */
1478 1, /* size (0 = byte, 1 = short, 2 = long) */
1480 TRUE, /* pc_relative */
1482 complain_overflow_signed, /* complain_on_overflow */
1483 bfd_elf_generic_reloc, /* special_function */
1484 "R_NDS32_9_PLTREL", /* name */
1485 FALSE, /* partial_inplace */
1486 0xff, /* src_mask */
1487 0xff, /* dst_mask */
1488 TRUE), /* pcrel_offset */
1489 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1490 HOWTO (R_NDS32_PLT_GOTREL_LO20, /* type */
1492 2, /* size (0 = byte, 1 = short, 2 = long) */
1494 FALSE, /* pc_relative */
1496 complain_overflow_dont,/* complain_on_overflow */
1497 bfd_elf_generic_reloc, /* special_function */
1498 "R_NDS32_PLT_GOTREL_LO20", /* name */
1499 FALSE, /* partial_inplace */
1500 0x000fffff, /* src_mask */
1501 0x000fffff, /* dst_mask */
1502 FALSE), /* pcrel_offset */
1503 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1504 HOWTO (R_NDS32_PLT_GOTREL_LO15, /* type */
1506 2, /* size (0 = byte, 1 = short, 2 = long) */
1508 FALSE, /* pc_relative */
1510 complain_overflow_dont,/* complain_on_overflow */
1511 bfd_elf_generic_reloc, /* special_function */
1512 "R_NDS32_PLT_GOTREL_LO15", /* name */
1513 FALSE, /* partial_inplace */
1514 0x00007fff, /* src_mask */
1515 0x00007fff, /* dst_mask */
1516 FALSE), /* pcrel_offset */
1517 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1518 HOWTO (R_NDS32_PLT_GOTREL_LO19, /* type */
1520 2, /* size (0 = byte, 1 = short, 2 = long) */
1522 FALSE, /* pc_relative */
1524 complain_overflow_dont,/* complain_on_overflow */
1525 bfd_elf_generic_reloc, /* special_function */
1526 "R_NDS32_PLT_GOTREL_LO19", /* name */
1527 FALSE, /* partial_inplace */
1528 0x0007ffff, /* src_mask */
1529 0x0007ffff, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1531 HOWTO (R_NDS32_GOT_LO15, /* type */
1533 2, /* size (0 = byte, 1 = short, 2 = long) */
1535 FALSE, /* pc_relative */
1537 complain_overflow_dont,/* complain_on_overflow */
1538 bfd_elf_generic_reloc, /* special_function */
1539 "R_NDS32_GOT_LO15", /* name */
1540 FALSE, /* partial_inplace */
1541 0x00007fff, /* src_mask */
1542 0x00007fff, /* dst_mask */
1543 FALSE), /* pcrel_offset */
1544 HOWTO (R_NDS32_GOT_LO19, /* type */
1546 2, /* size (0 = byte, 1 = short, 2 = long) */
1548 FALSE, /* pc_relative */
1550 complain_overflow_dont,/* complain_on_overflow */
1551 bfd_elf_generic_reloc, /* special_function */
1552 "R_NDS32_GOT_LO19", /* name */
1553 FALSE, /* partial_inplace */
1554 0x0007ffff, /* src_mask */
1555 0x0007ffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557 HOWTO (R_NDS32_GOTOFF_LO15, /* type */
1559 2, /* size (0 = byte, 1 = short, 2 = long) */
1561 FALSE, /* pc_relative */
1563 complain_overflow_dont,/* complain_on_overflow */
1564 bfd_elf_generic_reloc, /* special_function */
1565 "R_NDS32_GOTOFF_LO15", /* name */
1566 FALSE, /* partial_inplace */
1567 0x00007fff, /* src_mask */
1568 0x00007fff, /* dst_mask */
1569 FALSE), /* pcrel_offset */
1570 HOWTO (R_NDS32_GOTOFF_LO19, /* type */
1572 2, /* size (0 = byte, 1 = short, 2 = long) */
1574 FALSE, /* pc_relative */
1576 complain_overflow_dont,/* complain_on_overflow */
1577 bfd_elf_generic_reloc, /* special_function */
1578 "R_NDS32_GOTOFF_LO19", /* name */
1579 FALSE, /* partial_inplace */
1580 0x0007ffff, /* src_mask */
1581 0x0007ffff, /* dst_mask */
1582 FALSE), /* pcrel_offset */
1583 /* GOT 15 bits offset. */
1584 HOWTO (R_NDS32_GOT15S2_RELA, /* type */
1586 2, /* size (0 = byte, 1 = short, 2 = long) */
1588 FALSE, /* pc_relative */
1590 complain_overflow_signed, /* complain_on_overflow */
1591 bfd_elf_generic_reloc, /* special_function */
1592 "R_NDS32_GOT15S2_RELA",/* name */
1593 FALSE, /* partial_inplace */
1594 0x00007fff, /* src_mask */
1595 0x00007fff, /* dst_mask */
1596 FALSE), /* pcrel_offset */
1597 /* GOT 17 bits offset. */
1598 HOWTO (R_NDS32_GOT17S2_RELA, /* type */
1600 2, /* size (0 = byte, 1 = short, 2 = long) */
1602 FALSE, /* pc_relative */
1604 complain_overflow_signed, /* complain_on_overflow */
1605 bfd_elf_generic_reloc, /* special_function */
1606 "R_NDS32_GOT17S2_RELA",/* name */
1607 FALSE, /* partial_inplace */
1608 0x0001ffff, /* src_mask */
1609 0x0001ffff, /* dst_mask */
1610 FALSE), /* pcrel_offset */
1611 /* A 5 bit address. */
1612 HOWTO (R_NDS32_5_RELA, /* type */
1614 1, /* size (0 = byte, 1 = short, 2 = long) */
1616 FALSE, /* pc_relative */
1618 complain_overflow_signed, /* complain_on_overflow */
1619 bfd_elf_generic_reloc, /* special_function */
1620 "R_NDS32_5_RELA", /* name */
1621 FALSE, /* partial_inplace */
1622 0x1f, /* src_mask */
1623 0x1f, /* dst_mask */
1624 FALSE), /* pcrel_offset */
1625 HOWTO (R_NDS32_10_UPCREL_RELA,/* type */
1627 1, /* size (0 = byte, 1 = short, 2 = long) */
1629 TRUE, /* pc_relative */
1631 complain_overflow_unsigned, /* complain_on_overflow */
1632 bfd_elf_generic_reloc, /* special_function */
1633 "R_NDS32_10_UPCREL_RELA", /* name */
1634 FALSE, /* partial_inplace */
1635 0x1ff, /* src_mask */
1636 0x1ff, /* dst_mask */
1637 TRUE), /* pcrel_offset */
1638 HOWTO (R_NDS32_SDA_FP7U2_RELA,/* type */
1640 1, /* size (0 = byte, 1 = short, 2 = long) */
1642 FALSE, /* pc_relative */
1644 complain_overflow_unsigned, /* complain_on_overflow */
1645 bfd_elf_generic_reloc, /* special_function */
1646 "R_NDS32_SDA_FP7U2_RELA", /* name */
1647 FALSE, /* partial_inplace */
1648 0x0000007f, /* src_mask */
1649 0x0000007f, /* dst_mask */
1650 FALSE), /* pcrel_offset */
1651 HOWTO (R_NDS32_WORD_9_PCREL_RELA, /* type */
1653 2, /* size (0 = byte, 1 = short, 2 = long) */
1655 TRUE, /* pc_relative */
1657 complain_overflow_signed, /* complain_on_overflow */
1658 bfd_elf_generic_reloc, /* special_function */
1659 "R_NDS32_WORD_9_PCREL_RELA", /* name */
1660 FALSE, /* partial_inplace */
1661 0xff, /* src_mask */
1662 0xff, /* dst_mask */
1663 TRUE), /* pcrel_offset */
1664 HOWTO (R_NDS32_25_ABS_RELA, /* type */
1666 2, /* size (0 = byte, 1 = short, 2 = long) */
1668 FALSE, /* pc_relative */
1670 complain_overflow_dont,/* complain_on_overflow */
1671 bfd_elf_generic_reloc, /* special_function */
1672 "R_NDS32_25_ABS_RELA", /* name */
1673 FALSE, /* partial_inplace */
1674 0xffffff, /* src_mask */
1675 0xffffff, /* dst_mask */
1676 FALSE), /* pcrel_offset */
1678 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1679 HOWTO (R_NDS32_17IFC_PCREL_RELA, /* type */
1681 2, /* size (0 = byte, 1 = short, 2 = long) */
1683 TRUE, /* pc_relative */
1685 complain_overflow_signed, /* complain_on_overflow */
1686 bfd_elf_generic_reloc, /* special_function */
1687 "R_NDS32_17IFC_PCREL_RELA", /* name */
1688 FALSE, /* partial_inplace */
1689 0xffff, /* src_mask */
1690 0xffff, /* dst_mask */
1691 TRUE), /* pcrel_offset */
1693 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1694 HOWTO (R_NDS32_10IFCU_PCREL_RELA, /* type */
1696 1, /* size (0 = byte, 1 = short, 2 = long) */
1698 TRUE, /* pc_relative */
1700 complain_overflow_unsigned, /* complain_on_overflow */
1701 bfd_elf_generic_reloc, /* special_function */
1702 "R_NDS32_10IFCU_PCREL_RELA", /* name */
1703 FALSE, /* partial_inplace */
1704 0x1ff, /* src_mask */
1705 0x1ff, /* dst_mask */
1706 TRUE), /* pcrel_offset */
1709 /* Relocations used for relaxation. */
1710 static reloc_howto_type nds32_elf_relax_howto_table[] =
1712 HOWTO (R_NDS32_RELAX_ENTRY, /* type */
1714 2, /* size (0 = byte, 1 = short, 2 = long) */
1716 FALSE, /* pc_relative */
1718 complain_overflow_dont,/* complain_on_overflow */
1719 nds32_elf_ignore_reloc,/* special_function */
1720 "R_NDS32_RELAX_ENTRY", /* name */
1721 FALSE, /* partial_inplace */
1722 0xffffffff, /* src_mask */
1723 0xffffffff, /* dst_mask */
1724 FALSE), /* pcrel_offset */
1725 HOWTO (R_NDS32_GOT_SUFF, /* type */
1727 2, /* size (0 = byte, 1 = short, 2 = long) */
1729 FALSE, /* pc_relative */
1731 complain_overflow_dont,/* complain_on_overflow */
1732 nds32_elf_ignore_reloc,/* special_function */
1733 "R_NDS32_GOT_SUFF", /* name */
1734 FALSE, /* partial_inplace */
1735 0xffffffff, /* src_mask */
1736 0xffffffff, /* dst_mask */
1737 FALSE), /* pcrel_offset */
1738 HOWTO (R_NDS32_GOTOFF_SUFF, /* type */
1740 2, /* size (0 = byte, 1 = short, 2 = long) */
1742 FALSE, /* pc_relative */
1744 complain_overflow_bitfield, /* complain_on_overflow */
1745 nds32_elf_ignore_reloc,/* special_function */
1746 "R_NDS32_GOTOFF_SUFF", /* name */
1747 FALSE, /* partial_inplace */
1748 0xffffffff, /* src_mask */
1749 0xffffffff, /* dst_mask */
1750 FALSE), /* pcrel_offset */
1751 HOWTO (R_NDS32_PLT_GOT_SUFF, /* type */
1753 2, /* size (0 = byte, 1 = short, 2 = long) */
1755 FALSE, /* pc_relative */
1757 complain_overflow_dont,/* complain_on_overflow */
1758 nds32_elf_ignore_reloc,/* special_function */
1759 "R_NDS32_PLT_GOT_SUFF",/* name */
1760 FALSE, /* partial_inplace */
1761 0xffffffff, /* src_mask */
1762 0xffffffff, /* dst_mask */
1763 FALSE), /* pcrel_offset */
1764 HOWTO (R_NDS32_MULCALL_SUFF, /* type */
1766 2, /* size (0 = byte, 1 = short, 2 = long) */
1768 FALSE, /* pc_relative */
1770 complain_overflow_dont,/* complain_on_overflow */
1771 nds32_elf_ignore_reloc,/* special_function */
1772 "R_NDS32_MULCALL_SUFF",/* name */
1773 FALSE, /* partial_inplace */
1774 0xffffffff, /* src_mask */
1775 0xffffffff, /* dst_mask */
1776 FALSE), /* pcrel_offset */
1777 HOWTO (R_NDS32_PTR, /* type */
1779 2, /* size (0 = byte, 1 = short, 2 = long) */
1781 FALSE, /* pc_relative */
1783 complain_overflow_dont,/* complain_on_overflow */
1784 nds32_elf_ignore_reloc,/* special_function */
1785 "R_NDS32_PTR", /* name */
1786 FALSE, /* partial_inplace */
1787 0xffffffff, /* src_mask */
1788 0xffffffff, /* dst_mask */
1789 FALSE), /* pcrel_offset */
1790 HOWTO (R_NDS32_PTR_COUNT, /* type */
1792 2, /* size (0 = byte, 1 = short, 2 = long) */
1794 FALSE, /* pc_relative */
1796 complain_overflow_dont,/* complain_on_overflow */
1797 nds32_elf_ignore_reloc,/* special_function */
1798 "R_NDS32_PTR_COUNT", /* name */
1799 FALSE, /* partial_inplace */
1800 0xffffffff, /* src_mask */
1801 0xffffffff, /* dst_mask */
1802 FALSE), /* pcrel_offset */
1803 HOWTO (R_NDS32_PTR_RESOLVED, /* type */
1805 2, /* size (0 = byte, 1 = short, 2 = long) */
1807 FALSE, /* pc_relative */
1809 complain_overflow_dont,/* complain_on_overflow */
1810 nds32_elf_ignore_reloc,/* special_function */
1811 "R_NDS32_PTR_RESOLVED",/* name */
1812 FALSE, /* partial_inplace */
1813 0xffffffff, /* src_mask */
1814 0xffffffff, /* dst_mask */
1815 FALSE), /* pcrel_offset */
1816 HOWTO (R_NDS32_PLTBLOCK, /* type */
1818 2, /* size (0 = byte, 1 = short, 2 = long) */
1820 FALSE, /* pc_relative */
1822 complain_overflow_dont,/* complain_on_overflow */
1823 nds32_elf_ignore_reloc,/* special_function */
1824 "R_NDS32_PLTBLOCK", /* name */
1825 FALSE, /* partial_inplace */
1826 0xffffffff, /* src_mask */
1827 0xffffffff, /* dst_mask */
1828 FALSE), /* pcrel_offset */
1829 HOWTO (R_NDS32_RELAX_REGION_BEGIN, /* type */
1831 2, /* size (0 = byte, 1 = short, 2 = long) */
1833 FALSE, /* pc_relative */
1835 complain_overflow_dont,/* complain_on_overflow */
1836 nds32_elf_ignore_reloc,/* special_function */
1837 "R_NDS32_RELAX_REGION_BEGIN", /* name */
1838 FALSE, /* partial_inplace */
1839 0xffffffff, /* src_mask */
1840 0xffffffff, /* dst_mask */
1841 FALSE), /* pcrel_offset */
1842 HOWTO (R_NDS32_RELAX_REGION_END, /* type */
1844 2, /* size (0 = byte, 1 = short, 2 = long) */
1846 FALSE, /* pc_relative */
1848 complain_overflow_dont,/* complain_on_overflow */
1849 nds32_elf_ignore_reloc,/* special_function */
1850 "R_NDS32_RELAX_REGION_END", /* name */
1851 FALSE, /* partial_inplace */
1852 0xffffffff, /* src_mask */
1853 0xffffffff, /* dst_mask */
1854 FALSE), /* pcrel_offset */
1855 HOWTO (R_NDS32_MINUEND, /* type */
1857 2, /* size (0 = byte, 1 = short, 2 = long) */
1859 FALSE, /* pc_relative */
1861 complain_overflow_dont,/* complain_on_overflow */
1862 nds32_elf_ignore_reloc,/* special_function */
1863 "R_NDS32_MINUEND", /* name */
1864 FALSE, /* partial_inplace */
1865 0xffffffff, /* src_mask */
1866 0xffffffff, /* dst_mask */
1867 FALSE), /* pcrel_offset */
1868 HOWTO (R_NDS32_SUBTRAHEND, /* type */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1872 FALSE, /* pc_relative */
1874 complain_overflow_dont,/* complain_on_overflow */
1875 nds32_elf_ignore_reloc,/* special_function */
1876 "R_NDS32_SUBTRAHEND", /* name */
1877 FALSE, /* partial_inplace */
1878 0xffffffff, /* src_mask */
1879 0xffffffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1881 HOWTO (R_NDS32_DIFF8, /* type */
1883 0, /* size (0 = byte, 1 = short, 2 = long) */
1885 FALSE, /* pc_relative */
1887 complain_overflow_dont,/* complain_on_overflow */
1888 nds32_elf_ignore_reloc,/* special_function */
1889 "R_NDS32_DIFF8", /* name */
1890 FALSE, /* partial_inplace */
1891 0x000000ff, /* src_mask */
1892 0x000000ff, /* dst_mask */
1893 FALSE), /* pcrel_offset */
1894 HOWTO (R_NDS32_DIFF16, /* type */
1896 1, /* size (0 = byte, 1 = short, 2 = long) */
1898 FALSE, /* pc_relative */
1900 complain_overflow_dont,/* complain_on_overflow */
1901 nds32_elf_ignore_reloc,/* special_function */
1902 "R_NDS32_DIFF16", /* name */
1903 FALSE, /* partial_inplace */
1904 0x0000ffff, /* src_mask */
1905 0x0000ffff, /* dst_mask */
1906 FALSE), /* pcrel_offset */
1907 HOWTO (R_NDS32_DIFF32, /* type */
1909 2, /* size (0 = byte, 1 = short, 2 = long) */
1911 FALSE, /* pc_relative */
1913 complain_overflow_dont,/* complain_on_overflow */
1914 nds32_elf_ignore_reloc,/* special_function */
1915 "R_NDS32_DIFF32", /* name */
1916 FALSE, /* partial_inplace */
1917 0xffffffff, /* src_mask */
1918 0xffffffff, /* dst_mask */
1919 FALSE), /* pcrel_offset */
1920 HOWTO (R_NDS32_DIFF_ULEB128, /* type */
1922 0, /* size (0 = byte, 1 = short, 2 = long) */
1924 FALSE, /* pc_relative */
1926 complain_overflow_dont,/* complain_on_overflow */
1927 nds32_elf_ignore_reloc,/* special_function */
1928 "R_NDS32_DIFF_ULEB128",/* name */
1929 FALSE, /* partial_inplace */
1930 0xffffffff, /* src_mask */
1931 0xffffffff, /* dst_mask */
1932 FALSE), /* pcrel_offset */
1933 HOWTO (R_NDS32_DATA, /* type */
1935 2, /* size (0 = byte, 1 = short, 2 = long) */
1937 FALSE, /* pc_relative */
1939 complain_overflow_dont,/* complain_on_overflow */
1940 nds32_elf_ignore_reloc,/* special_function */
1941 "R_NDS32_DATA", /* name */
1942 FALSE, /* partial_inplace */
1943 0xffffffff, /* src_mask */
1944 0xffffffff, /* dst_mask */
1945 FALSE), /* pcrel_offset */
1946 HOWTO (R_NDS32_TRAN, /* type */
1948 2, /* size (0 = byte, 1 = short, 2 = long) */
1950 FALSE, /* pc_relative */
1952 complain_overflow_dont,/* complain_on_overflow */
1953 nds32_elf_ignore_reloc,/* special_function */
1954 "R_NDS32_TRAN", /* name */
1955 FALSE, /* partial_inplace */
1956 0xffffffff, /* src_mask */
1957 0xffffffff, /* dst_mask */
1958 FALSE), /* pcrel_offset */
1962 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
1963 This prototype is the same as qsort (). */
1966 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
1967 int (*compar) (const void *lhs, const void *rhs))
1969 char *ptr = (char *) base;
1971 char *tmp = alloca (size);
1973 /* If i is less than j, i is inserted before j.
1975 |---- j ----- i --------------|
1980 for (i = 1; i < nmemb; i++)
1982 for (j = 0; j < i; j++)
1983 if (compar (ptr + i * size, ptr + j * size) < 0)
1987 continue; /* j is in order. */
1989 memcpy (tmp, ptr + i * size, size);
1990 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
1991 memcpy (ptr + j * size, tmp, size);
1995 /* Sort relocation by r_offset.
1997 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
1998 algorithm. Relocations at the same r_offset must keep their order.
1999 For example, RELAX_ENTRY must be the very first relocation entry.
2001 Currently, this function implements insertion-sort.
2003 FIXME: If we already sort them in assembler, why bother sort them
2007 compar_reloc (const void *lhs, const void *rhs)
2009 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2010 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2012 if (l->r_offset > r->r_offset)
2014 else if (l->r_offset == r->r_offset)
2020 /* Functions listed below are only used for old relocs.
2021 * nds32_elf_9_pcrel_reloc
2022 * nds32_elf_do_9_pcrel_reloc
2023 * nds32_elf_hi20_reloc
2024 * nds32_elf_relocate_hi20
2025 * nds32_elf_lo12_reloc
2026 * nds32_elf_sda15_reloc
2027 * nds32_elf_generic_reloc
2030 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2032 static bfd_reloc_status_type
2033 nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2034 void *data, asection *input_section, bfd *output_bfd,
2035 char **error_message ATTRIBUTE_UNUSED)
2037 /* This part is from bfd_elf_generic_reloc. */
2038 if (output_bfd != (bfd *) NULL
2039 && (symbol->flags & BSF_SECTION_SYM) == 0
2040 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2042 reloc_entry->address += input_section->output_offset;
2043 return bfd_reloc_ok;
2046 if (output_bfd != NULL)
2048 /* FIXME: See bfd_perform_relocation. Is this right? */
2049 return bfd_reloc_continue;
2052 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2054 data, reloc_entry->address,
2057 + symbol->section->output_section->vma
2058 + symbol->section->output_offset),
2059 reloc_entry->addend);
2062 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2063 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2065 static bfd_reloc_status_type
2066 nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
2067 asection *input_section, bfd_byte *data,
2068 bfd_vma offset, asection *symbol_section ATTRIBUTE_UNUSED,
2069 bfd_vma symbol_value, bfd_vma addend)
2071 bfd_signed_vma relocation;
2073 bfd_reloc_status_type status;
2075 /* Sanity check the address (offset in section). */
2076 if (offset > bfd_get_section_limit (abfd, input_section))
2077 return bfd_reloc_outofrange;
2079 relocation = symbol_value + addend;
2080 /* Make it pc relative. */
2081 relocation -= (input_section->output_section->vma
2082 + input_section->output_offset);
2083 /* These jumps mask off the lower two bits of the current address
2084 before doing pcrel calculations. */
2085 relocation -= (offset & -(bfd_vma) 2);
2087 if (relocation < -0x100 || relocation > 0xff)
2088 status = bfd_reloc_overflow;
2090 status = bfd_reloc_ok;
2092 x = bfd_getb16 (data + offset);
2094 relocation >>= howto->rightshift;
2095 relocation <<= howto->bitpos;
2096 x = (x & ~howto->dst_mask)
2097 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2099 bfd_putb16 ((bfd_vma) x, data + offset);
2104 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2105 HI20_SLO is for the add3 and load/store with displacement instructions.
2106 HI20 is for the or3 instruction.
2107 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2108 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2109 we must add one to the high 16 bytes (which will get subtracted off when
2110 the low 16 bits are added).
2111 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2112 because there is a carry from the LO12 to the HI20. Here we just save
2113 the information we need; we do the actual relocation when we see the LO12.
2114 This code is copied from the elf32-mips.c. We also support an arbitrary
2115 number of HI20 relocs to be associated with a single LO12 reloc. The
2116 assembler sorts the relocs to ensure each HI20 immediately precedes its
2117 LO12. However if there are multiple copies, the assembler may not find
2118 the real LO12 so it picks the first one it finds. */
2122 struct nds32_hi20 *next;
2127 static struct nds32_hi20 *nds32_hi20_list;
2129 static bfd_reloc_status_type
2130 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2131 asymbol *symbol, void *data, asection *input_section,
2132 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2134 bfd_reloc_status_type ret;
2136 struct nds32_hi20 *n;
2138 /* This part is from bfd_elf_generic_reloc.
2139 If we're relocating, and this an external symbol, we don't want
2140 to change anything. */
2141 if (output_bfd != (bfd *) NULL
2142 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2144 reloc_entry->address += input_section->output_offset;
2145 return bfd_reloc_ok;
2148 /* Sanity check the address (offset in section). */
2149 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2150 return bfd_reloc_outofrange;
2153 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2154 ret = bfd_reloc_undefined;
2156 if (bfd_is_com_section (symbol->section))
2159 relocation = symbol->value;
2161 relocation += symbol->section->output_section->vma;
2162 relocation += symbol->section->output_offset;
2163 relocation += reloc_entry->addend;
2165 /* Save the information, and let LO12 do the actual relocation. */
2166 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2168 return bfd_reloc_outofrange;
2170 n->addr = (bfd_byte *) data + reloc_entry->address;
2171 n->addend = relocation;
2172 n->next = nds32_hi20_list;
2173 nds32_hi20_list = n;
2175 if (output_bfd != (bfd *) NULL)
2176 reloc_entry->address += input_section->output_offset;
2181 /* Handle an NDS32 ELF HI20 reloc. */
2184 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2185 int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
2186 Elf_Internal_Rela *rello, bfd_byte *contents,
2192 insn = bfd_getb32 (contents + relhi->r_offset);
2194 addlo = bfd_getb32 (contents + rello->r_offset);
2197 addend += ((insn & 0xfffff) << 20) + addlo;
2199 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2200 bfd_putb32 (insn, contents + relhi->r_offset);
2203 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2204 inplace relocation; this function exists in order to do the
2205 R_NDS32_HI20_[SU]LO relocation described above. */
2207 static bfd_reloc_status_type
2208 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2209 void *data, asection *input_section, bfd *output_bfd,
2210 char **error_message)
2212 /* This part is from bfd_elf_generic_reloc.
2213 If we're relocating, and this an external symbol, we don't want
2214 to change anything. */
2215 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2216 && reloc_entry->addend == 0)
2218 reloc_entry->address += input_section->output_offset;
2219 return bfd_reloc_ok;
2222 if (nds32_hi20_list != NULL)
2224 struct nds32_hi20 *l;
2226 l = nds32_hi20_list;
2231 unsigned long vallo;
2232 struct nds32_hi20 *next;
2234 /* Do the HI20 relocation. Note that we actually don't need
2235 to know anything about the LO12 itself, except where to
2236 find the low 12 bits of the addend needed by the LO12. */
2237 insn = bfd_getb32 (l->addr);
2238 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2240 switch (reloc_entry->howto->type)
2242 case R_NDS32_LO12S3:
2246 case R_NDS32_LO12S2:
2250 case R_NDS32_LO12S1:
2254 case R_NDS32_LO12S0:
2259 val = ((insn & 0xfffff) << 12) + vallo;
2262 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2263 bfd_putb32 ((bfd_vma) insn, l->addr);
2270 nds32_hi20_list = NULL;
2273 /* Now do the LO12 reloc in the usual way.
2274 ??? It would be nice to call bfd_elf_generic_reloc here,
2275 but we have partial_inplace set. bfd_elf_generic_reloc will
2276 pass the handling back to bfd_install_relocation which will install
2277 a section relative addend which is wrong. */
2278 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2279 input_section, output_bfd, error_message);
2282 /* Do generic partial_inplace relocation.
2283 This is a local replacement for bfd_elf_generic_reloc. */
2285 static bfd_reloc_status_type
2286 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2287 asymbol *symbol, void *data, asection *input_section,
2288 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2290 bfd_reloc_status_type ret;
2292 bfd_byte *inplace_address;
2294 /* This part is from bfd_elf_generic_reloc.
2295 If we're relocating, and this an external symbol, we don't want
2296 to change anything. */
2297 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2298 && reloc_entry->addend == 0)
2300 reloc_entry->address += input_section->output_offset;
2301 return bfd_reloc_ok;
2304 /* Now do the reloc in the usual way.
2305 ??? It would be nice to call bfd_elf_generic_reloc here,
2306 but we have partial_inplace set. bfd_elf_generic_reloc will
2307 pass the handling back to bfd_install_relocation which will install
2308 a section relative addend which is wrong. */
2310 /* Sanity check the address (offset in section). */
2311 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2312 return bfd_reloc_outofrange;
2315 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2316 ret = bfd_reloc_undefined;
2318 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2321 relocation = symbol->value;
2323 /* Only do this for a final link. */
2324 if (output_bfd == (bfd *) NULL)
2326 relocation += symbol->section->output_section->vma;
2327 relocation += symbol->section->output_offset;
2330 relocation += reloc_entry->addend;
2331 switch (reloc_entry->howto->type)
2333 case R_NDS32_LO12S3:
2337 case R_NDS32_LO12S2:
2341 case R_NDS32_LO12S1:
2345 case R_NDS32_LO12S0:
2351 inplace_address = (bfd_byte *) data + reloc_entry->address;
2354 x = ((x & ~reloc_entry->howto->dst_mask) | \
2355 (((x & reloc_entry->howto->src_mask) + relocation) & \
2356 reloc_entry->howto->dst_mask))
2358 switch (reloc_entry->howto->size)
2362 short x = bfd_getb16 (inplace_address);
2365 bfd_putb16 ((bfd_vma) x, inplace_address);
2370 unsigned long x = bfd_getb32 (inplace_address);
2373 bfd_putb32 ((bfd_vma) x, inplace_address);
2380 if (output_bfd != (bfd *) NULL)
2381 reloc_entry->address += input_section->output_offset;
2386 /* Handle the R_NDS32_SDA15 reloc.
2387 This reloc is used to compute the address of objects in the small data area
2388 and to perform loads and stores from that area.
2389 The lower 15 bits are sign extended and added to the register specified
2390 in the instruction, which is assumed to point to _SDA_BASE_.
2392 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2393 the access size, this must be taken care of. */
2395 static bfd_reloc_status_type
2396 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2397 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2398 asection *input_section, bfd *output_bfd,
2399 char **error_message ATTRIBUTE_UNUSED)
2401 /* This part is from bfd_elf_generic_reloc. */
2402 if (output_bfd != (bfd *) NULL
2403 && (symbol->flags & BSF_SECTION_SYM) == 0
2404 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2406 reloc_entry->address += input_section->output_offset;
2407 return bfd_reloc_ok;
2410 if (output_bfd != NULL)
2412 /* FIXME: See bfd_perform_relocation. Is this right? */
2413 return bfd_reloc_continue;
2416 /* FIXME: not sure what to do here yet. But then again, the linker
2417 may never call us. */
2421 /* nds32_elf_ignore_reloc is the special function for
2422 relocation types which don't need to be relocated
2423 like relaxation relocation types.
2424 This function simply return bfd_reloc_ok when it is
2427 static bfd_reloc_status_type
2428 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2429 asymbol *symbol ATTRIBUTE_UNUSED,
2430 void *data ATTRIBUTE_UNUSED, asection *input_section,
2431 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2433 if (output_bfd != NULL)
2434 reloc_entry->address += input_section->output_offset;
2436 return bfd_reloc_ok;
2440 /* Map BFD reloc types to NDS32 ELF reloc types. */
2442 struct nds32_reloc_map_entry
2444 bfd_reloc_code_real_type bfd_reloc_val;
2445 unsigned char elf_reloc_val;
2448 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
2450 {BFD_RELOC_NONE, R_NDS32_NONE},
2451 {BFD_RELOC_16, R_NDS32_16_RELA},
2452 {BFD_RELOC_32, R_NDS32_32_RELA},
2453 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
2454 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
2455 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
2456 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
2457 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
2458 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
2459 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
2460 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
2461 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
2462 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
2463 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
2464 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
2465 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
2466 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
2467 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
2468 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
2469 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
2470 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
2471 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
2472 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
2474 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
2475 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
2476 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
2477 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
2478 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
2479 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
2480 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
2481 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
2482 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
2483 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
2484 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
2485 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
2486 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
2487 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
2488 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
2489 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
2490 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
2491 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
2492 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
2493 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
2494 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
2495 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
2496 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
2497 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
2498 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
2499 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
2500 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
2501 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
2502 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
2503 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
2504 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
2505 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
2506 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
2507 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
2508 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
2509 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
2510 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
2511 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
2512 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
2513 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
2514 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
2515 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
2516 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
2517 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
2518 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
2519 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
2520 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
2521 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
2522 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
2523 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
2524 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
2525 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
2526 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
2527 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
2528 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
2529 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
2530 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
2531 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
2532 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
2533 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
2534 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
2535 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
2536 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
2537 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
2538 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
2539 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
2540 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
2542 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
2543 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
2544 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
2545 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
2546 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
2547 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
2548 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
2549 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
2550 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
2555 static reloc_howto_type *
2556 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2561 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
2562 if (nds32_elf_howto_table[i].name != NULL
2563 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
2564 return &nds32_elf_howto_table[i];
2566 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
2567 if (nds32_elf_relax_howto_table[i].name != NULL
2568 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
2569 return &nds32_elf_relax_howto_table[i];
2574 static reloc_howto_type *
2575 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
2577 if (code < R_NDS32_RELAX_ENTRY)
2579 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
2580 return &nds32_elf_howto_table[code];
2584 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
2585 < ARRAY_SIZE (nds32_elf_relax_howto_table));
2586 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
2590 static reloc_howto_type *
2591 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2592 bfd_reloc_code_real_type code)
2596 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
2598 if (nds32_reloc_map[i].bfd_reloc_val == code)
2599 return bfd_elf32_bfd_reloc_type_table_lookup
2600 (nds32_reloc_map[i].elf_reloc_val);
2606 /* Set the howto pointer for an NDS32 ELF reloc. */
2609 nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2610 Elf_Internal_Rela *dst)
2612 enum elf_nds32_reloc_type r_type;
2614 r_type = ELF32_R_TYPE (dst->r_info);
2615 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
2616 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
2620 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2621 Elf_Internal_Rela *dst)
2623 BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
2624 || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
2625 && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
2626 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
2629 /* Support for core dump NOTE sections.
2630 Reference to include/linux/elfcore.h in Linux. */
2633 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2638 switch (note->descsz)
2641 /* Linux/NDS32 32-bit, ABI1 */
2644 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2647 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
2655 /* Linux/NDS32 32-bit */
2658 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2661 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
2672 /* Make a ".reg" section. */
2673 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2674 size, note->descpos + offset);
2678 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2680 switch (note->descsz)
2685 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
2686 elf_tdata (abfd)->core->program =
2687 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
2688 elf_tdata (abfd)->core->command =
2689 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2695 /* Note that for some reason, a spurious space is tacked
2696 onto the end of the args in some (at least one anyway)
2697 implementations, so strip it off if it exists. */
2699 char *command = elf_tdata (abfd)->core->command;
2700 int n = strlen (command);
2702 if (0 < n && command[n - 1] == ' ')
2703 command[n - 1] = '\0';
2709 /* Hook called by the linker routine which adds symbols from an object
2710 file. We must handle the special NDS32 section numbers here.
2711 We also keep watching for whether we need to create the sdata special
2715 nds32_elf_add_symbol_hook (bfd *abfd,
2716 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2717 Elf_Internal_Sym *sym,
2718 const char **namep ATTRIBUTE_UNUSED,
2719 flagword *flagsp ATTRIBUTE_UNUSED,
2720 asection **secp, bfd_vma *valp)
2722 switch (sym->st_shndx)
2725 /* Common symbols less than the GP size are automatically
2726 treated as SHN_MIPS_SCOMMON symbols. */
2727 if (sym->st_size > elf_gp_size (abfd)
2728 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
2731 /* st_value is the alignemnt constraint.
2732 That might be its actual size if it is an array or structure. */
2733 switch (sym->st_value)
2736 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
2739 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
2742 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
2745 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
2751 (*secp)->flags |= SEC_IS_COMMON;
2752 *valp = sym->st_size;
2760 /* This function can figure out the best location for a base register to access
2761 data relative to this base register
2763 sda_d0: size of first DOUBLE WORD data section
2764 sda_w0: size of first WORD data section
2765 sda_h0: size of first HALF WORD data section
2766 sda_b : size of BYTE data section
2767 sda_hi: size of second HALF WORD data section
2768 sda_w1: size of second WORD data section
2769 sda_d1: size of second DOUBLE WORD data section
2771 offset (always positive) from the beginning of sda_d0 if OK
2772 a negative error value if fail
2774 these 7 sections have to be located back to back if exist
2775 a pass in 0 value for non-existing section */
2777 /* Due to the interpretation of simm15 field of load/store depending on
2778 data accessing size, the organization of base register relative data shall
2779 like the following figure
2780 -------------------------------------------
2781 | DOUBLE WORD sized data (range +/- 128K)
2782 -------------------------------------------
2783 | WORD sized data (range +/- 64K)
2784 -------------------------------------------
2785 | HALF WORD sized data (range +/- 32K)
2786 -------------------------------------------
2787 | BYTE sized data (range +/- 16K)
2788 -------------------------------------------
2789 | HALF WORD sized data (range +/- 32K)
2790 -------------------------------------------
2791 | WORD sized data (range +/- 64K)
2792 -------------------------------------------
2793 | DOUBLE WORD sized data (range +/- 128K)
2794 -------------------------------------------
2795 Its base register shall be set to access these data freely. */
2797 /* We have to figure out the SDA_BASE value, so that we can adjust the
2798 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
2799 BFD. If we can't find it, we're stuck. We cache it in the ELF
2800 target data. We don't need to adjust the symbol value for an
2801 external symbol if we are producing relocatable output. */
2803 static asection *sda_rela_sec = NULL;
2805 #define SDA_SECTION_NUM 11
2807 static bfd_reloc_status_type
2808 nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
2809 bfd_vma *psb, bfd_boolean add_symbol)
2812 struct elf_nds32_link_hash_table *table;
2813 struct bfd_link_hash_entry *h, *h2;
2815 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
2816 if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
2818 asection *first = NULL, *final = NULL, *temp;
2820 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
2821 4 byte-aligned. Therefore, it has to set the first section ".data"
2823 static const char sec_name[SDA_SECTION_NUM][10] =
2825 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
2826 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d", ".bss"
2830 if (output_bfd->sections == NULL)
2832 *psb = elf_gp (output_bfd);
2833 return bfd_reloc_ok;
2836 /* Get the first and final section. */
2837 while (i < sizeof (sec_name) / 10)
2839 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
2840 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
2842 if (temp && (temp->size != 0 || temp->rawsize != 0))
2849 /* The middle of data region. */
2850 sda_base = (final->vma + final->rawsize + first->vma) / 2;
2852 /* Find the section sda_base located. */
2854 while (i < sizeof (sec_name) / 10)
2856 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
2857 if (final && (final->size != 0 || final->rawsize != 0)
2858 && sda_base >= final->vma)
2869 /* There is not any data section in output bfd, and set _SDA_BASE_ in
2870 first output section. */
2871 first = output_bfd->sections;
2872 while (first && first->size == 0 && first->rawsize == 0)
2873 first = first->next;
2876 *psb = elf_gp (output_bfd);
2877 return bfd_reloc_ok;
2879 sda_base = first->vma;
2882 sda_base -= first->vma;
2883 sda_base = sda_base & (~7);
2885 if (!_bfd_generic_link_add_one_symbol
2886 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
2887 (bfd_vma) sda_base, (const char *) NULL, FALSE,
2888 get_elf_backend_data (output_bfd)->collect, &h))
2891 sda_rela_sec = first;
2893 table = nds32_elf_hash_table (info);
2894 relax_fp_as_gp = table->relax_fp_as_gp;
2897 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
2898 FALSE, FALSE, FALSE);
2899 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
2900 And set FP equal to SDA_BASE to do relaxation for
2901 la $fp, _FP_BASE_. */
2902 if (!_bfd_generic_link_add_one_symbol
2903 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
2904 first, (bfd_vma) sda_base, (const char *) NULL,
2905 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
2910 if (add_symbol == TRUE)
2915 elf_gp (output_bfd) = (h->u.def.value
2916 + h->u.def.section->output_section->vma
2917 + h->u.def.section->output_offset);
2921 (*_bfd_error_handler) (_("error: Can't find symbol: _SDA_BASE_."));
2922 return bfd_reloc_dangerous;
2926 *psb = h->u.def.value + h->u.def.section->output_section->vma
2927 + h->u.def.section->output_offset;
2928 return bfd_reloc_ok;
2932 /* Return size of a PLT entry. */
2933 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
2936 /* Create an entry in an nds32 ELF linker hash table. */
2938 static struct bfd_hash_entry *
2939 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2940 struct bfd_hash_table *table,
2943 struct elf_nds32_link_hash_entry *ret;
2945 ret = (struct elf_nds32_link_hash_entry *) entry;
2947 /* Allocate the structure if it has not already been allocated by a
2950 ret = (struct elf_nds32_link_hash_entry *)
2951 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
2954 return (struct bfd_hash_entry *) ret;
2956 /* Call the allocation method of the superclass. */
2957 ret = (struct elf_nds32_link_hash_entry *)
2958 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
2962 struct elf_nds32_link_hash_entry *eh;
2964 eh = (struct elf_nds32_link_hash_entry *) ret;
2965 eh->dyn_relocs = NULL;
2968 return (struct bfd_hash_entry *) ret;
2971 /* Create an nds32 ELF linker hash table. */
2973 static struct bfd_link_hash_table *
2974 nds32_elf_link_hash_table_create (bfd *abfd)
2976 struct elf_nds32_link_hash_table *ret;
2978 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
2980 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
2985 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2986 nds32_elf_link_hash_newfunc,
2987 sizeof (struct elf_nds32_link_hash_entry),
2995 ret->sgotplt = NULL;
2996 ret->srelgot = NULL;
2998 ret->srelplt = NULL;
2999 ret->sdynbss = NULL;
3000 ret->srelbss = NULL;
3001 ret->sym_ld_script = NULL;
3002 ret->ex9_export_file = NULL;
3003 ret->ex9_import_file = NULL;
3005 return &ret->root.root;
3008 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3009 shortcuts to them in our hash table. */
3012 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3014 struct elf_nds32_link_hash_table *htab;
3016 if (!_bfd_elf_create_got_section (dynobj, info))
3019 htab = nds32_elf_hash_table (info);
3020 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3021 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3022 if (!htab->sgot || !htab->sgotplt)
3025 /* _bfd_elf_create_got_section will create it for us. */
3026 htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3027 if (htab->srelgot == NULL
3028 || !bfd_set_section_flags (dynobj, htab->srelgot,
3029 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3030 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3032 || !bfd_set_section_alignment (dynobj, htab->srelgot, 2))
3038 /* Create dynamic sections when linking against a dynamic object. */
3041 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3043 struct elf_nds32_link_hash_table *htab;
3044 flagword flags, pltflags;
3045 register asection *s;
3046 const struct elf_backend_data *bed;
3047 int ptralign = 2; /* 32-bit */
3049 bed = get_elf_backend_data (abfd);
3051 htab = nds32_elf_hash_table (info);
3053 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3054 .rel[a].bss sections. */
3056 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3057 | SEC_LINKER_CREATED);
3060 pltflags |= SEC_CODE;
3061 if (bed->plt_not_loaded)
3062 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3063 if (bed->plt_readonly)
3064 pltflags |= SEC_READONLY;
3066 s = bfd_make_section (abfd, ".plt");
3069 || !bfd_set_section_flags (abfd, s, pltflags)
3070 || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3073 if (bed->want_plt_sym)
3075 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3077 struct bfd_link_hash_entry *bh = NULL;
3078 struct elf_link_hash_entry *h;
3080 if (!(_bfd_generic_link_add_one_symbol
3081 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3082 (bfd_vma) 0, (const char *) NULL, FALSE,
3083 get_elf_backend_data (abfd)->collect, &bh)))
3086 h = (struct elf_link_hash_entry *) bh;
3088 h->type = STT_OBJECT;
3090 if (info->shared && !bfd_elf_link_record_dynamic_symbol (info, h))
3094 s = bfd_make_section (abfd,
3095 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3098 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3099 || !bfd_set_section_alignment (abfd, s, ptralign))
3102 if (htab->sgot == NULL && !create_got_section (abfd, info))
3106 const char *secname;
3111 for (sec = abfd->sections; sec; sec = sec->next)
3113 secflags = bfd_get_section_flags (abfd, sec);
3114 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3115 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3117 secname = bfd_get_section_name (abfd, sec);
3118 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3119 strcpy (relname, ".rela");
3120 strcat (relname, secname);
3121 if (bfd_get_section_by_name (abfd, secname))
3123 s = bfd_make_section (abfd, relname);
3125 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3126 || !bfd_set_section_alignment (abfd, s, ptralign))
3131 if (bed->want_dynbss)
3133 /* The .dynbss section is a place to put symbols which are defined
3134 by dynamic objects, are referenced by regular objects, and are
3135 not functions. We must allocate space for them in the process
3136 image and use a R_*_COPY reloc to tell the dynamic linker to
3137 initialize them at run time. The linker script puts the .dynbss
3138 section into the .bss section of the final image. */
3139 s = bfd_make_section (abfd, ".dynbss");
3142 || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3144 /* The .rel[a].bss section holds copy relocs. This section is not
3145 normally needed. We need to create it here, though, so that the
3146 linker will map it to an output section. We can't just create it
3147 only if we need it, because we will not know whether we need it
3148 until we have seen all the input files, and the first time the
3149 main linker code calls BFD after examining all the input files
3150 (size_dynamic_sections) the input sections have already been
3151 mapped to the output sections. If the section turns out not to
3152 be needed, we can discard it later. We will never need this
3153 section when generating a shared object, since they do not use
3157 s = bfd_make_section (abfd, (bed->default_use_rela_p
3158 ? ".rela.bss" : ".rel.bss"));
3161 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3162 || !bfd_set_section_alignment (abfd, s, ptralign))
3170 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3172 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3173 struct elf_link_hash_entry *dir,
3174 struct elf_link_hash_entry *ind)
3176 struct elf_nds32_link_hash_entry *edir, *eind;
3178 edir = (struct elf_nds32_link_hash_entry *) dir;
3179 eind = (struct elf_nds32_link_hash_entry *) ind;
3181 if (eind->dyn_relocs != NULL)
3183 if (edir->dyn_relocs != NULL)
3185 struct elf_nds32_dyn_relocs **pp;
3186 struct elf_nds32_dyn_relocs *p;
3188 if (ind->root.type == bfd_link_hash_indirect)
3191 /* Add reloc counts against the weak sym to the strong sym
3192 list. Merge any entries against the same section. */
3193 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3195 struct elf_nds32_dyn_relocs *q;
3197 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3198 if (q->sec == p->sec)
3200 q->pc_count += p->pc_count;
3201 q->count += p->count;
3208 *pp = edir->dyn_relocs;
3211 edir->dyn_relocs = eind->dyn_relocs;
3212 eind->dyn_relocs = NULL;
3215 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3219 /* Adjust a symbol defined by a dynamic object and referenced by a
3220 regular object. The current definition is in some section of the
3221 dynamic object, but we're not including those sections. We have to
3222 change the definition to something the rest of the link can
3226 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3227 struct elf_link_hash_entry *h)
3229 struct elf_nds32_link_hash_table *htab;
3230 struct elf_nds32_link_hash_entry *eh;
3231 struct elf_nds32_dyn_relocs *p;
3234 unsigned int power_of_two;
3236 dynobj = elf_hash_table (info)->dynobj;
3238 /* Make sure we know what is going on here. */
3239 BFD_ASSERT (dynobj != NULL
3241 || h->u.weakdef != NULL
3242 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3245 /* If this is a function, put it in the procedure linkage table. We
3246 will fill in the contents of the procedure linkage table later,
3247 when we know the address of the .got section. */
3248 if (h->type == STT_FUNC || h->needs_plt)
3253 && h->root.type != bfd_link_hash_undefweak
3254 && h->root.type != bfd_link_hash_undefined)
3256 /* This case can occur if we saw a PLT reloc in an input
3257 file, but the symbol was never referred to by a dynamic
3258 object. In such a case, we don't actually need to build
3259 a procedure linkage table, and we can just do a PCREL
3261 h->plt.offset = (bfd_vma) - 1;
3268 h->plt.offset = (bfd_vma) - 1;
3270 /* If this is a weak symbol, and there is a real definition, the
3271 processor independent code will have arranged for us to see the
3272 real definition first, and we can just use the same value. */
3273 if (h->u.weakdef != NULL)
3275 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3276 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3277 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3278 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3282 /* This is a reference to a symbol defined by a dynamic object which
3283 is not a function. */
3285 /* If we are creating a shared library, we must presume that the
3286 only references to the symbol are via the global offset table.
3287 For such cases we need not do anything here; the relocations will
3288 be handled correctly by relocate_section. */
3292 /* If there are no references to this symbol that do not use the
3293 GOT, we don't need to generate a copy reloc. */
3294 if (!h->non_got_ref)
3297 /* If -z nocopyreloc was given, we won't generate them either. */
3298 if (info->nocopyreloc)
3304 eh = (struct elf_nds32_link_hash_entry *) h;
3305 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3307 s = p->sec->output_section;
3308 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3312 /* If we didn't find any dynamic relocs in sections which needs the
3313 copy reloc, then we'll be keeping the dynamic relocs and avoiding
3321 /* We must allocate the symbol in our .dynbss section, which will
3322 become part of the .bss section of the executable. There will be
3323 an entry for this symbol in the .dynsym section. The dynamic
3324 object will contain position independent code, so all references
3325 from the dynamic object to this symbol will go through the global
3326 offset table. The dynamic linker will use the .dynsym entry to
3327 determine the address it must put in the global offset table, so
3328 both the dynamic object and the regular object will refer to the
3329 same memory location for the variable. */
3331 htab = nds32_elf_hash_table (info);
3333 BFD_ASSERT (s != NULL);
3335 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3336 to copy the initial value out of the dynamic object and into the
3337 runtime process image. We need to remember the offset into the
3338 .rela.bss section we are going to use. */
3339 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3343 srel = htab->srelbss;
3344 BFD_ASSERT (srel != NULL);
3345 srel->size += sizeof (Elf32_External_Rela);
3349 /* We need to figure out the alignment required for this symbol. I
3350 have no idea how ELF linkers handle this. */
3351 power_of_two = bfd_log2 (h->size);
3352 if (power_of_two > 3)
3355 /* Apply the required alignment. */
3356 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3357 if (power_of_two > bfd_get_section_alignment (dynobj, s))
3359 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
3363 /* Define the symbol as being at this point in the section. */
3364 h->root.u.def.section = s;
3365 h->root.u.def.value = s->size;
3367 /* Increment the section size to make room for the symbol. */
3373 /* Allocate space in .plt, .got and associated reloc sections for
3377 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3379 struct bfd_link_info *info;
3380 struct elf_nds32_link_hash_table *htab;
3381 struct elf_nds32_link_hash_entry *eh;
3382 struct elf_nds32_dyn_relocs *p;
3384 if (h->root.type == bfd_link_hash_indirect)
3387 if (h->root.type == bfd_link_hash_warning)
3388 /* When warning symbols are created, they **replace** the "real"
3389 entry in the hash table, thus we never get to see the real
3390 symbol in a hash traversal. So look at it now. */
3391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3393 info = (struct bfd_link_info *) inf;
3394 htab = nds32_elf_hash_table (info);
3396 eh = (struct elf_nds32_link_hash_entry *) h;
3398 if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
3400 /* Make sure this symbol is output as a dynamic symbol.
3401 Undefined weak syms won't yet be marked as dynamic. */
3402 if (h->dynindx == -1 && !h->forced_local)
3404 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3408 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
3410 asection *s = htab->splt;
3412 /* If this is the first .plt entry, make room for the special
3415 s->size += PLT_ENTRY_SIZE;
3417 h->plt.offset = s->size;
3419 /* If this symbol is not defined in a regular file, and we are
3420 not generating a shared library, then set the symbol to this
3421 location in the .plt. This is required to make function
3422 pointers compare as equal between the normal executable and
3423 the shared library. */
3424 if (!info->shared && !h->def_regular)
3426 h->root.u.def.section = s;
3427 h->root.u.def.value = h->plt.offset;
3430 /* Make room for this entry. */
3431 s->size += PLT_ENTRY_SIZE;
3433 /* We also need to make an entry in the .got.plt section, which
3434 will be placed in the .got section by the linker script. */
3435 htab->sgotplt->size += 4;
3437 /* We also need to make an entry in the .rel.plt section. */
3438 htab->srelplt->size += sizeof (Elf32_External_Rela);
3442 h->plt.offset = (bfd_vma) - 1;
3448 h->plt.offset = (bfd_vma) - 1;
3452 if (h->got.refcount > 0)
3457 /* Make sure this symbol is output as a dynamic symbol.
3458 Undefined weak syms won't yet be marked as dynamic. */
3459 if (h->dynindx == -1 && !h->forced_local)
3461 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3467 h->got.offset = s->size;
3469 dyn = htab->root.dynamic_sections_created;
3470 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
3471 htab->srelgot->size += sizeof (Elf32_External_Rela);
3474 h->got.offset = (bfd_vma) - 1;
3476 if (eh->dyn_relocs == NULL)
3479 /* In the shared -Bsymbolic case, discard space allocated for
3480 dynamic pc-relative relocs against symbols which turn out to be
3481 defined in regular objects. For the normal shared case, discard
3482 space for pc-relative relocs that have become local due to symbol
3483 visibility changes. */
3487 if (h->def_regular && (h->forced_local || info->symbolic))
3489 struct elf_nds32_dyn_relocs **pp;
3491 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
3493 p->count -= p->pc_count;
3504 /* For the non-shared case, discard space for relocs against
3505 symbols which turn out to need copy relocs or are not dynamic. */
3510 || (htab->root.dynamic_sections_created
3511 && (h->root.type == bfd_link_hash_undefweak
3512 || h->root.type == bfd_link_hash_undefined))))
3514 /* Make sure this symbol is output as a dynamic symbol.
3515 Undefined weak syms won't yet be marked as dynamic. */
3516 if (h->dynindx == -1 && !h->forced_local)
3518 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3522 /* If that succeeded, we know we'll be keeping all the
3524 if (h->dynindx != -1)
3528 eh->dyn_relocs = NULL;
3533 /* Finally, allocate space. */
3534 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3536 asection *sreloc = elf_section_data (p->sec)->sreloc;
3537 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3543 /* Find any dynamic relocs that apply to read-only sections. */
3546 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3548 struct elf_nds32_link_hash_entry *eh;
3549 struct elf_nds32_dyn_relocs *p;
3551 if (h->root.type == bfd_link_hash_warning)
3552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3554 eh = (struct elf_nds32_link_hash_entry *) h;
3555 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3557 asection *s = p->sec->output_section;
3559 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3561 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3563 info->flags |= DF_TEXTREL;
3565 /* Not an error, just cut short the traversal. */
3572 /* Set the sizes of the dynamic sections. */
3575 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3576 struct bfd_link_info *info)
3578 struct elf_nds32_link_hash_table *htab;
3584 htab = nds32_elf_hash_table (info);
3585 dynobj = htab->root.dynobj;
3586 BFD_ASSERT (dynobj != NULL);
3588 if (htab->root.dynamic_sections_created)
3590 /* Set the contents of the .interp section to the interpreter. */
3593 s = bfd_get_section_by_name (dynobj, ".interp");
3594 BFD_ASSERT (s != NULL);
3595 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3596 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3600 /* Set up .got offsets for local syms, and space for local dynamic
3602 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3604 bfd_signed_vma *local_got;
3605 bfd_signed_vma *end_local_got;
3606 bfd_size_type locsymcount;
3607 Elf_Internal_Shdr *symtab_hdr;
3610 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3613 for (s = ibfd->sections; s != NULL; s = s->next)
3615 struct elf_nds32_dyn_relocs *p;
3617 for (p = ((struct elf_nds32_dyn_relocs *)
3618 elf_section_data (s)->local_dynrel);
3619 p != NULL; p = p->next)
3621 if (!bfd_is_abs_section (p->sec)
3622 && bfd_is_abs_section (p->sec->output_section))
3624 /* Input section has been discarded, either because
3625 it is a copy of a linkonce section or due to
3626 linker script /DISCARD/, so we'll be discarding
3629 else if (p->count != 0)
3631 srel = elf_section_data (p->sec)->sreloc;
3632 srel->size += p->count * sizeof (Elf32_External_Rela);
3633 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3634 info->flags |= DF_TEXTREL;
3639 local_got = elf_local_got_refcounts (ibfd);
3643 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3644 locsymcount = symtab_hdr->sh_info;
3645 end_local_got = local_got + locsymcount;
3647 srel = htab->srelgot;
3648 for (; local_got < end_local_got; ++local_got)
3652 *local_got = s->size;
3655 srel->size += sizeof (Elf32_External_Rela);
3658 *local_got = (bfd_vma) - 1;
3662 /* Allocate global sym .plt and .got entries, and space for global
3663 sym dynamic relocs. */
3664 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
3666 /* We now have determined the sizes of the various dynamic sections.
3667 Allocate memory for them. */
3669 for (s = dynobj->sections; s != NULL; s = s->next)
3671 if ((s->flags & SEC_LINKER_CREATED) == 0)
3674 if (s == htab->splt)
3676 /* Strip this section if we don't need it; see the
3679 else if (s == htab->sgot)
3681 got_size += s->size;
3683 else if (s == htab->sgotplt)
3685 got_size += s->size;
3687 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3689 if (s->size != 0 && s != htab->srelplt)
3692 /* We use the reloc_count field as a counter if we need
3693 to copy relocs into the output file. */
3698 /* It's not one of our sections, so don't allocate space. */
3704 /* If we don't need this section, strip it from the
3705 output file. This is mostly to handle .rela.bss and
3706 .rela.plt. We must create both sections in
3707 create_dynamic_sections, because they must be created
3708 before the linker maps input sections to output
3709 sections. The linker does that before
3710 adjust_dynamic_symbol is called, and it is that
3711 function which decides whether anything needs to go
3712 into these sections. */
3713 s->flags |= SEC_EXCLUDE;
3717 /* Allocate memory for the section contents. We use bfd_zalloc
3718 here in case unused entries are not reclaimed before the
3719 section's contents are written out. This should not happen,
3720 but this way if it does, we get a R_NDS32_NONE reloc instead
3722 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3723 if (s->contents == NULL)
3728 if (htab->root.dynamic_sections_created)
3730 /* Add some entries to the .dynamic section. We fill in the
3731 values later, in nds32_elf_finish_dynamic_sections, but we
3732 must add the entries now so that we get the correct size for
3733 the .dynamic section. The DT_DEBUG entry is filled in by the
3734 dynamic linker and used by the debugger. */
3735 #define add_dynamic_entry(TAG, VAL) \
3736 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3740 if (!add_dynamic_entry (DT_DEBUG, 0))
3744 if (htab->splt->size != 0)
3746 if (!add_dynamic_entry (DT_PLTGOT, 0)
3747 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3748 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3749 || !add_dynamic_entry (DT_JMPREL, 0))
3755 if (!add_dynamic_entry (DT_RELA, 0)
3756 || !add_dynamic_entry (DT_RELASZ, 0)
3757 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3760 /* If any dynamic relocs apply to a read-only section,
3761 then we need a DT_TEXTREL entry. */
3762 if ((info->flags & DF_TEXTREL) == 0)
3763 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
3766 if ((info->flags & DF_TEXTREL) != 0)
3768 if (!add_dynamic_entry (DT_TEXTREL, 0))
3773 #undef add_dynamic_entry
3778 static bfd_reloc_status_type
3779 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
3780 bfd_vma relocation, bfd_byte *location)
3784 bfd_reloc_status_type flag;
3785 unsigned int rightshift = howto->rightshift;
3786 unsigned int bitpos = howto->bitpos;
3788 /* If the size is negative, negate RELOCATION. This isn't very
3790 if (howto->size < 0)
3791 relocation = -relocation;
3793 /* Get the value we are going to relocate. */
3794 size = bfd_get_reloc_size (howto);
3804 x = bfd_getb16 (location);
3807 x = bfd_getb32 (location);
3811 /* Check for overflow. FIXME: We may drop bits during the addition
3812 which we don't check for. We must either check at every single
3813 operation, which would be tedious, or we must do the computations
3814 in a type larger than bfd_vma, which would be inefficient. */
3815 flag = bfd_reloc_ok;
3816 if (howto->complain_on_overflow != complain_overflow_dont)
3818 bfd_vma addrmask, fieldmask, signmask, ss;
3821 /* Get the values to be added together. For signed and unsigned
3822 relocations, we assume that all values should be truncated to
3823 the size of an address. For bitfields, all the bits matter.
3824 See also bfd_check_overflow. */
3825 fieldmask = N_ONES (howto->bitsize);
3826 signmask = ~fieldmask;
3827 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3828 a = (relocation & addrmask) >> rightshift;
3829 b = (x & howto->src_mask & addrmask) >> bitpos;
3831 switch (howto->complain_on_overflow)
3833 case complain_overflow_signed:
3834 /* If any sign bits are set, all sign bits must be set.
3835 That is, A must be a valid negative address after
3837 signmask = ~(fieldmask >> 1);
3840 case complain_overflow_bitfield:
3841 /* Much like the signed check, but for a field one bit
3842 wider. We allow a bitfield to represent numbers in the
3843 range -2**n to 2**n-1, where n is the number of bits in the
3844 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
3845 can't overflow, which is exactly what we want. */
3847 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
3848 flag = bfd_reloc_overflow;
3850 /* We only need this next bit of code if the sign bit of B
3851 is below the sign bit of A. This would only happen if
3852 SRC_MASK had fewer bits than BITSIZE. Note that if
3853 SRC_MASK has more bits than BITSIZE, we can get into
3854 trouble; we would need to verify that B is in range, as
3855 we do for A above. */
3856 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
3859 /* Set all the bits above the sign bit. */
3862 /* Now we can do the addition. */
3865 /* See if the result has the correct sign. Bits above the
3866 sign bit are junk now; ignore them. If the sum is
3867 positive, make sure we did not have all negative inputs;
3868 if the sum is negative, make sure we did not have all
3869 positive inputs. The test below looks only at the sign
3870 bits, and it really just
3871 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3873 We mask with addrmask here to explicitly allow an address
3874 wrap-around. The Linux kernel relies on it, and it is
3875 the only way to write assembler code which can run when
3876 loaded at a location 0x80000000 away from the location at
3877 which it is linked. */
3878 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
3879 flag = bfd_reloc_overflow;
3883 case complain_overflow_unsigned:
3884 /* Checking for an unsigned overflow is relatively easy:
3885 trim the addresses and add, and trim the result as well.
3886 Overflow is normally indicated when the result does not
3887 fit in the field. However, we also need to consider the
3888 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3889 input is 0x80000000, and bfd_vma is only 32 bits; then we
3890 will get sum == 0, but there is an overflow, since the
3891 inputs did not fit in the field. Instead of doing a
3892 separate test, we can check for this by or-ing in the
3893 operands when testing for the sum overflowing its final
3895 sum = (a + b) & addrmask;
3896 if ((a | b | sum) & signmask)
3897 flag = bfd_reloc_overflow;
3905 /* Put RELOCATION in the right bits. */
3906 relocation >>= (bfd_vma) rightshift;
3907 relocation <<= (bfd_vma) bitpos;
3909 /* Add RELOCATION to the right bits of X. */
3911 Because the relaxation may generate duplicate relocation at one address,
3912 an addition to immediate in the instruction may cause the relocation added
3914 This bug should be fixed in assembler, but a check is also needed here. */
3915 if (howto->partial_inplace)
3916 x = ((x & ~howto->dst_mask)
3917 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
3919 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
3922 /* Put the relocated value back in the object file. */
3932 bfd_putb16 (x, location);
3935 bfd_putb32 (x, location);
3942 static bfd_reloc_status_type
3943 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
3944 asection *input_section, bfd_byte *contents,
3945 bfd_vma address, bfd_vma value, bfd_vma addend)
3949 /* Sanity check the address. */
3950 if (address > bfd_get_section_limit (input_bfd, input_section))
3951 return bfd_reloc_outofrange;
3953 /* This function assumes that we are dealing with a basic relocation
3954 against a symbol. We want to compute the value of the symbol to
3955 relocate to. This is just VALUE, the value of the symbol, plus
3956 ADDEND, any addend associated with the reloc. */
3957 relocation = value + addend;
3959 /* If the relocation is PC relative, we want to set RELOCATION to
3960 the distance between the symbol (currently in RELOCATION) and the
3961 location we are relocating. Some targets (e.g., i386-aout)
3962 arrange for the contents of the section to be the negative of the
3963 offset of the location within the section; for such targets
3964 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
3965 simply leave the contents of the section as zero; for such
3966 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
3967 need to subtract out the offset of the location within the
3968 section (which is just ADDRESS). */
3969 if (howto->pc_relative)
3971 relocation -= (input_section->output_section->vma
3972 + input_section->output_offset);
3973 if (howto->pcrel_offset)
3974 relocation -= address;
3977 return nds32_relocate_contents (howto, input_bfd, relocation,
3978 contents + address);
3982 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
3984 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
3985 asection *input_sec,
3986 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
3989 FILE *sym_ld_script = NULL;
3990 struct elf_nds32_link_hash_table *table;
3992 table = nds32_elf_hash_table (info);
3993 sym_ld_script = table->sym_ld_script;
3997 if (!h || !name || *name == '\0')
4000 if (input_sec->flags & SEC_EXCLUDE)
4003 if (!check_start_export_sym)
4005 fprintf (sym_ld_script, "SECTIONS\n{\n");
4006 check_start_export_sym = 1;
4009 if (h->root.type == bfd_link_hash_defined
4010 || h->root.type == bfd_link_hash_defweak)
4012 if (!h->root.u.def.section->output_section)
4015 if (bfd_is_const_section (input_sec))
4016 source = input_sec->name;
4018 source = input_sec->owner->filename;
4020 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4021 h->root.root.string,
4022 (long) (h->root.u.def.value
4023 + h->root.u.def.section->output_section->vma
4024 + h->root.u.def.section->output_offset), source);
4030 /* Relocate an NDS32/D ELF section.
4031 There is some attempt to make this function usable for many architectures,
4032 both for RELA and REL type relocs, if only to serve as a learning tool.
4034 The RELOCATE_SECTION function is called by the new ELF backend linker
4035 to handle the relocations for a section.
4037 The relocs are always passed as Rela structures; if the section
4038 actually uses Rel structures, the r_addend field will always be
4041 This function is responsible for adjust the section contents as
4042 necessary, and (if using Rela relocs and generating a
4043 relocatable output file) adjusting the reloc addend as
4046 This function does not have to worry about setting the reloc
4047 address or the reloc symbol index.
4049 LOCAL_SYMS is a pointer to the swapped in local symbols.
4051 LOCAL_SECTIONS is an array giving the section in the input file
4052 corresponding to the st_shndx field of each local symbol.
4054 The global hash table entry for the global symbols can be found
4055 via elf_sym_hashes (input_bfd).
4057 When generating relocatable output, this function must handle
4058 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4059 going to be the section symbol corresponding to the output
4060 section, which means that the addend must be adjusted
4064 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
4065 struct bfd_link_info * info,
4067 asection * input_section,
4068 bfd_byte * contents,
4069 Elf_Internal_Rela * relocs,
4070 Elf_Internal_Sym * local_syms,
4071 asection ** local_sections)
4073 Elf_Internal_Shdr *symtab_hdr;
4074 struct elf_link_hash_entry **sym_hashes;
4075 Elf_Internal_Rela *rel, *relend;
4076 bfd_boolean ret = TRUE; /* Assume success. */
4078 bfd_reloc_status_type r;
4079 const char *errmsg = NULL;
4081 struct elf_nds32_link_hash_table *htab;
4083 bfd_vma *local_got_offsets;
4084 asection *sgot, *splt, *sreloc;
4085 bfd_vma high_address;
4086 struct elf_nds32_link_hash_table *table;
4087 int eliminate_gc_relocs;
4088 bfd_vma fpbase_addr;
4090 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4091 sym_hashes = elf_sym_hashes (input_bfd);
4092 htab = nds32_elf_hash_table (info);
4093 high_address = bfd_get_section_limit (input_bfd, input_section);
4095 dynobj = htab->root.dynobj;
4096 local_got_offsets = elf_local_got_offsets (input_bfd);
4103 relend = relocs + input_section->reloc_count;
4105 table = nds32_elf_hash_table (info);
4106 eliminate_gc_relocs = table->eliminate_gc_relocs;
4107 /* By this time, we can adjust the value of _SDA_BASE_. */
4108 if ((!info->relocatable))
4110 is_SDA_BASE_set = 1;
4111 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
4112 if (r != bfd_reloc_ok)
4116 /* Use gp as fp to prevent truncated fit. Because in relaxation time
4117 the fp value is set as gp, and it has be reverted for instruction
4119 fpbase_addr = elf_gp (output_bfd);
4121 for (rel = relocs; rel < relend; rel++)
4123 enum elf_nds32_reloc_type r_type;
4124 reloc_howto_type *howto = NULL;
4125 unsigned long r_symndx;
4126 struct elf_link_hash_entry *h = NULL;
4127 Elf_Internal_Sym *sym = NULL;
4131 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
4132 ensure it's zero (we use REL relocs, not RELA). Therefore this
4133 should be assigning zero to `addend', but for clarity we use
4136 bfd_vma addend = rel->r_addend;
4137 bfd_vma offset = rel->r_offset;
4139 r_type = ELF32_R_TYPE (rel->r_info);
4140 if (r_type >= R_NDS32_max)
4142 (*_bfd_error_handler) (_("%B: error: unknown relocation type %d."),
4144 bfd_set_error (bfd_error_bad_value);
4149 if (r_type == R_NDS32_GNU_VTENTRY
4150 || r_type == R_NDS32_GNU_VTINHERIT
4151 || r_type == R_NDS32_NONE
4152 || r_type == R_NDS32_RELA_GNU_VTENTRY
4153 || r_type == R_NDS32_RELA_GNU_VTINHERIT
4154 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
4155 || r_type == R_NDS32_DATA
4156 || r_type == R_NDS32_TRAN)
4159 /* If we enter the fp-as-gp region. Resolve the address of best fp-base. */
4160 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
4161 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4165 /* Distance to relocation of best fp-base is encoded in R_SYM. */
4166 dist = rel->r_addend >> 16;
4167 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
4168 local_syms, symtab_hdr);
4170 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
4171 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4173 fpbase_addr = elf_gp (output_bfd);
4176 if (((r_type >= R_NDS32_DWARF2_OP1_RELA
4177 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4178 || r_type >= R_NDS32_RELAX_ENTRY) && !info->relocatable)
4181 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
4182 r_symndx = ELF32_R_SYM (rel->r_info);
4184 /* This is a final link. */
4189 if (r_symndx < symtab_hdr->sh_info)
4192 sym = local_syms + r_symndx;
4193 sec = local_sections[r_symndx];
4195 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4196 addend = rel->r_addend;
4200 /* External symbol. */
4201 bfd_boolean warned, ignored, unresolved_reloc;
4202 int symndx = r_symndx - symtab_hdr->sh_info;
4204 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4205 r_symndx, symtab_hdr, sym_hashes, h, sec,
4206 relocation, unresolved_reloc, warned,
4209 /* la $fp, _FP_BASE_ is per-function (region).
4210 Handle it specially. */
4211 switch ((int) r_type)
4213 case R_NDS32_SDA19S0_RELA:
4214 case R_NDS32_SDA15S0_RELA:
4215 case R_NDS32_20_RELA:
4216 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
4219 relocation = fpbase_addr;
4226 if (info->relocatable)
4228 /* This is a relocatable link. We don't have to change
4229 anything, unless the reloc is against a section symbol,
4230 in which case we have to adjust according to where the
4231 section symbol winds up in the output section. */
4232 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4233 rel->r_addend += sec->output_offset + sym->st_value;
4238 /* Sanity check the address. */
4239 if (offset > high_address)
4241 r = bfd_reloc_outofrange;
4245 if ((r_type >= R_NDS32_DWARF2_OP1_RELA
4246 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4247 || r_type >= R_NDS32_RELAX_ENTRY)
4250 switch ((int) r_type)
4252 case R_NDS32_GOTOFF:
4253 /* Relocation is relative to the start of the global offset
4254 table (for ld24 rx, #uimm24), e.g. access at label+addend
4256 ld24 rx. #label@GOTOFF + addend
4258 case R_NDS32_GOTOFF_HI20:
4259 case R_NDS32_GOTOFF_LO12:
4260 case R_NDS32_GOTOFF_LO15:
4261 case R_NDS32_GOTOFF_LO19:
4262 BFD_ASSERT (sgot != NULL);
4264 relocation -= elf_gp (output_bfd);
4267 case R_NDS32_9_PLTREL:
4268 case R_NDS32_25_PLTREL:
4269 /* Relocation is to the entry for this symbol in the
4270 procedure linkage table. */
4272 /* The native assembler will generate a 25_PLTREL reloc
4273 for a local symbol if you assemble a call from one
4274 section to another when using -K pic. */
4278 if (h->forced_local)
4281 /* We didn't make a PLT entry for this symbol. This
4282 happens when statically linking PIC code, or when
4283 using -Bsymbolic. */
4284 if (h->plt.offset == (bfd_vma) - 1)
4287 relocation = (splt->output_section->vma
4288 + splt->output_offset + h->plt.offset);
4291 case R_NDS32_PLT_GOTREL_HI20:
4292 case R_NDS32_PLT_GOTREL_LO12:
4293 case R_NDS32_PLT_GOTREL_LO15:
4294 case R_NDS32_PLT_GOTREL_LO19:
4295 case R_NDS32_PLT_GOTREL_LO20:
4296 if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
4298 /* We didn't make a PLT entry for this symbol. This
4299 happens when statically linking PIC code, or when
4300 using -Bsymbolic. */
4301 relocation -= elf_gp (output_bfd);
4305 relocation = (splt->output_section->vma
4306 + splt->output_offset + h->plt.offset);
4308 relocation -= elf_gp (output_bfd);
4311 case R_NDS32_PLTREL_HI20:
4312 case R_NDS32_PLTREL_LO12:
4314 /* Relocation is to the entry for this symbol in the
4315 procedure linkage table. */
4317 /* The native assembler will generate a 25_PLTREL reloc
4318 for a local symbol if you assemble a call from one
4319 section to another when using -K pic. */
4323 if (h->forced_local)
4326 if (h->plt.offset == (bfd_vma) - 1)
4327 /* We didn't make a PLT entry for this symbol. This
4328 happens when statically linking PIC code, or when
4329 using -Bsymbolic. */
4335 relocation = (splt->output_section->vma
4336 + splt->output_offset
4337 + h->plt.offset + 4)
4338 - (input_section->output_section->vma
4339 + input_section->output_offset
4344 case R_NDS32_GOTPC20:
4345 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4346 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
4347 relocation = elf_gp (output_bfd);
4350 case R_NDS32_GOTPC_HI20:
4351 case R_NDS32_GOTPC_LO12:
4353 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4355 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
4356 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4359 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
4360 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4362 relocation = elf_gp (output_bfd);
4363 relocation -= (input_section->output_section->vma
4364 + input_section->output_offset + rel->r_offset);
4370 case R_NDS32_GOT_HI20:
4371 case R_NDS32_GOT_LO12:
4372 case R_NDS32_GOT_LO15:
4373 case R_NDS32_GOT_LO19:
4374 /* Relocation is to the entry for this symbol in the global
4376 BFD_ASSERT (sgot != NULL);
4383 off = h->got.offset;
4384 BFD_ASSERT (off != (bfd_vma) - 1);
4385 dyn = htab->root.dynamic_sections_created;
4386 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4390 || h->forced_local) && h->def_regular))
4392 /* This is actually a static link, or it is a
4393 -Bsymbolic link and the symbol is defined
4394 locally, or the symbol was forced to be local
4395 because of a version file. We must initialize
4396 this entry in the global offset table. Since the
4397 offset must always be a multiple of 4, we use the
4398 least significant bit to record whether we have
4399 initialized it already.
4401 When doing a dynamic link, we create a .rela.got
4402 relocation entry to initialize the value. This
4403 is done in the finish_dynamic_symbol routine. */
4408 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4412 relocation = sgot->output_section->vma + sgot->output_offset + off
4413 - elf_gp (output_bfd);
4420 BFD_ASSERT (local_got_offsets != NULL
4421 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4423 off = local_got_offsets[r_symndx];
4425 /* The offset must always be a multiple of 4. We use
4426 the least significant bit to record whether we have
4427 already processed this entry. */
4432 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4437 Elf_Internal_Rela outrel;
4439 /* We need to generate a R_NDS32_RELATIVE reloc
4440 for the dynamic linker. */
4441 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4442 BFD_ASSERT (srelgot != NULL);
4444 outrel.r_offset = (elf_gp (output_bfd)
4445 + sgot->output_offset + off);
4446 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4447 outrel.r_addend = relocation;
4448 loc = srelgot->contents;
4450 srelgot->reloc_count * sizeof (Elf32_External_Rela);
4451 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4452 ++srelgot->reloc_count;
4454 local_got_offsets[r_symndx] |= 1;
4456 relocation = sgot->output_section->vma + sgot->output_offset + off
4457 - elf_gp (output_bfd);
4462 case R_NDS32_16_RELA:
4463 case R_NDS32_20_RELA:
4464 case R_NDS32_5_RELA:
4465 case R_NDS32_32_RELA:
4466 case R_NDS32_9_PCREL_RELA:
4467 case R_NDS32_WORD_9_PCREL_RELA:
4468 case R_NDS32_10_UPCREL_RELA:
4469 case R_NDS32_15_PCREL_RELA:
4470 case R_NDS32_17_PCREL_RELA:
4471 case R_NDS32_25_PCREL_RELA:
4472 case R_NDS32_HI20_RELA:
4473 case R_NDS32_LO12S3_RELA:
4474 case R_NDS32_LO12S2_RELA:
4475 case R_NDS32_LO12S2_DP_RELA:
4476 case R_NDS32_LO12S2_SP_RELA:
4477 case R_NDS32_LO12S1_RELA:
4478 case R_NDS32_LO12S0_RELA:
4479 case R_NDS32_LO12S0_ORI_RELA:
4480 if (info->shared && r_symndx != 0
4481 && (input_section->flags & SEC_ALLOC) != 0
4482 && (eliminate_gc_relocs == 0
4483 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
4484 && ((r_type != R_NDS32_9_PCREL_RELA
4485 && r_type != R_NDS32_WORD_9_PCREL_RELA
4486 && r_type != R_NDS32_10_UPCREL_RELA
4487 && r_type != R_NDS32_15_PCREL_RELA
4488 && r_type != R_NDS32_17_PCREL_RELA
4489 && r_type != R_NDS32_25_PCREL_RELA
4490 && !(r_type == R_NDS32_32_RELA
4491 && strcmp (input_section->name, ".eh_frame") == 0))
4492 || (h != NULL && h->dynindx != -1
4493 && (!info->symbolic || !h->def_regular))))
4495 Elf_Internal_Rela outrel;
4496 bfd_boolean skip, relocate;
4499 /* When generating a shared object, these relocations
4500 are copied into the output file to be resolved at run
4507 name = bfd_elf_string_from_elf_section
4508 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
4509 elf_section_data (input_section)->rela.hdr->sh_name);
4513 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4514 && strcmp (bfd_get_section_name (input_bfd,
4518 sreloc = bfd_get_section_by_name (dynobj, name);
4519 BFD_ASSERT (sreloc != NULL);
4525 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4529 if (outrel.r_offset == (bfd_vma) - 1)
4531 else if (outrel.r_offset == (bfd_vma) - 2)
4532 skip = TRUE, relocate = TRUE;
4533 outrel.r_offset += (input_section->output_section->vma
4534 + input_section->output_offset);
4537 memset (&outrel, 0, sizeof outrel);
4538 else if (r_type == R_NDS32_17_PCREL_RELA
4539 || r_type == R_NDS32_15_PCREL_RELA
4540 || r_type == R_NDS32_25_PCREL_RELA)
4542 BFD_ASSERT (h != NULL && h->dynindx != -1);
4543 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4544 outrel.r_addend = rel->r_addend;
4548 /* h->dynindx may be -1 if this symbol was marked to
4551 || ((info->symbolic || h->dynindx == -1)
4555 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4556 outrel.r_addend = relocation + rel->r_addend;
4560 BFD_ASSERT (h->dynindx != -1);
4561 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4562 outrel.r_addend = rel->r_addend;
4566 loc = sreloc->contents;
4567 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
4568 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4569 ++sreloc->reloc_count;
4571 /* If this reloc is against an external symbol, we do
4572 not want to fiddle with the addend. Otherwise, we
4573 need to include the symbol value so that it becomes
4574 an addend for the dynamic reloc. */
4580 case R_NDS32_25_ABS_RELA:
4583 (*_bfd_error_handler)
4584 (_("%s: warning: cannot deal R_NDS32_25_ABS_RELA in shared mode."),
4585 bfd_get_filename (input_bfd));
4590 case R_NDS32_9_PCREL:
4591 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
4593 sec, relocation, addend);
4598 Elf_Internal_Rela *lorel;
4600 /* We allow an arbitrary number of HI20 relocs before the
4601 LO12 reloc. This permits gcc to emit the HI and LO relocs
4603 for (lorel = rel + 1;
4605 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
4608 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
4609 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
4610 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
4611 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
4613 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
4614 contents, relocation + addend);
4618 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4619 contents, offset, relocation, addend);
4624 case R_NDS32_GOT17S2_RELA:
4625 case R_NDS32_GOT15S2_RELA:
4629 BFD_ASSERT (sgot != NULL);
4635 off = h->got.offset;
4636 BFD_ASSERT (off != (bfd_vma) - 1);
4638 dyn = htab->root.dynamic_sections_created;
4639 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
4640 (dyn, info->shared, h) || (info->shared
4646 /* This is actually a static link, or it is a
4647 -Bsymbolic link and the symbol is defined
4648 locally, or the symbol was forced to be local
4649 because of a version file. We must initialize
4650 this entry in the global offset table. Since the
4651 offset must always be a multiple of 4, we use the
4652 least significant bit to record whether we have
4653 initialized it already.
4655 When doing a dynamic link, we create a .rela.got
4656 relocation entry to initialize the value. This
4657 is done in the finish_dynamic_symbol routine. */
4662 bfd_put_32 (output_bfd, relocation,
4663 sgot->contents + off);
4672 BFD_ASSERT (local_got_offsets != NULL
4673 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4675 off = local_got_offsets[r_symndx];
4677 /* The offset must always be a multiple of 4. We use
4678 the least significant bit to record whether we have
4679 already processed this entry. */
4684 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4689 Elf_Internal_Rela outrel;
4691 /* We need to generate a R_NDS32_RELATIVE reloc
4692 for the dynamic linker. */
4693 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4694 BFD_ASSERT (srelgot != NULL);
4696 outrel.r_offset = (elf_gp (output_bfd)
4697 + sgot->output_offset + off);
4698 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4699 outrel.r_addend = relocation;
4700 loc = srelgot->contents;
4702 srelgot->reloc_count * sizeof (Elf32_External_Rela);
4703 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4704 ++srelgot->reloc_count;
4706 local_got_offsets[r_symndx] |= 1;
4709 relocation = sgot->output_section->vma + sgot->output_offset + off
4710 - elf_gp (output_bfd);
4712 if (relocation & align)
4714 /* Incorrect alignment. */
4715 (*_bfd_error_handler)
4716 (_("%B: warning: unaligned access to GOT entry."), input_bfd);
4718 r = bfd_reloc_dangerous;
4723 case R_NDS32_SDA16S3_RELA:
4724 case R_NDS32_SDA15S3_RELA:
4725 case R_NDS32_SDA15S3:
4729 case R_NDS32_SDA17S2_RELA:
4730 case R_NDS32_SDA15S2_RELA:
4731 case R_NDS32_SDA12S2_SP_RELA:
4732 case R_NDS32_SDA12S2_DP_RELA:
4733 case R_NDS32_SDA15S2:
4734 case R_NDS32_SDA_FP7U2_RELA:
4738 case R_NDS32_SDA18S1_RELA:
4739 case R_NDS32_SDA15S1_RELA:
4740 case R_NDS32_SDA15S1:
4744 case R_NDS32_SDA19S0_RELA:
4745 case R_NDS32_SDA15S0_RELA:
4746 case R_NDS32_SDA15S0:
4750 BFD_ASSERT (sec != NULL);
4752 /* If the symbol is in the abs section, the out_bfd will be null.
4753 This happens when the relocation has a symbol@GOTOFF. */
4754 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
4755 if (r != bfd_reloc_ok)
4757 (*_bfd_error_handler)
4758 (_("%B: warning: relocate SDA_BASE failed."), input_bfd);
4763 /* At this point `relocation' contains the object's
4765 if (r_type == R_NDS32_SDA_FP7U2_RELA)
4767 relocation -= fpbase_addr;
4771 /* Now it contains the offset from _SDA_BASE_. */
4773 /* Make sure alignment is correct. */
4775 if (relocation & align)
4777 /* Incorrect alignment. */
4778 (*_bfd_error_handler)
4779 (_("%B(%A): warning: unaligned small data access of type %d."),
4780 input_bfd, input_section, r_type);
4787 case R_NDS32_17IFC_PCREL_RELA:
4788 case R_NDS32_10IFCU_PCREL_RELA:
4792 /* DON'T fall through. */
4795 /* OLD_NDS32_RELOC. */
4797 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4798 contents, offset, relocation, addend);
4802 switch ((int) r_type)
4804 case R_NDS32_20_RELA:
4805 case R_NDS32_5_RELA:
4806 case R_NDS32_9_PCREL_RELA:
4807 case R_NDS32_WORD_9_PCREL_RELA:
4808 case R_NDS32_10_UPCREL_RELA:
4809 case R_NDS32_15_PCREL_RELA:
4810 case R_NDS32_17_PCREL_RELA:
4811 case R_NDS32_25_PCREL_RELA:
4812 case R_NDS32_25_ABS_RELA:
4813 case R_NDS32_HI20_RELA:
4814 case R_NDS32_LO12S3_RELA:
4815 case R_NDS32_LO12S2_RELA:
4816 case R_NDS32_LO12S2_DP_RELA:
4817 case R_NDS32_LO12S2_SP_RELA:
4818 case R_NDS32_LO12S1_RELA:
4819 case R_NDS32_LO12S0_RELA:
4820 case R_NDS32_LO12S0_ORI_RELA:
4821 case R_NDS32_SDA16S3_RELA:
4822 case R_NDS32_SDA17S2_RELA:
4823 case R_NDS32_SDA18S1_RELA:
4824 case R_NDS32_SDA19S0_RELA:
4825 case R_NDS32_SDA15S3_RELA:
4826 case R_NDS32_SDA15S2_RELA:
4827 case R_NDS32_SDA12S2_DP_RELA:
4828 case R_NDS32_SDA12S2_SP_RELA:
4829 case R_NDS32_SDA15S1_RELA:
4830 case R_NDS32_SDA15S0_RELA:
4831 case R_NDS32_SDA_FP7U2_RELA:
4832 case R_NDS32_9_PLTREL:
4833 case R_NDS32_25_PLTREL:
4835 case R_NDS32_GOT_HI20:
4836 case R_NDS32_GOT_LO12:
4837 case R_NDS32_GOT_LO15:
4838 case R_NDS32_GOT_LO19:
4839 case R_NDS32_GOT15S2_RELA:
4840 case R_NDS32_GOT17S2_RELA:
4841 case R_NDS32_GOTPC20:
4842 case R_NDS32_GOTPC_HI20:
4843 case R_NDS32_GOTPC_LO12:
4844 case R_NDS32_GOTOFF:
4845 case R_NDS32_GOTOFF_HI20:
4846 case R_NDS32_GOTOFF_LO12:
4847 case R_NDS32_GOTOFF_LO15:
4848 case R_NDS32_GOTOFF_LO19:
4849 case R_NDS32_PLTREL_HI20:
4850 case R_NDS32_PLTREL_LO12:
4851 case R_NDS32_PLT_GOTREL_HI20:
4852 case R_NDS32_PLT_GOTREL_LO12:
4853 case R_NDS32_PLT_GOTREL_LO15:
4854 case R_NDS32_PLT_GOTREL_LO19:
4855 case R_NDS32_PLT_GOTREL_LO20:
4856 case R_NDS32_17IFC_PCREL_RELA:
4857 case R_NDS32_10IFCU_PCREL_RELA:
4858 /* Instruction related relocs must handle endian properly. */
4859 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER */
4860 r = nds32_elf_final_link_relocate (howto, input_bfd,
4861 input_section, contents,
4862 rel->r_offset, relocation,
4867 /* All other relocs can use default handler. */
4868 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4869 contents, rel->r_offset,
4870 relocation, rel->r_addend);
4876 if (r != bfd_reloc_ok)
4878 /* FIXME: This should be generic enough to go in a utility. */
4882 name = h->root.root.string;
4885 name = bfd_elf_string_from_elf_section
4886 (input_bfd, symtab_hdr->sh_link, sym->st_name);
4887 if (name == NULL || *name == '\0')
4888 name = bfd_section_name (input_bfd, sec);
4896 case bfd_reloc_overflow:
4897 if (!((*info->callbacks->reloc_overflow)
4898 (info, (h ? &h->root : NULL), name, howto->name,
4899 (bfd_vma) 0, input_bfd, input_section, offset)))
4903 case bfd_reloc_undefined:
4904 if (!((*info->callbacks->undefined_symbol)
4905 (info, name, input_bfd, input_section, offset, TRUE)))
4909 case bfd_reloc_outofrange:
4910 errmsg = _("internal error: out of range error");
4913 case bfd_reloc_notsupported:
4914 errmsg = _("internal error: unsupported relocation error");
4917 case bfd_reloc_dangerous:
4918 errmsg = _("internal error: dangerous error");
4922 errmsg = _("internal error: unknown error");
4926 if (!((*info->callbacks->warning)
4927 (info, errmsg, name, input_bfd, input_section, offset)))
4937 /* Finish up dynamic symbol handling. We set the contents of various
4938 dynamic sections here. */
4941 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
4942 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
4944 struct elf_nds32_link_hash_table *htab;
4947 htab = nds32_elf_hash_table (info);
4949 if (h->plt.offset != (bfd_vma) - 1)
4957 bfd_vma local_plt_offset;
4958 Elf_Internal_Rela rela;
4960 /* This symbol has an entry in the procedure linkage table. Set
4963 BFD_ASSERT (h->dynindx != -1);
4966 sgot = htab->sgotplt;
4967 srela = htab->srelplt;
4968 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
4970 /* Get the index in the procedure linkage table which
4971 corresponds to this symbol. This is the index of this symbol
4972 in all the symbols for which we are making plt entries. The
4973 first entry in the procedure linkage table is reserved. */
4974 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
4976 /* Get the offset into the .got table of the entry that
4977 corresponds to this function. Each .got entry is 4 bytes.
4978 The first three are reserved. */
4979 got_offset = (plt_index + 3) * 4;
4981 /* Fill in the entry in the procedure linkage table. */
4986 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
4987 + sgot->output_offset + got_offset) >> 12)
4989 bfd_putb32 (insn, splt->contents + h->plt.offset);
4991 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
4992 + sgot->output_offset + got_offset) & 0x0fff)
4994 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
4996 insn = PLT_ENTRY_WORD2;
4997 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
4999 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
5000 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5002 insn = PLT_ENTRY_WORD4
5003 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
5004 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5005 local_plt_offset = 12;
5009 /* sda_base must be set at this time. */
5013 /* FIXME, sda_base is 65536, it will damage opcode. */
5014 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5015 offset = sgot->output_section->vma + sgot->output_offset + got_offset
5016 - elf_gp (output_bfd);
5017 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
5018 bfd_putb32 (insn, splt->contents + h->plt.offset);
5020 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
5021 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5023 insn = PLT_PIC_ENTRY_WORD2;
5024 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5026 insn = PLT_PIC_ENTRY_WORD3;
5027 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5029 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
5030 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5032 insn = PLT_PIC_ENTRY_WORD5
5033 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
5034 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
5036 local_plt_offset = 16;
5039 /* Fill in the entry in the global offset table,
5040 so it will fall through to the next instruction for the first time. */
5041 bfd_put_32 (output_bfd,
5042 (splt->output_section->vma + splt->output_offset
5043 + h->plt.offset + local_plt_offset),
5044 sgot->contents + got_offset);
5046 /* Fill in the entry in the .rela.plt section. */
5047 rela.r_offset = (sgot->output_section->vma
5048 + sgot->output_offset + got_offset);
5049 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
5051 loc = srela->contents;
5052 loc += plt_index * sizeof (Elf32_External_Rela);
5053 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5055 if (!h->def_regular)
5057 /* Mark the symbol as undefined, rather than as defined in
5058 the .plt section. Leave the value alone. */
5059 sym->st_shndx = SHN_UNDEF;
5060 if (!h->ref_regular_nonweak)
5065 if (h->got.offset != (bfd_vma) - 1)
5069 Elf_Internal_Rela rela;
5071 /* This symbol has an entry in the global offset table.
5075 srela = htab->srelgot;
5076 BFD_ASSERT (sgot != NULL && srela != NULL);
5078 rela.r_offset = (sgot->output_section->vma
5079 + sgot->output_offset + (h->got.offset & ~1));
5081 /* If this is a -Bsymbolic link, and the symbol is defined
5082 locally, we just want to emit a RELATIVE reloc. Likewise if
5083 the symbol was forced to be local because of a version file.
5084 The entry in the global offset table will already have been
5085 initialized in the relocate_section function. */
5088 || h->dynindx == -1 || h->forced_local) && h->def_regular)
5090 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5091 rela.r_addend = (h->root.u.def.value
5092 + h->root.u.def.section->output_section->vma
5093 + h->root.u.def.section->output_offset);
5097 BFD_ASSERT ((h->got.offset & 1) == 0);
5098 bfd_put_32 (output_bfd, (bfd_vma) 0,
5099 sgot->contents + h->got.offset);
5100 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
5104 loc = srela->contents;
5105 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
5106 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5107 ++srela->reloc_count;
5113 Elf_Internal_Rela rela;
5115 /* This symbols needs a copy reloc. Set it up. */
5117 BFD_ASSERT (h->dynindx != -1
5118 && (h->root.type == bfd_link_hash_defined
5119 || h->root.type == bfd_link_hash_defweak));
5121 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
5122 BFD_ASSERT (s != NULL);
5124 rela.r_offset = (h->root.u.def.value
5125 + h->root.u.def.section->output_section->vma
5126 + h->root.u.def.section->output_offset);
5127 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
5130 loc += s->reloc_count * sizeof (Elf32_External_Rela);
5131 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5135 /* Mark some specially defined symbols as absolute. */
5136 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5137 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5138 sym->st_shndx = SHN_ABS;
5144 /* Finish up the dynamic sections. */
5147 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
5149 struct elf_nds32_link_hash_table *htab;
5154 htab = nds32_elf_hash_table (info);
5155 dynobj = htab->root.dynobj;
5157 sgot = htab->sgotplt;
5158 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5160 if (htab->root.dynamic_sections_created)
5163 Elf32_External_Dyn *dyncon, *dynconend;
5165 BFD_ASSERT (sgot != NULL && sdyn != NULL);
5167 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5168 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5170 for (; dyncon < dynconend; dyncon++)
5172 Elf_Internal_Dyn dyn;
5175 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5183 /* name = ".got"; */
5184 s = htab->sgot->output_section;
5187 s = htab->srelplt->output_section;
5189 BFD_ASSERT (s != NULL);
5190 dyn.d_un.d_ptr = s->vma;
5191 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5195 s = htab->srelplt->output_section;
5196 BFD_ASSERT (s != NULL);
5197 dyn.d_un.d_val = s->size;
5198 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5202 /* My reading of the SVR4 ABI indicates that the
5203 procedure linkage table relocs (DT_JMPREL) should be
5204 included in the overall relocs (DT_RELA). This is
5205 what Solaris does. However, UnixWare can not handle
5206 that case. Therefore, we override the DT_RELASZ entry
5207 here to make it not include the JMPREL relocs. Since
5208 the linker script arranges for .rela.plt to follow all
5209 other relocation sections, we don't have to worry
5210 about changing the DT_RELA entry. */
5211 if (htab->srelplt != NULL)
5213 s = htab->srelplt->output_section;
5214 dyn.d_un.d_val -= s->size;
5216 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5221 /* Fill in the first entry in the procedure linkage table. */
5223 if (splt && splt->size > 0)
5230 /* FIXME, sda_base is 65536, it will damage opcode. */
5231 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5232 offset = sgot->output_section->vma + sgot->output_offset + 4
5233 - elf_gp (output_bfd);
5234 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
5235 bfd_putb32 (insn, splt->contents);
5237 /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
5238 /* here has a typo? */
5239 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
5240 bfd_putb32 (insn, splt->contents + 4);
5242 insn = PLT0_PIC_ENTRY_WORD2;
5243 bfd_putb32 (insn, splt->contents + 8);
5245 insn = PLT0_PIC_ENTRY_WORD3;
5246 bfd_putb32 (insn, splt->contents + 12);
5248 insn = PLT0_PIC_ENTRY_WORD4;
5249 bfd_putb32 (insn, splt->contents + 16);
5251 insn = PLT0_PIC_ENTRY_WORD5;
5252 bfd_putb32 (insn, splt->contents + 20);
5259 /* addr = .got + 4 */
5260 addr = sgot->output_section->vma + sgot->output_offset + 4;
5261 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
5262 bfd_putb32 (insn, splt->contents);
5264 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
5265 bfd_putb32 (insn, splt->contents + 4);
5267 insn = PLT0_ENTRY_WORD2;
5268 bfd_putb32 (insn, splt->contents + 8);
5270 insn = PLT0_ENTRY_WORD3;
5271 bfd_putb32 (insn, splt->contents + 12);
5273 insn = PLT0_ENTRY_WORD4;
5274 bfd_putb32 (insn, splt->contents + 16);
5277 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
5282 /* Fill in the first three entries in the global offset table. */
5283 if (sgot && sgot->size > 0)
5286 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5288 bfd_put_32 (output_bfd,
5289 sdyn->output_section->vma + sdyn->output_offset,
5291 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5292 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5294 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5301 /* Set the right machine number. */
5304 nds32_elf_object_p (bfd *abfd)
5306 static unsigned int cur_arch = 0;
5308 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
5310 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
5311 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
5318 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
5321 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
5323 case E_NDS_ARCH_STAR_V2_0:
5324 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
5326 case E_NDS_ARCH_STAR_V3_0:
5327 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
5329 case E_NDS_ARCH_STAR_V3_M:
5330 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
5337 /* Store the machine number in the flags field. */
5340 nds32_elf_final_write_processing (bfd *abfd,
5341 bfd_boolean linker ATTRIBUTE_UNUSED)
5344 static unsigned int cur_mach = 0;
5346 if (bfd_mach_n1 != bfd_get_mach (abfd))
5348 cur_mach = bfd_get_mach (abfd);
5354 /* Only happen when object is empty, since the case is abandon. */
5356 val |= E_NDS_ABI_AABI;
5357 val |= E_NDS32_ELF_VER_1_4;
5362 case bfd_mach_n1h_v2:
5363 val = E_NDS_ARCH_STAR_V2_0;
5365 case bfd_mach_n1h_v3:
5366 val = E_NDS_ARCH_STAR_V3_0;
5368 case bfd_mach_n1h_v3m:
5369 val = E_NDS_ARCH_STAR_V3_M;
5376 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
5377 elf_elfheader (abfd)->e_flags |= val;
5380 /* Function to keep NDS32 specific file flags. */
5383 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
5385 BFD_ASSERT (!elf_flags_init (abfd)
5386 || elf_elfheader (abfd)->e_flags == flags);
5388 elf_elfheader (abfd)->e_flags = flags;
5389 elf_flags_init (abfd) = TRUE;
5394 convert_e_flags (unsigned int e_flags, unsigned int arch)
5396 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
5398 /* From 0.9 to 1.0. */
5399 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
5401 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5402 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5403 if (arch == E_NDS_ARCH_STAR_V1_0)
5410 /* From 1.0 to 2.0. */
5411 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
5413 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
5414 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
5416 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5417 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5422 nds32_check_vec_size (bfd *ibfd)
5424 static unsigned int nds32_vec_size = 0;
5426 asection *sec_t = NULL;
5427 bfd_byte *contents = NULL;
5429 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
5431 if (sec_t && sec_t->size >= 4)
5433 /* Get vec_size in file. */
5434 unsigned int flag_t;
5436 nds32_get_section_contents (ibfd, sec_t, &contents);
5437 flag_t = bfd_get_32 (ibfd, contents);
5439 /* The value could only be 4 or 16. */
5441 if (!nds32_vec_size)
5442 /* Set if not set yet. */
5443 nds32_vec_size = (flag_t & 0x3);
5444 else if (nds32_vec_size != (flag_t & 0x3))
5446 (*_bfd_error_handler) (_("%B: ISR vector size mismatch"
5447 " with previous modules, previous %u-byte, current %u-byte"),
5449 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
5450 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
5454 /* Only keep the first vec_size section. */
5455 sec_t->flags |= SEC_EXCLUDE;
5461 /* Merge backend specific data from an object file to the output
5462 object file when linking. */
5465 nds32_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5469 flagword out_16regs;
5471 flagword out_no_mac;
5473 flagword out_version;
5474 flagword in_version;
5475 flagword out_fpu_config;
5476 flagword in_fpu_config;
5478 /* TODO: Revise to use object-attributes instead. */
5479 if (!nds32_check_vec_size (ibfd))
5482 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5483 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5486 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
5488 (*_bfd_error_handler)
5489 (_("%B: warning: Endian mismatch with previous modules."), ibfd);
5491 bfd_set_error (bfd_error_bad_value);
5495 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
5496 if (in_version == E_NDS32_ELF_VER_1_2)
5498 (*_bfd_error_handler)
5499 (_("%B: warning: Older version of object file encountered, "
5500 "Please recompile with current tool chain."), ibfd);
5503 /* We may need to merge V1 and V2 arch object files to V2. */
5504 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5505 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5507 /* Need to convert version. */
5508 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5509 == E_NDS_ARCH_STAR_RESERVED)
5511 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5513 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
5514 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5515 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5517 elf_elfheader (obfd)->e_flags =
5518 convert_e_flags (elf_elfheader (obfd)->e_flags,
5519 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
5523 elf_elfheader (ibfd)->e_flags =
5524 convert_e_flags (elf_elfheader (ibfd)->e_flags,
5525 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
5529 /* Extract some flags. */
5530 in_flags = elf_elfheader (ibfd)->e_flags
5531 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
5532 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
5534 /* The following flags need special treatment. */
5535 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
5536 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
5537 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
5539 /* Extract some flags. */
5540 out_flags = elf_elfheader (obfd)->e_flags
5541 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
5542 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
5544 /* The following flags need special treatment. */
5545 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
5546 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
5547 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
5548 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
5549 if (!elf_flags_init (obfd))
5551 /* If the input is the default architecture then do not
5552 bother setting the flags for the output architecture,
5553 instead allow future merges to do this. If no future
5554 merges ever set these flags then they will retain their
5555 unitialised values, which surprise surprise, correspond
5556 to the default values. */
5557 if (bfd_get_arch_info (ibfd)->the_default)
5560 elf_flags_init (obfd) = TRUE;
5561 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5563 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
5564 && bfd_get_arch_info (obfd)->the_default)
5566 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
5567 bfd_get_mach (ibfd));
5573 /* Check flag compatibility. */
5574 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
5576 (*_bfd_error_handler)
5577 (_("%B: error: ABI mismatch with previous modules."), ibfd);
5579 bfd_set_error (bfd_error_bad_value);
5583 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
5585 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
5587 (*_bfd_error_handler)
5588 (_("%B: error: Instruction set mismatch with previous modules."), ibfd);
5590 bfd_set_error (bfd_error_bad_value);
5595 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
5596 and perf ext1 and DIV are mergerd to perf ext1. */
5597 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
5599 elf_elfheader (obfd)->e_flags =
5600 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
5601 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
5602 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
5603 ? E_NDS32_HAS_EXT_INST : 0)
5604 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
5605 ? E_NDS32_HAS_EXT_INST : 0)
5606 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
5607 | ((in_version > out_version) ? out_version : in_version);
5611 if (in_version != out_version)
5612 (*_bfd_error_handler) (_("%B: warning: Incompatible elf-versions %s and %s."),
5613 ibfd, nds32_elfver_strtab[out_version],
5614 nds32_elfver_strtab[in_version]);
5616 elf_elfheader (obfd)->e_flags = in_flags | out_flags
5617 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
5618 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
5619 | (in_version > out_version ? out_version : in_version);
5625 /* Display the flags field. */
5628 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5630 FILE *file = (FILE *) ptr;
5632 BFD_ASSERT (abfd != NULL && ptr != NULL);
5634 _bfd_elf_print_private_bfd_data (abfd, ptr);
5636 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
5638 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
5642 fprintf (file, _(": n1 instructions"));
5645 fprintf (file, _(": n1h instructions"));
5655 nds32_elf_action_discarded (asection *sec)
5659 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
5662 return _bfd_elf_default_action_discarded (sec);
5666 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
5667 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
5668 Elf_Internal_Sym *sym)
5671 switch (ELF32_R_TYPE (rel->r_info))
5673 case R_NDS32_GNU_VTINHERIT:
5674 case R_NDS32_GNU_VTENTRY:
5675 case R_NDS32_RELA_GNU_VTINHERIT:
5676 case R_NDS32_RELA_GNU_VTENTRY:
5680 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5684 nds32_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, asection *sec,
5685 const Elf_Internal_Rela *relocs)
5687 /* Update the got entry reference counts for the section being removed. */
5688 Elf_Internal_Shdr *symtab_hdr;
5689 struct elf_link_hash_entry **sym_hashes;
5690 bfd_signed_vma *local_got_refcounts;
5691 const Elf_Internal_Rela *rel, *relend;
5693 elf_section_data (sec)->local_dynrel = NULL;
5695 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5696 sym_hashes = elf_sym_hashes (abfd);
5697 local_got_refcounts = elf_local_got_refcounts (abfd);
5699 relend = relocs + sec->reloc_count;
5700 for (rel = relocs; rel < relend; rel++)
5702 unsigned long r_symndx;
5703 struct elf_link_hash_entry *h = NULL;
5705 r_symndx = ELF32_R_SYM (rel->r_info);
5706 if (r_symndx >= symtab_hdr->sh_info)
5708 /* External symbol. */
5709 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5710 while (h->root.type == bfd_link_hash_indirect
5711 || h->root.type == bfd_link_hash_warning)
5712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5715 switch (ELF32_R_TYPE (rel->r_info))
5717 case R_NDS32_GOT_HI20:
5718 case R_NDS32_GOT_LO12:
5719 case R_NDS32_GOT_LO15:
5720 case R_NDS32_GOT_LO19:
5721 case R_NDS32_GOT17S2_RELA:
5722 case R_NDS32_GOT15S2_RELA:
5723 case R_NDS32_GOTOFF:
5724 case R_NDS32_GOTOFF_HI20:
5725 case R_NDS32_GOTOFF_LO12:
5726 case R_NDS32_GOTOFF_LO15:
5727 case R_NDS32_GOTOFF_LO19:
5729 case R_NDS32_GOTPC_HI20:
5730 case R_NDS32_GOTPC_LO12:
5731 case R_NDS32_GOTPC20:
5734 if (h->got.refcount > 0)
5739 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
5740 local_got_refcounts[r_symndx]--;
5744 case R_NDS32_16_RELA:
5745 case R_NDS32_20_RELA:
5746 case R_NDS32_5_RELA:
5747 case R_NDS32_32_RELA:
5748 case R_NDS32_HI20_RELA:
5749 case R_NDS32_LO12S3_RELA:
5750 case R_NDS32_LO12S2_RELA:
5751 case R_NDS32_LO12S2_DP_RELA:
5752 case R_NDS32_LO12S2_SP_RELA:
5753 case R_NDS32_LO12S1_RELA:
5754 case R_NDS32_LO12S0_RELA:
5755 case R_NDS32_LO12S0_ORI_RELA:
5756 case R_NDS32_SDA16S3_RELA:
5757 case R_NDS32_SDA17S2_RELA:
5758 case R_NDS32_SDA18S1_RELA:
5759 case R_NDS32_SDA19S0_RELA:
5760 case R_NDS32_SDA15S3_RELA:
5761 case R_NDS32_SDA15S2_RELA:
5762 case R_NDS32_SDA12S2_DP_RELA:
5763 case R_NDS32_SDA12S2_SP_RELA:
5764 case R_NDS32_SDA15S1_RELA:
5765 case R_NDS32_SDA15S0_RELA:
5766 case R_NDS32_SDA_FP7U2_RELA:
5767 case R_NDS32_15_PCREL_RELA:
5768 case R_NDS32_17_PCREL_RELA:
5769 case R_NDS32_25_PCREL_RELA:
5772 struct elf_nds32_link_hash_entry *eh;
5773 struct elf_nds32_dyn_relocs **pp;
5774 struct elf_nds32_dyn_relocs *p;
5776 if (!info->shared && h->plt.refcount > 0)
5777 h->plt.refcount -= 1;
5779 eh = (struct elf_nds32_link_hash_entry *) h;
5781 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5784 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
5785 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
5786 || ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA)
5796 case R_NDS32_9_PLTREL:
5797 case R_NDS32_25_PLTREL:
5800 if (h->plt.refcount > 0)
5813 /* Look through the relocs for a section during the first phase.
5814 Since we don't do .gots or .plts, we just need to consider the
5815 virtual table relocs for gc. */
5818 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5819 asection *sec, const Elf_Internal_Rela *relocs)
5821 Elf_Internal_Shdr *symtab_hdr;
5822 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5823 const Elf_Internal_Rela *rel;
5824 const Elf_Internal_Rela *rel_end;
5825 struct elf_nds32_link_hash_table *htab;
5827 asection *sreloc = NULL;
5829 if (info->relocatable)
5832 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5833 sym_hashes = elf_sym_hashes (abfd);
5835 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5836 if (!elf_bad_symtab (abfd))
5837 sym_hashes_end -= symtab_hdr->sh_info;
5839 htab = nds32_elf_hash_table (info);
5840 dynobj = htab->root.dynobj;
5842 rel_end = relocs + sec->reloc_count;
5843 for (rel = relocs; rel < rel_end; rel++)
5845 enum elf_nds32_reloc_type r_type;
5846 struct elf_link_hash_entry *h;
5847 unsigned long r_symndx;
5849 r_symndx = ELF32_R_SYM (rel->r_info);
5850 r_type = ELF32_R_TYPE (rel->r_info);
5851 if (r_symndx < symtab_hdr->sh_info)
5855 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5856 while (h->root.type == bfd_link_hash_indirect
5857 || h->root.type == bfd_link_hash_warning)
5858 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5861 /* Some relocs require a global offset table. */
5862 if (htab->sgot == NULL)
5866 case R_NDS32_GOT_HI20:
5867 case R_NDS32_GOT_LO12:
5868 case R_NDS32_GOT_LO15:
5869 case R_NDS32_GOT_LO19:
5870 case R_NDS32_GOT17S2_RELA:
5871 case R_NDS32_GOT15S2_RELA:
5872 case R_NDS32_GOTOFF:
5873 case R_NDS32_GOTOFF_HI20:
5874 case R_NDS32_GOTOFF_LO12:
5875 case R_NDS32_GOTOFF_LO15:
5876 case R_NDS32_GOTOFF_LO19:
5877 case R_NDS32_GOTPC20:
5878 case R_NDS32_GOTPC_HI20:
5879 case R_NDS32_GOTPC_LO12:
5882 htab->root.dynobj = dynobj = abfd;
5883 if (!create_got_section (dynobj, info))
5892 switch ((int) r_type)
5894 case R_NDS32_GOT_HI20:
5895 case R_NDS32_GOT_LO12:
5896 case R_NDS32_GOT_LO15:
5897 case R_NDS32_GOT_LO19:
5900 h->got.refcount += 1;
5903 bfd_signed_vma *local_got_refcounts;
5905 /* This is a global offset table entry for a local
5907 local_got_refcounts = elf_local_got_refcounts (abfd);
5908 if (local_got_refcounts == NULL)
5912 size = symtab_hdr->sh_info;
5913 size *= sizeof (bfd_signed_vma);
5914 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
5915 if (local_got_refcounts == NULL)
5917 elf_local_got_refcounts (abfd) = local_got_refcounts;
5919 local_got_refcounts[r_symndx] += 1;
5923 case R_NDS32_9_PLTREL:
5924 case R_NDS32_25_PLTREL:
5925 case R_NDS32_PLTREL_HI20:
5926 case R_NDS32_PLTREL_LO12:
5927 case R_NDS32_PLT_GOTREL_HI20:
5928 case R_NDS32_PLT_GOTREL_LO12:
5929 case R_NDS32_PLT_GOTREL_LO15:
5930 case R_NDS32_PLT_GOTREL_LO19:
5931 case R_NDS32_PLT_GOTREL_LO20:
5933 /* This symbol requires a procedure linkage table entry. We
5934 actually build the entry in adjust_dynamic_symbol,
5935 because this might be a case of linking PIC code without
5936 linking in any dynamic objects, in which case we don't
5937 need to generate a procedure linkage table after all. */
5939 /* If this is a local symbol, we resolve it directly without
5940 creating a procedure linkage table entry. */
5944 if (h->forced_local)
5948 h->plt.refcount += 1;
5951 case R_NDS32_16_RELA:
5952 case R_NDS32_20_RELA:
5953 case R_NDS32_5_RELA:
5954 case R_NDS32_32_RELA:
5955 case R_NDS32_HI20_RELA:
5956 case R_NDS32_LO12S3_RELA:
5957 case R_NDS32_LO12S2_RELA:
5958 case R_NDS32_LO12S2_DP_RELA:
5959 case R_NDS32_LO12S2_SP_RELA:
5960 case R_NDS32_LO12S1_RELA:
5961 case R_NDS32_LO12S0_RELA:
5962 case R_NDS32_LO12S0_ORI_RELA:
5963 case R_NDS32_SDA16S3_RELA:
5964 case R_NDS32_SDA17S2_RELA:
5965 case R_NDS32_SDA18S1_RELA:
5966 case R_NDS32_SDA19S0_RELA:
5967 case R_NDS32_SDA15S3_RELA:
5968 case R_NDS32_SDA15S2_RELA:
5969 case R_NDS32_SDA12S2_DP_RELA:
5970 case R_NDS32_SDA12S2_SP_RELA:
5971 case R_NDS32_SDA15S1_RELA:
5972 case R_NDS32_SDA15S0_RELA:
5973 case R_NDS32_SDA_FP7U2_RELA:
5974 case R_NDS32_15_PCREL_RELA:
5975 case R_NDS32_17_PCREL_RELA:
5976 case R_NDS32_25_PCREL_RELA:
5978 if (h != NULL && !info->shared)
5981 h->plt.refcount += 1;
5984 /* If we are creating a shared library, and this is a reloc against
5985 a global symbol, or a non PC relative reloc against a local
5986 symbol, then we need to copy the reloc into the shared library.
5987 However, if we are linking with -Bsymbolic, we do not need to
5988 copy a reloc against a global symbol which is defined in an
5989 object we are including in the link (i.e., DEF_REGULAR is set).
5990 At this point we have not seen all the input files, so it is
5991 possible that DEF_REGULAR is not set now but will be set later
5992 (it is never cleared). We account for that possibility below by
5993 storing information in the dyn_relocs field of the hash table
5994 entry. A similar situation occurs when creating shared libraries
5995 and symbol visibility changes render the symbol local.
5997 If on the other hand, we are creating an executable, we may need
5998 to keep relocations for symbols satisfied by a dynamic library
5999 if we manage to avoid copy relocs for the symbol. */
6001 && (sec->flags & SEC_ALLOC) != 0
6002 && ((r_type != R_NDS32_25_PCREL_RELA
6003 && r_type != R_NDS32_15_PCREL_RELA
6004 && r_type != R_NDS32_17_PCREL_RELA
6005 && !(r_type == R_NDS32_32_RELA
6006 && strcmp (sec->name, ".eh_frame") == 0))
6009 || h->root.type == bfd_link_hash_defweak
6010 || !h->def_regular))))
6012 && (sec->flags & SEC_ALLOC) != 0
6014 && (h->root.type == bfd_link_hash_defweak
6015 || !h->def_regular)))
6017 struct elf_nds32_dyn_relocs *p;
6018 struct elf_nds32_dyn_relocs **head;
6021 htab->root.dynobj = dynobj = abfd;
6023 /* When creating a shared object, we must copy these
6024 relocs into the output file. We create a reloc
6025 section in dynobj and make room for the reloc. */
6030 name = bfd_elf_string_from_elf_section
6031 (abfd, elf_elfheader (abfd)->e_shstrndx,
6032 elf_section_data (sec)->rela.hdr->sh_name);
6036 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6037 && strcmp (bfd_get_section_name (abfd, sec),
6040 sreloc = bfd_get_section_by_name (dynobj, name);
6045 sreloc = bfd_make_section (dynobj, name);
6046 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6047 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6048 if ((sec->flags & SEC_ALLOC) != 0)
6049 flags |= SEC_ALLOC | SEC_LOAD;
6051 || !bfd_set_section_flags (dynobj, sreloc, flags)
6052 || !bfd_set_section_alignment (dynobj, sreloc, 2))
6055 elf_section_type (sreloc) = SHT_RELA;
6057 elf_section_data (sec)->sreloc = sreloc;
6060 /* If this is a global symbol, we count the number of
6061 relocations we need for this symbol. */
6063 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
6068 Elf_Internal_Sym *isym;
6069 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
6073 /* Track dynamic relocs needed for local syms too. */
6074 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6078 head = ((struct elf_nds32_dyn_relocs **)
6079 &elf_section_data (s)->local_dynrel);
6083 if (p == NULL || p->sec != sec)
6085 bfd_size_type amt = sizeof (*p);
6086 p = (struct elf_nds32_dyn_relocs *) bfd_alloc (dynobj, amt);
6097 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
6098 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6099 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
6104 /* This relocation describes the C++ object vtable hierarchy.
6105 Reconstruct it for later use during GC. */
6106 case R_NDS32_RELA_GNU_VTINHERIT:
6107 case R_NDS32_GNU_VTINHERIT:
6108 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6112 /* This relocation describes which C++ vtable entries are actually
6113 used. Record for later use during GC. */
6114 case R_NDS32_GNU_VTENTRY:
6115 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6118 case R_NDS32_RELA_GNU_VTENTRY:
6119 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6128 /* Write VAL in uleb128 format to P, returning a pointer to the
6130 This code is copied from elf-attr.c. */
6133 write_uleb128 (bfd_byte *p, unsigned int val)
6148 static bfd_signed_vma
6149 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
6150 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
6151 int *pic_ext_target)
6153 bfd_signed_vma foff;
6154 bfd_vma symval, addend;
6157 /* Get the value of the symbol referred to by the reloc. */
6158 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6160 Elf_Internal_Sym *isym;
6162 /* A local symbol. */
6163 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6165 if (isym->st_shndx == SHN_UNDEF)
6166 sym_sec = bfd_und_section_ptr;
6167 else if (isym->st_shndx == SHN_ABS)
6168 sym_sec = bfd_abs_section_ptr;
6169 else if (isym->st_shndx == SHN_COMMON)
6170 sym_sec = bfd_com_section_ptr;
6172 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6173 symval = isym->st_value + sym_sec->output_section->vma
6174 + sym_sec->output_offset;
6179 struct elf_link_hash_entry *h;
6182 /* An external symbol. */
6183 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6184 h = elf_sym_hashes (abfd)[indx];
6185 BFD_ASSERT (h != NULL);
6187 if (h->root.type != bfd_link_hash_defined
6188 && h->root.type != bfd_link_hash_defweak)
6189 /* This appears to be a reference to an undefined
6190 symbol. Just ignore it--it will be caught by the
6191 regular reloc processing. */
6193 owner = h->root.u.def.section->owner;
6194 if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
6195 *pic_ext_target = 1;
6197 if (h->root.u.def.section->flags & SEC_MERGE)
6199 sym_sec = h->root.u.def.section;
6200 symval = _bfd_merged_section_offset (abfd, &sym_sec,
6201 elf_section_data (sym_sec)->sec_info,
6202 h->root.u.def.value);
6203 symval = symval + sym_sec->output_section->vma
6204 + sym_sec->output_offset;
6207 symval = (h->root.u.def.value
6208 + h->root.u.def.section->output_section->vma
6209 + h->root.u.def.section->output_offset);
6212 addend = irel->r_addend;
6214 foff = (symval + addend
6215 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
6220 calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
6221 Elf_Internal_Sym *isymbuf,
6222 Elf_Internal_Rela *irel,
6223 Elf_Internal_Shdr *symtab_hdr)
6227 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6229 Elf_Internal_Sym *isym;
6231 /* A local symbol. */
6232 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6234 if (isym->st_shndx == SHN_UNDEF)
6235 sym_sec = bfd_und_section_ptr;
6236 else if (isym->st_shndx == SHN_ABS)
6237 sym_sec = bfd_abs_section_ptr;
6238 else if (isym->st_shndx == SHN_COMMON)
6239 sym_sec = bfd_com_section_ptr;
6241 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6242 symval = isym->st_value + sym_sec->output_section->vma
6243 + sym_sec->output_offset;
6248 struct elf_link_hash_entry *h;
6249 struct elf_nds32_link_hash_table *htab;
6252 /* An external symbol. */
6253 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6254 h = elf_sym_hashes (abfd)[indx];
6255 BFD_ASSERT (h != NULL);
6256 htab = nds32_elf_hash_table (link_info);
6259 while (h->root.type == bfd_link_hash_indirect
6260 || h->root.type == bfd_link_hash_warning)
6261 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6263 if (h->plt.offset == (bfd_vma) - 1)
6265 if (h->root.type != bfd_link_hash_defined
6266 && h->root.type != bfd_link_hash_defweak)
6267 /* This appears to be a reference to an undefined
6268 * symbol. Just ignore it--it will be caught by the
6269 * regular reloc processing. */
6271 symval = (h->root.u.def.value
6272 + h->root.u.def.section->output_section->vma
6273 + h->root.u.def.section->output_offset);
6276 symval = splt->output_section->vma + h->plt.offset;
6282 static bfd_signed_vma
6283 calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
6284 Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
6285 Elf_Internal_Shdr *symtab_hdr)
6288 if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
6292 return foff - (irel->r_offset
6293 + sec->output_section->vma + sec->output_offset);
6296 /* Convert a 32-bit instruction to 16-bit one.
6297 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
6298 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
6299 type of INSN16. Return 1 if successful. */
6302 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6305 uint16_t insn16 = 0;
6307 unsigned long mach = bfd_get_mach (abfd);
6309 if (N32_SH5 (insn) != 0)
6312 switch (N32_SUB5 (insn))
6314 case N32_ALU1_ADD_SLLI:
6315 case N32_ALU1_ADD_SRLI:
6316 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6318 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
6320 insn_type = NDS32_INSN_ADD333;
6322 else if (N32_IS_RT4 (insn))
6324 if (N32_RT5 (insn) == N32_RA5 (insn))
6325 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
6326 else if (N32_RT5 (insn) == N32_RB5 (insn))
6327 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
6328 insn_type = NDS32_INSN_ADD45;
6332 case N32_ALU1_SUB_SLLI:
6333 case N32_ALU1_SUB_SRLI:
6334 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6336 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
6338 insn_type = NDS32_INSN_SUB333;
6340 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6342 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
6343 insn_type = NDS32_INSN_SUB45;
6347 case N32_ALU1_AND_SLLI:
6348 case N32_ALU1_AND_SRLI:
6349 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
6350 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6351 && N32_IS_RB3 (insn))
6353 if (N32_RT5 (insn) == N32_RA5 (insn))
6354 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
6355 else if (N32_RT5 (insn) == N32_RB5 (insn))
6356 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
6358 insn_type = NDS32_INSN_AND33;
6362 case N32_ALU1_XOR_SLLI:
6363 case N32_ALU1_XOR_SRLI:
6364 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
6365 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6366 && N32_IS_RB3 (insn))
6368 if (N32_RT5 (insn) == N32_RA5 (insn))
6369 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
6370 else if (N32_RT5 (insn) == N32_RB5 (insn))
6371 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
6373 insn_type = NDS32_INSN_XOR33;
6377 case N32_ALU1_OR_SLLI:
6378 case N32_ALU1_OR_SRLI:
6379 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
6380 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6381 && N32_IS_RB3 (insn))
6383 if (N32_RT5 (insn) == N32_RA5 (insn))
6384 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
6385 else if (N32_RT5 (insn) == N32_RB5 (insn))
6386 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
6388 insn_type = NDS32_INSN_OR33;
6392 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
6393 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
6394 && N32_RA5 (insn) == N32_RB5 (insn))
6396 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
6397 insn_type = NDS32_INSN_NOT33;
6401 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6403 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
6404 insn_type = NDS32_INSN_SRAI45;
6409 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6411 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
6412 insn_type = NDS32_INSN_SRLI45;
6417 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
6419 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
6421 insn_type = NDS32_INSN_SLLI333;
6426 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6428 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
6429 insn_type = NDS32_INSN_ZEH33;
6434 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6436 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
6437 insn_type = NDS32_INSN_SEB33;
6442 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6444 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
6445 insn_type = NDS32_INSN_SEH33;
6450 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6453 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
6454 insn_type = NDS32_INSN_SLT45;
6459 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6462 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
6463 insn_type = NDS32_INSN_SLTS45;
6468 if ((insn16 & 0x8000) == 0)
6474 *pinsn_type = insn_type;
6479 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6482 uint16_t insn16 = 0;
6484 unsigned long mach = bfd_get_mach (abfd);
6486 /* TODO: bset, bclr, btgl, btst. */
6487 if (__GF (insn, 6, 4) != 0)
6490 switch (N32_IMMU (insn, 6))
6493 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6494 && N32_IS_RB3 (insn))
6496 if (N32_RT5 (insn) == N32_RA5 (insn))
6497 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
6498 else if (N32_RT5 (insn) == N32_RB5 (insn))
6499 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
6501 insn_type = NDS32_INSN_MUL33;
6505 if ((insn16 & 0x8000) == 0)
6511 *pinsn_type = insn_type;
6516 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6520 uint16_t insn16 = 0;
6522 unsigned long mach = bfd_get_mach (abfd);
6524 /* Decode 32-bit instruction. */
6525 if (insn & 0x80000000)
6527 /* Not 32-bit insn. */
6531 op6 = N32_OP6 (insn);
6533 /* Convert it to 16-bit instruction. */
6537 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
6539 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
6540 insn_type = NDS32_INSN_MOVI55;
6542 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
6543 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
6545 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
6546 N32_IMM20S (insn) - 16);
6547 insn_type = NDS32_INSN_MOVPI45;
6552 if (N32_IMM15S (insn) == 0)
6554 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
6555 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
6557 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
6559 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
6560 insn_type = NDS32_INSN_MOV55;
6563 else if (N32_IMM15S (insn) > 0)
6565 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
6567 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
6569 insn_type = NDS32_INSN_ADDI333;
6571 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6572 && N32_IMM15S (insn) < 32)
6574 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
6575 insn_type = NDS32_INSN_ADDI45;
6577 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6578 && N32_RT5 (insn) == N32_RA5 (insn)
6579 && N32_IMM15S (insn) < 512)
6581 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6582 insn_type = NDS32_INSN_ADDI10_SP;
6584 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
6585 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
6586 && (N32_IMM15S (insn) % 4 == 0))
6588 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
6589 N32_IMM15S (insn) >> 2);
6590 insn_type = NDS32_INSN_ADDRI36_SP;
6596 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
6598 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
6599 0 - N32_IMM15S (insn));
6600 insn_type = NDS32_INSN_SUBI333;
6602 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6603 && N32_IMM15S (insn) > -32)
6605 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn), 0 - N32_IMM15S (insn));
6606 insn_type = NDS32_INSN_SUBI45;
6608 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6609 && N32_RT5 (insn) == N32_RA5 (insn)
6610 && N32_IMM15S (insn) >= -512)
6612 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6613 insn_type = NDS32_INSN_ADDI10_SP;
6619 if (N32_IMM15S (insn) == 0)
6621 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
6622 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
6624 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
6626 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
6627 insn_type = NDS32_INSN_MOV55;
6633 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
6634 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
6636 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
6637 insn_type = NDS32_INSN_NEG33;
6642 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6644 if (N32_IMM15U (insn) == 1)
6646 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
6647 insn_type = NDS32_INSN_XLSB33;
6649 else if (N32_IMM15U (insn) == 0x7ff)
6651 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
6652 insn_type = NDS32_INSN_X11B33;
6654 else if (N32_IMM15U (insn) == 0xff)
6656 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
6657 insn_type = NDS32_INSN_ZEB33;
6659 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
6660 && N32_IMM15U (insn) < 256)
6662 int imm15u = N32_IMM15U (insn);
6664 if (__builtin_popcount (imm15u) == 1)
6667 int imm3u = __builtin_ctz (imm15u);
6669 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
6670 insn_type = NDS32_INSN_BMSKI33;
6672 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
6675 int imm3u = __builtin_ctz (imm15u + 1) - 1;
6677 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
6678 insn_type = NDS32_INSN_FEXTI33;
6685 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
6686 && IS_WITHIN_U (N32_IMM15S (insn), 5))
6688 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
6689 insn_type = NDS32_INSN_SLTI45;
6694 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
6695 && IS_WITHIN_U (N32_IMM15S (insn), 5))
6697 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
6698 insn_type = NDS32_INSN_SLTSI45;
6703 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
6705 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
6706 insn_type = NDS32_INSN_LWI450;
6708 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6709 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6711 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
6713 insn_type = NDS32_INSN_LWI333;
6715 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
6716 && IS_WITHIN_U (N32_IMM15S (insn), 7))
6718 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
6719 insn_type = NDS32_INSN_LWI37;
6721 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
6722 && IS_WITHIN_U (N32_IMM15S (insn), 7))
6724 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
6725 insn_type = NDS32_INSN_LWI37_SP;
6727 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
6728 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
6730 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn), N32_IMM15S (insn) + 32);
6731 insn_type = NDS32_INSN_LWI45_FE;
6736 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
6738 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
6739 insn_type = NDS32_INSN_SWI450;
6741 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6742 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6744 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn), N32_IMM15S (insn));
6745 insn_type = NDS32_INSN_SWI333;
6747 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
6748 && IS_WITHIN_U (N32_IMM15S (insn), 7))
6750 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
6751 insn_type = NDS32_INSN_SWI37;
6753 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
6754 && IS_WITHIN_U (N32_IMM15S (insn), 7))
6756 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
6757 insn_type = NDS32_INSN_SWI37_SP;
6761 case N32_OP6_LWI_BI:
6762 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6763 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6765 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
6767 insn_type = NDS32_INSN_LWI333_BI;
6771 case N32_OP6_SWI_BI:
6772 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6773 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6775 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
6777 insn_type = NDS32_INSN_SWI333_BI;
6782 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6783 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6785 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
6787 insn_type = NDS32_INSN_LHI333;
6792 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6793 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6795 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
6797 insn_type = NDS32_INSN_SHI333;
6802 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6803 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6805 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
6807 insn_type = NDS32_INSN_LBI333;
6812 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6813 && IS_WITHIN_U (N32_IMM15S (insn), 3))
6815 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
6817 insn_type = NDS32_INSN_SBI333;
6822 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
6825 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
6828 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
6831 if ((insn & __BIT (14)) == 0)
6834 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
6835 && N32_RT5 (insn) != REG_R5)
6836 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
6837 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
6838 && N32_RA5 (insn) != REG_R5)
6839 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
6840 insn_type = NDS32_INSN_BEQS38;
6846 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
6847 && N32_RT5 (insn) != REG_R5)
6848 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
6849 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
6850 && N32_RA5 (insn) != REG_R5)
6851 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
6852 insn_type = NDS32_INSN_BNES38;
6858 switch (N32_BR2_SUB (insn))
6861 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
6863 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
6864 insn_type = NDS32_INSN_BEQZ38;
6866 else if (N32_RT5 (insn) == REG_R15 && IS_WITHIN_S (N32_IMM16S (insn), 8))
6868 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
6869 insn_type = NDS32_INSN_BEQZS8;
6874 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
6876 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
6877 insn_type = NDS32_INSN_BNEZ38;
6879 else if (N32_RT5 (insn) == REG_R15 && IS_WITHIN_S (N32_IMM16S (insn), 8))
6881 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
6882 insn_type = NDS32_INSN_BNEZS8;
6886 case N32_BR2_IFCALL:
6887 if (IS_WITHIN_U (N32_IMM16S (insn), 9))
6889 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
6890 insn_type = NDS32_INSN_IFCALL9;
6897 if ((insn & __BIT (24)) == 0)
6900 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
6902 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
6903 insn_type = NDS32_INSN_J8;
6909 if (__GF (insn, 8, 2) != 0)
6912 switch (N32_IMMU (insn, 5))
6915 if (N32_JREG_HINT (insn) == 0)
6918 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
6919 insn_type = NDS32_INSN_JR5;
6921 else if (N32_JREG_HINT (insn) == 1)
6924 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
6925 insn_type = NDS32_INSN_RET5;
6927 else if (N32_JREG_HINT (insn) == 3)
6929 /* ifret = mov55 $sp, $sp */
6930 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
6931 insn_type = NDS32_INSN_IFRET;
6936 /* It's convertible when return rt5 is $lp and address
6937 translation is kept. */
6938 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
6940 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
6941 insn_type = NDS32_INSN_JRAL5;
6948 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
6950 /* For v3, swid above 31 are used for ex9.it. */
6951 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
6952 insn_type = NDS32_INSN_BREAK16;
6957 /* This instruction has no 16-bit variant. */
6962 /* Bit-15 of insn16 should be set for a valid instruction. */
6963 if ((insn16 & 0x8000) == 0)
6969 *pinsn_type = insn_type;
6974 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
6975 Elf_Internal_Rela *reloc)
6977 uint16_t insn16 = 0;
6979 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
6980 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
6983 if (!N32_IS_RT3 (insn))
6986 switch (N32_OP6 (insn))
6989 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
6990 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
6993 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
6994 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
6997 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
7000 if (__GF (insn, 17, 3) == 6)
7001 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
7002 else if (__GF (insn, 17, 3) == 7)
7003 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
7007 if ((insn16 & 0x8000) == 0)
7014 /* Convert a 16-bit instruction to 32-bit one.
7015 INSN16 it the input and PINSN it the point to output.
7016 Return non-zero on successful. Otherwise 0 is returned. */
7019 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
7021 uint32_t insn = 0xffffffff;
7022 unsigned long mach = bfd_get_mach (abfd);
7024 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
7026 switch (__GF (insn16, 9, 6))
7028 case 0x4: /* add45 */
7029 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16), N16_RA5 (insn16));
7031 case 0x5: /* sub45 */
7032 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16), N16_RA5 (insn16));
7034 case 0x6: /* addi45 */
7035 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16), N16_IMM5U (insn16));
7037 case 0x7: /* subi45 */
7038 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16), -N16_IMM5U (insn16));
7040 case 0x8: /* srai45 */
7041 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16), N16_IMM5U (insn16));
7043 case 0x9: /* srli45 */
7044 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16), N16_IMM5U (insn16));
7047 case 0xa: /* slli333 */
7048 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7050 case 0xc: /* add333 */
7051 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16), N16_RB3 (insn16));
7053 case 0xd: /* sub333 */
7054 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16), N16_RB3 (insn16));
7056 case 0xe: /* addi333 */
7057 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7059 case 0xf: /* subi333 */
7060 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16), -N16_IMM3U (insn16));
7063 case 0x10: /* lwi333 */
7064 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7066 case 0x12: /* lhi333 */
7067 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7069 case 0x13: /* lbi333 */
7070 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7072 case 0x11: /* lwi333.bi */
7073 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7075 case 0x14: /* swi333 */
7076 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7078 case 0x16: /* shi333 */
7079 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7081 case 0x17: /* sbi333 */
7082 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7084 case 0x15: /* swi333.bi */
7085 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16), N16_IMM3U (insn16));
7088 case 0x18: /* addri36.sp */
7089 insn = N32_TYPE2 (ADDI, REG_SP, N16_RT3 (insn16), N16_IMM6U (insn16) << 2);
7092 case 0x19: /* lwi45.fe */
7093 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8, (32 - N16_IMM5U (insn16)) << 2);
7095 case 0x1a: /* lwi450 */
7096 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7098 case 0x1b: /* swi450 */
7099 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7102 /* These are r15 implied instructions. */
7103 case 0x30: /* slts45 */
7104 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7106 case 0x31: /* slt45 */
7107 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7109 case 0x32: /* sltsi45 */
7110 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7112 case 0x33: /* slti45 */
7113 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7115 case 0x34: /* beqzs8, bnezs8 */
7116 if (insn16 & __BIT (8))
7117 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
7119 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
7122 case 0x35: /* break16, ex9.it */
7123 /* Only consider range of v3 break16. */
7124 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
7127 case 0x3c: /* ifcall9 */
7128 insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
7130 case 0x3d: /* movpi45 */
7131 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
7134 case 0x3f: /* MISC33 */
7138 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7141 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16), N16_RA3 (insn16));
7144 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16), N16_RA3 (insn16));
7147 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16), N16_RA3 (insn16));
7150 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16), N16_RA3 (insn16));
7153 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16), N16_RA3 (insn16));
7159 switch (insn16 & 0x7)
7162 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
7165 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7168 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7171 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7173 case 4: /* xlsb33 */
7174 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
7176 case 5: /* x11b33 */
7177 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
7179 case 6: /* bmski33 */
7180 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7181 1 << N16_IMM3U (insn16));
7183 case 7: /* fexti33 */
7184 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7185 (1 << (N16_IMM3U (insn16) + 1)) - 1);
7191 switch (__GF (insn16, 10, 5))
7193 case 0x0: /* mov55 or ifret16 */
7194 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
7195 && N16_RT5 (insn16) == N16_RA5 (insn16))
7196 insn = N32_JREG (JR, 0, 0, 0, 3);
7198 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
7200 case 0x1: /* movi55 */
7201 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
7203 case 0x1b: /* addi10s (V2) */
7204 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
7208 switch (__GF (insn16, 11, 4))
7210 case 0x7: /* lwi37.fp/swi37.fp */
7211 if (insn16 & __BIT (7)) /* swi37.fp */
7212 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7214 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7216 case 0x8: /* beqz38 */
7217 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7219 case 0x9: /* bnez38 */
7220 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7222 case 0xa: /* beqs38/j8, implied r5 */
7223 if (N16_RT38 (insn16) == 5)
7224 insn = N32_JI (J, N16_IMM8S (insn16));
7226 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7228 case 0xb: /* bnes38 and others */
7229 if (N16_RT38 (insn16) == 5)
7231 switch (__GF (insn16, 5, 3))
7234 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
7237 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
7240 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
7242 case 2: /* ex9.it imm5 */
7243 /* ex9.it had no 32-bit variantl. */
7245 case 5: /* add5.pc */
7246 /* add5.pc had no 32-bit variantl. */
7251 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7253 case 0xe: /* lwi37/swi37 */
7254 if (insn16 & (1 << 7)) /* swi37.sp */
7255 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7257 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7262 if (insn & 0x80000000)
7271 is_sda_access_insn (unsigned long insn)
7273 switch (N32_OP6 (insn))
7294 static unsigned long
7295 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
7301 case R_NDS32_GOT_LO12:
7302 case R_NDS32_GOTOFF_LO12:
7303 case R_NDS32_PLTREL_LO12:
7304 case R_NDS32_PLT_GOTREL_LO12:
7305 case R_NDS32_LO12S0_RELA:
7306 switch (N32_OP6 (insn))
7310 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
7314 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
7318 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
7322 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
7327 case R_NDS32_LO12S1_RELA:
7328 switch (N32_OP6 (insn))
7332 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
7336 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
7340 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
7345 case R_NDS32_LO12S2_RELA:
7346 switch (N32_OP6 (insn))
7350 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
7354 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
7359 case R_NDS32_LO12S2_DP_RELA:
7360 case R_NDS32_LO12S2_SP_RELA:
7361 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
7371 /* Linker hasn't found the correct merge section for non-section symbol
7372 in relax time, this work is left to the function elf_link_input_bfd().
7373 So for non-section symbol, _bfd_merged_section_offset is also needed
7374 to find the correct symbol address. */
7377 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
7378 asection **psec, Elf_Internal_Rela *rel)
7380 asection *sec = *psec;
7383 relocation = (sec->output_section->vma
7384 + sec->output_offset + sym->st_value);
7385 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
7387 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7389 _bfd_merged_section_offset (abfd, psec,
7390 elf_section_data (sec)->sec_info,
7391 sym->st_value + rel->r_addend);
7394 _bfd_merged_section_offset (abfd, psec,
7395 elf_section_data (sec)->sec_info,
7396 sym->st_value) + rel->r_addend;
7400 /* If we have changed the section, and our original section is
7401 marked with SEC_EXCLUDE, it means that the original
7402 SEC_MERGE section has been completely subsumed in some
7403 other SEC_MERGE section. In this case, we need to leave
7404 some info around for --emit-relocs. */
7405 if ((sec->flags & SEC_EXCLUDE) != 0)
7406 sec->kept_section = *psec;
7409 rel->r_addend -= relocation;
7410 rel->r_addend += sec->output_section->vma + sec->output_offset;
7416 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
7417 Elf_Internal_Sym *isymbuf,
7418 Elf_Internal_Shdr *symtab_hdr)
7420 bfd_signed_vma foff;
7421 bfd_vma symval, addend;
7422 Elf_Internal_Rela irel_fn;
7423 Elf_Internal_Sym *isym;
7426 /* Get the value of the symbol referred to by the reloc. */
7427 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7429 /* A local symbol. */
7430 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7432 if (isym->st_shndx == SHN_UNDEF)
7433 sym_sec = bfd_und_section_ptr;
7434 else if (isym->st_shndx == SHN_ABS)
7435 sym_sec = bfd_abs_section_ptr;
7436 else if (isym->st_shndx == SHN_COMMON)
7437 sym_sec = bfd_com_section_ptr;
7439 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7440 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
7441 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
7442 addend = irel_fn.r_addend;
7447 struct elf_link_hash_entry *h;
7449 /* An external symbol. */
7450 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7451 h = elf_sym_hashes (abfd)[indx];
7452 BFD_ASSERT (h != NULL);
7454 while (h->root.type == bfd_link_hash_indirect
7455 || h->root.type == bfd_link_hash_warning)
7456 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7458 if (h->root.type != bfd_link_hash_defined
7459 && h->root.type != bfd_link_hash_defweak)
7460 /* This appears to be a reference to an undefined
7461 symbol. Just ignore it--it will be caught by the
7462 regular reloc processing. */
7465 if (h->root.u.def.section->flags & SEC_MERGE)
7467 sym_sec = h->root.u.def.section;
7468 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
7469 (sym_sec)->sec_info, h->root.u.def.value);
7470 symval = symval + sym_sec->output_section->vma
7471 + sym_sec->output_offset;
7474 symval = (h->root.u.def.value
7475 + h->root.u.def.section->output_section->vma
7476 + h->root.u.def.section->output_offset);
7477 addend = irel->r_addend;
7480 foff = symval + addend;
7486 calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
7487 Elf_Internal_Rela *irel,
7488 Elf_Internal_Shdr *symtab_hdr)
7491 bfd_vma *local_got_offsets;
7492 /* Get the value of the symbol referred to by the reloc. */
7493 struct elf_link_hash_entry *h;
7494 struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);
7496 /* An external symbol. */
7497 symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7498 h = elf_sym_hashes (abfd)[symndx];
7499 while (h->root.type == bfd_link_hash_indirect
7500 || h->root.type == bfd_link_hash_warning)
7501 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7505 BFD_ASSERT (h != NULL);
7506 return htab->sgot->output_section->vma + htab->sgot->output_offset
7511 local_got_offsets = elf_local_got_offsets (abfd);
7512 BFD_ASSERT (local_got_offsets != NULL);
7513 return htab->sgot->output_section->vma + htab->sgot->output_offset
7514 + local_got_offsets[ELF32_R_SYM (irel->r_info)];
7517 /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?). */
7518 /* The check of h->root.type is passed. */
7522 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
7523 asection *sec, Elf_Internal_Rela *rel)
7526 unsigned short insn16;
7528 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
7530 contents = elf_section_data (sec)->this_hdr.contents;
7531 insn16 = bfd_getb16 (contents + rel->r_offset);
7532 if (insn16 == NDS32_NOP16)
7537 /* It checks whether the instruction could be converted to
7538 16-bit form and returns the converted one.
7540 `internal_relocs' is supposed to be sorted. */
7543 is_convert_32_to_16 (bfd *abfd, asection *sec,
7544 Elf_Internal_Rela *reloc,
7545 Elf_Internal_Rela *internal_relocs,
7546 Elf_Internal_Rela *irelend,
7549 #define NORMAL_32_TO_16 (1 << 0)
7550 #define SPECIAL_32_TO_16 (1 << 1)
7551 bfd_byte *contents = NULL;
7555 Elf_Internal_Rela *pc_rel;
7556 int pic_ext_target = 0;
7557 Elf_Internal_Shdr *symtab_hdr;
7558 Elf_Internal_Sym *isymbuf = NULL;
7562 if (reloc->r_offset + 4 > sec->size)
7565 offset = reloc->r_offset;
7567 if (!nds32_get_section_contents (abfd, sec, &contents))
7569 insn = bfd_getb32 (contents + offset);
7571 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
7572 convert_type = NORMAL_32_TO_16;
7573 else if (special_convert_32_to_16 (insn, insn16, reloc))
7574 convert_type = SPECIAL_32_TO_16;
7578 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7579 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
7582 /* Find the first relocation of the same relocation-type,
7583 so we iteratie them forward. */
7585 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
7588 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
7590 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
7591 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
7592 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
7593 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
7595 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
7597 if (off > 0xff || off < -0x100 || off == 0)
7601 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
7603 /* movi => movi55 */
7604 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf, symtab_hdr);
7605 /* mem_addr is unsigned, but the value should be between [-16, 15]. */
7606 if ((mem_addr + 0x10) >> 5)
7610 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
7611 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
7612 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
7613 && convert_type == SPECIAL_32_TO_16)
7616 We've selected a best fp-base for this access, so we can
7617 always resolve it anyway. Do nothing. */
7620 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
7621 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
7622 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
7623 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
7624 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
7625 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
7627 /* Prevent unresolved addi instruction translate to addi45 or addi333. */
7636 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
7637 Elf_Internal_Rela *reloc,
7638 Elf_Internal_Rela *internal_relocs,
7639 Elf_Internal_Rela *irelend,
7640 unsigned short insn16)
7642 Elf_Internal_Rela *pc_rel;
7645 offset = reloc->r_offset;
7646 bfd_putb16 (insn16, contents + offset);
7647 /* Find the first relocation of the same relocation-type,
7648 so we iteratie them forward. */
7650 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
7653 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
7655 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
7656 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
7657 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
7660 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
7662 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
7664 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
7665 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
7667 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
7668 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
7669 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
7671 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
7675 /* Find a relocation of type specified by `reloc_type'
7676 of the same r_offset with reloc.
7677 If not found, return irelend.
7679 Assuming relocations are sorted by r_offset,
7680 we find the relocation from `reloc' backward untill relocs,
7681 or find it from `reloc' forward untill irelend. */
7683 static Elf_Internal_Rela *
7684 find_relocs_at_address (Elf_Internal_Rela *reloc,
7685 Elf_Internal_Rela *relocs,
7686 Elf_Internal_Rela *irelend,
7687 enum elf_nds32_reloc_type reloc_type)
7689 Elf_Internal_Rela *rel_t;
7691 /* Find backward. */
7693 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
7695 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
7698 /* We didn't find it backward. Try find it forward. */
7700 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
7702 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
7708 /* Find a relocation of specified type and offset.
7709 `reloc' is just a refence point to find a relocation at specified offset.
7710 If not found, return irelend.
7712 Assuming relocations are sorted by r_offset,
7713 we find the relocation from `reloc' backward untill relocs,
7714 or find it from `reloc' forward untill irelend. */
7716 static Elf_Internal_Rela *
7717 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
7718 Elf_Internal_Rela *relocs,
7719 Elf_Internal_Rela *irelend,
7720 unsigned char reloc_type,
7723 Elf_Internal_Rela *rel_t = NULL;
7725 /* First, we try to find a relocation of offset `offset_p',
7726 and then we use find_relocs_at_address to find specific type. */
7728 if (reloc->r_offset > offset_p)
7730 /* Find backward. */
7732 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
7735 else if (reloc->r_offset < offset_p)
7739 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
7746 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
7749 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
7753 nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
7754 Elf_Internal_Rela *internal_relocs,
7755 Elf_Internal_Rela *irelend,
7756 unsigned char reloc_type)
7758 Elf_Internal_Rela *rel_t;
7761 rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
7763 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
7765 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
7766 && rel_t->r_addend == reloc->r_addend)
7771 for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
7773 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
7775 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
7776 && rel_t->r_addend == reloc->r_addend)
7784 typedef struct nds32_elf_blank nds32_elf_blank_t;
7785 struct nds32_elf_blank
7787 /* Where the blank begins. */
7789 /* The size of the blank. */
7791 /* The accumulative size before this blank. */
7793 nds32_elf_blank_t *next;
7794 nds32_elf_blank_t *prev;
7797 static nds32_elf_blank_t *blank_free_list = NULL;
7799 static nds32_elf_blank_t *
7800 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
7802 nds32_elf_blank_t *blank_t;
7804 if (blank_free_list)
7806 blank_t = blank_free_list;
7807 blank_free_list = blank_free_list->next;
7810 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
7812 if (blank_t == NULL)
7815 blank_t->offset = offset_p;
7816 blank_t->size = size_p;
7817 blank_t->total_size = 0;
7818 blank_t->next = NULL;
7819 blank_t->prev = NULL;
7825 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
7827 if (blank_free_list)
7829 blank_free_list->prev = blank_p;
7830 blank_p->next = blank_free_list;
7833 blank_p->next = NULL;
7835 blank_p->prev = NULL;
7836 blank_free_list = blank_p;
7840 clean_nds32_elf_blank (void)
7842 nds32_elf_blank_t *blank_t;
7844 while (blank_free_list)
7846 blank_t = blank_free_list;
7847 blank_free_list = blank_free_list->next;
7852 static nds32_elf_blank_t *
7853 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
7855 nds32_elf_blank_t *blank_t;
7861 while (blank_t && addr < blank_t->offset)
7862 blank_t = blank_t->prev;
7863 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
7864 blank_t = blank_t->next;
7870 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
7873 nds32_elf_blank_t *blank_t;
7875 blank_t = search_nds32_elf_blank (*blank_p, addr);
7882 if (addr < blank_t->offset + blank_t->size)
7883 return blank_t->total_size + (addr - blank_t->offset);
7885 return blank_t->total_size + blank_t->size;
7889 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
7891 nds32_elf_blank_t *blank_t, *blank_t2;
7895 *blank_p = create_nds32_elf_blank (addr, len);
7896 return *blank_p ? TRUE : FALSE;
7899 blank_t = search_nds32_elf_blank (*blank_p, addr);
7901 if (blank_t == NULL)
7903 blank_t = create_nds32_elf_blank (addr, len);
7906 while ((*blank_p)->prev != NULL)
7907 *blank_p = (*blank_p)->prev;
7908 blank_t->next = *blank_p;
7909 (*blank_p)->prev = blank_t;
7910 (*blank_p) = blank_t;
7914 if (addr < blank_t->offset + blank_t->size)
7916 if (addr > blank_t->offset + blank_t->size)
7917 blank_t->size = addr - blank_t->offset;
7921 blank_t2 = create_nds32_elf_blank (addr, len);
7926 blank_t->next->prev = blank_t2;
7927 blank_t2->next = blank_t->next;
7929 blank_t2->prev = blank_t;
7930 blank_t->next = blank_t2;
7931 *blank_p = blank_t2;
7938 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
7941 nds32_elf_blank_t *blank_t;
7943 if (!insert_nds32_elf_blank (blank_p, addr, len))
7950 blank_t->total_size = 0;
7951 blank_t = blank_t->next;
7956 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
7957 blank_t = blank_t->next;
7964 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
7966 nds32_elf_blank_t *blank_t;
7967 bfd_vma total_size = 0;
7973 while (blank_t->prev)
7974 blank_t = blank_t->prev;
7977 blank_t->total_size = total_size;
7978 total_size += blank_t->size;
7979 blank_t = blank_t->next;
7984 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
7985 nds32_elf_blank_t *blank_p)
7987 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
7988 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
7989 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
7990 unsigned int sec_shndx; /* The section the be relaxed. */
7991 bfd_byte *contents; /* Contents data of iterating section. */
7992 Elf_Internal_Rela *internal_relocs;
7993 Elf_Internal_Rela *irel;
7994 Elf_Internal_Rela *irelend;
7995 struct elf_link_hash_entry **sym_hashes;
7996 struct elf_link_hash_entry **end_hashes;
7997 unsigned int symcount;
7999 nds32_elf_blank_t *blank_t;
8000 nds32_elf_blank_t *blank_t2;
8001 nds32_elf_blank_t *blank_head;
8003 blank_head = blank_t = blank_p;
8004 while (blank_head->prev != NULL)
8005 blank_head = blank_head->prev;
8006 while (blank_t->next != NULL)
8007 blank_t = blank_t->next;
8009 if (blank_t->offset + blank_t->size <= sec->size)
8011 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
8012 blank_t->next->prev = blank_t;
8014 if (blank_head->offset > 0)
8016 blank_head->prev = create_nds32_elf_blank (0, 0);
8017 blank_head->prev->next = blank_head;
8018 blank_head = blank_head->prev;
8021 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8023 /* The deletion must stop at the next ALIGN reloc for an alignment
8024 power larger than the number of bytes we are deleting. */
8026 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8027 if (!nds32_get_local_syms (abfd, sec, &isym))
8032 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8033 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
8034 symtab_hdr->contents = (bfd_byte *) isym;
8037 if (isym == NULL || symtab_hdr->sh_info == 0)
8040 blank_t = blank_head;
8041 calc_nds32_blank_total (blank_head);
8043 for (sect = abfd->sections; sect != NULL; sect = sect->next)
8045 /* Adjust all the relocs. */
8047 /* Relocations MUST be kept in memory, because relaxation adjust them. */
8048 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
8049 TRUE /* keep_memory */);
8050 irelend = internal_relocs + sect->reloc_count;
8052 blank_t = blank_head;
8053 blank_t2 = blank_head;
8055 if (!(sect->flags & SEC_RELOC))
8058 nds32_get_section_contents (abfd, sect, &contents);
8060 for (irel = internal_relocs; irel < irelend; irel++)
8064 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
8065 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
8066 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8068 unsigned long val = 0;
8069 unsigned long before, between;
8071 switch (ELF32_R_TYPE (irel->r_info))
8074 val = bfd_get_8 (abfd, contents + irel->r_offset);
8076 case R_NDS32_DIFF16:
8077 val = bfd_get_16 (abfd, contents + irel->r_offset);
8079 case R_NDS32_DIFF32:
8080 val = bfd_get_32 (abfd, contents + irel->r_offset);
8087 0 |encoded in location|
8088 |------------|-------------------|---------
8090 -- before ---| *****************
8091 --------------------- between ---|
8093 We only care how much data are relax between DIFF, marked as ***. */
8095 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8096 between = get_nds32_elf_blank_total (&blank_t, irel->r_addend + val, 0);
8097 if (between == before)
8098 goto done_adjust_diff;
8100 switch (ELF32_R_TYPE (irel->r_info))
8103 bfd_put_8 (abfd, val - (between - before), contents + irel->r_offset);
8105 case R_NDS32_DIFF16:
8106 bfd_put_16 (abfd, val - (between - before), contents + irel->r_offset);
8108 case R_NDS32_DIFF32:
8109 bfd_put_32 (abfd, val - (between - before), contents + irel->r_offset);
8113 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
8114 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8117 unsigned int len = 0;
8118 unsigned long before, between;
8121 val = read_unsigned_leb128 (abfd, contents + irel->r_offset, &len);
8123 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8124 between = get_nds32_elf_blank_total (&blank_t, irel->r_addend + val, 0);
8125 if (between == before)
8126 goto done_adjust_diff;
8128 p = contents + irel->r_offset;
8130 memset (p, 0x80, len);
8132 p = write_uleb128 (p, val - (between - before)) - 1;
8140 raddr = irel->r_offset;
8141 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2, irel->r_offset, 1);
8143 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
8145 if (blank_t2 && blank_t2->next
8146 && (blank_t2->offset > raddr || blank_t2->next->offset <= raddr))
8147 (*_bfd_error_handler) (_("%B: %s\n"), abfd,
8148 "Error: search_nds32_elf_blank reports wrong node");
8150 /* Mark reloc in deleted portion as NONE.
8151 For some relocs like R_NDS32_LABEL that doesn't modify the
8152 content in the section. R_NDS32_LABEL doesn't belong to the
8153 instruction in the section, so we should preserve it. */
8154 if (raddr >= blank_t2->offset
8155 && raddr < blank_t2->offset + blank_t2->size
8156 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
8157 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
8158 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
8159 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
8160 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
8161 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
8163 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
8169 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
8170 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
8171 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
8174 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
8175 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
8176 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
8178 if (irel->r_addend <= sec->size)
8180 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
8185 /* Adjust the local symbols defined in this section. */
8186 blank_t = blank_head;
8187 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
8189 if (isym->st_shndx == sec_shndx)
8191 if (isym->st_value <= sec->size)
8194 bfd_vma orig_addr = isym->st_value;
8196 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
8197 isym->st_value -= ahead;
8199 /* Adjust function size. */
8200 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC && isym->st_size > 0)
8201 isym->st_size -= get_nds32_elf_blank_total
8202 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
8207 /* Now adjust the global symbols defined in this section. */
8208 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
8209 - symtab_hdr->sh_info);
8210 sym_hashes = elf_sym_hashes (abfd);
8211 end_hashes = sym_hashes + symcount;
8212 blank_t = blank_head;
8213 for (; sym_hashes < end_hashes; sym_hashes++)
8215 struct elf_link_hash_entry *sym_hash = *sym_hashes;
8217 if ((sym_hash->root.type == bfd_link_hash_defined
8218 || sym_hash->root.type == bfd_link_hash_defweak)
8219 && sym_hash->root.u.def.section == sec)
8221 if (sym_hash->root.u.def.value <= sec->size)
8224 bfd_vma orig_addr = sym_hash->root.u.def.value;
8226 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
8227 sym_hash->root.u.def.value -= ahead;
8229 /* Adjust function size. */
8230 if (sym_hash->type == STT_FUNC)
8231 sym_hash->size -= get_nds32_elf_blank_total
8232 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
8238 contents = elf_section_data (sec)->this_hdr.contents;
8239 blank_t = blank_head;
8240 while (blank_t->next)
8242 /* Actually delete the bytes. */
8244 /* If current blank is the last blank overlap with current section,
8245 go to finish process. */
8246 if (sec->size <= (blank_t->next->offset))
8249 memmove (contents + blank_t->offset - blank_t->total_size,
8250 contents + blank_t->offset + blank_t->size,
8251 blank_t->next->offset - (blank_t->offset + blank_t->size));
8253 blank_t = blank_t->next;
8256 if (sec->size > (blank_t->offset + blank_t->size))
8258 /* There are remaining code between blank and section boundary.
8259 Move the remaining code to appropriate location. */
8260 memmove (contents + blank_t->offset - blank_t->total_size,
8261 contents + blank_t->offset + blank_t->size,
8262 sec->size - (blank_t->offset + blank_t->size));
8263 sec->size -= blank_t->total_size + blank_t->size;
8266 /* This blank is not entirely included in the section,
8267 reduce the section size by only part of the blank size. */
8268 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
8272 blank_t = blank_head;
8273 blank_head = blank_head->next;
8274 remove_nds32_elf_blank (blank_t);
8280 /* Get the contents of a section. */
8283 nds32_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents_p)
8285 /* Get the section contents. */
8286 if (elf_section_data (sec)->this_hdr.contents != NULL)
8287 *contents_p = elf_section_data (sec)->this_hdr.contents;
8290 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
8292 elf_section_data (sec)->this_hdr.contents = *contents_p;
8298 /* Get the contents of the internal symbol of abfd. */
8301 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
8302 Elf_Internal_Sym **isymbuf_p)
8304 Elf_Internal_Shdr *symtab_hdr;
8305 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8307 /* Read this BFD's local symbols if we haven't done so already. */
8308 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
8310 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
8311 if (*isymbuf_p == NULL)
8313 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8314 symtab_hdr->sh_info, 0,
8316 if (*isymbuf_p == NULL)
8320 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
8325 /* Range of small data. */
8326 static bfd_vma sdata_range[5][2];
8327 static bfd_vma const sdata_init_range[5] = { 0x2000, 0x4000, 0x8000, 0x10000, 0x40000 };
8330 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
8331 bfd_byte *contents, bfd_vma addr)
8333 unsigned long insn = bfd_getb32 (contents + addr);
8335 if (insn & 0x80000000)
8341 /* Set the gp relax range. We have to measure the safe range
8342 to do gp relaxation. */
8345 relax_range_measurement (bfd *abfd)
8347 asection *sec_f, *sec_b;
8348 /* For upper bound. */
8349 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
8352 static int decide_relax_range = 0;
8355 if (decide_relax_range)
8357 decide_relax_range = 1;
8359 if (sda_rela_sec == NULL)
8361 /* Since there is no data sections, we assume the range is page size. */
8362 for (i = 0; i < 5; i++)
8364 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
8365 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
8370 /* Get the biggest alignment power after the gp located section. */
8371 sec_f = sda_rela_sec->output_section;
8372 sec_b = sec_f->next;
8374 while (sec_b != NULL)
8376 if ((unsigned)(1 << sec_b->alignment_power) > align)
8377 align = (1 << sec_b->alignment_power);
8378 sec_b = sec_b->next;
8381 /* I guess we can not determine the section before
8382 gp located section, so we assume the align is max page size. */
8383 for (i = 0; i < 5; i++)
8385 init_range = sdata_init_range[i];
8386 sdata_range[i][1] = init_range - align;
8387 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
8388 sdata_range[i][0] = init_range - maxpgsz;
8389 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
8393 /* These are macros used to check flags encoded in r_addend.
8394 They are only used by nds32_elf_relax_section (). */
8395 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
8396 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
8397 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
8398 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
8401 nds32_elf_relax_section (bfd *abfd, asection *sec,
8402 struct bfd_link_info *link_info, bfd_boolean *again)
8404 nds32_elf_blank_t *relax_blank_list = NULL;
8405 Elf_Internal_Shdr *symtab_hdr;
8406 Elf_Internal_Rela *internal_relocs;
8407 Elf_Internal_Rela *irel;
8408 Elf_Internal_Rela *irelend;
8409 Elf_Internal_Sym *isymbuf = NULL;
8410 bfd_byte *contents = NULL;
8411 bfd_boolean result = TRUE;
8413 int optimize_for_space ATTRIBUTE_UNUSED = 0;
8414 int optimize_for_space_no_align ATTRIBUTE_UNUSED = 0;
8421 /* Target dependnet option. */
8422 struct elf_nds32_link_hash_table *table;
8423 int load_store_relax;
8426 relax_blank_list = NULL;
8430 /* Nothing to do for
8431 * relocatable link or
8432 * non-relocatable section or
8433 * non-code section or
8435 * no reloc entry. */
8436 if (link_info->relocatable
8437 || (sec->flags & SEC_RELOC) == 0
8438 || (sec->flags & SEC_EXCLUDE) == 1
8439 || (sec->flags & SEC_CODE) == 0
8443 /* 09.12.11 Workaround. */
8444 /* We have to adjust align for R_NDS32_LABEL if needed.
8445 The adjust approach only can fix 2-byte align once. */
8446 if (sec->alignment_power > 2)
8448 (*_bfd_error_handler)
8449 (_("%B(%A): warning: relax is suppressed for sections "
8450 "of alignment %d-bytes > 4-byte."),
8451 abfd, sec, sec->alignment_power);
8455 /* The optimization type to do. */
8457 table = nds32_elf_hash_table (link_info);
8458 relax_round = table->relax_round;
8459 switch (relax_round)
8461 case NDS32_RELAX_JUMP_IFC_ROUND:
8462 /* Here is the entrance of ifc jump relaxation. */
8463 if (!nds32_elf_ifc_calc (link_info, abfd, sec))
8467 case NDS32_RELAX_EX9_BUILD_ROUND:
8468 /* Here is the entrance of ex9 relaxation. There are two pass of
8469 ex9 relaxation. The one is to traverse all instructions and build
8470 the hash table. The other one is to compare instructions and replace
8472 if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
8476 case NDS32_RELAX_EX9_REPLACE_ROUND:
8477 if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
8482 if (sec->reloc_count == 0)
8487 /* The begining of general relaxation. */
8489 if (is_SDA_BASE_set == 0)
8492 is_SDA_BASE_set = 1;
8493 nds32_elf_final_sda_base (sec->output_section->owner, link_info, &gp, FALSE);
8494 relax_range_measurement (abfd);
8497 if (is_ITB_BASE_set == 0)
8499 /* Set the _ITB_BASE_. */
8500 if (!nds32_elf_ex9_itb_base (link_info))
8502 (*_bfd_error_handler) (_("%B: error: Cannot set _ITB_BASE_"), abfd);
8503 bfd_set_error (bfd_error_bad_value);
8507 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8508 /* Relocations MUST be kept in memory, because relaxation adjust them. */
8509 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8510 TRUE /* keep_memory */);
8511 if (internal_relocs == NULL)
8514 irelend = internal_relocs + sec->reloc_count;
8516 find_relocs_at_address (internal_relocs, internal_relocs, irelend,
8517 R_NDS32_RELAX_ENTRY);
8518 /* If 31th bit of addend of R_NDS32_RELAX_ENTRY is set,
8519 this section is already relaxed. */
8520 if (irel == irelend)
8523 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
8525 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
8528 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
8531 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
8532 optimize_for_space = 1;
8536 load_store_relax = table->load_store_relax;
8538 /* Get symbol table and section content. */
8539 if (!nds32_get_section_contents (abfd, sec, &contents)
8540 || !nds32_get_local_syms (abfd, sec, &isymbuf))
8543 /* Do relax loop only when finalize is not done.
8544 Take care of relaxable relocs except INSN16. */
8545 for (irel = internal_relocs; irel < irelend; irel++)
8548 unsigned long comp_insn = 0;
8549 unsigned short comp_insn16 = 0;
8550 unsigned long i_mask = 0xffffffff;
8551 int seq_len; /* Original length of instruction sequence. */
8552 int insn_len = 0; /* Final length of instruction sequence. */
8553 int convertible; /* 1st insn convertible. */
8554 int insn16_on; /* 16-bit on/off. */
8555 Elf_Internal_Rela *hi_irelfn = NULL;
8556 Elf_Internal_Rela *lo_irelfn = NULL;
8557 Elf_Internal_Rela *i1_irelfn = NULL;
8558 Elf_Internal_Rela *i2_irelfn = NULL;
8559 Elf_Internal_Rela *cond_irelfn = NULL;
8562 bfd_signed_vma foff;
8563 unsigned long reloc = R_NDS32_NONE;
8566 int pic_ext_target = 0;
8567 bfd_vma access_addr = 0;
8568 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
8572 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
8573 && (irel->r_addend & 0x1f) >= 2)
8577 R_NDS32_LONGCALL1 53
8578 R_NDS32_LONGCALL2 54
8579 R_NDS32_LONGCALL3 55
8580 R_NDS32_LONGJUMP1 56
8581 R_NDS32_LONGJUMP2 57
8582 R_NDS32_LONGJUMP3 58
8583 R_NDS32_LOADSTORE 59 */
8584 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
8585 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
8587 seq_len = GET_SEQ_LEN (irel->r_addend);
8588 insn_opt = IS_OPTIMIZE (irel->r_addend);
8589 convertible = IS_1ST_CONVERT (irel->r_addend);
8590 insn16_on = IS_16BIT_ON (irel->r_addend);
8591 laddr = irel->r_offset;
8594 R_NDS32_LO12S0_RELA 30
8595 R_NDS32_LO12S1_RELA 29
8596 R_NDS32_LO12S2_RELA 28
8597 R_NDS32_LO12S2_SP_RELA 71
8598 R_NDS32_LO12S2_DP_RELA 70
8600 R_NDS32_GOTOFF_LO12 50
8601 R_NDS32_PLTREL_LO12 65
8602 R_NDS32_PLT_GOTREL_LO12 67
8603 R_NDS32_GOT_SUFF 193
8604 R_NDS32_GOTOFF_SUFF 194
8605 R_NDS32_PLT_GOT_SUFF 195
8606 R_NDS32_MULCALL_SUFF 196
8608 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
8609 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
8610 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
8611 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
8612 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
8613 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
8614 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
8615 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
8616 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
8617 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
8618 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTBLOCK
8619 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA)
8622 insn_opt = IS_OPTIMIZE (irel->r_addend) > 0;
8625 laddr = irel->r_offset;
8632 if (laddr + seq_len > (bfd_vma) sec->size)
8637 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
8638 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP3)
8640 for (i1_irelfn = irel;
8641 i1_irelfn < irelend && i1_irelfn->r_offset < (laddr + seq_len - 4);
8646 i1_irelfn < irelend && i1_irelfn->r_offset == (laddr + seq_len - 4);
8648 if (ELF32_R_TYPE (i1_irelfn->r_info) == R_NDS32_INSN16)
8656 if (pass_check == 0)
8658 reloc_howto_type *howto =
8659 bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
8663 (*_bfd_error_handler)
8664 ("%B: warning: %s points to unrecognized insns at 0x%lx.",
8665 abfd, s, (long) irel->r_offset);
8671 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LONGCALL1)
8673 /* There are 3 variations for LONGCALL1
8674 case 4-4-2; 16-bit on, optimize off or optimize for space
8675 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8676 ori ta, ta, lo12(symbol) ; LO12S0
8679 case 4-4-4; 16-bit off, optimize don't care
8680 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8681 ori ta, ta, lo12(symbol) ; LO12S0
8684 case 4-4-4; 16-bit on, optimize for speed
8685 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8686 ori ta, ta, lo12(symbol) ; LO12S0
8688 Check code for -mlong-calls output. */
8690 /* Get the reloc for the address from which the register is
8691 being loaded. This reloc will tell us which function is
8692 actually being called. */
8693 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8694 R_NDS32_HI20_RELA, laddr);
8695 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8696 R_NDS32_LO12S0_ORI_RELA,
8700 if (hi_irelfn == irelend || lo_irelfn == irelend)
8702 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8703 R_NDS32_20_RELA, laddr);
8705 if (hi_irelfn == irelend)
8707 (*_bfd_error_handler)
8708 ("%B: warning: R_NDS32_LONGCALL1 points to unrecognized reloc at 0x%lx.",
8709 abfd, (long) irel->r_offset);
8714 i1_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8718 /* Get the value of the symbol referred to by the reloc. */
8719 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
8722 /* This condition only happened when symbol is undefined. */
8723 if (pic_ext_target || foff == 0)
8725 if (foff < -0x1000000 || foff >= 0x1000000)
8731 jal symbol ; 25_PCREL */
8732 /* For simplicity of coding, we are going to modify the section
8733 contents, the section relocs, and the BFD symbol table. We
8734 must tell the rest of the code not to free up this
8735 information. It would be possible to instead create a table
8736 of changes which have to be made, as is done in coff-mips.c;
8737 that would be more work, but would require less memory when
8738 the linker is run. */
8740 /* Replace the long call with a jal. */
8741 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8742 R_NDS32_25_PCREL_RELA);
8743 irel->r_addend = hi_irelfn->r_addend;
8745 /* We don't resolve this here but resolve it in relocate_section. */
8748 bfd_putb32 (insn, contents + irel->r_offset);
8750 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
8752 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
8754 if (i1_irelfn != irelend)
8757 && (i1_irelfn->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8759 /* The instruction pointed by R_NDS32_INSN16 is already
8760 turned into 16-bit instruction, so the total length of
8761 this sequence is decreased by 2. */
8762 seq_len = seq_len - 2;
8765 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
8769 insn16 = NDS32_NOP16;
8770 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
8772 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
8774 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
8778 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LONGCALL2)
8780 /* bltz rt, $1 ; LONGCALL2
8781 jal symbol ; 25_FIXED
8783 /* Get the reloc for the address from which the register is
8784 being loaded. This reloc will tell us which function is
8785 actually being called. */
8787 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8788 R_NDS32_25_PCREL_RELA, laddr + 4);
8790 if (i1_irelfn == irelend)
8792 (*_bfd_error_handler)
8793 ("%B: warning: R_NDS32_LONGCALL2 points to unrecognized reloc at 0x%lx.",
8794 abfd, (long) irel->r_offset);
8799 insn = bfd_getb32 (contents + laddr);
8801 /* Get the value of the symbol referred to by the reloc. */
8803 calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
8807 if (foff < -0x10000 - 4 || foff >= 0x10000 - 4)
8808 /* After all that work, we can't shorten this function call. */
8811 /* Relax to bgezal rt, label ; 17_PCREL
8812 or bltzal rt, label ; 17_PCREL */
8814 /* Convert to complimentary conditional call. */
8818 /* For simplicity of coding, we are going to modify the section
8819 contents, the section relocs, and the BFD symbol table. We
8820 must tell the rest of the code not to free up this
8821 information. It would be possible to instead create a table
8822 of changes which have to be made, as is done in coff-mips.c;
8823 that would be more work, but would require less memory when
8824 the linker is run. */
8826 /* Replace the long call with a bgezal. */
8828 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
8829 R_NDS32_17_PCREL_RELA);
8831 bfd_putb32 (insn, contents + irel->r_offset);
8834 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
8836 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8837 R_NDS32_17_PCREL_RELA, laddr);
8838 if (cond_irelfn != irelend)
8840 cond_irelfn->r_info =
8841 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
8842 R_NDS32_17_PCREL_RELA);
8843 cond_irelfn->r_addend = i1_irelfn->r_addend;
8847 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LONGCALL3)
8849 /* There are 3 variations for LONGCALL3
8850 case 4-4-4-2; 16-bit on, optimize off or optimize for space
8851 bltz rt, $1 ; LONGCALL3
8852 sethi ta, hi20(symbol) ; HI20
8853 ori ta, ta, lo12(symbol) ; LO12S0
8857 case 4-4-4-4; 16-bit off, optimize don't care
8858 bltz rt, $1 ; LONGCALL3
8859 sethi ta, hi20(symbol) ; HI20
8860 ori ta, ta, lo12(symbol) ; LO12S0
8864 case 4-4-4-4; 16-bit on, optimize for speed
8865 bltz rt, $1 ; LONGCALL3
8866 sethi ta, hi20(symbol) ; HI20
8867 ori ta, ta, lo12(symbol) ; LO12S0
8871 /* Get the reloc for the address from which the register is
8872 being loaded. This reloc will tell us which function is
8873 actually being called. */
8875 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8876 R_NDS32_HI20_RELA, laddr + 4);
8878 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8879 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
8882 if (hi_irelfn == irelend || lo_irelfn == irelend)
8886 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8887 R_NDS32_20_RELA, laddr + 4);
8889 if (hi_irelfn == irelend)
8891 (*_bfd_error_handler)
8892 ("%B: warning: R_NDS32_LONGCALL3 points to unrecognized reloc at 0x%lx.",
8893 abfd, (long) irel->r_offset);
8899 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8900 R_NDS32_INSN16, laddr + i2_offset);
8902 /* Get the value of the symbol referred to by the reloc. */
8904 calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
8906 if (pic_ext_target || foff == 0)
8908 if (foff < -0x1000000 || foff >= 0x1000000)
8911 insn = bfd_getb32 (contents + laddr);
8912 if (foff >= -0x10000 - 4 && foff < 0x10000 - 4)
8914 /* Relax to bgezal rt, label ; 17_PCREL
8915 or bltzal rt, label ; 17_PCREL */
8917 /* Convert to complimentary conditional call. */
8920 bfd_putb32 (insn, contents + irel->r_offset);
8924 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8927 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
8929 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
8930 if (i2_irelfn != irelend)
8933 && (i2_irelfn->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8935 /* The instruction pointed by R_NDS32_INSN16 is already
8936 turned into 16-bit instruction, so the total length
8937 of this sequence is decreased by 2. */
8938 seq_len = seq_len - 2;
8941 ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
8945 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8946 R_NDS32_17_PCREL_RELA, laddr);
8947 if (cond_irelfn != irelend)
8949 cond_irelfn->r_info =
8950 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8951 R_NDS32_17_PCREL_RELA);
8952 cond_irelfn->r_addend = hi_irelfn->r_addend;
8957 insn16 = NDS32_NOP16;
8958 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
8960 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8962 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
8968 /* Relax to the following instruction sequence
8969 bltz rt, $1 ; LONGCALL2
8970 jal symbol ; 25_PCREL
8973 insn = (insn & 0xffff0000) | 4;
8974 bfd_putb32 (insn, contents + irel->r_offset);
8975 /* This relax is incorrect. Review, fix and test it.
8976 Check 6a726f0f for the oringnal code. */
8979 bfd_putb32 (insn, contents + irel->r_offset + 4);
8982 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8983 R_NDS32_25_PCREL_RELA);
8985 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
8988 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
8989 if (i2_irelfn != irelend)
8992 ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
8997 insn16 = NDS32_NOP16;
8998 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
9000 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9002 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9007 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LONGJUMP1)
9009 /* There are 3 variations for LONGJUMP1
9010 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9011 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9012 ori ta, ta, lo12(symbol) ; LO12S0
9015 case 4-4-4; 16-bit off, optimize don't care
9016 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9017 ori ta, ta, lo12(symbol) ; LO12S0
9020 case 4-4-4; 16-bit on, optimize for speed
9021 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9022 ori ta, ta, lo12(symbol) ; LO12S0
9025 /* Get the reloc for the address from which the register is
9026 being loaded. This reloc will tell us which function is
9027 actually being called. */
9029 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9030 R_NDS32_HI20_RELA, laddr);
9032 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9033 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9036 if (hi_irelfn == irelend || lo_irelfn == irelend)
9039 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9040 R_NDS32_20_RELA, laddr);
9043 if (hi_irelfn == irelend)
9045 (*_bfd_error_handler)
9046 ("%B: warning: R_NDS32_LONGJUMP1 points to unrecognized reloc at 0x%lx.",
9047 abfd, (long) irel->r_offset);
9054 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9055 R_NDS32_INSN16, laddr + i1_offset);
9057 /* Get the value of the symbol referred to by the reloc. */
9059 calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9061 if (pic_ext_target || foff == 0)
9064 if (foff >= -0x1000000 && foff < 0x1000000)
9067 if (!insn_opt && insn16_on && foff >= -0x100 && foff < 0x100
9070 /* 16-bit on, but not optimized for speed. */
9071 reloc = R_NDS32_9_PCREL_RELA;
9073 bfd_putb16 (insn16, contents + irel->r_offset);
9078 reloc = R_NDS32_25_PCREL_RELA;
9080 bfd_putb32 (insn, contents + irel->r_offset);
9089 /* For simplicity of coding, we are going to modify the section
9090 contents, the section relocs, and the BFD symbol table. We
9091 must tell the rest of the code not to free up this
9092 information. It would be possible to instead create a table
9093 of changes which have to be made, as is done in coff-mips.c;
9094 that would be more work, but would require less memory when
9095 the linker is run. */
9100 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9105 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9108 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9110 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9111 if (i1_irelfn != irelend)
9114 && (i1_irelfn->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
9116 /* The instruction pointed by R_NDS32_INSN16 is already
9117 turned into 16-bit instruction, so the total length
9118 of this sequence is decreased by 2. */
9119 seq_len = seq_len - 2;
9120 i1_irelfn->r_addend = 0;
9123 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9126 if ((seq_len & 0x2) && ((insn_len & 2) == 0))
9128 insn16 = NDS32_NOP16;
9129 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
9131 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9133 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9137 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LONGJUMP2)
9139 /* There are 3 variations for LONGJUMP2
9140 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize for space
9141 bnes38 rt, ra, $1 ; LONGJUMP2
9145 case 4-4; 1st insn not convertible
9146 bne rt, ra, $1 ; LONGJUMP2
9150 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9151 bne rt, ra, $1 ; LONGJUMP2/INSN16
9155 /* Get the reloc for the address from which the register is
9156 being loaded. This reloc will tell us which function is
9157 actually being called. */
9158 enum elf_nds32_reloc_type checked_types[] =
9159 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9160 hi_off = (seq_len == 6) ? 2 : 4;
9162 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9163 R_NDS32_25_PCREL_RELA,
9166 for (i = 0; i < 2; i++)
9169 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9170 checked_types[i], laddr);
9171 if (cond_irelfn != irelend)
9174 if (i2_irelfn == irelend)
9176 (*_bfd_error_handler)
9177 ("%B: warning: R_NDS32_LONGJUMP2 points to unrecognized reloc at 0x%lx.",
9178 abfd, (long) irel->r_offset);
9184 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9185 R_NDS32_INSN16, laddr);
9187 if (i1_irelfn != irelend && !insn_opt
9188 && (i1_irelfn->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
9190 /* The instruction pointed by R_NDS32_INSN16 is already turned
9191 into 16-bit instruction, so the total length of this sequence
9192 is decreased by 2. */
9193 seq_len = seq_len - 2;
9199 1. range is outside of +/-256 bytes
9200 2. optimize is on with INSN16
9201 3. optimize is off */
9203 insn = bfd_getb32 (contents + laddr);
9206 /* 16-bit is off, can't convert to 16-bit. */
9209 else if (N32_OP6 (insn) == N32_OP6_BR1)
9211 /* beqs label ; 15_PCREL (INSN16) */
9212 comp_insn = (insn ^ 0x4000) & 0xffffc000;
9213 i_mask = 0xffffc000;
9214 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
9216 /* Insn can be contracted to 16-bit. */
9218 (insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
9219 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9223 /* No conversion. */
9229 comp_insn = (insn ^ 0x10000) & 0xffffc000;
9230 i_mask = 0xffff0000;
9231 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
9232 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
9234 if (N32_IS_RT3 (insn))
9236 /* Insn can be contracted to 16-bit. */
9238 (insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
9239 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9241 else if (N32_RT5 (insn) == REG_R15)
9243 /* Insn can be contracted to 16-bit. */
9245 (insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
9249 /* No conversion. */
9255 /* No conversion. */
9262 /* First instruction is 16-bit. */
9264 insn16 = bfd_getb16 (contents + laddr);
9265 switch ((insn16 & 0xf000) >> 12)
9268 /* beqz38 or bnez38 */
9269 comp_insn = (insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
9270 comp_insn |= ((insn16 & 0x0700) >> 8) << 20;
9271 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9272 insn = (insn16 & 0x0800) ? INSN_BEQZ : INSN_BNEZ;
9273 insn |= ((insn16 & 0x0700) >> 8) << 20;
9274 i_mask = 0xffff0000;
9278 /* beqs38 or bnes38 */
9279 comp_insn = (insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
9280 comp_insn |= (((insn16 & 0x0700) >> 8) << 20)
9282 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9283 insn = (insn16 & 0x0800) ? INSN_BEQ : INSN_BNE;
9284 insn |= (((insn16 & 0x0700) >> 8) << 20) | (REG_R5 << 15);
9285 i_mask = 0xffffc000;
9289 /* beqzS8 or bnezS8 */
9290 comp_insn = (insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
9291 comp_insn |= REG_R15 << 20;
9292 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
9293 insn = (insn16 & 0x0100) ? INSN_BEQZ : INSN_BNEZ;
9294 insn |= REG_R15 << 20;
9295 i_mask = 0xffff0000;
9305 /* Get the value of the symbol referred to by the reloc. */
9307 calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
9309 if (pic_ext_target || foff == 0)
9313 && foff >= -0x100 - insn_off && foff < 0x100 - insn_off)
9315 if (insn_opt || seq_len == 8)
9317 /* Don't convert it to 16-bit now, keep this as relaxable for
9318 ``label reloc; INSN16''. */
9320 /* Save comp_insn32 to buffer. */
9322 bfd_putb32 (insn, contents + irel->r_offset);
9324 reloc = (N32_OP6 (comp_insn) == N32_OP6_BR1) ?
9325 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9327 if (cond_irelfn != irelend)
9329 cond_irelfn->r_info =
9330 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9332 cond_irelfn->r_addend = 0;
9337 /* Not optimize for speed; convert sequence to 16-bit. */
9339 /* Save comp_insn16 to buffer. */
9340 insn16 = comp_insn16;
9341 bfd_putb16 (insn16, contents + irel->r_offset);
9343 reloc = R_NDS32_9_PCREL_RELA;
9346 /* Change relocs. */
9347 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
9348 irel->r_addend = i2_irelfn->r_addend;
9350 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9353 else if (N32_OP6 (insn) == N32_OP6_BR1
9354 && (foff >= -0x4000 - insn_off && foff < 0x4000 - insn_off))
9356 /* beqs label ; 15_PCREL */
9358 bfd_putb32 (insn, contents + irel->r_offset);
9361 /* Change relocs. */
9362 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9363 R_NDS32_15_PCREL_RELA);
9364 irel->r_addend = i2_irelfn->r_addend;
9365 if (i1_irelfn != irelend)
9366 i1_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9371 insn16 = NDS32_NOP16;
9372 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
9374 ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9376 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9380 else if (N32_OP6 (insn) == N32_OP6_BR2 && foff >= -0x10000 && foff < 0x10000)
9382 /* beqz label ; 17_PCREL */
9384 bfd_putb32 (insn, contents + irel->r_offset);
9387 /* Change relocs. */
9388 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9389 R_NDS32_17_PCREL_RELA);
9390 irel->r_addend = i2_irelfn->r_addend;
9391 if (i1_irelfn != irelend)
9392 i1_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9396 insn16 = NDS32_NOP16;
9397 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
9398 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9400 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9407 if (cond_irelfn != irelend)
9408 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9412 /* For simplicity of coding, we are going to modify the section
9413 contents, the section relocs, and the BFD symbol table. We
9414 must tell the rest of the code not to free up this
9415 information. It would be possible to instead create a table
9416 of changes which have to be made, as is done in coff-mips.c;
9417 that would be more work, but would require less memory when
9418 the linker is run. */
9420 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LONGJUMP3)
9422 int reloc_off = 0, cond_removed = 0;
9423 /* Get the reloc for the address from which the register is
9424 being loaded. This reloc will tell us which function is
9425 actually being called. */
9426 enum elf_nds32_reloc_type checked_types[] =
9427 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9429 /* There are 5 variations for LONGJUMP3
9430 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
9431 optimize off or optimize for space
9432 bnes38 rt, ra, $1 ; LONGJUMP3
9433 sethi ta, hi20(symbol) ; HI20
9434 ori ta, ta, lo12(symbol) ; LO12S0
9438 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
9439 bnes38 rt, ra, $1 ; LONGJUMP3
9440 sethi ta, hi20(symbol) ; HI20
9441 ori ta, ta, lo12(symbol) ; LO12S0
9445 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
9446 optimize off or optimize for space
9447 bne rt, ra, $1 ; LONGJUMP3
9448 sethi ta, hi20(symbol) ; HI20
9449 ori ta, ta, lo12(symbol) ; LO12S0
9453 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
9454 16-bit off if no INSN16
9455 bne rt, ra, $1 ; LONGJUMP3
9456 sethi ta, hi20(symbol) ; HI20
9457 ori ta, ta, lo12(symbol) ; LO12S0
9461 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
9462 16-bit off if no INSN16
9463 bne rt, ra, $1 ; LONGJUMP3
9464 sethi ta, hi20(symbol) ; HI20
9465 ori ta, ta, lo12(symbol) ; LO12S0
9481 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9484 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9485 R_NDS32_LO12S0_ORI_RELA,
9486 laddr + hi_off + 4);
9489 if (hi_irelfn == irelend || lo_irelfn == irelend)
9491 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9496 if (hi_irelfn == irelend)
9498 (*_bfd_error_handler)
9499 ("%B: warning: R_NDS32_LONGJUMP3 points to unrecognized reloc at 0x%lx.",
9500 abfd, (long) irel->r_offset);
9506 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9508 laddr + hi_off + i2_offset);
9510 /* Get the value of the symbol referred to by the reloc. */
9512 calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9514 if (pic_ext_target || foff == 0)
9517 /* Set offset adjustment value. */
9518 /* Check instruction type and set complimentary instruction. */
9521 /* First instruction is 16-bit. */
9523 insn16 = bfd_getb16 (contents + laddr);
9524 switch ((insn16 & 0xf000) >> 12)
9527 /* beqz38 or bnez38 */
9528 comp_insn = (insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
9529 comp_insn |= ((insn16 & 0x0700) >> 8) << 20;
9530 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9531 insn = (insn16 & 0x0800) ? INSN_BEQZ : INSN_BNEZ;
9532 insn |= ((insn16 & 0x0700) >> 8) << 20;
9533 i_mask = 0xffff0000;
9537 /* beqs38 or bnes38 */
9538 comp_insn = (insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
9539 comp_insn |= (((insn16 & 0x0700) >> 8) << 20)
9541 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9542 insn = (insn16 & 0x0800) ? INSN_BEQ : INSN_BNE;
9543 insn |= (((insn16 & 0x0700) >> 8) << 20) | (REG_R5 << 15);
9544 i_mask = 0xffffc000;
9548 /* beqzS8 or bnezS8 */
9549 comp_insn = (insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
9550 comp_insn |= REG_R15 << 20;
9551 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
9552 insn = (insn16 & 0x0100) ? INSN_BEQZ : INSN_BNEZ;
9553 insn |= REG_R15 << 20;
9554 i_mask = 0xffff0000;
9560 /* First instruction is 32-bit. */
9562 insn = bfd_getb32 (contents + laddr);
9568 else if (N32_OP6 (insn) == N32_OP6_BR1)
9571 comp_insn = insn ^ 0x4000;
9572 i_mask = 0xffffc000;
9573 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
9575 /* This instruction can turn to 16-bit. */
9577 (insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
9578 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9589 comp_insn = insn ^ 0x10000;
9590 i_mask = 0xffff0000;
9591 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
9592 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
9594 if (N32_IS_RT3 (insn))
9596 /* This instruction can turn to 16-bit. */
9598 (insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
9599 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9601 else if (N32_RT5 (insn) == REG_R15)
9603 /* This instruction can turn to 16-bit. */
9605 (insn & 0x10000) ? INSN_BNEZS8 : INSN_BEQZS8;
9609 /* No conversion. */
9615 /* No conversion. */
9621 if (foff < -0x1000000 && foff >= 0x1000000)
9624 if (i2_irelfn != irelend)
9627 && (i2_irelfn->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
9629 /* The instruction pointed by R_NDS32_INSN16 is already
9630 turned into 16-bit instruction, so the total length
9631 of this sequence is decreased by 2. */
9632 seq_len = seq_len - 2;
9633 i2_irelfn->r_addend = 0;
9637 /* For simplicity of coding, we are going to modify the section
9638 contents, the section relocs, and the BFD symbol table. We
9639 must tell the rest of the code not to free up this
9640 information. It would be possible to instead create a table
9641 of changes which have to be made, as is done in coff-mips.c;
9642 that would be more work, but would require less memory when
9643 the linker is run. */
9646 && foff >= -0x100 - insn_off && foff < 0x100 - insn_off)
9648 if (insn_opt || (seq_len & 0x2) == 0)
9650 /* Don't convert it to 16-bit now, keep this as relaxable
9651 for ``label reloc; INSN1a''6. */
9652 /* Save comp_insn32 to buffer. */
9654 bfd_putb32 (insn, contents + irel->r_offset);
9656 reloc = (N32_OP6 (comp_insn) == N32_OP6_BR1) ?
9657 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9660 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9665 /* Not optimize for speed; convert sequence to 16-bit. */
9666 /* Save comp_insn16 to buffer. */
9667 insn16 = comp_insn16;
9668 bfd_putb16 (insn16, contents + irel->r_offset);
9670 reloc = R_NDS32_9_PCREL_RELA;
9672 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9676 /* Change relocs. */
9677 for (i = 0; i < 2; i++)
9680 find_relocs_at_address_addr (irel, internal_relocs,
9681 irelend, checked_types[i],
9684 if (cond_irelfn != irelend)
9686 cond_irelfn->r_info =
9687 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9688 cond_irelfn->r_addend = hi_irelfn->r_addend;
9692 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9694 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9697 else if (N32_OP6 (insn) == N32_OP6_BR1
9698 && foff >= -0x4000 - insn_off && foff < 0x4000 - insn_off)
9700 /* Relax to `beq label ; 15_PCREL'. */
9702 /* Save comp_insn to buffer. */
9704 bfd_putb32 (insn, contents + irel->r_offset);
9706 reloc = R_NDS32_15_PCREL_RELA;
9708 /* Change relocs. */
9710 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9712 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9714 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9717 insn16 = NDS32_NOP16;
9718 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
9720 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9722 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9724 hi_irelfn->r_offset += 2;
9729 else if (N32_OP6 (insn) == N32_OP6_BR2
9730 && foff >= -0x10000 - insn_off
9731 && foff < 0x10000 - insn_off)
9733 /* Relax to `beqz label ; 17_PCREL'. */
9735 /* Save comp_insn to buffer. */
9737 bfd_putb32 (insn, contents + irel->r_offset);
9739 reloc = R_NDS32_17_PCREL_RELA;
9741 /* Change relocs. */
9743 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9745 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9747 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9750 insn16 = NDS32_NOP16;
9751 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
9753 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9755 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9757 hi_irelfn->r_offset += 2;
9762 else if (foff >= -0x1000000 - reloc_off
9763 && foff < 0x1000000 - reloc_off)
9765 /* Relax to one of the following 3 variations
9767 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize for space
9768 bnes38 rt, $1 ; LONGJUMP2
9772 case 4-4; 1st insn not convertible, others don't care
9773 bne rt, ra, $1 ; LONGJUMP2
9777 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9778 bne rt, ra, $1 ; LONGJUMP2/INSN16
9783 /* Offset for first instruction. */
9787 /* First instruction is 16-bit. */
9788 if (hi_irelfn != irelend)
9790 /* INSN16 exists so this is optimized for speed. */
9791 /* Convert this instruction to 32-bit for label alignment. */
9792 insn = (insn & i_mask) | 4;
9793 bfd_putb32 (insn, contents + irel->r_offset);
9795 hi_irelfn->r_offset += 2;
9799 /* Not optimized for speed. */
9800 insn16 = (insn16 & 0xff00) | 3;
9801 bfd_putb16 (insn16, contents + irel->r_offset);
9807 /* First instruction is 32-bit. */
9808 insn = (insn & i_mask) | 4;
9809 bfd_putb32 (insn, contents + irel->r_offset);
9813 /* Use j label as second instruction. */
9815 bfd_putb32 (insn, contents + irel->r_offset);
9817 /* Change relocs. */
9818 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGJUMP2);
9819 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9820 R_NDS32_25_PCREL_RELA);
9822 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9823 if (((seq_len ^ insn_len) & 0x2) != 0x2)
9825 insn16 = NDS32_NOP16;
9826 bfd_putb16 (insn16, contents + irel->r_offset + insn_len);
9827 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9829 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9830 lo_irelfn->r_offset = hi_irelfn->r_offset + 4;
9837 for (i = 0; i < 2; i++)
9840 find_relocs_at_address_addr (irel, internal_relocs,
9841 irelend, checked_types[i],
9844 if (cond_irelfn != irelend)
9847 if (cond_irelfn != irelend)
9849 cond_irelfn->r_info =
9850 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9851 cond_irelfn->r_addend = hi_irelfn->r_addend;
9855 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LOADSTORE)
9857 int eliminate_sethi = 0, ls_range_type;
9858 enum elf_nds32_reloc_type checked_types[] =
9859 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
9860 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
9861 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20
9866 for (i = 0; i < 6; i++)
9868 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9869 checked_types[i], laddr);
9870 if (hi_irelfn != irelend)
9874 if (hi_irelfn == irelend)
9876 (*_bfd_error_handler)
9877 ("%B: warning: R_NDS32_LOADSTORE points to unrecognized reloc at 0x%lx.",
9878 abfd, (long) irel->r_offset);
9882 ls_range_type = (irel->r_addend >> 8) & 0x3f;
9884 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
9886 switch (ELF32_R_TYPE (hi_irelfn->r_info))
9888 case R_NDS32_HI20_RELA:
9889 insn = bfd_getb32 (contents + laddr);
9891 calculate_memory_address (abfd, hi_irelfn, isymbuf,
9894 if ((ls_range_type & 0x3f) == 0x20)
9896 if ((access_addr < 0x7f000))
9898 eliminate_sethi = 1;
9903 /* This is avoid to relax symbol address which is fixed
9904 relocations. Ex: _stack. */
9905 struct elf_link_hash_entry *h;
9907 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
9910 h = elf_sym_hashes (abfd)[indx];
9911 if (h && bfd_is_abs_section (h->root.u.def.section))
9917 if (!load_store_relax)
9920 if (((insn >> 20) & 0x1f) == REG_GP)
9923 if (ls_range_type & 0x8 || ls_range_type & 0x10)
9925 range_l = sdata_range[0][0];
9926 range_h = sdata_range[0][1];
9930 range_l = sdata_range[4][0];
9931 range_h = sdata_range[4][1];
9935 case R_NDS32_GOT_HI20:
9937 calculate_got_memory_address (abfd, link_info, hi_irelfn,
9940 /* If this symbol is not in .got, the return value will be -1.
9941 Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
9942 a negative offset is allowed. */
9943 if ((bfd_signed_vma) (access_addr - local_sda) < 0x7f000
9944 && (bfd_signed_vma) (access_addr - local_sda) >= -0x7f000)
9945 eliminate_sethi = 1;
9948 case R_NDS32_PLT_GOTREL_HI20:
9950 calculate_plt_memory_address (abfd, link_info, isymbuf,
9951 hi_irelfn, symtab_hdr);
9953 if ((bfd_signed_vma) (access_addr - local_sda) < 0x7f000
9954 && (bfd_signed_vma) (access_addr - local_sda) >= -0x7f000)
9955 eliminate_sethi = 1;
9958 case R_NDS32_GOTOFF_HI20:
9960 calculate_memory_address (abfd, hi_irelfn, isymbuf,
9963 if ((bfd_signed_vma) (access_addr - local_sda) < 0x7f000
9964 && (bfd_signed_vma) (access_addr - local_sda) >= -0x7f000)
9965 eliminate_sethi = 1;
9968 case R_NDS32_GOTPC_HI20:
9969 for (i1_irelfn = irel;
9970 i1_irelfn->r_offset <= irel->r_offset + 4
9971 && i1_irelfn < irelend; i1_irelfn++)
9972 if (ELF32_R_TYPE (i1_irelfn->r_info) == R_NDS32_GOTPC_LO12)
9974 if (i1_irelfn == irelend
9975 || i1_irelfn->r_offset != irel->r_offset + 4)
9978 access_addr = sec->output_section->vma + sec->output_offset
9980 if ((bfd_signed_vma) (local_sda - access_addr) < 0x7f000
9981 && (bfd_signed_vma) (local_sda - access_addr) >= -0x7f000)
9983 /* Turn into MOVI. */
9984 insn = bfd_getb32 (contents + laddr + 4);
9985 if (((insn & 0x1f00000) >> 20) != REG_GP)
9988 hi_irelfn->r_addend = ((int) hi_irelfn->r_addend) < -4
9989 ? (hi_irelfn->r_addend + 4) : (hi_irelfn->r_addend);
9991 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9993 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9994 i1_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9996 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
9997 bfd_putb32 (insn, contents + laddr);
10006 if (eliminate_sethi == 1
10007 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
10008 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10010 hi_irelfn->r_info =
10011 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
10013 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10017 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA)
10019 foff = calculate_offset (abfd, sec, irel, isymbuf, symtab_hdr,
10021 if (pic_ext_target || foff == 0)
10023 if (foff < 1022 && foff >= 0)
10025 reloc = R_NDS32_10IFCU_PCREL_RELA;
10026 insn16 = INSN_IFCALL9;
10027 bfd_putb16 (insn16, contents + irel->r_offset);
10030 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_10IFCU_PCREL_RELA);
10033 i2_irelfn = find_relocs_at_address (irel, internal_relocs,
10034 irelend, R_NDS32_INSN16);
10035 if (i2_irelfn < irelend)
10037 insn16 = NDS32_NOP16;
10038 bfd_putb16 (insn16, contents + irel->r_offset + 2);
10039 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10040 i2_irelfn->r_offset += 2;
10045 ((*_bfd_error_handler)
10046 ("%s: 0x%lx: warning: R_NDS32_17IFC points to unrecognized reloc at 0x%lx",
10047 bfd_get_filename (abfd), (long) irel->r_offset));
10051 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
10052 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
10053 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
10054 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
10055 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA)
10057 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10058 &local_sda, FALSE);
10060 insn = bfd_getb32 (contents + laddr);
10062 if (!is_sda_access_insn (insn)
10063 && N32_OP6 (insn) != N32_OP6_ORI)
10067 calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10068 insn_len = seq_len = 4;
10070 /* This is avoid to relax symbol address which is fixed
10071 relocations. Ex: _stack. */
10072 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr >= 0x7f000)
10074 struct elf_link_hash_entry *h;
10076 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
10079 h = elf_sym_hashes (abfd)[indx];
10080 if (h && bfd_is_abs_section (h->root.u.def.section))
10085 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < 0x7f000)
10087 reloc = R_NDS32_20_RELA;
10088 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10089 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10090 bfd_putb32 (insn, contents + laddr);
10092 else if (load_store_relax)
10094 range_l = sdata_range[4][0];
10095 range_h = sdata_range[4][1];
10096 switch (ELF32_R_TYPE (irel->r_info))
10098 case R_NDS32_LO12S0_RELA:
10099 reloc = R_NDS32_SDA19S0_RELA;
10101 case R_NDS32_LO12S1_RELA:
10102 reloc = R_NDS32_SDA18S1_RELA;
10104 case R_NDS32_LO12S2_RELA:
10105 reloc = R_NDS32_SDA17S2_RELA;
10107 case R_NDS32_LO12S2_DP_RELA:
10108 range_l = sdata_range[0][0];
10109 range_h = sdata_range[0][1];
10110 reloc = R_NDS32_SDA12S2_DP_RELA;
10112 case R_NDS32_LO12S2_SP_RELA:
10113 range_l = sdata_range[0][0];
10114 range_h = sdata_range[0][1];
10115 reloc = R_NDS32_SDA12S2_SP_RELA;
10121 /* There are range_h and range_l because linker has to promise
10122 all sections move cross one page together. */
10123 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
10124 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10126 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
10128 /* Maybe we should add R_NDS32_INSN16 reloc type here
10129 or manually do some optimization. sethi can't be
10130 eliminated when updating $gp so the relative ori
10131 needs to be preserved. */
10134 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
10138 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10139 bfd_putb32 (insn, contents + laddr);
10143 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
10144 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
10145 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
10146 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
10148 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10149 &local_sda, FALSE);
10151 insn = bfd_getb32 (contents + laddr);
10153 if (N32_OP6 (insn) != N32_OP6_ORI)
10156 insn_len = seq_len = 4;
10157 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
10159 foff = calculate_got_memory_address (abfd, link_info, irel,
10160 symtab_hdr) - local_sda;
10161 reloc = R_NDS32_GOT20;
10163 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
10165 foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
10166 symtab_hdr) - local_sda;
10167 reloc = R_NDS32_PLT_GOTREL_LO20;
10169 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
10171 foff = calculate_memory_address (abfd, irel, isymbuf,
10172 symtab_hdr) - local_sda;
10173 reloc = R_NDS32_GOTOFF;
10178 if ((foff < 0x7f000) && (foff >= -0x7f000))
10180 /* Turn into MOVI. */
10181 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10182 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10183 bfd_putb32 (insn, contents + laddr);
10186 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PTR)
10189 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10190 R_NDS32_PTR_RESOLVED, irel->r_addend);
10192 if (i1_irelfn == irelend)
10194 (*_bfd_error_handler)
10195 ("%B: warning: R_NDS32_PTR points to unrecognized reloc at 0x%lx.",
10196 abfd, (long) irel->r_offset);
10200 if (i1_irelfn->r_addend & 1)
10202 /* Pointed target is relaxed and no longer needs this void *,
10203 change the type to NONE. */
10204 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10207 find_relocs_at_address (irel, internal_relocs, irelend,
10208 R_NDS32_PTR_COUNT);
10210 if (i1_irelfn == irelend)
10212 (*_bfd_error_handler)
10213 ("%B: warning: no R_NDS32_PTR_COUNT coexist with R_NDS32_PTR at 0x%lx.",
10214 abfd, (long) irel->r_offset);
10218 if (--i1_irelfn->r_addend > 0)
10221 /* If the PTR_COUNT is already 0, remove current instruction. */
10222 seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
10228 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOT_SUFF)
10230 /* FIXME: It's a little trouble to turn JRAL5 to JAL since
10231 we need additional space. It might be help if we could
10232 borrow some space from instructions to be eliminated
10233 such as sethi, ori, add. */
10235 insn = bfd_getb32 (contents + laddr);
10236 if (insn & 0x80000000)
10239 if (nds32_elf_check_dup_relocs
10240 (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
10243 seq_len = insn_len = 4;
10245 find_relocs_at_address (irel, internal_relocs, irelend,
10246 R_NDS32_PTR_RESOLVED);
10249 The boundary should be reduced since the .plt section hasn't
10250 been created and the address of specific entry is still unknown
10251 Maybe the range between the function call and the begin of the
10252 .text section can be used to decide if the .plt is in the range
10253 of function call. */
10255 if (N32_OP6 (insn) == N32_OP6_ALU1
10256 && N32_SUB5 (insn) == N32_ALU1_ADD_SLLI
10257 && N32_SH5 (insn) == 0)
10259 /* Get the value of the symbol referred to by the reloc. */
10260 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10261 &local_sda, FALSE);
10262 foff = (bfd_signed_vma) (calculate_plt_memory_address
10263 (abfd, link_info, isymbuf, irel,
10264 symtab_hdr) - local_sda);
10265 /* This condition only happened when symbol is undefined. */
10269 if (foff < -0x3f000 || foff >= 0x3f000)
10271 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10272 R_NDS32_PLT_GOTREL_LO19);
10274 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
10276 else if (N32_OP6 (insn) == N32_OP6_JREG
10277 && N32_SUB5 (insn) == N32_JREG_JRAL)
10279 /* Get the value of the symbol referred to by the reloc. */
10281 calculate_plt_offset (abfd, sec, link_info, isymbuf, irel,
10283 /* This condition only happened when symbol is undefined. */
10286 if (foff < -0x1000000 || foff >= 0x1000000)
10288 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
10294 bfd_putb32 (insn, contents + laddr);
10295 if (i1_irelfn != irelend)
10297 i1_irelfn->r_addend |= 1;
10301 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_SUFF)
10304 insn = bfd_getb32 (contents + laddr);
10305 if (insn & 0x80000000)
10308 if (nds32_elf_check_dup_relocs
10309 (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
10312 seq_len = insn_len = 4;
10313 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10314 R_NDS32_PTR_RESOLVED);
10316 foff = calculate_got_memory_address (abfd, link_info, irel,
10317 symtab_hdr) - local_sda;
10319 if (foff < 0x3f000 && foff >= -0x3f000)
10321 /* Turn LW to LWI.GP. Change relocation type to R_NDS32_GOT_REL. */
10322 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
10324 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
10329 bfd_putb32 (insn, contents + laddr);
10330 if (i1_irelfn != irelend)
10332 i1_irelfn->r_addend |= 1;
10336 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_SUFF)
10338 int opc_insn_gotoff;
10340 insn = bfd_getb32 (contents + laddr);
10341 if (insn & 0x80000000)
10344 if (nds32_elf_check_dup_relocs
10345 (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
10348 seq_len = insn_len = 4;
10350 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10351 R_NDS32_PTR_RESOLVED);
10352 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10353 &local_sda, FALSE);
10354 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10355 foff = access_addr - local_sda;
10357 if (foff >= 0x3f000 || foff < -0x3f000)
10360 /* Concatenate opcode and sub-opcode for switch case.
10361 It may be MEM or ALU1. */
10362 opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
10363 switch (opc_insn_gotoff)
10365 case (N32_OP6_MEM << 8) | N32_MEM_LW:
10366 /* 4-byte aligned. */
10367 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
10369 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
10371 case (N32_OP6_MEM << 8) | N32_MEM_SW:
10372 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
10374 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
10376 case (N32_OP6_MEM << 8) | N32_MEM_LH:
10377 /* 2-byte aligned. */
10378 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
10380 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
10382 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
10383 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
10385 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
10387 case (N32_OP6_MEM << 8) | N32_MEM_SH:
10388 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
10390 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
10392 case (N32_OP6_MEM << 8) | N32_MEM_LB:
10393 /* 1-byte aligned. */
10394 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
10396 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
10398 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
10399 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
10401 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
10403 case (N32_OP6_MEM << 8) | N32_MEM_SB:
10404 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
10406 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
10408 case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD_SLLI:
10409 if (N32_SH5 (insn) != 0)
10411 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
10413 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
10419 bfd_putb32 (insn, contents + laddr);
10420 if (i1_irelfn != irelend)
10422 i1_irelfn->r_addend |= 1;
10426 find_relocs_at_address (irel, internal_relocs, irelend,
10427 R_NDS32_INSN16)) != irelend)
10428 i2_irelfn->r_info =
10429 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10431 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_MULCALL_SUFF)
10433 /* The last bit of r_addend indicates its a two instruction block. */
10434 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10435 R_NDS32_PTR_RESOLVED);
10436 if ((i1_irelfn != irelend && (i1_irelfn->r_addend & 1))
10437 || (nds32_elf_insn_size (abfd, contents, irel->r_offset) != 4
10438 && !(i1_irelfn != irelend && (i1_irelfn->r_addend & 2))))
10441 /* Get the value of the symbol referred to by the reloc. */
10442 foff = calculate_offset (abfd, sec, irel, isymbuf, symtab_hdr,
10445 /* This condition only happened when symbol is undefined. */
10446 if (pic_ext_target || foff == 0)
10448 if (foff < -0x1000000 || foff >= 0x1000000)
10451 if (i1_irelfn != irelend && (i1_irelfn->r_addend & 2))
10453 seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
10454 seq_len += nds32_elf_insn_size (abfd, contents,
10455 irel->r_offset + seq_len);
10462 bfd_putb32 (insn, contents + laddr);
10463 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PCREL_RELA);
10465 if (i1_irelfn != irelend)
10467 i1_irelfn->r_addend |= 1;
10470 while (i1_irelfn != irelend
10471 && irel->r_offset == i1_irelfn->r_offset)
10474 i1_irelfn != irelend
10475 && i1_irelfn->r_offset < irel->r_offset + 4; i1_irelfn++)
10476 i1_irelfn->r_info =
10477 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
10479 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTBLOCK)
10481 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10482 R_NDS32_PLT_GOTREL_HI20);
10484 if (i1_irelfn == irelend)
10486 (*_bfd_error_handler)
10487 ("%B: warning: R_NDS32_PLTBLOCK points to unrecognized reloc at 0x%lx.",
10488 abfd, (long) irel->r_offset);
10492 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10493 &local_sda, FALSE);
10495 calculate_plt_offset (abfd, sec, link_info, isymbuf, hi_irelfn,
10498 if (foff < -0x1000000 || foff >= 0x1000000)
10500 foff = (bfd_signed_vma) (calculate_plt_memory_address
10501 (abfd, link_info, isymbuf, hi_irelfn,
10502 symtab_hdr) - local_sda);
10503 if (foff >= -0x4000 && foff < 0x4000)
10505 /* addi $rt, $gp, lo15(Sym - SDA_BASE)
10508 /* TODO: We can use add.gp here, once ISA V1 is obsolete. */
10509 insn = N32_TYPE2 (ADDI, N32_RT5 (insn), REG_GP, 0);
10510 bfd_putb32 (insn, contents + irel->r_offset + 8);
10512 i1_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10513 R_NDS32_PLT_GOTREL_LO15);
10514 i1_irelfn->r_addend = hi_irelfn->r_addend;
10518 else if (foff >= -0x80000 && foff < 0x80000)
10520 /* movi $rt, lo20(Sym - SDA_BASE) PLT_GOTREL_LO20
10521 add $rt, $gp, $rt INSN16
10524 for (i1_irelfn = irel;
10525 i1_irelfn->r_offset < irel->r_offset + 4; i1_irelfn++)
10527 for (; i1_irelfn->r_offset < irel->r_offset + 8; i1_irelfn++)
10528 if (ELF32_R_TYPE (i1_irelfn->r_info) != R_NDS32_PLT_GOTREL_LO12)
10529 i2_irelfn = i1_irelfn;
10530 else if (ELF32_R_TYPE (i1_irelfn->r_info) != R_NDS32_LABEL)
10531 i1_irelfn->r_info =
10532 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
10536 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10537 bfd_putb32 (insn, contents + irel->r_offset + 4);
10538 i2_irelfn->r_info =
10539 ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10540 R_NDS32_PLT_GOTREL_LO20);
10550 /* jal Sym INSN16/25_PLTREL */
10551 for (i1_irelfn = irel;
10552 i1_irelfn->r_offset < irel->r_offset + 12; i1_irelfn++)
10555 i2_irelfn = i1_irelfn - 1;
10556 i2_irelfn->r_offset = i1_irelfn->r_offset;
10557 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10558 R_NDS32_25_PLTREL);
10559 i2_irelfn->r_addend = hi_irelfn->r_addend;
10561 bfd_putb32 (insn, contents + irel->r_offset + 12);
10570 if (seq_len - insn_len > 0)
10572 if (!insert_nds32_elf_blank
10573 (&relax_blank_list, irel->r_offset + insn_len,
10574 seq_len - insn_len))
10580 calc_nds32_blank_total (relax_blank_list);
10582 if (table->relax_fp_as_gp)
10584 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
10588 if (*again == FALSE)
10590 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
10596 if (*again == FALSE)
10598 /* This code block is used to adjust 4-byte alignment by relax a pair
10599 of instruction a time.
10601 It recognizes three types of relocations.
10602 1. R_NDS32_LABEL - a aligment.
10603 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
10604 3. is_16bit_NOP () - remove a 16-bit instruction.
10606 FIXME: It seems currently implementation only support 4-byte aligment.
10607 We should handle any-aligment. */
10609 Elf_Internal_Rela *insn_rel = NULL;
10610 Elf_Internal_Rela *label_rel = NULL;
10611 Elf_Internal_Rela *tmp_rel, tmp2_rel, *tmp3_rel = NULL;
10613 /* Checking for branch relaxation relies on the relocations to
10614 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
10615 nds32_insertion_sort (internal_relocs, sec->reloc_count,
10616 sizeof (Elf_Internal_Rela), compar_reloc);
10618 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10619 &local_sda, FALSE);
10621 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
10622 /* FIXME: Can we generate the right order in assembler?
10623 So we don't have to swapping them here. */
10624 for (label_rel = internal_relocs, insn_rel = internal_relocs;
10625 label_rel < irelend; label_rel++)
10627 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
10630 /* Find the first reloc has the same offset with label_rel. */
10631 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
10635 insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
10637 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
10639 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
10642 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
10643 && insn_rel < label_rel)
10645 /* Swap the two reloc if the R_NDS32_INSN16 is before R_NDS32_LABEL. */
10646 memcpy (&tmp2_rel, insn_rel, sizeof (Elf_Internal_Rela));
10647 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
10648 memcpy (label_rel, &tmp2_rel, sizeof (Elf_Internal_Rela));
10654 /* If there were a sequence of R_NDS32_LABEL end up with .align 2 or higher,
10655 remove other R_NDS32_LABEL with lower alignment.
10656 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
10657 then the R_NDS32_LABEL sequence is broke. */
10658 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
10660 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
10662 if (label_rel == NULL)
10664 if (tmp_rel->r_addend < 2)
10665 label_rel = tmp_rel;
10668 else if (tmp_rel->r_addend > 1)
10670 for (tmp3_rel = label_rel; tmp3_rel < tmp_rel; tmp3_rel++)
10672 if (ELF32_R_TYPE (tmp3_rel->r_info) == R_NDS32_LABEL
10673 && tmp3_rel->r_addend < 2)
10674 tmp3_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (tmp3_rel->r_info), R_NDS32_NONE);
10679 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16)
10682 && label_rel->r_offset != tmp_rel->r_offset
10683 && (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
10685 || is_16bit_NOP (abfd, sec, tmp_rel)))
10694 /* Optimized for speed and nothing has not been relaxed.
10695 It's time to align labels.
10696 We may convert a 16-bit instruction right before a label to
10697 32-bit, in order to align the label if necessary
10698 all reloc entries has been sorted by r_offset. */
10699 for (irel = internal_relocs; irel < irelend; irel++)
10701 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
10702 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
10705 /* Search for INSN16 reloc. */
10706 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
10710 /* Previous LABEL reloc exists. Try to resolve it. */
10711 if (label_rel->r_offset == irel->r_offset)
10713 /* LABEL and INSN are at the same addr. */
10714 if ((irel->r_offset
10715 - get_nds32_elf_blank_total (&relax_blank_list,
10719 if (irel->r_addend > 1)
10721 /* Force to relax. */
10722 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10724 if (is_convert_32_to_16
10725 (abfd, sec, irel, internal_relocs, irelend,
10728 nds32_elf_write_16 (abfd, contents, irel,
10729 internal_relocs, irelend,
10732 if (!insert_nds32_elf_blank_recalc_total
10733 (&relax_blank_list, irel->r_offset + 2,
10737 else if (is_16bit_NOP (abfd, sec, irel))
10739 if (!insert_nds32_elf_blank_recalc_total
10740 (&relax_blank_list, irel->r_offset, 2))
10746 if (is_convert_32_to_16
10747 (abfd, sec, irel, internal_relocs, irelend,
10749 || is_16bit_NOP (abfd, sec, irel))
10757 /* Already aligned, reset LABEL and keep INSN16. */
10762 /* No INSN16 to relax, we don't want to insert 16-bit. */
10763 /* Nop here, just signal the algorithm is wrong. */
10767 /* A new INSN16 found, resize the old one. */
10770 if (!is_convert_32_to_16
10771 (abfd, sec, irel, internal_relocs, irelend,
10773 && !is_16bit_NOP (abfd, sec, irel))
10775 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10779 /* Previous INSN16 reloc exists, reduce its size to 16-bit. */
10780 if (is_convert_32_to_16
10781 (abfd, sec, insn_rel, internal_relocs, irelend,
10784 nds32_elf_write_16 (abfd, contents, insn_rel,
10785 internal_relocs, irelend, insn16);
10787 if (!insert_nds32_elf_blank_recalc_total
10788 (&relax_blank_list, insn_rel->r_offset + 2, 2))
10791 else if (is_16bit_NOP (abfd, sec, insn_rel))
10793 if (!insert_nds32_elf_blank_recalc_total
10794 (&relax_blank_list, insn_rel->r_offset, 2))
10798 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
10803 if (is_convert_32_to_16
10804 (abfd, sec, irel, internal_relocs, irelend, &insn16)
10805 || is_16bit_NOP (abfd, sec, irel))
10809 /* Save the new one for later use. */
10811 /* Search for label. */
10812 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
10814 /* Label on 16-bit instruction, just reset this reloc. */
10815 insn16 = bfd_getb16 (contents + irel->r_offset);
10816 if ((irel->r_addend & 0x1f) < 2 && (insn16 & 0x8000))
10819 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10823 if (!optimize && (irel->r_addend & 0x1f) < 2)
10826 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10830 /* Try to align this label. */
10833 int force_relax = 0;
10835 /* If current location is .align 2, we can't relax previous 32-bit inst. */
10836 /* Or the alignment constraint is broke. */
10837 if ((irel->r_addend & 0x1f) < 2)
10839 /* Label_rel always seats before insn_rel after our sort. */
10841 /* INSN16 and LABEL at different location. */
10842 /* Search for INSN16 at LABEL location. */
10843 for (tmp_rel = irel;
10844 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
10847 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16)
10851 if (tmp_rel < irelend
10852 && tmp_rel->r_offset == irel->r_offset)
10854 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16)
10856 if (is_convert_32_to_16
10857 (abfd, sec, tmp_rel, internal_relocs,
10859 || is_16bit_NOP (abfd, sec, tmp_rel))
10865 if ((irel->r_offset
10866 - get_nds32_elf_blank_total (&relax_blank_list,
10867 irel->r_offset, 1)) & 0x01)
10869 /* Can't align on byte, BIG ERROR. */
10874 || ((irel->r_offset
10875 - get_nds32_elf_blank_total
10876 (&relax_blank_list, irel->r_offset, 1))
10878 || irel->r_addend == 1)
10880 if (insn_rel != NULL)
10882 /* Label not aligned. */
10883 /* Previous reloc exists, reduce its size to 16-bit. */
10884 if (is_convert_32_to_16
10885 (abfd, sec, insn_rel, internal_relocs,
10888 nds32_elf_write_16 (abfd, contents, insn_rel,
10889 internal_relocs, irelend,
10892 if (!insert_nds32_elf_blank_recalc_total
10893 (&relax_blank_list,
10894 insn_rel->r_offset + 2, 2))
10897 else if (is_16bit_NOP (abfd, sec, insn_rel))
10899 if (!insert_nds32_elf_blank_recalc_total
10900 (&relax_blank_list, insn_rel->r_offset,
10916 /* INSN16 reloc is used. */
10925 if (((sec->size - get_nds32_elf_blank_total (&relax_blank_list, sec->size, 0))
10926 - ((sec->size - get_nds32_elf_blank_total (&relax_blank_list, sec->size, 0))
10927 & (0xffffffff << sec->alignment_power)) == 2)
10928 || optimize_for_space)
10930 if (is_convert_32_to_16
10931 (abfd, sec, insn_rel, internal_relocs, irelend,
10934 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
10936 if (!insert_nds32_elf_blank_recalc_total
10937 (&relax_blank_list, insn_rel->r_offset + 2, 2))
10939 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
10942 else if (is_16bit_NOP (abfd, sec, insn_rel))
10944 if (!insert_nds32_elf_blank_recalc_total
10945 (&relax_blank_list, insn_rel->r_offset, 2))
10947 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
10954 /* It doesn't matter optimize_for_space_no_align anymore.
10955 If object file is assembled with flag '-Os',
10956 the we don't adjust jump-destination on 4-byte boundary. */
10958 if (relax_blank_list)
10960 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
10961 relax_blank_list = NULL;
10964 if (*again == FALSE)
10966 /* Closing the section, so we don't relax it anymore. */
10967 bfd_vma sec_size_align;
10968 Elf_Internal_Rela *tmp_rel;
10970 /* Pad to alignment boundary. Only handle current section alignment. */
10971 sec_size_align = (sec->size + (~((-1) << sec->alignment_power)))
10972 & ((-1) << sec->alignment_power);
10973 if ((sec_size_align - sec->size) & 0x2)
10975 insn16 = NDS32_NOP16;
10976 bfd_putb16 (insn16, contents + sec->size);
10980 while (sec_size_align != sec->size)
10982 insn = NDS32_NOP32;
10983 bfd_putb32 (insn, contents + sec->size);
10987 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
10988 R_NDS32_RELAX_ENTRY);
10989 if (tmp_rel != irelend)
10990 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
10992 clean_nds32_elf_blank ();
10996 if (internal_relocs != NULL
10997 && elf_section_data (sec)->relocs != internal_relocs)
10998 free (internal_relocs);
11000 if (contents != NULL
11001 && elf_section_data (sec)->this_hdr.contents != contents)
11004 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
11014 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
11016 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
11017 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
11022 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
11023 struct bfd_link_info *info,
11024 void *finfo ATTRIBUTE_UNUSED,
11025 bfd_boolean (*func) (void *, const char *,
11026 Elf_Internal_Sym *,
11028 struct elf_link_hash_entry *)
11031 FILE *sym_ld_script = NULL;
11032 struct elf_nds32_link_hash_table *table;
11034 table = nds32_elf_hash_table (info);
11035 sym_ld_script = table->sym_ld_script;
11037 if (check_start_export_sym)
11038 fprintf (sym_ld_script, "}\n");
11043 static enum elf_reloc_type_class
11044 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11045 const asection *rel_sec ATTRIBUTE_UNUSED,
11046 const Elf_Internal_Rela *rela)
11048 switch ((int) ELF32_R_TYPE (rela->r_info))
11050 case R_NDS32_RELATIVE:
11051 return reloc_class_relative;
11052 case R_NDS32_JMP_SLOT:
11053 return reloc_class_plt;
11055 return reloc_class_copy;
11057 return reloc_class_normal;
11061 /* Put target dependent option into info hash table. */
11063 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
11064 int relax_fp_as_gp,
11065 int eliminate_gc_relocs,
11066 FILE * sym_ld_script, int load_store_relax,
11067 int target_optimize, int relax_status,
11068 int relax_round, FILE * ex9_export_file,
11069 FILE * ex9_import_file,
11070 int update_ex9_table, int ex9_limit,
11071 bfd_boolean ex9_loop_aware,
11072 bfd_boolean ifc_loop_aware)
11074 struct elf_nds32_link_hash_table *table;
11076 table = nds32_elf_hash_table (link_info);
11080 table->relax_fp_as_gp = relax_fp_as_gp;
11081 table->eliminate_gc_relocs = eliminate_gc_relocs;
11082 table->sym_ld_script = sym_ld_script;
11083 table ->load_store_relax = load_store_relax;
11084 table->target_optimize = target_optimize;
11085 table->relax_status = relax_status;
11086 table->relax_round = relax_round;
11087 table->ex9_export_file = ex9_export_file;
11088 table->ex9_import_file = ex9_import_file;
11089 table->update_ex9_table = update_ex9_table;
11090 table->ex9_limit = ex9_limit;
11091 table->ex9_loop_aware = ex9_loop_aware;
11092 table->ifc_loop_aware = ifc_loop_aware;
11095 /* These functions and data-structures are used for fp-as-gp
11098 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
11099 #define FAG_BUMPER 8 /* Leave some space to avoid aligment issues. */
11100 #define FAG_WINDOW (512 - FAG_BUMPER) /* lwi37.fp covers 512 bytes. */
11102 /* An nds32_fag represent a gp-relative access.
11103 We find best fp-base by using a sliding window
11104 to find a base address which can cover most gp-access. */
11107 struct nds32_fag *next; /* NULL-teminated linked list. */
11108 bfd_vma addr; /* The address of this fag. */
11109 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
11110 It is used for applying FP7U2_FLAG. */
11111 int count; /* How many times this address is referred.
11112 There should be exactly `count' relocations
11114 int relas_capcity; /* The buffer size of relas.
11115 We use an array instead of linked-list,
11116 and realloc is used to adjust buffer size. */
11120 nds32_fag_init (struct nds32_fag *head)
11122 memset (head, 0, sizeof (struct nds32_fag));
11126 nds32_fag_verify (struct nds32_fag *head)
11128 struct nds32_fag *iter;
11129 struct nds32_fag *prev;
11135 if (prev && prev->addr >= iter->addr)
11136 puts ("Bug in fp-as-gp insertion.");
11142 /* Insert a fag in ascending order.
11143 If a fag of the same address already exists,
11144 they are chained by relas array. */
11147 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
11148 Elf_Internal_Rela * rel)
11150 struct nds32_fag *iter;
11151 struct nds32_fag *new_fag;
11152 const int INIT_RELAS_CAP = 4;
11155 iter->next && iter->next->addr <= addr;
11157 /* Find somewhere to insert. */ ;
11159 /* `iter' will be equal to `head' if the list is empty. */
11160 if (iter != head && iter->addr == addr)
11162 /* The address exists in the list.
11163 Insert `rel' into relocation list, relas. */
11165 /* Check whether relas is big enough. */
11166 if (iter->count >= iter->relas_capcity)
11168 iter->relas_capcity *= 2;
11169 iter->relas = bfd_realloc
11170 (iter->relas, iter->relas_capcity * sizeof (void *));
11172 iter->relas[iter->count++] = rel;
11176 /* This is a new address. Create a fag node for it. */
11177 new_fag = bfd_malloc (sizeof (struct nds32_fag));
11178 memset (new_fag, 0, sizeof (*new_fag));
11179 new_fag->addr = addr;
11180 new_fag->count = 1;
11181 new_fag->next = iter->next;
11182 new_fag->relas_capcity = INIT_RELAS_CAP;
11183 new_fag->relas = (Elf_Internal_Rela **)
11184 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
11185 new_fag->relas[0] = rel;
11186 iter->next = new_fag;
11188 nds32_fag_verify (head);
11192 nds32_fag_free_list (struct nds32_fag *head)
11194 struct nds32_fag *iter;
11199 struct nds32_fag *tmp = iter;
11208 nds32_fag_isempty (struct nds32_fag *head)
11210 return head->next == NULL;
11213 /* Find the best fp-base address.
11214 The relocation associated with that address is returned,
11215 so we can track the symbol instead of a fixed address.
11217 When relaxation, the address of an datum may change,
11218 because a text section is shrinked, so the data section
11219 moves forward. If the aligments of text and data section
11220 are different, their distance may change too.
11221 Therefore, tracking a fixed address is not appriate. */
11224 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
11226 struct nds32_fag *base; /* First fag in the window. */
11227 struct nds32_fag *last; /* First fag outside the window. */
11228 int accu = 0; /* Usage accumulation. */
11229 struct nds32_fag *best; /* Best fag. */
11230 int baccu = 0; /* Best accumulation. */
11232 /* Use first fag for initial, and find the last fag in the window.
11234 In each iteration, we could simply subtract previous fag
11235 and accumulate following fags which are inside the window,
11236 untill we each the end. */
11238 if (nds32_fag_isempty (head))
11241 /* Initialize base. */
11245 last && last->addr < base->addr + FAG_WINDOW;
11247 accu += last->count;
11251 /* Record the best base in each iteration. */
11254 accu -= base->count;
11256 /* Account fags in window. */
11257 for (/* Nothing. */;
11258 last && last->addr < base->addr + FAG_WINDOW;
11260 accu += last->count;
11262 /* A better fp-base? */
11275 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
11276 so we can convert it fo fp-relative access later.
11277 `best_fag' is the best fp-base. Only those inside the window
11278 of best_fag is applied the flag. */
11281 nds32_fag_mark_relax (struct bfd_link_info *link_info,
11282 bfd *abfd, struct nds32_fag *best_fag,
11283 Elf_Internal_Rela *internal_relocs,
11284 Elf_Internal_Rela *irelend)
11286 struct nds32_fag *ifag;
11287 bfd_vma best_fpbase, gp;
11290 output_bfd = abfd->sections->output_section->owner;
11291 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
11292 best_fpbase = best_fag->addr;
11294 if (best_fpbase > gp + sdata_range[4][1]
11295 || best_fpbase < gp - sdata_range[4][0])
11298 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
11299 so we know they can be converted to lwi37.fp. */
11300 for (ifag = best_fag;
11301 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
11305 for (i = 0; i < ifag->count; i++)
11307 Elf_Internal_Rela *insn16_rel;
11308 Elf_Internal_Rela *fag_rel;
11310 fag_rel = ifag->relas[i];
11312 /* Only if this is within the WINDOWS, FP7U2_FLAG
11315 insn16_rel = find_relocs_at_address
11316 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
11318 if (insn16_rel != irelend)
11319 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
11325 /* This is the main function of fp-as-gp optimization.
11326 It should be called by relax_section. */
11329 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
11330 bfd *abfd, asection *sec,
11331 Elf_Internal_Rela *internal_relocs,
11332 Elf_Internal_Rela *irelend,
11333 Elf_Internal_Sym *isymbuf)
11335 Elf_Internal_Rela *begin_rel = NULL;
11336 Elf_Internal_Rela *irel;
11337 struct nds32_fag fag_head;
11338 Elf_Internal_Shdr *symtab_hdr;
11339 bfd_byte *contents;
11341 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
11343 /* Per-function fp-base selection.
11344 1. Create a list for all the gp-relative access.
11345 2. Base on those gp-relative address,
11346 find a fp-base which can cover most access.
11347 3. Use the fp-base for fp-as-gp relaxation.
11349 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
11351 1. delete the `la $fp, _FP_BASE_' instruction and
11352 2. not convert lwi.gp to lwi37.fp.
11354 To delete the _FP_BASE_ instruction, we simply apply
11355 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
11357 To suppress the conversion, we simply NOT to apply
11358 R_NDS32_INSN16_FP7U2_FLAG flag. */
11360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11362 if (!nds32_get_section_contents (abfd, sec, &contents)
11363 || !nds32_get_local_syms (abfd, sec, &isymbuf))
11366 /* Check whether it is worth for fp-as-gp optimization,
11367 i.e., at least 3 gp-load.
11369 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
11370 apply this optimization. */
11372 for (irel = internal_relocs; irel < irelend; irel++)
11374 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
11375 One we enter the begin of the region, we track all the LW/ST
11376 instructions, so when we leave the region, we try to find
11377 the best fp-base address for those LW/ST instructions. */
11379 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
11380 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
11382 /* Begin of the region. */
11384 (*_bfd_error_handler) (_("%B: Nested OMIT_FP in %A."), abfd, sec);
11387 nds32_fag_init (&fag_head);
11389 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
11390 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
11393 struct nds32_fag *best_fag;
11396 /* End of the region.
11397 Check whether it is worth to do fp-as-gp. */
11399 if (begin_rel == NULL)
11401 (*_bfd_error_handler) (_("%B: Unmatched OMIT_FP in %A."), abfd, sec);
11405 accu = nds32_fag_find_base (&fag_head, &best_fag);
11407 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
11408 if (accu < FAG_THRESHOLD
11409 || !nds32_fag_mark_relax (link_info, abfd, best_fag,
11410 internal_relocs, irelend))
11412 /* Not worth to do fp-as-gp. */
11413 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
11414 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
11415 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
11416 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
11417 nds32_fag_free_list (&fag_head);
11422 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
11423 so we use it to record the distance to the reloction of best
11425 dist = best_fag->relas[0] - begin_rel;
11426 BFD_ASSERT (dist > 0 && dist < 0xffffff);
11427 /* Use high 16 bits of addend to record the _FP_BASE_ matched
11428 relocation. And get the base value when relocating. */
11429 begin_rel->r_addend |= dist << 16;
11431 nds32_fag_free_list (&fag_head);
11435 if (begin_rel == NULL)
11436 /* Skip if we are not in the region of fp-as-gp. */
11439 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
11440 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
11445 /* A gp-relative access is found. Insert it to the fag-list. */
11447 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
11448 insn = bfd_getb32 (contents + irel->r_offset);
11449 if (!N32_IS_RT3 (insn))
11452 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11453 nds32_fag_insert (&fag_head, addr, irel);
11455 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
11464 /* Remove unused `la $fp, _FD_BASE_' instruction. */
11467 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
11468 Elf_Internal_Rela *internal_relocs,
11469 Elf_Internal_Rela *irelend)
11471 Elf_Internal_Rela *irel;
11472 Elf_Internal_Shdr *symtab_hdr;
11473 bfd_byte *contents = NULL;
11474 nds32_elf_blank_t *relax_blank_list = NULL;
11475 bfd_boolean result = TRUE;
11476 bfd_boolean unused_region = FALSE;
11479 NOTE: Disable fp-as-gp if we encounter ifcall relocations.
11480 * R_NDS32_17IFC_PCREL_RELA
11481 * R_NDS32_10IFCU_PCREL_RELA
11486 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11487 nds32_get_section_contents (abfd, sec, &contents);
11489 for (irel = internal_relocs; irel < irelend; irel++)
11491 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
11492 we marked to in previous pass.
11493 DO NOT scan relocations again, since we've alreadly decided it
11494 and set the flag. */
11495 const char *syname;
11499 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
11500 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
11501 unused_region = TRUE;
11502 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
11503 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
11504 unused_region = FALSE;
11506 /* We're not in the region. */
11507 if (!unused_region)
11510 /* _FP_BASE_ must be a GLOBAL symbol. */
11511 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11512 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
11515 /* The symbol name must be _FP_BASE_. */
11516 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
11517 if (strcmp (syname, FP_BASE_NAME) != 0)
11520 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
11522 /* addi.gp $fp, -256 */
11523 insn = bfd_getb32 (contents + irel->r_offset);
11524 if (insn != INSN_ADDIGP_TO_FP)
11527 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
11529 /* addi $fp, $gp, -256 */
11530 insn = bfd_getb32 (contents + irel->r_offset);
11531 if (insn != INSN_ADDI_GP_TO_FP)
11534 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
11536 /* movi $fp, FP_BASE */
11537 insn = bfd_getb32 (contents + irel->r_offset);
11538 if (insn != INSN_MOVI_TO_FP)
11544 /* We got here because a FP_BASE instruction is found. */
11545 if (!insert_nds32_elf_blank_recalc_total
11546 (&relax_blank_list, irel->r_offset, 4))
11551 if (relax_blank_list)
11553 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
11554 relax_blank_list = NULL;
11563 /* Link-time IFC relaxation.
11564 In this optimization, we chains jump instructions
11565 of the same destination with ifcall. */
11568 /* List to save jal and j relocation. */
11569 struct elf_nds32_ifc_symbol_entry
11572 struct elf_link_hash_entry *h;
11573 struct elf_nds32_ifc_irel_list *irel_head;
11574 unsigned long insn;
11576 int enable; /* Apply ifc. */
11577 int ex9_enable; /* Apply ifc after ex9. */
11578 struct elf_nds32_ifc_symbol_entry *next;
11581 struct elf_nds32_ifc_irel_list
11583 Elf_Internal_Rela *irel;
11586 /* If this is set, then it is the last instruction for
11587 ifc-chain, so it must be keep for the actual branching. */
11589 struct elf_nds32_ifc_irel_list *next;
11592 static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;
11594 /* Insert symbol of jal and j for ifc. */
11597 nds32_elf_ifc_insert_symbol (asection *sec,
11598 struct elf_link_hash_entry *h,
11599 Elf_Internal_Rela *irel,
11600 unsigned long insn)
11602 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
11604 /* Check there is target of existing entry the same as the new one. */
11605 while (ptr != NULL)
11607 if (((h == NULL && ptr->sec == sec
11608 && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
11609 && ptr->irel_head->irel->r_addend == irel->r_addend)
11612 && ptr->insn == insn)
11614 /* The same target exist, so insert into list. */
11615 struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;
11617 while (irel_list->next != NULL)
11618 irel_list = irel_list->next;
11619 irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
11620 irel_list = irel_list->next;
11621 irel_list->irel = irel;
11622 irel_list->keep = 1;
11625 irel_list->sec = NULL;
11627 irel_list->sec = sec;
11628 irel_list->next = NULL;
11631 if (ptr->next == NULL)
11636 /* There is no same target entry, so build a new one. */
11637 if (ifc_symbol_head == NULL)
11639 ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
11640 ptr = ifc_symbol_head;
11644 ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
11649 ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
11650 ptr->irel_head->irel = irel;
11652 ptr->irel_head->keep = 1;
11656 /* Local symbols. */
11658 ptr->irel_head->sec = NULL;
11662 /* Global symbol. */
11664 ptr->irel_head->sec = sec;
11667 ptr->irel_head->next = NULL;
11670 ptr->ex9_enable = 0;
11674 /* Gather all jal and j instructions. */
11677 nds32_elf_ifc_calc (struct bfd_link_info *info,
11678 bfd *abfd, asection *sec)
11680 Elf_Internal_Rela *internal_relocs;
11681 Elf_Internal_Rela *irelend;
11682 Elf_Internal_Rela *irel;
11683 Elf_Internal_Shdr *symtab_hdr;
11684 bfd_byte *contents = NULL;
11685 unsigned long insn, insn_with_reg;
11686 unsigned long r_symndx;
11687 struct elf_link_hash_entry *h;
11688 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
11689 struct elf_nds32_link_hash_table *table;
11690 bfd_boolean ifc_loop_aware;
11692 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11693 TRUE /* keep_memory */);
11694 irelend = internal_relocs + sec->reloc_count;
11695 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11697 /* Check if the object enable ifc. */
11698 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
11699 R_NDS32_RELAX_ENTRY);
11703 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
11704 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
11705 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
11708 if (!nds32_get_section_contents (abfd, sec, &contents))
11711 table = nds32_elf_hash_table (info);
11712 ifc_loop_aware = table->ifc_loop_aware;
11713 while (irel != NULL && irel < irelend)
11715 /* Traverse all relocation and gather all of them to build the list. */
11717 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
11719 if (ifc_loop_aware == 1
11720 && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
11722 /* Check the region if loop or not. If it is true and
11723 ifc-loop-aware is true, ignore the region till region end. */
11724 while (irel != NULL
11726 && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
11727 || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
11732 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
11734 insn = bfd_getb32 (contents + irel->r_offset);
11735 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
11736 r_symndx = ELF32_R_SYM (irel->r_info);
11737 if (r_symndx < symtab_hdr->sh_info)
11739 /* Local symbol. */
11740 nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
11744 /* External symbol. */
11745 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
11746 nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
11754 /* Determine whether j and jal should be substituted. */
11757 nds32_elf_ifc_filter (struct bfd_link_info *info)
11759 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
11760 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
11761 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
11762 struct elf_nds32_link_hash_table *table;
11763 int target_optimize;
11766 table = nds32_elf_hash_table (info);
11767 target_optimize = table->target_optimize;
11770 irel_ptr = ptr->irel_head;
11771 if (ptr->h == NULL)
11773 /* Local symbol. */
11774 irel_keeper = irel_ptr;
11775 while (irel_ptr && irel_ptr->next)
11777 /* Check there is jump target can be used. */
11778 if ((irel_ptr->next->irel->r_offset
11779 - irel_keeper->irel->r_offset) > 1022)
11780 irel_keeper = irel_ptr->next;
11784 irel_ptr->keep = 0;
11786 irel_ptr = irel_ptr->next;
11791 /* Global symbol. We have to get the absolute address
11792 and decide whether to keep it or not.*/
11796 address = (irel_ptr->irel->r_offset
11797 + irel_ptr->sec->output_section->vma
11798 + irel_ptr->sec->output_offset);
11799 irel_ptr->addr = address;
11800 irel_ptr = irel_ptr->next;
11803 irel_ptr = ptr->irel_head;
11806 struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
11807 struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
11808 struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
11809 struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;
11811 while (irel_temp->next)
11813 if (irel_temp->next->addr < irel_dest->addr)
11815 irel_dest_prev = irel_temp;
11816 irel_dest = irel_temp->next;
11818 irel_temp = irel_temp->next;
11820 if (irel_dest != irel_ptr)
11823 irel_ptr_prev->next = irel_dest;
11824 if (irel_dest_prev)
11825 irel_dest_prev->next = irel_ptr;
11826 irel_temp = irel_ptr->next;
11827 irel_ptr->next = irel_dest->next;
11828 irel_dest->next = irel_temp;
11830 irel_ptr_prev = irel_ptr;
11831 irel_ptr = irel_ptr->next;
11834 irel_ptr = ptr->irel_head;
11835 irel_keeper = irel_ptr;
11836 while (irel_ptr && irel_ptr->next)
11838 if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
11839 irel_keeper = irel_ptr->next;
11843 irel_ptr->keep = 0;
11845 irel_ptr = irel_ptr->next;
11849 /* Ex9 enable. Reserve it for ex9. */
11850 if ((target_optimize & NDS32_RELAX_EX9_ON)
11851 && ptr->irel_head != irel_keeper)
11857 /* Determine whether j and jal should be substituted after ex9 done. */
11860 nds32_elf_ifc_filter_after_ex9 (void)
11862 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
11863 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
11867 if (ptr->enable == 0)
11869 /* Check whether ifc is applied or not. */
11870 irel_ptr = ptr->irel_head;
11871 ptr->ex9_enable = 1;
11874 if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
11877 ptr->ex9_enable = 0;
11880 irel_ptr = irel_ptr->next;
11887 /* Wrapper to do ifc relaxation. */
11890 nds32_elf_ifc_finish (struct bfd_link_info *info)
11893 struct elf_nds32_link_hash_table *table;
11895 table = nds32_elf_hash_table (info);
11896 relax_status = table->relax_status;
11898 if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
11899 nds32_elf_ifc_filter (info);
11901 nds32_elf_ifc_filter_after_ex9 ();
11903 if (!nds32_elf_ifc_replace (info))
11907 table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
11911 /* Traverse the result of ifc filter and replace it with ifcall9. */
11914 nds32_elf_ifc_replace (struct bfd_link_info *info)
11916 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
11917 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
11918 nds32_elf_blank_t *relax_blank_list = NULL;
11919 bfd_byte *contents = NULL;
11920 Elf_Internal_Rela *internal_relocs;
11921 Elf_Internal_Rela *irel;
11922 Elf_Internal_Rela *irelend;
11923 unsigned short insn16 = INSN_IFCALL9;
11924 struct elf_nds32_link_hash_table *table;
11927 table = nds32_elf_hash_table (info);
11928 relax_status = table->relax_status;
11932 /* Traverse the ifc gather list, and replace the
11933 filter entries by ifcall9. */
11934 if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
11935 || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->ex9_enable == 1))
11937 irel_ptr = ptr->irel_head;
11938 if (ptr->h == NULL)
11940 /* Local symbol. */
11941 internal_relocs = _bfd_elf_link_read_relocs
11942 (ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
11943 irelend = internal_relocs + ptr->sec->reloc_count;
11945 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec, &contents))
11950 if (irel_ptr->keep == 0 && irel_ptr->next)
11952 /* The one can be replaced. We have to check whether
11953 there is any alignment point in the region. */
11954 irel = irel_ptr->irel;
11955 while (((irel_ptr->next->keep == 0 && irel < irel_ptr->next->irel)
11956 || (irel_ptr->next->keep == 1 && irel < irelend))
11957 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
11958 && (irel->r_addend & 0x1f) == 2))
11960 if (irel >= irelend
11961 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
11962 && (irel->r_addend & 0x1f) == 2
11963 && ((irel->r_offset
11964 - get_nds32_elf_blank_total
11965 (&relax_blank_list, irel->r_offset, 1)) & 0x02) == 0))
11967 /* Replace by ifcall9. */
11968 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
11969 if (!insert_nds32_elf_blank_recalc_total
11970 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
11972 irel_ptr->irel->r_info =
11973 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
11976 irel_ptr = irel_ptr->next;
11979 /* Delete the redundant code. */
11980 if (relax_blank_list)
11982 nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
11984 relax_blank_list = NULL;
11989 /* Global symbol. */
11992 if (irel_ptr->keep == 0 && irel_ptr->next)
11994 /* The one can be replaced, and we have to check
11995 whether there is any alignment point in the region. */
11996 internal_relocs = _bfd_elf_link_read_relocs
11997 (irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
11998 TRUE /* keep_memory */);
11999 irelend = internal_relocs + irel_ptr->sec->reloc_count;
12000 if (!nds32_get_section_contents
12001 (irel_ptr->sec->owner, irel_ptr->sec, &contents))
12004 irel = irel_ptr->irel;
12005 while (((irel_ptr->sec == irel_ptr->next->sec
12006 && irel_ptr->next->keep == 0
12007 && irel < irel_ptr->next->irel)
12008 || ((irel_ptr->sec != irel_ptr->next->sec
12009 || irel_ptr->next->keep == 1)
12010 && irel < irelend))
12011 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12012 && (irel->r_addend & 0x1f) == 2))
12014 if (irel >= irelend
12015 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12016 && (irel->r_addend & 0x1f) == 2
12017 && ((irel->r_offset
12018 - get_nds32_elf_blank_total (&relax_blank_list,
12019 irel->r_offset, 1)) & 0x02) == 0))
12021 /* Replace by ifcall9. */
12022 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
12023 if (!insert_nds32_elf_blank_recalc_total
12024 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
12027 /* Delete the redundant code, and clear the relocation. */
12028 nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
12031 irel_ptr->irel->r_info =
12032 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
12033 relax_blank_list = NULL;
12037 irel_ptr = irel_ptr->next;
12047 /* Relocate ifcall. */
12050 nds32_elf_ifc_reloc (void)
12052 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
12053 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
12054 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
12055 bfd_vma relocation, address;
12056 unsigned short insn16;
12058 bfd_byte *contents = NULL;
12062 if (ptr->enable == 1 || ptr->ex9_enable == 1)
12064 /* Check the entry is enable ifcall. */
12065 irel_ptr = ptr->irel_head;
12068 if (irel_ptr->keep == 1)
12070 irel_keeper = irel_ptr;
12073 irel_ptr = irel_ptr->next;
12076 irel_ptr = ptr->irel_head;
12077 if (ptr->h == NULL)
12079 /* Local symbol. */
12080 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec, &contents))
12085 if (irel_ptr->keep == 0
12086 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
12088 relocation = irel_keeper->irel->r_offset;
12089 relocation = relocation - irel_ptr->irel->r_offset;
12090 while (irel_keeper && relocation > 1022)
12092 irel_keeper = irel_keeper->next;
12093 if (irel_keeper && irel_keeper->keep == 1)
12095 relocation = irel_keeper->irel->r_offset;
12096 relocation = relocation - irel_ptr->irel->r_offset;
12099 if (relocation > 1022)
12101 /* Double check. */
12102 irel_keeper = ptr->irel_head;
12103 while (irel_keeper)
12105 if (irel_keeper->keep == 1)
12107 relocation = irel_keeper->irel->r_offset;
12108 relocation = relocation - irel_ptr->irel->r_offset;
12110 if (relocation <= 1022)
12112 irel_keeper = irel_keeper->next;
12118 insn16 = INSN_IFCALL9 | (relocation >> 1);
12119 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
12121 irel_ptr = irel_ptr->next;
12126 /* Global symbol. */
12129 if (irel_ptr->keep == 0
12130 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
12132 relocation = (irel_keeper->irel->r_offset
12133 + irel_keeper->sec->output_section->vma
12134 + irel_keeper->sec->output_offset);
12135 address = (irel_ptr->irel->r_offset
12136 + irel_ptr->sec->output_section->vma
12137 + irel_ptr->sec->output_offset);
12138 relocation = relocation - address;
12139 while (irel_keeper && relocation > 1022)
12141 irel_keeper = irel_keeper->next;
12142 if (irel_keeper && irel_keeper->keep ==1)
12144 relocation = (irel_keeper->irel->r_offset
12145 + irel_keeper->sec->output_section->vma
12146 + irel_keeper->sec->output_offset);
12147 relocation = relocation - address;
12151 if (relocation > 1022)
12153 /* Double check. */
12154 irel_keeper = ptr->irel_head;
12155 while (irel_keeper)
12157 if (irel_keeper->keep == 1)
12160 relocation = (irel_keeper->irel->r_offset
12161 + irel_keeper->sec->output_section->vma
12162 + irel_keeper->sec->output_offset);
12163 relocation = relocation - address;
12165 if (relocation <= 1022)
12167 irel_keeper = irel_keeper->next;
12172 if (!nds32_get_section_contents
12173 (irel_ptr->sec->owner, irel_ptr->sec, &contents))
12175 insn16 = INSN_IFCALL9 | (relocation >> 1);
12176 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
12178 irel_ptr =irel_ptr->next;
12188 /* End of IFC relaxation. */
12190 /* EX9 Instruction Table Relaxation. */
12192 /* Global hash list. */
12193 struct elf_link_hash_entry_list
12195 struct elf_link_hash_entry *h;
12196 struct elf_link_hash_entry_list *next;
12199 /* Save different destination but same insn. */
12200 struct elf_link_hash_entry_mul_list
12202 /* Global symbol times. */
12204 /* Save relocation for each global symbol but useful?? */
12205 Elf_Internal_Rela *irel;
12206 /* For sethi, two sethi may have the same high-part but different low-parts. */
12207 Elf_Internal_Rela rel_backup;
12208 struct elf_link_hash_entry_list *h_list;
12209 struct elf_link_hash_entry_mul_list *next;
12212 /* Instruction hash table. */
12213 struct elf_nds32_code_hash_entry
12215 struct bfd_hash_entry root;
12217 /* For insn that can use relocation or constant ex: sethi. */
12220 struct elf_link_hash_entry_mul_list *m_list;
12221 /* Using r_addend. */
12222 Elf_Internal_Rela *irel;
12223 /* Using r_info. */
12224 Elf_Internal_Rela rel_backup;
12227 /* Instruction count list. */
12228 struct elf_nds32_insn_times_entry
12230 const char *string;
12234 struct elf_link_hash_entry_mul_list *m_list;
12235 Elf_Internal_Rela *irel;
12236 Elf_Internal_Rela rel_backup;
12237 struct elf_nds32_insn_times_entry *next;
12240 /* J and JAL symbol list. */
12241 struct elf_nds32_symbol_entry
12244 unsigned long insn;
12245 struct elf_nds32_symbol_entry *next;
12248 /* Relocation list. */
12249 struct elf_nds32_irel_entry
12251 Elf_Internal_Rela *irel;
12252 struct elf_nds32_irel_entry *next;
12255 /* ex9.it insn need to be fixed. */
12256 struct elf_nds32_ex9_refix
12258 Elf_Internal_Rela *irel;
12260 struct elf_link_hash_entry *h;
12262 struct elf_nds32_ex9_refix *next;
12265 static struct bfd_hash_table ex9_code_table;
12266 static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
12267 static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;
12269 /* EX9 hash function. */
12271 static struct bfd_hash_entry *
12272 nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
12273 struct bfd_hash_table *table,
12274 const char *string)
12276 struct elf_nds32_code_hash_entry *ret;
12278 /* Allocate the structure if it has not already been allocated by a
12282 entry = (struct bfd_hash_entry *)
12283 bfd_hash_allocate (table, sizeof (*ret));
12288 /* Call the allocation method of the superclass. */
12289 entry = bfd_hash_newfunc (entry, table, string);
12293 ret = (struct elf_nds32_code_hash_entry*) entry;
12295 ret->const_insn = 0;
12296 ret->m_list = NULL;
12302 /* Insert ex9 entry
12303 this insert must be stable sorted by times. */
12306 nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
12308 struct elf_nds32_insn_times_entry *temp;
12309 struct elf_nds32_insn_times_entry *temp2;
12311 if (ex9_insn_head == NULL)
12313 ex9_insn_head = ptr;
12318 temp = ex9_insn_head;
12319 temp2 = ex9_insn_head;
12320 while (temp->next &&
12321 (temp->next->times >= ptr->times
12322 || temp->times == -1))
12324 if (temp->times == -1)
12328 if (ptr->times > temp->times && temp->times != -1)
12331 if (temp2->times == -1)
12334 ex9_insn_head = ptr;
12336 else if (temp->next == NULL)
12343 ptr->next = temp->next;
12349 /* Examine each insn times in hash table.
12350 Handle multi-link hash entry.
12352 TODO: This function doesn't assign so much info since it is fake. */
12355 nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
12357 struct elf_nds32_insn_times_entry *ptr;
12360 if (h->m_list == NULL)
12362 /* Local symbol insn or insn without relocation. */
12366 ptr = (struct elf_nds32_insn_times_entry *)
12367 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12368 ptr->times = h->times;
12369 ptr->string = h->root.string;
12370 ptr->m_list = NULL;
12372 ptr->irel = h->irel;
12373 ptr->rel_backup = h->rel_backup;
12374 nds32_elf_ex9_insert_entry (ptr);
12378 /* Global symbol insn. */
12379 /* Only sethi insn has multiple m_list. */
12380 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
12385 times += m_list->times;
12386 m_list = m_list->next;
12390 m_list = h->m_list;
12391 ptr = (struct elf_nds32_insn_times_entry *)
12392 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12393 ptr->times = times; /* Use the total times. */
12394 ptr->string = h->root.string;
12395 ptr->m_list = m_list;
12397 ptr->irel = m_list->irel;
12398 ptr->rel_backup = m_list->rel_backup;
12399 nds32_elf_ex9_insert_entry (ptr);
12401 if (h->const_insn == 1)
12403 /* sethi with constant value. */
12407 ptr = (struct elf_nds32_insn_times_entry *)
12408 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12409 ptr->times = h->times;
12410 ptr->string = h->root.string;
12411 ptr->m_list = NULL;
12414 ptr->rel_backup = h->rel_backup;
12415 nds32_elf_ex9_insert_entry (ptr);
12421 /* Count each insn times in hash table.
12422 Handle multi-link hash entry. */
12425 nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
12427 int reservation, times;
12428 unsigned long relocation, min_relocation;
12429 struct elf_nds32_insn_times_entry *ptr;
12431 if (h->m_list == NULL)
12433 /* Local symbol insn or insn without relocation. */
12436 ptr = (struct elf_nds32_insn_times_entry *)
12437 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12438 ptr->times = h->times;
12439 ptr->string = h->root.string;
12440 ptr->m_list = NULL;
12442 ptr->irel = h->irel;
12443 ptr->rel_backup = h->rel_backup;
12444 nds32_elf_ex9_insert_entry (ptr);
12448 /* Global symbol insn. */
12449 /* Only sethi insn has multiple m_list. */
12450 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
12452 if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
12453 && m_list->next != NULL)
12455 /* Sethi insn has different symbol or addend but has same hi20. */
12459 min_relocation = 0xffffffff;
12462 /* Get the minimum sethi address
12463 and calculate how many entry the sethi-list have to use. */
12464 if ((m_list->h_list->h->root.type == bfd_link_hash_defined
12465 || m_list->h_list->h->root.type == bfd_link_hash_defweak)
12466 && (m_list->h_list->h->root.u.def.section != NULL
12467 && m_list->h_list->h->root.u.def.section->output_section != NULL))
12469 relocation = (m_list->h_list->h->root.u.def.value +
12470 m_list->h_list->h->root.u.def.section->output_section->vma +
12471 m_list->h_list->h->root.u.def.section->output_offset);
12472 relocation += m_list->irel->r_addend;
12476 if (relocation < min_relocation)
12477 min_relocation = relocation;
12478 times += m_list->times;
12479 m_list = m_list->next;
12481 if (min_relocation < ex9_relax_size)
12482 reservation = (min_relocation >> 12) + 1;
12484 reservation = (min_relocation >> 12)
12485 - ((min_relocation - ex9_relax_size) >> 12) + 1;
12486 if (reservation < (times / 3))
12488 /* Efficient enough to use ex9. */
12491 for (i = reservation ; i > 0; i--)
12493 /* Allocate number of reservation ex9 entry. */
12494 ptr = (struct elf_nds32_insn_times_entry *)
12495 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12496 ptr->times = h->m_list->times / reservation;
12497 ptr->string = h->root.string;
12498 ptr->m_list = h->m_list;
12500 ptr->irel = h->m_list->irel;
12501 ptr->rel_backup = h->m_list->rel_backup;
12502 nds32_elf_ex9_insert_entry (ptr);
12508 /* Normal global symbol that means no different address symbol
12509 using same ex9 entry. */
12510 if (m_list->times >= 3)
12512 ptr = (struct elf_nds32_insn_times_entry *)
12513 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12514 ptr->times = m_list->times;
12515 ptr->string = h->root.string;
12516 ptr->m_list = h->m_list;
12518 ptr->irel = h->m_list->irel;
12519 ptr->rel_backup = h->m_list->rel_backup;
12520 nds32_elf_ex9_insert_entry (ptr);
12524 if (h->const_insn == 1)
12526 /* sethi with constant value. */
12530 ptr = (struct elf_nds32_insn_times_entry *)
12531 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12532 ptr->times = h->times;
12533 ptr->string = h->root.string;
12534 ptr->m_list = NULL;
12537 ptr->rel_backup = h->rel_backup;
12538 nds32_elf_ex9_insert_entry (ptr);
12545 /* Hash table traverse function. */
12548 nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
12552 ex9_code_table.frozen = 1;
12553 for (i = 0; i < ex9_code_table.size; i++)
12555 struct bfd_hash_entry *p;
12557 for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
12558 if (!func ((struct elf_nds32_code_hash_entry *) p))
12562 ex9_code_table.frozen = 0;
12566 /* Give order number to insn list. */
12569 nds32_elf_order_insn_times (struct bfd_link_info *info)
12571 struct elf_nds32_insn_times_entry *ex9_insn;
12572 struct elf_nds32_insn_times_entry *temp;
12573 struct elf_nds32_link_hash_table *table;
12576 int number = 0, total = 0;
12577 struct bfd_link_hash_entry *bh;
12579 /* The max number of entries is 512. */
12580 ex9_insn = ex9_insn_head;
12581 table = nds32_elf_hash_table (info);
12582 ex9_limit = table->ex9_limit;
12584 /* Get the minimun one of ex9 list and limitation. */
12588 ex9_insn = ex9_insn->next;
12590 total = MIN (total, ex9_limit);
12592 temp = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
12593 temp->string = bfd_malloc (sizeof (char) * 10);
12596 temp->m_list = NULL;
12599 /* Since the struct elf_nds32_insn_times_entry string is const char,
12600 it has to allocate another space to write break 0xea. */
12601 insn = bfd_malloc (sizeof (char) * 10);
12602 snprintf (insn, sizeof (char) * 10, "%08x", INSN_BREAK_EA);
12603 temp->string = (const char *) insn;
12605 ex9_insn = ex9_insn_head;
12607 while (ex9_insn != NULL && number <= ex9_limit)
12609 /* Save 234th entry for break 0xea, because trace32 need to use
12610 break16 0xea. If the number of entry is less than 234, adjust
12611 the address of _ITB_BASE_ backward. */
12614 ex9_insn->order = number + 234 - total;
12615 if (!ex9_insn->next)
12617 /* Link break 0xea entry into list. */
12618 ex9_insn->next = temp;
12620 temp ->order = number + 235 - total;
12626 ex9_insn->order = number;
12632 /* Link break 0xea entry into list. */
12633 temp->next = ex9_insn->next;
12634 ex9_insn->next = temp;
12635 temp->order = number;
12637 ex9_insn = ex9_insn->next;
12640 if (number > ex9_limit)
12643 ex9_insn = ex9_insn->next;
12647 ex9_insn = ex9_insn->next;
12652 /* Adjust the address of _ITB_BASE_. */
12653 bh = bfd_link_hash_lookup (info->hash, "_ITB_BASE_",
12654 FALSE, FALSE, FALSE);
12656 bh->u.def.value = (total - 234) * 4;
12659 while (ex9_insn != NULL)
12661 /* Free useless entry. */
12663 ex9_insn = ex9_insn->next;
12668 /* Build .ex9.itable section. */
12671 nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
12673 asection *table_sec;
12674 struct elf_nds32_insn_times_entry *ptr;
12677 bfd_byte *contents = NULL;
12679 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
12680 it_abfd = it_abfd->link.next)
12682 /* Find the section .ex9.itable, and put all entries into it. */
12683 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
12684 if (table_sec != NULL)
12686 if (!nds32_get_section_contents (it_abfd, table_sec, &contents))
12689 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
12692 table_sec->size = number * 4;
12696 /* There is no insntruction effective enough to convert to ex9.
12697 Only add break 0xea into ex9 table. */
12698 table_sec->size = 4;
12699 bfd_putb32 ((bfd_vma) INSN_BREAK_EA, (char *) contents);
12703 elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
12705 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
12709 val = strtol (ptr->string, NULL, 16);
12710 bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
12718 /* Get insn with regs according to relocation type. */
12721 nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
12722 unsigned long insn, unsigned long *insn_with_reg)
12724 reloc_howto_type *howto = NULL;
12727 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
12728 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
12729 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
12731 *insn_with_reg = insn;
12735 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
12736 *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
12739 /* Mask number of address bits according to relocation. */
12741 static unsigned long
12742 nds32_elf_irel_mask (Elf_Internal_Rela *irel)
12744 reloc_howto_type *howto = NULL;
12747 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
12748 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
12749 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
12752 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
12753 return howto->dst_mask;
12757 nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
12758 struct elf_nds32_irel_entry *irel_ptr)
12760 if (*irel_list == NULL)
12762 *irel_list = irel_ptr;
12763 irel_ptr->next = NULL;
12767 irel_ptr->next = *irel_list;
12768 *irel_list = irel_ptr;
12773 nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
12774 struct elf_link_hash_entry *h, int order)
12776 struct elf_nds32_ex9_refix *ptr;
12778 ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
12782 ptr->order = order;
12785 if (ex9_refix_head == NULL)
12786 ex9_refix_head = ptr;
12789 struct elf_nds32_ex9_refix *temp = ex9_refix_head;
12791 while (temp->next != NULL)
12800 CLEAN_PRE = 1 << 1,
12804 /* Check relocation type if supporting for ex9. */
12807 nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
12808 Elf_Internal_Rela **irel,
12809 Elf_Internal_Rela *irelend,
12810 nds32_elf_blank_t *relax_blank_list,
12812 long unsigned int *off,
12813 bfd_byte *contents)
12815 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
12816 bfd_boolean nested_ex9, nested_loop;
12817 bfd_boolean ex9_loop_aware;
12818 /* We use the highest 1 byte of result to record
12819 how many bytes location counter has to move. */
12821 Elf_Internal_Rela *irel_save = NULL;
12822 struct elf_nds32_link_hash_table *table;
12824 table = nds32_elf_hash_table (info);
12825 ex9_loop_aware = table->ex9_loop_aware;
12827 while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
12829 switch (ELF32_R_TYPE ((*irel)->r_info))
12831 case R_NDS32_RELAX_REGION_BEGIN:
12832 /* Ignore code block. */
12833 nested_ex9 = FALSE;
12834 nested_loop = FALSE;
12835 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
12837 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
12839 /* Check the region if loop or not. If it is true and
12840 ex9-loop-aware is true, ignore the region till region end. */
12841 /* To save the status for in .no_relax ex9 region and
12842 loop region to conform the block can do ex9 relaxation. */
12843 nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
12844 nested_loop = (ex9_loop_aware
12845 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
12846 while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
12849 if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
12851 /* There may be nested region. */
12852 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
12854 else if (ex9_loop_aware
12855 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
12856 nested_loop = TRUE;
12858 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
12860 /* The end of region. */
12861 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
12862 nested_ex9 = FALSE;
12863 else if (ex9_loop_aware
12864 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
12865 nested_loop = FALSE;
12867 else if (relax_blank_list
12868 && ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
12869 && ((*irel)->r_addend & 0x1f) == 2)
12871 /* Alignment exist in the region. */
12872 result |= CLEAN_PRE;
12873 if (((*irel)->r_offset -
12874 get_nds32_elf_blank_total (&relax_blank_list,
12875 (*irel)->r_offset, 0)) & 0x02)
12876 result |= PUSH_PRE;
12879 if ((*irel) >= irelend)
12882 *off = (*irel)->r_offset;
12884 /* The final instruction in the region, regard this one as data to ignore it. */
12885 result |= DATA_EXIST;
12890 case R_NDS32_LABEL:
12891 if (relax_blank_list && ((*irel)->r_addend & 0x1f) == 2)
12893 /* Check this point is align and decide to do ex9 or not. */
12894 result |= CLEAN_PRE;
12895 if (((*irel)->r_offset -
12896 get_nds32_elf_blank_total (&relax_blank_list,
12897 (*irel)->r_offset, 0)) & 0x02)
12898 result |= PUSH_PRE;
12901 case R_NDS32_32_RELA:
12903 result |= (4 << 24);
12904 result |= DATA_EXIST;
12906 case R_NDS32_16_RELA:
12908 result |= (2 << 24);
12909 result |= DATA_EXIST;
12913 /* The least code alignment is 2. If the data is only one byte,
12914 we have to shift one more byte. */
12915 if ((*irel)->r_addend == 1)
12916 result |= ((*irel)->r_addend << 25) ;
12918 result |= ((*irel)->r_addend << 24) ;
12920 result |= DATA_EXIST;
12923 case R_NDS32_25_PCREL_RELA:
12924 case R_NDS32_SDA16S3_RELA:
12925 case R_NDS32_SDA15S3_RELA:
12926 case R_NDS32_SDA15S3:
12927 case R_NDS32_SDA17S2_RELA:
12928 case R_NDS32_SDA15S2_RELA:
12929 case R_NDS32_SDA12S2_SP_RELA:
12930 case R_NDS32_SDA12S2_DP_RELA:
12931 case R_NDS32_SDA15S2:
12932 case R_NDS32_SDA18S1_RELA:
12933 case R_NDS32_SDA15S1_RELA:
12934 case R_NDS32_SDA15S1:
12935 case R_NDS32_SDA19S0_RELA:
12936 case R_NDS32_SDA15S0_RELA:
12937 case R_NDS32_SDA15S0:
12938 case R_NDS32_HI20_RELA:
12939 case R_NDS32_LO12S0_ORI_RELA:
12940 case R_NDS32_LO12S0_RELA:
12941 case R_NDS32_LO12S1_RELA:
12942 case R_NDS32_LO12S2_RELA:
12943 /* These relocation is supported ex9 relaxation currently. */
12944 /* We have to save the relocation for using later, since we have
12945 to check there is any alignment in the same address. */
12949 /* Not support relocations. */
12950 if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
12951 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE)
12953 /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
12954 But we have to consider if there is any side-effect. */
12955 if (!(result & DATA_EXIST))
12957 /* We have to confirm there is no data relocation in the
12958 same address. In general case, this won't happen. */
12959 /* We have to do ex9 conservative, for those relocation not
12960 considerd we ignore instruction. */
12961 result |= DATA_EXIST;
12962 if (*(contents + *off) & 0x80)
12963 result |= (2 << 24);
12965 result |= (4 << 24);
12970 if ((*irel) < irelend
12971 && ((*irel) + 1) < irelend
12972 && (*irel)->r_offset == ((*irel) + 1)->r_offset)
12973 /* There are relocations pointing to the same address, we have to
12974 check all of them. */
12986 /* Replace input file instruction which is in ex9 itable. */
12989 nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
12991 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
12992 bfd_byte *contents = NULL;
12993 long unsigned int off;
12994 unsigned short insn16, insn_ex9;
12995 /* `pre_*' are used to track previous instruction that can use ex9.it. */
12996 unsigned int pre_off = -1;
12997 unsigned short pre_insn16 = 0;
12998 struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
12999 Elf_Internal_Rela *internal_relocs;
13000 Elf_Internal_Rela *irel;
13001 Elf_Internal_Rela *irelend;
13002 Elf_Internal_Shdr *symtab_hdr;
13003 Elf_Internal_Sym *isym = NULL;
13004 nds32_elf_blank_t *relax_blank_list = NULL;
13005 unsigned long insn = 0;
13006 unsigned long insn_with_reg = 0;
13007 unsigned long it_insn;
13008 unsigned long it_insn_with_reg;
13009 unsigned long r_symndx;
13011 struct elf_nds32_irel_entry *irel_list = NULL;
13012 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
13013 int data_flag, do_replace, save_irel;
13015 /* Load section instructions, relocations, and symbol table. */
13016 if (!nds32_get_section_contents (abfd, sec, &contents)
13017 || !nds32_get_local_syms (abfd, sec, &isym))
13019 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13020 TRUE /* keep_memory */);
13021 irelend = internal_relocs + sec->reloc_count;
13022 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13026 /* Check if the object enable ex9. */
13027 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
13028 R_NDS32_RELAX_ENTRY);
13030 /* Check this section trigger ex9 relaxation. */
13033 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
13034 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
13035 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
13038 irel = internal_relocs;
13040 /* Check alignment and fetch proper relocation. */
13041 while (off < sec->size)
13043 struct elf_link_hash_entry *h = NULL;
13044 struct elf_nds32_irel_entry *irel_ptr = NULL;
13046 /* Syn the instruction and the relocation. */
13047 while (irel != NULL && irel < irelend && irel->r_offset < off)
13050 data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
13051 relax_blank_list, sec,
13053 if (data_flag & PUSH_PRE)
13055 if (pre_insn16 != 0)
13057 /* Implement the ex9 relaxation. */
13058 bfd_putb16 (pre_insn16, contents + pre_off);
13059 if (!insert_nds32_elf_blank_recalc_total
13060 (&relax_blank_list, pre_off + 2, 2))
13062 if (pre_irel_ptr != NULL)
13063 nds32_elf_insert_irel_entry (&irel_list,
13068 if (data_flag & CLEAN_PRE)
13072 pre_irel_ptr = NULL;
13074 if (data_flag & DATA_EXIST)
13076 /* We save the move offset in the highest byte. */
13077 off += (data_flag >> 24);
13081 if (*(contents + off) & 0x80)
13083 /* 2-byte instruction. */
13088 /* Load the instruction and its opcode with register for comparing. */
13089 ex9_insn = ex9_insn_head;
13090 insn = bfd_getb32 (contents + off);
13094 it_insn = strtol (ex9_insn->string, NULL, 16);
13095 it_insn_with_reg = 0;
13099 if (irel != NULL && irel < irelend && irel->r_offset == off)
13101 /* Insn with relocation. */
13102 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
13104 if (ex9_insn->irel != NULL)
13105 nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn, &it_insn_with_reg);
13107 if (ex9_insn->irel != NULL
13108 && ELF32_R_TYPE (irel->r_info) == ELF32_R_TYPE (ex9_insn->irel->r_info)
13109 && (insn_with_reg == it_insn_with_reg))
13111 /* Insn relocation and format is the same as table entry. */
13113 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
13114 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
13115 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
13116 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
13117 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
13118 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
13119 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
13120 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
13121 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
13122 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
13123 && ELF32_R_TYPE (irel->r_info) <=
13124 R_NDS32_SDA12S2_SP_RELA)
13125 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
13126 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
13128 r_symndx = ELF32_R_SYM (irel->r_info);
13129 if (r_symndx < symtab_hdr->sh_info)
13131 /* Local symbol. */
13132 int shndx = isym[r_symndx].st_shndx;
13134 isec = elf_elfsections (abfd)[shndx]->bfd_section;
13135 if (ex9_insn->sec == isec
13136 && ex9_insn->irel->r_addend == irel->r_addend
13137 && ex9_insn->irel->r_info == irel->r_info)
13145 /* External symbol. */
13146 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13147 if (ex9_insn->m_list)
13149 struct elf_link_hash_entry_list *h_list;
13151 h_list = ex9_insn->m_list->h_list;
13155 && ex9_insn->m_list->irel->r_addend == irel->r_addend)
13161 h_list = h_list->next;
13166 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
13168 r_symndx = ELF32_R_SYM (irel->r_info);
13169 if (r_symndx < symtab_hdr->sh_info)
13171 /* Local symbols. Compare its base symbol and offset. */
13172 int shndx = isym[r_symndx].st_shndx;
13174 isec = elf_elfsections (abfd)[shndx]->bfd_section;
13175 if (ex9_insn->sec == isec
13176 && ex9_insn->irel->r_addend == irel->r_addend
13177 && ex9_insn->irel->r_info == irel->r_info)
13185 /* External symbol. */
13186 struct elf_link_hash_entry_mul_list *m_list;
13188 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13189 m_list = ex9_insn->m_list;
13193 struct elf_link_hash_entry_list *h_list = m_list->h_list;
13198 && m_list->irel->r_addend == irel->r_addend)
13203 && ex9_insn->m_list
13204 && ex9_insn->m_list == ex9_insn->next->m_list)
13206 /* sethi multiple entry must be fixed */
13207 nds32_elf_ex9_insert_fix (sec, irel,
13208 h, ex9_insn->order);
13212 h_list = h_list->next;
13214 m_list = m_list->next;
13220 /* Import table: Check the symbol hash table and the
13221 jump target. Only R_NDS32_25_PCREL_RELA now. */
13222 else if (ex9_insn->times == -1
13223 && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
13225 nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
13226 if (insn_with_reg == it_insn_with_reg)
13229 bfd_vma relocation;
13231 r_symndx = ELF32_R_SYM (irel->r_info);
13232 if (r_symndx >= symtab_hdr->sh_info)
13234 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13235 if ((h->root.type == bfd_link_hash_defined
13236 || h->root.type == bfd_link_hash_defweak)
13237 && (h->root.u.def.section != NULL
13238 && h->root.u.def.section->output_section != NULL)
13239 && h->root.u.def.section->gc_mark == 1
13240 && strcmp (h->root.u.def.section->name,
13241 BFD_ABS_SECTION_NAME) == 0
13242 && h->root.u.def.value > sec->size)
13244 relocation = (h->root.u.def.value +
13245 h->root.u.def.section->output_section->vma +
13246 h->root.u.def.section->output_offset);
13247 relocation += irel->r_addend;
13248 insn = insn_with_reg | ((relocation >> 1) & 0xffffff);
13249 snprintf (code, sizeof (code), "%08lx", insn);
13250 if (strcmp (code, ex9_insn->string) == 0)
13259 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13260 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END)
13262 /* These relocations do not have to relocate contens, so it can
13263 be regard as instruction without relocation. */
13264 if (insn == it_insn && ex9_insn->irel == NULL)
13270 /* Instruction without relocation, we only
13271 have to compare their byte code. */
13272 if (insn == it_insn && ex9_insn->irel == NULL)
13276 /* Insntruction match so replacing the code here. */
13277 if (do_replace == 1)
13279 /* There are two formats of ex9 instruction. */
13280 if (ex9_insn->order < 32)
13281 insn_ex9 = INSN_EX9_IT_2;
13283 insn_ex9 = INSN_EX9_IT_1;
13284 insn16 = insn_ex9 | ex9_insn->order;
13286 if (pre_insn16 != 0)
13288 bfd_putb16 (pre_insn16, contents + pre_off);
13289 if (!insert_nds32_elf_blank_recalc_total
13290 (&relax_blank_list, pre_off + 2, 2))
13292 if (pre_irel_ptr != NULL)
13293 nds32_elf_insert_irel_entry (&irel_list, pre_irel_ptr);
13296 pre_insn16 = insn16;
13300 /* For instuction with relocation do relax. */
13301 irel_ptr = (struct elf_nds32_irel_entry *)
13302 bfd_malloc (sizeof (struct elf_nds32_irel_entry));
13303 irel_ptr->irel = irel;
13304 irel_ptr->next = NULL;
13305 pre_irel_ptr = irel_ptr;
13308 pre_irel_ptr = NULL;
13311 ex9_insn = ex9_insn->next;
13316 if (pre_insn16 != 0)
13318 /* Implement the ex9 relaxation. */
13319 bfd_putb16 (pre_insn16, contents + pre_off);
13320 if (!insert_nds32_elf_blank_recalc_total
13321 (&relax_blank_list, pre_off + 2, 2))
13323 if (pre_irel_ptr != NULL)
13324 nds32_elf_insert_irel_entry (&irel_list, pre_irel_ptr);
13327 /* Delete the redundant code. */
13328 if (relax_blank_list)
13330 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13331 relax_blank_list = NULL;
13334 /* Clear the relocation that is replaced by ex9. */
13337 struct elf_nds32_irel_entry *irel_ptr;
13339 irel_ptr = irel_list;
13340 irel_list = irel_ptr->next;
13341 irel_ptr->irel->r_info =
13342 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
13348 /* Initialize ex9 hash table. */
13351 nds32_elf_ex9_init (void)
13353 if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
13354 sizeof (struct elf_nds32_code_hash_entry),
13357 (*_bfd_error_handler) (_("Linker: cannot init ex9 hash table error \n"));
13363 /* Predict how many bytes will be relaxed with ex9 and ifc. */
13366 nds32_elf_ex9_total_relax (struct bfd_link_info *info)
13368 struct elf_nds32_insn_times_entry *ex9_insn;
13369 struct elf_nds32_insn_times_entry *temp;
13370 int target_optimize;
13371 struct elf_nds32_link_hash_table *table;
13373 if (ex9_insn_head == NULL)
13376 table = nds32_elf_hash_table (info);
13377 target_optimize = table->target_optimize;
13378 ex9_insn = ex9_insn_head;
13381 ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
13383 ex9_insn = ex9_insn->next;
13386 ex9_insn_head = NULL;
13388 if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
13390 /* Examine ifc reduce size. */
13391 struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
13392 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13397 if (ifc_ent->enable == 0)
13400 irel_ptr = ifc_ent->irel_head;
13404 irel_ptr = irel_ptr->next;
13408 ifc_ent = ifc_ent->next;
13410 ex9_relax_size += size;
13414 /* Finish ex9 table. */
13417 nds32_elf_ex9_finish (struct bfd_link_info *link_info)
13419 struct elf_nds32_link_hash_table *table;
13421 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
13422 nds32_elf_order_insn_times (link_info);
13423 nds32_elf_ex9_total_relax (link_info);
13424 /* Traverse the hash table and count its times. */
13425 nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
13426 nds32_elf_order_insn_times (link_info);
13427 nds32_elf_ex9_build_itable (link_info);
13428 table = nds32_elf_hash_table (link_info);
13430 table->relax_round = NDS32_RELAX_EX9_REPLACE_ROUND;
13433 /* Relocate the entries in ex9 table. */
13436 nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
13437 struct bfd_link_info *link_info)
13439 Elf_Internal_Sym *isym = NULL;
13440 bfd_vma relocation = -1;
13442 if (ptr->m_list != NULL)
13444 /* Global symbol. */
13445 if ((ptr->m_list->h_list->h->root.type == bfd_link_hash_defined
13446 || ptr->m_list->h_list->h->root.type == bfd_link_hash_defweak)
13447 && (ptr->m_list->h_list->h->root.u.def.section != NULL
13448 && ptr->m_list->h_list->h->root.u.def.section->output_section != NULL))
13451 relocation = (ptr->m_list->h_list->h->root.u.def.value +
13452 ptr->m_list->h_list->h->root.u.def.section->output_section->vma +
13453 ptr->m_list->h_list->h->root.u.def.section->output_offset);
13454 relocation += ptr->m_list->irel->r_addend;
13459 else if (ptr->sec !=NULL)
13461 /* Local symbol. */
13462 Elf_Internal_Sym sym;
13463 asection *sec = NULL;
13465 asection *isec_ptr = &isec;
13466 Elf_Internal_Rela irel_backup = *(ptr->irel);
13467 asection *sec_backup = ptr->sec;
13468 bfd *abfd = ptr->sec->owner;
13470 if (!nds32_get_local_syms (abfd, sec, &isym))
13472 isym = isym + ELF32_R_SYM (ptr->irel->r_info);
13474 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13479 /* The purpose is same as elf_link_input_bfd. */
13480 if (isec_ptr != NULL
13481 && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
13482 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
13485 _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
13486 elf_section_data (isec_ptr)->sec_info,
13489 relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
13490 &ptr->sec, ptr->irel);
13491 if (ptr->irel != NULL)
13492 relocation += ptr->irel->r_addend;
13494 /* Restore origin value since there may be some insntructions that
13495 could not be replaced with ex9.it. */
13496 *(ptr->irel) = irel_backup;
13497 ptr->sec = sec_backup;
13503 /* Import ex9 table and build list. */
13506 nds32_elf_ex9_import_table (struct bfd_link_info *info)
13508 int count = 0, num = 1;
13509 bfd_byte *contents;
13510 unsigned long insn;
13511 FILE *ex9_import_file;
13512 int update_ex9_table;
13513 struct elf_nds32_link_hash_table *table;
13515 table = nds32_elf_hash_table (info);
13516 ex9_import_file = table->ex9_import_file;
13518 contents = bfd_malloc (sizeof (bfd_byte) * 4);
13520 /* Count the number of input file instructions. */
13521 while (!feof (ex9_import_file))
13523 fgetc (ex9_import_file);
13527 rewind (ex9_import_file);
13528 /* Read instructions from the input file and build the list. */
13532 struct elf_nds32_insn_times_entry *ptr;
13535 nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
13536 if (nread < sizeof (bfd_byte) * 4)
13538 (*_bfd_error_handler) ("Unexpected size of imported ex9 table.");
13541 insn = bfd_getb32 (contents);
13542 code = bfd_malloc (sizeof (char) * 9);
13543 snprintf (code, 9, "%08lx", insn);
13544 ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13545 ptr->string = code;
13549 ptr->m_list = NULL;
13550 ptr->rel_backup.r_offset = 0;
13551 ptr->rel_backup.r_info = 0;
13552 ptr->rel_backup.r_addend = 0;
13555 nds32_elf_ex9_insert_entry (ptr);
13560 update_ex9_table = table->update_ex9_table;
13561 if (update_ex9_table == 1)
13563 /* It has to consider of sethi need to use multiple page
13564 but it not be done yet. */
13565 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
13566 nds32_elf_order_insn_times (info);
13570 /* Export ex9 table. */
13573 nds32_elf_ex9_export (struct bfd_link_info *info,
13574 bfd_byte *contents, int size)
13576 FILE *ex9_export_file;
13577 struct elf_nds32_link_hash_table *table;
13579 table = nds32_elf_hash_table (info);
13580 ex9_export_file = table->ex9_export_file;
13581 fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
13582 fclose (ex9_export_file);
13585 /* Adjust relocations of J and JAL in ex9.itable.
13586 Export ex9 table. */
13589 nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
13591 asection *table_sec = NULL;
13592 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
13593 struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
13595 unsigned long insn, insn_with_reg, source_insn;
13596 bfd_byte *contents = NULL, *source_contents = NULL;
13599 int shift, update_ex9_table, offset = 0;
13600 reloc_howto_type *howto = NULL;
13601 Elf_Internal_Rela rel_backup;
13602 unsigned short insn_ex9;
13603 struct elf_nds32_link_hash_table *table;
13604 FILE *ex9_export_file, *ex9_import_file;
13606 table = nds32_elf_hash_table (link_info);
13608 table->relax_status |= NDS32_RELAX_EX9_DONE;
13611 update_ex9_table = table->update_ex9_table;
13612 /* Generated ex9.itable exactly. */
13613 if (update_ex9_table == 0)
13615 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
13616 it_abfd = it_abfd->link.next)
13618 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
13619 if (table_sec != NULL)
13623 if (table_sec != NULL)
13626 struct bfd_link_hash_entry *bh = NULL;
13628 output_bfd = table_sec->output_section->owner;
13629 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
13630 if (table_sec->size == 0)
13633 if (!nds32_get_section_contents (it_abfd, table_sec, &contents))
13635 /* Get the offset between _ITB_BASE_ and .ex9.itable. */
13636 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
13637 FALSE, FALSE, FALSE);
13638 offset = bh->u.def.value;
13646 output_bfd = link_info->input_bfds->sections->output_section->owner;
13647 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
13648 contents = bfd_malloc (sizeof (bfd_byte) * 2048);
13651 /* Relocate instruction. */
13654 bfd_vma relocation, min_relocation = 0xffffffff;
13656 insn = strtol (ex9_insn->string, NULL, 16);
13658 if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
13660 if (ex9_insn->m_list)
13661 rel_backup = ex9_insn->m_list->rel_backup;
13663 rel_backup = ex9_insn->rel_backup;
13665 nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
13667 bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
13668 (rel_backup.r_info));
13669 shift = howto->rightshift;
13670 if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
13671 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
13672 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
13673 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
13674 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
13676 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
13678 insn_with_reg | ((relocation >> shift) &
13679 nds32_elf_irel_mask (&rel_backup));
13680 bfd_putb32 (insn, contents + (ex9_insn->order) * 4 + offset);
13682 else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
13683 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
13684 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
13685 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
13686 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
13687 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
13688 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
13689 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
13691 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
13693 insn_with_reg | (((relocation - gp) >> shift) &
13694 nds32_elf_irel_mask (&rel_backup));
13695 bfd_putb32 (insn, contents + (ex9_insn->order) * 4 + offset);
13697 else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
13699 /* Sethi may be multiple entry for one insn. */
13700 if (ex9_insn->next && ((ex9_insn->m_list && ex9_insn->m_list == ex9_insn->next->m_list)
13701 || (ex9_insn->m_list && ex9_insn->next->order == 234
13702 && ex9_insn->next->next
13703 && ex9_insn->m_list == ex9_insn->next->next->m_list)))
13705 struct elf_link_hash_entry_mul_list *m_list;
13706 struct elf_nds32_ex9_refix *fix_ptr;
13708 temp_ptr = ex9_insn;
13709 temp_ptr2 = ex9_insn;
13710 m_list = ex9_insn->m_list;
13713 relocation = (m_list->h_list->h->root.u.def.value +
13714 m_list->h_list->h->root.u.def.section->output_section->vma +
13715 m_list->h_list->h->root.u.def.section->output_offset);
13716 relocation += m_list->irel->r_addend;
13718 if (relocation < min_relocation)
13719 min_relocation = relocation;
13720 m_list = m_list->next;
13722 relocation = min_relocation;
13724 /* Put insntruction into ex9 table. */
13725 insn = insn_with_reg
13726 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
13727 bfd_putb32 (insn, contents + (ex9_insn->order) * 4 + offset);
13728 relocation = relocation + 0x1000; /* hi20 */
13730 while (ex9_insn->next && ((ex9_insn->m_list && ex9_insn->m_list == ex9_insn->next->m_list)
13731 || (ex9_insn->m_list && ex9_insn->next->order == 234
13732 && ex9_insn->next->next
13733 && ex9_insn->m_list == ex9_insn->next->next->m_list)))
13735 /* Multiple sethi. */
13736 ex9_insn = ex9_insn->next;
13738 if (ex9_insn->order == 234)
13740 ex9_insn = ex9_insn->next;
13744 insn_with_reg | ((relocation >> shift) &
13745 nds32_elf_irel_mask (&rel_backup));
13746 bfd_putb32 (insn, contents + (ex9_insn->order) * 4 + offset);
13747 relocation = relocation + 0x1000; /* hi20 */
13750 fix_ptr = ex9_refix_head;
13753 /* Fix ex9 insn. */
13754 /* temp_ptr2 points to the head of multiple sethi. */
13755 temp_ptr = temp_ptr2;
13756 while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
13758 fix_ptr = fix_ptr->next;
13760 if (fix_ptr->order != temp_ptr->order)
13763 /* Set source insn. */
13764 relocation = (fix_ptr->h->root.u.def.value +
13765 fix_ptr->h->root.u.def.section->output_section->vma +
13766 fix_ptr->h->root.u.def.section->output_offset);
13767 relocation += fix_ptr->irel->r_addend;
13768 /* sethi imm is imm20s. */
13769 source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);
13773 if (temp_ptr->order == 234)
13775 temp_ptr = temp_ptr->next;
13779 /* Match entry and source code. */
13780 insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
13781 if (insn == source_insn)
13783 /* Fix the ex9 insn. */
13784 if (temp_ptr->order != fix_ptr->order)
13786 if (!nds32_get_section_contents
13787 (fix_ptr->sec->owner, fix_ptr->sec,
13789 (*_bfd_error_handler)
13790 (_("Linker: error cannot fixed ex9 relocation \n"));
13791 if (temp_ptr->order < 32)
13792 insn_ex9 = INSN_EX9_IT_2;
13794 insn_ex9 = INSN_EX9_IT_1;
13795 insn_ex9 = insn_ex9 | temp_ptr->order;
13796 bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
13802 if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
13803 (*_bfd_error_handler)
13804 (_("Linker: error cannot fixed ex9 relocation \n"));
13806 temp_ptr = temp_ptr->next;
13809 fix_ptr = fix_ptr->next;
13814 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
13815 insn = insn_with_reg
13816 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
13817 bfd_putb32 (insn, contents + (ex9_insn->order) * 4 + offset);
13823 /* Insn without relocation does not have to be fixed
13824 if need to update export table. */
13825 if (update_ex9_table == 1)
13826 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
13828 ex9_insn = ex9_insn->next;
13832 ex9_export_file = table->ex9_export_file;
13833 if (ex9_export_file != NULL)
13834 nds32_elf_ex9_export (link_info, contents + 4, table_sec->size - 4);
13835 else if (update_ex9_table == 1)
13837 ex9_import_file = table->ex9_import_file;
13838 ex9_export_file = ex9_import_file;
13839 rewind (ex9_export_file);
13840 nds32_elf_ex9_export (link_info, contents + 4, size);
13844 /* Generate ex9 hash table. */
13847 nds32_elf_ex9_build_hash_table (bfd * abfd, asection * sec,
13848 struct bfd_link_info *link_info)
13850 Elf_Internal_Rela *internal_relocs;
13851 Elf_Internal_Rela *irelend;
13852 Elf_Internal_Rela *irel;
13853 Elf_Internal_Rela *jrel;
13854 Elf_Internal_Rela rel_backup;
13855 Elf_Internal_Shdr *symtab_hdr;
13856 Elf_Internal_Sym *isym = NULL;
13858 struct elf_link_hash_entry **sym_hashes;
13859 bfd_byte *contents = NULL;
13860 long unsigned int off = 0;
13861 unsigned long r_symndx;
13862 unsigned long insn;
13863 unsigned long insn_with_reg;
13864 struct elf_link_hash_entry *h;
13865 int data_flag, shift, align;
13866 bfd_vma relocation;
13867 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
13868 reloc_howto_type *howto = NULL;
13870 sym_hashes = elf_sym_hashes (abfd);
13871 /* Load section instructions, relocations, and symbol table. */
13872 if (!nds32_get_section_contents (abfd, sec, &contents))
13875 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13876 TRUE /* keep_memory */);
13877 irelend = internal_relocs + sec->reloc_count;
13878 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13879 if (!nds32_get_local_syms (abfd, sec, &isym))
13882 /* Check the object if enable ex9. */
13883 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
13884 R_NDS32_RELAX_ENTRY);
13886 /* Check this section trigger ex9 relaxation. */
13889 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
13890 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
13891 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
13894 irel = internal_relocs;
13896 /* Push each insn into hash table. */
13897 while (off < sec->size)
13900 struct elf_nds32_code_hash_entry *entry;
13902 while (irel != NULL && irel < irelend && irel->r_offset < off)
13905 data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend, NULL,
13906 sec, &off, contents);
13907 if (data_flag & DATA_EXIST)
13909 /* We save the move offset in the highest byte. */
13910 off += (data_flag >> 24);
13914 if (*(contents + off) & 0x80)
13923 rel_backup.r_info = 0;
13924 rel_backup.r_offset = 0;
13925 rel_backup.r_addend = 0;
13926 /* Load the instruction and its opcode with register for comparing. */
13927 insn = bfd_getb32 (contents + off);
13929 if (irel != NULL && irel < irelend && irel->r_offset == off)
13931 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
13932 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
13933 shift = howto->rightshift;
13934 align = (1 << shift) - 1;
13935 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
13936 || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
13937 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
13938 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
13939 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
13940 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
13941 ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
13942 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
13943 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
13944 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
13945 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
13946 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
13947 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
13948 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
13950 r_symndx = ELF32_R_SYM (irel->r_info);
13952 rel_backup = *irel;
13953 if (r_symndx < symtab_hdr->sh_info)
13955 /* Local symbol. */
13956 int shndx = isym[r_symndx].st_shndx;
13958 bfd_vma st_value = (isym + r_symndx)->st_value;
13959 isec = elf_elfsections (abfd)[shndx]->bfd_section;
13960 relocation = (isec->output_section->vma + isec->output_offset
13961 + st_value + irel->r_addend);
13965 /* External symbol. */
13966 bfd_boolean warned ATTRIBUTE_UNUSED;
13967 bfd_boolean ignored ATTRIBUTE_UNUSED;
13968 bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
13971 /* Maybe there is a better way to get h and relocation */
13972 RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
13973 r_symndx, symtab_hdr, sym_hashes,
13974 h, sym_sec, relocation,
13975 unresolved_reloc, warned, ignored);
13976 relocation += irel->r_addend;
13977 if (h->type != bfd_link_hash_defined
13978 && h->type != bfd_link_hash_defweak)
13985 /* Check for gp relative instruction alignment. */
13986 if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
13987 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
13988 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
13989 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
13990 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
13991 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
13992 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
13993 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
13996 bfd *output_bfd = sec->output_section->owner;
13997 bfd_reloc_status_type r;
13999 /* If the symbol is in the abs section, the out_bfd will be null.
14000 This happens when the relocation has a symbol@GOTOFF. */
14001 r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
14002 if (r != bfd_reloc_ok)
14010 /* Make sure alignment is correct. */
14011 if (relocation & align)
14013 /* Incorrect alignment. */
14014 (*_bfd_error_handler)
14015 (_("%s: warning: unaligned small data access. "
14016 "For entry: {%d, %d, %d}, addr = 0x%x, align = 0x%x."),
14017 bfd_get_filename (abfd), irel->r_offset,
14018 irel->r_info, irel->r_addend, relocation, align);
14024 insn = insn_with_reg
14025 | ((relocation >> shift) & nds32_elf_irel_mask (irel));
14027 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
14028 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END)
14030 /* These relocations do not have to relocate contens, so it can
14031 be regard as instruction without relocation. */
14040 snprintf (code, sizeof (code), "%08lx", insn);
14042 entry = (struct elf_nds32_code_hash_entry*)
14043 bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
14046 (*_bfd_error_handler)
14047 (_("%P%F: failed creating ex9.it %s hash table: %E\n"), code);
14052 if (h->root.type == bfd_link_hash_undefined)
14054 /* Global symbol. */
14055 /* In order to do sethi with different symbol but same value. */
14056 if (entry->m_list == NULL)
14058 struct elf_link_hash_entry_mul_list *m_list_new;
14059 struct elf_link_hash_entry_list *h_list_new;
14061 m_list_new = (struct elf_link_hash_entry_mul_list *)
14062 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
14063 h_list_new = (struct elf_link_hash_entry_list *)
14064 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
14065 entry->m_list = m_list_new;
14066 m_list_new->h_list = h_list_new;
14067 m_list_new->rel_backup = rel_backup;
14068 m_list_new->times = 1;
14069 m_list_new->irel = jrel;
14070 m_list_new->next = NULL;
14072 h_list_new->next = NULL;
14076 struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
14077 struct elf_link_hash_entry_list *h_list;
14081 /* Build the different symbols that point to the same address. */
14082 h_list = m_list->h_list;
14083 if (h_list->h->root.u.def.value == h->root.u.def.value
14084 && h_list->h->root.u.def.section->output_section->vma
14085 == h->root.u.def.section->output_section->vma
14086 && h_list->h->root.u.def.section->output_offset
14087 == h->root.u.def.section->output_offset
14088 && m_list->rel_backup.r_addend == rel_backup.r_addend)
14091 m_list->irel = jrel;
14092 while (h_list->h != h && h_list->next)
14093 h_list = h_list->next;
14094 if (h_list->h != h)
14096 struct elf_link_hash_entry_list *h_list_new;
14098 h_list_new = (struct elf_link_hash_entry_list *)
14099 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
14100 h_list->next = h_list_new;
14102 h_list_new->next = NULL;
14106 /* The sethi case may have different address but the
14107 hi20 is the same. */
14108 else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
14109 && m_list->next == NULL)
14111 struct elf_link_hash_entry_mul_list *m_list_new;
14112 struct elf_link_hash_entry_list *h_list_new;
14114 m_list_new = (struct elf_link_hash_entry_mul_list *)
14115 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
14116 h_list_new = (struct elf_link_hash_entry_list *)
14117 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
14118 m_list->next = m_list_new;
14119 m_list_new->h_list = h_list_new;
14120 m_list_new->rel_backup = rel_backup;
14121 m_list_new->times = 1;
14122 m_list_new->irel = jrel;
14123 m_list_new->next = NULL;
14125 h_list_new->next = NULL;
14128 m_list = m_list->next;
14139 /* Local symbol and insn without relocation*/
14141 entry->rel_backup = rel_backup;
14144 /* Use in sethi insn with constant and global symbol in same format. */
14146 entry->const_insn = 1;
14148 entry->irel = jrel;
14156 /* Set the _ITB_BASE, and point it to ex9 table. */
14159 nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
14163 bfd *output_bfd = NULL;
14164 struct bfd_link_hash_entry *bh = NULL;
14165 int target_optimize;
14166 struct elf_nds32_link_hash_table *table;
14168 if (is_ITB_BASE_set == 1)
14171 is_ITB_BASE_set = 1;
14173 table = nds32_elf_hash_table (link_info);
14174 target_optimize = table->target_optimize;
14176 for (abfd = link_info->input_bfds; abfd != NULL;
14177 abfd = abfd->link.next)
14179 sec = bfd_get_section_by_name (abfd, ".ex9.itable");
14182 output_bfd = sec->output_section->owner;
14186 if (output_bfd == NULL)
14188 output_bfd = link_info->output_bfd;
14189 if (output_bfd->sections == NULL)
14192 sec = bfd_abs_section_ptr;
14194 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
14195 FALSE, FALSE, TRUE);
14196 return (_bfd_generic_link_add_one_symbol
14197 (link_info, output_bfd, "_ITB_BASE_",
14198 BSF_GLOBAL | BSF_WEAK, sec,
14199 /* We don't know its value yet, set it to 0. */
14200 (target_optimize & NDS32_RELAX_EX9_ON) ? 0 : (-234 * 4),
14201 (const char *) NULL, FALSE, get_elf_backend_data
14202 (output_bfd)->collect, &bh));
14206 #define ELF_ARCH bfd_arch_nds32
14207 #define ELF_MACHINE_CODE EM_NDS32
14208 #define ELF_MAXPAGESIZE 0x1000
14210 #define TARGET_BIG_SYM nds32_elf32_be_vec
14211 #define TARGET_BIG_NAME "elf32-nds32be"
14212 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
14213 #define TARGET_LITTLE_NAME "elf32-nds32le"
14215 #define elf_info_to_howto nds32_info_to_howto
14216 #define elf_info_to_howto_rel nds32_info_to_howto_rel
14218 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14219 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14220 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14221 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14222 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14224 #define elf_backend_action_discarded nds32_elf_action_discarded
14225 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14226 #define elf_backend_check_relocs nds32_elf_check_relocs
14227 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14228 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14229 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14230 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14231 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14232 #define elf_backend_relocate_section nds32_elf_relocate_section
14233 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
14234 #define elf_backend_gc_sweep_hook nds32_elf_gc_sweep_hook
14235 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14236 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14237 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14238 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14239 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14240 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14241 #define elf_backend_object_p nds32_elf_object_p
14242 #define elf_backend_final_write_processing nds32_elf_final_write_processing
14243 #define elf_backend_special_sections nds32_elf_special_sections
14245 #define elf_backend_can_gc_sections 1
14246 #define elf_backend_can_refcount 1
14247 #define elf_backend_want_got_plt 1
14248 #define elf_backend_plt_readonly 1
14249 #define elf_backend_want_plt_sym 0
14250 #define elf_backend_got_header_size 12
14251 #define elf_backend_may_use_rel_p 1
14252 #define elf_backend_default_use_rela_p 1
14253 #define elf_backend_may_use_rela_p 1
14255 #include "elf32-target.h"
14257 #undef ELF_MAXPAGESIZE
14258 #define ELF_MAXPAGESIZE 0x2000
14260 #undef TARGET_BIG_SYM
14261 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
14262 #undef TARGET_BIG_NAME
14263 #define TARGET_BIG_NAME "elf32-nds32be-linux"
14264 #undef TARGET_LITTLE_SYM
14265 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
14266 #undef TARGET_LITTLE_NAME
14267 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
14269 #define elf32_bed elf32_nds32_lin_bed
14271 #include "elf32-target.h"