1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2018 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);
60 /* Nds32 helper functions. */
61 static bfd_vma calculate_memory_address
62 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
63 static int nds32_get_section_contents (bfd *, asection *,
64 bfd_byte **, bfd_boolean);
65 static bfd_boolean nds32_elf_ex9_build_hash_table
66 (bfd *, asection *, struct bfd_link_info *);
67 static bfd_boolean nds32_elf_ex9_itb_base (struct bfd_link_info *);
68 static void nds32_elf_ex9_import_table (struct bfd_link_info *);
69 static void nds32_elf_ex9_finish (struct bfd_link_info *);
70 static void nds32_elf_ex9_reloc_jmp (struct bfd_link_info *);
71 static void nds32_elf_get_insn_with_reg
72 (Elf_Internal_Rela *, uint32_t, uint32_t *);
73 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
75 static bfd_boolean nds32_elf_ex9_replace_instruction
76 (struct bfd_link_info *, bfd *, asection *);
77 static bfd_boolean nds32_elf_ifc_calc (struct bfd_link_info *, bfd *,
79 static bfd_boolean nds32_elf_ifc_finish (struct bfd_link_info *);
80 static bfd_boolean nds32_elf_ifc_replace (struct bfd_link_info *);
81 static bfd_boolean nds32_elf_ifc_reloc (void);
82 static bfd_boolean nds32_relax_fp_as_gp
83 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
84 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
85 Elf_Internal_Sym *isymbuf);
86 static bfd_boolean nds32_fag_remove_unused_fpbase
87 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
88 Elf_Internal_Rela *irelend);
92 MACH_V1 = bfd_mach_n1h,
93 MACH_V2 = bfd_mach_n1h_v2,
94 MACH_V3 = bfd_mach_n1h_v3,
95 MACH_V3M = bfd_mach_n1h_v3m
98 #define MIN(a, b) ((a) > (b) ? (b) : (a))
99 #define MAX(a, b) ((a) > (b) ? (a) : (b))
101 /* The name of the dynamic interpreter. This is put in the .interp
103 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
105 /* The nop opcode we use. */
106 #define NDS32_NOP32 0x40000009
107 #define NDS32_NOP16 0x9200
109 /* The size in bytes of an entry in the procedure linkage table. */
110 #define PLT_ENTRY_SIZE 24
111 #define PLT_HEADER_SIZE 24
113 /* The first entry in a procedure linkage table are reserved,
114 and the initial contents are unimportant (we zero them out).
115 Subsequent entries look like this. */
116 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
117 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
118 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
119 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
120 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
122 /* $ta is change to $r15 (from $r25). */
123 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
124 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
125 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
126 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
127 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
128 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
130 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
131 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
132 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
133 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
134 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
136 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
137 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
138 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
139 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
140 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
141 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
143 /* These are macros used to get the relocation accurate value. */
144 #define ACCURATE_8BIT_S1 (0x100)
145 #define ACCURATE_U9BIT_S1 (0x400)
146 #define ACCURATE_12BIT_S1 (0x2000)
147 #define ACCURATE_14BIT_S1 (0x4000)
148 #define ACCURATE_19BIT (0x40000)
150 /* These are macros used to get the relocation conservative value. */
151 #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
152 #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
153 #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
154 #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
155 /* These must be more conservative because the address may be in
156 different segment. */
157 #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
158 #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
159 #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
160 #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
161 #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
163 /* Size of small data/bss sections, used to calculate SDA_BASE. */
164 static long got_size = 0;
165 static int is_SDA_BASE_set = 0;
166 static int is_ITB_BASE_set = 0;
168 /* Convert ELF-VER in eflags to string for debugging purpose. */
169 static const char *const nds32_elfver_strtab[] =
176 /* The nds32 linker needs to keep track of the number of relocs that it
177 decides to copy in check_relocs for each symbol. This is so that
178 it can discard PC relative relocs if it doesn't need them when
179 linking with -Bsymbolic. We store the information in a field
180 extending the regular ELF linker hash table. */
182 /* This structure keeps track of the number of PC relative relocs we
183 have copied for a given symbol. */
185 struct elf_nds32_pcrel_relocs_copied
188 struct elf_nds32_pcrel_relocs_copied *next;
189 /* A section in dynobj. */
191 /* Number of relocs copied in this section. */
195 /* Nds32 ELF linker hash entry. */
197 struct elf_nds32_link_hash_entry
199 struct elf_link_hash_entry root;
201 /* Track dynamic relocs copied for this symbol. */
202 struct elf_dyn_relocs *dyn_relocs;
204 /* For checking relocation type. */
205 #define GOT_UNKNOWN 0
208 unsigned int tls_type;
211 /* Get the nds32 ELF linker hash table from a link_info structure. */
213 #define FP_BASE_NAME "_FP_BASE_"
214 static int check_start_export_sym = 0;
215 static size_t ex9_relax_size = 0; /* Save ex9 predicted reducing size. */
217 /* The offset for executable tls relaxation. */
218 #define TP_OFFSET 0x0
220 struct elf_nds32_obj_tdata
222 struct elf_obj_tdata root;
224 /* tls_type for each local got entry. */
225 char *local_got_tls_type;
228 #define elf_nds32_tdata(bfd) \
229 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
231 #define elf32_nds32_local_got_tls_type(bfd) \
232 (elf_nds32_tdata (bfd)->local_got_tls_type)
234 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
237 nds32_elf_mkobject (bfd *abfd)
239 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
243 /* Relocations used for relocation. */
244 static reloc_howto_type nds32_elf_howto_table[] =
246 /* This reloc does nothing. */
247 HOWTO (R_NDS32_NONE, /* type */
249 3, /* size (0 = byte, 1 = short, 2 = long) */
251 FALSE, /* pc_relative */
253 complain_overflow_dont, /* complain_on_overflow */
254 bfd_elf_generic_reloc, /* special_function */
255 "R_NDS32_NONE", /* name */
256 FALSE, /* partial_inplace */
259 FALSE), /* pcrel_offset */
261 /* A 16 bit absolute relocation. */
262 HOWTO (R_NDS32_16, /* type */
264 1, /* size (0 = byte, 1 = short, 2 = long) */
266 FALSE, /* pc_relative */
268 complain_overflow_bitfield, /* complain_on_overflow */
269 nds32_elf_generic_reloc, /* special_function */
270 "R_NDS32_16", /* name */
271 FALSE, /* partial_inplace */
272 0xffff, /* src_mask */
273 0xffff, /* dst_mask */
274 FALSE), /* pcrel_offset */
276 /* A 32 bit absolute relocation. */
277 HOWTO (R_NDS32_32, /* type */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
281 FALSE, /* pc_relative */
283 complain_overflow_bitfield, /* complain_on_overflow */
284 nds32_elf_generic_reloc, /* special_function */
285 "R_NDS32_32", /* name */
286 FALSE, /* partial_inplace */
287 0xffffffff, /* src_mask */
288 0xffffffff, /* dst_mask */
289 FALSE), /* pcrel_offset */
291 /* A 20 bit address. */
292 HOWTO (R_NDS32_20, /* type */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
296 FALSE, /* pc_relative */
298 complain_overflow_unsigned, /* complain_on_overflow */
299 nds32_elf_generic_reloc, /* special_function */
300 "R_NDS32_20", /* name */
301 FALSE, /* partial_inplace */
302 0xfffff, /* src_mask */
303 0xfffff, /* dst_mask */
304 FALSE), /* pcrel_offset */
306 /* An PC Relative 9-bit relocation, shifted by 2.
307 This reloc is complicated because relocations are relative to pc & -4.
308 i.e. branches in the right insn slot use the address of the left insn
310 /* ??? It's not clear whether this should have partial_inplace set or not.
311 Branch relaxing in the assembler can store the addend in the insn,
312 and if bfd_install_relocation gets called the addend may get added
314 HOWTO (R_NDS32_9_PCREL, /* type */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
318 TRUE, /* pc_relative */
320 complain_overflow_signed, /* complain_on_overflow */
321 nds32_elf_9_pcrel_reloc, /* special_function */
322 "R_NDS32_9_PCREL", /* name */
323 FALSE, /* partial_inplace */
326 TRUE), /* pcrel_offset */
328 /* A relative 15 bit relocation, right shifted by 1. */
329 HOWTO (R_NDS32_15_PCREL, /* type */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
333 TRUE, /* pc_relative */
335 complain_overflow_signed, /* complain_on_overflow */
336 bfd_elf_generic_reloc, /* special_function */
337 "R_NDS32_15_PCREL", /* name */
338 FALSE, /* partial_inplace */
339 0x3fff, /* src_mask */
340 0x3fff, /* dst_mask */
341 TRUE), /* pcrel_offset */
343 /* A relative 17 bit relocation, right shifted by 1. */
344 HOWTO (R_NDS32_17_PCREL, /* type */
346 2, /* size (0 = byte, 1 = short, 2 = long) */
348 TRUE, /* pc_relative */
350 complain_overflow_signed, /* complain_on_overflow */
351 bfd_elf_generic_reloc, /* special_function */
352 "R_NDS32_17_PCREL", /* name */
353 FALSE, /* partial_inplace */
354 0xffff, /* src_mask */
355 0xffff, /* dst_mask */
356 TRUE), /* pcrel_offset */
358 /* A relative 25 bit relocation, right shifted by 1. */
359 /* ??? It's not clear whether this should have partial_inplace set or not.
360 Branch relaxing in the assembler can store the addend in the insn,
361 and if bfd_install_relocation gets called the addend may get added
363 HOWTO (R_NDS32_25_PCREL, /* type */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
367 TRUE, /* pc_relative */
369 complain_overflow_signed, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_NDS32_25_PCREL", /* name */
372 FALSE, /* partial_inplace */
373 0xffffff, /* src_mask */
374 0xffffff, /* dst_mask */
375 TRUE), /* pcrel_offset */
377 /* High 20 bits of address when lower 12 is or'd in. */
378 HOWTO (R_NDS32_HI20, /* type */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
382 FALSE, /* pc_relative */
384 complain_overflow_dont,/* complain_on_overflow */
385 nds32_elf_hi20_reloc, /* special_function */
386 "R_NDS32_HI20", /* name */
387 FALSE, /* partial_inplace */
388 0x000fffff, /* src_mask */
389 0x000fffff, /* dst_mask */
390 FALSE), /* pcrel_offset */
392 /* Lower 12 bits of address. */
393 HOWTO (R_NDS32_LO12S3, /* type */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
397 FALSE, /* pc_relative */
399 complain_overflow_dont,/* complain_on_overflow */
400 nds32_elf_lo12_reloc, /* special_function */
401 "R_NDS32_LO12S3", /* name */
402 FALSE, /* partial_inplace */
403 0x000001ff, /* src_mask */
404 0x000001ff, /* dst_mask */
405 FALSE), /* pcrel_offset */
407 /* Lower 12 bits of address. */
408 HOWTO (R_NDS32_LO12S2, /* type */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
412 FALSE, /* pc_relative */
414 complain_overflow_dont,/* complain_on_overflow */
415 nds32_elf_lo12_reloc, /* special_function */
416 "R_NDS32_LO12S2", /* name */
417 FALSE, /* partial_inplace */
418 0x000003ff, /* src_mask */
419 0x000003ff, /* dst_mask */
420 FALSE), /* pcrel_offset */
422 /* Lower 12 bits of address. */
423 HOWTO (R_NDS32_LO12S1, /* type */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
427 FALSE, /* pc_relative */
429 complain_overflow_dont,/* complain_on_overflow */
430 nds32_elf_lo12_reloc, /* special_function */
431 "R_NDS32_LO12S1", /* name */
432 FALSE, /* partial_inplace */
433 0x000007ff, /* src_mask */
434 0x000007ff, /* dst_mask */
435 FALSE), /* pcrel_offset */
437 /* Lower 12 bits of address. */
438 HOWTO (R_NDS32_LO12S0, /* type */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
442 FALSE, /* pc_relative */
444 complain_overflow_dont,/* complain_on_overflow */
445 nds32_elf_lo12_reloc, /* special_function */
446 "R_NDS32_LO12S0", /* name */
447 FALSE, /* partial_inplace */
448 0x00000fff, /* src_mask */
449 0x00000fff, /* dst_mask */
450 FALSE), /* pcrel_offset */
452 /* Small data area 15 bits offset. */
453 HOWTO (R_NDS32_SDA15S3, /* type */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
457 FALSE, /* pc_relative */
459 complain_overflow_signed, /* complain_on_overflow */
460 nds32_elf_sda15_reloc, /* special_function */
461 "R_NDS32_SDA15S3", /* name */
462 FALSE, /* partial_inplace */
463 0x00007fff, /* src_mask */
464 0x00007fff, /* dst_mask */
465 FALSE), /* pcrel_offset */
467 /* Small data area 15 bits offset. */
468 HOWTO (R_NDS32_SDA15S2, /* type */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
472 FALSE, /* pc_relative */
474 complain_overflow_signed, /* complain_on_overflow */
475 nds32_elf_sda15_reloc, /* special_function */
476 "R_NDS32_SDA15S2", /* name */
477 FALSE, /* partial_inplace */
478 0x00007fff, /* src_mask */
479 0x00007fff, /* dst_mask */
480 FALSE), /* pcrel_offset */
482 /* Small data area 15 bits offset. */
483 HOWTO (R_NDS32_SDA15S1, /* type */
485 2, /* size (0 = byte, 1 = short, 2 = long) */
487 FALSE, /* pc_relative */
489 complain_overflow_signed, /* complain_on_overflow */
490 nds32_elf_sda15_reloc, /* special_function */
491 "R_NDS32_SDA15S1", /* name */
492 FALSE, /* partial_inplace */
493 0x00007fff, /* src_mask */
494 0x00007fff, /* dst_mask */
495 FALSE), /* pcrel_offset */
497 /* Small data area 15 bits offset. */
498 HOWTO (R_NDS32_SDA15S0, /* type */
500 2, /* size (0 = byte, 1 = short, 2 = long) */
502 FALSE, /* pc_relative */
504 complain_overflow_signed, /* complain_on_overflow */
505 nds32_elf_sda15_reloc, /* special_function */
506 "R_NDS32_SDA15S0", /* name */
507 FALSE, /* partial_inplace */
508 0x00007fff, /* src_mask */
509 0x00007fff, /* dst_mask */
510 FALSE), /* pcrel_offset */
512 /* GNU extension to record C++ vtable hierarchy */
513 HOWTO (R_NDS32_GNU_VTINHERIT, /* type */
515 2, /* size (0 = byte, 1 = short, 2 = long) */
517 FALSE, /* pc_relative */
519 complain_overflow_dont,/* complain_on_overflow */
520 NULL, /* special_function */
521 "R_NDS32_GNU_VTINHERIT", /* name */
522 FALSE, /* partial_inplace */
525 FALSE), /* pcrel_offset */
527 /* GNU extension to record C++ vtable member usage */
528 HOWTO (R_NDS32_GNU_VTENTRY, /* type */
530 2, /* size (0 = byte, 1 = short, 2 = long) */
532 FALSE, /* pc_relative */
534 complain_overflow_dont,/* complain_on_overflow */
535 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
536 "R_NDS32_GNU_VTENTRY", /* name */
537 FALSE, /* partial_inplace */
540 FALSE), /* pcrel_offset */
542 /* A 16 bit absolute relocation. */
543 HOWTO (R_NDS32_16_RELA, /* type */
545 1, /* size (0 = byte, 1 = short, 2 = long) */
547 FALSE, /* pc_relative */
549 complain_overflow_bitfield, /* complain_on_overflow */
550 bfd_elf_generic_reloc, /* special_function */
551 "R_NDS32_16_RELA", /* name */
552 FALSE, /* partial_inplace */
553 0xffff, /* src_mask */
554 0xffff, /* dst_mask */
555 FALSE), /* pcrel_offset */
557 /* A 32 bit absolute relocation. */
558 HOWTO (R_NDS32_32_RELA, /* type */
560 2, /* size (0 = byte, 1 = short, 2 = long) */
562 FALSE, /* pc_relative */
564 complain_overflow_bitfield, /* complain_on_overflow */
565 bfd_elf_generic_reloc, /* special_function */
566 "R_NDS32_32_RELA", /* name */
567 FALSE, /* partial_inplace */
568 0xffffffff, /* src_mask */
569 0xffffffff, /* dst_mask */
570 FALSE), /* pcrel_offset */
572 /* A 20 bit address. */
573 HOWTO (R_NDS32_20_RELA, /* type */
575 2, /* size (0 = byte, 1 = short, 2 = long) */
577 FALSE, /* pc_relative */
579 complain_overflow_signed, /* complain_on_overflow */
580 bfd_elf_generic_reloc, /* special_function */
581 "R_NDS32_20_RELA", /* name */
582 FALSE, /* partial_inplace */
583 0xfffff, /* src_mask */
584 0xfffff, /* dst_mask */
585 FALSE), /* pcrel_offset */
587 HOWTO (R_NDS32_9_PCREL_RELA, /* type */
589 1, /* size (0 = byte, 1 = short, 2 = long) */
591 TRUE, /* pc_relative */
593 complain_overflow_signed, /* complain_on_overflow */
594 bfd_elf_generic_reloc, /* special_function */
595 "R_NDS32_9_PCREL_RELA",/* name */
596 FALSE, /* partial_inplace */
599 TRUE), /* pcrel_offset */
601 /* A relative 15 bit relocation, right shifted by 1. */
602 HOWTO (R_NDS32_15_PCREL_RELA, /* type */
604 2, /* size (0 = byte, 1 = short, 2 = long) */
606 TRUE, /* pc_relative */
608 complain_overflow_signed, /* complain_on_overflow */
609 bfd_elf_generic_reloc, /* special_function */
610 "R_NDS32_15_PCREL_RELA", /* name */
611 FALSE, /* partial_inplace */
612 0x3fff, /* src_mask */
613 0x3fff, /* dst_mask */
614 TRUE), /* pcrel_offset */
616 /* A relative 17 bit relocation, right shifted by 1. */
617 HOWTO (R_NDS32_17_PCREL_RELA, /* type */
619 2, /* size (0 = byte, 1 = short, 2 = long) */
621 TRUE, /* pc_relative */
623 complain_overflow_signed, /* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_NDS32_17_PCREL_RELA", /* name */
626 FALSE, /* partial_inplace */
627 0xffff, /* src_mask */
628 0xffff, /* dst_mask */
629 TRUE), /* pcrel_offset */
631 /* A relative 25 bit relocation, right shifted by 2. */
632 HOWTO (R_NDS32_25_PCREL_RELA, /* type */
634 2, /* size (0 = byte, 1 = short, 2 = long) */
636 TRUE, /* pc_relative */
638 complain_overflow_signed, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_NDS32_25_PCREL_RELA", /* name */
641 FALSE, /* partial_inplace */
642 0xffffff, /* src_mask */
643 0xffffff, /* dst_mask */
644 TRUE), /* pcrel_offset */
646 /* High 20 bits of address when lower 16 is or'd in. */
647 HOWTO (R_NDS32_HI20_RELA, /* type */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
651 FALSE, /* pc_relative */
653 complain_overflow_dont,/* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_NDS32_HI20_RELA", /* name */
656 FALSE, /* partial_inplace */
657 0x000fffff, /* src_mask */
658 0x000fffff, /* dst_mask */
659 FALSE), /* pcrel_offset */
661 /* Lower 12 bits of address. */
662 HOWTO (R_NDS32_LO12S3_RELA, /* type */
664 2, /* size (0 = byte, 1 = short, 2 = long) */
666 FALSE, /* pc_relative */
668 complain_overflow_dont,/* complain_on_overflow */
669 bfd_elf_generic_reloc, /* special_function */
670 "R_NDS32_LO12S3_RELA", /* name */
671 FALSE, /* partial_inplace */
672 0x000001ff, /* src_mask */
673 0x000001ff, /* dst_mask */
674 FALSE), /* pcrel_offset */
676 /* Lower 12 bits of address. */
677 HOWTO (R_NDS32_LO12S2_RELA, /* type */
679 2, /* size (0 = byte, 1 = short, 2 = long) */
681 FALSE, /* pc_relative */
683 complain_overflow_dont,/* complain_on_overflow */
684 bfd_elf_generic_reloc, /* special_function */
685 "R_NDS32_LO12S2_RELA", /* name */
686 FALSE, /* partial_inplace */
687 0x000003ff, /* src_mask */
688 0x000003ff, /* dst_mask */
689 FALSE), /* pcrel_offset */
691 /* Lower 12 bits of address. */
692 HOWTO (R_NDS32_LO12S1_RELA, /* type */
694 2, /* size (0 = byte, 1 = short, 2 = long) */
696 FALSE, /* pc_relative */
698 complain_overflow_dont,/* complain_on_overflow */
699 bfd_elf_generic_reloc, /* special_function */
700 "R_NDS32_LO12S1_RELA", /* name */
701 FALSE, /* partial_inplace */
702 0x000007ff, /* src_mask */
703 0x000007ff, /* dst_mask */
704 FALSE), /* pcrel_offset */
706 /* Lower 12 bits of address. */
707 HOWTO (R_NDS32_LO12S0_RELA, /* type */
709 2, /* size (0 = byte, 1 = short, 2 = long) */
711 FALSE, /* pc_relative */
713 complain_overflow_dont,/* complain_on_overflow */
714 bfd_elf_generic_reloc, /* special_function */
715 "R_NDS32_LO12S0_RELA", /* name */
716 FALSE, /* partial_inplace */
717 0x00000fff, /* src_mask */
718 0x00000fff, /* dst_mask */
719 FALSE), /* pcrel_offset */
721 /* Small data area 15 bits offset. */
722 HOWTO (R_NDS32_SDA15S3_RELA, /* type */
724 2, /* size (0 = byte, 1 = short, 2 = long) */
726 FALSE, /* pc_relative */
728 complain_overflow_signed, /* complain_on_overflow */
729 bfd_elf_generic_reloc, /* special_function */
730 "R_NDS32_SDA15S3_RELA",/* name */
731 FALSE, /* partial_inplace */
732 0x00007fff, /* src_mask */
733 0x00007fff, /* dst_mask */
734 FALSE), /* pcrel_offset */
736 /* Small data area 15 bits offset. */
737 HOWTO (R_NDS32_SDA15S2_RELA, /* type */
739 2, /* size (0 = byte, 1 = short, 2 = long) */
741 FALSE, /* pc_relative */
743 complain_overflow_signed, /* complain_on_overflow */
744 bfd_elf_generic_reloc, /* special_function */
745 "R_NDS32_SDA15S2_RELA",/* name */
746 FALSE, /* partial_inplace */
747 0x00007fff, /* src_mask */
748 0x00007fff, /* dst_mask */
749 FALSE), /* pcrel_offset */
751 HOWTO (R_NDS32_SDA15S1_RELA, /* type */
753 2, /* size (0 = byte, 1 = short, 2 = long) */
755 FALSE, /* pc_relative */
757 complain_overflow_signed, /* complain_on_overflow */
758 bfd_elf_generic_reloc, /* special_function */
759 "R_NDS32_SDA15S1_RELA",/* name */
760 FALSE, /* partial_inplace */
761 0x00007fff, /* src_mask */
762 0x00007fff, /* dst_mask */
763 FALSE), /* pcrel_offset */
765 HOWTO (R_NDS32_SDA15S0_RELA, /* type */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
769 FALSE, /* pc_relative */
771 complain_overflow_signed, /* complain_on_overflow */
772 bfd_elf_generic_reloc, /* special_function */
773 "R_NDS32_SDA15S0_RELA",/* name */
774 FALSE, /* partial_inplace */
775 0x00007fff, /* src_mask */
776 0x00007fff, /* dst_mask */
777 FALSE), /* pcrel_offset */
779 /* GNU extension to record C++ vtable hierarchy */
780 HOWTO (R_NDS32_RELA_GNU_VTINHERIT, /* type */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
784 FALSE, /* pc_relative */
786 complain_overflow_dont,/* complain_on_overflow */
787 NULL, /* special_function */
788 "R_NDS32_RELA_GNU_VTINHERIT", /* name */
789 FALSE, /* partial_inplace */
792 FALSE), /* pcrel_offset */
794 /* GNU extension to record C++ vtable member usage */
795 HOWTO (R_NDS32_RELA_GNU_VTENTRY, /* type */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
799 FALSE, /* pc_relative */
801 complain_overflow_dont,/* complain_on_overflow */
802 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
803 "R_NDS32_RELA_GNU_VTENTRY", /* name */
804 FALSE, /* partial_inplace */
807 FALSE), /* pcrel_offset */
809 /* Like R_NDS32_20, but referring to the GOT table entry for
811 HOWTO (R_NDS32_GOT20, /* type */
813 2, /* size (0 = byte, 1 = short, 2 = long) */
815 FALSE, /* pc_relative */
817 complain_overflow_signed, /* complain_on_overflow */
818 bfd_elf_generic_reloc, /* special_function */
819 "R_NDS32_GOT20", /* name */
820 FALSE, /* partial_inplace */
821 0xfffff, /* src_mask */
822 0xfffff, /* dst_mask */
823 FALSE), /* pcrel_offset */
825 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
826 entry for the symbol. */
827 HOWTO (R_NDS32_25_PLTREL, /* type */
829 2, /* size (0 = byte, 1 = short, 2 = long) */
831 TRUE, /* pc_relative */
833 complain_overflow_signed, /* complain_on_overflow */
834 bfd_elf_generic_reloc, /* special_function */
835 "R_NDS32_25_PLTREL", /* name */
836 FALSE, /* partial_inplace */
837 0xffffff, /* src_mask */
838 0xffffff, /* dst_mask */
839 TRUE), /* pcrel_offset */
841 /* This is used only by the dynamic linker. The symbol should exist
842 both in the object being run and in some shared library. The
843 dynamic linker copies the data addressed by the symbol from the
844 shared library into the object, because the object being
845 run has to have the data at some particular address. */
846 HOWTO (R_NDS32_COPY, /* type */
848 2, /* size (0 = byte, 1 = short, 2 = long) */
850 FALSE, /* pc_relative */
852 complain_overflow_bitfield, /* complain_on_overflow */
853 bfd_elf_generic_reloc, /* special_function */
854 "R_NDS32_COPY", /* name */
855 FALSE, /* partial_inplace */
856 0xffffffff, /* src_mask */
857 0xffffffff, /* dst_mask */
858 FALSE), /* pcrel_offset */
860 /* Like R_NDS32_20, but used when setting global offset table
862 HOWTO (R_NDS32_GLOB_DAT, /* type */
864 2, /* size (0 = byte, 1 = short, 2 = long) */
866 FALSE, /* pc_relative */
868 complain_overflow_bitfield, /* complain_on_overflow */
869 bfd_elf_generic_reloc, /* special_function */
870 "R_NDS32_GLOB_DAT", /* name */
871 FALSE, /* partial_inplace */
872 0xffffffff, /* src_mask */
873 0xffffffff, /* dst_mask */
874 FALSE), /* pcrel_offset */
876 /* Marks a procedure linkage table entry for a symbol. */
877 HOWTO (R_NDS32_JMP_SLOT, /* type */
879 2, /* size (0 = byte, 1 = short, 2 = long) */
881 FALSE, /* pc_relative */
883 complain_overflow_bitfield, /* complain_on_overflow */
884 bfd_elf_generic_reloc, /* special_function */
885 "R_NDS32_JMP_SLOT", /* name */
886 FALSE, /* partial_inplace */
887 0xffffffff, /* src_mask */
888 0xffffffff, /* dst_mask */
889 FALSE), /* pcrel_offset */
891 /* Used only by the dynamic linker. When the object is run, this
892 longword is set to the load address of the object, plus the
894 HOWTO (R_NDS32_RELATIVE, /* type */
896 2, /* size (0 = byte, 1 = short, 2 = long) */
898 FALSE, /* pc_relative */
900 complain_overflow_bitfield, /* complain_on_overflow */
901 bfd_elf_generic_reloc, /* special_function */
902 "R_NDS32_RELATIVE", /* name */
903 FALSE, /* partial_inplace */
904 0xffffffff, /* src_mask */
905 0xffffffff, /* dst_mask */
906 FALSE), /* pcrel_offset */
908 HOWTO (R_NDS32_GOTOFF, /* type */
910 2, /* size (0 = byte, 1 = short, 2 = long) */
912 FALSE, /* pc_relative */
914 complain_overflow_signed, /* complain_on_overflow */
915 bfd_elf_generic_reloc, /* special_function */
916 "R_NDS32_GOTOFF", /* name */
917 FALSE, /* partial_inplace */
918 0xfffff, /* src_mask */
919 0xfffff, /* dst_mask */
920 FALSE), /* pcrel_offset */
922 /* An PC Relative 20-bit relocation used when setting PIC offset
924 HOWTO (R_NDS32_GOTPC20, /* type */
926 2, /* size (0 = byte, 1 = short, 2 = long) */
928 TRUE, /* pc_relative */
930 complain_overflow_signed, /* complain_on_overflow */
931 bfd_elf_generic_reloc, /* special_function */
932 "R_NDS32_GOTPC20", /* name */
933 FALSE, /* partial_inplace */
934 0xfffff, /* src_mask */
935 0xfffff, /* dst_mask */
936 TRUE), /* pcrel_offset */
938 /* Like R_NDS32_HI20, but referring to the GOT table entry for
940 HOWTO (R_NDS32_GOT_HI20, /* type */
942 2, /* size (0 = byte, 1 = short, 2 = long) */
944 FALSE, /* pc_relative */
946 complain_overflow_dont,/* complain_on_overflow */
947 bfd_elf_generic_reloc, /* special_function */
948 "R_NDS32_GOT_HI20", /* name */
949 FALSE, /* partial_inplace */
950 0x000fffff, /* src_mask */
951 0x000fffff, /* dst_mask */
952 FALSE), /* pcrel_offset */
953 HOWTO (R_NDS32_GOT_LO12, /* type */
955 2, /* size (0 = byte, 1 = short, 2 = long) */
957 FALSE, /* pc_relative */
959 complain_overflow_dont,/* complain_on_overflow */
960 bfd_elf_generic_reloc, /* special_function */
961 "R_NDS32_GOT_LO12", /* name */
962 FALSE, /* partial_inplace */
963 0x00000fff, /* src_mask */
964 0x00000fff, /* dst_mask */
965 FALSE), /* pcrel_offset */
967 /* An PC Relative relocation used when setting PIC offset table register.
968 Like R_NDS32_HI20, but referring to the GOT table entry for
970 HOWTO (R_NDS32_GOTPC_HI20, /* type */
972 2, /* size (0 = byte, 1 = short, 2 = long) */
974 FALSE, /* pc_relative */
976 complain_overflow_dont,/* complain_on_overflow */
977 bfd_elf_generic_reloc, /* special_function */
978 "R_NDS32_GOTPC_HI20", /* name */
979 FALSE, /* partial_inplace */
980 0x000fffff, /* src_mask */
981 0x000fffff, /* dst_mask */
982 TRUE), /* pcrel_offset */
983 HOWTO (R_NDS32_GOTPC_LO12, /* type */
985 2, /* size (0 = byte, 1 = short, 2 = long) */
987 FALSE, /* pc_relative */
989 complain_overflow_dont, /* complain_on_overflow */
990 bfd_elf_generic_reloc, /* special_function */
991 "R_NDS32_GOTPC_LO12", /* name */
992 FALSE, /* partial_inplace */
993 0x00000fff, /* src_mask */
994 0x00000fff, /* dst_mask */
995 TRUE), /* pcrel_offset */
997 HOWTO (R_NDS32_GOTOFF_HI20, /* type */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1001 FALSE, /* pc_relative */
1003 complain_overflow_dont,/* complain_on_overflow */
1004 bfd_elf_generic_reloc, /* special_function */
1005 "R_NDS32_GOTOFF_HI20", /* name */
1006 FALSE, /* partial_inplace */
1007 0x000fffff, /* src_mask */
1008 0x000fffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1010 HOWTO (R_NDS32_GOTOFF_LO12, /* type */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1014 FALSE, /* pc_relative */
1016 complain_overflow_dont,/* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_NDS32_GOTOFF_LO12", /* name */
1019 FALSE, /* partial_inplace */
1020 0x00000fff, /* src_mask */
1021 0x00000fff, /* dst_mask */
1022 FALSE), /* pcrel_offset */
1024 /* Alignment hint for relaxable instruction. This is used with
1025 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1026 in order to make next label aligned on word boundary. */
1027 HOWTO (R_NDS32_INSN16, /* type */
1029 2, /* size (0 = byte, 1 = short, 2 = long) */
1031 FALSE, /* pc_relative */
1033 complain_overflow_dont,/* complain_on_overflow */
1034 nds32_elf_ignore_reloc,/* special_function */
1035 "R_NDS32_INSN16", /* name */
1036 FALSE, /* partial_inplace */
1037 0x00000fff, /* src_mask */
1038 0x00000fff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1041 /* Alignment hint for label. */
1042 HOWTO (R_NDS32_LABEL, /* type */
1044 2, /* size (0 = byte, 1 = short, 2 = long) */
1046 FALSE, /* pc_relative */
1048 complain_overflow_dont,/* complain_on_overflow */
1049 nds32_elf_ignore_reloc,/* special_function */
1050 "R_NDS32_LABEL", /* name */
1051 FALSE, /* partial_inplace */
1052 0xffffffff, /* src_mask */
1053 0xffffffff, /* dst_mask */
1054 FALSE), /* pcrel_offset */
1056 /* Relax hint for unconditional call sequence */
1057 HOWTO (R_NDS32_LONGCALL1, /* type */
1059 2, /* size (0 = byte, 1 = short, 2 = long) */
1061 FALSE, /* pc_relative */
1063 complain_overflow_dont,/* complain_on_overflow */
1064 nds32_elf_ignore_reloc,/* special_function */
1065 "R_NDS32_LONGCALL1", /* name */
1066 FALSE, /* partial_inplace */
1067 0xffffffff, /* src_mask */
1068 0xffffffff, /* dst_mask */
1069 FALSE), /* pcrel_offset */
1071 /* Relax hint for conditional call sequence. */
1072 HOWTO (R_NDS32_LONGCALL2, /* type */
1074 2, /* size (0 = byte, 1 = short, 2 = long) */
1076 FALSE, /* pc_relative */
1078 complain_overflow_dont,/* complain_on_overflow */
1079 nds32_elf_ignore_reloc,/* special_function */
1080 "R_NDS32_LONGCALL2", /* name */
1081 FALSE, /* partial_inplace */
1082 0xffffffff, /* src_mask */
1083 0xffffffff, /* dst_mask */
1084 FALSE), /* pcrel_offset */
1086 /* Relax hint for conditional call sequence. */
1087 HOWTO (R_NDS32_LONGCALL3, /* type */
1089 2, /* size (0 = byte, 1 = short, 2 = long) */
1091 FALSE, /* pc_relative */
1093 complain_overflow_dont,/* complain_on_overflow */
1094 nds32_elf_ignore_reloc,/* special_function */
1095 "R_NDS32_LONGCALL3", /* name */
1096 FALSE, /* partial_inplace */
1097 0xffffffff, /* src_mask */
1098 0xffffffff, /* dst_mask */
1099 FALSE), /* pcrel_offset */
1101 /* Relax hint for unconditional branch sequence. */
1102 HOWTO (R_NDS32_LONGJUMP1, /* type */
1104 2, /* size (0 = byte, 1 = short, 2 = long) */
1106 FALSE, /* pc_relative */
1108 complain_overflow_dont,/* complain_on_overflow */
1109 nds32_elf_ignore_reloc,/* special_function */
1110 "R_NDS32_LONGJUMP1", /* name */
1111 FALSE, /* partial_inplace */
1112 0xffffffff, /* src_mask */
1113 0xffffffff, /* dst_mask */
1114 FALSE), /* pcrel_offset */
1116 /* Relax hint for conditional branch sequence. */
1117 HOWTO (R_NDS32_LONGJUMP2, /* type */
1119 2, /* size (0 = byte, 1 = short, 2 = long) */
1121 FALSE, /* pc_relative */
1123 complain_overflow_dont,/* complain_on_overflow */
1124 nds32_elf_ignore_reloc,/* special_function */
1125 "R_NDS32_LONGJUMP2", /* name */
1126 FALSE, /* partial_inplace */
1127 0xffffffff, /* src_mask */
1128 0xffffffff, /* dst_mask */
1129 FALSE), /* pcrel_offset */
1131 /* Relax hint for conditional branch sequence. */
1132 HOWTO (R_NDS32_LONGJUMP3, /* type */
1134 2, /* size (0 = byte, 1 = short, 2 = long) */
1136 FALSE, /* pc_relative */
1138 complain_overflow_dont,/* complain_on_overflow */
1139 nds32_elf_ignore_reloc,/* special_function */
1140 "R_NDS32_LONGJUMP3", /* name */
1141 FALSE, /* partial_inplace */
1142 0xffffffff, /* src_mask */
1143 0xffffffff, /* dst_mask */
1144 FALSE), /* pcrel_offset */
1146 /* Relax hint for load/store sequence. */
1147 HOWTO (R_NDS32_LOADSTORE, /* type */
1149 2, /* size (0 = byte, 1 = short, 2 = long) */
1151 FALSE, /* pc_relative */
1153 complain_overflow_dont,/* complain_on_overflow */
1154 nds32_elf_ignore_reloc,/* special_function */
1155 "R_NDS32_LOADSTORE", /* name */
1156 FALSE, /* partial_inplace */
1157 0xffffffff, /* src_mask */
1158 0xffffffff, /* dst_mask */
1159 FALSE), /* pcrel_offset */
1161 /* Relax hint for load/store sequence. */
1162 HOWTO (R_NDS32_9_FIXED_RELA, /* type */
1164 1, /* size (0 = byte, 1 = short, 2 = long) */
1166 FALSE, /* pc_relative */
1168 complain_overflow_dont,/* complain_on_overflow */
1169 nds32_elf_ignore_reloc,/* special_function */
1170 "R_NDS32_9_FIXED_RELA",/* name */
1171 FALSE, /* partial_inplace */
1172 0x000000ff, /* src_mask */
1173 0x000000ff, /* dst_mask */
1174 FALSE), /* pcrel_offset */
1176 /* Relax hint for load/store sequence. */
1177 HOWTO (R_NDS32_15_FIXED_RELA, /* type */
1179 2, /* size (0 = byte, 1 = short, 2 = long) */
1181 FALSE, /* pc_relative */
1183 complain_overflow_dont,/* complain_on_overflow */
1184 nds32_elf_ignore_reloc,/* special_function */
1185 "R_NDS32_15_FIXED_RELA", /* name */
1186 FALSE, /* partial_inplace */
1187 0x00003fff, /* src_mask */
1188 0x00003fff, /* dst_mask */
1189 FALSE), /* pcrel_offset */
1191 /* Relax hint for load/store sequence. */
1192 HOWTO (R_NDS32_17_FIXED_RELA, /* type */
1194 2, /* size (0 = byte, 1 = short, 2 = long) */
1196 FALSE, /* pc_relative */
1198 complain_overflow_dont,/* complain_on_overflow */
1199 nds32_elf_ignore_reloc,/* special_function */
1200 "R_NDS32_17_FIXED_RELA", /* name */
1201 FALSE, /* partial_inplace */
1202 0x0000ffff, /* src_mask */
1203 0x0000ffff, /* dst_mask */
1204 FALSE), /* pcrel_offset */
1206 /* Relax hint for load/store sequence. */
1207 HOWTO (R_NDS32_25_FIXED_RELA, /* type */
1209 2, /* size (0 = byte, 1 = short, 2 = long) */
1211 FALSE, /* pc_relative */
1213 complain_overflow_dont,/* complain_on_overflow */
1214 nds32_elf_ignore_reloc,/* special_function */
1215 "R_NDS32_25_FIXED_RELA", /* name */
1216 FALSE, /* partial_inplace */
1217 0x00ffffff, /* src_mask */
1218 0x00ffffff, /* dst_mask */
1219 FALSE), /* pcrel_offset */
1221 /* High 20 bits of PLT symbol offset relative to PC. */
1222 HOWTO (R_NDS32_PLTREL_HI20, /* type */
1223 12, /* rightshift */
1224 2, /* size (0 = byte, 1 = short, 2 = long) */
1226 FALSE, /* pc_relative */
1228 complain_overflow_dont,/* complain_on_overflow */
1229 bfd_elf_generic_reloc, /* special_function */
1230 "R_NDS32_PLTREL_HI20", /* name */
1231 FALSE, /* partial_inplace */
1232 0x000fffff, /* src_mask */
1233 0x000fffff, /* dst_mask */
1234 FALSE), /* pcrel_offset */
1236 /* Low 12 bits of PLT symbol offset relative to PC. */
1237 HOWTO (R_NDS32_PLTREL_LO12, /* type */
1239 2, /* size (0 = byte, 1 = short, 2 = long) */
1241 FALSE, /* pc_relative */
1243 complain_overflow_dont,/* complain_on_overflow */
1244 bfd_elf_generic_reloc, /* special_function */
1245 "R_NDS32_PLTREL_LO12", /* name */
1246 FALSE, /* partial_inplace */
1247 0x00000fff, /* src_mask */
1248 0x00000fff, /* dst_mask */
1249 FALSE), /* pcrel_offset */
1251 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1252 HOWTO (R_NDS32_PLT_GOTREL_HI20, /* type */
1253 12, /* rightshift */
1254 2, /* size (0 = byte, 1 = short, 2 = long) */
1256 FALSE, /* pc_relative */
1258 complain_overflow_dont,/* complain_on_overflow */
1259 bfd_elf_generic_reloc, /* special_function */
1260 "R_NDS32_PLT_GOTREL_HI20", /* name */
1261 FALSE, /* partial_inplace */
1262 0x000fffff, /* src_mask */
1263 0x000fffff, /* dst_mask */
1264 FALSE), /* pcrel_offset */
1266 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1267 HOWTO (R_NDS32_PLT_GOTREL_LO12, /* type */
1269 2, /* size (0 = byte, 1 = short, 2 = long) */
1271 FALSE, /* pc_relative */
1273 complain_overflow_dont,/* complain_on_overflow */
1274 bfd_elf_generic_reloc, /* special_function */
1275 "R_NDS32_PLT_GOTREL_LO12", /* name */
1276 FALSE, /* partial_inplace */
1277 0x00000fff, /* src_mask */
1278 0x00000fff, /* dst_mask */
1279 FALSE), /* pcrel_offset */
1281 /* Small data area 12 bits offset. */
1282 HOWTO (R_NDS32_SDA12S2_DP_RELA, /* type */
1284 2, /* size (0 = byte, 1 = short, 2 = long) */
1286 FALSE, /* pc_relative */
1288 complain_overflow_signed, /* complain_on_overflow */
1289 bfd_elf_generic_reloc, /* special_function */
1290 "R_NDS32_SDA12S2_DP_RELA", /* name */
1291 FALSE, /* partial_inplace */
1292 0x00000fff, /* src_mask */
1293 0x00000fff, /* dst_mask */
1294 FALSE), /* pcrel_offset */
1296 /* Small data area 12 bits offset. */
1297 HOWTO (R_NDS32_SDA12S2_SP_RELA, /* type */
1299 2, /* size (0 = byte, 1 = short, 2 = long) */
1301 FALSE, /* pc_relative */
1303 complain_overflow_signed, /* complain_on_overflow */
1304 bfd_elf_generic_reloc, /* special_function */
1305 "R_NDS32_SDA12S2_SP_RELA", /* name */
1306 FALSE, /* partial_inplace */
1307 0x00000fff, /* src_mask */
1308 0x00000fff, /* dst_mask */
1309 FALSE), /* pcrel_offset */
1310 /* Lower 12 bits of address. */
1312 HOWTO (R_NDS32_LO12S2_DP_RELA, /* type */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1316 FALSE, /* pc_relative */
1318 complain_overflow_dont,/* complain_on_overflow */
1319 bfd_elf_generic_reloc, /* special_function */
1320 "R_NDS32_LO12S2_DP_RELA", /* name */
1321 FALSE, /* partial_inplace */
1322 0x000003ff, /* src_mask */
1323 0x000003ff, /* dst_mask */
1324 FALSE), /* pcrel_offset */
1326 /* Lower 12 bits of address. */
1327 HOWTO (R_NDS32_LO12S2_SP_RELA,/* type */
1329 2, /* size (0 = byte, 1 = short, 2 = long) */
1331 FALSE, /* pc_relative */
1333 complain_overflow_dont,/* complain_on_overflow */
1334 bfd_elf_generic_reloc, /* special_function */
1335 "R_NDS32_LO12S2_SP_RELA", /* name */
1336 FALSE, /* partial_inplace */
1337 0x000003ff, /* src_mask */
1338 0x000003ff, /* dst_mask */
1339 FALSE), /* pcrel_offset */
1340 /* Lower 12 bits of address. Special identity for or case. */
1341 HOWTO (R_NDS32_LO12S0_ORI_RELA, /* type */
1343 2, /* size (0 = byte, 1 = short, 2 = long) */
1345 FALSE, /* pc_relative */
1347 complain_overflow_dont,/* complain_on_overflow */
1348 bfd_elf_generic_reloc, /* special_function */
1349 "R_NDS32_LO12S0_ORI_RELA", /* name */
1350 FALSE, /* partial_inplace */
1351 0x00000fff, /* src_mask */
1352 0x00000fff, /* dst_mask */
1353 FALSE), /* pcrel_offset */
1354 /* Small data area 19 bits offset. */
1355 HOWTO (R_NDS32_SDA16S3_RELA, /* type */
1357 2, /* size (0 = byte, 1 = short, 2 = long) */
1359 FALSE, /* pc_relative */
1361 complain_overflow_signed, /* complain_on_overflow */
1362 bfd_elf_generic_reloc, /* special_function */
1363 "R_NDS32_SDA16S3_RELA",/* name */
1364 FALSE, /* partial_inplace */
1365 0x0000ffff, /* src_mask */
1366 0x0000ffff, /* dst_mask */
1367 FALSE), /* pcrel_offset */
1369 /* Small data area 15 bits offset. */
1370 HOWTO (R_NDS32_SDA17S2_RELA, /* type */
1372 2, /* size (0 = byte, 1 = short, 2 = long) */
1374 FALSE, /* pc_relative */
1376 complain_overflow_signed, /* complain_on_overflow */
1377 bfd_elf_generic_reloc, /* special_function */
1378 "R_NDS32_SDA17S2_RELA",/* name */
1379 FALSE, /* partial_inplace */
1380 0x0001ffff, /* src_mask */
1381 0x0001ffff, /* dst_mask */
1382 FALSE), /* pcrel_offset */
1384 HOWTO (R_NDS32_SDA18S1_RELA, /* type */
1386 2, /* size (0 = byte, 1 = short, 2 = long) */
1388 FALSE, /* pc_relative */
1390 complain_overflow_signed, /* complain_on_overflow */
1391 bfd_elf_generic_reloc, /* special_function */
1392 "R_NDS32_SDA18S1_RELA",/* name */
1393 FALSE, /* partial_inplace */
1394 0x0003ffff, /* src_mask */
1395 0x0003ffff, /* dst_mask */
1396 FALSE), /* pcrel_offset */
1398 HOWTO (R_NDS32_SDA19S0_RELA, /* type */
1400 2, /* size (0 = byte, 1 = short, 2 = long) */
1402 FALSE, /* pc_relative */
1404 complain_overflow_signed, /* complain_on_overflow */
1405 bfd_elf_generic_reloc, /* special_function */
1406 "R_NDS32_SDA19S0_RELA",/* name */
1407 FALSE, /* partial_inplace */
1408 0x0007ffff, /* src_mask */
1409 0x0007ffff, /* dst_mask */
1410 FALSE), /* pcrel_offset */
1411 HOWTO (R_NDS32_DWARF2_OP1_RELA, /* type */
1413 0, /* size (0 = byte, 1 = short, 2 = long) */
1415 FALSE, /* pc_relative */
1417 complain_overflow_dont,/* complain_on_overflow */
1418 nds32_elf_ignore_reloc,/* special_function */
1419 "R_NDS32_DWARF2_OP1_RELA", /* name */
1420 FALSE, /* partial_inplace */
1421 0xff, /* src_mask */
1422 0xff, /* dst_mask */
1423 FALSE), /* pcrel_offset */
1424 HOWTO (R_NDS32_DWARF2_OP2_RELA, /* type */
1426 1, /* size (0 = byte, 1 = short, 2 = long) */
1428 FALSE, /* pc_relative */
1430 complain_overflow_dont,/* complain_on_overflow */
1431 nds32_elf_ignore_reloc,/* special_function */
1432 "R_NDS32_DWARF2_OP2_RELA", /* name */
1433 FALSE, /* partial_inplace */
1434 0xffff, /* src_mask */
1435 0xffff, /* dst_mask */
1436 FALSE), /* pcrel_offset */
1437 HOWTO (R_NDS32_DWARF2_LEB_RELA, /* type */
1439 2, /* size (0 = byte, 1 = short, 2 = long) */
1441 FALSE, /* pc_relative */
1443 complain_overflow_dont,/* complain_on_overflow */
1444 nds32_elf_ignore_reloc,/* special_function */
1445 "R_NDS32_DWARF2_LEB_RELA", /* name */
1446 FALSE, /* partial_inplace */
1447 0xffffffff, /* src_mask */
1448 0xffffffff, /* dst_mask */
1449 FALSE), /* pcrel_offset */
1450 HOWTO (R_NDS32_UPDATE_TA_RELA,/* type */
1452 1, /* size (0 = byte, 1 = short, 2 = long) */
1454 FALSE, /* pc_relative */
1456 complain_overflow_dont,/* complain_on_overflow */
1457 nds32_elf_ignore_reloc,/* special_function */
1458 "R_NDS32_UPDATE_TA_RELA", /* name */
1459 FALSE, /* partial_inplace */
1460 0xffff, /* src_mask */
1461 0xffff, /* dst_mask */
1462 FALSE), /* pcrel_offset */
1463 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1464 entry for the symbol. */
1465 HOWTO (R_NDS32_9_PLTREL, /* type */
1467 1, /* size (0 = byte, 1 = short, 2 = long) */
1469 TRUE, /* pc_relative */
1471 complain_overflow_signed, /* complain_on_overflow */
1472 bfd_elf_generic_reloc, /* special_function */
1473 "R_NDS32_9_PLTREL", /* name */
1474 FALSE, /* partial_inplace */
1475 0xff, /* src_mask */
1476 0xff, /* dst_mask */
1477 TRUE), /* pcrel_offset */
1478 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1479 HOWTO (R_NDS32_PLT_GOTREL_LO20, /* type */
1481 2, /* size (0 = byte, 1 = short, 2 = long) */
1483 FALSE, /* pc_relative */
1485 complain_overflow_dont,/* complain_on_overflow */
1486 bfd_elf_generic_reloc, /* special_function */
1487 "R_NDS32_PLT_GOTREL_LO20", /* name */
1488 FALSE, /* partial_inplace */
1489 0x000fffff, /* src_mask */
1490 0x000fffff, /* dst_mask */
1491 FALSE), /* pcrel_offset */
1492 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1493 HOWTO (R_NDS32_PLT_GOTREL_LO15, /* type */
1495 2, /* size (0 = byte, 1 = short, 2 = long) */
1497 FALSE, /* pc_relative */
1499 complain_overflow_dont,/* complain_on_overflow */
1500 bfd_elf_generic_reloc, /* special_function */
1501 "R_NDS32_PLT_GOTREL_LO15", /* name */
1502 FALSE, /* partial_inplace */
1503 0x00007fff, /* src_mask */
1504 0x00007fff, /* dst_mask */
1505 FALSE), /* pcrel_offset */
1506 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1507 HOWTO (R_NDS32_PLT_GOTREL_LO19, /* type */
1509 2, /* size (0 = byte, 1 = short, 2 = long) */
1511 FALSE, /* pc_relative */
1513 complain_overflow_dont,/* complain_on_overflow */
1514 bfd_elf_generic_reloc, /* special_function */
1515 "R_NDS32_PLT_GOTREL_LO19", /* name */
1516 FALSE, /* partial_inplace */
1517 0x0007ffff, /* src_mask */
1518 0x0007ffff, /* dst_mask */
1519 FALSE), /* pcrel_offset */
1520 HOWTO (R_NDS32_GOT_LO15, /* type */
1522 2, /* size (0 = byte, 1 = short, 2 = long) */
1524 FALSE, /* pc_relative */
1526 complain_overflow_dont,/* complain_on_overflow */
1527 bfd_elf_generic_reloc, /* special_function */
1528 "R_NDS32_GOT_LO15", /* name */
1529 FALSE, /* partial_inplace */
1530 0x00007fff, /* src_mask */
1531 0x00007fff, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1533 HOWTO (R_NDS32_GOT_LO19, /* type */
1535 2, /* size (0 = byte, 1 = short, 2 = long) */
1537 FALSE, /* pc_relative */
1539 complain_overflow_dont,/* complain_on_overflow */
1540 bfd_elf_generic_reloc, /* special_function */
1541 "R_NDS32_GOT_LO19", /* name */
1542 FALSE, /* partial_inplace */
1543 0x0007ffff, /* src_mask */
1544 0x0007ffff, /* dst_mask */
1545 FALSE), /* pcrel_offset */
1546 HOWTO (R_NDS32_GOTOFF_LO15, /* type */
1548 2, /* size (0 = byte, 1 = short, 2 = long) */
1550 FALSE, /* pc_relative */
1552 complain_overflow_dont,/* complain_on_overflow */
1553 bfd_elf_generic_reloc, /* special_function */
1554 "R_NDS32_GOTOFF_LO15", /* name */
1555 FALSE, /* partial_inplace */
1556 0x00007fff, /* src_mask */
1557 0x00007fff, /* dst_mask */
1558 FALSE), /* pcrel_offset */
1559 HOWTO (R_NDS32_GOTOFF_LO19, /* type */
1561 2, /* size (0 = byte, 1 = short, 2 = long) */
1563 FALSE, /* pc_relative */
1565 complain_overflow_dont,/* complain_on_overflow */
1566 bfd_elf_generic_reloc, /* special_function */
1567 "R_NDS32_GOTOFF_LO19", /* name */
1568 FALSE, /* partial_inplace */
1569 0x0007ffff, /* src_mask */
1570 0x0007ffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572 /* GOT 15 bits offset. */
1573 HOWTO (R_NDS32_GOT15S2_RELA, /* type */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1577 FALSE, /* pc_relative */
1579 complain_overflow_signed, /* complain_on_overflow */
1580 bfd_elf_generic_reloc, /* special_function */
1581 "R_NDS32_GOT15S2_RELA",/* name */
1582 FALSE, /* partial_inplace */
1583 0x00007fff, /* src_mask */
1584 0x00007fff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586 /* GOT 17 bits offset. */
1587 HOWTO (R_NDS32_GOT17S2_RELA, /* type */
1589 2, /* size (0 = byte, 1 = short, 2 = long) */
1591 FALSE, /* pc_relative */
1593 complain_overflow_signed, /* complain_on_overflow */
1594 bfd_elf_generic_reloc, /* special_function */
1595 "R_NDS32_GOT17S2_RELA",/* name */
1596 FALSE, /* partial_inplace */
1597 0x0001ffff, /* src_mask */
1598 0x0001ffff, /* dst_mask */
1599 FALSE), /* pcrel_offset */
1600 /* A 5 bit address. */
1601 HOWTO (R_NDS32_5_RELA, /* type */
1603 1, /* size (0 = byte, 1 = short, 2 = long) */
1605 FALSE, /* pc_relative */
1607 complain_overflow_signed, /* complain_on_overflow */
1608 bfd_elf_generic_reloc, /* special_function */
1609 "R_NDS32_5_RELA", /* name */
1610 FALSE, /* partial_inplace */
1611 0x1f, /* src_mask */
1612 0x1f, /* dst_mask */
1613 FALSE), /* pcrel_offset */
1614 HOWTO (R_NDS32_10_UPCREL_RELA,/* type */
1616 1, /* size (0 = byte, 1 = short, 2 = long) */
1618 TRUE, /* pc_relative */
1620 complain_overflow_unsigned, /* complain_on_overflow */
1621 bfd_elf_generic_reloc, /* special_function */
1622 "R_NDS32_10_UPCREL_RELA", /* name */
1623 FALSE, /* partial_inplace */
1624 0x1ff, /* src_mask */
1625 0x1ff, /* dst_mask */
1626 TRUE), /* pcrel_offset */
1627 HOWTO (R_NDS32_SDA_FP7U2_RELA,/* type */
1629 1, /* size (0 = byte, 1 = short, 2 = long) */
1631 FALSE, /* pc_relative */
1633 complain_overflow_unsigned, /* complain_on_overflow */
1634 bfd_elf_generic_reloc, /* special_function */
1635 "R_NDS32_SDA_FP7U2_RELA", /* name */
1636 FALSE, /* partial_inplace */
1637 0x0000007f, /* src_mask */
1638 0x0000007f, /* dst_mask */
1639 FALSE), /* pcrel_offset */
1640 HOWTO (R_NDS32_WORD_9_PCREL_RELA, /* type */
1642 2, /* size (0 = byte, 1 = short, 2 = long) */
1644 TRUE, /* pc_relative */
1646 complain_overflow_signed, /* complain_on_overflow */
1647 bfd_elf_generic_reloc, /* special_function */
1648 "R_NDS32_WORD_9_PCREL_RELA", /* name */
1649 FALSE, /* partial_inplace */
1650 0xff, /* src_mask */
1651 0xff, /* dst_mask */
1652 TRUE), /* pcrel_offset */
1653 HOWTO (R_NDS32_25_ABS_RELA, /* type */
1655 2, /* size (0 = byte, 1 = short, 2 = long) */
1657 FALSE, /* pc_relative */
1659 complain_overflow_dont,/* complain_on_overflow */
1660 bfd_elf_generic_reloc, /* special_function */
1661 "R_NDS32_25_ABS_RELA", /* name */
1662 FALSE, /* partial_inplace */
1663 0xffffff, /* src_mask */
1664 0xffffff, /* dst_mask */
1665 FALSE), /* pcrel_offset */
1667 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1668 HOWTO (R_NDS32_17IFC_PCREL_RELA, /* type */
1670 2, /* size (0 = byte, 1 = short, 2 = long) */
1672 TRUE, /* pc_relative */
1674 complain_overflow_signed, /* complain_on_overflow */
1675 bfd_elf_generic_reloc, /* special_function */
1676 "R_NDS32_17IFC_PCREL_RELA", /* name */
1677 FALSE, /* partial_inplace */
1678 0xffff, /* src_mask */
1679 0xffff, /* dst_mask */
1680 TRUE), /* pcrel_offset */
1682 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1683 HOWTO (R_NDS32_10IFCU_PCREL_RELA, /* type */
1685 1, /* size (0 = byte, 1 = short, 2 = long) */
1687 TRUE, /* pc_relative */
1689 complain_overflow_unsigned, /* complain_on_overflow */
1690 bfd_elf_generic_reloc, /* special_function */
1691 "R_NDS32_10IFCU_PCREL_RELA", /* name */
1692 FALSE, /* partial_inplace */
1693 0x1ff, /* src_mask */
1694 0x1ff, /* dst_mask */
1695 TRUE), /* pcrel_offset */
1697 /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol. */
1698 HOWTO (R_NDS32_TLS_LE_HI20, /* type */
1699 12, /* rightshift */
1700 2, /* size (0 = byte, 1 = short, 2 = long) */
1702 FALSE, /* pc_relative */
1704 complain_overflow_dont, /* complain_on_overflow */
1705 bfd_elf_generic_reloc, /* special_function */
1706 "R_NDS32_TLS_LE_HI20", /* name */
1707 FALSE, /* partial_inplace */
1708 0x000fffff, /* src_mask */
1709 0x000fffff, /* dst_mask */
1710 FALSE), /* pcrel_offset */
1711 HOWTO (R_NDS32_TLS_LE_LO12, /* type */
1713 2, /* size (0 = byte, 1 = short, 2 = long) */
1715 FALSE, /* pc_relative */
1717 complain_overflow_dont, /* complain_on_overflow */
1718 bfd_elf_generic_reloc, /* special_function */
1719 "R_NDS32_TLS_LE_LO12", /* name */
1720 FALSE, /* partial_inplace */
1721 0x00000fff, /* src_mask */
1722 0x00000fff, /* dst_mask */
1723 FALSE), /* pcrel_offset */
1725 /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol. */
1726 HOWTO (R_NDS32_TLS_IE_HI20, /* type */
1727 12, /* rightshift */
1728 2, /* size (0 = byte, 1 = short, 2 = long) */
1730 FALSE, /* pc_relative */
1732 complain_overflow_dont, /* complain_on_overflow */
1733 bfd_elf_generic_reloc, /* special_function */
1734 "R_NDS32_TLS_IE_HI20", /* name */
1735 FALSE, /* partial_inplace */
1736 0x000fffff, /* src_mask */
1737 0x000fffff, /* dst_mask */
1738 FALSE), /* pcrel_offset */
1739 HOWTO (R_NDS32_TLS_IE_LO12S2, /* type */
1741 2, /* size (0 = byte, 1 = short, 2 = long) */
1743 FALSE, /* pc_relative */
1745 complain_overflow_dont, /* complain_on_overflow */
1746 bfd_elf_generic_reloc, /* special_function */
1747 "R_NDS32_TLS_IE_LO12S2", /* name */
1748 FALSE, /* partial_inplace */
1749 0x000003ff, /* src_mask */
1750 0x000003ff, /* dst_mask */
1751 FALSE), /* pcrel_offset */
1752 /* Mark a TLS IE entry in GOT. */
1753 HOWTO (R_NDS32_TLS_TPOFF, /* type */
1755 2, /* size (0 = byte, 1 = short, 2 = long) */
1757 FALSE, /* pc_relative */
1759 complain_overflow_bitfield, /* complain_on_overflow */
1760 bfd_elf_generic_reloc, /* special_function */
1761 "R_NDS32_TLS_TPOFF", /* name */
1762 FALSE, /* partial_inplace */
1763 0xffffffff, /* src_mask */
1764 0xffffffff, /* dst_mask */
1765 FALSE), /* pcrel_offset */
1766 /* A 20 bit address. */
1767 HOWTO (R_NDS32_TLS_LE_20, /* type */
1769 2, /* size (0 = byte, 1 = short, 2 = long) */
1771 FALSE, /* pc_relative */
1773 complain_overflow_signed, /* complain_on_overflow */
1774 bfd_elf_generic_reloc, /* special_function */
1775 "R_NDS32_TLS_LE_20", /* name */
1776 FALSE, /* partial_inplace */
1777 0xfffff, /* src_mask */
1778 0xfffff, /* dst_mask */
1779 FALSE), /* pcrel_offset */
1780 HOWTO (R_NDS32_TLS_LE_15S0, /* type */
1782 2, /* size (0 = byte, 1 = short, 2 = long) */
1784 FALSE, /* pc_relative */
1786 complain_overflow_signed, /* complain_on_overflow */
1787 bfd_elf_generic_reloc, /* special_function */
1788 "R_NDS32_TLS_LE_15S0", /* name */
1789 FALSE, /* partial_inplace */
1790 0x7fff, /* src_mask */
1791 0x7fff, /* dst_mask */
1792 FALSE), /* pcrel_offset */
1793 HOWTO (R_NDS32_TLS_LE_15S1, /* type */
1795 2, /* size (0 = byte, 1 = short, 2 = long) */
1797 FALSE, /* pc_relative */
1799 complain_overflow_signed, /* complain_on_overflow */
1800 bfd_elf_generic_reloc, /* special_function */
1801 "R_NDS32_TLS_LE_15S1", /* name */
1802 FALSE, /* partial_inplace */
1803 0x7fff, /* src_mask */
1804 0x7fff, /* dst_mask */
1805 FALSE), /* pcrel_offset */
1806 HOWTO (R_NDS32_TLS_LE_15S2, /* type */
1808 2, /* size (0 = byte, 1 = short, 2 = long) */
1810 FALSE, /* pc_relative */
1812 complain_overflow_signed, /* complain_on_overflow */
1813 bfd_elf_generic_reloc, /* special_function */
1814 "R_NDS32_TLS_LE_15S2", /* name */
1815 FALSE, /* partial_inplace */
1816 0x7fff, /* src_mask */
1817 0x7fff, /* dst_mask */
1818 FALSE), /* pcrel_offset */
1820 /* Relax hint for unconditional call sequence */
1821 HOWTO (R_NDS32_LONGCALL4, /* type */
1823 2, /* size (0 = byte, 1 = short, 2 = long) */
1825 FALSE, /* pc_relative */
1827 complain_overflow_dont, /* complain_on_overflow */
1828 nds32_elf_ignore_reloc, /* special_function */
1829 "R_NDS32_LONGCALL4", /* name */
1830 FALSE, /* partial_inplace */
1831 0xffffffff, /* src_mask */
1832 0xffffffff, /* dst_mask */
1833 FALSE), /* pcrel_offset */
1835 /* Relax hint for conditional call sequence. */
1836 HOWTO (R_NDS32_LONGCALL5, /* type */
1838 2, /* size (0 = byte, 1 = short, 2 = long) */
1840 FALSE, /* pc_relative */
1842 complain_overflow_dont, /* complain_on_overflow */
1843 nds32_elf_ignore_reloc, /* special_function */
1844 "R_NDS32_LONGCALL5", /* name */
1845 FALSE, /* partial_inplace */
1846 0xffffffff, /* src_mask */
1847 0xffffffff, /* dst_mask */
1848 FALSE), /* pcrel_offset */
1850 /* Relax hint for conditional call sequence. */
1851 HOWTO (R_NDS32_LONGCALL6, /* type */
1853 2, /* size (0 = byte, 1 = short, 2 = long) */
1855 FALSE, /* pc_relative */
1857 complain_overflow_dont, /* complain_on_overflow */
1858 nds32_elf_ignore_reloc, /* special_function */
1859 "R_NDS32_LONGCALL6", /* name */
1860 FALSE, /* partial_inplace */
1861 0xffffffff, /* src_mask */
1862 0xffffffff, /* dst_mask */
1863 FALSE), /* pcrel_offset */
1865 /* Relax hint for unconditional branch sequence. */
1866 HOWTO (R_NDS32_LONGJUMP4, /* type */
1868 2, /* size (0 = byte, 1 = short, 2 = long) */
1870 FALSE, /* pc_relative */
1872 complain_overflow_dont, /* complain_on_overflow */
1873 nds32_elf_ignore_reloc, /* special_function */
1874 "R_NDS32_LONGJUMP4", /* name */
1875 FALSE, /* partial_inplace */
1876 0xffffffff, /* src_mask */
1877 0xffffffff, /* dst_mask */
1878 FALSE), /* pcrel_offset */
1880 /* Relax hint for conditional branch sequence. */
1881 HOWTO (R_NDS32_LONGJUMP5, /* type */
1883 2, /* 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_LONGJUMP5", /* name */
1890 FALSE, /* partial_inplace */
1891 0xffffffff, /* src_mask */
1892 0xffffffff, /* dst_mask */
1893 FALSE), /* pcrel_offset */
1895 /* Relax hint for conditional branch sequence. */
1896 HOWTO (R_NDS32_LONGJUMP6, /* type */
1898 2, /* size (0 = byte, 1 = short, 2 = long) */
1900 FALSE, /* pc_relative */
1902 complain_overflow_dont, /* complain_on_overflow */
1903 nds32_elf_ignore_reloc, /* special_function */
1904 "R_NDS32_LONGJUMP6", /* name */
1905 FALSE, /* partial_inplace */
1906 0xffffffff, /* src_mask */
1907 0xffffffff, /* dst_mask */
1908 FALSE), /* pcrel_offset */
1910 /* Relax hint for conditional branch sequence. */
1911 HOWTO (R_NDS32_LONGJUMP7, /* type */
1913 2, /* size (0 = byte, 1 = short, 2 = long) */
1915 FALSE, /* pc_relative */
1917 complain_overflow_dont, /* complain_on_overflow */
1918 nds32_elf_ignore_reloc, /* special_function */
1919 "R_NDS32_LONGJUMP7", /* name */
1920 FALSE, /* partial_inplace */
1921 0xffffffff, /* src_mask */
1922 0xffffffff, /* dst_mask */
1923 FALSE), /* pcrel_offset */
1926 /* Relocations used for relaxation. */
1927 static reloc_howto_type nds32_elf_relax_howto_table[] =
1929 HOWTO (R_NDS32_RELAX_ENTRY, /* type */
1931 2, /* size (0 = byte, 1 = short, 2 = long) */
1933 FALSE, /* pc_relative */
1935 complain_overflow_dont,/* complain_on_overflow */
1936 nds32_elf_ignore_reloc,/* special_function */
1937 "R_NDS32_RELAX_ENTRY", /* name */
1938 FALSE, /* partial_inplace */
1939 0xffffffff, /* src_mask */
1940 0xffffffff, /* dst_mask */
1941 FALSE), /* pcrel_offset */
1942 HOWTO (R_NDS32_GOT_SUFF, /* type */
1944 2, /* size (0 = byte, 1 = short, 2 = long) */
1946 FALSE, /* pc_relative */
1948 complain_overflow_dont,/* complain_on_overflow */
1949 nds32_elf_ignore_reloc,/* special_function */
1950 "R_NDS32_GOT_SUFF", /* name */
1951 FALSE, /* partial_inplace */
1952 0xffffffff, /* src_mask */
1953 0xffffffff, /* dst_mask */
1954 FALSE), /* pcrel_offset */
1955 HOWTO (R_NDS32_GOTOFF_SUFF, /* type */
1957 2, /* size (0 = byte, 1 = short, 2 = long) */
1959 FALSE, /* pc_relative */
1961 complain_overflow_bitfield, /* complain_on_overflow */
1962 nds32_elf_ignore_reloc,/* special_function */
1963 "R_NDS32_GOTOFF_SUFF", /* name */
1964 FALSE, /* partial_inplace */
1965 0xffffffff, /* src_mask */
1966 0xffffffff, /* dst_mask */
1967 FALSE), /* pcrel_offset */
1968 HOWTO (R_NDS32_PLT_GOT_SUFF, /* type */
1970 2, /* size (0 = byte, 1 = short, 2 = long) */
1972 FALSE, /* pc_relative */
1974 complain_overflow_dont,/* complain_on_overflow */
1975 nds32_elf_ignore_reloc,/* special_function */
1976 "R_NDS32_PLT_GOT_SUFF",/* name */
1977 FALSE, /* partial_inplace */
1978 0xffffffff, /* src_mask */
1979 0xffffffff, /* dst_mask */
1980 FALSE), /* pcrel_offset */
1981 HOWTO (R_NDS32_MULCALL_SUFF, /* type */
1983 2, /* size (0 = byte, 1 = short, 2 = long) */
1985 FALSE, /* pc_relative */
1987 complain_overflow_dont,/* complain_on_overflow */
1988 nds32_elf_ignore_reloc,/* special_function */
1989 "R_NDS32_MULCALL_SUFF",/* name */
1990 FALSE, /* partial_inplace */
1991 0xffffffff, /* src_mask */
1992 0xffffffff, /* dst_mask */
1993 FALSE), /* pcrel_offset */
1994 HOWTO (R_NDS32_PTR, /* type */
1996 2, /* size (0 = byte, 1 = short, 2 = long) */
1998 FALSE, /* pc_relative */
2000 complain_overflow_dont,/* complain_on_overflow */
2001 nds32_elf_ignore_reloc,/* special_function */
2002 "R_NDS32_PTR", /* name */
2003 FALSE, /* partial_inplace */
2004 0xffffffff, /* src_mask */
2005 0xffffffff, /* dst_mask */
2006 FALSE), /* pcrel_offset */
2007 HOWTO (R_NDS32_PTR_COUNT, /* type */
2009 2, /* size (0 = byte, 1 = short, 2 = long) */
2011 FALSE, /* pc_relative */
2013 complain_overflow_dont,/* complain_on_overflow */
2014 nds32_elf_ignore_reloc,/* special_function */
2015 "R_NDS32_PTR_COUNT", /* name */
2016 FALSE, /* partial_inplace */
2017 0xffffffff, /* src_mask */
2018 0xffffffff, /* dst_mask */
2019 FALSE), /* pcrel_offset */
2020 HOWTO (R_NDS32_PTR_RESOLVED, /* type */
2022 2, /* size (0 = byte, 1 = short, 2 = long) */
2024 FALSE, /* pc_relative */
2026 complain_overflow_dont,/* complain_on_overflow */
2027 nds32_elf_ignore_reloc,/* special_function */
2028 "R_NDS32_PTR_RESOLVED",/* name */
2029 FALSE, /* partial_inplace */
2030 0xffffffff, /* src_mask */
2031 0xffffffff, /* dst_mask */
2032 FALSE), /* pcrel_offset */
2033 HOWTO (R_NDS32_PLTBLOCK, /* type */
2035 2, /* size (0 = byte, 1 = short, 2 = long) */
2037 FALSE, /* pc_relative */
2039 complain_overflow_dont,/* complain_on_overflow */
2040 nds32_elf_ignore_reloc,/* special_function */
2041 "R_NDS32_PLTBLOCK", /* name */
2042 FALSE, /* partial_inplace */
2043 0xffffffff, /* src_mask */
2044 0xffffffff, /* dst_mask */
2045 FALSE), /* pcrel_offset */
2046 HOWTO (R_NDS32_RELAX_REGION_BEGIN, /* type */
2048 2, /* size (0 = byte, 1 = short, 2 = long) */
2050 FALSE, /* pc_relative */
2052 complain_overflow_dont,/* complain_on_overflow */
2053 nds32_elf_ignore_reloc,/* special_function */
2054 "R_NDS32_RELAX_REGION_BEGIN", /* name */
2055 FALSE, /* partial_inplace */
2056 0xffffffff, /* src_mask */
2057 0xffffffff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2059 HOWTO (R_NDS32_RELAX_REGION_END, /* type */
2061 2, /* size (0 = byte, 1 = short, 2 = long) */
2063 FALSE, /* pc_relative */
2065 complain_overflow_dont,/* complain_on_overflow */
2066 nds32_elf_ignore_reloc,/* special_function */
2067 "R_NDS32_RELAX_REGION_END", /* name */
2068 FALSE, /* partial_inplace */
2069 0xffffffff, /* src_mask */
2070 0xffffffff, /* dst_mask */
2071 FALSE), /* pcrel_offset */
2072 HOWTO (R_NDS32_MINUEND, /* type */
2074 2, /* size (0 = byte, 1 = short, 2 = long) */
2076 FALSE, /* pc_relative */
2078 complain_overflow_dont,/* complain_on_overflow */
2079 nds32_elf_ignore_reloc,/* special_function */
2080 "R_NDS32_MINUEND", /* name */
2081 FALSE, /* partial_inplace */
2082 0xffffffff, /* src_mask */
2083 0xffffffff, /* dst_mask */
2084 FALSE), /* pcrel_offset */
2085 HOWTO (R_NDS32_SUBTRAHEND, /* type */
2087 2, /* size (0 = byte, 1 = short, 2 = long) */
2089 FALSE, /* pc_relative */
2091 complain_overflow_dont,/* complain_on_overflow */
2092 nds32_elf_ignore_reloc,/* special_function */
2093 "R_NDS32_SUBTRAHEND", /* name */
2094 FALSE, /* partial_inplace */
2095 0xffffffff, /* src_mask */
2096 0xffffffff, /* dst_mask */
2097 FALSE), /* pcrel_offset */
2098 HOWTO (R_NDS32_DIFF8, /* type */
2100 0, /* size (0 = byte, 1 = short, 2 = long) */
2102 FALSE, /* pc_relative */
2104 complain_overflow_dont,/* complain_on_overflow */
2105 nds32_elf_ignore_reloc,/* special_function */
2106 "R_NDS32_DIFF8", /* name */
2107 FALSE, /* partial_inplace */
2108 0x000000ff, /* src_mask */
2109 0x000000ff, /* dst_mask */
2110 FALSE), /* pcrel_offset */
2111 HOWTO (R_NDS32_DIFF16, /* type */
2113 1, /* size (0 = byte, 1 = short, 2 = long) */
2115 FALSE, /* pc_relative */
2117 complain_overflow_dont,/* complain_on_overflow */
2118 nds32_elf_ignore_reloc,/* special_function */
2119 "R_NDS32_DIFF16", /* name */
2120 FALSE, /* partial_inplace */
2121 0x0000ffff, /* src_mask */
2122 0x0000ffff, /* dst_mask */
2123 FALSE), /* pcrel_offset */
2124 HOWTO (R_NDS32_DIFF32, /* type */
2126 2, /* size (0 = byte, 1 = short, 2 = long) */
2128 FALSE, /* pc_relative */
2130 complain_overflow_dont,/* complain_on_overflow */
2131 nds32_elf_ignore_reloc,/* special_function */
2132 "R_NDS32_DIFF32", /* name */
2133 FALSE, /* partial_inplace */
2134 0xffffffff, /* src_mask */
2135 0xffffffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137 HOWTO (R_NDS32_DIFF_ULEB128, /* type */
2139 0, /* size (0 = byte, 1 = short, 2 = long) */
2141 FALSE, /* pc_relative */
2143 complain_overflow_dont,/* complain_on_overflow */
2144 nds32_elf_ignore_reloc,/* special_function */
2145 "R_NDS32_DIFF_ULEB128",/* name */
2146 FALSE, /* partial_inplace */
2147 0xffffffff, /* src_mask */
2148 0xffffffff, /* dst_mask */
2149 FALSE), /* pcrel_offset */
2150 HOWTO (R_NDS32_DATA, /* type */
2152 2, /* size (0 = byte, 1 = short, 2 = long) */
2154 FALSE, /* pc_relative */
2156 complain_overflow_dont,/* complain_on_overflow */
2157 nds32_elf_ignore_reloc,/* special_function */
2158 "R_NDS32_DATA", /* name */
2159 FALSE, /* partial_inplace */
2160 0xffffffff, /* src_mask */
2161 0xffffffff, /* dst_mask */
2162 FALSE), /* pcrel_offset */
2163 HOWTO (R_NDS32_TRAN, /* type */
2165 2, /* size (0 = byte, 1 = short, 2 = long) */
2167 FALSE, /* pc_relative */
2169 complain_overflow_dont,/* complain_on_overflow */
2170 nds32_elf_ignore_reloc,/* special_function */
2171 "R_NDS32_TRAN", /* name */
2172 FALSE, /* partial_inplace */
2173 0xffffffff, /* src_mask */
2174 0xffffffff, /* dst_mask */
2175 FALSE), /* pcrel_offset */
2176 HOWTO (R_NDS32_TLS_LE_ADD, /* type */
2178 2, /* size (0 = byte, 1 = short, 2 = long) */
2180 FALSE, /* pc_relative */
2182 complain_overflow_dont, /* complain_on_overflow */
2183 nds32_elf_ignore_reloc, /* special_function */
2184 "R_NDS32_TLS_LE_ADD", /* name */
2185 FALSE, /* partial_inplace */
2186 0xffffffff, /* src_mask */
2187 0xffffffff, /* dst_mask */
2188 FALSE), /* pcrel_offset */
2189 HOWTO (R_NDS32_TLS_LE_LS, /* type */
2191 2, /* size (0 = byte, 1 = short, 2 = long) */
2193 FALSE, /* pc_relative */
2195 complain_overflow_dont, /* complain_on_overflow */
2196 nds32_elf_ignore_reloc, /* special_function */
2197 "R_NDS32_TLS_LE_LS", /* name */
2198 FALSE, /* partial_inplace */
2199 0xffffffff, /* src_mask */
2200 0xffffffff, /* dst_mask */
2201 FALSE), /* pcrel_offset */
2202 HOWTO (R_NDS32_EMPTY, /* type */
2204 2, /* size (0 = byte, 1 = short, 2 = long) */
2206 FALSE, /* pc_relative */
2208 complain_overflow_dont, /* complain_on_overflow */
2209 nds32_elf_ignore_reloc, /* special_function */
2210 "R_NDS32_EMPTY", /* name */
2211 FALSE, /* partial_inplace */
2212 0xffffffff, /* src_mask */
2213 0xffffffff, /* dst_mask */
2214 FALSE), /* pcrel_offset */
2218 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2219 This prototype is the same as qsort (). */
2222 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2223 int (*compar) (const void *lhs, const void *rhs))
2225 char *ptr = (char *) base;
2227 char *tmp = xmalloc (size);
2229 /* If i is less than j, i is inserted before j.
2231 |---- j ----- i --------------|
2236 for (i = 1; i < (int) nmemb; i++)
2238 for (j = (i - 1); j >= 0; j--)
2239 if (compar (ptr + i * size, ptr + j * size) >= 0)
2245 continue; /* i is in order. */
2247 memcpy (tmp, ptr + i * size, size);
2248 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2249 memcpy (ptr + j * size, tmp, size);
2254 /* Sort relocation by r_offset.
2256 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2257 algorithm. Relocations at the same r_offset must keep their order.
2258 For example, RELAX_ENTRY must be the very first relocation entry.
2260 Currently, this function implements insertion-sort.
2262 FIXME: If we already sort them in assembler, why bother sort them
2266 compar_reloc (const void *lhs, const void *rhs)
2268 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2269 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2271 if (l->r_offset > r->r_offset)
2273 else if (l->r_offset == r->r_offset)
2279 /* Functions listed below are only used for old relocs.
2280 * nds32_elf_9_pcrel_reloc
2281 * nds32_elf_do_9_pcrel_reloc
2282 * nds32_elf_hi20_reloc
2283 * nds32_elf_relocate_hi20
2284 * nds32_elf_lo12_reloc
2285 * nds32_elf_sda15_reloc
2286 * nds32_elf_generic_reloc
2289 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2291 static bfd_reloc_status_type
2292 nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2293 void *data, asection *input_section, bfd *output_bfd,
2294 char **error_message ATTRIBUTE_UNUSED)
2296 /* This part is from bfd_elf_generic_reloc. */
2297 if (output_bfd != (bfd *) NULL
2298 && (symbol->flags & BSF_SECTION_SYM) == 0
2299 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2301 reloc_entry->address += input_section->output_offset;
2302 return bfd_reloc_ok;
2305 if (output_bfd != NULL)
2307 /* FIXME: See bfd_perform_relocation. Is this right? */
2308 return bfd_reloc_continue;
2311 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2313 data, reloc_entry->address,
2316 + symbol->section->output_section->vma
2317 + symbol->section->output_offset),
2318 reloc_entry->addend);
2321 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2322 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2324 static bfd_reloc_status_type
2325 nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
2326 asection *input_section, bfd_byte *data,
2328 asection *symbol_section ATTRIBUTE_UNUSED,
2329 bfd_vma symbol_value, bfd_vma addend)
2331 bfd_signed_vma relocation;
2333 bfd_reloc_status_type status;
2335 /* Sanity check the address (offset in section). */
2336 if (offset > bfd_get_section_limit (abfd, input_section))
2337 return bfd_reloc_outofrange;
2339 relocation = symbol_value + addend;
2340 /* Make it pc relative. */
2341 relocation -= (input_section->output_section->vma
2342 + input_section->output_offset);
2343 /* These jumps mask off the lower two bits of the current address
2344 before doing pcrel calculations. */
2345 relocation -= (offset & -(bfd_vma) 2);
2347 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2348 status = bfd_reloc_overflow;
2350 status = bfd_reloc_ok;
2352 x = bfd_getb16 (data + offset);
2354 relocation >>= howto->rightshift;
2355 relocation <<= howto->bitpos;
2356 x = (x & ~howto->dst_mask)
2357 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2359 bfd_putb16 ((bfd_vma) x, data + offset);
2364 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2365 HI20_SLO is for the add3 and load/store with displacement instructions.
2366 HI20 is for the or3 instruction.
2367 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2368 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2369 we must add one to the high 16 bytes (which will get subtracted off when
2370 the low 16 bits are added).
2371 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2372 because there is a carry from the LO12 to the HI20. Here we just save
2373 the information we need; we do the actual relocation when we see the LO12.
2374 This code is copied from the elf32-mips.c. We also support an arbitrary
2375 number of HI20 relocs to be associated with a single LO12 reloc. The
2376 assembler sorts the relocs to ensure each HI20 immediately precedes its
2377 LO12. However if there are multiple copies, the assembler may not find
2378 the real LO12 so it picks the first one it finds. */
2382 struct nds32_hi20 *next;
2387 static struct nds32_hi20 *nds32_hi20_list;
2389 static bfd_reloc_status_type
2390 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2391 asymbol *symbol, void *data, asection *input_section,
2392 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2394 bfd_reloc_status_type ret;
2396 struct nds32_hi20 *n;
2398 /* This part is from bfd_elf_generic_reloc.
2399 If we're relocating, and this an external symbol, we don't want
2400 to change anything. */
2401 if (output_bfd != (bfd *) NULL
2402 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2404 reloc_entry->address += input_section->output_offset;
2405 return bfd_reloc_ok;
2408 /* Sanity check the address (offset in section). */
2409 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2410 return bfd_reloc_outofrange;
2413 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2414 ret = bfd_reloc_undefined;
2416 if (bfd_is_com_section (symbol->section))
2419 relocation = symbol->value;
2421 relocation += symbol->section->output_section->vma;
2422 relocation += symbol->section->output_offset;
2423 relocation += reloc_entry->addend;
2425 /* Save the information, and let LO12 do the actual relocation. */
2426 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2428 return bfd_reloc_outofrange;
2430 n->addr = (bfd_byte *) data + reloc_entry->address;
2431 n->addend = relocation;
2432 n->next = nds32_hi20_list;
2433 nds32_hi20_list = n;
2435 if (output_bfd != (bfd *) NULL)
2436 reloc_entry->address += input_section->output_offset;
2441 /* Handle an NDS32 ELF HI20 reloc. */
2444 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2445 int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
2446 Elf_Internal_Rela *rello, bfd_byte *contents,
2452 insn = bfd_getb32 (contents + relhi->r_offset);
2454 addlo = bfd_getb32 (contents + rello->r_offset);
2457 addend += ((insn & 0xfffff) << 20) + addlo;
2459 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2460 bfd_putb32 (insn, contents + relhi->r_offset);
2463 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2464 inplace relocation; this function exists in order to do the
2465 R_NDS32_HI20_[SU]LO relocation described above. */
2467 static bfd_reloc_status_type
2468 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2469 void *data, asection *input_section, bfd *output_bfd,
2470 char **error_message)
2472 /* This part is from bfd_elf_generic_reloc.
2473 If we're relocating, and this an external symbol, we don't want
2474 to change anything. */
2475 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2476 && reloc_entry->addend == 0)
2478 reloc_entry->address += input_section->output_offset;
2479 return bfd_reloc_ok;
2482 if (nds32_hi20_list != NULL)
2484 struct nds32_hi20 *l;
2486 l = nds32_hi20_list;
2491 unsigned long vallo;
2492 struct nds32_hi20 *next;
2494 /* Do the HI20 relocation. Note that we actually don't need
2495 to know anything about the LO12 itself, except where to
2496 find the low 12 bits of the addend needed by the LO12. */
2497 insn = bfd_getb32 (l->addr);
2498 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2500 switch (reloc_entry->howto->type)
2502 case R_NDS32_LO12S3:
2506 case R_NDS32_LO12S2:
2510 case R_NDS32_LO12S1:
2514 case R_NDS32_LO12S0:
2519 val = ((insn & 0xfffff) << 12) + vallo;
2522 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2523 bfd_putb32 ((bfd_vma) insn, l->addr);
2530 nds32_hi20_list = NULL;
2533 /* Now do the LO12 reloc in the usual way.
2534 ??? It would be nice to call bfd_elf_generic_reloc here,
2535 but we have partial_inplace set. bfd_elf_generic_reloc will
2536 pass the handling back to bfd_install_relocation which will install
2537 a section relative addend which is wrong. */
2538 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2539 input_section, output_bfd, error_message);
2542 /* Do generic partial_inplace relocation.
2543 This is a local replacement for bfd_elf_generic_reloc. */
2545 static bfd_reloc_status_type
2546 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2547 asymbol *symbol, void *data, asection *input_section,
2548 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2550 bfd_reloc_status_type ret;
2552 bfd_byte *inplace_address;
2554 /* This part is from bfd_elf_generic_reloc.
2555 If we're relocating, and this an external symbol, we don't want
2556 to change anything. */
2557 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2558 && reloc_entry->addend == 0)
2560 reloc_entry->address += input_section->output_offset;
2561 return bfd_reloc_ok;
2564 /* Now do the reloc in the usual way.
2565 ??? It would be nice to call bfd_elf_generic_reloc here,
2566 but we have partial_inplace set. bfd_elf_generic_reloc will
2567 pass the handling back to bfd_install_relocation which will install
2568 a section relative addend which is wrong. */
2570 /* Sanity check the address (offset in section). */
2571 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2572 return bfd_reloc_outofrange;
2575 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2576 ret = bfd_reloc_undefined;
2578 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2581 relocation = symbol->value;
2583 /* Only do this for a final link. */
2584 if (output_bfd == (bfd *) NULL)
2586 relocation += symbol->section->output_section->vma;
2587 relocation += symbol->section->output_offset;
2590 relocation += reloc_entry->addend;
2591 switch (reloc_entry->howto->type)
2593 case R_NDS32_LO12S3:
2597 case R_NDS32_LO12S2:
2601 case R_NDS32_LO12S1:
2605 case R_NDS32_LO12S0:
2611 inplace_address = (bfd_byte *) data + reloc_entry->address;
2614 x = ((x & ~reloc_entry->howto->dst_mask) | \
2615 (((x & reloc_entry->howto->src_mask) + relocation) & \
2616 reloc_entry->howto->dst_mask))
2618 switch (reloc_entry->howto->size)
2622 short x = bfd_getb16 (inplace_address);
2625 bfd_putb16 ((bfd_vma) x, inplace_address);
2630 unsigned long x = bfd_getb32 (inplace_address);
2633 bfd_putb32 ((bfd_vma) x, inplace_address);
2640 if (output_bfd != (bfd *) NULL)
2641 reloc_entry->address += input_section->output_offset;
2646 /* Handle the R_NDS32_SDA15 reloc.
2647 This reloc is used to compute the address of objects in the small data area
2648 and to perform loads and stores from that area.
2649 The lower 15 bits are sign extended and added to the register specified
2650 in the instruction, which is assumed to point to _SDA_BASE_.
2652 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2653 the access size, this must be taken care of. */
2655 static bfd_reloc_status_type
2656 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2657 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2658 asection *input_section, bfd *output_bfd,
2659 char **error_message ATTRIBUTE_UNUSED)
2661 /* This part is from bfd_elf_generic_reloc. */
2662 if (output_bfd != (bfd *) NULL
2663 && (symbol->flags & BSF_SECTION_SYM) == 0
2664 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2666 reloc_entry->address += input_section->output_offset;
2667 return bfd_reloc_ok;
2670 if (output_bfd != NULL)
2672 /* FIXME: See bfd_perform_relocation. Is this right? */
2673 return bfd_reloc_continue;
2676 /* FIXME: not sure what to do here yet. But then again, the linker
2677 may never call us. */
2681 /* nds32_elf_ignore_reloc is the special function for
2682 relocation types which don't need to be relocated
2683 like relaxation relocation types.
2684 This function simply return bfd_reloc_ok when it is
2687 static bfd_reloc_status_type
2688 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2689 asymbol *symbol ATTRIBUTE_UNUSED,
2690 void *data ATTRIBUTE_UNUSED, asection *input_section,
2691 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2693 if (output_bfd != NULL)
2694 reloc_entry->address += input_section->output_offset;
2696 return bfd_reloc_ok;
2700 /* Map BFD reloc types to NDS32 ELF reloc types. */
2702 struct nds32_reloc_map_entry
2704 bfd_reloc_code_real_type bfd_reloc_val;
2705 unsigned char elf_reloc_val;
2708 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
2710 {BFD_RELOC_NONE, R_NDS32_NONE},
2711 {BFD_RELOC_16, R_NDS32_16_RELA},
2712 {BFD_RELOC_32, R_NDS32_32_RELA},
2713 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
2714 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
2715 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
2716 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
2717 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
2718 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
2719 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
2720 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
2721 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
2722 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
2723 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
2724 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
2725 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
2726 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
2727 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
2728 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
2729 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
2730 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
2731 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
2732 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
2734 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
2735 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
2736 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
2737 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
2738 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
2739 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
2740 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
2741 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
2742 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
2743 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
2744 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
2745 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
2746 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
2747 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
2748 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
2749 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
2750 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
2751 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
2752 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
2753 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
2754 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
2755 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
2756 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
2757 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
2758 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
2759 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
2760 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
2761 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
2762 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
2763 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
2764 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
2765 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
2766 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
2767 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
2768 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
2769 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
2770 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
2771 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
2772 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
2773 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
2774 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
2775 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
2776 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
2777 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
2778 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
2779 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
2780 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
2781 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
2782 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
2783 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
2784 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
2785 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
2786 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
2787 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
2788 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
2789 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
2790 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
2791 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
2792 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
2793 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
2794 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
2795 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
2796 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
2797 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
2798 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
2799 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
2800 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
2801 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
2802 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
2803 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
2804 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
2805 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
2806 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
2807 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
2808 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
2810 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
2811 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
2812 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
2813 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
2814 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
2815 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
2816 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
2817 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
2818 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
2819 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
2820 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
2821 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
2822 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
2823 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
2824 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
2825 {BFD_RELOC_NDS32_TLS_TPOFF, R_NDS32_TLS_TPOFF},
2826 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
2827 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
2828 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
2829 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
2834 static reloc_howto_type *
2835 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2840 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
2841 if (nds32_elf_howto_table[i].name != NULL
2842 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
2843 return &nds32_elf_howto_table[i];
2845 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
2846 if (nds32_elf_relax_howto_table[i].name != NULL
2847 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
2848 return &nds32_elf_relax_howto_table[i];
2853 static reloc_howto_type *
2854 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
2856 if (code < R_NDS32_RELAX_ENTRY)
2858 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
2859 return &nds32_elf_howto_table[code];
2863 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
2864 < ARRAY_SIZE (nds32_elf_relax_howto_table));
2865 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
2869 static reloc_howto_type *
2870 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2871 bfd_reloc_code_real_type code)
2875 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
2877 if (nds32_reloc_map[i].bfd_reloc_val == code)
2878 return bfd_elf32_bfd_reloc_type_table_lookup
2879 (nds32_reloc_map[i].elf_reloc_val);
2885 /* Set the howto pointer for an NDS32 ELF reloc. */
2888 nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2889 Elf_Internal_Rela *dst)
2891 enum elf_nds32_reloc_type r_type;
2893 r_type = ELF32_R_TYPE (dst->r_info);
2894 if (r_type > R_NDS32_GNU_VTENTRY)
2896 /* xgettext:c-format */
2897 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2901 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
2905 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2906 Elf_Internal_Rela *dst)
2908 BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
2909 || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
2910 && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
2911 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
2914 /* Support for core dump NOTE sections.
2915 Reference to include/linux/elfcore.h in Linux. */
2918 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2923 switch (note->descsz)
2926 /* Linux/NDS32 32-bit, ABI1 */
2929 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2932 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
2940 /* Linux/NDS32 32-bit */
2943 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2946 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
2957 /* Make a ".reg" section. */
2958 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2959 size, note->descpos + offset);
2963 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2965 switch (note->descsz)
2970 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
2971 elf_tdata (abfd)->core->program =
2972 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
2973 elf_tdata (abfd)->core->command =
2974 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2981 /* Note that for some reason, a spurious space is tacked
2982 onto the end of the args in some (at least one anyway)
2983 implementations, so strip it off if it exists. */
2985 char *command = elf_tdata (abfd)->core->command;
2986 int n = strlen (command);
2988 if (0 < n && command[n - 1] == ' ')
2989 command[n - 1] = '\0';
2995 /* Hook called by the linker routine which adds symbols from an object
2996 file. We must handle the special NDS32 section numbers here.
2997 We also keep watching for whether we need to create the sdata special
3001 nds32_elf_add_symbol_hook (bfd *abfd,
3002 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3003 Elf_Internal_Sym *sym,
3004 const char **namep ATTRIBUTE_UNUSED,
3005 flagword *flagsp ATTRIBUTE_UNUSED,
3006 asection **secp, bfd_vma *valp)
3008 switch (sym->st_shndx)
3011 /* Common symbols less than the GP size are automatically
3012 treated as SHN_MIPS_SCOMMON symbols. */
3013 if (sym->st_size > elf_gp_size (abfd)
3014 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3017 /* st_value is the alignemnt constraint.
3018 That might be its actual size if it is an array or structure. */
3019 switch (sym->st_value)
3022 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3025 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3028 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3031 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3037 (*secp)->flags |= SEC_IS_COMMON;
3038 *valp = sym->st_size;
3046 /* This function can figure out the best location for a base register to access
3047 data relative to this base register
3049 sda_d0: size of first DOUBLE WORD data section
3050 sda_w0: size of first WORD data section
3051 sda_h0: size of first HALF WORD data section
3052 sda_b : size of BYTE data section
3053 sda_hi: size of second HALF WORD data section
3054 sda_w1: size of second WORD data section
3055 sda_d1: size of second DOUBLE WORD data section
3057 offset (always positive) from the beginning of sda_d0 if OK
3058 a negative error value if fail
3060 these 7 sections have to be located back to back if exist
3061 a pass in 0 value for non-existing section */
3063 /* Due to the interpretation of simm15 field of load/store depending on
3064 data accessing size, the organization of base register relative data shall
3065 like the following figure
3066 -------------------------------------------
3067 | DOUBLE WORD sized data (range +/- 128K)
3068 -------------------------------------------
3069 | WORD sized data (range +/- 64K)
3070 -------------------------------------------
3071 | HALF WORD sized data (range +/- 32K)
3072 -------------------------------------------
3073 | BYTE sized data (range +/- 16K)
3074 -------------------------------------------
3075 | HALF WORD sized data (range +/- 32K)
3076 -------------------------------------------
3077 | WORD sized data (range +/- 64K)
3078 -------------------------------------------
3079 | DOUBLE WORD sized data (range +/- 128K)
3080 -------------------------------------------
3081 Its base register shall be set to access these data freely. */
3083 /* We have to figure out the SDA_BASE value, so that we can adjust the
3084 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3085 BFD. If we can't find it, we're stuck. We cache it in the ELF
3086 target data. We don't need to adjust the symbol value for an
3087 external symbol if we are producing relocatable output. */
3089 static asection *sda_rela_sec = NULL;
3091 #define SDA_SECTION_NUM 10
3093 static bfd_reloc_status_type
3094 nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
3095 bfd_vma *psb, bfd_boolean add_symbol)
3098 struct elf_nds32_link_hash_table *table;
3099 struct bfd_link_hash_entry *h, *h2;
3100 long unsigned int total = 0;
3102 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3103 if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
3105 asection *first = NULL, *final = NULL, *temp;
3107 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3108 4 byte-aligned. Therefore, it has to set the first section ".data"
3110 static const char sec_name[SDA_SECTION_NUM][10] =
3112 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3113 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3117 if (output_bfd->sections == NULL)
3119 *psb = elf_gp (output_bfd);
3120 return bfd_reloc_ok;
3123 /* Get the first and final section. */
3124 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3126 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3127 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3129 if (temp && (temp->size != 0 || temp->rawsize != 0))
3132 /* Summarize the sections in order to check if joining .bss. */
3133 if (temp && temp->size != 0)
3134 total += temp->size;
3135 else if (temp && temp->rawsize != 0)
3136 total += temp->rawsize;
3141 /* Check .bss size. */
3142 temp = bfd_get_section_by_name (output_bfd, ".bss");
3145 if (temp->size != 0)
3146 total += temp->size;
3147 else if (temp->rawsize != 0)
3148 total += temp->rawsize;
3150 if (total < 0x80000)
3152 if (!first && (temp->size != 0 || temp->rawsize != 0))
3154 if ((temp->size != 0 || temp->rawsize != 0))
3161 /* The middle of data region. */
3162 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3164 /* Find the section sda_base located. */
3166 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3168 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3169 if (final && (final->size != 0 || final->rawsize != 0)
3170 && sda_base >= final->vma)
3181 /* There is not any data section in output bfd, and set _SDA_BASE_ in
3182 first output section. */
3183 first = output_bfd->sections;
3184 while (first && first->size == 0 && first->rawsize == 0)
3185 first = first->next;
3188 *psb = elf_gp (output_bfd);
3189 return bfd_reloc_ok;
3191 sda_base = first->vma + first->rawsize;
3194 sda_base -= first->vma;
3195 sda_base = sda_base & (~7);
3197 if (!_bfd_generic_link_add_one_symbol
3198 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3199 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3200 get_elf_backend_data (output_bfd)->collect, &h))
3203 sda_rela_sec = first;
3205 table = nds32_elf_hash_table (info);
3206 relax_fp_as_gp = table->relax_fp_as_gp;
3209 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
3210 FALSE, FALSE, FALSE);
3211 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3212 And set FP equal to SDA_BASE to do relaxation for
3213 la $fp, _FP_BASE_. */
3214 if (!_bfd_generic_link_add_one_symbol
3215 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3216 first, (bfd_vma) sda_base, (const char *) NULL,
3217 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3227 elf_gp (output_bfd) = (h->u.def.value
3228 + h->u.def.section->output_section->vma
3229 + h->u.def.section->output_offset);
3233 _bfd_error_handler (_("error: Can't find symbol: _SDA_BASE_."));
3234 return bfd_reloc_dangerous;
3238 *psb = h->u.def.value + h->u.def.section->output_section->vma
3239 + h->u.def.section->output_offset;
3240 return bfd_reloc_ok;
3244 /* Return size of a PLT entry. */
3245 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3248 /* Create an entry in an nds32 ELF linker hash table. */
3250 static struct bfd_hash_entry *
3251 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3252 struct bfd_hash_table *table,
3255 struct elf_nds32_link_hash_entry *ret;
3257 ret = (struct elf_nds32_link_hash_entry *) entry;
3259 /* Allocate the structure if it has not already been allocated by a
3262 ret = (struct elf_nds32_link_hash_entry *)
3263 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3266 return (struct bfd_hash_entry *) ret;
3268 /* Call the allocation method of the superclass. */
3269 ret = (struct elf_nds32_link_hash_entry *)
3270 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3274 struct elf_nds32_link_hash_entry *eh;
3276 eh = (struct elf_nds32_link_hash_entry *) ret;
3277 eh->dyn_relocs = NULL;
3278 eh->tls_type = GOT_UNKNOWN;
3281 return (struct bfd_hash_entry *) ret;
3284 /* Create an nds32 ELF linker hash table. */
3286 static struct bfd_link_hash_table *
3287 nds32_elf_link_hash_table_create (bfd *abfd)
3289 struct elf_nds32_link_hash_table *ret;
3291 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3293 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3298 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3299 nds32_elf_link_hash_newfunc,
3300 sizeof (struct elf_nds32_link_hash_entry),
3307 return &ret->root.root;
3310 /* Create dynamic sections when linking against a dynamic object. */
3313 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3315 struct elf_nds32_link_hash_table *htab;
3316 flagword flags, pltflags;
3317 register asection *s;
3318 const struct elf_backend_data *bed;
3319 int ptralign = 2; /* 32-bit */
3321 bed = get_elf_backend_data (abfd);
3323 htab = nds32_elf_hash_table (info);
3325 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3326 .rel[a].bss sections. */
3328 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3329 | SEC_LINKER_CREATED);
3332 pltflags |= SEC_CODE;
3333 if (bed->plt_not_loaded)
3334 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3335 if (bed->plt_readonly)
3336 pltflags |= SEC_READONLY;
3338 s = bfd_make_section (abfd, ".plt");
3339 htab->root.splt = s;
3341 || !bfd_set_section_flags (abfd, s, pltflags)
3342 || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3345 if (bed->want_plt_sym)
3347 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3349 struct bfd_link_hash_entry *bh = NULL;
3350 struct elf_link_hash_entry *h;
3352 if (!(_bfd_generic_link_add_one_symbol
3353 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3354 (bfd_vma) 0, (const char *) NULL, FALSE,
3355 get_elf_backend_data (abfd)->collect, &bh)))
3358 h = (struct elf_link_hash_entry *) bh;
3360 h->type = STT_OBJECT;
3362 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3366 s = bfd_make_section (abfd,
3367 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3368 htab->root.srelplt = s;
3370 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3371 || !bfd_set_section_alignment (abfd, s, ptralign))
3374 if (htab->root.sgot == NULL && !_bfd_elf_create_got_section (abfd, info))
3378 const char *secname;
3383 for (sec = abfd->sections; sec; sec = sec->next)
3385 secflags = bfd_get_section_flags (abfd, sec);
3386 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3387 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3389 secname = bfd_get_section_name (abfd, sec);
3390 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3391 strcpy (relname, ".rela");
3392 strcat (relname, secname);
3393 if (bfd_get_section_by_name (abfd, secname))
3395 s = bfd_make_section (abfd, relname);
3397 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3398 || !bfd_set_section_alignment (abfd, s, ptralign))
3403 if (bed->want_dynbss)
3405 /* The .dynbss section is a place to put symbols which are defined
3406 by dynamic objects, are referenced by regular objects, and are
3407 not functions. We must allocate space for them in the process
3408 image and use a R_*_COPY reloc to tell the dynamic linker to
3409 initialize them at run time. The linker script puts the .dynbss
3410 section into the .bss section of the final image. */
3411 s = bfd_make_section (abfd, ".dynbss");
3414 || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3416 /* The .rel[a].bss section holds copy relocs. This section is not
3417 normally needed. We need to create it here, though, so that the
3418 linker will map it to an output section. We can't just create it
3419 only if we need it, because we will not know whether we need it
3420 until we have seen all the input files, and the first time the
3421 main linker code calls BFD after examining all the input files
3422 (size_dynamic_sections) the input sections have already been
3423 mapped to the output sections. If the section turns out not to
3424 be needed, we can discard it later. We will never need this
3425 section when generating a shared object, since they do not use
3427 if (!bfd_link_pic (info))
3429 s = bfd_make_section (abfd, (bed->default_use_rela_p
3430 ? ".rela.bss" : ".rel.bss"));
3433 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3434 || !bfd_set_section_alignment (abfd, s, ptralign))
3442 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3444 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3445 struct elf_link_hash_entry *dir,
3446 struct elf_link_hash_entry *ind)
3448 struct elf_nds32_link_hash_entry *edir, *eind;
3450 edir = (struct elf_nds32_link_hash_entry *) dir;
3451 eind = (struct elf_nds32_link_hash_entry *) ind;
3453 if (eind->dyn_relocs != NULL)
3455 if (edir->dyn_relocs != NULL)
3457 struct elf_dyn_relocs **pp;
3458 struct elf_dyn_relocs *p;
3460 if (ind->root.type == bfd_link_hash_indirect)
3463 /* Add reloc counts against the weak sym to the strong sym
3464 list. Merge any entries against the same section. */
3465 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3467 struct elf_dyn_relocs *q;
3469 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3470 if (q->sec == p->sec)
3472 q->pc_count += p->pc_count;
3473 q->count += p->count;
3480 *pp = edir->dyn_relocs;
3483 edir->dyn_relocs = eind->dyn_relocs;
3484 eind->dyn_relocs = NULL;
3487 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3490 /* Find dynamic relocs for H that apply to read-only sections. */
3493 readonly_dynrelocs (struct elf_link_hash_entry *h)
3495 struct elf_dyn_relocs *p;
3497 for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3499 asection *s = p->sec->output_section;
3501 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3507 /* Adjust a symbol defined by a dynamic object and referenced by a
3508 regular object. The current definition is in some section of the
3509 dynamic object, but we're not including those sections. We have to
3510 change the definition to something the rest of the link can
3514 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3515 struct elf_link_hash_entry *h)
3517 struct elf_nds32_link_hash_table *htab;
3520 unsigned int power_of_two;
3522 dynobj = elf_hash_table (info)->dynobj;
3524 /* Make sure we know what is going on here. */
3525 BFD_ASSERT (dynobj != NULL
3528 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3531 /* If this is a function, put it in the procedure linkage table. We
3532 will fill in the contents of the procedure linkage table later,
3533 when we know the address of the .got section. */
3534 if (h->type == STT_FUNC || h->needs_plt)
3536 if (!bfd_link_pic (info)
3539 && h->root.type != bfd_link_hash_undefweak
3540 && h->root.type != bfd_link_hash_undefined)
3542 /* This case can occur if we saw a PLT reloc in an input
3543 file, but the symbol was never referred to by a dynamic
3544 object. In such a case, we don't actually need to build
3545 a procedure linkage table, and we can just do a PCREL
3547 h->plt.offset = (bfd_vma) - 1;
3554 h->plt.offset = (bfd_vma) - 1;
3556 /* If this is a weak symbol, and there is a real definition, the
3557 processor independent code will have arranged for us to see the
3558 real definition first, and we can just use the same value. */
3559 if (h->is_weakalias)
3561 struct elf_link_hash_entry *def = weakdef (h);
3562 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3563 h->root.u.def.section = def->root.u.def.section;
3564 h->root.u.def.value = def->root.u.def.value;
3568 /* This is a reference to a symbol defined by a dynamic object which
3569 is not a function. */
3571 /* If we are creating a shared library, we must presume that the
3572 only references to the symbol are via the global offset table.
3573 For such cases we need not do anything here; the relocations will
3574 be handled correctly by relocate_section. */
3575 if (bfd_link_pic (info))
3578 /* If there are no references to this symbol that do not use the
3579 GOT, we don't need to generate a copy reloc. */
3580 if (!h->non_got_ref)
3583 /* If -z nocopyreloc was given, we won't generate them either. */
3584 if (0 && info->nocopyreloc)
3590 /* If we don't find any dynamic relocs in read-only sections, then
3591 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3592 if (0 && !readonly_dynrelocs (h))
3598 /* We must allocate the symbol in our .dynbss section, which will
3599 become part of the .bss section of the executable. There will be
3600 an entry for this symbol in the .dynsym section. The dynamic
3601 object will contain position independent code, so all references
3602 from the dynamic object to this symbol will go through the global
3603 offset table. The dynamic linker will use the .dynsym entry to
3604 determine the address it must put in the global offset table, so
3605 both the dynamic object and the regular object will refer to the
3606 same memory location for the variable. */
3608 htab = nds32_elf_hash_table (info);
3610 BFD_ASSERT (s != NULL);
3612 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3613 to copy the initial value out of the dynamic object and into the
3614 runtime process image. We need to remember the offset into the
3615 .rela.bss section we are going to use. */
3616 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3620 srel = htab->srelbss;
3621 BFD_ASSERT (srel != NULL);
3622 srel->size += sizeof (Elf32_External_Rela);
3626 /* We need to figure out the alignment required for this symbol. I
3627 have no idea how ELF linkers handle this. */
3628 power_of_two = bfd_log2 (h->size);
3629 if (power_of_two > 3)
3632 /* Apply the required alignment. */
3633 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3634 if (power_of_two > bfd_get_section_alignment (dynobj, s))
3636 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
3640 /* Define the symbol as being at this point in the section. */
3641 h->root.u.def.section = s;
3642 h->root.u.def.value = s->size;
3644 /* Increment the section size to make room for the symbol. */
3650 /* Allocate space in .plt, .got and associated reloc sections for
3654 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3656 struct bfd_link_info *info;
3657 struct elf_nds32_link_hash_table *htab;
3658 struct elf_nds32_link_hash_entry *eh;
3659 struct elf_dyn_relocs *p;
3661 if (h->root.type == bfd_link_hash_indirect)
3664 if (h->root.type == bfd_link_hash_warning)
3665 /* When warning symbols are created, they **replace** the "real"
3666 entry in the hash table, thus we never get to see the real
3667 symbol in a hash traversal. So look at it now. */
3668 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3670 info = (struct bfd_link_info *) inf;
3671 htab = nds32_elf_hash_table (info);
3673 eh = (struct elf_nds32_link_hash_entry *) h;
3675 if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
3677 /* Make sure this symbol is output as a dynamic symbol.
3678 Undefined weak syms won't yet be marked as dynamic. */
3679 if (h->dynindx == -1 && !h->forced_local)
3681 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3685 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
3687 asection *s = htab->root.splt;
3689 /* If this is the first .plt entry, make room for the special
3692 s->size += PLT_ENTRY_SIZE;
3694 h->plt.offset = s->size;
3696 /* If this symbol is not defined in a regular file, and we are
3697 not generating a shared library, then set the symbol to this
3698 location in the .plt. This is required to make function
3699 pointers compare as equal between the normal executable and
3700 the shared library. */
3701 if (!bfd_link_pic (info) && !h->def_regular)
3703 h->root.u.def.section = s;
3704 h->root.u.def.value = h->plt.offset;
3707 /* Make room for this entry. */
3708 s->size += PLT_ENTRY_SIZE;
3710 /* We also need to make an entry in the .got.plt section, which
3711 will be placed in the .got section by the linker script. */
3712 htab->root.sgotplt->size += 4;
3714 /* We also need to make an entry in the .rel.plt section. */
3715 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3719 h->plt.offset = (bfd_vma) - 1;
3725 h->plt.offset = (bfd_vma) - 1;
3729 if (h->got.refcount > 0)
3733 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
3735 /* Make sure this symbol is output as a dynamic symbol.
3736 Undefined weak syms won't yet be marked as dynamic. */
3737 if (h->dynindx == -1 && !h->forced_local)
3739 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3743 s = htab->root.sgot;
3744 h->got.offset = s->size;
3746 if (tls_type == GOT_UNKNOWN)
3748 else if (tls_type == GOT_NORMAL
3749 || tls_type == GOT_TLS_IE)
3750 /* Need a GOT slot. */
3753 dyn = htab->root.dynamic_sections_created;
3754 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
3755 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3758 h->got.offset = (bfd_vma) - 1;
3760 if (eh->dyn_relocs == NULL)
3763 /* In the shared -Bsymbolic case, discard space allocated for
3764 dynamic pc-relative relocs against symbols which turn out to be
3765 defined in regular objects. For the normal shared case, discard
3766 space for pc-relative relocs that have become local due to symbol
3767 visibility changes. */
3769 if (bfd_link_pic (info))
3771 if (h->def_regular && (h->forced_local || info->symbolic))
3773 struct elf_dyn_relocs **pp;
3775 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
3777 p->count -= p->pc_count;
3788 /* For the non-shared case, discard space for relocs against
3789 symbols which turn out to need copy relocs or are not dynamic. */
3794 || (htab->root.dynamic_sections_created
3795 && (h->root.type == bfd_link_hash_undefweak
3796 || h->root.type == bfd_link_hash_undefined))))
3798 /* Make sure this symbol is output as a dynamic symbol.
3799 Undefined weak syms won't yet be marked as dynamic. */
3800 if (h->dynindx == -1 && !h->forced_local)
3802 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3806 /* If that succeeded, we know we'll be keeping all the
3808 if (h->dynindx != -1)
3812 eh->dyn_relocs = NULL;
3817 /* Finally, allocate space. */
3818 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3820 asection *sreloc = elf_section_data (p->sec)->sreloc;
3821 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3827 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
3828 read-only sections. */
3831 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
3835 if (h->root.type == bfd_link_hash_indirect)
3838 sec = readonly_dynrelocs (h);
3841 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
3843 info->flags |= DF_TEXTREL;
3844 info->callbacks->minfo
3845 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
3846 sec->owner, h->root.root.string, sec);
3848 /* Not an error, just cut short the traversal. */
3854 /* Set the sizes of the dynamic sections. */
3857 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3858 struct bfd_link_info *info)
3860 struct elf_nds32_link_hash_table *htab;
3866 htab = nds32_elf_hash_table (info);
3867 dynobj = htab->root.dynobj;
3868 BFD_ASSERT (dynobj != NULL);
3870 if (htab->root.dynamic_sections_created)
3872 /* Set the contents of the .interp section to the interpreter. */
3873 if (bfd_link_executable (info) && !info->nointerp)
3875 s = bfd_get_section_by_name (dynobj, ".interp");
3876 BFD_ASSERT (s != NULL);
3877 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3878 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3882 /* Set up .got offsets for local syms, and space for local dynamic
3884 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3886 bfd_signed_vma *local_got;
3887 bfd_signed_vma *end_local_got;
3888 bfd_size_type locsymcount;
3889 Elf_Internal_Shdr *symtab_hdr;
3892 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3895 for (s = ibfd->sections; s != NULL; s = s->next)
3897 struct elf_dyn_relocs *p;
3899 for (p = ((struct elf_dyn_relocs *)
3900 elf_section_data (s)->local_dynrel);
3901 p != NULL; p = p->next)
3903 if (!bfd_is_abs_section (p->sec)
3904 && bfd_is_abs_section (p->sec->output_section))
3906 /* Input section has been discarded, either because
3907 it is a copy of a linkonce section or due to
3908 linker script /DISCARD/, so we'll be discarding
3911 else if (p->count != 0)
3913 srel = elf_section_data (p->sec)->sreloc;
3914 srel->size += p->count * sizeof (Elf32_External_Rela);
3915 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3916 info->flags |= DF_TEXTREL;
3921 local_got = elf_local_got_refcounts (ibfd);
3925 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3926 locsymcount = symtab_hdr->sh_info;
3927 end_local_got = local_got + locsymcount;
3928 s = htab->root.sgot;
3929 srel = htab->root.srelgot;
3930 for (; local_got < end_local_got; ++local_got)
3934 *local_got = s->size;
3936 if (bfd_link_pic (info))
3937 srel->size += sizeof (Elf32_External_Rela);
3940 *local_got = (bfd_vma) - 1;
3944 /* Allocate global sym .plt and .got entries, and space for global
3945 sym dynamic relocs. */
3946 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
3948 /* We now have determined the sizes of the various dynamic sections.
3949 Allocate memory for them. */
3951 for (s = dynobj->sections; s != NULL; s = s->next)
3953 if ((s->flags & SEC_LINKER_CREATED) == 0)
3956 if (s == htab->root.splt)
3958 /* Strip this section if we don't need it; see the
3961 else if (s == htab->root.sgot)
3963 got_size += s->size;
3965 else if (s == htab->root.sgotplt)
3967 got_size += s->size;
3969 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3971 if (s->size != 0 && s != htab->root.srelplt)
3974 /* We use the reloc_count field as a counter if we need
3975 to copy relocs into the output file. */
3980 /* It's not one of our sections, so don't allocate space. */
3986 /* If we don't need this section, strip it from the
3987 output file. This is mostly to handle .rela.bss and
3988 .rela.plt. We must create both sections in
3989 create_dynamic_sections, because they must be created
3990 before the linker maps input sections to output
3991 sections. The linker does that before
3992 adjust_dynamic_symbol is called, and it is that
3993 function which decides whether anything needs to go
3994 into these sections. */
3995 s->flags |= SEC_EXCLUDE;
3999 /* Allocate memory for the section contents. We use bfd_zalloc
4000 here in case unused entries are not reclaimed before the
4001 section's contents are written out. This should not happen,
4002 but this way if it does, we get a R_NDS32_NONE reloc instead
4004 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4005 if (s->contents == NULL)
4010 if (htab->root.dynamic_sections_created)
4012 /* Add some entries to the .dynamic section. We fill in the
4013 values later, in nds32_elf_finish_dynamic_sections, but we
4014 must add the entries now so that we get the correct size for
4015 the .dynamic section. The DT_DEBUG entry is filled in by the
4016 dynamic linker and used by the debugger. */
4017 #define add_dynamic_entry(TAG, VAL) \
4018 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4020 if (!bfd_link_pic (info))
4022 if (!add_dynamic_entry (DT_DEBUG, 0))
4026 if (htab->root.splt->size != 0)
4028 if (!add_dynamic_entry (DT_PLTGOT, 0)
4029 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4030 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4031 || !add_dynamic_entry (DT_JMPREL, 0))
4037 if (!add_dynamic_entry (DT_RELA, 0)
4038 || !add_dynamic_entry (DT_RELASZ, 0)
4039 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4042 /* If any dynamic relocs apply to a read-only section,
4043 then we need a DT_TEXTREL entry. */
4044 if ((info->flags & DF_TEXTREL) == 0)
4045 elf_link_hash_traverse (&htab->root, maybe_set_textrel,
4048 if ((info->flags & DF_TEXTREL) != 0)
4050 if (!add_dynamic_entry (DT_TEXTREL, 0))
4055 #undef add_dynamic_entry
4060 static bfd_reloc_status_type
4061 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4062 bfd_vma relocation, bfd_byte *location)
4066 bfd_reloc_status_type flag;
4067 unsigned int rightshift = howto->rightshift;
4068 unsigned int bitpos = howto->bitpos;
4070 /* If the size is negative, negate RELOCATION. This isn't very
4072 if (howto->size < 0)
4073 relocation = -relocation;
4075 /* Get the value we are going to relocate. */
4076 size = bfd_get_reloc_size (howto);
4083 return bfd_reloc_ok;
4085 x = bfd_getb16 (location);
4088 x = bfd_getb32 (location);
4092 /* Check for overflow. FIXME: We may drop bits during the addition
4093 which we don't check for. We must either check at every single
4094 operation, which would be tedious, or we must do the computations
4095 in a type larger than bfd_vma, which would be inefficient. */
4096 flag = bfd_reloc_ok;
4097 if (howto->complain_on_overflow != complain_overflow_dont)
4099 bfd_vma addrmask, fieldmask, signmask, ss;
4102 /* Get the values to be added together. For signed and unsigned
4103 relocations, we assume that all values should be truncated to
4104 the size of an address. For bitfields, all the bits matter.
4105 See also bfd_check_overflow. */
4106 fieldmask = N_ONES (howto->bitsize);
4107 signmask = ~fieldmask;
4108 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4109 a = (relocation & addrmask) >> rightshift;
4110 b = (x & howto->src_mask & addrmask) >> bitpos;
4112 switch (howto->complain_on_overflow)
4114 case complain_overflow_signed:
4115 /* If any sign bits are set, all sign bits must be set.
4116 That is, A must be a valid negative address after
4118 signmask = ~(fieldmask >> 1);
4121 case complain_overflow_bitfield:
4122 /* Much like the signed check, but for a field one bit
4123 wider. We allow a bitfield to represent numbers in the
4124 range -2**n to 2**n-1, where n is the number of bits in the
4125 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4126 can't overflow, which is exactly what we want. */
4128 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4129 flag = bfd_reloc_overflow;
4131 /* We only need this next bit of code if the sign bit of B
4132 is below the sign bit of A. This would only happen if
4133 SRC_MASK had fewer bits than BITSIZE. Note that if
4134 SRC_MASK has more bits than BITSIZE, we can get into
4135 trouble; we would need to verify that B is in range, as
4136 we do for A above. */
4137 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4140 /* Set all the bits above the sign bit. */
4143 /* Now we can do the addition. */
4146 /* See if the result has the correct sign. Bits above the
4147 sign bit are junk now; ignore them. If the sum is
4148 positive, make sure we did not have all negative inputs;
4149 if the sum is negative, make sure we did not have all
4150 positive inputs. The test below looks only at the sign
4151 bits, and it really just
4152 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4154 We mask with addrmask here to explicitly allow an address
4155 wrap-around. The Linux kernel relies on it, and it is
4156 the only way to write assembler code which can run when
4157 loaded at a location 0x80000000 away from the location at
4158 which it is linked. */
4159 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4160 flag = bfd_reloc_overflow;
4164 case complain_overflow_unsigned:
4165 /* Checking for an unsigned overflow is relatively easy:
4166 trim the addresses and add, and trim the result as well.
4167 Overflow is normally indicated when the result does not
4168 fit in the field. However, we also need to consider the
4169 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4170 input is 0x80000000, and bfd_vma is only 32 bits; then we
4171 will get sum == 0, but there is an overflow, since the
4172 inputs did not fit in the field. Instead of doing a
4173 separate test, we can check for this by or-ing in the
4174 operands when testing for the sum overflowing its final
4176 sum = (a + b) & addrmask;
4177 if ((a | b | sum) & signmask)
4178 flag = bfd_reloc_overflow;
4186 /* Put RELOCATION in the right bits. */
4187 relocation >>= (bfd_vma) rightshift;
4188 relocation <<= (bfd_vma) bitpos;
4190 /* Add RELOCATION to the right bits of X. */
4192 Because the relaxation may generate duplicate relocation at one address,
4193 an addition to immediate in the instruction may cause the relocation added
4195 This bug should be fixed in assembler, but a check is also needed here. */
4196 if (howto->partial_inplace)
4197 x = ((x & ~howto->dst_mask)
4198 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4200 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4203 /* Put the relocated value back in the object file. */
4213 bfd_putb16 (x, location);
4216 bfd_putb32 (x, location);
4223 static bfd_reloc_status_type
4224 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4225 asection *input_section, bfd_byte *contents,
4226 bfd_vma address, bfd_vma value, bfd_vma addend)
4230 /* Sanity check the address. */
4231 if (address > bfd_get_section_limit (input_bfd, input_section))
4232 return bfd_reloc_outofrange;
4234 /* This function assumes that we are dealing with a basic relocation
4235 against a symbol. We want to compute the value of the symbol to
4236 relocate to. This is just VALUE, the value of the symbol, plus
4237 ADDEND, any addend associated with the reloc. */
4238 relocation = value + addend;
4240 /* If the relocation is PC relative, we want to set RELOCATION to
4241 the distance between the symbol (currently in RELOCATION) and the
4242 location we are relocating. Some targets (e.g., i386-aout)
4243 arrange for the contents of the section to be the negative of the
4244 offset of the location within the section; for such targets
4245 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4246 simply leave the contents of the section as zero; for such
4247 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4248 need to subtract out the offset of the location within the
4249 section (which is just ADDRESS). */
4250 if (howto->pc_relative)
4252 relocation -= (input_section->output_section->vma
4253 + input_section->output_offset);
4254 if (howto->pcrel_offset)
4255 relocation -= address;
4258 return nds32_relocate_contents (howto, input_bfd, relocation,
4259 contents + address);
4263 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4265 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4266 asection *input_sec,
4267 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4270 FILE *sym_ld_script = NULL;
4271 struct elf_nds32_link_hash_table *table;
4273 table = nds32_elf_hash_table (info);
4274 sym_ld_script = table->sym_ld_script;
4278 if (!h || !name || *name == '\0')
4281 if (input_sec->flags & SEC_EXCLUDE)
4284 if (!check_start_export_sym)
4286 fprintf (sym_ld_script, "SECTIONS\n{\n");
4287 check_start_export_sym = 1;
4290 if (h->root.type == bfd_link_hash_defined
4291 || h->root.type == bfd_link_hash_defweak)
4293 if (!h->root.u.def.section->output_section)
4296 if (bfd_is_const_section (input_sec))
4297 source = input_sec->name;
4299 source = input_sec->owner->filename;
4301 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4302 h->root.root.string,
4303 (long) (h->root.u.def.value
4304 + h->root.u.def.section->output_section->vma
4305 + h->root.u.def.section->output_offset), source);
4311 /* Relocate an NDS32/D ELF section.
4312 There is some attempt to make this function usable for many architectures,
4313 both for RELA and REL type relocs, if only to serve as a learning tool.
4315 The RELOCATE_SECTION function is called by the new ELF backend linker
4316 to handle the relocations for a section.
4318 The relocs are always passed as Rela structures; if the section
4319 actually uses Rel structures, the r_addend field will always be
4322 This function is responsible for adjust the section contents as
4323 necessary, and (if using Rela relocs and generating a
4324 relocatable output file) adjusting the reloc addend as
4327 This function does not have to worry about setting the reloc
4328 address or the reloc symbol index.
4330 LOCAL_SYMS is a pointer to the swapped in local symbols.
4332 LOCAL_SECTIONS is an array giving the section in the input file
4333 corresponding to the st_shndx field of each local symbol.
4335 The global hash table entry for the global symbols can be found
4336 via elf_sym_hashes (input_bfd).
4338 When generating relocatable output, this function must handle
4339 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4340 going to be the section symbol corresponding to the output
4341 section, which means that the addend must be adjusted
4345 dtpoff_base (struct bfd_link_info *info)
4347 /* If tls_sec is NULL, we should have signalled an error already. */
4348 if (elf_hash_table (info)->tls_sec == NULL)
4350 return elf_hash_table (info)->tls_sec->vma;
4354 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
4355 struct bfd_link_info * info,
4357 asection * input_section,
4358 bfd_byte * contents,
4359 Elf_Internal_Rela * relocs,
4360 Elf_Internal_Sym * local_syms,
4361 asection ** local_sections)
4363 Elf_Internal_Shdr *symtab_hdr;
4364 struct elf_link_hash_entry **sym_hashes;
4365 Elf_Internal_Rela *rel, *relend;
4366 bfd_boolean ret = TRUE; /* Assume success. */
4368 bfd_reloc_status_type r;
4369 const char *errmsg = NULL;
4371 struct elf_nds32_link_hash_table *htab;
4373 bfd_vma *local_got_offsets;
4374 asection *sgot, *splt, *sreloc;
4375 bfd_vma high_address;
4376 struct elf_nds32_link_hash_table *table;
4377 int eliminate_gc_relocs;
4378 bfd_vma fpbase_addr;
4380 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4381 sym_hashes = elf_sym_hashes (input_bfd);
4382 htab = nds32_elf_hash_table (info);
4383 high_address = bfd_get_section_limit (input_bfd, input_section);
4385 dynobj = htab->root.dynobj;
4386 local_got_offsets = elf_local_got_offsets (input_bfd);
4388 sgot = htab->root.sgot;
4389 splt = htab->root.splt;
4393 relend = relocs + input_section->reloc_count;
4395 table = nds32_elf_hash_table (info);
4396 eliminate_gc_relocs = table->eliminate_gc_relocs;
4397 /* By this time, we can adjust the value of _SDA_BASE_. */
4398 if ((!bfd_link_relocatable (info)))
4400 is_SDA_BASE_set = 1;
4401 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
4402 if (r != bfd_reloc_ok)
4406 if (is_ITB_BASE_set == 0)
4408 /* Set the _ITB_BASE_. */
4409 if (!nds32_elf_ex9_itb_base (info))
4411 _bfd_error_handler (_("%pB: error: Cannot set _ITB_BASE_"),
4413 bfd_set_error (bfd_error_bad_value);
4417 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
4418 if (!nds32_elf_ifc_reloc ())
4419 _bfd_error_handler (_("error: IFC relocation error."));
4421 /* Relocation for .ex9.itable. */
4422 if (table->target_optimize & NDS32_RELAX_EX9_ON
4423 || (table->ex9_import_file && table->update_ex9_table))
4424 nds32_elf_ex9_reloc_jmp (info);
4426 /* Use gp as fp to prevent truncated fit. Because in relaxation time
4427 the fp value is set as gp, and it has be reverted for instruction
4429 fpbase_addr = elf_gp (output_bfd);
4431 for (rel = relocs; rel < relend; rel++)
4433 enum elf_nds32_reloc_type r_type;
4434 reloc_howto_type *howto = NULL;
4435 unsigned long r_symndx;
4436 struct elf_link_hash_entry *h = NULL;
4437 Elf_Internal_Sym *sym = NULL;
4441 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
4442 ensure it's zero (we use REL relocs, not RELA). Therefore this
4443 should be assigning zero to `addend', but for clarity we use
4446 bfd_vma addend = rel->r_addend;
4447 bfd_vma offset = rel->r_offset;
4449 r_type = ELF32_R_TYPE (rel->r_info);
4450 if (r_type >= R_NDS32_max)
4452 /* xgettext:c-format */
4453 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4455 bfd_set_error (bfd_error_bad_value);
4460 if (r_type == R_NDS32_GNU_VTENTRY
4461 || r_type == R_NDS32_GNU_VTINHERIT
4462 || r_type == R_NDS32_NONE
4463 || r_type == R_NDS32_RELA_GNU_VTENTRY
4464 || r_type == R_NDS32_RELA_GNU_VTINHERIT
4465 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
4466 || r_type == R_NDS32_DATA
4467 || r_type == R_NDS32_TRAN
4468 || (r_type >= R_NDS32_LONGCALL4 && r_type <= R_NDS32_LONGJUMP7))
4471 /* If we enter the fp-as-gp region. Resolve the address
4473 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
4474 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4478 /* Distance to relocation of best fp-base is encoded in R_SYM. */
4479 dist = rel->r_addend >> 16;
4480 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
4481 local_syms, symtab_hdr);
4483 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
4484 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4486 fpbase_addr = elf_gp (output_bfd);
4489 if (((r_type >= R_NDS32_DWARF2_OP1_RELA
4490 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4491 || r_type >= R_NDS32_RELAX_ENTRY) && !bfd_link_relocatable (info))
4494 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
4495 r_symndx = ELF32_R_SYM (rel->r_info);
4497 /* This is a final link. */
4502 if (r_symndx < symtab_hdr->sh_info)
4505 sym = local_syms + r_symndx;
4506 sec = local_sections[r_symndx];
4508 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4509 addend = rel->r_addend;
4513 /* External symbol. */
4514 bfd_boolean warned, ignored, unresolved_reloc;
4515 int symndx = r_symndx - symtab_hdr->sh_info;
4517 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4518 r_symndx, symtab_hdr, sym_hashes, h, sec,
4519 relocation, unresolved_reloc, warned,
4522 /* la $fp, _FP_BASE_ is per-function (region).
4523 Handle it specially. */
4524 switch ((int) r_type)
4526 case R_NDS32_SDA19S0_RELA:
4527 case R_NDS32_SDA15S0_RELA:
4528 case R_NDS32_20_RELA:
4529 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
4532 relocation = fpbase_addr;
4539 if (bfd_link_relocatable (info))
4541 /* This is a relocatable link. We don't have to change
4542 anything, unless the reloc is against a section symbol,
4543 in which case we have to adjust according to where the
4544 section symbol winds up in the output section. */
4545 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4546 rel->r_addend += sec->output_offset + sym->st_value;
4551 /* Sanity check the address. */
4552 if (offset > high_address)
4554 r = bfd_reloc_outofrange;
4558 if ((r_type >= R_NDS32_DWARF2_OP1_RELA
4559 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4560 || r_type >= R_NDS32_RELAX_ENTRY)
4563 switch ((int) r_type)
4565 case R_NDS32_GOTOFF:
4566 /* Relocation is relative to the start of the global offset
4567 table (for ld24 rx, #uimm24), e.g. access at label+addend
4569 ld24 rx. #label@GOTOFF + addend
4571 case R_NDS32_GOTOFF_HI20:
4572 case R_NDS32_GOTOFF_LO12:
4573 case R_NDS32_GOTOFF_LO15:
4574 case R_NDS32_GOTOFF_LO19:
4575 BFD_ASSERT (sgot != NULL);
4577 relocation -= elf_gp (output_bfd);
4580 case R_NDS32_9_PLTREL:
4581 case R_NDS32_25_PLTREL:
4582 /* Relocation is to the entry for this symbol in the
4583 procedure linkage table. */
4585 /* The native assembler will generate a 25_PLTREL reloc
4586 for a local symbol if you assemble a call from one
4587 section to another when using -K pic. */
4591 if (h->forced_local)
4594 /* We didn't make a PLT entry for this symbol. This
4595 happens when statically linking PIC code, or when
4596 using -Bsymbolic. */
4597 if (h->plt.offset == (bfd_vma) - 1)
4600 relocation = (splt->output_section->vma
4601 + splt->output_offset + h->plt.offset);
4604 case R_NDS32_PLT_GOTREL_HI20:
4605 case R_NDS32_PLT_GOTREL_LO12:
4606 case R_NDS32_PLT_GOTREL_LO15:
4607 case R_NDS32_PLT_GOTREL_LO19:
4608 case R_NDS32_PLT_GOTREL_LO20:
4609 if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
4611 /* We didn't make a PLT entry for this symbol. This
4612 happens when statically linking PIC code, or when
4613 using -Bsymbolic. */
4614 relocation -= elf_gp (output_bfd);
4618 relocation = (splt->output_section->vma
4619 + splt->output_offset + h->plt.offset);
4621 relocation -= elf_gp (output_bfd);
4624 case R_NDS32_PLTREL_HI20:
4625 case R_NDS32_PLTREL_LO12:
4627 /* Relocation is to the entry for this symbol in the
4628 procedure linkage table. */
4630 /* The native assembler will generate a 25_PLTREL reloc
4631 for a local symbol if you assemble a call from one
4632 section to another when using -K pic. */
4636 if (h->forced_local)
4639 if (h->plt.offset == (bfd_vma) - 1)
4640 /* We didn't make a PLT entry for this symbol. This
4641 happens when statically linking PIC code, or when
4642 using -Bsymbolic. */
4648 relocation = (splt->output_section->vma
4649 + splt->output_offset
4650 + h->plt.offset + 4)
4651 - (input_section->output_section->vma
4652 + input_section->output_offset
4657 case R_NDS32_GOTPC20:
4658 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4659 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
4660 relocation = elf_gp (output_bfd);
4663 case R_NDS32_GOTPC_HI20:
4664 case R_NDS32_GOTPC_LO12:
4666 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4668 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
4669 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4672 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
4673 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4675 relocation = elf_gp (output_bfd);
4676 relocation -= (input_section->output_section->vma
4677 + input_section->output_offset + rel->r_offset);
4683 case R_NDS32_GOT_HI20:
4684 case R_NDS32_GOT_LO12:
4685 case R_NDS32_GOT_LO15:
4686 case R_NDS32_GOT_LO19:
4687 /* Relocation is to the entry for this symbol in the global
4689 BFD_ASSERT (sgot != NULL);
4696 off = h->got.offset;
4697 BFD_ASSERT (off != (bfd_vma) - 1);
4698 dyn = htab->root.dynamic_sections_created;
4699 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4700 bfd_link_pic (info),
4702 || (bfd_link_pic (info)
4705 || h->forced_local) && h->def_regular))
4707 /* This is actually a static link, or it is a
4708 -Bsymbolic link and the symbol is defined
4709 locally, or the symbol was forced to be local
4710 because of a version file. We must initialize
4711 this entry in the global offset table. Since the
4712 offset must always be a multiple of 4, we use the
4713 least significant bit to record whether we have
4714 initialized it already.
4716 When doing a dynamic link, we create a .rela.got
4717 relocation entry to initialize the value. This
4718 is done in the finish_dynamic_symbol routine. */
4723 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4727 relocation = sgot->output_section->vma + sgot->output_offset + off
4728 - elf_gp (output_bfd);
4735 BFD_ASSERT (local_got_offsets != NULL
4736 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4738 off = local_got_offsets[r_symndx];
4740 /* The offset must always be a multiple of 4. We use
4741 the least significant bit to record whether we have
4742 already processed this entry. */
4747 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4749 if (bfd_link_pic (info))
4752 Elf_Internal_Rela outrel;
4754 /* We need to generate a R_NDS32_RELATIVE reloc
4755 for the dynamic linker. */
4756 srelgot = htab->root.srelgot;
4757 BFD_ASSERT (srelgot != NULL);
4759 outrel.r_offset = (elf_gp (output_bfd)
4760 + sgot->output_offset + off);
4761 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4762 outrel.r_addend = relocation;
4763 loc = srelgot->contents;
4765 srelgot->reloc_count * sizeof (Elf32_External_Rela);
4766 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4767 ++srelgot->reloc_count;
4769 local_got_offsets[r_symndx] |= 1;
4771 relocation = sgot->output_section->vma + sgot->output_offset + off
4772 - elf_gp (output_bfd);
4777 case R_NDS32_16_RELA:
4778 case R_NDS32_20_RELA:
4779 case R_NDS32_5_RELA:
4780 case R_NDS32_32_RELA:
4781 case R_NDS32_9_PCREL_RELA:
4782 case R_NDS32_WORD_9_PCREL_RELA:
4783 case R_NDS32_10_UPCREL_RELA:
4784 case R_NDS32_15_PCREL_RELA:
4785 case R_NDS32_17_PCREL_RELA:
4786 case R_NDS32_25_PCREL_RELA:
4787 case R_NDS32_HI20_RELA:
4788 case R_NDS32_LO12S3_RELA:
4789 case R_NDS32_LO12S2_RELA:
4790 case R_NDS32_LO12S2_DP_RELA:
4791 case R_NDS32_LO12S2_SP_RELA:
4792 case R_NDS32_LO12S1_RELA:
4793 case R_NDS32_LO12S0_RELA:
4794 case R_NDS32_LO12S0_ORI_RELA:
4795 if (bfd_link_pic (info) && r_symndx != 0
4796 && (input_section->flags & SEC_ALLOC) != 0
4797 && (eliminate_gc_relocs == 0
4798 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
4799 && ((r_type != R_NDS32_9_PCREL_RELA
4800 && r_type != R_NDS32_WORD_9_PCREL_RELA
4801 && r_type != R_NDS32_10_UPCREL_RELA
4802 && r_type != R_NDS32_15_PCREL_RELA
4803 && r_type != R_NDS32_17_PCREL_RELA
4804 && r_type != R_NDS32_25_PCREL_RELA
4805 && !(r_type == R_NDS32_32_RELA
4806 && strcmp (input_section->name, ".eh_frame") == 0))
4807 || (h != NULL && h->dynindx != -1
4808 && (!info->symbolic || !h->def_regular))))
4810 Elf_Internal_Rela outrel;
4811 bfd_boolean skip, relocate;
4814 /* When generating a shared object, these relocations
4815 are copied into the output file to be resolved at run
4822 name = bfd_elf_string_from_elf_section
4823 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
4824 elf_section_data (input_section)->rela.hdr->sh_name);
4828 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4829 && strcmp (bfd_get_section_name (input_bfd,
4833 sreloc = bfd_get_section_by_name (dynobj, name);
4834 BFD_ASSERT (sreloc != NULL);
4840 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4844 if (outrel.r_offset == (bfd_vma) - 1)
4846 else if (outrel.r_offset == (bfd_vma) - 2)
4847 skip = TRUE, relocate = TRUE;
4848 outrel.r_offset += (input_section->output_section->vma
4849 + input_section->output_offset);
4852 memset (&outrel, 0, sizeof outrel);
4853 else if (r_type == R_NDS32_17_PCREL_RELA
4854 || r_type == R_NDS32_15_PCREL_RELA
4855 || r_type == R_NDS32_25_PCREL_RELA)
4857 BFD_ASSERT (h != NULL && h->dynindx != -1);
4858 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4859 outrel.r_addend = rel->r_addend;
4863 /* h->dynindx may be -1 if this symbol was marked to
4866 || ((info->symbolic || h->dynindx == -1)
4870 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4871 outrel.r_addend = relocation + rel->r_addend;
4875 BFD_ASSERT (h->dynindx != -1);
4876 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4877 outrel.r_addend = rel->r_addend;
4881 loc = sreloc->contents;
4882 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
4883 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4884 ++sreloc->reloc_count;
4886 /* If this reloc is against an external symbol, we do
4887 not want to fiddle with the addend. Otherwise, we
4888 need to include the symbol value so that it becomes
4889 an addend for the dynamic reloc. */
4895 case R_NDS32_25_ABS_RELA:
4896 if (bfd_link_pic (info))
4899 (_("%pB: warning: cannot deal R_NDS32_25_ABS_RELA in shared "
4900 "mode."), input_bfd);
4905 case R_NDS32_9_PCREL:
4906 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
4908 sec, relocation, addend);
4913 Elf_Internal_Rela *lorel;
4915 /* We allow an arbitrary number of HI20 relocs before the
4916 LO12 reloc. This permits gcc to emit the HI and LO relocs
4918 for (lorel = rel + 1;
4920 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
4923 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
4924 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
4925 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
4926 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
4928 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
4929 contents, relocation + addend);
4933 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4934 contents, offset, relocation,
4940 case R_NDS32_GOT17S2_RELA:
4941 case R_NDS32_GOT15S2_RELA:
4945 BFD_ASSERT (sgot != NULL);
4951 off = h->got.offset;
4952 BFD_ASSERT (off != (bfd_vma) - 1);
4954 dyn = htab->root.dynamic_sections_created;
4955 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
4956 (dyn, bfd_link_pic (info), h)
4957 || (bfd_link_pic (info)
4963 /* This is actually a static link, or it is a
4964 -Bsymbolic link and the symbol is defined
4965 locally, or the symbol was forced to be local
4966 because of a version file. We must initialize
4967 this entry in the global offset table. Since the
4968 offset must always be a multiple of 4, we use the
4969 least significant bit to record whether we have
4970 initialized it already.
4972 When doing a dynamic link, we create a .rela.got
4973 relocation entry to initialize the value. This
4974 is done in the finish_dynamic_symbol routine. */
4979 bfd_put_32 (output_bfd, relocation,
4980 sgot->contents + off);
4989 BFD_ASSERT (local_got_offsets != NULL
4990 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4992 off = local_got_offsets[r_symndx];
4994 /* The offset must always be a multiple of 4. We use
4995 the least significant bit to record whether we have
4996 already processed this entry. */
5001 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5003 if (bfd_link_pic (info))
5006 Elf_Internal_Rela outrel;
5008 /* We need to generate a R_NDS32_RELATIVE reloc
5009 for the dynamic linker. */
5010 srelgot = htab->root.srelgot;
5011 BFD_ASSERT (srelgot != NULL);
5013 outrel.r_offset = (elf_gp (output_bfd)
5014 + sgot->output_offset + off);
5015 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5016 outrel.r_addend = relocation;
5017 loc = srelgot->contents;
5019 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5020 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5021 ++srelgot->reloc_count;
5023 local_got_offsets[r_symndx] |= 1;
5026 relocation = sgot->output_section->vma + sgot->output_offset + off
5027 - elf_gp (output_bfd);
5029 if (relocation & align)
5031 /* Incorrect alignment. */
5033 (_("%pB: warning: unaligned access to GOT entry."), input_bfd);
5035 r = bfd_reloc_dangerous;
5040 case R_NDS32_SDA16S3_RELA:
5041 case R_NDS32_SDA15S3_RELA:
5042 case R_NDS32_SDA15S3:
5046 case R_NDS32_SDA17S2_RELA:
5047 case R_NDS32_SDA15S2_RELA:
5048 case R_NDS32_SDA12S2_SP_RELA:
5049 case R_NDS32_SDA12S2_DP_RELA:
5050 case R_NDS32_SDA15S2:
5051 case R_NDS32_SDA_FP7U2_RELA:
5055 case R_NDS32_SDA18S1_RELA:
5056 case R_NDS32_SDA15S1_RELA:
5057 case R_NDS32_SDA15S1:
5061 case R_NDS32_SDA19S0_RELA:
5062 case R_NDS32_SDA15S0_RELA:
5063 case R_NDS32_SDA15S0:
5067 BFD_ASSERT (sec != NULL);
5069 /* If the symbol is in the abs section, the out_bfd will be null.
5070 This happens when the relocation has a symbol@GOTOFF. */
5071 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5072 if (r != bfd_reloc_ok)
5075 (_("%pB: warning: relocate SDA_BASE failed."), input_bfd);
5080 /* At this point `relocation' contains the object's
5082 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5084 relocation -= fpbase_addr;
5088 /* Now it contains the offset from _SDA_BASE_. */
5090 /* Make sure alignment is correct. */
5092 if (relocation & align)
5094 /* Incorrect alignment. */
5096 /* xgettext:c-format */
5097 (_("%pB(%pA): warning: unaligned small data access of type %d."),
5098 input_bfd, input_section, r_type);
5105 case R_NDS32_17IFC_PCREL_RELA:
5106 case R_NDS32_10IFCU_PCREL_RELA:
5110 case R_NDS32_TLS_LE_HI20:
5111 case R_NDS32_TLS_LE_LO12:
5112 case R_NDS32_TLS_LE_20:
5113 case R_NDS32_TLS_LE_15S0:
5114 case R_NDS32_TLS_LE_15S1:
5115 case R_NDS32_TLS_LE_15S2:
5116 if (elf_hash_table (info)->tls_sec != NULL)
5117 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5119 case R_NDS32_TLS_IE_HI20:
5120 case R_NDS32_TLS_IE_LO12S2:
5122 /* Relocation is to the entry for this symbol in the global
5124 unsigned int tls_type;
5126 Elf_Internal_Rela outrel;
5131 BFD_ASSERT (sgot != NULL);
5136 off = h->got.offset;
5137 BFD_ASSERT (off != (bfd_vma) - 1);
5138 dyn = htab->root.dynamic_sections_created;
5139 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5140 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5141 && (!bfd_link_pic (info)
5142 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5147 /* Never happen currently. */
5148 BFD_ASSERT (local_got_offsets != NULL
5149 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5151 off = local_got_offsets[r_symndx];
5153 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5155 relocation = sgot->output_section->vma + sgot->output_offset + off;
5157 if (r_type == R_NDS32_TLS_IE_LO12S2)
5160 /* The offset must always be a multiple of 4. We use
5161 the least significant bit to record whether we have
5162 already processed this entry. */
5167 bfd_boolean need_relocs = FALSE;
5168 srelgot = htab->root.srelgot;
5169 if ((bfd_link_pic (info) || indx != 0)
5171 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5172 || h->root.type != bfd_link_hash_undefweak))
5175 BFD_ASSERT (srelgot != NULL);
5177 if (tls_type & GOT_TLS_IE)
5181 if (h->dynindx == 0)
5182 outrel.r_addend = relocation - dtpoff_base (info);
5184 outrel.r_addend = 0;
5185 outrel.r_offset = (sgot->output_section->vma
5186 + sgot->output_offset
5189 ELF32_R_INFO (h->dynindx, R_NDS32_TLS_TPOFF);
5191 loc = srelgot->contents;
5193 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5194 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5195 ++srelgot->reloc_count;
5198 bfd_put_32 (output_bfd, h->root.u.def.value - TP_OFFSET,
5199 sgot->contents + off);
5205 /* DON'T fall through. */
5208 /* OLD_NDS32_RELOC. */
5210 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5211 contents, offset, relocation, addend);
5215 switch ((int) r_type)
5217 case R_NDS32_20_RELA:
5218 case R_NDS32_5_RELA:
5219 case R_NDS32_9_PCREL_RELA:
5220 case R_NDS32_WORD_9_PCREL_RELA:
5221 case R_NDS32_10_UPCREL_RELA:
5222 case R_NDS32_15_PCREL_RELA:
5223 case R_NDS32_17_PCREL_RELA:
5224 case R_NDS32_25_PCREL_RELA:
5225 case R_NDS32_25_ABS_RELA:
5226 case R_NDS32_HI20_RELA:
5227 case R_NDS32_LO12S3_RELA:
5228 case R_NDS32_LO12S2_RELA:
5229 case R_NDS32_LO12S2_DP_RELA:
5230 case R_NDS32_LO12S2_SP_RELA:
5231 case R_NDS32_LO12S1_RELA:
5232 case R_NDS32_LO12S0_RELA:
5233 case R_NDS32_LO12S0_ORI_RELA:
5234 case R_NDS32_SDA16S3_RELA:
5235 case R_NDS32_SDA17S2_RELA:
5236 case R_NDS32_SDA18S1_RELA:
5237 case R_NDS32_SDA19S0_RELA:
5238 case R_NDS32_SDA15S3_RELA:
5239 case R_NDS32_SDA15S2_RELA:
5240 case R_NDS32_SDA12S2_DP_RELA:
5241 case R_NDS32_SDA12S2_SP_RELA:
5242 case R_NDS32_SDA15S1_RELA:
5243 case R_NDS32_SDA15S0_RELA:
5244 case R_NDS32_SDA_FP7U2_RELA:
5245 case R_NDS32_9_PLTREL:
5246 case R_NDS32_25_PLTREL:
5248 case R_NDS32_GOT_HI20:
5249 case R_NDS32_GOT_LO12:
5250 case R_NDS32_GOT_LO15:
5251 case R_NDS32_GOT_LO19:
5252 case R_NDS32_GOT15S2_RELA:
5253 case R_NDS32_GOT17S2_RELA:
5254 case R_NDS32_GOTPC20:
5255 case R_NDS32_GOTPC_HI20:
5256 case R_NDS32_GOTPC_LO12:
5257 case R_NDS32_GOTOFF:
5258 case R_NDS32_GOTOFF_HI20:
5259 case R_NDS32_GOTOFF_LO12:
5260 case R_NDS32_GOTOFF_LO15:
5261 case R_NDS32_GOTOFF_LO19:
5262 case R_NDS32_PLTREL_HI20:
5263 case R_NDS32_PLTREL_LO12:
5264 case R_NDS32_PLT_GOTREL_HI20:
5265 case R_NDS32_PLT_GOTREL_LO12:
5266 case R_NDS32_PLT_GOTREL_LO15:
5267 case R_NDS32_PLT_GOTREL_LO19:
5268 case R_NDS32_PLT_GOTREL_LO20:
5269 case R_NDS32_17IFC_PCREL_RELA:
5270 case R_NDS32_10IFCU_PCREL_RELA:
5271 case R_NDS32_TLS_LE_HI20:
5272 case R_NDS32_TLS_LE_LO12:
5273 case R_NDS32_TLS_IE_HI20:
5274 case R_NDS32_TLS_IE_LO12S2:
5275 case R_NDS32_TLS_LE_20:
5276 case R_NDS32_TLS_LE_15S0:
5277 case R_NDS32_TLS_LE_15S1:
5278 case R_NDS32_TLS_LE_15S2:
5279 /* Instruction related relocs must handle endian properly. */
5280 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
5281 r = nds32_elf_final_link_relocate (howto, input_bfd,
5282 input_section, contents,
5283 rel->r_offset, relocation,
5288 /* All other relocs can use default handler. */
5289 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5290 contents, rel->r_offset,
5291 relocation, rel->r_addend);
5297 if (r != bfd_reloc_ok)
5299 /* FIXME: This should be generic enough to go in a utility. */
5303 name = h->root.root.string;
5306 name = bfd_elf_string_from_elf_section
5307 (input_bfd, symtab_hdr->sh_link, sym->st_name);
5308 if (name == NULL || *name == '\0')
5309 name = bfd_section_name (input_bfd, sec);
5317 case bfd_reloc_overflow:
5318 (*info->callbacks->reloc_overflow)
5319 (info, (h ? &h->root : NULL), name, howto->name,
5320 (bfd_vma) 0, input_bfd, input_section, offset);
5323 case bfd_reloc_undefined:
5324 (*info->callbacks->undefined_symbol)
5325 (info, name, input_bfd, input_section, offset, TRUE);
5328 case bfd_reloc_outofrange:
5329 errmsg = _("internal error: out of range error");
5332 case bfd_reloc_notsupported:
5333 errmsg = _("internal error: unsupported relocation error");
5336 case bfd_reloc_dangerous:
5337 errmsg = _("internal error: dangerous error");
5341 errmsg = _("internal error: unknown error");
5345 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
5346 input_section, offset);
5355 /* Finish up dynamic symbol handling. We set the contents of various
5356 dynamic sections here. */
5359 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5360 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
5362 struct elf_nds32_link_hash_table *htab;
5365 htab = nds32_elf_hash_table (info);
5367 if (h->plt.offset != (bfd_vma) - 1)
5375 bfd_vma local_plt_offset;
5376 Elf_Internal_Rela rela;
5378 /* This symbol has an entry in the procedure linkage table. Set
5381 BFD_ASSERT (h->dynindx != -1);
5383 splt = htab->root.splt;
5384 sgot = htab->root.sgotplt;
5385 srela = htab->root.srelplt;
5386 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
5388 /* Get the index in the procedure linkage table which
5389 corresponds to this symbol. This is the index of this symbol
5390 in all the symbols for which we are making plt entries. The
5391 first entry in the procedure linkage table is reserved. */
5392 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
5394 /* Get the offset into the .got table of the entry that
5395 corresponds to this function. Each .got entry is 4 bytes.
5396 The first three are reserved. */
5397 got_offset = (plt_index + 3) * 4;
5399 /* Fill in the entry in the procedure linkage table. */
5400 if (!bfd_link_pic (info))
5404 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
5405 + sgot->output_offset + got_offset) >> 12)
5407 bfd_putb32 (insn, splt->contents + h->plt.offset);
5409 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
5410 + sgot->output_offset + got_offset) & 0x0fff)
5412 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5414 insn = PLT_ENTRY_WORD2;
5415 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5417 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
5418 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5420 insn = PLT_ENTRY_WORD4
5421 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
5422 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5423 local_plt_offset = 12;
5427 /* sda_base must be set at this time. */
5431 /* FIXME, sda_base is 65536, it will damage opcode. */
5432 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5433 offset = sgot->output_section->vma + sgot->output_offset + got_offset
5434 - elf_gp (output_bfd);
5435 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
5436 bfd_putb32 (insn, splt->contents + h->plt.offset);
5438 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
5439 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5441 insn = PLT_PIC_ENTRY_WORD2;
5442 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5444 insn = PLT_PIC_ENTRY_WORD3;
5445 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5447 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
5448 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5450 insn = PLT_PIC_ENTRY_WORD5
5451 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
5452 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
5454 local_plt_offset = 16;
5457 /* Fill in the entry in the global offset table,
5458 so it will fall through to the next instruction for the first time. */
5459 bfd_put_32 (output_bfd,
5460 (splt->output_section->vma + splt->output_offset
5461 + h->plt.offset + local_plt_offset),
5462 sgot->contents + got_offset);
5464 /* Fill in the entry in the .rela.plt section. */
5465 rela.r_offset = (sgot->output_section->vma
5466 + sgot->output_offset + got_offset);
5467 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
5469 loc = srela->contents;
5470 loc += plt_index * sizeof (Elf32_External_Rela);
5471 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5473 if (!h->def_regular)
5475 /* Mark the symbol as undefined, rather than as defined in
5476 the .plt section. Leave the value alone. */
5477 sym->st_shndx = SHN_UNDEF;
5478 if (!h->ref_regular_nonweak)
5483 if (h->got.offset != (bfd_vma) - 1)
5487 Elf_Internal_Rela rela;
5489 /* This symbol has an entry in the global offset table.
5492 sgot = htab->root.sgot;
5493 srela = htab->root.srelgot;
5494 BFD_ASSERT (sgot != NULL && srela != NULL);
5496 rela.r_offset = (sgot->output_section->vma
5497 + sgot->output_offset + (h->got.offset & ~1));
5499 /* If this is a -Bsymbolic link, and the symbol is defined
5500 locally, we just want to emit a RELATIVE reloc. Likewise if
5501 the symbol was forced to be local because of a version file.
5502 The entry in the global offset table will already have been
5503 initialized in the relocate_section function. */
5504 if (bfd_link_pic (info)
5506 || h->dynindx == -1 || h->forced_local) && h->def_regular)
5508 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5509 rela.r_addend = (h->root.u.def.value
5510 + h->root.u.def.section->output_section->vma
5511 + h->root.u.def.section->output_offset);
5515 BFD_ASSERT ((h->got.offset & 1) == 0);
5516 bfd_put_32 (output_bfd, (bfd_vma) 0,
5517 sgot->contents + h->got.offset);
5518 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
5522 loc = srela->contents;
5523 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
5524 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5525 ++srela->reloc_count;
5531 Elf_Internal_Rela rela;
5533 /* This symbols needs a copy reloc. Set it up. */
5535 BFD_ASSERT (h->dynindx != -1
5536 && (h->root.type == bfd_link_hash_defined
5537 || h->root.type == bfd_link_hash_defweak));
5539 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
5540 BFD_ASSERT (s != NULL);
5542 rela.r_offset = (h->root.u.def.value
5543 + h->root.u.def.section->output_section->vma
5544 + h->root.u.def.section->output_offset);
5545 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
5548 loc += s->reloc_count * sizeof (Elf32_External_Rela);
5549 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5553 /* Mark some specially defined symbols as absolute. */
5554 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5555 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5556 sym->st_shndx = SHN_ABS;
5562 /* Finish up the dynamic sections. */
5565 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
5567 struct elf_nds32_link_hash_table *htab;
5572 htab = nds32_elf_hash_table (info);
5573 dynobj = htab->root.dynobj;
5575 sgot = htab->root.sgotplt;
5576 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5578 if (htab->root.dynamic_sections_created)
5581 Elf32_External_Dyn *dyncon, *dynconend;
5583 BFD_ASSERT (sgot != NULL && sdyn != NULL);
5585 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5586 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5588 for (; dyncon < dynconend; dyncon++)
5590 Elf_Internal_Dyn dyn;
5593 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5601 s = htab->root.sgotplt;
5604 s = htab->root.srelplt;
5606 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5607 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5611 s = htab->root.srelplt;
5612 dyn.d_un.d_val = s->size;
5613 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5618 /* Fill in the first entry in the procedure linkage table. */
5619 splt = htab->root.splt;
5620 if (splt && splt->size > 0)
5622 if (bfd_link_pic (info))
5627 /* FIXME, sda_base is 65536, it will damage opcode. */
5628 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5629 offset = sgot->output_section->vma + sgot->output_offset + 4
5630 - elf_gp (output_bfd);
5631 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
5632 bfd_putb32 (insn, splt->contents);
5634 /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
5635 /* here has a typo? */
5636 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
5637 bfd_putb32 (insn, splt->contents + 4);
5639 insn = PLT0_PIC_ENTRY_WORD2;
5640 bfd_putb32 (insn, splt->contents + 8);
5642 insn = PLT0_PIC_ENTRY_WORD3;
5643 bfd_putb32 (insn, splt->contents + 12);
5645 insn = PLT0_PIC_ENTRY_WORD4;
5646 bfd_putb32 (insn, splt->contents + 16);
5648 insn = PLT0_PIC_ENTRY_WORD5;
5649 bfd_putb32 (insn, splt->contents + 20);
5656 /* addr = .got + 4 */
5657 addr = sgot->output_section->vma + sgot->output_offset + 4;
5658 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
5659 bfd_putb32 (insn, splt->contents);
5661 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
5662 bfd_putb32 (insn, splt->contents + 4);
5664 insn = PLT0_ENTRY_WORD2;
5665 bfd_putb32 (insn, splt->contents + 8);
5667 insn = PLT0_ENTRY_WORD3;
5668 bfd_putb32 (insn, splt->contents + 12);
5670 insn = PLT0_ENTRY_WORD4;
5671 bfd_putb32 (insn, splt->contents + 16);
5674 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
5679 /* Fill in the first three entries in the global offset table. */
5680 if (sgot && sgot->size > 0)
5683 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5685 bfd_put_32 (output_bfd,
5686 sdyn->output_section->vma + sdyn->output_offset,
5688 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5689 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5691 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5698 /* Set the right machine number. */
5701 nds32_elf_object_p (bfd *abfd)
5703 static unsigned int cur_arch = 0;
5705 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
5707 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
5708 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
5715 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
5718 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
5720 case E_NDS_ARCH_STAR_V2_0:
5721 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
5723 case E_NDS_ARCH_STAR_V3_0:
5724 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
5726 case E_NDS_ARCH_STAR_V3_M:
5727 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
5734 /* Store the machine number in the flags field. */
5737 nds32_elf_final_write_processing (bfd *abfd,
5738 bfd_boolean linker ATTRIBUTE_UNUSED)
5741 static unsigned int cur_mach = 0;
5743 if (bfd_mach_n1 != bfd_get_mach (abfd))
5745 cur_mach = bfd_get_mach (abfd);
5751 /* Only happen when object is empty, since the case is abandon. */
5753 val |= E_NDS_ABI_AABI;
5754 val |= E_NDS32_ELF_VER_1_4;
5759 case bfd_mach_n1h_v2:
5760 val = E_NDS_ARCH_STAR_V2_0;
5762 case bfd_mach_n1h_v3:
5763 val = E_NDS_ARCH_STAR_V3_0;
5765 case bfd_mach_n1h_v3m:
5766 val = E_NDS_ARCH_STAR_V3_M;
5773 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
5774 elf_elfheader (abfd)->e_flags |= val;
5777 /* Function to keep NDS32 specific file flags. */
5780 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
5782 BFD_ASSERT (!elf_flags_init (abfd)
5783 || elf_elfheader (abfd)->e_flags == flags);
5785 elf_elfheader (abfd)->e_flags = flags;
5786 elf_flags_init (abfd) = TRUE;
5791 convert_e_flags (unsigned int e_flags, unsigned int arch)
5793 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
5795 /* From 0.9 to 1.0. */
5796 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
5798 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5799 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5800 if (arch == E_NDS_ARCH_STAR_V1_0)
5807 /* From 1.0 to 2.0. */
5808 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
5810 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
5811 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
5813 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5814 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5819 nds32_check_vec_size (bfd *ibfd)
5821 static unsigned int nds32_vec_size = 0;
5823 asection *sec_t = NULL;
5824 bfd_byte *contents = NULL;
5826 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
5828 if (sec_t && sec_t->size >= 4)
5830 /* Get vec_size in file. */
5831 unsigned int flag_t;
5833 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
5834 flag_t = bfd_get_32 (ibfd, contents);
5836 /* The value could only be 4 or 16. */
5838 if (!nds32_vec_size)
5839 /* Set if not set yet. */
5840 nds32_vec_size = (flag_t & 0x3);
5841 else if (nds32_vec_size != (flag_t & 0x3))
5844 /* xgettext:c-format */
5845 (_("%pB: ISR vector size mismatch"
5846 " with previous modules, previous %u-byte, current %u-byte"),
5848 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
5849 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
5853 /* Only keep the first vec_size section. */
5854 sec_t->flags |= SEC_EXCLUDE;
5860 /* Merge backend specific data from an object file to the output
5861 object file when linking. */
5864 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5866 bfd *obfd = info->output_bfd;
5869 flagword out_16regs;
5871 flagword out_no_mac;
5873 flagword out_version;
5874 flagword in_version;
5875 flagword out_fpu_config;
5876 flagword in_fpu_config;
5878 /* TODO: Revise to use object-attributes instead. */
5879 if (!nds32_check_vec_size (ibfd))
5882 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5883 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5886 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
5889 (_("%pB: warning: Endian mismatch with previous modules."), ibfd);
5891 bfd_set_error (bfd_error_bad_value);
5895 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
5896 if (in_version == E_NDS32_ELF_VER_1_2)
5899 (_("%pB: warning: Older version of object file encountered, "
5900 "Please recompile with current tool chain."), ibfd);
5903 /* We may need to merge V1 and V2 arch object files to V2. */
5904 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5905 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5907 /* Need to convert version. */
5908 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5909 == E_NDS_ARCH_STAR_RESERVED)
5911 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5913 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
5914 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5915 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5917 elf_elfheader (obfd)->e_flags =
5918 convert_e_flags (elf_elfheader (obfd)->e_flags,
5919 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
5923 elf_elfheader (ibfd)->e_flags =
5924 convert_e_flags (elf_elfheader (ibfd)->e_flags,
5925 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
5929 /* Extract some flags. */
5930 in_flags = elf_elfheader (ibfd)->e_flags
5931 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
5932 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
5934 /* The following flags need special treatment. */
5935 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
5936 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
5937 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
5939 /* Extract some flags. */
5940 out_flags = elf_elfheader (obfd)->e_flags
5941 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
5942 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
5944 /* The following flags need special treatment. */
5945 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
5946 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
5947 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
5948 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
5949 if (!elf_flags_init (obfd))
5951 /* If the input is the default architecture then do not
5952 bother setting the flags for the output architecture,
5953 instead allow future merges to do this. If no future
5954 merges ever set these flags then they will retain their
5955 unitialised values, which surprise surprise, correspond
5956 to the default values. */
5957 if (bfd_get_arch_info (ibfd)->the_default)
5960 elf_flags_init (obfd) = TRUE;
5961 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5963 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
5964 && bfd_get_arch_info (obfd)->the_default)
5966 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
5967 bfd_get_mach (ibfd));
5973 /* Check flag compatibility. */
5974 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
5977 (_("%pB: error: ABI mismatch with previous modules."), ibfd);
5979 bfd_set_error (bfd_error_bad_value);
5983 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
5985 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
5988 (_("%pB: error: Instruction set mismatch with previous modules."), ibfd);
5990 bfd_set_error (bfd_error_bad_value);
5995 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
5996 and perf ext1 and DIV are mergerd to perf ext1. */
5997 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
5999 elf_elfheader (obfd)->e_flags =
6000 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6001 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6002 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6003 ? E_NDS32_HAS_EXT_INST : 0)
6004 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6005 ? E_NDS32_HAS_EXT_INST : 0)
6006 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6007 | ((in_version > out_version) ? out_version : in_version);
6011 if (in_version != out_version)
6013 /* xgettext:c-format */
6014 (_("%pB: warning: Incompatible elf-versions %s and %s."),
6015 ibfd, nds32_elfver_strtab[out_version],
6016 nds32_elfver_strtab[in_version]);
6018 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6019 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6020 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6021 | (in_version > out_version ? out_version : in_version);
6027 /* Display the flags field. */
6030 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6032 FILE *file = (FILE *) ptr;
6034 BFD_ASSERT (abfd != NULL && ptr != NULL);
6036 _bfd_elf_print_private_bfd_data (abfd, ptr);
6038 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6040 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6044 fprintf (file, _(": n1 instructions"));
6047 fprintf (file, _(": n1h instructions"));
6057 nds32_elf_action_discarded (asection *sec)
6061 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6064 return _bfd_elf_default_action_discarded (sec);
6068 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6069 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6070 Elf_Internal_Sym *sym)
6073 switch (ELF32_R_TYPE (rel->r_info))
6075 case R_NDS32_GNU_VTINHERIT:
6076 case R_NDS32_GNU_VTENTRY:
6077 case R_NDS32_RELA_GNU_VTINHERIT:
6078 case R_NDS32_RELA_GNU_VTENTRY:
6082 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6085 /* Look through the relocs for a section during the first phase.
6086 Since we don't do .gots or .plts, we just need to consider the
6087 virtual table relocs for gc. */
6090 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6091 asection *sec, const Elf_Internal_Rela *relocs)
6093 Elf_Internal_Shdr *symtab_hdr;
6094 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6095 const Elf_Internal_Rela *rel;
6096 const Elf_Internal_Rela *rel_end;
6097 struct elf_nds32_link_hash_table *htab;
6099 asection *sreloc = NULL;
6101 if (bfd_link_relocatable (info))
6104 /* Don't do anything special with non-loaded, non-alloced sections.
6105 In particular, any relocs in such sections should not affect GOT
6106 and PLT reference counting (ie. we don't allow them to create GOT
6107 or PLT entries), there's no possibility or desire to optimize TLS
6108 relocs, and there's not much point in propagating relocs to shared
6109 libs that the dynamic linker won't relocate. */
6110 if ((sec->flags & SEC_ALLOC) == 0)
6113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6114 sym_hashes = elf_sym_hashes (abfd);
6116 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6117 if (!elf_bad_symtab (abfd))
6118 sym_hashes_end -= symtab_hdr->sh_info;
6120 htab = nds32_elf_hash_table (info);
6121 dynobj = htab->root.dynobj;
6123 rel_end = relocs + sec->reloc_count;
6124 for (rel = relocs; rel < rel_end; rel++)
6126 enum elf_nds32_reloc_type r_type;
6127 struct elf_link_hash_entry *h;
6128 unsigned long r_symndx;
6129 int tls_type, old_tls_type;
6131 r_symndx = ELF32_R_SYM (rel->r_info);
6132 r_type = ELF32_R_TYPE (rel->r_info);
6133 if (r_symndx < symtab_hdr->sh_info)
6137 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6138 while (h->root.type == bfd_link_hash_indirect
6139 || h->root.type == bfd_link_hash_warning)
6140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6143 /* Some relocs require a global offset table. We create
6144 got section here, since these relocation need got section
6145 and it is not created yet. */
6146 if (htab->root.sgot == NULL)
6150 case R_NDS32_GOT_HI20:
6151 case R_NDS32_GOT_LO12:
6152 case R_NDS32_GOT_LO15:
6153 case R_NDS32_GOT_LO19:
6154 case R_NDS32_GOT17S2_RELA:
6155 case R_NDS32_GOT15S2_RELA:
6156 case R_NDS32_GOTOFF:
6157 case R_NDS32_GOTOFF_HI20:
6158 case R_NDS32_GOTOFF_LO12:
6159 case R_NDS32_GOTOFF_LO15:
6160 case R_NDS32_GOTOFF_LO19:
6161 case R_NDS32_GOTPC20:
6162 case R_NDS32_GOTPC_HI20:
6163 case R_NDS32_GOTPC_LO12:
6165 case R_NDS32_TLS_IE_HI20:
6166 case R_NDS32_TLS_IE_LO12S2:
6168 htab->root.dynobj = dynobj = abfd;
6169 if (!_bfd_elf_create_got_section (dynobj, info))
6178 switch ((int) r_type)
6180 case R_NDS32_GOT_HI20:
6181 case R_NDS32_GOT_LO12:
6182 case R_NDS32_GOT_LO15:
6183 case R_NDS32_GOT_LO19:
6185 case R_NDS32_TLS_IE_HI20:
6186 case R_NDS32_TLS_IE_LO12S2:
6189 case R_NDS32_TLS_IE_HI20:
6190 case R_NDS32_TLS_IE_LO12S2:
6191 tls_type = GOT_TLS_IE;
6194 tls_type = GOT_NORMAL;
6199 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
6200 h->got.refcount += 1;
6204 bfd_signed_vma *local_got_refcounts;
6206 /* This is a global offset table entry for a local
6208 local_got_refcounts = elf_local_got_refcounts (abfd);
6209 if (local_got_refcounts == NULL)
6213 size = symtab_hdr->sh_info;
6214 size *= sizeof (bfd_signed_vma);
6215 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
6216 if (local_got_refcounts == NULL)
6218 elf_local_got_refcounts (abfd) = local_got_refcounts;
6220 local_got_refcounts[r_symndx] += 1;
6221 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
6224 /* We will already have issued an error message if there
6225 is a TLS/non-TLS mismatch, based on the symbol
6226 type. So just combine any TLS types needed. */
6227 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
6228 && tls_type != GOT_NORMAL)
6229 tls_type |= old_tls_type;
6231 if (old_tls_type != tls_type)
6234 elf32_nds32_hash_entry (h)->tls_type = tls_type;
6236 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
6239 case R_NDS32_9_PLTREL:
6240 case R_NDS32_25_PLTREL:
6241 case R_NDS32_PLTREL_HI20:
6242 case R_NDS32_PLTREL_LO12:
6243 case R_NDS32_PLT_GOTREL_HI20:
6244 case R_NDS32_PLT_GOTREL_LO12:
6245 case R_NDS32_PLT_GOTREL_LO15:
6246 case R_NDS32_PLT_GOTREL_LO19:
6247 case R_NDS32_PLT_GOTREL_LO20:
6249 /* This symbol requires a procedure linkage table entry. We
6250 actually build the entry in adjust_dynamic_symbol,
6251 because this might be a case of linking PIC code without
6252 linking in any dynamic objects, in which case we don't
6253 need to generate a procedure linkage table after all. */
6255 /* If this is a local symbol, we resolve it directly without
6256 creating a procedure linkage table entry. */
6260 if (h->forced_local)
6263 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
6265 h->plt.refcount += 1;
6268 case R_NDS32_16_RELA:
6269 case R_NDS32_20_RELA:
6270 case R_NDS32_5_RELA:
6271 case R_NDS32_32_RELA:
6272 case R_NDS32_HI20_RELA:
6273 case R_NDS32_LO12S3_RELA:
6274 case R_NDS32_LO12S2_RELA:
6275 case R_NDS32_LO12S2_DP_RELA:
6276 case R_NDS32_LO12S2_SP_RELA:
6277 case R_NDS32_LO12S1_RELA:
6278 case R_NDS32_LO12S0_RELA:
6279 case R_NDS32_LO12S0_ORI_RELA:
6280 case R_NDS32_SDA16S3_RELA:
6281 case R_NDS32_SDA17S2_RELA:
6282 case R_NDS32_SDA18S1_RELA:
6283 case R_NDS32_SDA19S0_RELA:
6284 case R_NDS32_SDA15S3_RELA:
6285 case R_NDS32_SDA15S2_RELA:
6286 case R_NDS32_SDA12S2_DP_RELA:
6287 case R_NDS32_SDA12S2_SP_RELA:
6288 case R_NDS32_SDA15S1_RELA:
6289 case R_NDS32_SDA15S0_RELA:
6290 case R_NDS32_SDA_FP7U2_RELA:
6291 case R_NDS32_15_PCREL_RELA:
6292 case R_NDS32_17_PCREL_RELA:
6293 case R_NDS32_25_PCREL_RELA:
6295 if (h != NULL && !bfd_link_pic (info))
6298 h->plt.refcount += 1;
6301 /* If we are creating a shared library, and this is a reloc against
6302 a global symbol, or a non PC relative reloc against a local
6303 symbol, then we need to copy the reloc into the shared library.
6304 However, if we are linking with -Bsymbolic, we do not need to
6305 copy a reloc against a global symbol which is defined in an
6306 object we are including in the link (i.e., DEF_REGULAR is set).
6307 At this point we have not seen all the input files, so it is
6308 possible that DEF_REGULAR is not set now but will be set later
6309 (it is never cleared). We account for that possibility below by
6310 storing information in the dyn_relocs field of the hash table
6311 entry. A similar situation occurs when creating shared libraries
6312 and symbol visibility changes render the symbol local.
6314 If on the other hand, we are creating an executable, we may need
6315 to keep relocations for symbols satisfied by a dynamic library
6316 if we manage to avoid copy relocs for the symbol. */
6317 if ((bfd_link_pic (info)
6318 && (sec->flags & SEC_ALLOC) != 0
6319 && ((r_type != R_NDS32_25_PCREL_RELA
6320 && r_type != R_NDS32_15_PCREL_RELA
6321 && r_type != R_NDS32_17_PCREL_RELA
6322 && !(r_type == R_NDS32_32_RELA
6323 && strcmp (sec->name, ".eh_frame") == 0))
6326 || h->root.type == bfd_link_hash_defweak
6327 || !h->def_regular))))
6328 || (!bfd_link_pic (info)
6329 && (sec->flags & SEC_ALLOC) != 0
6331 && (h->root.type == bfd_link_hash_defweak
6332 || !h->def_regular)))
6334 struct elf_dyn_relocs *p;
6335 struct elf_dyn_relocs **head;
6338 htab->root.dynobj = dynobj = abfd;
6340 /* When creating a shared object, we must copy these
6341 relocs into the output file. We create a reloc
6342 section in dynobj and make room for the reloc. */
6347 name = bfd_elf_string_from_elf_section
6348 (abfd, elf_elfheader (abfd)->e_shstrndx,
6349 elf_section_data (sec)->rela.hdr->sh_name);
6353 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6354 && strcmp (bfd_get_section_name (abfd, sec),
6357 sreloc = bfd_get_section_by_name (dynobj, name);
6362 sreloc = bfd_make_section (dynobj, name);
6363 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6364 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6365 if ((sec->flags & SEC_ALLOC) != 0)
6366 flags |= SEC_ALLOC | SEC_LOAD;
6368 || !bfd_set_section_flags (dynobj, sreloc, flags)
6369 || !bfd_set_section_alignment (dynobj, sreloc, 2))
6372 elf_section_type (sreloc) = SHT_RELA;
6374 elf_section_data (sec)->sreloc = sreloc;
6377 /* If this is a global symbol, we count the number of
6378 relocations we need for this symbol. */
6380 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
6386 Elf_Internal_Sym *isym;
6387 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
6391 /* Track dynamic relocs needed for local syms too. */
6392 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6396 vpp = &elf_section_data (s)->local_dynrel;
6397 head = (struct elf_dyn_relocs **) vpp;
6401 if (p == NULL || p->sec != sec)
6403 bfd_size_type amt = sizeof (*p);
6404 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
6415 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
6416 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6417 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
6422 /* This relocation describes the C++ object vtable hierarchy.
6423 Reconstruct it for later use during GC. */
6424 case R_NDS32_RELA_GNU_VTINHERIT:
6425 case R_NDS32_GNU_VTINHERIT:
6426 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6430 /* This relocation describes which C++ vtable entries are actually
6431 used. Record for later use during GC. */
6432 case R_NDS32_GNU_VTENTRY:
6433 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6436 case R_NDS32_RELA_GNU_VTENTRY:
6437 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6446 /* Write VAL in uleb128 format to P, returning a pointer to the
6448 This code is copied from elf-attr.c. */
6451 write_uleb128 (bfd_byte *p, unsigned int val)
6466 static bfd_signed_vma
6467 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
6468 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
6469 int *pic_ext_target)
6471 bfd_signed_vma foff;
6472 bfd_vma symval, addend;
6475 /* Get the value of the symbol referred to by the reloc. */
6476 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6478 Elf_Internal_Sym *isym;
6480 /* A local symbol. */
6481 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6483 if (isym->st_shndx == SHN_UNDEF)
6484 sym_sec = bfd_und_section_ptr;
6485 else if (isym->st_shndx == SHN_ABS)
6486 sym_sec = bfd_abs_section_ptr;
6487 else if (isym->st_shndx == SHN_COMMON)
6488 sym_sec = bfd_com_section_ptr;
6490 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6491 symval = isym->st_value + sym_sec->output_section->vma
6492 + sym_sec->output_offset;
6497 struct elf_link_hash_entry *h;
6500 /* An external symbol. */
6501 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6502 h = elf_sym_hashes (abfd)[indx];
6503 BFD_ASSERT (h != NULL);
6505 if (h->root.type != bfd_link_hash_defined
6506 && h->root.type != bfd_link_hash_defweak)
6507 /* This appears to be a reference to an undefined
6508 symbol. Just ignore it--it will be caught by the
6509 regular reloc processing. */
6511 owner = h->root.u.def.section->owner;
6512 if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
6513 *pic_ext_target = 1;
6515 if (h->root.u.def.section->flags & SEC_MERGE)
6517 sym_sec = h->root.u.def.section;
6518 symval = _bfd_merged_section_offset (abfd, &sym_sec,
6519 elf_section_data (sym_sec)->sec_info,
6520 h->root.u.def.value);
6521 symval = symval + sym_sec->output_section->vma
6522 + sym_sec->output_offset;
6525 symval = (h->root.u.def.value
6526 + h->root.u.def.section->output_section->vma
6527 + h->root.u.def.section->output_offset);
6530 addend = irel->r_addend;
6532 foff = (symval + addend
6533 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
6538 calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
6539 Elf_Internal_Sym *isymbuf,
6540 Elf_Internal_Rela *irel,
6541 Elf_Internal_Shdr *symtab_hdr)
6545 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6547 Elf_Internal_Sym *isym;
6549 /* A local symbol. */
6550 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6552 if (isym->st_shndx == SHN_UNDEF)
6553 sym_sec = bfd_und_section_ptr;
6554 else if (isym->st_shndx == SHN_ABS)
6555 sym_sec = bfd_abs_section_ptr;
6556 else if (isym->st_shndx == SHN_COMMON)
6557 sym_sec = bfd_com_section_ptr;
6559 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6560 symval = isym->st_value + sym_sec->output_section->vma
6561 + sym_sec->output_offset;
6566 struct elf_link_hash_entry *h;
6567 struct elf_nds32_link_hash_table *htab;
6570 /* An external symbol. */
6571 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6572 h = elf_sym_hashes (abfd)[indx];
6573 BFD_ASSERT (h != NULL);
6574 htab = nds32_elf_hash_table (link_info);
6575 splt = htab->root.splt;
6577 while (h->root.type == bfd_link_hash_indirect
6578 || h->root.type == bfd_link_hash_warning)
6579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6581 if (h->plt.offset == (bfd_vma) - 1)
6583 if (h->root.type != bfd_link_hash_defined
6584 && h->root.type != bfd_link_hash_defweak)
6585 /* This appears to be a reference to an undefined
6586 * symbol. Just ignore it--it will be caught by the
6587 * regular reloc processing. */
6589 symval = (h->root.u.def.value
6590 + h->root.u.def.section->output_section->vma
6591 + h->root.u.def.section->output_offset);
6594 symval = splt->output_section->vma + h->plt.offset;
6600 static bfd_signed_vma
6601 calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
6602 Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
6603 Elf_Internal_Shdr *symtab_hdr)
6606 if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
6610 return foff - (irel->r_offset
6611 + sec->output_section->vma + sec->output_offset);
6614 /* Convert a 32-bit instruction to 16-bit one.
6615 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
6616 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
6617 type of INSN16. Return 1 if successful. */
6620 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6623 uint16_t insn16 = 0;
6625 unsigned long mach = bfd_get_mach (abfd);
6627 if (N32_SH5 (insn) != 0)
6630 switch (N32_SUB5 (insn))
6632 case N32_ALU1_ADD_SLLI:
6633 case N32_ALU1_ADD_SRLI:
6634 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6636 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
6638 insn_type = NDS32_INSN_ADD333;
6640 else if (N32_IS_RT4 (insn))
6642 if (N32_RT5 (insn) == N32_RA5 (insn))
6643 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
6644 else if (N32_RT5 (insn) == N32_RB5 (insn))
6645 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
6646 insn_type = NDS32_INSN_ADD45;
6650 case N32_ALU1_SUB_SLLI:
6651 case N32_ALU1_SUB_SRLI:
6652 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6654 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
6656 insn_type = NDS32_INSN_SUB333;
6658 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6660 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
6661 insn_type = NDS32_INSN_SUB45;
6665 case N32_ALU1_AND_SLLI:
6666 case N32_ALU1_AND_SRLI:
6667 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
6668 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6669 && N32_IS_RB3 (insn))
6671 if (N32_RT5 (insn) == N32_RA5 (insn))
6672 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
6673 else if (N32_RT5 (insn) == N32_RB5 (insn))
6674 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
6676 insn_type = NDS32_INSN_AND33;
6680 case N32_ALU1_XOR_SLLI:
6681 case N32_ALU1_XOR_SRLI:
6682 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
6683 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6684 && N32_IS_RB3 (insn))
6686 if (N32_RT5 (insn) == N32_RA5 (insn))
6687 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
6688 else if (N32_RT5 (insn) == N32_RB5 (insn))
6689 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
6691 insn_type = NDS32_INSN_XOR33;
6695 case N32_ALU1_OR_SLLI:
6696 case N32_ALU1_OR_SRLI:
6697 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
6698 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6699 && N32_IS_RB3 (insn))
6701 if (N32_RT5 (insn) == N32_RA5 (insn))
6702 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
6703 else if (N32_RT5 (insn) == N32_RB5 (insn))
6704 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
6706 insn_type = NDS32_INSN_OR33;
6710 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
6711 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
6712 && N32_RA5 (insn) == N32_RB5 (insn))
6714 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
6715 insn_type = NDS32_INSN_NOT33;
6719 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6721 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
6722 insn_type = NDS32_INSN_SRAI45;
6727 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6729 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
6730 insn_type = NDS32_INSN_SRLI45;
6735 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
6737 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
6739 insn_type = NDS32_INSN_SLLI333;
6744 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6746 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
6747 insn_type = NDS32_INSN_ZEH33;
6752 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6754 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
6755 insn_type = NDS32_INSN_SEB33;
6760 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6762 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
6763 insn_type = NDS32_INSN_SEH33;
6768 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6771 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
6772 insn_type = NDS32_INSN_SLT45;
6777 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6780 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
6781 insn_type = NDS32_INSN_SLTS45;
6786 if ((insn16 & 0x8000) == 0)
6792 *pinsn_type = insn_type;
6797 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6800 uint16_t insn16 = 0;
6802 unsigned long mach = bfd_get_mach (abfd);
6804 /* TODO: bset, bclr, btgl, btst. */
6805 if (__GF (insn, 6, 4) != 0)
6808 switch (N32_IMMU (insn, 6))
6811 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6812 && N32_IS_RB3 (insn))
6814 if (N32_RT5 (insn) == N32_RA5 (insn))
6815 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
6816 else if (N32_RT5 (insn) == N32_RB5 (insn))
6817 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
6819 insn_type = NDS32_INSN_MUL33;
6823 if ((insn16 & 0x8000) == 0)
6829 *pinsn_type = insn_type;
6834 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6838 uint16_t insn16 = 0;
6840 unsigned long mach = bfd_get_mach (abfd);
6842 /* Decode 32-bit instruction. */
6843 if (insn & 0x80000000)
6845 /* Not 32-bit insn. */
6849 op6 = N32_OP6 (insn);
6851 /* Convert it to 16-bit instruction. */
6855 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
6857 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
6858 insn_type = NDS32_INSN_MOVI55;
6860 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
6861 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
6863 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
6864 N32_IMM20S (insn) - 16);
6865 insn_type = NDS32_INSN_MOVPI45;
6870 if (N32_IMM15S (insn) == 0)
6872 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
6873 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
6875 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
6877 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
6878 insn_type = NDS32_INSN_MOV55;
6881 else if (N32_IMM15S (insn) > 0)
6883 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
6885 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
6887 insn_type = NDS32_INSN_ADDI333;
6889 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6890 && N32_IMM15S (insn) < 32)
6892 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
6893 insn_type = NDS32_INSN_ADDI45;
6895 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6896 && N32_RT5 (insn) == N32_RA5 (insn)
6897 && N32_IMM15S (insn) < 512)
6899 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6900 insn_type = NDS32_INSN_ADDI10_SP;
6902 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
6903 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
6904 && (N32_IMM15S (insn) % 4 == 0))
6906 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
6907 N32_IMM15S (insn) >> 2);
6908 insn_type = NDS32_INSN_ADDRI36_SP;
6914 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
6916 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
6917 0 - N32_IMM15S (insn));
6918 insn_type = NDS32_INSN_SUBI333;
6920 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6921 && N32_IMM15S (insn) > -32)
6923 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
6924 0 - N32_IMM15S (insn));
6925 insn_type = NDS32_INSN_SUBI45;
6927 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6928 && N32_RT5 (insn) == N32_RA5 (insn)
6929 && N32_IMM15S (insn) >= -512)
6931 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6932 insn_type = NDS32_INSN_ADDI10_SP;
6938 if (N32_IMM15S (insn) == 0)
6940 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
6941 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
6943 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
6945 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
6946 insn_type = NDS32_INSN_MOV55;
6952 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
6953 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
6955 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
6956 insn_type = NDS32_INSN_NEG33;
6961 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6963 if (N32_IMM15U (insn) == 1)
6965 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
6966 insn_type = NDS32_INSN_XLSB33;
6968 else if (N32_IMM15U (insn) == 0x7ff)
6970 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
6971 insn_type = NDS32_INSN_X11B33;
6973 else if (N32_IMM15U (insn) == 0xff)
6975 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
6976 insn_type = NDS32_INSN_ZEB33;
6978 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
6979 && N32_IMM15U (insn) < 256)
6981 int imm15u = N32_IMM15U (insn);
6983 if (__builtin_popcount (imm15u) == 1)
6986 int imm3u = __builtin_ctz (imm15u);
6988 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
6989 insn_type = NDS32_INSN_BMSKI33;
6991 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
6994 int imm3u = __builtin_ctz (imm15u + 1) - 1;
6996 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
6997 insn_type = NDS32_INSN_FEXTI33;
7004 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7005 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7007 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7008 insn_type = NDS32_INSN_SLTI45;
7013 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7014 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7016 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7017 insn_type = NDS32_INSN_SLTSI45;
7022 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7024 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7025 insn_type = NDS32_INSN_LWI450;
7027 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7028 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7030 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7032 insn_type = NDS32_INSN_LWI333;
7034 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7035 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7037 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7038 insn_type = NDS32_INSN_LWI37;
7040 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7041 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7043 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7044 insn_type = NDS32_INSN_LWI37_SP;
7046 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7047 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7049 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7050 N32_IMM15S (insn) + 32);
7051 insn_type = NDS32_INSN_LWI45_FE;
7056 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7058 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7059 insn_type = NDS32_INSN_SWI450;
7061 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7062 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7064 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7066 insn_type = NDS32_INSN_SWI333;
7068 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7069 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7071 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7072 insn_type = NDS32_INSN_SWI37;
7074 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7075 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7077 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7078 insn_type = NDS32_INSN_SWI37_SP;
7082 case N32_OP6_LWI_BI:
7083 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7084 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7086 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7088 insn_type = NDS32_INSN_LWI333_BI;
7092 case N32_OP6_SWI_BI:
7093 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7094 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7096 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7098 insn_type = NDS32_INSN_SWI333_BI;
7103 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7104 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7106 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7108 insn_type = NDS32_INSN_LHI333;
7113 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7114 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7116 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
7118 insn_type = NDS32_INSN_SHI333;
7123 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7124 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7126 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
7128 insn_type = NDS32_INSN_LBI333;
7133 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7134 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7136 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
7138 insn_type = NDS32_INSN_SBI333;
7143 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
7146 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
7149 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
7152 if ((insn & N32_BIT (14)) == 0)
7155 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7156 && N32_RT5 (insn) != REG_R5)
7157 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
7158 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7159 && N32_RA5 (insn) != REG_R5)
7160 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
7161 insn_type = NDS32_INSN_BEQS38;
7167 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7168 && N32_RT5 (insn) != REG_R5)
7169 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
7170 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7171 && N32_RA5 (insn) != REG_R5)
7172 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
7173 insn_type = NDS32_INSN_BNES38;
7179 switch (N32_BR2_SUB (insn))
7182 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7184 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
7185 insn_type = NDS32_INSN_BEQZ38;
7187 else if (N32_RT5 (insn) == REG_R15
7188 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7190 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
7191 insn_type = NDS32_INSN_BEQZS8;
7196 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7198 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
7199 insn_type = NDS32_INSN_BNEZ38;
7201 else if (N32_RT5 (insn) == REG_R15
7202 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7204 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
7205 insn_type = NDS32_INSN_BNEZS8;
7209 case N32_BR2_IFCALL:
7210 if (IS_WITHIN_U (N32_IMM16S (insn), 9))
7212 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
7213 insn_type = NDS32_INSN_IFCALL9;
7220 if ((insn & N32_BIT (24)) == 0)
7223 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
7225 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
7226 insn_type = NDS32_INSN_J8;
7232 if (__GF (insn, 8, 2) != 0)
7235 switch (N32_IMMU (insn, 5))
7238 if (N32_JREG_HINT (insn) == 0)
7241 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
7242 insn_type = NDS32_INSN_JR5;
7244 else if (N32_JREG_HINT (insn) == 1)
7247 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
7248 insn_type = NDS32_INSN_RET5;
7250 else if (N32_JREG_HINT (insn) == 3)
7252 /* ifret = mov55 $sp, $sp */
7253 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
7254 insn_type = NDS32_INSN_IFRET;
7259 /* It's convertible when return rt5 is $lp and address
7260 translation is kept. */
7261 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
7263 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
7264 insn_type = NDS32_INSN_JRAL5;
7271 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
7273 /* For v3, swid above 31 are used for ex9.it. */
7274 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
7275 insn_type = NDS32_INSN_BREAK16;
7280 /* This instruction has no 16-bit variant. */
7285 /* Bit-15 of insn16 should be set for a valid instruction. */
7286 if ((insn16 & 0x8000) == 0)
7292 *pinsn_type = insn_type;
7297 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
7298 Elf_Internal_Rela *reloc)
7300 uint16_t insn16 = 0;
7302 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
7303 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
7306 if (!N32_IS_RT3 (insn))
7309 switch (N32_OP6 (insn))
7312 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7313 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7316 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7317 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7320 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
7323 if (__GF (insn, 17, 3) == 6)
7324 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
7325 else if (__GF (insn, 17, 3) == 7)
7326 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
7330 if ((insn16 & 0x8000) == 0)
7337 /* Convert a 16-bit instruction to 32-bit one.
7338 INSN16 it the input and PINSN it the point to output.
7339 Return non-zero on successful. Otherwise 0 is returned. */
7342 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
7344 uint32_t insn = 0xffffffff;
7345 unsigned long mach = bfd_get_mach (abfd);
7347 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
7349 switch (__GF (insn16, 9, 6))
7351 case 0x4: /* add45 */
7352 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
7355 case 0x5: /* sub45 */
7356 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
7359 case 0x6: /* addi45 */
7360 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7361 N16_IMM5U (insn16));
7363 case 0x7: /* subi45 */
7364 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7365 -N16_IMM5U (insn16));
7367 case 0x8: /* srai45 */
7368 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
7369 N16_IMM5U (insn16));
7371 case 0x9: /* srli45 */
7372 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
7373 N16_IMM5U (insn16));
7375 case 0xa: /* slli333 */
7376 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
7377 N16_IMM3U (insn16));
7379 case 0xc: /* add333 */
7380 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
7383 case 0xd: /* sub333 */
7384 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
7387 case 0xe: /* addi333 */
7388 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7389 N16_IMM3U (insn16));
7391 case 0xf: /* subi333 */
7392 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7393 -N16_IMM3U (insn16));
7395 case 0x10: /* lwi333 */
7396 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
7397 N16_IMM3U (insn16));
7399 case 0x12: /* lhi333 */
7400 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
7401 N16_IMM3U (insn16));
7403 case 0x13: /* lbi333 */
7404 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
7405 N16_IMM3U (insn16));
7407 case 0x11: /* lwi333.bi */
7408 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7409 N16_IMM3U (insn16));
7411 case 0x14: /* swi333 */
7412 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
7413 N16_IMM3U (insn16));
7415 case 0x16: /* shi333 */
7416 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
7417 N16_IMM3U (insn16));
7419 case 0x17: /* sbi333 */
7420 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
7421 N16_IMM3U (insn16));
7423 case 0x15: /* swi333.bi */
7424 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7425 N16_IMM3U (insn16));
7427 case 0x18: /* addri36.sp */
7428 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
7429 N16_IMM6U (insn16) << 2);
7431 case 0x19: /* lwi45.fe */
7432 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
7433 (N16_IMM5U (insn16) - 32));
7435 case 0x1a: /* lwi450 */
7436 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7438 case 0x1b: /* swi450 */
7439 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7442 /* These are r15 implied instructions. */
7443 case 0x30: /* slts45 */
7444 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7446 case 0x31: /* slt45 */
7447 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7449 case 0x32: /* sltsi45 */
7450 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7452 case 0x33: /* slti45 */
7453 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7455 case 0x34: /* beqzs8, bnezs8 */
7456 if (insn16 & N32_BIT (8))
7457 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
7459 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
7462 case 0x35: /* break16, ex9.it */
7463 /* Only consider range of v3 break16. */
7464 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
7467 case 0x3c: /* ifcall9 */
7468 insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
7470 case 0x3d: /* movpi45 */
7471 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
7474 case 0x3f: /* MISC33 */
7475 switch (insn16 & 0x7)
7478 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7481 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
7485 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
7489 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
7493 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
7497 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
7504 switch (insn16 & 0x7)
7507 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
7510 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7513 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7516 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7518 case 4: /* xlsb33 */
7519 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
7521 case 5: /* x11b33 */
7522 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
7524 case 6: /* bmski33 */
7525 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7526 1 << __GF (insn16, 3, 3));
7528 case 7: /* fexti33 */
7529 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7530 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
7536 switch (__GF (insn16, 10, 5))
7538 case 0x0: /* mov55 or ifret16 */
7539 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
7540 && N16_RT5 (insn16) == N16_RA5 (insn16))
7541 insn = N32_JREG (JR, 0, 0, 0, 3);
7543 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
7545 case 0x1: /* movi55 */
7546 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
7548 case 0x1b: /* addi10s (V2) */
7549 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
7553 switch (__GF (insn16, 11, 4))
7555 case 0x7: /* lwi37.fp/swi37.fp */
7556 if (insn16 & N32_BIT (7)) /* swi37.fp */
7557 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7559 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7561 case 0x8: /* beqz38 */
7562 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7564 case 0x9: /* bnez38 */
7565 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7567 case 0xa: /* beqs38/j8, implied r5 */
7568 if (N16_RT38 (insn16) == 5)
7569 insn = N32_JI (J, N16_IMM8S (insn16));
7571 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7573 case 0xb: /* bnes38 and others */
7574 if (N16_RT38 (insn16) == 5)
7576 switch (__GF (insn16, 5, 3))
7579 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
7582 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
7585 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
7587 case 2: /* ex9.it imm5 */
7588 /* ex9.it had no 32-bit variantl. */
7590 case 5: /* add5.pc */
7591 /* add5.pc had no 32-bit variantl. */
7596 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7598 case 0xe: /* lwi37/swi37 */
7599 if (insn16 & (1 << 7)) /* swi37.sp */
7600 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7602 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7607 if (insn & 0x80000000)
7616 is_sda_access_insn (unsigned long insn)
7618 switch (N32_OP6 (insn))
7639 static unsigned long
7640 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
7646 case R_NDS32_GOT_LO12:
7647 case R_NDS32_GOTOFF_LO12:
7648 case R_NDS32_PLTREL_LO12:
7649 case R_NDS32_PLT_GOTREL_LO12:
7650 case R_NDS32_LO12S0_RELA:
7651 switch (N32_OP6 (insn))
7655 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
7659 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
7663 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
7667 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
7672 case R_NDS32_LO12S1_RELA:
7673 switch (N32_OP6 (insn))
7677 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
7681 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
7685 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
7690 case R_NDS32_LO12S2_RELA:
7691 switch (N32_OP6 (insn))
7695 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
7699 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
7704 case R_NDS32_LO12S2_DP_RELA:
7705 case R_NDS32_LO12S2_SP_RELA:
7706 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
7716 /* Linker hasn't found the correct merge section for non-section symbol
7717 in relax time, this work is left to the function elf_link_input_bfd().
7718 So for non-section symbol, _bfd_merged_section_offset is also needed
7719 to find the correct symbol address. */
7722 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
7723 asection **psec, Elf_Internal_Rela *rel)
7725 asection *sec = *psec;
7728 relocation = (sec->output_section->vma
7729 + sec->output_offset + sym->st_value);
7730 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
7732 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7734 _bfd_merged_section_offset (abfd, psec,
7735 elf_section_data (sec)->sec_info,
7736 sym->st_value + rel->r_addend);
7739 _bfd_merged_section_offset (abfd, psec,
7740 elf_section_data (sec)->sec_info,
7741 sym->st_value) + rel->r_addend;
7745 /* If we have changed the section, and our original section is
7746 marked with SEC_EXCLUDE, it means that the original
7747 SEC_MERGE section has been completely subsumed in some
7748 other SEC_MERGE section. In this case, we need to leave
7749 some info around for --emit-relocs. */
7750 if ((sec->flags & SEC_EXCLUDE) != 0)
7751 sec->kept_section = *psec;
7754 rel->r_addend -= relocation;
7755 rel->r_addend += sec->output_section->vma + sec->output_offset;
7761 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
7762 Elf_Internal_Sym *isymbuf,
7763 Elf_Internal_Shdr *symtab_hdr)
7765 bfd_signed_vma foff;
7766 bfd_vma symval, addend;
7767 Elf_Internal_Rela irel_fn;
7768 Elf_Internal_Sym *isym;
7771 /* Get the value of the symbol referred to by the reloc. */
7772 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7774 /* A local symbol. */
7775 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7777 if (isym->st_shndx == SHN_UNDEF)
7778 sym_sec = bfd_und_section_ptr;
7779 else if (isym->st_shndx == SHN_ABS)
7780 sym_sec = bfd_abs_section_ptr;
7781 else if (isym->st_shndx == SHN_COMMON)
7782 sym_sec = bfd_com_section_ptr;
7784 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7785 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
7786 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
7787 addend = irel_fn.r_addend;
7792 struct elf_link_hash_entry *h;
7794 /* An external symbol. */
7795 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7796 h = elf_sym_hashes (abfd)[indx];
7797 BFD_ASSERT (h != NULL);
7799 while (h->root.type == bfd_link_hash_indirect
7800 || h->root.type == bfd_link_hash_warning)
7801 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7803 if (h->root.type != bfd_link_hash_defined
7804 && h->root.type != bfd_link_hash_defweak)
7805 /* This appears to be a reference to an undefined
7806 symbol. Just ignore it--it will be caught by the
7807 regular reloc processing. */
7810 if (h->root.u.def.section->flags & SEC_MERGE)
7812 sym_sec = h->root.u.def.section;
7813 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
7814 (sym_sec)->sec_info, h->root.u.def.value);
7815 symval = symval + sym_sec->output_section->vma
7816 + sym_sec->output_offset;
7819 symval = (h->root.u.def.value
7820 + h->root.u.def.section->output_section->vma
7821 + h->root.u.def.section->output_offset);
7822 addend = irel->r_addend;
7825 foff = symval + addend;
7831 calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
7832 Elf_Internal_Rela *irel,
7833 Elf_Internal_Shdr *symtab_hdr)
7836 bfd_vma *local_got_offsets;
7837 /* Get the value of the symbol referred to by the reloc. */
7838 struct elf_link_hash_entry *h;
7839 struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);
7841 /* An external symbol. */
7842 symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7843 h = elf_sym_hashes (abfd)[symndx];
7844 while (h->root.type == bfd_link_hash_indirect
7845 || h->root.type == bfd_link_hash_warning)
7846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7850 BFD_ASSERT (h != NULL);
7851 return (htab->root.sgot->output_section->vma
7852 + htab->root.sgot->output_offset
7857 local_got_offsets = elf_local_got_offsets (abfd);
7858 BFD_ASSERT (local_got_offsets != NULL);
7859 return (htab->root.sgot->output_section->vma
7860 + htab->root.sgot->output_offset
7861 + local_got_offsets[ELF32_R_SYM (irel->r_info)]);
7864 /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?). */
7865 /* The check of h->root.type is passed. */
7869 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
7870 asection *sec, Elf_Internal_Rela *rel)
7873 unsigned short insn16;
7875 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
7877 contents = elf_section_data (sec)->this_hdr.contents;
7878 insn16 = bfd_getb16 (contents + rel->r_offset);
7879 if (insn16 == NDS32_NOP16)
7884 /* It checks whether the instruction could be converted to
7885 16-bit form and returns the converted one.
7887 `internal_relocs' is supposed to be sorted. */
7890 is_convert_32_to_16 (bfd *abfd, asection *sec,
7891 Elf_Internal_Rela *reloc,
7892 Elf_Internal_Rela *internal_relocs,
7893 Elf_Internal_Rela *irelend,
7896 #define NORMAL_32_TO_16 (1 << 0)
7897 #define SPECIAL_32_TO_16 (1 << 1)
7898 bfd_byte *contents = NULL;
7902 Elf_Internal_Rela *pc_rel;
7903 int pic_ext_target = 0;
7904 Elf_Internal_Shdr *symtab_hdr;
7905 Elf_Internal_Sym *isymbuf = NULL;
7909 if (reloc->r_offset + 4 > sec->size)
7912 offset = reloc->r_offset;
7914 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
7916 insn = bfd_getb32 (contents + offset);
7918 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
7919 convert_type = NORMAL_32_TO_16;
7920 else if (special_convert_32_to_16 (insn, insn16, reloc))
7921 convert_type = SPECIAL_32_TO_16;
7925 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7926 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
7929 /* Find the first relocation of the same relocation-type,
7930 so we iteratie them forward. */
7932 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
7935 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
7937 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
7938 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
7939 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
7940 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
7942 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
7944 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
7949 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
7951 /* movi => movi55 */
7952 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
7954 /* mem_addr is unsigned, but the value should
7955 be between [-16, 15]. */
7956 if ((mem_addr + 0x10) >> 5)
7960 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
7961 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
7963 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
7964 because it can be relaxed to addi for TLS_LE_ADD. */
7967 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
7968 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
7969 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
7970 && convert_type == SPECIAL_32_TO_16)
7973 We've selected a best fp-base for this access, so we can
7974 always resolve it anyway. Do nothing. */
7977 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
7978 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
7979 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
7980 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
7981 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
7982 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
7984 /* Prevent unresolved addi instruction translate
7985 to addi45 or addi333. */
7988 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
7990 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
7992 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8002 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8003 Elf_Internal_Rela *reloc,
8004 Elf_Internal_Rela *internal_relocs,
8005 Elf_Internal_Rela *irelend,
8006 unsigned short insn16)
8008 Elf_Internal_Rela *pc_rel;
8011 offset = reloc->r_offset;
8012 bfd_putb16 (insn16, contents + offset);
8013 /* Find the first relocation of the same relocation-type,
8014 so we iteratie them forward. */
8016 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8019 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8021 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8022 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8023 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8026 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8028 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8030 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8031 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8033 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8034 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8035 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8037 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8038 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8040 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8044 /* Find a relocation of type specified by `reloc_type'
8045 of the same r_offset with reloc.
8046 If not found, return irelend.
8048 Assuming relocations are sorted by r_offset,
8049 we find the relocation from `reloc' backward untill relocs,
8050 or find it from `reloc' forward untill irelend. */
8052 static Elf_Internal_Rela *
8053 find_relocs_at_address (Elf_Internal_Rela *reloc,
8054 Elf_Internal_Rela *relocs,
8055 Elf_Internal_Rela *irelend,
8056 enum elf_nds32_reloc_type reloc_type)
8058 Elf_Internal_Rela *rel_t;
8060 /* Find backward. */
8062 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8064 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8067 /* We didn't find it backward. Try find it forward. */
8069 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8071 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8077 /* Find a relocation of specified type and offset.
8078 `reloc' is just a refence point to find a relocation at specified offset.
8079 If not found, return irelend.
8081 Assuming relocations are sorted by r_offset,
8082 we find the relocation from `reloc' backward untill relocs,
8083 or find it from `reloc' forward untill irelend. */
8085 static Elf_Internal_Rela *
8086 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8087 Elf_Internal_Rela *relocs,
8088 Elf_Internal_Rela *irelend,
8089 enum elf_nds32_reloc_type reloc_type,
8092 Elf_Internal_Rela *rel_t = NULL;
8094 /* First, we try to find a relocation of offset `offset_p',
8095 and then we use find_relocs_at_address to find specific type. */
8097 if (reloc->r_offset > offset_p)
8099 /* Find backward. */
8101 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8104 else if (reloc->r_offset < offset_p)
8108 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8115 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8118 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8122 nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
8123 Elf_Internal_Rela *internal_relocs,
8124 Elf_Internal_Rela *irelend,
8125 unsigned char reloc_type)
8127 Elf_Internal_Rela *rel_t;
8130 rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
8132 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8134 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8135 && rel_t->r_addend == reloc->r_addend)
8140 for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8142 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8144 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8145 && rel_t->r_addend == reloc->r_addend)
8153 typedef struct nds32_elf_blank nds32_elf_blank_t;
8154 struct nds32_elf_blank
8156 /* Where the blank begins. */
8158 /* The size of the blank. */
8160 /* The accumulative size before this blank. */
8162 nds32_elf_blank_t *next;
8163 nds32_elf_blank_t *prev;
8166 static nds32_elf_blank_t *blank_free_list = NULL;
8168 static nds32_elf_blank_t *
8169 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8171 nds32_elf_blank_t *blank_t;
8173 if (blank_free_list)
8175 blank_t = blank_free_list;
8176 blank_free_list = blank_free_list->next;
8179 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
8181 if (blank_t == NULL)
8184 blank_t->offset = offset_p;
8185 blank_t->size = size_p;
8186 blank_t->total_size = 0;
8187 blank_t->next = NULL;
8188 blank_t->prev = NULL;
8194 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
8196 if (blank_free_list)
8198 blank_free_list->prev = blank_p;
8199 blank_p->next = blank_free_list;
8202 blank_p->next = NULL;
8204 blank_p->prev = NULL;
8205 blank_free_list = blank_p;
8209 clean_nds32_elf_blank (void)
8211 nds32_elf_blank_t *blank_t;
8213 while (blank_free_list)
8215 blank_t = blank_free_list;
8216 blank_free_list = blank_free_list->next;
8221 static nds32_elf_blank_t *
8222 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
8224 nds32_elf_blank_t *blank_t;
8230 while (blank_t && addr < blank_t->offset)
8231 blank_t = blank_t->prev;
8232 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
8233 blank_t = blank_t->next;
8239 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8242 nds32_elf_blank_t *blank_t;
8244 blank_t = search_nds32_elf_blank (*blank_p, addr);
8251 if (addr < blank_t->offset + blank_t->size)
8252 return blank_t->total_size + (addr - blank_t->offset);
8254 return blank_t->total_size + blank_t->size;
8258 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
8260 nds32_elf_blank_t *blank_t, *blank_t2;
8264 *blank_p = create_nds32_elf_blank (addr, len);
8265 return *blank_p ? TRUE : FALSE;
8268 blank_t = search_nds32_elf_blank (*blank_p, addr);
8270 if (blank_t == NULL)
8272 blank_t = create_nds32_elf_blank (addr, len);
8275 while ((*blank_p)->prev != NULL)
8276 *blank_p = (*blank_p)->prev;
8277 blank_t->next = *blank_p;
8278 (*blank_p)->prev = blank_t;
8279 (*blank_p) = blank_t;
8283 if (addr < blank_t->offset + blank_t->size)
8285 if (addr > blank_t->offset + blank_t->size)
8286 blank_t->size = addr - blank_t->offset;
8290 blank_t2 = create_nds32_elf_blank (addr, len);
8295 blank_t->next->prev = blank_t2;
8296 blank_t2->next = blank_t->next;
8298 blank_t2->prev = blank_t;
8299 blank_t->next = blank_t2;
8300 *blank_p = blank_t2;
8307 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8310 nds32_elf_blank_t *blank_t;
8312 if (!insert_nds32_elf_blank (blank_p, addr, len))
8319 blank_t->total_size = 0;
8320 blank_t = blank_t->next;
8325 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
8326 blank_t = blank_t->next;
8333 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
8335 nds32_elf_blank_t *blank_t;
8336 bfd_vma total_size = 0;
8342 while (blank_t->prev)
8343 blank_t = blank_t->prev;
8346 blank_t->total_size = total_size;
8347 total_size += blank_t->size;
8348 blank_t = blank_t->next;
8353 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
8354 nds32_elf_blank_t *blank_p)
8356 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
8357 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
8358 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
8359 unsigned int sec_shndx; /* The section the be relaxed. */
8360 bfd_byte *contents; /* Contents data of iterating section. */
8361 Elf_Internal_Rela *internal_relocs;
8362 Elf_Internal_Rela *irel;
8363 Elf_Internal_Rela *irelend;
8364 struct elf_link_hash_entry **sym_hashes;
8365 struct elf_link_hash_entry **end_hashes;
8366 unsigned int symcount;
8368 nds32_elf_blank_t *blank_t;
8369 nds32_elf_blank_t *blank_t2;
8370 nds32_elf_blank_t *blank_head;
8372 blank_head = blank_t = blank_p;
8373 while (blank_head->prev != NULL)
8374 blank_head = blank_head->prev;
8375 while (blank_t->next != NULL)
8376 blank_t = blank_t->next;
8378 if (blank_t->offset + blank_t->size <= sec->size)
8380 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
8381 blank_t->next->prev = blank_t;
8383 if (blank_head->offset > 0)
8385 blank_head->prev = create_nds32_elf_blank (0, 0);
8386 blank_head->prev->next = blank_head;
8387 blank_head = blank_head->prev;
8390 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8392 /* The deletion must stop at the next ALIGN reloc for an alignment
8393 power larger than the number of bytes we are deleting. */
8395 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8396 if (!nds32_get_local_syms (abfd, sec, &isym))
8401 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8402 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
8403 symtab_hdr->contents = (bfd_byte *) isym;
8406 if (isym == NULL || symtab_hdr->sh_info == 0)
8409 blank_t = blank_head;
8410 calc_nds32_blank_total (blank_head);
8412 for (sect = abfd->sections; sect != NULL; sect = sect->next)
8414 /* Adjust all the relocs. */
8416 /* Relocations MUST be kept in memory, because relaxation adjust them. */
8417 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
8418 TRUE /* keep_memory */);
8419 irelend = internal_relocs + sect->reloc_count;
8421 blank_t = blank_head;
8422 blank_t2 = blank_head;
8424 if (!(sect->flags & SEC_RELOC))
8427 nds32_get_section_contents (abfd, sect, &contents, TRUE);
8429 for (irel = internal_relocs; irel < irelend; irel++)
8433 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
8434 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
8435 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8437 unsigned long val = 0;
8439 long before, between;
8442 switch (ELF32_R_TYPE (irel->r_info))
8445 offset = bfd_get_8 (abfd, contents + irel->r_offset);
8447 case R_NDS32_DIFF16:
8448 offset = bfd_get_16 (abfd, contents + irel->r_offset);
8450 case R_NDS32_DIFF32:
8451 val = bfd_get_32 (abfd, contents + irel->r_offset);
8452 /* Get the signed bit and mask for the high part. The
8453 gcc will alarm when right shift 32-bit since the
8454 type size of long may be 32-bit. */
8455 mask = 0 - (val >> 31);
8457 offset = (val | (mask - 0xffffffff));
8466 0 |encoded in location|
8467 |------------|-------------------|---------
8469 -- before ---| *****************
8470 --------------------- between ---|
8472 We only care how much data are relax between DIFF,
8475 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8476 between = get_nds32_elf_blank_total (&blank_t,
8477 irel->r_addend + offset, 0);
8478 if (between == before)
8479 goto done_adjust_diff;
8481 switch (ELF32_R_TYPE (irel->r_info))
8484 bfd_put_8 (abfd, offset - (between - before),
8485 contents + irel->r_offset);
8487 case R_NDS32_DIFF16:
8488 bfd_put_16 (abfd, offset - (between - before),
8489 contents + irel->r_offset);
8491 case R_NDS32_DIFF32:
8492 bfd_put_32 (abfd, offset - (between - before),
8493 contents + irel->r_offset);
8497 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
8498 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8501 unsigned int len = 0;
8502 unsigned long before, between;
8505 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
8508 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8509 between = get_nds32_elf_blank_total (&blank_t,
8510 irel->r_addend + val, 0);
8511 if (between == before)
8512 goto done_adjust_diff;
8514 p = contents + irel->r_offset;
8516 memset (p, 0x80, len);
8518 p = write_uleb128 (p, val - (between - before)) - 1;
8526 raddr = irel->r_offset;
8527 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
8530 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
8532 if (blank_t2 && blank_t2->next
8533 && (blank_t2->offset > raddr
8534 || blank_t2->next->offset <= raddr))
8536 (_("%pB: Error: search_nds32_elf_blank reports wrong node\n"), abfd);
8538 /* Mark reloc in deleted portion as NONE.
8539 For some relocs like R_NDS32_LABEL that doesn't modify the
8540 content in the section. R_NDS32_LABEL doesn't belong to the
8541 instruction in the section, so we should preserve it. */
8542 if (raddr >= blank_t2->offset
8543 && raddr < blank_t2->offset + blank_t2->size
8544 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
8545 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
8546 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
8547 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
8548 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
8549 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
8551 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
8557 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
8558 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
8559 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
8562 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
8563 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
8564 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
8566 if (irel->r_addend <= sec->size)
8568 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
8573 /* Adjust the local symbols defined in this section. */
8574 blank_t = blank_head;
8575 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
8577 if (isym->st_shndx == sec_shndx)
8579 if (isym->st_value <= sec->size)
8582 bfd_vma orig_addr = isym->st_value;
8584 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
8585 isym->st_value -= ahead;
8587 /* Adjust function size. */
8588 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
8589 && isym->st_size > 0)
8591 get_nds32_elf_blank_total
8592 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
8597 /* Now adjust the global symbols defined in this section. */
8598 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
8599 - symtab_hdr->sh_info);
8600 sym_hashes = elf_sym_hashes (abfd);
8601 end_hashes = sym_hashes + symcount;
8602 blank_t = blank_head;
8603 for (; sym_hashes < end_hashes; sym_hashes++)
8605 struct elf_link_hash_entry *sym_hash = *sym_hashes;
8607 if ((sym_hash->root.type == bfd_link_hash_defined
8608 || sym_hash->root.type == bfd_link_hash_defweak)
8609 && sym_hash->root.u.def.section == sec)
8611 if (sym_hash->root.u.def.value <= sec->size)
8614 bfd_vma orig_addr = sym_hash->root.u.def.value;
8616 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
8617 sym_hash->root.u.def.value -= ahead;
8619 /* Adjust function size. */
8620 if (sym_hash->type == STT_FUNC)
8622 get_nds32_elf_blank_total
8623 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
8629 contents = elf_section_data (sec)->this_hdr.contents;
8630 blank_t = blank_head;
8631 while (blank_t->next)
8633 /* Actually delete the bytes. */
8635 /* If current blank is the last blank overlap with current section,
8636 go to finish process. */
8637 if (sec->size <= (blank_t->next->offset))
8640 memmove (contents + blank_t->offset - blank_t->total_size,
8641 contents + blank_t->offset + blank_t->size,
8642 blank_t->next->offset - (blank_t->offset + blank_t->size));
8644 blank_t = blank_t->next;
8647 if (sec->size > (blank_t->offset + blank_t->size))
8649 /* There are remaining code between blank and section boundary.
8650 Move the remaining code to appropriate location. */
8651 memmove (contents + blank_t->offset - blank_t->total_size,
8652 contents + blank_t->offset + blank_t->size,
8653 sec->size - (blank_t->offset + blank_t->size));
8654 sec->size -= blank_t->total_size + blank_t->size;
8657 /* This blank is not entirely included in the section,
8658 reduce the section size by only part of the blank size. */
8659 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
8663 blank_t = blank_head;
8664 blank_head = blank_head->next;
8665 remove_nds32_elf_blank (blank_t);
8671 /* Get the contents of a section. */
8674 nds32_get_section_contents (bfd *abfd, asection *sec,
8675 bfd_byte **contents_p, bfd_boolean cache)
8677 /* Get the section contents. */
8678 if (elf_section_data (sec)->this_hdr.contents != NULL)
8679 *contents_p = elf_section_data (sec)->this_hdr.contents;
8682 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
8685 elf_section_data (sec)->this_hdr.contents = *contents_p;
8691 /* Get the contents of the internal symbol of abfd. */
8694 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
8695 Elf_Internal_Sym **isymbuf_p)
8697 Elf_Internal_Shdr *symtab_hdr;
8698 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8700 /* Read this BFD's local symbols if we haven't done so already. */
8701 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
8703 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
8704 if (*isymbuf_p == NULL)
8706 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8707 symtab_hdr->sh_info, 0,
8709 if (*isymbuf_p == NULL)
8713 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
8718 /* Range of small data. */
8719 static bfd_vma sdata_range[2][2];
8720 static bfd_vma const sdata_init_range[2] =
8721 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
8724 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
8725 bfd_byte *contents, bfd_vma addr)
8727 unsigned long insn = bfd_getb32 (contents + addr);
8729 if (insn & 0x80000000)
8735 /* Set the gp relax range. We have to measure the safe range
8736 to do gp relaxation. */
8739 relax_range_measurement (bfd *abfd)
8741 asection *sec_f, *sec_b;
8742 /* For upper bound. */
8743 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
8745 static int decide_relax_range = 0;
8747 int range_number = sizeof (sdata_init_range) / sizeof (sdata_init_range[0]);
8749 if (decide_relax_range)
8751 decide_relax_range = 1;
8753 if (sda_rela_sec == NULL)
8755 /* Since there is no data sections, we assume the range is page size. */
8756 for (i = 0; i < range_number; i++)
8758 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
8759 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
8764 /* Get the biggest alignment power after the gp located section. */
8765 sec_f = sda_rela_sec->output_section;
8766 sec_b = sec_f->next;
8768 while (sec_b != NULL)
8770 if ((unsigned)(1 << sec_b->alignment_power) > align)
8771 align = (1 << sec_b->alignment_power);
8772 sec_b = sec_b->next;
8775 /* I guess we can not determine the section before
8776 gp located section, so we assume the align is max page size. */
8777 for (i = 0; i < range_number; i++)
8779 sdata_range[i][1] = sdata_init_range[i] - align;
8780 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
8781 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
8782 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
8786 /* These are macros used to check flags encoded in r_addend.
8787 They are only used by nds32_elf_relax_section (). */
8788 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
8789 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
8790 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
8791 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
8793 static const char * unrecognized_reloc_msg =
8794 /* xgettext:c-format */
8795 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
8797 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
8800 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
8801 Elf_Internal_Rela *internal_relocs, int *insn_len,
8802 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
8803 Elf_Internal_Shdr *symtab_hdr)
8805 /* There are 3 variations for LONGCALL1
8806 case 4-4-2; 16-bit on, optimize off or optimize for space
8807 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8808 ori ta, ta, lo12(symbol) ; LO12S0
8811 case 4-4-4; 16-bit off, optimize don't care
8812 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8813 ori ta, ta, lo12(symbol) ; LO12S0
8816 case 4-4-4; 16-bit on, optimize for speed
8817 sethi ta, hi20(symbol) ; LONGCALL1/HI20
8818 ori ta, ta, lo12(symbol) ; LO12S0
8820 Check code for -mlong-calls output. */
8822 /* Get the reloc for the address from which the register is
8823 being loaded. This reloc will tell us which function is
8824 actually being called. */
8827 int seq_len; /* Original length of instruction sequence. */
8829 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
8830 int pic_ext_target = 0;
8831 bfd_signed_vma foff;
8834 irelend = internal_relocs + sec->reloc_count;
8835 seq_len = GET_SEQ_LEN (irel->r_addend);
8836 laddr = irel->r_offset;
8837 *insn_len = seq_len;
8839 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8840 R_NDS32_HI20_RELA, laddr);
8841 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8842 R_NDS32_LO12S0_ORI_RELA,
8845 if (hi_irelfn == irelend || lo_irelfn == irelend)
8847 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
8848 (uint64_t) irel->r_offset);
8852 /* Get the value of the symbol referred to by the reloc. */
8853 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
8856 /* This condition only happened when symbol is undefined. */
8857 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
8858 || foff >= CONSERVATIVE_24BIT_S1)
8861 /* Relax to: jal symbol; 25_PCREL */
8862 /* For simplicity of coding, we are going to modify the section
8863 contents, the section relocs, and the BFD symbol table. We
8864 must tell the rest of the code not to free up this
8865 information. It would be possible to instead create a table
8866 of changes which have to be made, as is done in coff-mips.c;
8867 that would be more work, but would require less memory when
8868 the linker is run. */
8870 /* Replace the long call with a jal. */
8871 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8872 R_NDS32_25_PCREL_RELA);
8873 irel->r_addend = hi_irelfn->r_addend;
8875 /* We don't resolve this here but resolve it in relocate_section. */
8877 bfd_putb32 (insn, contents + irel->r_offset);
8880 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
8882 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
8887 insn16 = NDS32_NOP16;
8888 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
8890 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
8891 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
8897 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
8898 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
8901 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
8902 Elf_Internal_Rela *internal_relocs, int *insn_len,
8903 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
8904 Elf_Internal_Shdr *symtab_hdr)
8906 /* bltz rt, .L1 ; LONGCALL2
8907 jal symbol ; 25_PCREL
8910 /* Get the reloc for the address from which the register is
8911 being loaded. This reloc will tell us which function is
8912 actually being called. */
8916 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
8917 int pic_ext_target = 0;
8918 bfd_signed_vma foff;
8920 irelend = internal_relocs + sec->reloc_count;
8921 laddr = irel->r_offset;
8923 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8924 R_NDS32_25_PCREL_RELA, laddr + 4);
8926 if (i1_irelfn == irelend)
8928 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
8929 (uint64_t) irel->r_offset);
8933 insn = bfd_getb32 (contents + laddr);
8935 /* Get the value of the symbol referred to by the reloc. */
8936 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
8939 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
8940 || foff >= CONSERVATIVE_16BIT_S1)
8943 /* Relax to bgezal rt, label ; 17_PCREL
8944 or bltzal rt, label ; 17_PCREL */
8946 /* Convert to complimentary conditional call. */
8947 insn = CONVERT_CONDITION_CALL (insn);
8949 /* For simplicity of coding, we are going to modify the section
8950 contents, the section relocs, and the BFD symbol table. We
8951 must tell the rest of the code not to free up this
8952 information. It would be possible to instead create a table
8953 of changes which have to be made, as is done in coff-mips.c;
8954 that would be more work, but would require less memory when
8955 the linker is run. */
8957 /* Clean unnessary relocations. */
8959 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
8961 find_relocs_at_address_addr (irel, internal_relocs, irelend,
8962 R_NDS32_17_PCREL_RELA, laddr);
8963 if (cond_irelfn != irelend)
8964 cond_irelfn->r_info =
8965 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
8967 /* Replace the long call with a bgezal. */
8968 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
8969 R_NDS32_17_PCREL_RELA);
8970 irel->r_addend = i1_irelfn->r_addend;
8972 bfd_putb32 (insn, contents + irel->r_offset);
8978 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
8981 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
8982 Elf_Internal_Rela *internal_relocs, int *insn_len,
8983 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
8984 Elf_Internal_Shdr *symtab_hdr)
8986 /* There are 3 variations for LONGCALL3
8987 case 4-4-4-2; 16-bit on, optimize off or optimize for space
8988 bltz rt, $1 ; LONGCALL3
8989 sethi ta, hi20(symbol) ; HI20
8990 ori ta, ta, lo12(symbol) ; LO12S0
8994 case 4-4-4-4; 16-bit off, optimize don't care
8995 bltz rt, $1 ; LONGCALL3
8996 sethi ta, hi20(symbol) ; HI20
8997 ori ta, ta, lo12(symbol) ; LO12S0
9001 case 4-4-4-4; 16-bit on, optimize for speed
9002 bltz rt, $1 ; LONGCALL3
9003 sethi ta, hi20(symbol) ; HI20
9004 ori ta, ta, lo12(symbol) ; LO12S0
9008 /* Get the reloc for the address from which the register is
9009 being loaded. This reloc will tell us which function is
9010 actually being called. */
9013 int seq_len; /* Original length of instruction sequence. */
9015 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9016 int pic_ext_target = 0;
9017 bfd_signed_vma foff;
9020 irelend = internal_relocs + sec->reloc_count;
9021 seq_len = GET_SEQ_LEN (irel->r_addend);
9022 laddr = irel->r_offset;
9023 *insn_len = seq_len;
9026 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9027 R_NDS32_HI20_RELA, laddr + 4);
9029 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9030 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9032 if (hi_irelfn == irelend || lo_irelfn == irelend)
9034 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9035 (uint64_t) irel->r_offset);
9039 /* Get the value of the symbol referred to by the reloc. */
9040 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9043 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9044 || foff >= CONSERVATIVE_24BIT_S1)
9047 insn = bfd_getb32 (contents + laddr);
9048 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9050 /* Relax to bgezal rt, label ; 17_PCREL
9051 or bltzal rt, label ; 17_PCREL */
9053 /* Convert to complimentary conditional call. */
9054 insn = CONVERT_CONDITION_CALL (insn);
9055 bfd_putb32 (insn, contents + irel->r_offset);
9059 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9061 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9063 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9066 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9067 R_NDS32_17_PCREL_RELA, laddr);
9068 if (cond_irelfn != irelend)
9070 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9071 R_NDS32_17_PCREL_RELA);
9072 cond_irelfn->r_addend = hi_irelfn->r_addend;
9077 insn16 = NDS32_NOP16;
9078 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9079 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9081 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9085 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9087 /* Relax to the following instruction sequence
9088 bltz rt, $1 ; LONGCALL2
9089 jal symbol ; 25_PCREL
9093 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9095 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9096 R_NDS32_25_PCREL_RELA);
9098 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9101 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9105 insn16 = NDS32_NOP16;
9106 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9108 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9109 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9116 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
9119 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9120 Elf_Internal_Rela *internal_relocs, int *insn_len,
9121 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9122 Elf_Internal_Shdr *symtab_hdr)
9124 /* There are 3 variations for LONGJUMP1
9125 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9126 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9127 ori ta, ta, lo12(symbol) ; LO12S0
9130 case 4-4-4; 16-bit off, optimize don't care
9131 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9132 ori ta, ta, lo12(symbol) ; LO12S0
9135 case 4-4-4; 16-bit on, optimize for speed
9136 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9137 ori ta, ta, lo12(symbol) ; LO12S0
9140 /* Get the reloc for the address from which the register is
9141 being loaded. This reloc will tell us which function is
9142 actually being called. */
9145 int seq_len; /* Original length of instruction sequence. */
9146 int insn16_on; /* 16-bit on/off. */
9148 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9149 int pic_ext_target = 0;
9150 bfd_signed_vma foff;
9152 unsigned long reloc;
9154 irelend = internal_relocs + sec->reloc_count;
9155 seq_len = GET_SEQ_LEN (irel->r_addend);
9156 laddr = irel->r_offset;
9157 *insn_len = seq_len;
9158 insn16_on = IS_16BIT_ON (irel->r_addend);
9161 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9162 R_NDS32_HI20_RELA, laddr);
9164 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9165 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9166 if (hi_irelfn == irelend || lo_irelfn == irelend)
9168 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
9169 (uint64_t) irel->r_offset);
9173 /* Get the value of the symbol referred to by the reloc. */
9174 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9177 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
9178 || foff < -CONSERVATIVE_24BIT_S1)
9181 if (insn16_on && foff >= -ACCURATE_8BIT_S1
9182 && foff < ACCURATE_8BIT_S1 && (seq_len & 0x2))
9185 /* 16-bit on, but not optimized for speed. */
9186 reloc = R_NDS32_9_PCREL_RELA;
9188 bfd_putb16 (insn16, contents + irel->r_offset);
9191 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9196 reloc = R_NDS32_25_PCREL_RELA;
9198 bfd_putb32 (insn, contents + irel->r_offset);
9201 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9206 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9208 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9210 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
9212 insn16 = NDS32_NOP16;
9213 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9215 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9217 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9223 /* Revert condition branch. This function does not check if the input
9224 instruction is condition branch or not. */
9227 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
9228 uint16_t *re_insn16, uint32_t *re_insn)
9230 uint32_t comp_insn = 0;
9231 uint16_t comp_insn16 = 0;
9235 if (N32_OP6 (insn) == N32_OP6_BR1)
9238 comp_insn = (insn ^ 0x4000) & 0xffffc000;
9239 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
9241 /* Insn can be contracted to 16-bit implied r5. */
9243 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
9244 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9247 else if (N32_OP6 (insn) == N32_OP6_BR3)
9249 /* bnec $ta, imm11, label. */
9250 comp_insn = (insn ^ 0x80000) & 0xffffff00;
9254 comp_insn = (insn ^ 0x10000) & 0xffffc000;
9255 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
9256 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
9258 if (N32_IS_RT3 (insn))
9260 /* Insn can be contracted to 16-bit. */
9262 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
9263 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9265 else if (N32_RT5 (insn) == REG_R15)
9267 /* Insn can be contracted to 16-bit. */
9269 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
9276 switch ((insn16 & 0xf000) >> 12)
9279 /* beqz38 or bnez38 */
9280 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9281 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
9282 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
9286 /* beqs38 or bnes38 */
9287 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9288 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
9289 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
9294 /* beqzS8 or bnezS8 */
9295 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
9296 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
9297 comp_insn |= REG_R15 << 20;
9304 if (comp_insn && re_insn)
9305 *re_insn = comp_insn;
9306 if (comp_insn16 && re_insn16)
9307 *re_insn16 = comp_insn16;
9310 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
9313 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9314 Elf_Internal_Rela *internal_relocs, int *insn_len,
9315 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9316 Elf_Internal_Shdr *symtab_hdr)
9318 /* There are 3 variations for LONGJUMP2
9319 case 2-4; 1st insn convertible, 16-bit on,
9320 optimize off or optimize for space
9321 bnes38 rt, ra, $1 ; LONGJUMP2
9325 case 4-4; 1st insn not convertible
9326 bne rt, ra, $1 ; LONGJUMP2
9330 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9331 bne rt, ra, $1 ; LONGJUMP2
9335 /* Get the reloc for the address from which the register is
9336 being loaded. This reloc will tell us which function is
9337 actually being called. */
9340 int seq_len; /* Original length of instruction sequence. */
9341 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
9342 int pic_ext_target = 0, first_size;
9344 bfd_signed_vma foff;
9345 uint32_t insn, re_insn = 0;
9346 uint16_t insn16, re_insn16 = 0;
9347 unsigned long reloc, cond_reloc;
9349 enum elf_nds32_reloc_type checked_types[] =
9350 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9352 irelend = internal_relocs + sec->reloc_count;
9353 seq_len = GET_SEQ_LEN (irel->r_addend);
9354 laddr = irel->r_offset;
9355 *insn_len = seq_len;
9356 first_size = (seq_len == 6) ? 2 : 4;
9359 find_relocs_at_address_addr (irel, internal_relocs,
9360 irelend, R_NDS32_25_PCREL_RELA,
9361 laddr + first_size);
9363 for (i = 0; i < sizeof (checked_types) / sizeof(checked_types[0]); i++)
9366 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9367 checked_types[i], laddr);
9368 if (cond_irelfn != irelend)
9372 if (i2_irelfn == irelend || cond_irelfn == irelend)
9374 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
9375 (uint64_t) irel->r_offset);
9379 /* Get the value of the symbol referred to by the reloc. */
9381 calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
9383 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9384 || foff >= CONSERVATIVE_16BIT_S1)
9387 /* Get the all corresponding instructions. */
9388 if (first_size == 4)
9390 insn = bfd_getb32 (contents + laddr);
9391 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9395 insn16 = bfd_getb16 (contents + laddr);
9396 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9399 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
9400 && foff < ACCURATE_8BIT_S1 - first_size)
9402 if (first_size == 4)
9404 /* Don't convert it to 16-bit now, keep this as relaxable for
9405 ``label reloc; INSN16''. */
9407 /* Save comp_insn32 to buffer. */
9408 bfd_putb32 (re_insn, contents + irel->r_offset);
9410 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9411 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9412 cond_reloc = R_NDS32_INSN16;
9416 bfd_putb16 (re_insn16, contents + irel->r_offset);
9418 reloc = R_NDS32_9_PCREL_RELA;
9419 cond_reloc = R_NDS32_NONE;
9422 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9423 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9424 && foff < ACCURATE_14BIT_S1 - first_size))
9426 /* beqs label ; 15_PCREL */
9427 bfd_putb32 (re_insn, contents + irel->r_offset);
9429 reloc = R_NDS32_15_PCREL_RELA;
9430 cond_reloc = R_NDS32_NONE;
9432 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9433 && foff >= -CONSERVATIVE_16BIT_S1
9434 && foff < CONSERVATIVE_16BIT_S1)
9436 /* beqz label ; 17_PCREL */
9437 bfd_putb32 (re_insn, contents + irel->r_offset);
9439 reloc = R_NDS32_17_PCREL_RELA;
9440 cond_reloc = R_NDS32_NONE;
9445 /* Set all relocations. */
9446 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
9447 irel->r_addend = i2_irelfn->r_addend;
9449 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9451 cond_irelfn->r_addend = 0;
9453 if ((seq_len ^ *insn_len ) & 0x2)
9455 insn16 = NDS32_NOP16;
9456 bfd_putb16 (insn16, contents + irel->r_offset + 4);
9457 i2_irelfn->r_offset = 4;
9458 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9460 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9464 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9469 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
9472 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9473 Elf_Internal_Rela *internal_relocs, int *insn_len,
9474 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9475 Elf_Internal_Shdr *symtab_hdr)
9477 /* There are 5 variations for LONGJUMP3
9478 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
9479 optimize off or optimize for space
9480 bnes38 rt, ra, $1 ; LONGJUMP3
9481 sethi ta, hi20(symbol) ; HI20
9482 ori ta, ta, lo12(symbol) ; LO12S0
9486 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
9487 bnes38 rt, ra, $1 ; LONGJUMP3
9488 sethi ta, hi20(symbol) ; HI20
9489 ori ta, ta, lo12(symbol) ; LO12S0
9493 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
9494 optimize off or optimize for space
9495 bne rt, ra, $1 ; LONGJUMP3
9496 sethi ta, hi20(symbol) ; HI20
9497 ori ta, ta, lo12(symbol) ; LO12S0
9501 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
9502 16-bit off if no INSN16
9503 bne rt, ra, $1 ; LONGJUMP3
9504 sethi ta, hi20(symbol) ; HI20
9505 ori ta, ta, lo12(symbol) ; LO12S0
9509 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
9510 16-bit off if no INSN16
9511 bne rt, ra, $1 ; LONGJUMP3
9512 sethi ta, hi20(symbol) ; HI20
9513 ori ta, ta, lo12(symbol) ; LO12S0
9517 /* Get the reloc for the address from which the register is
9518 being loaded. This reloc will tell us which function is
9519 actually being called. */
9520 enum elf_nds32_reloc_type checked_types[] =
9521 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9523 int reloc_off = 0, cond_removed = 0, convertible;
9525 int seq_len; /* Original length of instruction sequence. */
9526 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9527 int pic_ext_target = 0, first_size;
9529 bfd_signed_vma foff;
9530 uint32_t insn, re_insn = 0;
9531 uint16_t insn16, re_insn16 = 0;
9532 unsigned long reloc, cond_reloc;
9534 irelend = internal_relocs + sec->reloc_count;
9535 seq_len = GET_SEQ_LEN (irel->r_addend);
9536 laddr = irel->r_offset;
9537 *insn_len = seq_len;
9539 convertible = IS_1ST_CONVERT (irel->r_addend);
9546 /* Get all needed relocations. */
9548 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9549 R_NDS32_HI20_RELA, laddr + first_size);
9551 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9552 R_NDS32_LO12S0_ORI_RELA,
9553 laddr + first_size + 4);
9555 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
9558 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9559 checked_types[i], laddr);
9560 if (cond_irelfn != irelend)
9564 if (hi_irelfn == irelend || lo_irelfn == irelend || cond_irelfn == irelend)
9566 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
9567 (uint64_t) irel->r_offset);
9571 /* Get the value of the symbol referred to by the reloc. */
9572 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9575 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9576 || foff >= CONSERVATIVE_24BIT_S1)
9579 /* Get the all corresponding instructions. */
9580 if (first_size == 4)
9582 insn = bfd_getb32 (contents + laddr);
9583 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9587 insn16 = bfd_getb16 (contents + laddr);
9588 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9591 /* For simplicity of coding, we are going to modify the section
9592 contents, the section relocs, and the BFD symbol table. We
9593 must tell the rest of the code not to free up this
9594 information. It would be possible to instead create a table
9595 of changes which have to be made, as is done in coff-mips.c;
9596 that would be more work, but would require less memory when
9597 the linker is run. */
9599 if (re_insn16 && foff >= -ACCURATE_8BIT_S1 - first_size
9600 && foff < ACCURATE_8BIT_S1 - first_size)
9602 if (!(seq_len & 0x2))
9604 /* Don't convert it to 16-bit now, keep this as relaxable
9605 for ``label reloc; INSN1a''6. */
9606 /* Save comp_insn32 to buffer. */
9607 bfd_putb32 (re_insn, contents + irel->r_offset);
9609 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9610 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9611 cond_reloc = R_NDS32_INSN16;
9615 /* Not optimize for speed; convert sequence to 16-bit. */
9616 /* Save comp_insn16 to buffer. */
9617 bfd_putb16 (re_insn16, contents + irel->r_offset);
9619 reloc = R_NDS32_9_PCREL_RELA;
9620 cond_reloc = R_NDS32_NONE;
9624 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9625 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9626 && foff < ACCURATE_14BIT_S1 - first_size))
9628 /* beqs label ; 15_PCREL */
9629 bfd_putb32 (re_insn, contents + irel->r_offset);
9631 reloc = R_NDS32_15_PCREL_RELA;
9632 cond_reloc = R_NDS32_NONE;
9635 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9636 && foff >= -CONSERVATIVE_16BIT_S1
9637 && foff < CONSERVATIVE_16BIT_S1)
9639 /* beqz label ; 17_PCREL */
9640 bfd_putb32 (re_insn, contents + irel->r_offset);
9642 reloc = R_NDS32_17_PCREL_RELA;
9643 cond_reloc = R_NDS32_NONE;
9646 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
9647 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
9649 /* Relax to one of the following 3 variations
9651 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
9653 bnes38 rt, $1 ; LONGJUMP2
9657 case 4-4; 1st insn not convertible, others don't care
9658 bne rt, ra, $1 ; LONGJUMP2
9662 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9663 bne rt, ra, $1 ; LONGJUMP2
9667 /* Offset for first instruction. */
9669 /* Use j label as second instruction. */
9670 *insn_len = 4 + first_size;
9672 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9673 reloc = R_NDS32_LONGJUMP2;
9674 cond_reloc = R_NDS32_25_PLTREL;
9679 if (cond_removed == 1)
9681 /* Set all relocations. */
9682 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9683 irel->r_addend = hi_irelfn->r_addend;
9685 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9687 cond_irelfn->r_addend = 0;
9688 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9693 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
9694 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9698 if ((seq_len ^ *insn_len ) & 0x2)
9700 insn16 = NDS32_NOP16;
9701 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9702 lo_irelfn->r_offset = *insn_len;
9703 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9705 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9709 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9714 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
9717 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9718 Elf_Internal_Rela *internal_relocs, int *insn_len,
9719 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9720 Elf_Internal_Shdr *symtab_hdr)
9722 /* The pattern for LONGCALL4. Support for function cse.
9723 sethi ta, hi20(symbol) ; LONGCALL4/HI20
9724 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
9725 jral ta ; PTR_RES/EMPTY/INSN16 */
9729 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
9730 Elf_Internal_Rela *irelend;
9731 int pic_ext_target = 0;
9732 bfd_signed_vma foff;
9734 irelend = internal_relocs + sec->reloc_count;
9735 laddr = irel->r_offset;
9737 /* Get the reloc for the address from which the register is
9738 being loaded. This reloc will tell us which function is
9739 actually being called. */
9740 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9741 R_NDS32_HI20_RELA, laddr);
9743 if (hi_irel == irelend)
9745 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9746 (uint64_t) irel->r_offset);
9750 /* Get the value of the symbol referred to by the reloc. */
9751 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
9754 /* This condition only happened when symbol is undefined. */
9755 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9756 || foff >= CONSERVATIVE_24BIT_S1)
9759 /* Relax to: jal symbol; 25_PCREL */
9760 /* For simplicity of coding, we are going to modify the section
9761 contents, the section relocs, and the BFD symbol table. We
9762 must tell the rest of the code not to free up this
9763 information. It would be possible to instead create a table
9764 of changes which have to be made, as is done in coff-mips.c;
9765 that would be more work, but would require less memory when
9766 the linker is run. */
9768 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9769 R_NDS32_PTR_RESOLVED, irel->r_addend);
9770 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9771 R_NDS32_EMPTY, irel->r_addend);
9773 if (ptr_irel == irelend || em_irel == irelend)
9775 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9776 (uint64_t) irel->r_offset);
9779 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
9780 insn = bfd_getb32 (contents + irel->r_addend);
9781 if (insn & 0x80000000)
9784 /* Replace the long call with a jal. */
9785 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
9786 R_NDS32_25_PCREL_RELA);
9787 ptr_irel->r_addend = 1;
9789 /* We don't resolve this here but resolve it in relocate_section. */
9791 bfd_putb32 (insn, contents + em_irel->r_offset);
9794 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9796 /* If there is function cse, HI20 can not remove now. */
9797 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9798 R_NDS32_LONGCALL4, laddr);
9799 if (call_irel == irelend)
9803 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
9806 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9807 R_NDS32_INSN16, irel->r_addend);
9808 if (insn_irel != irelend)
9810 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9815 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
9818 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9819 Elf_Internal_Rela *internal_relocs, int *insn_len,
9820 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9821 Elf_Internal_Shdr *symtab_hdr)
9823 /* The pattern for LONGCALL5.
9824 bltz rt, .L1 ; LONGCALL5/17_PCREL
9825 jal symbol ; 25_PCREL
9830 Elf_Internal_Rela *cond_irel, *irelend;
9831 int pic_ext_target = 0;
9832 bfd_signed_vma foff;
9834 irelend = internal_relocs + sec->reloc_count;
9835 laddr = irel->r_offset;
9836 insn = bfd_getb32 (contents + laddr);
9838 /* Get the reloc for the address from which the register is
9839 being loaded. This reloc will tell us which function is
9840 actually being called. */
9842 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9843 R_NDS32_25_PCREL_RELA, irel->r_addend);
9844 if (cond_irel == irelend)
9846 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
9847 (uint64_t) irel->r_offset);
9851 /* Get the value of the symbol referred to by the reloc. */
9852 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
9855 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9856 || foff >= CONSERVATIVE_16BIT_S1)
9859 /* Relax to bgezal rt, label ; 17_PCREL
9860 or bltzal rt, label ; 17_PCREL */
9862 /* Convert to complimentary conditional call. */
9863 insn = CONVERT_CONDITION_CALL (insn);
9865 /* For simplicity of coding, we are going to modify the section
9866 contents, the section relocs, and the BFD symbol table. We
9867 must tell the rest of the code not to free up this
9868 information. It would be possible to instead create a table
9869 of changes which have to be made, as is done in coff-mips.c;
9870 that would be more work, but would require less memory when
9871 the linker is run. */
9873 /* Modify relocation and contents. */
9875 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
9877 /* Replace the long call with a bgezal. */
9878 bfd_putb32 (insn, contents + cond_irel->r_offset);
9881 /* Clean unnessary relocations. */
9882 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9884 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9885 R_NDS32_17_PCREL_RELA, laddr);
9887 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
9892 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
9895 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9896 Elf_Internal_Rela *internal_relocs, int *insn_len,
9897 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9898 Elf_Internal_Shdr *symtab_hdr)
9900 /* The pattern for LONGCALL6.
9901 bltz rt, .L1 ; LONGCALL6/17_PCREL
9902 sethi ta, hi20(symbol) ; HI20/PTR
9903 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
9904 jral ta ; PTR_RES/EMPTY/INSN16
9909 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
9910 int pic_ext_target = 0;
9911 bfd_signed_vma foff;
9913 irelend = internal_relocs + sec->reloc_count;
9914 laddr = irel->r_offset;
9916 /* Get the reloc for the address from which the register is
9917 being loaded. This reloc will tell us which function is
9918 actually being called. */
9919 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9920 R_NDS32_EMPTY, irel->r_addend);
9922 if (em_irel == irelend)
9924 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
9925 (uint64_t) irel->r_offset);
9929 /* Get the value of the symbol referred to by the reloc. */
9930 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
9933 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9934 || foff >= CONSERVATIVE_24BIT_S1)
9937 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
9938 insn = bfd_getb32 (contents + irel->r_addend);
9939 if (insn & 0x80000000)
9942 insn = bfd_getb32 (contents + laddr);
9943 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9945 /* Relax to bgezal rt, label ; 17_PCREL
9946 or bltzal rt, label ; 17_PCREL */
9948 /* Convert to complimentary conditional call. */
9950 insn = CONVERT_CONDITION_CALL (insn);
9951 bfd_putb32 (insn, contents + em_irel->r_offset);
9954 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
9956 /* Set resolved relocation. */
9958 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9959 R_NDS32_PTR_RESOLVED, irel->r_addend);
9960 if (cond_irel == irelend)
9962 _bfd_error_handler (unrecognized_reloc_msg, abfd,
9963 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
9966 cond_irel->r_addend = 1;
9968 /* Clear relocations. */
9971 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9974 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9975 R_NDS32_17_PCREL_RELA, laddr);
9976 if (cond_irel != irelend)
9978 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
9981 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9982 R_NDS32_INSN16, irel->r_addend);
9983 if (cond_irel != irelend)
9985 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
9988 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9990 /* Relax to the following instruction sequence
9991 bltz rt, .L1 ; LONGCALL2/17_PCREL
9992 jal symbol ; 25_PCREL/PTR_RES
9995 /* Convert instruction. */
9997 bfd_putb32 (insn, contents + em_irel->r_offset);
9999 /* Convert relocations. */
10000 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10001 R_NDS32_25_PCREL_RELA);
10003 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10005 /* Set resolved relocation. */
10007 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10008 R_NDS32_PTR_RESOLVED, irel->r_addend);
10009 if (cond_irel == irelend)
10011 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10012 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10015 cond_irel->r_addend = 1;
10018 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10019 R_NDS32_INSN16, irel->r_addend);
10020 if (cond_irel != irelend)
10021 cond_irel->r_info =
10022 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10027 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10030 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10031 Elf_Internal_Rela *internal_relocs, int *insn_len,
10032 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10033 Elf_Internal_Shdr *symtab_hdr)
10035 /* The pattern for LONGJUMP4.
10036 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10037 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10038 jr ta ; PTR_RES/INSN16/EMPTY */
10041 int seq_len; /* Original length of instruction sequence. */
10043 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10044 int pic_ext_target = 0;
10045 bfd_signed_vma foff;
10047 irelend = internal_relocs + sec->reloc_count;
10048 seq_len = GET_SEQ_LEN (irel->r_addend);
10049 laddr = irel->r_offset;
10050 *insn_len = seq_len;
10052 /* Get the reloc for the address from which the register is
10053 being loaded. This reloc will tell us which function is
10054 actually being called. */
10056 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10057 R_NDS32_HI20_RELA, laddr);
10059 if (hi_irel == irelend)
10061 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10062 (uint64_t) irel->r_offset);
10066 /* Get the value of the symbol referred to by the reloc. */
10067 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
10070 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
10071 || foff < -CONSERVATIVE_24BIT_S1)
10074 /* Convert it to "j label", it may be converted to j8 in the final
10075 pass of relaxation. Therefore, we do not consider this currently. */
10076 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10077 R_NDS32_PTR_RESOLVED, irel->r_addend);
10078 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10079 R_NDS32_EMPTY, irel->r_addend);
10081 if (ptr_irel == irelend || em_irel == irelend)
10083 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10084 (uint64_t) irel->r_offset);
10089 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10090 ptr_irel->r_addend = 1;
10092 /* Write instruction. */
10094 bfd_putb32 (insn, contents + em_irel->r_offset);
10096 /* Clear relocations. */
10097 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10099 /* If there is function cse, HI20 can not remove now. */
10100 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10101 R_NDS32_LONGJUMP4, laddr);
10102 if (call_irel == irelend)
10106 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10112 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
10115 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10116 Elf_Internal_Rela *internal_relocs, int *insn_len,
10117 int *seq_len, bfd_byte *contents,
10118 Elf_Internal_Sym *isymbuf,
10119 Elf_Internal_Shdr *symtab_hdr)
10121 /* There are 2 variations for LONGJUMP5
10122 case 2-4; 1st insn convertible, 16-bit on.
10123 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10124 j label ; 25_PCREL/INSN16
10127 case 4-4; 1st insn not convertible
10128 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10129 j label ; 25_PCREL/INSN16
10133 Elf_Internal_Rela *cond_irel, *irelend;
10134 int pic_ext_target = 0;
10136 bfd_signed_vma foff;
10137 uint32_t insn, re_insn = 0;
10138 uint16_t insn16, re_insn16 = 0;
10139 unsigned long reloc;
10141 enum elf_nds32_reloc_type checked_types[] =
10142 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10143 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10145 irelend = internal_relocs + sec->reloc_count;
10146 laddr = irel->r_offset;
10148 /* Get the reloc for the address from which the register is
10149 being loaded. This reloc will tell us which function is
10150 actually being called. */
10153 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10154 R_NDS32_25_PCREL_RELA, irel->r_addend);
10155 if (cond_irel == irelend)
10157 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10158 (uint64_t) irel->r_offset);
10162 /* Get the value of the symbol referred to by the reloc. */
10163 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10166 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
10167 || foff >= CONSERVATIVE_16BIT_S1)
10170 /* Get the all corresponding instructions. */
10171 insn = bfd_getb32 (contents + laddr);
10172 /* Check instruction size. */
10173 if (insn & 0x80000000)
10176 insn16 = insn >> 16;
10177 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10180 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10182 if (N32_OP6 (re_insn) == N32_OP6_BR1
10183 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10185 /* beqs label ; 15_PCREL. */
10186 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10187 reloc = R_NDS32_15_PCREL_RELA;
10189 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10190 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10192 /* beqz label ; 17_PCREL. */
10193 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10194 reloc = R_NDS32_17_PCREL_RELA;
10196 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
10197 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
10199 /* beqc label ; 9_PCREL. */
10200 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10201 reloc = R_NDS32_WORD_9_PCREL_RELA;
10206 /* Set all relocations. */
10207 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
10209 /* Clean relocations. */
10210 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10211 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10213 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10214 checked_types[i], laddr);
10215 if (cond_irel != irelend)
10218 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10220 /* If the branch instruction is 2 byte, it cannot remove
10221 directly. Only convert it to nop16 and remove it after
10222 checking alignment issue. */
10223 insn16 = NDS32_NOP16;
10224 bfd_putb16 (insn16, contents + laddr);
10225 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10228 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10237 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
10240 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10241 Elf_Internal_Rela *internal_relocs, int *insn_len,
10242 int *seq_len, bfd_byte *contents,
10243 Elf_Internal_Sym *isymbuf,
10244 Elf_Internal_Shdr *symtab_hdr)
10246 /* There are 5 variations for LONGJUMP6
10247 case : 2-4-4-4; 1st insn convertible, 16-bit on.
10248 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10249 sethi ta, hi20(symbol) ; HI20/PTR
10250 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10251 jr ta ; PTR_RES/INSN16/EMPTY
10254 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
10255 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10256 sethi ta, hi20(symbol) ; HI20/PTR
10257 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10258 jr ta ; PTR_RES/INSN16/EMPTY
10261 enum elf_nds32_reloc_type checked_types[] =
10262 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10263 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10265 int reloc_off = 0, cond_removed = 0;
10267 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
10268 int pic_ext_target = 0;
10270 bfd_signed_vma foff;
10271 uint32_t insn, re_insn = 0;
10272 uint16_t insn16, re_insn16 = 0;
10273 unsigned long reloc;
10275 irelend = internal_relocs + sec->reloc_count;
10276 laddr = irel->r_offset;
10278 /* Get the reloc for the address from which the register is
10279 being loaded. This reloc will tell us which function is
10280 actually being called. */
10281 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10282 R_NDS32_EMPTY, irel->r_addend);
10284 if (em_irel == irelend)
10286 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
10287 (uint64_t) irel->r_offset);
10291 /* Get the value of the symbol referred to by the reloc. */
10292 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
10295 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
10296 || foff >= CONSERVATIVE_24BIT_S1)
10299 insn = bfd_getb32 (contents + laddr);
10300 /* Check instruction size. */
10301 if (insn & 0x80000000)
10304 insn16 = insn >> 16;
10305 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10308 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10310 /* For simplicity of coding, we are going to modify the section
10311 contents, the section relocs, and the BFD symbol table. We
10312 must tell the rest of the code not to free up this
10313 information. It would be possible to instead create a table
10314 of changes which have to be made, as is done in coff-mips.c;
10315 that would be more work, but would require less memory when
10316 the linker is run. */
10318 if (N32_OP6 (re_insn) == N32_OP6_BR1
10319 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10321 /* beqs label ; 15_PCREL */
10322 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10323 reloc = R_NDS32_15_PCREL_RELA;
10326 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10327 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10329 /* beqz label ; 17_PCREL */
10330 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10331 reloc = R_NDS32_17_PCREL_RELA;
10334 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10335 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10337 /* Relax to one of the following 2 variations
10339 case 2-4; 1st insn convertible, 16-bit on.
10340 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10341 j label ; 25_PCREL/INSN16
10344 case 4-4; 1st insn not convertible
10345 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10346 j label ; 25_PCREL/INSN16
10349 /* Use j label as second instruction. */
10351 reloc = R_NDS32_25_PCREL_RELA;
10352 bfd_putb32 (insn, contents + em_irel->r_offset);
10357 /* Set all relocations. */
10358 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
10361 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10362 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
10363 cond_irel->r_addend = 1;
10365 /* Use INSN16 of first branch instruction to distinguish if keeping
10366 INSN16 of final instruction or not. */
10367 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10368 R_NDS32_INSN16, irel->r_offset);
10369 if (insn_irel == irelend)
10371 /* Clean the final INSN16. */
10373 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10374 R_NDS32_INSN16, em_irel->r_offset);
10375 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10379 if (cond_removed == 1)
10383 /* Clear relocations. */
10384 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10386 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10389 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10390 checked_types[i], laddr);
10391 if (cond_irel != irelend)
10394 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10396 /* If the branch instruction is 2 byte, it cannot remove
10397 directly. Only convert it to nop16 and remove it after
10398 checking alignment issue. */
10399 insn16 = NDS32_NOP16;
10400 bfd_putb16 (insn16, contents + laddr);
10401 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10404 cond_irel->r_info =
10405 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10411 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10412 R_NDS32_LONGJUMP5);
10418 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
10421 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10422 Elf_Internal_Rela *internal_relocs, int *insn_len,
10423 int *seq_len, bfd_byte *contents,
10424 Elf_Internal_Sym *isymbuf,
10425 Elf_Internal_Shdr *symtab_hdr)
10427 /* There are 2 variations for LONGJUMP5
10428 case 2-4; 1st insn convertible, 16-bit on.
10429 movi55 ta, imm11 ; LONGJUMP7/INSN16
10430 beq rt, ta, label ; 15_PCREL
10432 case 4-4; 1st insn not convertible
10433 movi55 ta, imm11 ; LONGJUMP7/INSN16
10434 beq rt, ta, label ; 15_PCREL */
10437 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
10438 int pic_ext_target = 0;
10439 bfd_signed_vma foff;
10440 uint32_t insn, re_insn = 0;
10444 irelend = internal_relocs + sec->reloc_count;
10445 laddr = irel->r_offset;
10447 /* Get the reloc for the address from which the register is
10448 being loaded. This reloc will tell us which function is
10449 actually being called. */
10452 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10453 R_NDS32_15_PCREL_RELA, irel->r_addend);
10454 if (cond_irel == irelend)
10456 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
10457 (uint64_t) irel->r_offset);
10461 /* Get the value of the symbol referred to by the reloc. */
10462 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10465 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_8BIT_S1
10466 || foff >= CONSERVATIVE_8BIT_S1)
10469 /* Get the first instruction for its size. */
10470 insn = bfd_getb32 (contents + laddr);
10471 if (insn & 0x80000000)
10474 /* Get the immediate from movi55. */
10475 imm11 = N16_IMM5S (insn >> 16);
10479 /* Get the immediate from movi. */
10480 imm11 = N32_IMM20S (insn);
10483 /* Get the branch instruction. */
10484 insn = bfd_getb32 (contents + irel->r_addend);
10485 /* Convert instruction to BR3. */
10486 if ((insn >> 14) & 0x1)
10487 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
10489 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
10491 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10493 /* Set all relocations. */
10494 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10495 R_NDS32_WORD_9_PCREL_RELA);
10497 /* Clean relocations. */
10498 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10499 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10500 R_NDS32_INSN16, irel->r_offset);
10501 if (insn_irel != irelend)
10505 /* If the first insntruction is 16bit, convert it to nop16. */
10506 insn16 = NDS32_NOP16;
10507 bfd_putb16 (insn16, contents + laddr);
10508 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10511 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10519 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
10521 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
10524 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
10525 asection *sec, Elf_Internal_Rela *irel,
10526 Elf_Internal_Rela *internal_relocs, int *insn_len,
10527 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10528 Elf_Internal_Shdr *symtab_hdr, int load_store_relax)
10530 int eliminate_sethi = 0, range_type;
10532 bfd_vma local_sda, laddr;
10533 int seq_len; /* Original length of instruction sequence. */
10535 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
10536 bfd_vma access_addr = 0;
10537 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10538 enum elf_nds32_reloc_type checked_types[] =
10539 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
10540 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
10541 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
10542 R_NDS32_TLS_LE_HI20
10545 irelend = internal_relocs + sec->reloc_count;
10546 seq_len = GET_SEQ_LEN (irel->r_addend);
10547 laddr = irel->r_offset;
10548 *insn_len = seq_len;
10550 /* Get the high part relocation. */
10551 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10553 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10554 checked_types[i], laddr);
10555 if (hi_irelfn != irelend)
10559 if (hi_irelfn == irelend)
10561 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
10562 (uint64_t) irel->r_offset);
10566 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
10567 nds32_elf_final_sda_base (sec->output_section->owner,
10568 link_info, &local_sda, FALSE);
10570 switch (ELF32_R_TYPE (hi_irelfn->r_info))
10572 case R_NDS32_HI20_RELA:
10573 insn = bfd_getb32 (contents + laddr);
10575 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10577 if (range_type == NDS32_LOADSTORE_IMM)
10579 struct elf_link_hash_entry *h = NULL;
10582 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
10584 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
10585 h = elf_sym_hashes (abfd)[indx];
10588 if ((access_addr < CONSERVATIVE_20BIT)
10589 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10591 eliminate_sethi = 1;
10595 /* This is avoid to relax symbol address which is fixed
10596 relocations. Ex: _stack. */
10597 if (h && bfd_is_abs_section (h->root.u.def.section))
10601 if (!load_store_relax)
10604 /* Case for set gp register. */
10605 if (N32_RT5 (insn) == REG_GP)
10608 if (range_type == NDS32_LOADSTORE_FLOAT_S
10609 || range_type == NDS32_LOADSTORE_FLOAT_D)
10611 range_l = sdata_range[0][0];
10612 range_h = sdata_range[0][1];
10616 range_l = sdata_range[1][0];
10617 range_h = sdata_range[1][1];
10621 case R_NDS32_GOT_HI20:
10623 calculate_got_memory_address (abfd, link_info, hi_irelfn, symtab_hdr);
10625 /* If this symbol is not in .got, the return value will be -1.
10626 Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
10627 a negative offset is allowed. */
10628 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10629 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10630 eliminate_sethi = 1;
10633 case R_NDS32_PLT_GOTREL_HI20:
10634 access_addr = calculate_plt_memory_address (abfd, link_info, isymbuf,
10635 hi_irelfn, symtab_hdr);
10637 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10638 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10639 eliminate_sethi = 1;
10642 case R_NDS32_GOTOFF_HI20:
10644 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10646 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10647 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10648 eliminate_sethi = 1;
10651 case R_NDS32_GOTPC_HI20:
10652 /* The access_addr must consider r_addend of hi_irel. */
10653 access_addr = sec->output_section->vma + sec->output_offset
10654 + irel->r_offset + hi_irelfn->r_addend;
10656 if ((bfd_signed_vma) (local_sda - access_addr) < CONSERVATIVE_20BIT
10657 && (bfd_signed_vma) (local_sda - access_addr) >= -CONSERVATIVE_20BIT)
10658 eliminate_sethi = 1;
10661 case R_NDS32_TLS_LE_HI20:
10663 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10664 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10665 access_addr -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10666 if ((range_type == NDS32_LOADSTORE_IMM)
10667 && (bfd_signed_vma) (access_addr) < CONSERVATIVE_20BIT
10668 && (bfd_signed_vma) (access_addr) >= -CONSERVATIVE_20BIT)
10669 eliminate_sethi = 1;
10676 /* Delete sethi instruction. */
10677 if (eliminate_sethi == 1
10678 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
10679 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10681 hi_irelfn->r_info =
10682 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
10684 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10690 /* Relax LO12 relocation for nds32_elf_relax_section. */
10693 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
10694 asection *sec, Elf_Internal_Rela *irel,
10695 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
10696 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
10699 bfd_vma local_sda, laddr;
10700 unsigned long reloc;
10701 bfd_vma access_addr;
10702 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10703 Elf_Internal_Rela *irelfn = NULL, *irelend;
10704 struct elf_link_hash_entry *h = NULL;
10707 /* For SDA base relative relaxation. */
10708 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10709 &local_sda, FALSE);
10711 irelend = internal_relocs + sec->reloc_count;
10712 laddr = irel->r_offset;
10713 insn = bfd_getb32 (contents + laddr);
10715 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
10718 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10720 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
10722 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
10723 h = elf_sym_hashes (abfd)[indx];
10726 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
10727 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10729 reloc = R_NDS32_20_RELA;
10730 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10731 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10732 bfd_putb32 (insn, contents + laddr);
10734 /* This is avoid to relax symbol address which is fixed
10735 relocations. Ex: _stack. */
10736 else if (N32_OP6 (insn) == N32_OP6_ORI
10737 && h && bfd_is_abs_section (h->root.u.def.section))
10741 range_l = sdata_range[1][0];
10742 range_h = sdata_range[1][1];
10743 switch (ELF32_R_TYPE (irel->r_info))
10745 case R_NDS32_LO12S0_RELA:
10746 reloc = R_NDS32_SDA19S0_RELA;
10748 case R_NDS32_LO12S1_RELA:
10749 reloc = R_NDS32_SDA18S1_RELA;
10751 case R_NDS32_LO12S2_RELA:
10752 reloc = R_NDS32_SDA17S2_RELA;
10754 case R_NDS32_LO12S2_DP_RELA:
10755 range_l = sdata_range[0][0];
10756 range_h = sdata_range[0][1];
10757 reloc = R_NDS32_SDA12S2_DP_RELA;
10759 case R_NDS32_LO12S2_SP_RELA:
10760 range_l = sdata_range[0][0];
10761 range_h = sdata_range[0][1];
10762 reloc = R_NDS32_SDA12S2_SP_RELA;
10768 /* There are range_h and range_l because linker has to promise
10769 all sections move cross one page together. */
10770 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
10771 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10773 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
10775 /* Maybe we should add R_NDS32_INSN16 reloc type here
10776 or manually do some optimization. sethi can't be
10777 eliminated when updating $gp so the relative ori
10778 needs to be preserved. */
10781 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
10784 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10785 bfd_putb32 (insn, contents + laddr);
10787 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10789 /* SDA17 must keep INSN16 for converting fp_as_gp. */
10790 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
10792 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
10799 /* Relax low part of PIC instruction pattern. */
10802 nds32_elf_relax_piclo12 (struct bfd_link_info *link_info, bfd *abfd,
10803 asection *sec, Elf_Internal_Rela *irel,
10804 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10805 Elf_Internal_Shdr *symtab_hdr)
10808 bfd_vma local_sda, laddr;
10809 bfd_signed_vma foff;
10810 unsigned long reloc;
10812 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10813 &local_sda, FALSE);
10814 laddr = irel->r_offset;
10815 insn = bfd_getb32 (contents + laddr);
10817 if (N32_OP6 (insn) != N32_OP6_ORI)
10820 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
10822 foff = calculate_got_memory_address (abfd, link_info, irel,
10823 symtab_hdr) - local_sda;
10824 reloc = R_NDS32_GOT20;
10826 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
10828 foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
10829 symtab_hdr) - local_sda;
10830 reloc = R_NDS32_PLT_GOTREL_LO20;
10832 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
10834 foff = calculate_memory_address (abfd, irel, isymbuf,
10835 symtab_hdr) - local_sda;
10836 reloc = R_NDS32_GOTOFF;
10838 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12)
10840 foff = local_sda - sec->output_section->vma + sec->output_offset
10841 + irel->r_offset + irel->r_addend;
10842 reloc = R_NDS32_GOTPC20;
10847 if ((foff < CONSERVATIVE_20BIT) && (foff >= -CONSERVATIVE_20BIT))
10849 /* Turn into MOVI. */
10850 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10851 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10852 bfd_putb32 (insn, contents + laddr);
10856 /* Relax low part of LE TLS instruction pattern. */
10859 nds32_elf_relax_letlslo12 (struct bfd_link_info *link_info, bfd *abfd,
10860 Elf_Internal_Rela *irel,
10861 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10862 Elf_Internal_Shdr *symtab_hdr)
10866 bfd_signed_vma foff;
10867 unsigned long reloc;
10869 laddr = irel->r_offset;
10870 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10871 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10872 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10873 insn = bfd_getb32 (contents + laddr);
10875 if ( (bfd_signed_vma) (foff) < CONSERVATIVE_20BIT
10876 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_20BIT)
10878 /* Pattern sethi-ori transform to movi. */
10879 reloc = R_NDS32_TLS_LE_20;
10880 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10881 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10882 bfd_putb32 (insn, contents + laddr);
10886 /* Relax LE TLS calculate address instruction pattern. */
10889 nds32_elf_relax_letlsadd (struct bfd_link_info *link_info, bfd *abfd,
10890 asection *sec, Elf_Internal_Rela *irel,
10891 Elf_Internal_Rela *internal_relocs,
10892 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10893 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
10895 /* Local TLS non-pic
10896 sethi ta, hi20(symbol@tpoff) ; TLS_LE_HI20
10897 ori ta, ta, lo12(symbol@tpoff) ; TLS_LE_LO12
10898 add ra, ta, tp ; TLS_LE_ADD */
10902 bfd_signed_vma foff;
10903 Elf_Internal_Rela *i1_irelfn, *irelend;
10905 irelend = internal_relocs + sec->reloc_count;
10906 laddr = irel->r_offset;
10907 insn = bfd_getb32 (contents + laddr);
10908 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10909 R_NDS32_PTR_RESOLVED);
10910 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10911 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10912 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10914 /* The range is +/-16k. */
10915 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
10916 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
10918 /* Transform add to addi. */
10919 insn = N32_TYPE2 (ADDI, N32_RT5 (insn), N32_RB5 (insn), 0);
10921 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
10923 bfd_putb32 (insn, contents + laddr);
10924 if (i1_irelfn != irelend)
10926 i1_irelfn->r_addend |= 1;
10932 /* Relax LE TLS load store instruction pattern. */
10935 nds32_elf_relax_letlsls (struct bfd_link_info *link_info, bfd *abfd,
10936 asection *sec, Elf_Internal_Rela *irel,
10937 Elf_Internal_Rela *internal_relocs,
10938 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10939 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
10944 bfd_signed_vma foff;
10945 Elf_Internal_Rela *i1_irelfn, *irelend;
10948 irelend = internal_relocs + sec->reloc_count;
10949 laddr = irel->r_offset;
10950 insn = bfd_getb32 (contents + laddr);
10951 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10952 R_NDS32_PTR_RESOLVED);
10953 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10954 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10955 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10957 switch ((N32_OP6 (insn) << 8) | (insn & 0xff))
10959 case (N32_OP6_MEM << 8) | N32_MEM_LB:
10960 case (N32_OP6_MEM << 8) | N32_MEM_SB:
10961 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
10962 /* The range is +/-16k. */
10963 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
10964 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
10967 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
10969 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
10973 /* Fall through. */
10974 case (N32_OP6_MEM << 8) | N32_MEM_LH:
10975 case (N32_OP6_MEM << 8) | N32_MEM_SH:
10976 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
10977 /* The range is +/-32k. */
10978 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S1
10979 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S1)
10982 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
10984 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S1);
10988 /* Fall through. */
10989 case (N32_OP6_MEM << 8) | N32_MEM_LW:
10990 case (N32_OP6_MEM << 8) | N32_MEM_SW:
10991 /* The range is +/-64k. */
10992 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S2
10993 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S2)
10996 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
10998 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S2);
11002 /* Fall through. */
11009 bfd_putb32 (insn, contents + laddr);
11010 if (i1_irelfn != irelend)
11012 i1_irelfn->r_addend |= 1;
11018 /* Relax PTR relocation for nds32_elf_relax_section. */
11021 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11022 Elf_Internal_Rela *internal_relocs, int *insn_len,
11023 int *seq_len, bfd_byte *contents)
11025 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11027 irelend = internal_relocs + sec->reloc_count;
11030 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11031 R_NDS32_PTR_RESOLVED, irel->r_addend);
11033 if (re_irel == irelend)
11035 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11036 (uint64_t) irel->r_offset);
11040 if (re_irel->r_addend != 1)
11043 /* Pointed target is relaxed and no longer needs this void *,
11044 change the type to NONE. */
11045 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11047 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11048 not exist, it means only count 1 and remove it directly. */
11049 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11050 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11051 R_NDS32_PTR_COUNT);
11052 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11054 if (count_irel != irelend)
11056 if (--count_irel->r_addend > 0)
11060 if (ptr_irel != irelend)
11063 /* If the PTR_COUNT is already 0, remove current instruction. */
11064 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11069 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11072 nds32_elf_relax_pltgot_suff (struct bfd_link_info *link_info, bfd *abfd,
11073 asection *sec, Elf_Internal_Rela *irel,
11074 Elf_Internal_Rela *internal_relocs,
11075 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11076 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11079 bfd_signed_vma foff;
11080 Elf_Internal_Rela *i1_irelfn, *irelend;
11081 bfd_vma local_sda, laddr;
11083 irelend = internal_relocs + sec->reloc_count;
11084 laddr = irel->r_offset;
11085 insn = bfd_getb32 (contents + laddr);
11087 /* FIXME: It's a little trouble to turn JRAL5 to JAL since
11088 we need additional space. It might be help if we could
11089 borrow some space from instructions to be eliminated
11090 such as sethi, ori, add. */
11091 if (insn & 0x80000000)
11094 if (nds32_elf_check_dup_relocs
11095 (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
11099 find_relocs_at_address (irel, internal_relocs, irelend,
11100 R_NDS32_PTR_RESOLVED);
11103 The boundary should be reduced since the .plt section hasn't
11104 been created and the address of specific entry is still unknown
11105 Maybe the range between the function call and the begin of the
11106 .text section can be used to decide if the .plt is in the range
11107 of function call. */
11109 if (N32_OP6 (insn) == N32_OP6_ALU1
11110 && N32_SUB5 (insn) == N32_ALU1_ADD)
11112 /* Get the value of the symbol referred to by the reloc. */
11113 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11114 &local_sda, FALSE);
11115 foff = (bfd_signed_vma) (calculate_plt_memory_address
11116 (abfd, link_info, isymbuf, irel,
11117 symtab_hdr) - local_sda);
11118 /* This condition only happened when symbol is undefined. */
11122 if (foff < -CONSERVATIVE_19BIT || foff >= CONSERVATIVE_19BIT)
11124 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11125 R_NDS32_PLT_GOTREL_LO19);
11127 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
11129 else if (N32_OP6 (insn) == N32_OP6_JREG
11130 && N32_SUB5 (insn) == N32_JREG_JRAL)
11132 /* Get the value of the symbol referred to by the reloc. */
11134 calculate_plt_offset (abfd, sec, link_info, isymbuf, irel, symtab_hdr);
11135 /* This condition only happened when symbol is undefined. */
11138 if (foff < -CONSERVATIVE_24BIT_S1 || foff >= CONSERVATIVE_24BIT_S1)
11140 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
11146 bfd_putb32 (insn, contents + laddr);
11147 if (i1_irelfn != irelend)
11149 i1_irelfn->r_addend |= 1;
11154 /* Relax GOT_SUFF relocation for nds32_elf_relax_section. */
11157 nds32_elf_relax_got_suff (struct bfd_link_info *link_info, bfd *abfd,
11158 asection *sec, Elf_Internal_Rela *irel,
11159 Elf_Internal_Rela *internal_relocs,
11160 bfd_byte *contents, Elf_Internal_Shdr *symtab_hdr,
11161 bfd_boolean *again)
11164 bfd_signed_vma foff;
11165 Elf_Internal_Rela *i1_irelfn, *irelend;
11166 bfd_vma local_sda, laddr;
11168 irelend = internal_relocs + sec->reloc_count;
11169 laddr = irel->r_offset;
11170 insn = bfd_getb32 (contents + laddr);
11171 if (insn & 0x80000000)
11174 if (nds32_elf_check_dup_relocs
11175 (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
11178 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11179 R_NDS32_PTR_RESOLVED);
11181 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11182 &local_sda, FALSE);
11183 foff = calculate_got_memory_address (abfd, link_info, irel,
11184 symtab_hdr) - local_sda;
11186 if (foff < CONSERVATIVE_19BIT && foff >= -CONSERVATIVE_19BIT)
11188 /* Turn LW to LWI.GP. Change relocation type to R_NDS32_GOT_REL. */
11189 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11191 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
11192 bfd_putb32 (insn, contents + laddr);
11193 if (i1_irelfn != irelend)
11195 i1_irelfn->r_addend |= 1;
11201 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11204 nds32_elf_relax_gotoff_suff (struct bfd_link_info *link_info, bfd *abfd,
11205 asection *sec, Elf_Internal_Rela *irel,
11206 Elf_Internal_Rela *internal_relocs,
11207 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11208 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11210 int opc_insn_gotoff;
11212 bfd_signed_vma foff;
11213 Elf_Internal_Rela *i1_irelfn, *i2_irelfn, *irelend;
11214 bfd_vma local_sda, laddr;
11216 irelend = internal_relocs + sec->reloc_count;
11217 laddr = irel->r_offset;
11218 insn = bfd_getb32 (contents + laddr);
11220 if (insn & 0x80000000)
11223 if (nds32_elf_check_dup_relocs
11224 (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
11227 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11228 R_NDS32_PTR_RESOLVED);
11229 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11230 &local_sda, FALSE);
11231 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11232 foff = foff - local_sda;
11234 if (foff >= CONSERVATIVE_19BIT || foff < -CONSERVATIVE_19BIT)
11237 /* Concatenate opcode and sub-opcode for switch case.
11238 It may be MEM or ALU1. */
11239 opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
11240 switch (opc_insn_gotoff)
11242 case (N32_OP6_MEM << 8) | N32_MEM_LW:
11243 /* 4-byte aligned. */
11244 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11246 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11248 case (N32_OP6_MEM << 8) | N32_MEM_SW:
11249 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
11251 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11253 case (N32_OP6_MEM << 8) | N32_MEM_LH:
11254 /* 2-byte aligned. */
11255 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
11257 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11259 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11260 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
11262 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11264 case (N32_OP6_MEM << 8) | N32_MEM_SH:
11265 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
11267 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11269 case (N32_OP6_MEM << 8) | N32_MEM_LB:
11270 /* 1-byte aligned. */
11271 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
11273 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11275 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11276 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
11278 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11280 case (N32_OP6_MEM << 8) | N32_MEM_SB:
11281 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
11283 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11285 case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD:
11286 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
11288 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11294 bfd_putb32 (insn, contents + laddr);
11295 if (i1_irelfn != irelend)
11297 i1_irelfn->r_addend |= 1;
11300 if ((i2_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11301 R_NDS32_INSN16)) != irelend)
11302 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11307 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11308 Elf_Internal_Rela *internal_relocs,
11309 bfd_byte *contents,
11310 nds32_elf_blank_t **relax_blank_list,
11311 int optimize, int opt_size)
11313 /* This code block is used to adjust 4-byte alignment by relax a pair
11314 of instruction a time.
11316 It recognizes three types of relocations.
11317 1. R_NDS32_LABEL - a alignment.
11318 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11319 3. is_16bit_NOP () - remove a 16-bit instruction. */
11321 /* TODO: It seems currently implementation only support 4-byte alignment.
11322 We should handle any-alignment. */
11324 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11325 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11326 Elf_Internal_Rela rel_temp;
11327 Elf_Internal_Rela *irelend;
11331 /* Checking for branch relaxation relies on the relocations to
11332 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11333 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11334 sizeof (Elf_Internal_Rela), compar_reloc);
11336 irelend = internal_relocs + sec->reloc_count;
11338 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11339 /* FIXME: Can we generate the right order in assembler?
11340 So we don't have to swapping them here. */
11342 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11343 label_rel < irelend; label_rel++)
11345 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11348 /* Find the first reloc has the same offset with label_rel. */
11349 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11352 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11354 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11356 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11359 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11360 && insn_rel < label_rel)
11362 /* Swap the two reloc if the R_NDS32_INSN16 is
11363 before R_NDS32_LABEL. */
11364 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11365 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11366 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11372 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11373 or higher, remove other R_NDS32_LABEL with lower alignment.
11374 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11375 then the R_NDS32_LABEL sequence is broke. */
11376 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11378 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11380 if (label_rel == NULL)
11382 if (tmp_rel->r_addend < 2)
11383 label_rel = tmp_rel;
11386 else if (tmp_rel->r_addend > 1)
11388 /* Remove all LABEL relocation from label_rel to tmp_rel
11389 including relocations with same offset as tmp_rel. */
11390 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel
11391 || tmp2_rel->r_offset == tmp_rel->r_offset; tmp2_rel++)
11393 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11394 && tmp2_rel->r_addend < 2)
11396 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11402 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11404 /* A new INSN16 which can be converted, so clear label_rel. */
11405 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11407 || is_16bit_NOP (abfd, sec, tmp_rel))
11414 /* Optimized for speed and nothing has not been relaxed.
11415 It's time to align labels.
11416 We may convert a 16-bit instruction right before a label to
11417 32-bit, in order to align the label if necessary
11418 all reloc entries has been sorted by r_offset. */
11419 for (irel = internal_relocs; irel < irelend; irel++)
11421 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11422 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11425 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11427 /* A new INSN16 found, resize the old one. */
11428 if (is_convert_32_to_16
11429 (abfd, sec, irel, internal_relocs, irelend, &insn16)
11430 || is_16bit_NOP (abfd, sec, irel))
11434 /* Previous INSN16 reloc exists, reduce its
11436 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11439 nds32_elf_write_16 (abfd, contents, insn_rel,
11440 internal_relocs, irelend, insn16);
11442 if (!insert_nds32_elf_blank_recalc_total
11443 (relax_blank_list, insn_rel->r_offset + 2, 2))
11446 else if (is_16bit_NOP (abfd, sec, insn_rel))
11448 if (!insert_nds32_elf_blank_recalc_total
11449 (relax_blank_list, insn_rel->r_offset, 2))
11453 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11455 /* Save the new one for later use. */
11459 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11462 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11464 /* Search for label. */
11465 int force_relax = 0;
11467 /* Label on 16-bit instruction or optimization
11468 needless, just reset this reloc. */
11469 insn16 = bfd_getb16 (contents + irel->r_offset);
11470 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
11473 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11478 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
11479 irel->r_offset, 1);
11483 /* Check if there is case which can not be aligned. */
11484 if (irel->r_addend == 2 && address & 0x2)
11489 /* Try to align this label. */
11491 if ((irel->r_addend & 0x1f) < 2)
11493 /* Check if there is a INSN16 at the same address.
11494 Label_rel always seats before insn_rel after
11497 /* Search for INSN16 at LABEL location. If INSN16 is at
11498 same location and this LABEL alignment is lower than 2,
11499 the INSN16 can be converted to 2-byte. */
11500 for (tmp_rel = irel;
11501 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
11504 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
11505 && (is_convert_32_to_16
11506 (abfd, sec, tmp_rel, internal_relocs,
11508 || is_16bit_NOP (abfd, sec, tmp_rel)))
11516 if (force_relax || irel->r_addend == 1 || address & 0x2)
11518 /* Label not aligned. */
11519 /* Previous reloc exists, reduce its size to 16-bit. */
11520 if (is_convert_32_to_16 (abfd, sec, insn_rel,
11521 internal_relocs, irelend, &insn16))
11523 nds32_elf_write_16 (abfd, contents, insn_rel,
11524 internal_relocs, irelend, insn16);
11526 if (!insert_nds32_elf_blank_recalc_total
11527 (relax_blank_list, insn_rel->r_offset + 2, 2))
11530 else if (is_16bit_NOP (abfd, sec, insn_rel))
11532 if (!insert_nds32_elf_blank_recalc_total
11533 (relax_blank_list, insn_rel->r_offset, 2))
11538 /* INSN16 reloc is used. */
11544 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
11545 if (insn_rel && (address & 0x2 || opt_size))
11547 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11550 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
11552 if (!insert_nds32_elf_blank_recalc_total
11553 (relax_blank_list, insn_rel->r_offset + 2, 2))
11555 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11558 else if (is_16bit_NOP (abfd, sec, insn_rel))
11560 if (!insert_nds32_elf_blank_recalc_total
11561 (relax_blank_list, insn_rel->r_offset, 2))
11563 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11571 /* Pick relaxation round. */
11574 nds32_elf_pick_relax (bfd_boolean init, asection *sec, bfd_boolean *again,
11575 struct elf_nds32_link_hash_table *table,
11576 struct bfd_link_info *link_info)
11578 static asection *final_sec, *first_sec = NULL;
11579 static bfd_boolean normal_again = FALSE;
11580 static bfd_boolean set = FALSE;
11581 static bfd_boolean first = TRUE;
11582 int round_table[] = {
11583 NDS32_RELAX_NORMAL_ROUND,
11584 NDS32_RELAX_JUMP_IFC_ROUND,
11585 NDS32_RELAX_EX9_BUILD_ROUND,
11586 NDS32_RELAX_EX9_REPLACE_ROUND,
11588 static int pass = 0;
11589 static int relax_round;
11591 /* The new round. */
11592 if (init && first_sec == sec)
11595 normal_again = FALSE;
11600 /* Run an empty run to get the final section. */
11601 relax_round = NDS32_RELAX_EMPTY_ROUND;
11603 /* It has to enter relax again because we can
11604 not make sure what the final turn is. */
11613 /* Not reenter yet. */
11615 return relax_round;
11618 relax_round = round_table[pass];
11620 if (!init && relax_round == NDS32_RELAX_NORMAL_ROUND && *again)
11621 normal_again = TRUE;
11623 if (!init && final_sec == sec)
11625 switch (relax_round)
11627 case NDS32_RELAX_NORMAL_ROUND:
11630 /* Normal relaxation done. */
11631 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11636 else if (table->target_optimize & NDS32_RELAX_EX9_ON)
11638 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11641 else if (table->ex9_import_file)
11643 /* Import ex9 table. */
11644 if (table->update_ex9_table)
11645 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11647 pass += 3; /* NDS32_RELAX_EX9_REPLACE_ROUND */
11648 nds32_elf_ex9_import_table (link_info);
11653 case NDS32_RELAX_JUMP_IFC_ROUND:
11654 if (!nds32_elf_ifc_finish (link_info))
11655 _bfd_error_handler (_("error: Jump IFC Fail."));
11656 if (table->target_optimize & NDS32_RELAX_EX9_ON)
11662 case NDS32_RELAX_EX9_BUILD_ROUND:
11663 nds32_elf_ex9_finish (link_info);
11667 case NDS32_RELAX_EX9_REPLACE_ROUND:
11668 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11670 /* Do jump IFC optimization again. */
11671 if (!nds32_elf_ifc_finish (link_info))
11672 _bfd_error_handler (_("error: Jump IFC Fail."));
11680 return relax_round;
11684 nds32_elf_relax_section (bfd *abfd, asection *sec,
11685 struct bfd_link_info *link_info, bfd_boolean *again)
11687 nds32_elf_blank_t *relax_blank_list = NULL;
11688 Elf_Internal_Shdr *symtab_hdr;
11689 Elf_Internal_Rela *internal_relocs;
11690 Elf_Internal_Rela *irel;
11691 Elf_Internal_Rela *irelend;
11692 Elf_Internal_Sym *isymbuf = NULL;
11693 bfd_byte *contents = NULL;
11694 bfd_boolean result = TRUE;
11700 /* Target dependnet option. */
11701 struct elf_nds32_link_hash_table *table;
11702 int load_store_relax;
11705 relax_blank_list = NULL;
11709 /* Nothing to do for
11710 * relocatable link or
11711 * non-relocatable section or
11712 * non-code section or
11714 * no reloc entry. */
11715 if (bfd_link_relocatable (link_info)
11716 || (sec->flags & SEC_RELOC) == 0
11717 || (sec->flags & SEC_EXCLUDE) != 0
11718 || (sec->flags & SEC_CODE) == 0
11722 /* 09.12.11 Workaround. */
11723 /* We have to adjust align for R_NDS32_LABEL if needed.
11724 The adjust approach only can fix 2-byte align once. */
11725 if (sec->alignment_power > 2)
11728 /* The optimization type to do. */
11730 table = nds32_elf_hash_table (link_info);
11731 relax_round = nds32_elf_pick_relax (TRUE, sec, again, table, link_info);
11732 switch (relax_round)
11734 case NDS32_RELAX_JUMP_IFC_ROUND:
11735 /* Here is the entrance of ifc jump relaxation. */
11736 if (!nds32_elf_ifc_calc (link_info, abfd, sec))
11738 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11741 case NDS32_RELAX_EX9_BUILD_ROUND:
11742 /* Here is the entrance of ex9 relaxation. There are two pass of
11743 ex9 relaxation. The one is to traverse all instructions and build
11744 the hash table. The other one is to compare instructions and replace
11746 if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
11748 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11751 case NDS32_RELAX_EX9_REPLACE_ROUND:
11752 if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
11756 case NDS32_RELAX_EMPTY_ROUND:
11757 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11760 case NDS32_RELAX_NORMAL_ROUND:
11762 if (sec->reloc_count == 0)
11767 /* The begining of general relaxation. */
11769 if (is_SDA_BASE_set == 0)
11772 is_SDA_BASE_set = 1;
11773 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11775 relax_range_measurement (abfd);
11778 if (is_ITB_BASE_set == 0)
11780 /* Set the _ITB_BASE_. */
11781 if (!nds32_elf_ex9_itb_base (link_info))
11783 _bfd_error_handler (_("%pB: error: Cannot set _ITB_BASE_"), abfd);
11784 bfd_set_error (bfd_error_bad_value);
11788 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11789 /* Relocations MUST be kept in memory, because relaxation adjust them. */
11790 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11791 TRUE /* keep_memory */);
11792 if (internal_relocs == NULL)
11795 irelend = internal_relocs + sec->reloc_count;
11796 irel = find_relocs_at_address (internal_relocs, internal_relocs,
11797 irelend, R_NDS32_RELAX_ENTRY);
11799 if (irel == irelend)
11802 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
11804 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
11806 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11810 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
11813 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
11817 load_store_relax = table->load_store_relax;
11819 /* Get symbol table and section content. */
11820 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
11821 || !nds32_get_local_syms (abfd, sec, &isymbuf))
11824 /* Do relax loop only when finalize is not done.
11825 Take care of relaxable relocs except INSN16. */
11826 for (irel = internal_relocs; irel < irelend; irel++)
11828 int seq_len; /* Original length of instruction sequence. */
11829 int insn_len = 0; /* Final length of instruction sequence. */
11830 bfd_boolean removed;
11833 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
11834 && (irel->r_addend & 0x1f) >= 2)
11837 /* Relocation Types
11838 R_NDS32_LONGCALL1 53
11839 R_NDS32_LONGCALL2 54
11840 R_NDS32_LONGCALL3 55
11841 R_NDS32_LONGJUMP1 56
11842 R_NDS32_LONGJUMP2 57
11843 R_NDS32_LONGJUMP3 58
11844 R_NDS32_LOADSTORE 59 */
11845 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
11846 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
11847 seq_len = GET_SEQ_LEN (irel->r_addend);
11849 /* Relocation Types
11850 R_NDS32_LONGCALL4 107
11851 R_NDS32_LONGCALL5 108
11852 R_NDS32_LONGCALL6 109
11853 R_NDS32_LONGJUMP4 110
11854 R_NDS32_LONGJUMP5 111
11855 R_NDS32_LONGJUMP6 112
11856 R_NDS32_LONGJUMP7 113 */
11857 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
11858 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
11861 /* Relocation Types
11862 R_NDS32_LO12S0_RELA 30
11863 R_NDS32_LO12S1_RELA 29
11864 R_NDS32_LO12S2_RELA 28
11865 R_NDS32_LO12S2_SP_RELA 71
11866 R_NDS32_LO12S2_DP_RELA 70
11867 R_NDS32_GOT_LO12 46
11868 R_NDS32_GOTOFF_LO12 50
11869 R_NDS32_PLTREL_LO12 65
11870 R_NDS32_PLT_GOTREL_LO12 67
11871 R_NDS32_17IFC_PCREL_RELA 96
11872 R_NDS32_GOT_SUFF 193
11873 R_NDS32_GOTOFF_SUFF 194
11874 R_NDS32_PLT_GOT_SUFF 195
11875 R_NDS32_MULCALL_SUFF 196
11877 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
11878 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
11879 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
11880 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
11881 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
11882 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
11883 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
11884 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
11885 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
11886 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
11887 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
11888 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
11889 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
11890 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
11891 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS)
11896 insn_len = seq_len;
11899 switch (ELF32_R_TYPE (irel->r_info))
11901 case R_NDS32_LONGCALL1:
11902 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
11903 &insn_len, contents, isymbuf,
11906 case R_NDS32_LONGCALL2:
11907 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
11908 &insn_len, contents, isymbuf,
11911 case R_NDS32_LONGCALL3:
11912 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
11913 &insn_len, contents, isymbuf,
11916 case R_NDS32_LONGJUMP1:
11917 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
11918 &insn_len, contents, isymbuf,
11921 case R_NDS32_LONGJUMP2:
11922 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
11923 &insn_len, contents, isymbuf,
11926 case R_NDS32_LONGJUMP3:
11927 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
11928 &insn_len, contents, isymbuf,
11931 case R_NDS32_LONGCALL4:
11932 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
11933 &insn_len, contents, isymbuf,
11936 case R_NDS32_LONGCALL5:
11937 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
11938 &insn_len, contents, isymbuf,
11941 case R_NDS32_LONGCALL6:
11942 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
11943 &insn_len, contents, isymbuf,
11946 case R_NDS32_LONGJUMP4:
11947 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
11948 &insn_len, contents, isymbuf,
11951 case R_NDS32_LONGJUMP5:
11952 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
11953 &insn_len, &seq_len, contents,
11954 isymbuf, symtab_hdr);
11956 case R_NDS32_LONGJUMP6:
11957 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
11958 &insn_len, &seq_len, contents,
11959 isymbuf, symtab_hdr);
11961 case R_NDS32_LONGJUMP7:
11962 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
11963 &insn_len, &seq_len, contents,
11964 isymbuf, symtab_hdr);
11966 case R_NDS32_LOADSTORE:
11967 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
11968 internal_relocs, &insn_len,
11969 contents, isymbuf, symtab_hdr,
11972 case R_NDS32_LO12S0_RELA:
11973 case R_NDS32_LO12S1_RELA:
11974 case R_NDS32_LO12S2_DP_RELA:
11975 case R_NDS32_LO12S2_SP_RELA:
11976 case R_NDS32_LO12S2_RELA:
11977 /* Relax for low part. */
11978 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
11979 contents, isymbuf, symtab_hdr);
11981 /* It is impossible to delete blank, so just continue. */
11983 case R_NDS32_GOT_LO12:
11984 case R_NDS32_GOTOFF_LO12:
11985 case R_NDS32_PLTREL_LO12:
11986 case R_NDS32_PLT_GOTREL_LO12:
11987 case R_NDS32_GOTPC_LO12:
11988 /* Relax for PIC gp-relative low part. */
11989 nds32_elf_relax_piclo12 (link_info, abfd, sec, irel, contents,
11990 isymbuf, symtab_hdr);
11992 /* It is impossible to delete blank, so just continue. */
11994 case R_NDS32_TLS_LE_LO12:
11995 /* Relax for LE TLS low part. */
11996 nds32_elf_relax_letlslo12 (link_info, abfd, irel, contents,
11997 isymbuf, symtab_hdr);
11999 /* It is impossible to delete blank, so just continue. */
12001 case R_NDS32_TLS_LE_ADD:
12002 nds32_elf_relax_letlsadd (link_info, abfd, sec, irel, internal_relocs,
12003 contents, isymbuf, symtab_hdr, again);
12004 /* It is impossible to delete blank, so just continue. */
12006 case R_NDS32_TLS_LE_LS:
12007 nds32_elf_relax_letlsls (link_info, abfd, sec, irel, internal_relocs,
12008 contents, isymbuf, symtab_hdr, again);
12011 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12012 &insn_len, &seq_len, contents);
12014 case R_NDS32_PLT_GOT_SUFF:
12015 nds32_elf_relax_pltgot_suff (link_info, abfd, sec, irel,
12016 internal_relocs, contents,
12017 isymbuf, symtab_hdr, again);
12018 /* It is impossible to delete blank, so just continue. */
12020 case R_NDS32_GOT_SUFF:
12021 nds32_elf_relax_got_suff (link_info, abfd, sec, irel,
12022 internal_relocs, contents,
12023 symtab_hdr, again);
12024 /* It is impossible to delete blank, so just continue. */
12026 case R_NDS32_GOTOFF_SUFF:
12027 nds32_elf_relax_gotoff_suff (link_info, abfd, sec, irel,
12028 internal_relocs, contents,
12029 isymbuf, symtab_hdr, again);
12030 /* It is impossible to delete blank, so just continue. */
12036 if (removed && seq_len - insn_len > 0)
12038 if (!insert_nds32_elf_blank
12039 (&relax_blank_list, irel->r_offset + insn_len,
12040 seq_len - insn_len))
12046 calc_nds32_blank_total (relax_blank_list);
12048 if (table->relax_fp_as_gp)
12050 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12056 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12062 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
12066 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12067 &relax_blank_list, optimize, opt_size))
12071 /* It doesn't matter optimize_for_space_no_align anymore.
12072 If object file is assembled with flag '-Os',
12073 the we don't adjust jump-destination on 4-byte boundary. */
12075 if (relax_blank_list)
12077 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12078 relax_blank_list = NULL;
12083 /* Closing the section, so we don't relax it anymore. */
12084 bfd_vma sec_size_align;
12085 Elf_Internal_Rela *tmp_rel;
12087 /* Pad to alignment boundary. Only handle current section alignment. */
12088 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12089 & ((-1U) << sec->alignment_power);
12090 if ((sec_size_align - sec->size) & 0x2)
12092 insn16 = NDS32_NOP16;
12093 bfd_putb16 (insn16, contents + sec->size);
12097 while (sec_size_align != sec->size)
12099 insn = NDS32_NOP32;
12100 bfd_putb32 (insn, contents + sec->size);
12104 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12105 irelend, R_NDS32_RELAX_ENTRY);
12106 if (tmp_rel != irelend)
12107 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12109 clean_nds32_elf_blank ();
12113 if (internal_relocs != NULL
12114 && elf_section_data (sec)->relocs != internal_relocs)
12115 free (internal_relocs);
12117 if (contents != NULL
12118 && elf_section_data (sec)->this_hdr.contents != contents)
12121 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12131 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12133 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12134 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12139 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12140 struct bfd_link_info *info,
12141 void *finfo ATTRIBUTE_UNUSED,
12142 bfd_boolean (*func) (void *, const char *,
12143 Elf_Internal_Sym *,
12145 struct elf_link_hash_entry *)
12148 FILE *sym_ld_script = NULL;
12149 struct elf_nds32_link_hash_table *table;
12151 table = nds32_elf_hash_table (info);
12152 sym_ld_script = table->sym_ld_script;
12154 if (check_start_export_sym)
12155 fprintf (sym_ld_script, "}\n");
12160 static enum elf_reloc_type_class
12161 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12162 const asection *rel_sec ATTRIBUTE_UNUSED,
12163 const Elf_Internal_Rela *rela)
12165 switch ((int) ELF32_R_TYPE (rela->r_info))
12167 case R_NDS32_RELATIVE:
12168 return reloc_class_relative;
12169 case R_NDS32_JMP_SLOT:
12170 return reloc_class_plt;
12172 return reloc_class_copy;
12174 return reloc_class_normal;
12178 /* Put target dependent option into info hash table. */
12180 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12181 int relax_fp_as_gp,
12182 int eliminate_gc_relocs,
12183 FILE * sym_ld_script, int load_store_relax,
12184 int target_optimize, int relax_status,
12185 int relax_round, FILE * ex9_export_file,
12186 FILE * ex9_import_file,
12187 int update_ex9_table, int ex9_limit,
12188 bfd_boolean ex9_loop_aware,
12189 bfd_boolean ifc_loop_aware)
12191 struct elf_nds32_link_hash_table *table;
12193 table = nds32_elf_hash_table (link_info);
12197 table->relax_fp_as_gp = relax_fp_as_gp;
12198 table->eliminate_gc_relocs = eliminate_gc_relocs;
12199 table->sym_ld_script = sym_ld_script;
12200 table ->load_store_relax = load_store_relax;
12201 table->target_optimize = target_optimize;
12202 table->relax_status = relax_status;
12203 table->relax_round = relax_round;
12204 table->ex9_export_file = ex9_export_file;
12205 table->ex9_import_file = ex9_import_file;
12206 table->update_ex9_table = update_ex9_table;
12207 table->ex9_limit = ex9_limit;
12208 table->ex9_loop_aware = ex9_loop_aware;
12209 table->ifc_loop_aware = ifc_loop_aware;
12212 /* These functions and data-structures are used for fp-as-gp
12215 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12216 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12217 the read-only section and read-write section. */
12218 #define FAG_WINDOW (508 - 32)
12220 /* An nds32_fag represent a gp-relative access.
12221 We find best fp-base by using a sliding window
12222 to find a base address which can cover most gp-access. */
12225 struct nds32_fag *next; /* NULL-teminated linked list. */
12226 bfd_vma addr; /* The address of this fag. */
12227 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12228 It is used for applying FP7U2_FLAG. */
12229 int count; /* How many times this address is referred.
12230 There should be exactly `count' relocations
12232 int relas_capcity; /* The buffer size of relas.
12233 We use an array instead of linked-list,
12234 and realloc is used to adjust buffer size. */
12238 nds32_fag_init (struct nds32_fag *head)
12240 memset (head, 0, sizeof (struct nds32_fag));
12244 nds32_fag_verify (struct nds32_fag *head)
12246 struct nds32_fag *iter;
12247 struct nds32_fag *prev;
12253 if (prev && prev->addr >= iter->addr)
12254 puts ("Bug in fp-as-gp insertion.");
12260 /* Insert a fag in ascending order.
12261 If a fag of the same address already exists,
12262 they are chained by relas array. */
12265 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12266 Elf_Internal_Rela * rel)
12268 struct nds32_fag *iter;
12269 struct nds32_fag *new_fag;
12270 const int INIT_RELAS_CAP = 4;
12273 iter->next && iter->next->addr <= addr;
12275 /* Find somewhere to insert. */ ;
12277 /* `iter' will be equal to `head' if the list is empty. */
12278 if (iter != head && iter->addr == addr)
12280 /* The address exists in the list.
12281 Insert `rel' into relocation list, relas. */
12283 /* Check whether relas is big enough. */
12284 if (iter->count >= iter->relas_capcity)
12286 iter->relas_capcity *= 2;
12287 iter->relas = bfd_realloc
12288 (iter->relas, iter->relas_capcity * sizeof (void *));
12290 iter->relas[iter->count++] = rel;
12294 /* This is a new address. Create a fag node for it. */
12295 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12296 memset (new_fag, 0, sizeof (*new_fag));
12297 new_fag->addr = addr;
12298 new_fag->count = 1;
12299 new_fag->next = iter->next;
12300 new_fag->relas_capcity = INIT_RELAS_CAP;
12301 new_fag->relas = (Elf_Internal_Rela **)
12302 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12303 new_fag->relas[0] = rel;
12304 iter->next = new_fag;
12306 nds32_fag_verify (head);
12310 nds32_fag_free_list (struct nds32_fag *head)
12312 struct nds32_fag *iter;
12317 struct nds32_fag *tmp = iter;
12325 /* Find the best fp-base address.
12326 The relocation associated with that address is returned,
12327 so we can track the symbol instead of a fixed address.
12329 When relaxation, the address of an datum may change,
12330 because a text section is shrinked, so the data section
12331 moves forward. If the aligments of text and data section
12332 are different, their distance may change too.
12333 Therefore, tracking a fixed address is not appriate. */
12336 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12338 struct nds32_fag *base; /* First fag in the window. */
12339 struct nds32_fag *last; /* First fag outside the window. */
12340 int accu = 0; /* Usage accumulation. */
12341 struct nds32_fag *best; /* Best fag. */
12342 int baccu = 0; /* Best accumulation. */
12344 /* Use first fag for initial, and find the last fag in the window.
12346 In each iteration, we could simply subtract previous fag
12347 and accumulate following fags which are inside the window,
12348 untill we each the end. */
12350 if (head->next == NULL)
12356 /* Initialize base. */
12360 last && last->addr < base->addr + FAG_WINDOW;
12362 accu += last->count;
12366 /* Record the best base in each iteration. */
12369 accu -= base->count;
12371 /* Account fags in window. */
12372 for (/* Nothing. */;
12373 last && last->addr < base->addr + FAG_WINDOW;
12375 accu += last->count;
12377 /* A better fp-base? */
12390 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12391 so we can convert it fo fp-relative access later.
12392 `best_fag' is the best fp-base. Only those inside the window
12393 of best_fag is applied the flag. */
12396 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12397 bfd *abfd, struct nds32_fag *best_fag,
12398 Elf_Internal_Rela *internal_relocs,
12399 Elf_Internal_Rela *irelend)
12401 struct nds32_fag *ifag;
12402 bfd_vma best_fpbase, gp;
12405 output_bfd = abfd->sections->output_section->owner;
12406 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12407 best_fpbase = best_fag->addr;
12409 if (best_fpbase > gp + sdata_range[1][1]
12410 || best_fpbase < gp - sdata_range[1][0])
12413 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12414 so we know they can be converted to lwi37.fp. */
12415 for (ifag = best_fag;
12416 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12420 for (i = 0; i < ifag->count; i++)
12422 Elf_Internal_Rela *insn16_rel;
12423 Elf_Internal_Rela *fag_rel;
12425 fag_rel = ifag->relas[i];
12427 /* Only if this is within the WINDOWS, FP7U2_FLAG
12430 insn16_rel = find_relocs_at_address
12431 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12433 if (insn16_rel != irelend)
12434 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12440 /* Reset INSN16 to clean fp as gp. */
12443 nds32_fag_unmark_relax (struct nds32_fag *fag,
12444 Elf_Internal_Rela *internal_relocs,
12445 Elf_Internal_Rela *irelend)
12447 struct nds32_fag *ifag;
12449 Elf_Internal_Rela *insn16_rel;
12450 Elf_Internal_Rela *fag_rel;
12452 for (ifag = fag; ifag; ifag = ifag->next)
12454 for (i = 0; i < ifag->count; i++)
12456 fag_rel = ifag->relas[i];
12458 /* Restore the INSN16 relocation. */
12459 insn16_rel = find_relocs_at_address
12460 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12462 if (insn16_rel != irelend)
12463 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12468 /* This is the main function of fp-as-gp optimization.
12469 It should be called by relax_section. */
12472 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12473 bfd *abfd, asection *sec,
12474 Elf_Internal_Rela *internal_relocs,
12475 Elf_Internal_Rela *irelend,
12476 Elf_Internal_Sym *isymbuf)
12478 Elf_Internal_Rela *begin_rel = NULL;
12479 Elf_Internal_Rela *irel;
12480 struct nds32_fag fag_head;
12481 Elf_Internal_Shdr *symtab_hdr;
12482 bfd_byte *contents;
12483 bfd_boolean ifc_inside = FALSE;
12485 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12487 /* Per-function fp-base selection.
12488 1. Create a list for all the gp-relative access.
12489 2. Base on those gp-relative address,
12490 find a fp-base which can cover most access.
12491 3. Use the fp-base for fp-as-gp relaxation.
12493 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12495 1. delete the `la $fp, _FP_BASE_' instruction and
12496 2. not convert lwi.gp to lwi37.fp.
12498 To delete the _FP_BASE_ instruction, we simply apply
12499 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12501 To suppress the conversion, we simply NOT to apply
12502 R_NDS32_INSN16_FP7U2_FLAG flag. */
12504 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12506 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12507 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12510 /* Check whether it is worth for fp-as-gp optimization,
12511 i.e., at least 3 gp-load.
12513 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12514 apply this optimization. */
12516 for (irel = internal_relocs; irel < irelend; irel++)
12518 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12519 One we enter the begin of the region, we track all the LW/ST
12520 instructions, so when we leave the region, we try to find
12521 the best fp-base address for those LW/ST instructions. */
12523 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12524 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12526 /* Begin of the region. */
12528 /* xgettext:c-format */
12529 _bfd_error_handler (_("%pB: Nested OMIT_FP in %pA."), abfd, sec);
12532 nds32_fag_init (&fag_head);
12533 ifc_inside = FALSE;
12535 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12536 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12539 struct nds32_fag *best_fag, *tmp_fag;
12542 /* End of the region.
12543 Check whether it is worth to do fp-as-gp. */
12545 if (begin_rel == NULL)
12547 /* xgettext:c-format */
12548 _bfd_error_handler (_("%pB: Unmatched OMIT_FP in %pA."), abfd, sec);
12552 accu = nds32_fag_find_base (&fag_head, &best_fag);
12554 /* Clean FP7U2_FLAG because they may set ever. */
12555 tmp_fag = fag_head.next;
12556 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12558 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12559 if (accu < FAG_THRESHOLD
12560 || !nds32_fag_mark_relax (link_info, abfd, best_fag,
12561 internal_relocs, irelend))
12563 /* Not worth to do fp-as-gp. */
12564 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12565 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12566 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12567 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12568 nds32_fag_free_list (&fag_head);
12573 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12574 so we use it to record the distance to the reloction of best
12576 dist = best_fag->relas[0] - begin_rel;
12577 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12578 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12579 relocation. And get the base value when relocating. */
12580 begin_rel->r_addend &= (0x1 << 16) - 1;
12581 begin_rel->r_addend |= dist << 16;
12583 nds32_fag_free_list (&fag_head);
12587 if (begin_rel == NULL || ifc_inside)
12588 /* Skip if we are not in the region of fp-as-gp. */
12591 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12592 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12597 /* A gp-relative access is found. Insert it to the fag-list. */
12599 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
12600 insn = bfd_getb32 (contents + irel->r_offset);
12601 if (!N32_IS_RT3 (insn))
12604 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12605 nds32_fag_insert (&fag_head, addr, irel);
12607 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12611 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12612 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12614 /* Suppress fp as gp when encounter ifc. */
12622 /* Remove unused `la $fp, _FD_BASE_' instruction. */
12625 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12626 Elf_Internal_Rela *internal_relocs,
12627 Elf_Internal_Rela *irelend)
12629 Elf_Internal_Rela *irel;
12630 Elf_Internal_Shdr *symtab_hdr;
12631 bfd_byte *contents = NULL;
12632 nds32_elf_blank_t *relax_blank_list = NULL;
12633 bfd_boolean result = TRUE;
12634 bfd_boolean unused_region = FALSE;
12637 NOTE: Disable fp-as-gp if we encounter ifcall relocations.
12638 * R_NDS32_17IFC_PCREL_RELA
12639 * R_NDS32_10IFCU_PCREL_RELA
12644 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12645 nds32_get_section_contents (abfd, sec, &contents, TRUE);
12647 for (irel = internal_relocs; irel < irelend; irel++)
12649 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
12650 we marked to in previous pass.
12651 DO NOT scan relocations again, since we've alreadly decided it
12652 and set the flag. */
12653 const char *syname;
12657 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12658 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12659 unused_region = TRUE;
12660 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12661 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12662 unused_region = FALSE;
12664 /* We're not in the region. */
12665 if (!unused_region)
12668 /* _FP_BASE_ must be a GLOBAL symbol. */
12669 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
12670 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
12673 /* The symbol name must be _FP_BASE_. */
12674 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
12675 if (strcmp (syname, FP_BASE_NAME) != 0)
12678 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
12680 /* addi.gp $fp, -256 */
12681 insn = bfd_getb32 (contents + irel->r_offset);
12682 if (insn != INSN_ADDIGP_TO_FP)
12685 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
12687 /* addi $fp, $gp, -256 */
12688 insn = bfd_getb32 (contents + irel->r_offset);
12689 if (insn != INSN_ADDI_GP_TO_FP)
12692 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
12694 /* movi $fp, FP_BASE */
12695 insn = bfd_getb32 (contents + irel->r_offset);
12696 if (insn != INSN_MOVI_TO_FP)
12702 /* We got here because a FP_BASE instruction is found. */
12703 if (!insert_nds32_elf_blank_recalc_total
12704 (&relax_blank_list, irel->r_offset, 4))
12709 if (relax_blank_list)
12711 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12712 relax_blank_list = NULL;
12721 /* This is a version of bfd_generic_get_relocated_section_contents.
12722 We need this variety because relaxation will modify the dwarf
12723 infomation. When there is undefined symbol reference error mesage,
12724 linker need to dump line number where the symbol be used. However
12725 the address is be relaxed, it can not get the original dwarf contents.
12726 The variety only modify function call for reading in the section. */
12729 nds32_elf_get_relocated_section_contents (bfd *abfd,
12730 struct bfd_link_info *link_info,
12731 struct bfd_link_order *link_order,
12733 bfd_boolean relocatable,
12736 bfd *input_bfd = link_order->u.indirect.section->owner;
12737 asection *input_section = link_order->u.indirect.section;
12739 arelent **reloc_vector;
12742 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12743 if (reloc_size < 0)
12746 /* Read in the section. */
12747 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
12750 if (reloc_size == 0)
12753 reloc_vector = (arelent **) bfd_malloc (reloc_size);
12754 if (reloc_vector == NULL)
12757 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
12758 reloc_vector, symbols);
12759 if (reloc_count < 0)
12762 if (reloc_count > 0)
12765 for (parent = reloc_vector; *parent != NULL; parent++)
12767 char *error_message = NULL;
12769 bfd_reloc_status_type r;
12771 symbol = *(*parent)->sym_ptr_ptr;
12772 if (symbol->section && discarded_section (symbol->section))
12775 static reloc_howto_type none_howto
12776 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
12777 "unused", FALSE, 0, 0, FALSE);
12779 p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
12780 _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
12782 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
12783 (*parent)->addend = 0;
12784 (*parent)->howto = &none_howto;
12788 r = bfd_perform_relocation (input_bfd, *parent, data,
12790 relocatable ? abfd : NULL,
12795 asection *os = input_section->output_section;
12797 /* A partial link, so keep the relocs. */
12798 os->orelocation[os->reloc_count] = *parent;
12802 if (r != bfd_reloc_ok)
12806 case bfd_reloc_undefined:
12807 (*link_info->callbacks->undefined_symbol)
12808 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12809 input_bfd, input_section, (*parent)->address, TRUE);
12811 case bfd_reloc_dangerous:
12812 BFD_ASSERT (error_message != NULL);
12813 (*link_info->callbacks->reloc_dangerous)
12814 (link_info, error_message,
12815 input_bfd, input_section, (*parent)->address);
12817 case bfd_reloc_overflow:
12818 (*link_info->callbacks->reloc_overflow)
12820 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12821 (*parent)->howto->name, (*parent)->addend,
12822 input_bfd, input_section, (*parent)->address);
12824 case bfd_reloc_outofrange:
12826 This error can result when processing some partially
12827 complete binaries. Do not abort, but issue an error
12828 message instead. */
12829 link_info->callbacks->einfo
12830 /* xgettext:c-format */
12831 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
12832 abfd, input_section, * parent);
12843 free (reloc_vector);
12847 free (reloc_vector);
12851 /* Link-time IFC relaxation.
12852 In this optimization, we chains jump instructions
12853 of the same destination with ifcall. */
12856 /* List to save jal and j relocation. */
12857 struct elf_nds32_ifc_symbol_entry
12860 struct elf_link_hash_entry *h;
12861 struct elf_nds32_ifc_irel_list *irel_head;
12862 unsigned long insn;
12864 int enable; /* Apply ifc. */
12865 int ex9_enable; /* Apply ifc after ex9. */
12866 struct elf_nds32_ifc_symbol_entry *next;
12869 struct elf_nds32_ifc_irel_list
12871 Elf_Internal_Rela *irel;
12874 /* If this is set, then it is the last instruction for
12875 ifc-chain, so it must be keep for the actual branching. */
12877 struct elf_nds32_ifc_irel_list *next;
12880 static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;
12882 /* Insert symbol of jal and j for ifc. */
12885 nds32_elf_ifc_insert_symbol (asection *sec,
12886 struct elf_link_hash_entry *h,
12887 Elf_Internal_Rela *irel,
12888 unsigned long insn)
12890 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
12892 /* Check there is target of existing entry the same as the new one. */
12893 while (ptr != NULL)
12895 if (((h == NULL && ptr->sec == sec
12896 && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
12897 && ptr->irel_head->irel->r_addend == irel->r_addend)
12900 && ptr->insn == insn)
12902 /* The same target exist, so insert into list. */
12903 struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;
12905 while (irel_list->next != NULL)
12906 irel_list = irel_list->next;
12907 irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
12908 irel_list = irel_list->next;
12909 irel_list->irel = irel;
12910 irel_list->keep = 1;
12913 irel_list->sec = NULL;
12915 irel_list->sec = sec;
12916 irel_list->next = NULL;
12919 if (ptr->next == NULL)
12924 /* There is no same target entry, so build a new one. */
12925 if (ifc_symbol_head == NULL)
12927 ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
12928 ptr = ifc_symbol_head;
12932 ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
12937 ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
12938 ptr->irel_head->irel = irel;
12940 ptr->irel_head->keep = 1;
12944 /* Local symbols. */
12946 ptr->irel_head->sec = NULL;
12950 /* Global symbol. */
12952 ptr->irel_head->sec = sec;
12955 ptr->irel_head->next = NULL;
12958 ptr->ex9_enable = 0;
12962 /* Gather all jal and j instructions. */
12965 nds32_elf_ifc_calc (struct bfd_link_info *info,
12966 bfd *abfd, asection *sec)
12968 Elf_Internal_Rela *internal_relocs;
12969 Elf_Internal_Rela *irelend;
12970 Elf_Internal_Rela *irel;
12971 Elf_Internal_Shdr *symtab_hdr;
12972 bfd_byte *contents = NULL;
12973 uint32_t insn, insn_with_reg;
12974 unsigned long r_symndx;
12975 struct elf_link_hash_entry *h;
12976 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
12977 struct elf_nds32_link_hash_table *table;
12978 bfd_boolean ifc_loop_aware;
12980 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12981 TRUE /* keep_memory */);
12982 irelend = internal_relocs + sec->reloc_count;
12983 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12985 /* Check if the object enable ifc. */
12986 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
12987 R_NDS32_RELAX_ENTRY);
12991 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
12992 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
12993 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
12996 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
12999 table = nds32_elf_hash_table (info);
13000 ifc_loop_aware = table->ifc_loop_aware;
13001 while (irel != NULL && irel < irelend)
13003 /* Traverse all relocation and gather all of them to build the list. */
13005 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
13007 if (ifc_loop_aware == 1
13008 && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
13010 /* Check the region if loop or not. If it is true and
13011 ifc-loop-aware is true, ignore the region till region end. */
13012 while (irel != NULL
13014 && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
13015 || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
13020 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
13022 insn = bfd_getb32 (contents + irel->r_offset);
13023 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
13024 r_symndx = ELF32_R_SYM (irel->r_info);
13025 if (r_symndx < symtab_hdr->sh_info)
13027 /* Local symbol. */
13028 nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
13032 /* External symbol. */
13033 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13034 nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
13042 /* Determine whether j and jal should be substituted. */
13045 nds32_elf_ifc_filter (struct bfd_link_info *info)
13047 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13048 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13049 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13050 struct elf_nds32_link_hash_table *table;
13051 int target_optimize;
13054 table = nds32_elf_hash_table (info);
13055 target_optimize = table->target_optimize;
13058 irel_ptr = ptr->irel_head;
13059 if (ptr->h == NULL)
13061 /* Local symbol. */
13062 irel_keeper = irel_ptr;
13063 while (irel_ptr && irel_ptr->next)
13065 /* Check there is jump target can be used. */
13066 if ((irel_ptr->next->irel->r_offset
13067 - irel_keeper->irel->r_offset) > 1022)
13068 irel_keeper = irel_ptr->next;
13072 irel_ptr->keep = 0;
13074 irel_ptr = irel_ptr->next;
13079 /* Global symbol. */
13080 /* We have to get the absolute address and decide
13081 whether to keep it or not. */
13084 address = (irel_ptr->irel->r_offset
13085 + irel_ptr->sec->output_section->vma
13086 + irel_ptr->sec->output_offset);
13087 irel_ptr->addr = address;
13088 irel_ptr = irel_ptr->next;
13091 irel_ptr = ptr->irel_head;
13094 /* Sort by address. */
13095 struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
13096 struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
13097 struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
13098 struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;
13100 /* Get the smallest one. */
13101 while (irel_temp->next)
13103 if (irel_temp->next->addr < irel_dest->addr)
13105 irel_dest_prev = irel_temp;
13106 irel_dest = irel_temp->next;
13108 irel_temp = irel_temp->next;
13111 if (irel_dest != irel_ptr)
13114 irel_ptr_prev->next = irel_dest;
13115 if (irel_dest_prev)
13116 irel_dest_prev->next = irel_ptr;
13117 irel_temp = irel_ptr->next;
13118 irel_ptr->next = irel_dest->next;
13119 irel_dest->next = irel_temp;
13121 irel_ptr_prev = irel_ptr;
13122 irel_ptr = irel_ptr->next;
13125 irel_ptr = ptr->irel_head;
13126 irel_keeper = irel_ptr;
13127 while (irel_ptr && irel_ptr->next)
13129 if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
13130 irel_keeper = irel_ptr->next;
13134 irel_ptr->keep = 0;
13136 irel_ptr = irel_ptr->next;
13140 /* Ex9 enable. Reserve it for ex9. */
13141 if ((target_optimize & NDS32_RELAX_EX9_ON)
13142 && ptr->irel_head != irel_keeper)
13148 /* Determine whether j and jal should be substituted after ex9 done. */
13151 nds32_elf_ifc_filter_after_ex9 (void)
13153 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13154 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13158 if (ptr->enable == 0)
13160 /* Check whether ifc is applied or not. */
13161 irel_ptr = ptr->irel_head;
13162 ptr->ex9_enable = 1;
13165 if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
13168 ptr->ex9_enable = 0;
13171 irel_ptr = irel_ptr->next;
13178 /* Wrapper to do ifc relaxation. */
13181 nds32_elf_ifc_finish (struct bfd_link_info *info)
13184 struct elf_nds32_link_hash_table *table;
13186 table = nds32_elf_hash_table (info);
13187 relax_status = table->relax_status;
13189 if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
13190 nds32_elf_ifc_filter (info);
13192 nds32_elf_ifc_filter_after_ex9 ();
13194 if (!nds32_elf_ifc_replace (info))
13198 table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
13202 /* Traverse the result of ifc filter and replace it with ifcall9. */
13205 nds32_elf_ifc_replace (struct bfd_link_info *info)
13207 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13208 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13209 nds32_elf_blank_t *relax_blank_list = NULL;
13210 bfd_byte *contents = NULL;
13211 Elf_Internal_Rela *internal_relocs;
13212 Elf_Internal_Rela *irel;
13213 Elf_Internal_Rela *irelend;
13214 unsigned short insn16 = INSN_IFCALL9;
13215 struct elf_nds32_link_hash_table *table;
13218 table = nds32_elf_hash_table (info);
13219 relax_status = table->relax_status;
13223 /* Traverse the ifc gather list, and replace the
13224 filter entries by ifcall9. */
13225 if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
13226 || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE)
13227 && ptr->ex9_enable == 1))
13229 irel_ptr = ptr->irel_head;
13230 if (ptr->h == NULL)
13232 /* Local symbol. */
13233 internal_relocs = _bfd_elf_link_read_relocs
13234 (ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
13235 irelend = internal_relocs + ptr->sec->reloc_count;
13237 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13243 if (irel_ptr->keep == 0 && irel_ptr->next)
13245 /* The one can be replaced. We have to check whether
13246 there is any alignment point in the region. */
13247 irel = irel_ptr->irel;
13248 while (((irel_ptr->next->keep == 0
13249 && irel < irel_ptr->next->irel)
13250 || (irel_ptr->next->keep == 1 && irel < irelend))
13251 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13252 && (irel->r_addend & 0x1f) == 2))
13254 if (irel >= irelend
13255 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13256 && (irel->r_addend & 0x1f) == 2
13257 && ((irel->r_offset - get_nds32_elf_blank_total
13258 (&relax_blank_list, irel->r_offset, 1))
13261 /* Replace by ifcall9. */
13262 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13263 if (!insert_nds32_elf_blank_recalc_total
13264 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13266 irel_ptr->irel->r_info =
13267 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13268 R_NDS32_10IFCU_PCREL_RELA);
13271 irel_ptr = irel_ptr->next;
13274 /* Delete the redundant code. */
13275 if (relax_blank_list)
13277 nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
13279 relax_blank_list = NULL;
13284 /* Global symbol. */
13287 if (irel_ptr->keep == 0 && irel_ptr->next)
13289 /* The one can be replaced, and we have to check
13290 whether there is any alignment point in the region. */
13291 internal_relocs = _bfd_elf_link_read_relocs
13292 (irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
13293 TRUE /* keep_memory */);
13294 irelend = internal_relocs + irel_ptr->sec->reloc_count;
13295 if (!nds32_get_section_contents (irel_ptr->sec->owner,
13296 irel_ptr->sec, &contents,
13300 irel = irel_ptr->irel;
13301 while (((irel_ptr->sec == irel_ptr->next->sec
13302 && irel_ptr->next->keep == 0
13303 && irel < irel_ptr->next->irel)
13304 || ((irel_ptr->sec != irel_ptr->next->sec
13305 || irel_ptr->next->keep == 1)
13306 && irel < irelend))
13307 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13308 && (irel->r_addend & 0x1f) == 2))
13310 if (irel >= irelend
13311 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13312 && (irel->r_addend & 0x1f) == 2
13313 && ((irel->r_offset
13314 - get_nds32_elf_blank_total (&relax_blank_list,
13315 irel->r_offset, 1)) & 0x02) == 0))
13317 /* Replace by ifcall9. */
13318 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13319 if (!insert_nds32_elf_blank_recalc_total
13320 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13323 /* Delete the redundant code, and clear the relocation. */
13324 nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
13327 irel_ptr->irel->r_info =
13328 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13329 R_NDS32_10IFCU_PCREL_RELA);
13330 relax_blank_list = NULL;
13334 irel_ptr = irel_ptr->next;
13344 /* Relocate ifcall. */
13347 nds32_elf_ifc_reloc (void)
13349 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13350 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13351 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13352 bfd_vma relocation, address;
13353 unsigned short insn16;
13354 bfd_byte *contents = NULL;
13355 static bfd_boolean done = FALSE;
13364 /* Check the entry is enable ifcall. */
13365 if (ptr->enable == 1 || ptr->ex9_enable == 1)
13367 /* Get the reserve jump. */
13368 irel_ptr = ptr->irel_head;
13371 if (irel_ptr->keep == 1)
13373 irel_keeper = irel_ptr;
13376 irel_ptr = irel_ptr->next;
13379 irel_ptr = ptr->irel_head;
13380 if (ptr->h == NULL)
13382 /* Local symbol. */
13383 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13389 if (irel_ptr->keep == 0
13390 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13392 relocation = irel_keeper->irel->r_offset;
13393 relocation = relocation - irel_ptr->irel->r_offset;
13394 while (irel_keeper && relocation > 1022)
13396 irel_keeper = irel_keeper->next;
13397 if (irel_keeper && irel_keeper->keep == 1)
13399 relocation = irel_keeper->irel->r_offset;
13400 relocation = relocation - irel_ptr->irel->r_offset;
13403 if (relocation > 1022)
13405 /* Double check. */
13406 irel_keeper = ptr->irel_head;
13407 while (irel_keeper)
13409 if (irel_keeper->keep == 1)
13411 relocation = irel_keeper->irel->r_offset;
13412 relocation = relocation - irel_ptr->irel->r_offset;
13414 if (relocation <= 1022)
13416 irel_keeper = irel_keeper->next;
13421 irel_ptr->irel->r_info =
13422 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13424 insn16 = INSN_IFCALL9 | (relocation >> 1);
13425 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13427 irel_ptr = irel_ptr->next;
13432 /* Global symbol. */
13435 if (irel_ptr->keep == 0
13436 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13438 /* Get the distance between ifcall and jump. */
13439 relocation = (irel_keeper->irel->r_offset
13440 + irel_keeper->sec->output_section->vma
13441 + irel_keeper->sec->output_offset);
13442 address = (irel_ptr->irel->r_offset
13443 + irel_ptr->sec->output_section->vma
13444 + irel_ptr->sec->output_offset);
13445 relocation = relocation - address;
13447 /* The distance is over ragne, find callee again. */
13448 while (irel_keeper && relocation > 1022)
13450 irel_keeper = irel_keeper->next;
13451 if (irel_keeper && irel_keeper->keep ==1)
13453 relocation = (irel_keeper->irel->r_offset
13454 + irel_keeper->sec->output_section->vma
13455 + irel_keeper->sec->output_offset);
13456 relocation = relocation - address;
13460 if (relocation > 1022)
13462 /* Double check. */
13463 irel_keeper = ptr->irel_head;
13464 while (irel_keeper)
13466 if (irel_keeper->keep == 1)
13469 relocation = (irel_keeper->irel->r_offset
13470 + irel_keeper->sec->output_section->vma
13471 + irel_keeper->sec->output_offset);
13472 relocation = relocation - address;
13474 if (relocation <= 1022)
13476 irel_keeper = irel_keeper->next;
13481 if (!nds32_get_section_contents
13482 (irel_ptr->sec->owner, irel_ptr->sec, &contents, TRUE))
13484 insn16 = INSN_IFCALL9 | (relocation >> 1);
13485 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13486 irel_ptr->irel->r_info =
13487 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13490 irel_ptr =irel_ptr->next;
13500 /* End of IFC relaxation. */
13502 /* EX9 Instruction Table Relaxation. */
13504 /* Global hash list. */
13505 struct elf_link_hash_entry_list
13507 struct elf_link_hash_entry *h;
13508 struct elf_link_hash_entry_list *next;
13511 /* Save different destination but same insn. */
13512 struct elf_link_hash_entry_mul_list
13514 /* Global symbol times. */
13516 /* Save relocation for each global symbol but useful?? */
13517 Elf_Internal_Rela *irel;
13518 /* For sethi, two sethi may have the same high-part but different low-parts. */
13519 Elf_Internal_Rela rel_backup;
13520 struct elf_link_hash_entry_list *h_list;
13521 struct elf_link_hash_entry_mul_list *next;
13524 /* Instruction hash table. */
13525 struct elf_nds32_code_hash_entry
13527 struct bfd_hash_entry root;
13529 /* For insn that can use relocation or constant ex: sethi. */
13532 struct elf_link_hash_entry_mul_list *m_list;
13533 /* Using r_addend. */
13534 Elf_Internal_Rela *irel;
13535 /* Using r_info. */
13536 Elf_Internal_Rela rel_backup;
13539 /* Instruction count list. */
13540 struct elf_nds32_insn_times_entry
13542 const char *string;
13546 struct elf_link_hash_entry_mul_list *m_list;
13547 Elf_Internal_Rela *irel;
13548 Elf_Internal_Rela rel_backup;
13549 struct elf_nds32_insn_times_entry *next;
13552 /* J and JAL symbol list. */
13553 struct elf_nds32_symbol_entry
13556 unsigned long insn;
13557 struct elf_nds32_symbol_entry *next;
13560 /* Relocation list. */
13561 struct elf_nds32_irel_entry
13563 Elf_Internal_Rela *irel;
13564 struct elf_nds32_irel_entry *next;
13567 /* ex9.it insn need to be fixed. */
13568 struct elf_nds32_ex9_refix
13570 Elf_Internal_Rela *irel;
13572 struct elf_link_hash_entry *h;
13574 struct elf_nds32_ex9_refix *next;
13577 static struct bfd_hash_table ex9_code_table;
13578 static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
13579 static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;
13581 /* EX9 hash function. */
13583 static struct bfd_hash_entry *
13584 nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
13585 struct bfd_hash_table *table,
13586 const char *string)
13588 struct elf_nds32_code_hash_entry *ret;
13590 /* Allocate the structure if it has not already been allocated by a
13594 entry = (struct bfd_hash_entry *)
13595 bfd_hash_allocate (table, sizeof (*ret));
13600 /* Call the allocation method of the superclass. */
13601 entry = bfd_hash_newfunc (entry, table, string);
13605 ret = (struct elf_nds32_code_hash_entry*) entry;
13607 ret->const_insn = 0;
13608 ret->m_list = NULL;
13614 /* Insert ex9 entry
13615 this insert must be stable sorted by times. */
13618 nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
13620 struct elf_nds32_insn_times_entry *temp;
13621 struct elf_nds32_insn_times_entry *temp2;
13623 if (ex9_insn_head == NULL)
13625 ex9_insn_head = ptr;
13630 temp = ex9_insn_head;
13631 temp2 = ex9_insn_head;
13632 while (temp->next &&
13633 (temp->next->times >= ptr->times
13634 || temp->times == -1))
13636 if (temp->times == -1)
13640 if (ptr->times > temp->times && temp->times != -1)
13643 if (temp2->times == -1)
13646 ex9_insn_head = ptr;
13648 else if (temp->next == NULL)
13655 ptr->next = temp->next;
13661 /* Examine each insn times in hash table.
13662 Handle multi-link hash entry.
13664 TODO: This function doesn't assign so much info since it is fake. */
13667 nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
13669 struct elf_nds32_insn_times_entry *ptr;
13672 if (h->m_list == NULL)
13674 /* Local symbol insn or insn without relocation. */
13678 ptr = (struct elf_nds32_insn_times_entry *)
13679 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13680 ptr->times = h->times;
13681 ptr->string = h->root.string;
13682 ptr->m_list = NULL;
13684 ptr->irel = h->irel;
13685 ptr->rel_backup = h->rel_backup;
13686 nds32_elf_ex9_insert_entry (ptr);
13690 /* Global symbol insn. */
13691 /* Only sethi insn has multiple m_list. */
13692 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13697 times += m_list->times;
13698 m_list = m_list->next;
13702 m_list = h->m_list;
13703 ptr = (struct elf_nds32_insn_times_entry *)
13704 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13705 ptr->times = times; /* Use the total times. */
13706 ptr->string = h->root.string;
13707 ptr->m_list = m_list;
13709 ptr->irel = m_list->irel;
13710 ptr->rel_backup = m_list->rel_backup;
13711 nds32_elf_ex9_insert_entry (ptr);
13713 if (h->const_insn == 1)
13715 /* sethi with constant value. */
13719 ptr = (struct elf_nds32_insn_times_entry *)
13720 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13721 ptr->times = h->times;
13722 ptr->string = h->root.string;
13723 ptr->m_list = NULL;
13726 ptr->rel_backup = h->rel_backup;
13727 nds32_elf_ex9_insert_entry (ptr);
13733 /* Count each insn times in hash table.
13734 Handle multi-link hash entry. */
13737 nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
13739 int reservation, times;
13740 unsigned long relocation, min_relocation;
13741 struct elf_nds32_insn_times_entry *ptr;
13743 if (h->m_list == NULL)
13745 /* Local symbol insn or insn without relocation. */
13748 ptr = (struct elf_nds32_insn_times_entry *)
13749 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13750 ptr->times = h->times;
13751 ptr->string = h->root.string;
13752 ptr->m_list = NULL;
13754 ptr->irel = h->irel;
13755 ptr->rel_backup = h->rel_backup;
13756 nds32_elf_ex9_insert_entry (ptr);
13760 /* Global symbol insn. */
13761 /* Only sethi insn has multiple m_list. */
13762 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13764 if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
13765 && m_list->next != NULL)
13767 /* Sethi insn has different symbol or addend but has same hi20. */
13771 min_relocation = 0xffffffff;
13774 /* Get the minimum sethi address
13775 and calculate how many entry the sethi-list have to use. */
13776 if ((m_list->h_list->h->root.type == bfd_link_hash_defined
13777 || m_list->h_list->h->root.type == bfd_link_hash_defweak)
13778 && (m_list->h_list->h->root.u.def.section != NULL
13779 && m_list->h_list->h->root.u.def.section->output_section != NULL))
13781 relocation = (m_list->h_list->h->root.u.def.value +
13782 m_list->h_list->h->root.u.def.section->output_section->vma +
13783 m_list->h_list->h->root.u.def.section->output_offset);
13784 relocation += m_list->irel->r_addend;
13788 if (relocation < min_relocation)
13789 min_relocation = relocation;
13790 times += m_list->times;
13791 m_list = m_list->next;
13793 if (min_relocation < ex9_relax_size)
13794 reservation = (min_relocation >> 12) + 1;
13796 reservation = (min_relocation >> 12)
13797 - ((min_relocation - ex9_relax_size) >> 12) + 1;
13798 if (reservation < (times / 3))
13800 /* Efficient enough to use ex9. */
13803 for (i = reservation ; i > 0; i--)
13805 /* Allocate number of reservation ex9 entry. */
13806 ptr = (struct elf_nds32_insn_times_entry *)
13807 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13808 ptr->times = h->m_list->times / reservation;
13809 ptr->string = h->root.string;
13810 ptr->m_list = h->m_list;
13812 ptr->irel = h->m_list->irel;
13813 ptr->rel_backup = h->m_list->rel_backup;
13814 nds32_elf_ex9_insert_entry (ptr);
13820 /* Normal global symbol that means no different address symbol
13821 using same ex9 entry. */
13822 if (m_list->times >= 3)
13824 ptr = (struct elf_nds32_insn_times_entry *)
13825 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13826 ptr->times = m_list->times;
13827 ptr->string = h->root.string;
13828 ptr->m_list = h->m_list;
13830 ptr->irel = h->m_list->irel;
13831 ptr->rel_backup = h->m_list->rel_backup;
13832 nds32_elf_ex9_insert_entry (ptr);
13836 if (h->const_insn == 1)
13838 /* sethi with constant value. */
13842 ptr = (struct elf_nds32_insn_times_entry *)
13843 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13844 ptr->times = h->times;
13845 ptr->string = h->root.string;
13846 ptr->m_list = NULL;
13849 ptr->rel_backup = h->rel_backup;
13850 nds32_elf_ex9_insert_entry (ptr);
13857 /* Hash table traverse function. */
13860 nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
13864 ex9_code_table.frozen = 1;
13865 for (i = 0; i < ex9_code_table.size; i++)
13867 struct bfd_hash_entry *p;
13869 for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
13870 if (!func ((struct elf_nds32_code_hash_entry *) p))
13874 ex9_code_table.frozen = 0;
13878 /* Give order number to insn list. */
13881 nds32_elf_order_insn_times (struct bfd_link_info *info)
13883 struct elf_nds32_insn_times_entry *ex9_insn;
13884 struct elf_nds32_insn_times_entry *temp = NULL;
13885 struct elf_nds32_link_hash_table *table;
13889 if (ex9_insn_head == NULL)
13892 /* The max number of entries is 512. */
13893 ex9_insn = ex9_insn_head;
13894 table = nds32_elf_hash_table (info);
13895 ex9_limit = table->ex9_limit;
13897 ex9_insn = ex9_insn_head;
13899 while (ex9_insn != NULL && number < ex9_limit)
13901 ex9_insn->order = number;
13904 ex9_insn = ex9_insn->next;
13907 if (ex9_insn && temp)
13910 while (ex9_insn != NULL)
13912 /* Free useless entry. */
13914 ex9_insn = ex9_insn->next;
13919 /* Build .ex9.itable section. */
13922 nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
13924 asection *table_sec;
13925 struct elf_nds32_insn_times_entry *ptr;
13928 bfd_byte *contents = NULL;
13930 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
13931 it_abfd = it_abfd->link.next)
13933 /* Find the section .ex9.itable, and put all entries into it. */
13934 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
13935 if (table_sec != NULL)
13937 if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
13940 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
13943 table_sec->size = number * 4;
13948 elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
13950 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
13954 val = strtol (ptr->string, NULL, 16);
13955 bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
13963 /* Get insn with regs according to relocation type. */
13966 nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
13967 uint32_t insn, uint32_t *insn_with_reg)
13969 reloc_howto_type *howto = NULL;
13972 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
13973 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
13974 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
13976 *insn_with_reg = insn;
13980 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
13981 *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
13984 /* Mask number of address bits according to relocation. */
13986 static unsigned long
13987 nds32_elf_irel_mask (Elf_Internal_Rela *irel)
13989 reloc_howto_type *howto = NULL;
13992 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
13993 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
13994 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
13997 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
13998 return howto->dst_mask;
14002 nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
14003 struct elf_nds32_irel_entry *irel_ptr)
14005 if (*irel_list == NULL)
14007 *irel_list = irel_ptr;
14008 irel_ptr->next = NULL;
14012 irel_ptr->next = *irel_list;
14013 *irel_list = irel_ptr;
14018 nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
14019 struct elf_link_hash_entry *h, int order)
14021 struct elf_nds32_ex9_refix *ptr;
14023 ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
14027 ptr->order = order;
14030 if (ex9_refix_head == NULL)
14031 ex9_refix_head = ptr;
14034 struct elf_nds32_ex9_refix *temp = ex9_refix_head;
14036 while (temp->next != NULL)
14045 CLEAN_PRE = 1 << 1,
14049 /* Check relocation type if supporting for ex9. */
14052 nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
14053 Elf_Internal_Rela **irel,
14054 Elf_Internal_Rela *irelend,
14055 nds32_elf_blank_t *relax_blank_list,
14056 asection *sec,bfd_vma *off,
14057 bfd_byte *contents)
14059 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
14060 bfd_boolean nested_ex9, nested_loop;
14061 bfd_boolean ex9_loop_aware;
14062 /* We use the highest 1 byte of result to record
14063 how many bytes location counter has to move. */
14065 Elf_Internal_Rela *irel_save = NULL;
14066 struct elf_nds32_link_hash_table *table;
14068 table = nds32_elf_hash_table (info);
14069 ex9_loop_aware = table->ex9_loop_aware;
14071 while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
14073 switch (ELF32_R_TYPE ((*irel)->r_info))
14075 case R_NDS32_RELAX_REGION_BEGIN:
14076 /* Ignore code block. */
14077 nested_ex9 = FALSE;
14078 nested_loop = FALSE;
14079 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
14081 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
14083 /* Check the region if loop or not. If it is true and
14084 ex9-loop-aware is true, ignore the region till region end. */
14085 /* To save the status for in .no_relax ex9 region and
14086 loop region to conform the block can do ex9 relaxation. */
14087 nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
14088 nested_loop = (ex9_loop_aware
14089 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
14090 while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
14093 if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
14095 /* There may be nested region. */
14096 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14098 else if (ex9_loop_aware
14099 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14100 nested_loop = TRUE;
14102 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
14104 /* The end of region. */
14105 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14106 nested_ex9 = FALSE;
14107 else if (ex9_loop_aware
14108 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14109 nested_loop = FALSE;
14111 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
14112 && ((*irel)->r_addend & 0x1f) == 2)
14114 /* Alignment exist in the region. */
14115 result |= CLEAN_PRE;
14116 if (((*irel)->r_offset -
14117 get_nds32_elf_blank_total (&relax_blank_list,
14118 (*irel)->r_offset, 0)) & 0x02)
14119 result |= PUSH_PRE;
14122 if ((*irel) >= irelend)
14125 *off = (*irel)->r_offset;
14127 /* The final instruction in the region, regard this one as data to ignore it. */
14128 result |= DATA_EXIST;
14133 case R_NDS32_LABEL:
14134 if (((*irel)->r_addend & 0x1f) == 2)
14136 /* Check this point is align and decide to do ex9 or not. */
14137 result |= CLEAN_PRE;
14138 if (((*irel)->r_offset -
14139 get_nds32_elf_blank_total (&relax_blank_list,
14140 (*irel)->r_offset, 0)) & 0x02)
14141 result |= PUSH_PRE;
14144 case R_NDS32_32_RELA:
14146 result |= (4 << 24);
14147 result |= DATA_EXIST;
14149 case R_NDS32_16_RELA:
14151 result |= (2 << 24);
14152 result |= DATA_EXIST;
14156 /* The least code alignment is 2. If the data is only one byte,
14157 we have to shift one more byte. */
14158 if ((*irel)->r_addend == 1)
14159 result |= ((*irel)->r_addend << 25) ;
14161 result |= ((*irel)->r_addend << 24) ;
14163 result |= DATA_EXIST;
14166 case R_NDS32_25_PCREL_RELA:
14167 case R_NDS32_SDA16S3_RELA:
14168 case R_NDS32_SDA15S3_RELA:
14169 case R_NDS32_SDA15S3:
14170 case R_NDS32_SDA17S2_RELA:
14171 case R_NDS32_SDA15S2_RELA:
14172 case R_NDS32_SDA12S2_SP_RELA:
14173 case R_NDS32_SDA12S2_DP_RELA:
14174 case R_NDS32_SDA15S2:
14175 case R_NDS32_SDA18S1_RELA:
14176 case R_NDS32_SDA15S1_RELA:
14177 case R_NDS32_SDA15S1:
14178 case R_NDS32_SDA19S0_RELA:
14179 case R_NDS32_SDA15S0_RELA:
14180 case R_NDS32_SDA15S0:
14181 case R_NDS32_HI20_RELA:
14182 case R_NDS32_LO12S0_ORI_RELA:
14183 case R_NDS32_LO12S0_RELA:
14184 case R_NDS32_LO12S1_RELA:
14185 case R_NDS32_LO12S2_RELA:
14186 /* These relocation is supported ex9 relaxation currently. */
14187 /* We have to save the relocation for using later, since we have
14188 to check there is any alignment in the same address. */
14192 /* Not support relocations. */
14193 if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
14194 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE
14195 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_INSN16)
14197 /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
14198 But we have to consider if there is any side-effect. */
14199 if (!(result & DATA_EXIST))
14201 /* We have to confirm there is no data relocation in the
14202 same address. In general case, this won't happen. */
14203 /* We have to do ex9 conservative, for those relocation not
14204 considerd we ignore instruction. */
14205 result |= DATA_EXIST;
14206 if (*(contents + *off) & 0x80)
14207 result |= (2 << 24);
14209 result |= (4 << 24);
14214 if ((*irel) < irelend
14215 && ((*irel) + 1) < irelend
14216 && (*irel)->r_offset == ((*irel) + 1)->r_offset)
14217 /* There are relocations pointing to the same address, we have to
14218 check all of them. */
14230 /* Replace with ex9 instruction. */
14233 nds32_elf_ex9_push_insn (uint16_t insn16, bfd_byte *contents, bfd_vma pre_off,
14234 nds32_elf_blank_t **relax_blank_list,
14235 struct elf_nds32_irel_entry *pre_irel_ptr,
14236 struct elf_nds32_irel_entry **irel_list)
14240 /* Implement the ex9 relaxation. */
14241 bfd_putb16 (insn16, contents + pre_off);
14242 if (!insert_nds32_elf_blank_recalc_total (relax_blank_list,
14245 if (pre_irel_ptr != NULL)
14246 nds32_elf_insert_irel_entry (irel_list, pre_irel_ptr);
14251 /* Replace input file instruction which is in ex9 itable. */
14254 nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
14256 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14257 bfd_byte *contents = NULL;
14259 uint16_t insn16, insn_ex9;
14260 /* `pre_*' are used to track previous instruction that can use ex9.it. */
14261 bfd_vma pre_off = -1;
14262 uint16_t pre_insn16 = 0;
14263 struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
14264 Elf_Internal_Rela *internal_relocs;
14265 Elf_Internal_Rela *irel;
14266 Elf_Internal_Rela *irelend;
14267 Elf_Internal_Shdr *symtab_hdr;
14268 Elf_Internal_Sym *isym = NULL;
14269 nds32_elf_blank_t *relax_blank_list = NULL;
14271 uint32_t insn_with_reg = 0;
14273 uint32_t it_insn_with_reg;
14274 unsigned long r_symndx;
14276 struct elf_nds32_irel_entry *irel_list = NULL;
14277 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
14278 int data_flag, do_replace, save_irel;
14279 struct elf_link_hash_entry_list *h_list;
14282 /* Load section instructions, relocations, and symbol table. */
14283 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
14284 || !nds32_get_local_syms (abfd, sec, &isym))
14287 _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, TRUE /* keep_memory */);
14288 irelend = internal_relocs + sec->reloc_count;
14289 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14293 /* Check if the object enable ex9. */
14294 irel = find_relocs_at_address (internal_relocs, internal_relocs,
14295 irelend, R_NDS32_RELAX_ENTRY);
14297 /* Check this section trigger ex9 relaxation. */
14300 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
14301 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
14302 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
14305 irel = internal_relocs;
14307 /* Check alignment and fetch proper relocation. */
14308 while (off < sec->size)
14310 struct elf_link_hash_entry *h = NULL;
14311 struct elf_nds32_irel_entry *irel_ptr = NULL;
14313 /* Syn the instruction and the relocation. */
14314 while (irel != NULL && irel < irelend && irel->r_offset < off)
14317 data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
14318 relax_blank_list, sec,
14320 if (data_flag & PUSH_PRE)
14321 if (!nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14322 &relax_blank_list, pre_irel_ptr,
14326 if (data_flag & CLEAN_PRE)
14330 pre_irel_ptr = NULL;
14332 if (data_flag & DATA_EXIST)
14334 /* We save the move offset in the highest byte. */
14335 off += (data_flag >> 24);
14339 if (*(contents + off) & 0x80)
14341 /* 2-byte instruction. */
14346 /* Load the instruction and its opcode with register for comparing. */
14347 ex9_insn = ex9_insn_head;
14348 insn = bfd_getb32 (contents + off);
14352 it_insn = strtol (ex9_insn->string, NULL, 16);
14353 it_insn_with_reg = 0;
14357 if (irel != NULL && irel < irelend && irel->r_offset == off)
14359 /* Insn with relocation. */
14360 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
14362 if (ex9_insn->irel != NULL)
14363 nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn,
14364 &it_insn_with_reg);
14366 if (ex9_insn->irel != NULL
14367 && (ELF32_R_TYPE (irel->r_info) ==
14368 ELF32_R_TYPE (ex9_insn->irel->r_info))
14369 && (insn_with_reg == it_insn_with_reg))
14371 /* Insn relocation and format is the same as table entry. */
14373 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
14374 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
14375 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
14376 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
14377 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
14378 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
14379 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
14380 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
14381 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
14382 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
14383 && ELF32_R_TYPE (irel->r_info) <=
14384 R_NDS32_SDA12S2_SP_RELA)
14385 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
14386 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
14388 r_symndx = ELF32_R_SYM (irel->r_info);
14389 if (r_symndx < symtab_hdr->sh_info)
14391 /* Local symbol. */
14392 int shndx = isym[r_symndx].st_shndx;
14394 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14395 if (ex9_insn->sec == isec
14396 && ex9_insn->irel->r_addend == irel->r_addend
14397 && ex9_insn->irel->r_info == irel->r_info)
14405 /* External symbol. */
14406 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14407 if (ex9_insn->m_list)
14409 h_list = ex9_insn->m_list->h_list;
14413 && (ex9_insn->m_list->irel->r_addend ==
14420 h_list = h_list->next;
14425 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
14427 r_symndx = ELF32_R_SYM (irel->r_info);
14428 if (r_symndx < symtab_hdr->sh_info)
14430 /* Local symbols. Compare its base symbol and offset. */
14431 int shndx = isym[r_symndx].st_shndx;
14433 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14434 if (ex9_insn->sec == isec
14435 && ex9_insn->irel->r_addend == irel->r_addend
14436 && ex9_insn->irel->r_info == irel->r_info)
14444 /* External symbol. */
14445 struct elf_link_hash_entry_mul_list *m_list;
14447 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14448 m_list = ex9_insn->m_list;
14452 h_list = m_list->h_list;
14457 && (m_list->irel->r_addend
14458 == irel->r_addend))
14463 && ex9_insn->m_list
14464 && ex9_insn->m_list == ex9_insn->next->m_list)
14466 /* sethi multiple entry must be fixed */
14467 nds32_elf_ex9_insert_fix (sec, irel,
14468 h, ex9_insn->order);
14472 h_list = h_list->next;
14474 m_list = m_list->next;
14480 /* Import table: Check the symbol hash table and the
14481 jump target. Only R_NDS32_25_PCREL_RELA now. */
14482 else if (ex9_insn->times == -1
14483 && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
14485 nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
14486 if (insn_with_reg == it_insn_with_reg)
14489 bfd_vma relocation;
14491 r_symndx = ELF32_R_SYM (irel->r_info);
14492 if (r_symndx >= symtab_hdr->sh_info)
14494 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14495 if ((h->root.type == bfd_link_hash_defined
14496 || h->root.type == bfd_link_hash_defweak)
14497 && h->root.u.def.section != NULL
14498 && h->root.u.def.section->output_section != NULL
14499 && h->root.u.def.section->gc_mark == 1
14500 && bfd_is_abs_section (h->root.u.def.section)
14501 && h->root.u.def.value > sec->size)
14503 relocation = h->root.u.def.value +
14504 h->root.u.def.section->output_section->vma +
14505 h->root.u.def.section->output_offset;
14506 relocation += irel->r_addend;
14507 insn = insn_with_reg
14508 | ((relocation >> 1) & 0xffffff);
14509 snprintf (code, sizeof (code), "%08x", insn);
14510 if (strcmp (code, ex9_insn->string) == 0)
14519 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
14520 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
14521 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
14523 /* These relocations do not have to relocate contens, so it can
14524 be regard as instruction without relocation. */
14525 if (insn == it_insn && ex9_insn->irel == NULL)
14531 /* Instruction without relocation, we only
14532 have to compare their byte code. */
14533 if (insn == it_insn && ex9_insn->irel == NULL)
14537 /* Insntruction match so replacing the code here. */
14538 if (do_replace == 1)
14540 /* There are two formats of ex9 instruction. */
14541 if (ex9_insn->order < 32)
14542 insn_ex9 = INSN_EX9_IT_2;
14544 insn_ex9 = INSN_EX9_IT_1;
14545 insn16 = insn_ex9 | ex9_insn->order;
14547 /* Insert ex9 instruction. */
14548 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14549 &relax_blank_list, pre_irel_ptr,
14552 pre_insn16 = insn16;
14556 /* For instuction with relocation do relax. */
14557 irel_ptr = (struct elf_nds32_irel_entry *)
14558 bfd_malloc (sizeof (struct elf_nds32_irel_entry));
14559 irel_ptr->irel = irel;
14560 irel_ptr->next = NULL;
14561 pre_irel_ptr = irel_ptr;
14564 pre_irel_ptr = NULL;
14567 ex9_insn = ex9_insn->next;
14572 /* Insert ex9 instruction. */
14573 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14574 &relax_blank_list, pre_irel_ptr,
14577 /* Delete the redundant code. */
14578 if (relax_blank_list)
14580 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
14581 relax_blank_list = NULL;
14584 /* Clear the relocation that is replaced by ex9. */
14587 struct elf_nds32_irel_entry *irel_ptr;
14589 irel_ptr = irel_list;
14590 irel_list = irel_ptr->next;
14591 irel_ptr->irel->r_info =
14592 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
14598 /* Initialize ex9 hash table. */
14601 nds32_elf_ex9_init (void)
14603 if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
14604 sizeof (struct elf_nds32_code_hash_entry),
14607 _bfd_error_handler (_("Linker: cannot init ex9 hash table error \n"));
14613 /* Predict how many bytes will be relaxed with ex9 and ifc. */
14616 nds32_elf_ex9_total_relax (struct bfd_link_info *info)
14618 struct elf_nds32_insn_times_entry *ex9_insn;
14619 struct elf_nds32_insn_times_entry *temp;
14620 int target_optimize;
14621 struct elf_nds32_link_hash_table *table;
14623 if (ex9_insn_head == NULL)
14626 table = nds32_elf_hash_table (info);
14627 target_optimize = table->target_optimize;
14628 ex9_insn = ex9_insn_head;
14631 ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
14633 ex9_insn = ex9_insn->next;
14636 ex9_insn_head = NULL;
14638 if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
14640 /* Examine ifc reduce size. */
14641 struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
14642 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
14647 if (ifc_ent->enable == 0)
14650 irel_ptr = ifc_ent->irel_head;
14654 irel_ptr = irel_ptr->next;
14658 ifc_ent = ifc_ent->next;
14660 ex9_relax_size += size;
14664 /* Finish ex9 table. */
14667 nds32_elf_ex9_finish (struct bfd_link_info *link_info)
14669 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14670 nds32_elf_order_insn_times (link_info);
14671 nds32_elf_ex9_total_relax (link_info);
14672 /* Traverse the hash table and count its times. */
14673 nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
14674 nds32_elf_order_insn_times (link_info);
14675 nds32_elf_ex9_build_itable (link_info);
14678 /* Relocate the entries in ex9 table. */
14681 nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
14682 struct bfd_link_info *link_info)
14684 Elf_Internal_Sym *isym = NULL;
14685 bfd_vma relocation = -1;
14686 struct elf_link_hash_entry *h;
14688 if (ptr->m_list != NULL)
14690 /* Global symbol. */
14691 h = ptr->m_list->h_list->h;
14692 if ((h->root.type == bfd_link_hash_defined
14693 || h->root.type == bfd_link_hash_defweak)
14694 && h->root.u.def.section != NULL
14695 && h->root.u.def.section->output_section != NULL)
14698 relocation = h->root.u.def.value +
14699 h->root.u.def.section->output_section->vma +
14700 h->root.u.def.section->output_offset;
14701 relocation += ptr->m_list->irel->r_addend;
14706 else if (ptr->sec !=NULL)
14708 /* Local symbol. */
14709 Elf_Internal_Sym sym;
14710 asection *sec = NULL;
14712 asection *isec_ptr = &isec;
14713 Elf_Internal_Rela irel_backup = *(ptr->irel);
14714 asection *sec_backup = ptr->sec;
14715 bfd *abfd = ptr->sec->owner;
14717 if (!nds32_get_local_syms (abfd, sec, &isym))
14719 isym = isym + ELF32_R_SYM (ptr->irel->r_info);
14721 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14726 /* The purpose is same as elf_link_input_bfd. */
14727 if (isec_ptr != NULL
14728 && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
14729 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
14732 _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
14733 elf_section_data (isec_ptr)->sec_info,
14736 relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
14737 &ptr->sec, ptr->irel);
14738 if (ptr->irel != NULL)
14739 relocation += ptr->irel->r_addend;
14741 /* Restore origin value since there may be some insntructions that
14742 could not be replaced with ex9.it. */
14743 *(ptr->irel) = irel_backup;
14744 ptr->sec = sec_backup;
14750 /* Import ex9 table and build list. */
14753 nds32_elf_ex9_import_table (struct bfd_link_info *info)
14756 bfd_byte *contents;
14757 FILE *ex9_import_file;
14758 int update_ex9_table;
14759 struct elf_nds32_link_hash_table *table;
14761 table = nds32_elf_hash_table (info);
14762 ex9_import_file = table->ex9_import_file;
14763 rewind (table->ex9_import_file);
14765 contents = bfd_malloc (sizeof (bfd_byte) * 4);
14767 /* Read instructions from the input file and build the list. */
14768 while (!feof (ex9_import_file))
14770 unsigned long insn;
14772 struct elf_nds32_insn_times_entry *ptr;
14775 nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
14776 /* Ignore the final byte 0x0a. */
14779 insn = bfd_getb32 (contents);
14780 code = bfd_malloc (sizeof (char) * 9);
14781 snprintf (code, 9, "%08lx", (insn & 0xffffffff));
14782 ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14783 ptr->string = code;
14787 ptr->m_list = NULL;
14788 ptr->rel_backup.r_offset = 0;
14789 ptr->rel_backup.r_info = 0;
14790 ptr->rel_backup.r_addend = 0;
14793 nds32_elf_ex9_insert_entry (ptr);
14797 update_ex9_table = table->update_ex9_table;
14798 if (update_ex9_table == 1)
14800 /* It has to consider of sethi need to use multiple page
14801 but it not be done yet. */
14802 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14803 nds32_elf_order_insn_times (info);
14807 /* Export ex9 table. */
14810 nds32_elf_ex9_export (struct bfd_link_info *info,
14811 bfd_byte *contents, int size)
14813 FILE *ex9_export_file;
14814 struct elf_nds32_link_hash_table *table;
14816 table = nds32_elf_hash_table (info);
14817 ex9_export_file = table->ex9_export_file;
14818 fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
14819 fclose (ex9_export_file);
14822 /* Adjust relocations of J and JAL in ex9.itable.
14823 Export ex9 table. */
14826 nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
14828 asection *table_sec = NULL;
14829 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14830 struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
14832 uint32_t insn, insn_with_reg, source_insn;
14833 bfd_byte *contents = NULL, *source_contents = NULL;
14836 int shift, update_ex9_table, offset = 0;
14837 reloc_howto_type *howto = NULL;
14838 Elf_Internal_Rela rel_backup;
14839 unsigned short insn_ex9;
14840 struct elf_nds32_link_hash_table *table;
14841 FILE *ex9_export_file;
14842 static bfd_boolean done = FALSE;
14849 table = nds32_elf_hash_table (link_info);
14851 table->relax_status |= NDS32_RELAX_EX9_DONE;
14854 update_ex9_table = table->update_ex9_table;
14855 /* Generated ex9.itable exactly. */
14856 if (update_ex9_table == 0)
14858 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
14859 it_abfd = it_abfd->link.next)
14861 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
14862 if (table_sec != NULL)
14866 if (table_sec != NULL)
14870 output_bfd = table_sec->output_section->owner;
14871 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
14872 if (table_sec->size == 0)
14875 if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
14884 output_bfd = link_info->input_bfds->sections->output_section->owner;
14885 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
14886 contents = bfd_malloc (sizeof (bfd_byte) * 2048);
14889 /* Relocate instruction. */
14892 bfd_vma relocation, min_relocation = 0xffffffff;
14894 insn = strtol (ex9_insn->string, NULL, 16);
14896 if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
14898 if (ex9_insn->m_list)
14899 rel_backup = ex9_insn->m_list->rel_backup;
14901 rel_backup = ex9_insn->rel_backup;
14903 nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
14905 bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
14906 (rel_backup.r_info));
14907 shift = howto->rightshift;
14908 if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
14909 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
14910 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
14911 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
14912 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
14914 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
14916 insn_with_reg | ((relocation >> shift) &
14917 nds32_elf_irel_mask (&rel_backup));
14918 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14920 else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
14921 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
14922 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
14923 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
14924 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
14925 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
14926 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
14927 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
14929 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
14931 insn_with_reg | (((relocation - gp) >> shift) &
14932 nds32_elf_irel_mask (&rel_backup));
14933 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14935 else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
14937 /* Sethi may be multiple entry for one insn. */
14938 if (ex9_insn->next && ex9_insn->m_list
14939 && ex9_insn->m_list == ex9_insn->next->m_list)
14941 struct elf_link_hash_entry_mul_list *m_list;
14942 struct elf_nds32_ex9_refix *fix_ptr;
14943 struct elf_link_hash_entry *h;
14945 temp_ptr = ex9_insn;
14946 temp_ptr2 = ex9_insn;
14947 m_list = ex9_insn->m_list;
14950 h = m_list->h_list->h;
14951 relocation = h->root.u.def.value +
14952 h->root.u.def.section->output_section->vma +
14953 h->root.u.def.section->output_offset;
14954 relocation += m_list->irel->r_addend;
14956 if (relocation < min_relocation)
14957 min_relocation = relocation;
14958 m_list = m_list->next;
14960 relocation = min_relocation;
14962 /* Put insntruction into ex9 table. */
14963 insn = insn_with_reg
14964 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
14965 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14966 relocation = relocation + 0x1000; /* hi20 */
14968 while (ex9_insn->next && ex9_insn->m_list
14969 && ex9_insn->m_list == ex9_insn->next->m_list)
14971 /* Multiple sethi. */
14972 ex9_insn = ex9_insn->next;
14975 insn_with_reg | ((relocation >> shift) &
14976 nds32_elf_irel_mask (&rel_backup));
14977 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14978 relocation = relocation + 0x1000; /* hi20 */
14981 fix_ptr = ex9_refix_head;
14984 /* Fix ex9 insn. */
14985 /* temp_ptr2 points to the head of multiple sethi. */
14986 temp_ptr = temp_ptr2;
14987 while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
14989 fix_ptr = fix_ptr->next;
14991 if (fix_ptr->order != temp_ptr->order)
14994 /* Set source insn. */
14996 fix_ptr->h->root.u.def.value +
14997 fix_ptr->h->root.u.def.section->output_section->vma +
14998 fix_ptr->h->root.u.def.section->output_offset;
14999 relocation += fix_ptr->irel->r_addend;
15000 /* sethi imm is imm20s. */
15001 source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);
15005 /* Match entry and source code. */
15006 insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
15007 if (insn == source_insn)
15009 /* Fix the ex9 insn. */
15010 if (temp_ptr->order != fix_ptr->order)
15012 if (!nds32_get_section_contents
15013 (fix_ptr->sec->owner, fix_ptr->sec,
15014 &source_contents, TRUE))
15016 (_("Linker: error cannot fixed ex9 relocation \n"));
15017 if (temp_ptr->order < 32)
15018 insn_ex9 = INSN_EX9_IT_2;
15020 insn_ex9 = INSN_EX9_IT_1;
15021 insn_ex9 = insn_ex9 | temp_ptr->order;
15022 bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
15028 if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
15030 (_("Linker: error cannot fixed ex9 relocation \n"));
15032 temp_ptr = temp_ptr->next;
15035 fix_ptr = fix_ptr->next;
15040 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15041 insn = insn_with_reg
15042 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15043 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15049 /* Insn without relocation does not have to be fixed
15050 if need to update export table. */
15051 if (update_ex9_table == 1)
15052 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15054 ex9_insn = ex9_insn->next;
15058 ex9_export_file = table->ex9_export_file;
15059 if (ex9_export_file != NULL)
15060 nds32_elf_ex9_export (link_info, contents, table_sec->size);
15061 else if (update_ex9_table == 1)
15063 table->ex9_export_file = table->ex9_import_file;
15064 rewind (table->ex9_export_file);
15065 nds32_elf_ex9_export (link_info, contents, size);
15069 /* Generate ex9 hash table. */
15072 nds32_elf_ex9_build_hash_table (bfd *abfd, asection *sec,
15073 struct bfd_link_info *link_info)
15075 Elf_Internal_Rela *internal_relocs;
15076 Elf_Internal_Rela *irelend;
15077 Elf_Internal_Rela *irel;
15078 Elf_Internal_Rela *jrel;
15079 Elf_Internal_Rela rel_backup;
15080 Elf_Internal_Shdr *symtab_hdr;
15081 Elf_Internal_Sym *isym = NULL;
15083 struct elf_link_hash_entry **sym_hashes;
15084 bfd_byte *contents = NULL;
15086 unsigned long r_symndx;
15087 uint32_t insn, insn_with_reg;
15088 struct elf_link_hash_entry *h;
15089 int data_flag, shift, align;
15090 bfd_vma relocation;
15091 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
15092 reloc_howto_type *howto = NULL;
15094 sym_hashes = elf_sym_hashes (abfd);
15095 /* Load section instructions, relocations, and symbol table. */
15096 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
15099 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
15100 TRUE /* keep_memory */);
15101 irelend = internal_relocs + sec->reloc_count;
15102 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
15103 if (!nds32_get_local_syms (abfd, sec, &isym))
15106 /* Check the object if enable ex9. */
15107 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
15108 R_NDS32_RELAX_ENTRY);
15110 /* Check this section trigger ex9 relaxation. */
15113 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
15114 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
15115 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
15118 irel = internal_relocs;
15120 /* Push each insn into hash table. */
15121 while (off < sec->size)
15124 struct elf_nds32_code_hash_entry *entry;
15126 while (irel != NULL && irel < irelend && irel->r_offset < off)
15129 data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend,
15130 NULL, sec, &off, contents);
15131 if (data_flag & DATA_EXIST)
15133 /* We save the move offset in the highest byte. */
15134 off += (data_flag >> 24);
15138 if (*(contents + off) & 0x80)
15147 rel_backup.r_info = 0;
15148 rel_backup.r_offset = 0;
15149 rel_backup.r_addend = 0;
15150 /* Load the instruction and its opcode with register for comparing. */
15151 insn = bfd_getb32 (contents + off);
15153 if (irel != NULL && irel < irelend && irel->r_offset == off)
15155 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
15156 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
15157 shift = howto->rightshift;
15158 align = (1 << shift) - 1;
15159 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
15160 || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
15161 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
15162 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
15163 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
15164 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
15165 ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15166 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15167 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15168 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15169 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15170 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15171 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15172 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15174 r_symndx = ELF32_R_SYM (irel->r_info);
15176 rel_backup = *irel;
15177 if (r_symndx < symtab_hdr->sh_info)
15179 /* Local symbol. */
15180 int shndx = isym[r_symndx].st_shndx;
15182 bfd_vma st_value = (isym + r_symndx)->st_value;
15183 isec = elf_elfsections (abfd)[shndx]->bfd_section;
15184 relocation = (isec->output_section->vma + isec->output_offset
15185 + st_value + irel->r_addend);
15189 /* External symbol. */
15190 bfd_boolean warned ATTRIBUTE_UNUSED;
15191 bfd_boolean ignored ATTRIBUTE_UNUSED;
15192 bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
15195 /* Maybe there is a better way to get h and relocation */
15196 RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
15197 r_symndx, symtab_hdr, sym_hashes,
15198 h, sym_sec, relocation,
15199 unresolved_reloc, warned, ignored);
15200 relocation += irel->r_addend;
15201 if ((h->root.type != bfd_link_hash_defined
15202 && h->root.type != bfd_link_hash_defweak)
15203 || strcmp (h->root.root.string, "_FP_BASE_") == 0)
15210 /* Check for gp relative instruction alignment. */
15211 if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15212 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15213 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15214 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15215 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15216 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15217 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15218 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15221 bfd *output_bfd = sec->output_section->owner;
15222 bfd_reloc_status_type r;
15224 /* If the symbol is in the abs section, the out_bfd will be null.
15225 This happens when the relocation has a symbol@GOTOFF. */
15226 r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15227 if (r != bfd_reloc_ok)
15235 /* Make sure alignment is correct. */
15236 if (relocation & align)
15238 /* Incorrect alignment. */
15240 /* xgettext:c-format */
15241 (_("%pB: warning: unaligned small data access "
15242 "for entry: {%" PRId64 ", %" PRId64 ", %" PRId64
15243 "}, addr = %#" PRIx64 ", align = %#x"),
15244 abfd, (int64_t) irel->r_offset,
15245 (int64_t) irel->r_info, (int64_t) irel->r_addend,
15246 (uint64_t) relocation, align);
15252 insn = insn_with_reg
15253 | ((relocation >> shift) & nds32_elf_irel_mask (irel));
15255 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
15256 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
15257 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
15259 /* These relocations do not have to relocate contens, so it can
15260 be regard as instruction without relocation. */
15269 snprintf (code, sizeof (code), "%08x", insn);
15271 entry = (struct elf_nds32_code_hash_entry*)
15272 bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
15276 (_("failed creating ex9.it %s hash table entry"), code);
15281 if (h->root.type == bfd_link_hash_undefined)
15283 /* Global symbol. */
15284 /* In order to do sethi with different symbol but same value. */
15285 if (entry->m_list == NULL)
15287 struct elf_link_hash_entry_mul_list *m_list_new;
15288 struct elf_link_hash_entry_list *h_list_new;
15290 m_list_new = (struct elf_link_hash_entry_mul_list *)
15291 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15292 h_list_new = (struct elf_link_hash_entry_list *)
15293 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15294 entry->m_list = m_list_new;
15295 m_list_new->h_list = h_list_new;
15296 m_list_new->rel_backup = rel_backup;
15297 m_list_new->times = 1;
15298 m_list_new->irel = jrel;
15299 m_list_new->next = NULL;
15301 h_list_new->next = NULL;
15305 struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
15306 struct elf_link_hash_entry_list *h_list;
15310 /* Build the different symbols that point to the same address. */
15311 h_list = m_list->h_list;
15312 if (h_list->h->root.u.def.value == h->root.u.def.value
15313 && h_list->h->root.u.def.section->output_section->vma
15314 == h->root.u.def.section->output_section->vma
15315 && h_list->h->root.u.def.section->output_offset
15316 == h->root.u.def.section->output_offset
15317 && m_list->rel_backup.r_addend == rel_backup.r_addend)
15320 m_list->irel = jrel;
15321 while (h_list->h != h && h_list->next)
15322 h_list = h_list->next;
15323 if (h_list->h != h)
15325 struct elf_link_hash_entry_list *h_list_new;
15327 h_list_new = (struct elf_link_hash_entry_list *)
15328 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15329 h_list->next = h_list_new;
15331 h_list_new->next = NULL;
15335 /* The sethi case may have different address but the
15336 hi20 is the same. */
15337 else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
15338 && m_list->next == NULL)
15340 struct elf_link_hash_entry_mul_list *m_list_new;
15341 struct elf_link_hash_entry_list *h_list_new;
15343 m_list_new = (struct elf_link_hash_entry_mul_list *)
15344 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15345 h_list_new = (struct elf_link_hash_entry_list *)
15346 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15347 m_list->next = m_list_new;
15348 m_list_new->h_list = h_list_new;
15349 m_list_new->rel_backup = rel_backup;
15350 m_list_new->times = 1;
15351 m_list_new->irel = jrel;
15352 m_list_new->next = NULL;
15354 h_list_new->next = NULL;
15357 m_list = m_list->next;
15368 /* Local symbol and insn without relocation*/
15370 entry->rel_backup = rel_backup;
15373 /* Use in sethi insn with constant and global symbol in same format. */
15375 entry->const_insn = 1;
15377 entry->irel = jrel;
15385 /* Set the _ITB_BASE, and point it to ex9 table. */
15388 nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
15392 bfd *output_bfd = NULL;
15393 struct bfd_link_hash_entry *bh = NULL;
15395 if (is_ITB_BASE_set == 1)
15398 is_ITB_BASE_set = 1;
15400 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_", FALSE, FALSE, TRUE);
15402 if (bh && (bh->type == bfd_link_hash_defined
15403 || bh->type == bfd_link_hash_defweak))
15406 for (abfd = link_info->input_bfds; abfd != NULL;
15407 abfd = abfd->link.next)
15409 sec = bfd_get_section_by_name (abfd, ".ex9.itable");
15412 output_bfd = sec->output_section->owner;
15416 if (output_bfd == NULL)
15418 output_bfd = link_info->output_bfd;
15419 if (output_bfd->sections == NULL)
15422 sec = bfd_abs_section_ptr;
15424 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
15425 FALSE, FALSE, TRUE);
15426 return (_bfd_generic_link_add_one_symbol
15427 (link_info, output_bfd, "_ITB_BASE_",
15428 BSF_GLOBAL | BSF_WEAK, sec, 0,
15429 (const char *) NULL, FALSE, get_elf_backend_data
15430 (output_bfd)->collect, &bh));
15434 #define ELF_ARCH bfd_arch_nds32
15435 #define ELF_MACHINE_CODE EM_NDS32
15436 #define ELF_MAXPAGESIZE 0x1000
15437 #define ELF_TARGET_ID NDS32_ELF_DATA
15439 #define TARGET_BIG_SYM nds32_elf32_be_vec
15440 #define TARGET_BIG_NAME "elf32-nds32be"
15441 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
15442 #define TARGET_LITTLE_NAME "elf32-nds32le"
15444 #define elf_info_to_howto nds32_info_to_howto
15445 #define elf_info_to_howto_rel nds32_info_to_howto_rel
15447 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
15448 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
15449 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
15450 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
15451 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
15453 #define bfd_elf32_mkobject nds32_elf_mkobject
15454 #define elf_backend_action_discarded nds32_elf_action_discarded
15455 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
15456 #define elf_backend_check_relocs nds32_elf_check_relocs
15457 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
15458 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
15459 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
15460 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
15461 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
15462 #define elf_backend_relocate_section nds32_elf_relocate_section
15463 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
15464 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
15465 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
15466 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
15467 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
15468 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
15469 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
15470 #define elf_backend_object_p nds32_elf_object_p
15471 #define elf_backend_final_write_processing nds32_elf_final_write_processing
15472 #define elf_backend_special_sections nds32_elf_special_sections
15473 #define bfd_elf32_bfd_get_relocated_section_contents \
15474 nds32_elf_get_relocated_section_contents
15476 #define elf_backend_can_gc_sections 1
15477 #define elf_backend_can_refcount 1
15478 #define elf_backend_want_got_plt 1
15479 #define elf_backend_plt_readonly 1
15480 #define elf_backend_want_plt_sym 0
15481 #define elf_backend_got_header_size 12
15482 #define elf_backend_may_use_rel_p 1
15483 #define elf_backend_default_use_rela_p 1
15484 #define elf_backend_may_use_rela_p 1
15485 #define elf_backend_dtrel_excludes_plt 1
15487 #include "elf32-target.h"
15489 #undef ELF_MAXPAGESIZE
15490 #define ELF_MAXPAGESIZE 0x2000
15492 #undef TARGET_BIG_SYM
15493 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
15494 #undef TARGET_BIG_NAME
15495 #define TARGET_BIG_NAME "elf32-nds32be-linux"
15496 #undef TARGET_LITTLE_SYM
15497 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
15498 #undef TARGET_LITTLE_NAME
15499 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
15501 #define elf32_bed elf32_nds32_lin_bed
15503 #include "elf32-target.h"