1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2016 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "bfd_stdint.h"
29 #include "libiberty.h"
30 #include "bfd_stdint.h"
31 #include "elf/nds32.h"
32 #include "opcode/nds32.h"
33 #include "elf32-nds32.h"
34 #include "opcode/cgen.h"
35 #include "../opcodes/nds32-opc.h"
37 /* Relocation HOWTO functions. */
38 static bfd_reloc_status_type nds32_elf_ignore_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type nds32_elf_hi20_reloc
43 (bfd *, arelent *, asymbol *, void *,
44 asection *, bfd *, char **);
45 static bfd_reloc_status_type nds32_elf_lo12_reloc
46 (bfd *, arelent *, asymbol *, void *,
47 asection *, bfd *, char **);
48 static bfd_reloc_status_type nds32_elf_generic_reloc
49 (bfd *, arelent *, asymbol *, void *,
50 asection *, bfd *, char **);
51 static bfd_reloc_status_type nds32_elf_sda15_reloc
52 (bfd *, arelent *, asymbol *, void *,
53 asection *, bfd *, char **);
55 /* Helper functions for HOWTO. */
56 static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
57 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
58 asection *, bfd_vma, bfd_vma);
59 static void nds32_elf_relocate_hi20
60 (bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *, bfd_vma);
61 static reloc_howto_type *bfd_elf32_bfd_reloc_type_table_lookup
62 (enum elf_nds32_reloc_type);
63 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
64 (bfd *, bfd_reloc_code_real_type);
67 static void nds32_info_to_howto_rel
68 (bfd *, arelent *, Elf_Internal_Rela *dst);
69 static void nds32_info_to_howto
70 (bfd *, arelent *, Elf_Internal_Rela *dst);
71 static bfd_boolean nds32_elf_add_symbol_hook
72 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, const char **,
73 flagword *, asection **, bfd_vma *);
74 static bfd_boolean nds32_elf_relocate_section
75 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
76 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
77 static bfd_boolean nds32_elf_object_p (bfd *);
78 static void nds32_elf_final_write_processing (bfd *, bfd_boolean);
79 static bfd_boolean nds32_elf_set_private_flags (bfd *, flagword);
80 static bfd_boolean nds32_elf_merge_private_bfd_data
81 (bfd *, struct bfd_link_info *);
82 static bfd_boolean nds32_elf_print_private_bfd_data (bfd *, void *);
83 static bfd_boolean nds32_elf_gc_sweep_hook
84 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
85 static bfd_boolean nds32_elf_check_relocs
86 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
87 static asection *nds32_elf_gc_mark_hook
88 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
89 struct elf_link_hash_entry *, Elf_Internal_Sym *);
90 static bfd_boolean nds32_elf_adjust_dynamic_symbol
91 (struct bfd_link_info *, struct elf_link_hash_entry *);
92 static bfd_boolean nds32_elf_size_dynamic_sections
93 (bfd *, struct bfd_link_info *);
94 static bfd_boolean nds32_elf_create_dynamic_sections
95 (bfd *, struct bfd_link_info *);
96 static bfd_boolean nds32_elf_finish_dynamic_sections
97 (bfd *, struct bfd_link_info *info);
98 static bfd_boolean nds32_elf_finish_dynamic_symbol
99 (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
101 static bfd_boolean nds32_elf_mkobject (bfd *);
103 /* Nds32 helper functions. */
104 static bfd_reloc_status_type nds32_elf_final_sda_base
105 (bfd *, struct bfd_link_info *, bfd_vma *, bfd_boolean);
106 static bfd_boolean allocate_dynrelocs (struct elf_link_hash_entry *, void *);
107 static bfd_boolean readonly_dynrelocs (struct elf_link_hash_entry *, void *);
108 static Elf_Internal_Rela *find_relocs_at_address
109 (Elf_Internal_Rela *, Elf_Internal_Rela *,
110 Elf_Internal_Rela *, enum elf_nds32_reloc_type);
111 static bfd_vma calculate_memory_address
112 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
113 static int nds32_get_section_contents (bfd *, asection *,
114 bfd_byte **, bfd_boolean);
115 static bfd_boolean nds32_elf_ex9_build_hash_table
116 (bfd *, asection *, struct bfd_link_info *);
117 static bfd_boolean nds32_elf_ex9_itb_base (struct bfd_link_info *);
118 static void nds32_elf_ex9_import_table (struct bfd_link_info *);
119 static void nds32_elf_ex9_finish (struct bfd_link_info *);
120 static void nds32_elf_ex9_reloc_jmp (struct bfd_link_info *);
121 static void nds32_elf_get_insn_with_reg
122 (Elf_Internal_Rela *, uint32_t, uint32_t *);
123 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
124 Elf_Internal_Sym **);
125 static bfd_boolean nds32_elf_ex9_replace_instruction
126 (struct bfd_link_info *, bfd *, asection *);
127 static bfd_boolean nds32_elf_ifc_calc (struct bfd_link_info *, bfd *,
129 static bfd_boolean nds32_elf_ifc_finish (struct bfd_link_info *);
130 static bfd_boolean nds32_elf_ifc_replace (struct bfd_link_info *);
131 static bfd_boolean nds32_elf_ifc_reloc (void);
132 static bfd_boolean nds32_relax_fp_as_gp
133 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
134 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
135 Elf_Internal_Sym *isymbuf);
136 static bfd_boolean nds32_fag_remove_unused_fpbase
137 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
138 Elf_Internal_Rela *irelend);
140 nds32_elf_get_relocated_section_contents (bfd *abfd,
141 struct bfd_link_info *link_info,
142 struct bfd_link_order *link_order,
144 bfd_boolean relocatable,
149 MACH_V1 = bfd_mach_n1h,
150 MACH_V2 = bfd_mach_n1h_v2,
151 MACH_V3 = bfd_mach_n1h_v3,
152 MACH_V3M = bfd_mach_n1h_v3m
155 #define MIN(a, b) ((a) > (b) ? (b) : (a))
156 #define MAX(a, b) ((a) > (b) ? (a) : (b))
158 /* The name of the dynamic interpreter. This is put in the .interp
160 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
162 /* The nop opcode we use. */
163 #define NDS32_NOP32 0x40000009
164 #define NDS32_NOP16 0x9200
166 /* The size in bytes of an entry in the procedure linkage table. */
167 #define PLT_ENTRY_SIZE 24
168 #define PLT_HEADER_SIZE 24
170 /* The first entry in a procedure linkage table are reserved,
171 and the initial contents are unimportant (we zero them out).
172 Subsequent entries look like this. */
173 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
174 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
175 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
176 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
177 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
179 /* $ta is change to $r15 (from $r25). */
180 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
181 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
182 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
183 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
184 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
185 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
187 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
188 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
189 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
190 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
191 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
193 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
194 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
195 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
196 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
197 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
198 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
200 /* These are macros used to get the relocation accurate value. */
201 #define ACCURATE_8BIT_S1 (0x100)
202 #define ACCURATE_U9BIT_S1 (0x400)
203 #define ACCURATE_12BIT_S1 (0x2000)
204 #define ACCURATE_14BIT_S1 (0x4000)
205 #define ACCURATE_19BIT (0x40000)
207 /* These are macros used to get the relocation conservative value. */
208 #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
209 #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
210 #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
211 #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
212 /* These must be more conservative because the address may be in
213 different segment. */
214 #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
215 #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
216 #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
217 #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
218 #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
220 /* Size of small data/bss sections, used to calculate SDA_BASE. */
221 static long got_size = 0;
222 static int is_SDA_BASE_set = 0;
223 static int is_ITB_BASE_set = 0;
225 /* Convert ELF-VER in eflags to string for debugging purpose. */
226 static const char *const nds32_elfver_strtab[] =
233 /* The nds32 linker needs to keep track of the number of relocs that it
234 decides to copy in check_relocs for each symbol. This is so that
235 it can discard PC relative relocs if it doesn't need them when
236 linking with -Bsymbolic. We store the information in a field
237 extending the regular ELF linker hash table. */
239 /* This structure keeps track of the number of PC relative relocs we
240 have copied for a given symbol. */
242 struct elf_nds32_pcrel_relocs_copied
245 struct elf_nds32_pcrel_relocs_copied *next;
246 /* A section in dynobj. */
248 /* Number of relocs copied in this section. */
252 /* The sh linker needs to keep track of the number of relocs that it
253 decides to copy as dynamic relocs in check_relocs for each symbol.
254 This is so that it can later discard them if they are found to be
255 unnecessary. We store the information in a field extending the
256 regular ELF linker hash table. */
258 struct elf_nds32_dyn_relocs
260 struct elf_nds32_dyn_relocs *next;
262 /* The input section of the reloc. */
265 /* Total number of relocs copied for the input section. */
268 /* Number of pc-relative relocs copied for the input section. */
269 bfd_size_type pc_count;
272 /* Nds32 ELF linker hash entry. */
274 struct elf_nds32_link_hash_entry
276 struct elf_link_hash_entry root;
278 /* Track dynamic relocs copied for this symbol. */
279 struct elf_nds32_dyn_relocs *dyn_relocs;
281 /* For checking relocation type. */
282 #define GOT_UNKNOWN 0
285 unsigned int tls_type;
288 /* Get the nds32 ELF linker hash table from a link_info structure. */
290 #define FP_BASE_NAME "_FP_BASE_"
291 static int check_start_export_sym = 0;
292 static size_t ex9_relax_size = 0; /* Save ex9 predicted reducing size. */
294 /* The offset for executable tls relaxation. */
295 #define TP_OFFSET 0x0
297 struct elf_nds32_obj_tdata
299 struct elf_obj_tdata root;
301 /* tls_type for each local got entry. */
302 char *local_got_tls_type;
305 #define elf_nds32_tdata(bfd) \
306 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
308 #define elf32_nds32_local_got_tls_type(bfd) \
309 (elf_nds32_tdata (bfd)->local_got_tls_type)
311 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
314 nds32_elf_mkobject (bfd *abfd)
316 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
320 /* Relocations used for relocation. */
321 static reloc_howto_type nds32_elf_howto_table[] =
323 /* This reloc does nothing. */
324 HOWTO (R_NDS32_NONE, /* type */
326 3, /* size (0 = byte, 1 = short, 2 = long) */
328 FALSE, /* pc_relative */
330 complain_overflow_dont, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_NDS32_NONE", /* name */
333 FALSE, /* partial_inplace */
336 FALSE), /* pcrel_offset */
338 /* A 16 bit absolute relocation. */
339 HOWTO (R_NDS32_16, /* type */
341 1, /* size (0 = byte, 1 = short, 2 = long) */
343 FALSE, /* pc_relative */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 nds32_elf_generic_reloc, /* special_function */
347 "R_NDS32_16", /* name */
348 FALSE, /* partial_inplace */
349 0xffff, /* src_mask */
350 0xffff, /* dst_mask */
351 FALSE), /* pcrel_offset */
353 /* A 32 bit absolute relocation. */
354 HOWTO (R_NDS32_32, /* type */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
358 FALSE, /* pc_relative */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 nds32_elf_generic_reloc, /* special_function */
362 "R_NDS32_32", /* name */
363 FALSE, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
366 FALSE), /* pcrel_offset */
368 /* A 20 bit address. */
369 HOWTO (R_NDS32_20, /* type */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
373 FALSE, /* pc_relative */
375 complain_overflow_unsigned, /* complain_on_overflow */
376 nds32_elf_generic_reloc, /* special_function */
377 "R_NDS32_20", /* name */
378 FALSE, /* partial_inplace */
379 0xfffff, /* src_mask */
380 0xfffff, /* dst_mask */
381 FALSE), /* pcrel_offset */
383 /* An PC Relative 9-bit relocation, shifted by 2.
384 This reloc is complicated because relocations are relative to pc & -4.
385 i.e. branches in the right insn slot use the address of the left insn
387 /* ??? It's not clear whether this should have partial_inplace set or not.
388 Branch relaxing in the assembler can store the addend in the insn,
389 and if bfd_install_relocation gets called the addend may get added
391 HOWTO (R_NDS32_9_PCREL, /* type */
393 1, /* size (0 = byte, 1 = short, 2 = long) */
395 TRUE, /* pc_relative */
397 complain_overflow_signed, /* complain_on_overflow */
398 nds32_elf_9_pcrel_reloc, /* special_function */
399 "R_NDS32_9_PCREL", /* name */
400 FALSE, /* partial_inplace */
403 TRUE), /* pcrel_offset */
405 /* A relative 15 bit relocation, right shifted by 1. */
406 HOWTO (R_NDS32_15_PCREL, /* type */
408 2, /* size (0 = byte, 1 = short, 2 = long) */
410 TRUE, /* pc_relative */
412 complain_overflow_signed, /* complain_on_overflow */
413 bfd_elf_generic_reloc, /* special_function */
414 "R_NDS32_15_PCREL", /* name */
415 FALSE, /* partial_inplace */
416 0x3fff, /* src_mask */
417 0x3fff, /* dst_mask */
418 TRUE), /* pcrel_offset */
420 /* A relative 17 bit relocation, right shifted by 1. */
421 HOWTO (R_NDS32_17_PCREL, /* type */
423 2, /* size (0 = byte, 1 = short, 2 = long) */
425 TRUE, /* pc_relative */
427 complain_overflow_signed, /* complain_on_overflow */
428 bfd_elf_generic_reloc, /* special_function */
429 "R_NDS32_17_PCREL", /* name */
430 FALSE, /* partial_inplace */
431 0xffff, /* src_mask */
432 0xffff, /* dst_mask */
433 TRUE), /* pcrel_offset */
435 /* A relative 25 bit relocation, right shifted by 1. */
436 /* ??? It's not clear whether this should have partial_inplace set or not.
437 Branch relaxing in the assembler can store the addend in the insn,
438 and if bfd_install_relocation gets called the addend may get added
440 HOWTO (R_NDS32_25_PCREL, /* type */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
444 TRUE, /* pc_relative */
446 complain_overflow_signed, /* complain_on_overflow */
447 bfd_elf_generic_reloc, /* special_function */
448 "R_NDS32_25_PCREL", /* name */
449 FALSE, /* partial_inplace */
450 0xffffff, /* src_mask */
451 0xffffff, /* dst_mask */
452 TRUE), /* pcrel_offset */
454 /* High 20 bits of address when lower 12 is or'd in. */
455 HOWTO (R_NDS32_HI20, /* type */
457 2, /* size (0 = byte, 1 = short, 2 = long) */
459 FALSE, /* pc_relative */
461 complain_overflow_dont,/* complain_on_overflow */
462 nds32_elf_hi20_reloc, /* special_function */
463 "R_NDS32_HI20", /* name */
464 FALSE, /* partial_inplace */
465 0x000fffff, /* src_mask */
466 0x000fffff, /* dst_mask */
467 FALSE), /* pcrel_offset */
469 /* Lower 12 bits of address. */
470 HOWTO (R_NDS32_LO12S3, /* type */
472 2, /* size (0 = byte, 1 = short, 2 = long) */
474 FALSE, /* pc_relative */
476 complain_overflow_dont,/* complain_on_overflow */
477 nds32_elf_lo12_reloc, /* special_function */
478 "R_NDS32_LO12S3", /* name */
479 FALSE, /* partial_inplace */
480 0x000001ff, /* src_mask */
481 0x000001ff, /* dst_mask */
482 FALSE), /* pcrel_offset */
484 /* Lower 12 bits of address. */
485 HOWTO (R_NDS32_LO12S2, /* type */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
489 FALSE, /* pc_relative */
491 complain_overflow_dont,/* complain_on_overflow */
492 nds32_elf_lo12_reloc, /* special_function */
493 "R_NDS32_LO12S2", /* name */
494 FALSE, /* partial_inplace */
495 0x000003ff, /* src_mask */
496 0x000003ff, /* dst_mask */
497 FALSE), /* pcrel_offset */
499 /* Lower 12 bits of address. */
500 HOWTO (R_NDS32_LO12S1, /* type */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
504 FALSE, /* pc_relative */
506 complain_overflow_dont,/* complain_on_overflow */
507 nds32_elf_lo12_reloc, /* special_function */
508 "R_NDS32_LO12S1", /* name */
509 FALSE, /* partial_inplace */
510 0x000007ff, /* src_mask */
511 0x000007ff, /* dst_mask */
512 FALSE), /* pcrel_offset */
514 /* Lower 12 bits of address. */
515 HOWTO (R_NDS32_LO12S0, /* type */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
519 FALSE, /* pc_relative */
521 complain_overflow_dont,/* complain_on_overflow */
522 nds32_elf_lo12_reloc, /* special_function */
523 "R_NDS32_LO12S0", /* name */
524 FALSE, /* partial_inplace */
525 0x00000fff, /* src_mask */
526 0x00000fff, /* dst_mask */
527 FALSE), /* pcrel_offset */
529 /* Small data area 15 bits offset. */
530 HOWTO (R_NDS32_SDA15S3, /* type */
532 2, /* size (0 = byte, 1 = short, 2 = long) */
534 FALSE, /* pc_relative */
536 complain_overflow_signed, /* complain_on_overflow */
537 nds32_elf_sda15_reloc, /* special_function */
538 "R_NDS32_SDA15S3", /* name */
539 FALSE, /* partial_inplace */
540 0x00007fff, /* src_mask */
541 0x00007fff, /* dst_mask */
542 FALSE), /* pcrel_offset */
544 /* Small data area 15 bits offset. */
545 HOWTO (R_NDS32_SDA15S2, /* type */
547 2, /* size (0 = byte, 1 = short, 2 = long) */
549 FALSE, /* pc_relative */
551 complain_overflow_signed, /* complain_on_overflow */
552 nds32_elf_sda15_reloc, /* special_function */
553 "R_NDS32_SDA15S2", /* name */
554 FALSE, /* partial_inplace */
555 0x00007fff, /* src_mask */
556 0x00007fff, /* dst_mask */
557 FALSE), /* pcrel_offset */
559 /* Small data area 15 bits offset. */
560 HOWTO (R_NDS32_SDA15S1, /* type */
562 2, /* size (0 = byte, 1 = short, 2 = long) */
564 FALSE, /* pc_relative */
566 complain_overflow_signed, /* complain_on_overflow */
567 nds32_elf_sda15_reloc, /* special_function */
568 "R_NDS32_SDA15S1", /* name */
569 FALSE, /* partial_inplace */
570 0x00007fff, /* src_mask */
571 0x00007fff, /* dst_mask */
572 FALSE), /* pcrel_offset */
574 /* Small data area 15 bits offset. */
575 HOWTO (R_NDS32_SDA15S0, /* type */
577 2, /* size (0 = byte, 1 = short, 2 = long) */
579 FALSE, /* pc_relative */
581 complain_overflow_signed, /* complain_on_overflow */
582 nds32_elf_sda15_reloc, /* special_function */
583 "R_NDS32_SDA15S0", /* name */
584 FALSE, /* partial_inplace */
585 0x00007fff, /* src_mask */
586 0x00007fff, /* dst_mask */
587 FALSE), /* pcrel_offset */
589 /* GNU extension to record C++ vtable hierarchy */
590 HOWTO (R_NDS32_GNU_VTINHERIT, /* type */
592 2, /* size (0 = byte, 1 = short, 2 = long) */
594 FALSE, /* pc_relative */
596 complain_overflow_dont,/* complain_on_overflow */
597 NULL, /* special_function */
598 "R_NDS32_GNU_VTINHERIT", /* name */
599 FALSE, /* partial_inplace */
602 FALSE), /* pcrel_offset */
604 /* GNU extension to record C++ vtable member usage */
605 HOWTO (R_NDS32_GNU_VTENTRY, /* type */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
609 FALSE, /* pc_relative */
611 complain_overflow_dont,/* complain_on_overflow */
612 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
613 "R_NDS32_GNU_VTENTRY", /* name */
614 FALSE, /* partial_inplace */
617 FALSE), /* pcrel_offset */
619 /* A 16 bit absolute relocation. */
620 HOWTO (R_NDS32_16_RELA, /* type */
622 1, /* size (0 = byte, 1 = short, 2 = long) */
624 FALSE, /* pc_relative */
626 complain_overflow_bitfield, /* complain_on_overflow */
627 bfd_elf_generic_reloc, /* special_function */
628 "R_NDS32_16_RELA", /* name */
629 FALSE, /* partial_inplace */
630 0xffff, /* src_mask */
631 0xffff, /* dst_mask */
632 FALSE), /* pcrel_offset */
634 /* A 32 bit absolute relocation. */
635 HOWTO (R_NDS32_32_RELA, /* type */
637 2, /* size (0 = byte, 1 = short, 2 = long) */
639 FALSE, /* pc_relative */
641 complain_overflow_bitfield, /* complain_on_overflow */
642 bfd_elf_generic_reloc, /* special_function */
643 "R_NDS32_32_RELA", /* name */
644 FALSE, /* partial_inplace */
645 0xffffffff, /* src_mask */
646 0xffffffff, /* dst_mask */
647 FALSE), /* pcrel_offset */
649 /* A 20 bit address. */
650 HOWTO (R_NDS32_20_RELA, /* type */
652 2, /* size (0 = byte, 1 = short, 2 = long) */
654 FALSE, /* pc_relative */
656 complain_overflow_signed, /* complain_on_overflow */
657 bfd_elf_generic_reloc, /* special_function */
658 "R_NDS32_20_RELA", /* name */
659 FALSE, /* partial_inplace */
660 0xfffff, /* src_mask */
661 0xfffff, /* dst_mask */
662 FALSE), /* pcrel_offset */
664 HOWTO (R_NDS32_9_PCREL_RELA, /* type */
666 1, /* size (0 = byte, 1 = short, 2 = long) */
668 TRUE, /* pc_relative */
670 complain_overflow_signed, /* complain_on_overflow */
671 bfd_elf_generic_reloc, /* special_function */
672 "R_NDS32_9_PCREL_RELA",/* name */
673 FALSE, /* partial_inplace */
676 TRUE), /* pcrel_offset */
678 /* A relative 15 bit relocation, right shifted by 1. */
679 HOWTO (R_NDS32_15_PCREL_RELA, /* type */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
683 TRUE, /* pc_relative */
685 complain_overflow_signed, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_NDS32_15_PCREL_RELA", /* name */
688 FALSE, /* partial_inplace */
689 0x3fff, /* src_mask */
690 0x3fff, /* dst_mask */
691 TRUE), /* pcrel_offset */
693 /* A relative 17 bit relocation, right shifted by 1. */
694 HOWTO (R_NDS32_17_PCREL_RELA, /* type */
696 2, /* size (0 = byte, 1 = short, 2 = long) */
698 TRUE, /* pc_relative */
700 complain_overflow_signed, /* complain_on_overflow */
701 bfd_elf_generic_reloc, /* special_function */
702 "R_NDS32_17_PCREL_RELA", /* name */
703 FALSE, /* partial_inplace */
704 0xffff, /* src_mask */
705 0xffff, /* dst_mask */
706 TRUE), /* pcrel_offset */
708 /* A relative 25 bit relocation, right shifted by 2. */
709 HOWTO (R_NDS32_25_PCREL_RELA, /* type */
711 2, /* size (0 = byte, 1 = short, 2 = long) */
713 TRUE, /* pc_relative */
715 complain_overflow_signed, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_NDS32_25_PCREL_RELA", /* name */
718 FALSE, /* partial_inplace */
719 0xffffff, /* src_mask */
720 0xffffff, /* dst_mask */
721 TRUE), /* pcrel_offset */
723 /* High 20 bits of address when lower 16 is or'd in. */
724 HOWTO (R_NDS32_HI20_RELA, /* type */
726 2, /* size (0 = byte, 1 = short, 2 = long) */
728 FALSE, /* pc_relative */
730 complain_overflow_dont,/* complain_on_overflow */
731 bfd_elf_generic_reloc, /* special_function */
732 "R_NDS32_HI20_RELA", /* name */
733 FALSE, /* partial_inplace */
734 0x000fffff, /* src_mask */
735 0x000fffff, /* dst_mask */
736 FALSE), /* pcrel_offset */
738 /* Lower 12 bits of address. */
739 HOWTO (R_NDS32_LO12S3_RELA, /* type */
741 2, /* size (0 = byte, 1 = short, 2 = long) */
743 FALSE, /* pc_relative */
745 complain_overflow_dont,/* complain_on_overflow */
746 bfd_elf_generic_reloc, /* special_function */
747 "R_NDS32_LO12S3_RELA", /* name */
748 FALSE, /* partial_inplace */
749 0x000001ff, /* src_mask */
750 0x000001ff, /* dst_mask */
751 FALSE), /* pcrel_offset */
753 /* Lower 12 bits of address. */
754 HOWTO (R_NDS32_LO12S2_RELA, /* type */
756 2, /* size (0 = byte, 1 = short, 2 = long) */
758 FALSE, /* pc_relative */
760 complain_overflow_dont,/* complain_on_overflow */
761 bfd_elf_generic_reloc, /* special_function */
762 "R_NDS32_LO12S2_RELA", /* name */
763 FALSE, /* partial_inplace */
764 0x000003ff, /* src_mask */
765 0x000003ff, /* dst_mask */
766 FALSE), /* pcrel_offset */
768 /* Lower 12 bits of address. */
769 HOWTO (R_NDS32_LO12S1_RELA, /* type */
771 2, /* size (0 = byte, 1 = short, 2 = long) */
773 FALSE, /* pc_relative */
775 complain_overflow_dont,/* complain_on_overflow */
776 bfd_elf_generic_reloc, /* special_function */
777 "R_NDS32_LO12S1_RELA", /* name */
778 FALSE, /* partial_inplace */
779 0x000007ff, /* src_mask */
780 0x000007ff, /* dst_mask */
781 FALSE), /* pcrel_offset */
783 /* Lower 12 bits of address. */
784 HOWTO (R_NDS32_LO12S0_RELA, /* type */
786 2, /* size (0 = byte, 1 = short, 2 = long) */
788 FALSE, /* pc_relative */
790 complain_overflow_dont,/* complain_on_overflow */
791 bfd_elf_generic_reloc, /* special_function */
792 "R_NDS32_LO12S0_RELA", /* name */
793 FALSE, /* partial_inplace */
794 0x00000fff, /* src_mask */
795 0x00000fff, /* dst_mask */
796 FALSE), /* pcrel_offset */
798 /* Small data area 15 bits offset. */
799 HOWTO (R_NDS32_SDA15S3_RELA, /* type */
801 2, /* size (0 = byte, 1 = short, 2 = long) */
803 FALSE, /* pc_relative */
805 complain_overflow_signed, /* complain_on_overflow */
806 bfd_elf_generic_reloc, /* special_function */
807 "R_NDS32_SDA15S3_RELA",/* name */
808 FALSE, /* partial_inplace */
809 0x00007fff, /* src_mask */
810 0x00007fff, /* dst_mask */
811 FALSE), /* pcrel_offset */
813 /* Small data area 15 bits offset. */
814 HOWTO (R_NDS32_SDA15S2_RELA, /* type */
816 2, /* size (0 = byte, 1 = short, 2 = long) */
818 FALSE, /* pc_relative */
820 complain_overflow_signed, /* complain_on_overflow */
821 bfd_elf_generic_reloc, /* special_function */
822 "R_NDS32_SDA15S2_RELA",/* name */
823 FALSE, /* partial_inplace */
824 0x00007fff, /* src_mask */
825 0x00007fff, /* dst_mask */
826 FALSE), /* pcrel_offset */
828 HOWTO (R_NDS32_SDA15S1_RELA, /* type */
830 2, /* size (0 = byte, 1 = short, 2 = long) */
832 FALSE, /* pc_relative */
834 complain_overflow_signed, /* complain_on_overflow */
835 bfd_elf_generic_reloc, /* special_function */
836 "R_NDS32_SDA15S1_RELA",/* name */
837 FALSE, /* partial_inplace */
838 0x00007fff, /* src_mask */
839 0x00007fff, /* dst_mask */
840 FALSE), /* pcrel_offset */
842 HOWTO (R_NDS32_SDA15S0_RELA, /* type */
844 2, /* size (0 = byte, 1 = short, 2 = long) */
846 FALSE, /* pc_relative */
848 complain_overflow_signed, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_NDS32_SDA15S0_RELA",/* name */
851 FALSE, /* partial_inplace */
852 0x00007fff, /* src_mask */
853 0x00007fff, /* dst_mask */
854 FALSE), /* pcrel_offset */
856 /* GNU extension to record C++ vtable hierarchy */
857 HOWTO (R_NDS32_RELA_GNU_VTINHERIT, /* type */
859 2, /* size (0 = byte, 1 = short, 2 = long) */
861 FALSE, /* pc_relative */
863 complain_overflow_dont,/* complain_on_overflow */
864 NULL, /* special_function */
865 "R_NDS32_RELA_GNU_VTINHERIT", /* name */
866 FALSE, /* partial_inplace */
869 FALSE), /* pcrel_offset */
871 /* GNU extension to record C++ vtable member usage */
872 HOWTO (R_NDS32_RELA_GNU_VTENTRY, /* type */
874 2, /* size (0 = byte, 1 = short, 2 = long) */
876 FALSE, /* pc_relative */
878 complain_overflow_dont,/* complain_on_overflow */
879 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
880 "R_NDS32_RELA_GNU_VTENTRY", /* name */
881 FALSE, /* partial_inplace */
884 FALSE), /* pcrel_offset */
886 /* Like R_NDS32_20, but referring to the GOT table entry for
888 HOWTO (R_NDS32_GOT20, /* type */
890 2, /* size (0 = byte, 1 = short, 2 = long) */
892 FALSE, /* pc_relative */
894 complain_overflow_signed, /* complain_on_overflow */
895 bfd_elf_generic_reloc, /* special_function */
896 "R_NDS32_GOT20", /* name */
897 FALSE, /* partial_inplace */
898 0xfffff, /* src_mask */
899 0xfffff, /* dst_mask */
900 FALSE), /* pcrel_offset */
902 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
903 entry for the symbol. */
904 HOWTO (R_NDS32_25_PLTREL, /* type */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
908 TRUE, /* pc_relative */
910 complain_overflow_signed, /* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_NDS32_25_PLTREL", /* name */
913 FALSE, /* partial_inplace */
914 0xffffff, /* src_mask */
915 0xffffff, /* dst_mask */
916 TRUE), /* pcrel_offset */
918 /* This is used only by the dynamic linker. The symbol should exist
919 both in the object being run and in some shared library. The
920 dynamic linker copies the data addressed by the symbol from the
921 shared library into the object, because the object being
922 run has to have the data at some particular address. */
923 HOWTO (R_NDS32_COPY, /* type */
925 2, /* size (0 = byte, 1 = short, 2 = long) */
927 FALSE, /* pc_relative */
929 complain_overflow_bitfield, /* complain_on_overflow */
930 bfd_elf_generic_reloc, /* special_function */
931 "R_NDS32_COPY", /* name */
932 FALSE, /* partial_inplace */
933 0xffffffff, /* src_mask */
934 0xffffffff, /* dst_mask */
935 FALSE), /* pcrel_offset */
937 /* Like R_NDS32_20, but used when setting global offset table
939 HOWTO (R_NDS32_GLOB_DAT, /* type */
941 2, /* size (0 = byte, 1 = short, 2 = long) */
943 FALSE, /* pc_relative */
945 complain_overflow_bitfield, /* complain_on_overflow */
946 bfd_elf_generic_reloc, /* special_function */
947 "R_NDS32_GLOB_DAT", /* name */
948 FALSE, /* partial_inplace */
949 0xffffffff, /* src_mask */
950 0xffffffff, /* dst_mask */
951 FALSE), /* pcrel_offset */
953 /* Marks a procedure linkage table entry for a symbol. */
954 HOWTO (R_NDS32_JMP_SLOT, /* type */
956 2, /* size (0 = byte, 1 = short, 2 = long) */
958 FALSE, /* pc_relative */
960 complain_overflow_bitfield, /* complain_on_overflow */
961 bfd_elf_generic_reloc, /* special_function */
962 "R_NDS32_JMP_SLOT", /* name */
963 FALSE, /* partial_inplace */
964 0xffffffff, /* src_mask */
965 0xffffffff, /* dst_mask */
966 FALSE), /* pcrel_offset */
968 /* Used only by the dynamic linker. When the object is run, this
969 longword is set to the load address of the object, plus the
971 HOWTO (R_NDS32_RELATIVE, /* type */
973 2, /* size (0 = byte, 1 = short, 2 = long) */
975 FALSE, /* pc_relative */
977 complain_overflow_bitfield, /* complain_on_overflow */
978 bfd_elf_generic_reloc, /* special_function */
979 "R_NDS32_RELATIVE", /* name */
980 FALSE, /* partial_inplace */
981 0xffffffff, /* src_mask */
982 0xffffffff, /* dst_mask */
983 FALSE), /* pcrel_offset */
985 HOWTO (R_NDS32_GOTOFF, /* type */
987 2, /* size (0 = byte, 1 = short, 2 = long) */
989 FALSE, /* pc_relative */
991 complain_overflow_signed, /* complain_on_overflow */
992 bfd_elf_generic_reloc, /* special_function */
993 "R_NDS32_GOTOFF", /* name */
994 FALSE, /* partial_inplace */
995 0xfffff, /* src_mask */
996 0xfffff, /* dst_mask */
997 FALSE), /* pcrel_offset */
999 /* An PC Relative 20-bit relocation used when setting PIC offset
1001 HOWTO (R_NDS32_GOTPC20, /* type */
1003 2, /* size (0 = byte, 1 = short, 2 = long) */
1005 TRUE, /* pc_relative */
1007 complain_overflow_signed, /* complain_on_overflow */
1008 bfd_elf_generic_reloc, /* special_function */
1009 "R_NDS32_GOTPC20", /* name */
1010 FALSE, /* partial_inplace */
1011 0xfffff, /* src_mask */
1012 0xfffff, /* dst_mask */
1013 TRUE), /* pcrel_offset */
1015 /* Like R_NDS32_HI20, but referring to the GOT table entry for
1017 HOWTO (R_NDS32_GOT_HI20, /* type */
1018 12, /* rightshift */
1019 2, /* size (0 = byte, 1 = short, 2 = long) */
1021 FALSE, /* pc_relative */
1023 complain_overflow_dont,/* complain_on_overflow */
1024 bfd_elf_generic_reloc, /* special_function */
1025 "R_NDS32_GOT_HI20", /* name */
1026 FALSE, /* partial_inplace */
1027 0x000fffff, /* src_mask */
1028 0x000fffff, /* dst_mask */
1029 FALSE), /* pcrel_offset */
1030 HOWTO (R_NDS32_GOT_LO12, /* type */
1032 2, /* size (0 = byte, 1 = short, 2 = long) */
1034 FALSE, /* pc_relative */
1036 complain_overflow_dont,/* complain_on_overflow */
1037 bfd_elf_generic_reloc, /* special_function */
1038 "R_NDS32_GOT_LO12", /* name */
1039 FALSE, /* partial_inplace */
1040 0x00000fff, /* src_mask */
1041 0x00000fff, /* dst_mask */
1042 FALSE), /* pcrel_offset */
1044 /* An PC Relative relocation used when setting PIC offset table register.
1045 Like R_NDS32_HI20, but referring to the GOT table entry for
1047 HOWTO (R_NDS32_GOTPC_HI20, /* type */
1048 12, /* rightshift */
1049 2, /* size (0 = byte, 1 = short, 2 = long) */
1051 FALSE, /* pc_relative */
1053 complain_overflow_dont,/* complain_on_overflow */
1054 bfd_elf_generic_reloc, /* special_function */
1055 "R_NDS32_GOTPC_HI20", /* name */
1056 FALSE, /* partial_inplace */
1057 0x000fffff, /* src_mask */
1058 0x000fffff, /* dst_mask */
1059 TRUE), /* pcrel_offset */
1060 HOWTO (R_NDS32_GOTPC_LO12, /* type */
1062 2, /* size (0 = byte, 1 = short, 2 = long) */
1064 FALSE, /* pc_relative */
1066 complain_overflow_dont, /* complain_on_overflow */
1067 bfd_elf_generic_reloc, /* special_function */
1068 "R_NDS32_GOTPC_LO12", /* name */
1069 FALSE, /* partial_inplace */
1070 0x00000fff, /* src_mask */
1071 0x00000fff, /* dst_mask */
1072 TRUE), /* pcrel_offset */
1074 HOWTO (R_NDS32_GOTOFF_HI20, /* type */
1075 12, /* rightshift */
1076 2, /* size (0 = byte, 1 = short, 2 = long) */
1078 FALSE, /* pc_relative */
1080 complain_overflow_dont,/* complain_on_overflow */
1081 bfd_elf_generic_reloc, /* special_function */
1082 "R_NDS32_GOTOFF_HI20", /* name */
1083 FALSE, /* partial_inplace */
1084 0x000fffff, /* src_mask */
1085 0x000fffff, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1087 HOWTO (R_NDS32_GOTOFF_LO12, /* type */
1089 2, /* size (0 = byte, 1 = short, 2 = long) */
1091 FALSE, /* pc_relative */
1093 complain_overflow_dont,/* complain_on_overflow */
1094 bfd_elf_generic_reloc, /* special_function */
1095 "R_NDS32_GOTOFF_LO12", /* name */
1096 FALSE, /* partial_inplace */
1097 0x00000fff, /* src_mask */
1098 0x00000fff, /* dst_mask */
1099 FALSE), /* pcrel_offset */
1101 /* Alignment hint for relaxable instruction. This is used with
1102 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1103 in order to make next label aligned on word boundary. */
1104 HOWTO (R_NDS32_INSN16, /* type */
1106 2, /* size (0 = byte, 1 = short, 2 = long) */
1108 FALSE, /* pc_relative */
1110 complain_overflow_dont,/* complain_on_overflow */
1111 nds32_elf_ignore_reloc,/* special_function */
1112 "R_NDS32_INSN16", /* name */
1113 FALSE, /* partial_inplace */
1114 0x00000fff, /* src_mask */
1115 0x00000fff, /* dst_mask */
1116 FALSE), /* pcrel_offset */
1118 /* Alignment hint for label. */
1119 HOWTO (R_NDS32_LABEL, /* type */
1121 2, /* size (0 = byte, 1 = short, 2 = long) */
1123 FALSE, /* pc_relative */
1125 complain_overflow_dont,/* complain_on_overflow */
1126 nds32_elf_ignore_reloc,/* special_function */
1127 "R_NDS32_LABEL", /* name */
1128 FALSE, /* partial_inplace */
1129 0xffffffff, /* src_mask */
1130 0xffffffff, /* dst_mask */
1131 FALSE), /* pcrel_offset */
1133 /* Relax hint for unconditional call sequence */
1134 HOWTO (R_NDS32_LONGCALL1, /* type */
1136 2, /* size (0 = byte, 1 = short, 2 = long) */
1138 FALSE, /* pc_relative */
1140 complain_overflow_dont,/* complain_on_overflow */
1141 nds32_elf_ignore_reloc,/* special_function */
1142 "R_NDS32_LONGCALL1", /* name */
1143 FALSE, /* partial_inplace */
1144 0xffffffff, /* src_mask */
1145 0xffffffff, /* dst_mask */
1146 FALSE), /* pcrel_offset */
1148 /* Relax hint for conditional call sequence. */
1149 HOWTO (R_NDS32_LONGCALL2, /* type */
1151 2, /* size (0 = byte, 1 = short, 2 = long) */
1153 FALSE, /* pc_relative */
1155 complain_overflow_dont,/* complain_on_overflow */
1156 nds32_elf_ignore_reloc,/* special_function */
1157 "R_NDS32_LONGCALL2", /* name */
1158 FALSE, /* partial_inplace */
1159 0xffffffff, /* src_mask */
1160 0xffffffff, /* dst_mask */
1161 FALSE), /* pcrel_offset */
1163 /* Relax hint for conditional call sequence. */
1164 HOWTO (R_NDS32_LONGCALL3, /* type */
1166 2, /* size (0 = byte, 1 = short, 2 = long) */
1168 FALSE, /* pc_relative */
1170 complain_overflow_dont,/* complain_on_overflow */
1171 nds32_elf_ignore_reloc,/* special_function */
1172 "R_NDS32_LONGCALL3", /* name */
1173 FALSE, /* partial_inplace */
1174 0xffffffff, /* src_mask */
1175 0xffffffff, /* dst_mask */
1176 FALSE), /* pcrel_offset */
1178 /* Relax hint for unconditional branch sequence. */
1179 HOWTO (R_NDS32_LONGJUMP1, /* type */
1181 2, /* size (0 = byte, 1 = short, 2 = long) */
1183 FALSE, /* pc_relative */
1185 complain_overflow_dont,/* complain_on_overflow */
1186 nds32_elf_ignore_reloc,/* special_function */
1187 "R_NDS32_LONGJUMP1", /* name */
1188 FALSE, /* partial_inplace */
1189 0xffffffff, /* src_mask */
1190 0xffffffff, /* dst_mask */
1191 FALSE), /* pcrel_offset */
1193 /* Relax hint for conditional branch sequence. */
1194 HOWTO (R_NDS32_LONGJUMP2, /* type */
1196 2, /* size (0 = byte, 1 = short, 2 = long) */
1198 FALSE, /* pc_relative */
1200 complain_overflow_dont,/* complain_on_overflow */
1201 nds32_elf_ignore_reloc,/* special_function */
1202 "R_NDS32_LONGJUMP2", /* name */
1203 FALSE, /* partial_inplace */
1204 0xffffffff, /* src_mask */
1205 0xffffffff, /* dst_mask */
1206 FALSE), /* pcrel_offset */
1208 /* Relax hint for conditional branch sequence. */
1209 HOWTO (R_NDS32_LONGJUMP3, /* type */
1211 2, /* size (0 = byte, 1 = short, 2 = long) */
1213 FALSE, /* pc_relative */
1215 complain_overflow_dont,/* complain_on_overflow */
1216 nds32_elf_ignore_reloc,/* special_function */
1217 "R_NDS32_LONGJUMP3", /* name */
1218 FALSE, /* partial_inplace */
1219 0xffffffff, /* src_mask */
1220 0xffffffff, /* dst_mask */
1221 FALSE), /* pcrel_offset */
1223 /* Relax hint for load/store sequence. */
1224 HOWTO (R_NDS32_LOADSTORE, /* type */
1226 2, /* size (0 = byte, 1 = short, 2 = long) */
1228 FALSE, /* pc_relative */
1230 complain_overflow_dont,/* complain_on_overflow */
1231 nds32_elf_ignore_reloc,/* special_function */
1232 "R_NDS32_LOADSTORE", /* name */
1233 FALSE, /* partial_inplace */
1234 0xffffffff, /* src_mask */
1235 0xffffffff, /* dst_mask */
1236 FALSE), /* pcrel_offset */
1238 /* Relax hint for load/store sequence. */
1239 HOWTO (R_NDS32_9_FIXED_RELA, /* type */
1241 1, /* size (0 = byte, 1 = short, 2 = long) */
1243 FALSE, /* pc_relative */
1245 complain_overflow_dont,/* complain_on_overflow */
1246 nds32_elf_ignore_reloc,/* special_function */
1247 "R_NDS32_9_FIXED_RELA",/* name */
1248 FALSE, /* partial_inplace */
1249 0x000000ff, /* src_mask */
1250 0x000000ff, /* dst_mask */
1251 FALSE), /* pcrel_offset */
1253 /* Relax hint for load/store sequence. */
1254 HOWTO (R_NDS32_15_FIXED_RELA, /* type */
1256 2, /* size (0 = byte, 1 = short, 2 = long) */
1258 FALSE, /* pc_relative */
1260 complain_overflow_dont,/* complain_on_overflow */
1261 nds32_elf_ignore_reloc,/* special_function */
1262 "R_NDS32_15_FIXED_RELA", /* name */
1263 FALSE, /* partial_inplace */
1264 0x00003fff, /* src_mask */
1265 0x00003fff, /* dst_mask */
1266 FALSE), /* pcrel_offset */
1268 /* Relax hint for load/store sequence. */
1269 HOWTO (R_NDS32_17_FIXED_RELA, /* type */
1271 2, /* size (0 = byte, 1 = short, 2 = long) */
1273 FALSE, /* pc_relative */
1275 complain_overflow_dont,/* complain_on_overflow */
1276 nds32_elf_ignore_reloc,/* special_function */
1277 "R_NDS32_17_FIXED_RELA", /* name */
1278 FALSE, /* partial_inplace */
1279 0x0000ffff, /* src_mask */
1280 0x0000ffff, /* dst_mask */
1281 FALSE), /* pcrel_offset */
1283 /* Relax hint for load/store sequence. */
1284 HOWTO (R_NDS32_25_FIXED_RELA, /* type */
1286 2, /* size (0 = byte, 1 = short, 2 = long) */
1288 FALSE, /* pc_relative */
1290 complain_overflow_dont,/* complain_on_overflow */
1291 nds32_elf_ignore_reloc,/* special_function */
1292 "R_NDS32_25_FIXED_RELA", /* name */
1293 FALSE, /* partial_inplace */
1294 0x00ffffff, /* src_mask */
1295 0x00ffffff, /* dst_mask */
1296 FALSE), /* pcrel_offset */
1298 /* High 20 bits of PLT symbol offset relative to PC. */
1299 HOWTO (R_NDS32_PLTREL_HI20, /* type */
1300 12, /* rightshift */
1301 2, /* size (0 = byte, 1 = short, 2 = long) */
1303 FALSE, /* pc_relative */
1305 complain_overflow_dont,/* complain_on_overflow */
1306 bfd_elf_generic_reloc, /* special_function */
1307 "R_NDS32_PLTREL_HI20", /* name */
1308 FALSE, /* partial_inplace */
1309 0x000fffff, /* src_mask */
1310 0x000fffff, /* dst_mask */
1311 FALSE), /* pcrel_offset */
1313 /* Low 12 bits of PLT symbol offset relative to PC. */
1314 HOWTO (R_NDS32_PLTREL_LO12, /* type */
1316 2, /* size (0 = byte, 1 = short, 2 = long) */
1318 FALSE, /* pc_relative */
1320 complain_overflow_dont,/* complain_on_overflow */
1321 bfd_elf_generic_reloc, /* special_function */
1322 "R_NDS32_PLTREL_LO12", /* name */
1323 FALSE, /* partial_inplace */
1324 0x00000fff, /* src_mask */
1325 0x00000fff, /* dst_mask */
1326 FALSE), /* pcrel_offset */
1328 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1329 HOWTO (R_NDS32_PLT_GOTREL_HI20, /* type */
1330 12, /* rightshift */
1331 2, /* size (0 = byte, 1 = short, 2 = long) */
1333 FALSE, /* pc_relative */
1335 complain_overflow_dont,/* complain_on_overflow */
1336 bfd_elf_generic_reloc, /* special_function */
1337 "R_NDS32_PLT_GOTREL_HI20", /* name */
1338 FALSE, /* partial_inplace */
1339 0x000fffff, /* src_mask */
1340 0x000fffff, /* dst_mask */
1341 FALSE), /* pcrel_offset */
1343 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1344 HOWTO (R_NDS32_PLT_GOTREL_LO12, /* type */
1346 2, /* size (0 = byte, 1 = short, 2 = long) */
1348 FALSE, /* pc_relative */
1350 complain_overflow_dont,/* complain_on_overflow */
1351 bfd_elf_generic_reloc, /* special_function */
1352 "R_NDS32_PLT_GOTREL_LO12", /* name */
1353 FALSE, /* partial_inplace */
1354 0x00000fff, /* src_mask */
1355 0x00000fff, /* dst_mask */
1356 FALSE), /* pcrel_offset */
1358 /* Small data area 12 bits offset. */
1359 HOWTO (R_NDS32_SDA12S2_DP_RELA, /* type */
1361 2, /* size (0 = byte, 1 = short, 2 = long) */
1363 FALSE, /* pc_relative */
1365 complain_overflow_signed, /* complain_on_overflow */
1366 bfd_elf_generic_reloc, /* special_function */
1367 "R_NDS32_SDA12S2_DP_RELA", /* name */
1368 FALSE, /* partial_inplace */
1369 0x00000fff, /* src_mask */
1370 0x00000fff, /* dst_mask */
1371 FALSE), /* pcrel_offset */
1373 /* Small data area 12 bits offset. */
1374 HOWTO (R_NDS32_SDA12S2_SP_RELA, /* type */
1376 2, /* size (0 = byte, 1 = short, 2 = long) */
1378 FALSE, /* pc_relative */
1380 complain_overflow_signed, /* complain_on_overflow */
1381 bfd_elf_generic_reloc, /* special_function */
1382 "R_NDS32_SDA12S2_SP_RELA", /* name */
1383 FALSE, /* partial_inplace */
1384 0x00000fff, /* src_mask */
1385 0x00000fff, /* dst_mask */
1386 FALSE), /* pcrel_offset */
1387 /* Lower 12 bits of address. */
1389 HOWTO (R_NDS32_LO12S2_DP_RELA, /* type */
1391 2, /* size (0 = byte, 1 = short, 2 = long) */
1393 FALSE, /* pc_relative */
1395 complain_overflow_dont,/* complain_on_overflow */
1396 bfd_elf_generic_reloc, /* special_function */
1397 "R_NDS32_LO12S2_DP_RELA", /* name */
1398 FALSE, /* partial_inplace */
1399 0x000003ff, /* src_mask */
1400 0x000003ff, /* dst_mask */
1401 FALSE), /* pcrel_offset */
1403 /* Lower 12 bits of address. */
1404 HOWTO (R_NDS32_LO12S2_SP_RELA,/* type */
1406 2, /* size (0 = byte, 1 = short, 2 = long) */
1408 FALSE, /* pc_relative */
1410 complain_overflow_dont,/* complain_on_overflow */
1411 bfd_elf_generic_reloc, /* special_function */
1412 "R_NDS32_LO12S2_SP_RELA", /* name */
1413 FALSE, /* partial_inplace */
1414 0x000003ff, /* src_mask */
1415 0x000003ff, /* dst_mask */
1416 FALSE), /* pcrel_offset */
1417 /* Lower 12 bits of address. Special identity for or case. */
1418 HOWTO (R_NDS32_LO12S0_ORI_RELA, /* type */
1420 2, /* size (0 = byte, 1 = short, 2 = long) */
1422 FALSE, /* pc_relative */
1424 complain_overflow_dont,/* complain_on_overflow */
1425 bfd_elf_generic_reloc, /* special_function */
1426 "R_NDS32_LO12S0_ORI_RELA", /* name */
1427 FALSE, /* partial_inplace */
1428 0x00000fff, /* src_mask */
1429 0x00000fff, /* dst_mask */
1430 FALSE), /* pcrel_offset */
1431 /* Small data area 19 bits offset. */
1432 HOWTO (R_NDS32_SDA16S3_RELA, /* type */
1434 2, /* size (0 = byte, 1 = short, 2 = long) */
1436 FALSE, /* pc_relative */
1438 complain_overflow_signed, /* complain_on_overflow */
1439 bfd_elf_generic_reloc, /* special_function */
1440 "R_NDS32_SDA16S3_RELA",/* name */
1441 FALSE, /* partial_inplace */
1442 0x0000ffff, /* src_mask */
1443 0x0000ffff, /* dst_mask */
1444 FALSE), /* pcrel_offset */
1446 /* Small data area 15 bits offset. */
1447 HOWTO (R_NDS32_SDA17S2_RELA, /* type */
1449 2, /* size (0 = byte, 1 = short, 2 = long) */
1451 FALSE, /* pc_relative */
1453 complain_overflow_signed, /* complain_on_overflow */
1454 bfd_elf_generic_reloc, /* special_function */
1455 "R_NDS32_SDA17S2_RELA",/* name */
1456 FALSE, /* partial_inplace */
1457 0x0001ffff, /* src_mask */
1458 0x0001ffff, /* dst_mask */
1459 FALSE), /* pcrel_offset */
1461 HOWTO (R_NDS32_SDA18S1_RELA, /* type */
1463 2, /* size (0 = byte, 1 = short, 2 = long) */
1465 FALSE, /* pc_relative */
1467 complain_overflow_signed, /* complain_on_overflow */
1468 bfd_elf_generic_reloc, /* special_function */
1469 "R_NDS32_SDA18S1_RELA",/* name */
1470 FALSE, /* partial_inplace */
1471 0x0003ffff, /* src_mask */
1472 0x0003ffff, /* dst_mask */
1473 FALSE), /* pcrel_offset */
1475 HOWTO (R_NDS32_SDA19S0_RELA, /* type */
1477 2, /* size (0 = byte, 1 = short, 2 = long) */
1479 FALSE, /* pc_relative */
1481 complain_overflow_signed, /* complain_on_overflow */
1482 bfd_elf_generic_reloc, /* special_function */
1483 "R_NDS32_SDA19S0_RELA",/* name */
1484 FALSE, /* partial_inplace */
1485 0x0007ffff, /* src_mask */
1486 0x0007ffff, /* dst_mask */
1487 FALSE), /* pcrel_offset */
1488 HOWTO (R_NDS32_DWARF2_OP1_RELA, /* type */
1490 0, /* size (0 = byte, 1 = short, 2 = long) */
1492 FALSE, /* pc_relative */
1494 complain_overflow_dont,/* complain_on_overflow */
1495 nds32_elf_ignore_reloc,/* special_function */
1496 "R_NDS32_DWARF2_OP1_RELA", /* name */
1497 FALSE, /* partial_inplace */
1498 0xff, /* src_mask */
1499 0xff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1501 HOWTO (R_NDS32_DWARF2_OP2_RELA, /* type */
1503 1, /* size (0 = byte, 1 = short, 2 = long) */
1505 FALSE, /* pc_relative */
1507 complain_overflow_dont,/* complain_on_overflow */
1508 nds32_elf_ignore_reloc,/* special_function */
1509 "R_NDS32_DWARF2_OP2_RELA", /* name */
1510 FALSE, /* partial_inplace */
1511 0xffff, /* src_mask */
1512 0xffff, /* dst_mask */
1513 FALSE), /* pcrel_offset */
1514 HOWTO (R_NDS32_DWARF2_LEB_RELA, /* type */
1516 2, /* size (0 = byte, 1 = short, 2 = long) */
1518 FALSE, /* pc_relative */
1520 complain_overflow_dont,/* complain_on_overflow */
1521 nds32_elf_ignore_reloc,/* special_function */
1522 "R_NDS32_DWARF2_LEB_RELA", /* name */
1523 FALSE, /* partial_inplace */
1524 0xffffffff, /* src_mask */
1525 0xffffffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527 HOWTO (R_NDS32_UPDATE_TA_RELA,/* type */
1529 1, /* size (0 = byte, 1 = short, 2 = long) */
1531 FALSE, /* pc_relative */
1533 complain_overflow_dont,/* complain_on_overflow */
1534 nds32_elf_ignore_reloc,/* special_function */
1535 "R_NDS32_UPDATE_TA_RELA", /* name */
1536 FALSE, /* partial_inplace */
1537 0xffff, /* src_mask */
1538 0xffff, /* dst_mask */
1539 FALSE), /* pcrel_offset */
1540 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1541 entry for the symbol. */
1542 HOWTO (R_NDS32_9_PLTREL, /* type */
1544 1, /* size (0 = byte, 1 = short, 2 = long) */
1546 TRUE, /* pc_relative */
1548 complain_overflow_signed, /* complain_on_overflow */
1549 bfd_elf_generic_reloc, /* special_function */
1550 "R_NDS32_9_PLTREL", /* name */
1551 FALSE, /* partial_inplace */
1552 0xff, /* src_mask */
1553 0xff, /* dst_mask */
1554 TRUE), /* pcrel_offset */
1555 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1556 HOWTO (R_NDS32_PLT_GOTREL_LO20, /* type */
1558 2, /* size (0 = byte, 1 = short, 2 = long) */
1560 FALSE, /* pc_relative */
1562 complain_overflow_dont,/* complain_on_overflow */
1563 bfd_elf_generic_reloc, /* special_function */
1564 "R_NDS32_PLT_GOTREL_LO20", /* name */
1565 FALSE, /* partial_inplace */
1566 0x000fffff, /* src_mask */
1567 0x000fffff, /* dst_mask */
1568 FALSE), /* pcrel_offset */
1569 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1570 HOWTO (R_NDS32_PLT_GOTREL_LO15, /* type */
1572 2, /* size (0 = byte, 1 = short, 2 = long) */
1574 FALSE, /* pc_relative */
1576 complain_overflow_dont,/* complain_on_overflow */
1577 bfd_elf_generic_reloc, /* special_function */
1578 "R_NDS32_PLT_GOTREL_LO15", /* name */
1579 FALSE, /* partial_inplace */
1580 0x00007fff, /* src_mask */
1581 0x00007fff, /* dst_mask */
1582 FALSE), /* pcrel_offset */
1583 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1584 HOWTO (R_NDS32_PLT_GOTREL_LO19, /* type */
1586 2, /* size (0 = byte, 1 = short, 2 = long) */
1588 FALSE, /* pc_relative */
1590 complain_overflow_dont,/* complain_on_overflow */
1591 bfd_elf_generic_reloc, /* special_function */
1592 "R_NDS32_PLT_GOTREL_LO19", /* name */
1593 FALSE, /* partial_inplace */
1594 0x0007ffff, /* src_mask */
1595 0x0007ffff, /* dst_mask */
1596 FALSE), /* pcrel_offset */
1597 HOWTO (R_NDS32_GOT_LO15, /* type */
1599 2, /* size (0 = byte, 1 = short, 2 = long) */
1601 FALSE, /* pc_relative */
1603 complain_overflow_dont,/* complain_on_overflow */
1604 bfd_elf_generic_reloc, /* special_function */
1605 "R_NDS32_GOT_LO15", /* name */
1606 FALSE, /* partial_inplace */
1607 0x00007fff, /* src_mask */
1608 0x00007fff, /* dst_mask */
1609 FALSE), /* pcrel_offset */
1610 HOWTO (R_NDS32_GOT_LO19, /* type */
1612 2, /* size (0 = byte, 1 = short, 2 = long) */
1614 FALSE, /* pc_relative */
1616 complain_overflow_dont,/* complain_on_overflow */
1617 bfd_elf_generic_reloc, /* special_function */
1618 "R_NDS32_GOT_LO19", /* name */
1619 FALSE, /* partial_inplace */
1620 0x0007ffff, /* src_mask */
1621 0x0007ffff, /* dst_mask */
1622 FALSE), /* pcrel_offset */
1623 HOWTO (R_NDS32_GOTOFF_LO15, /* type */
1625 2, /* size (0 = byte, 1 = short, 2 = long) */
1627 FALSE, /* pc_relative */
1629 complain_overflow_dont,/* complain_on_overflow */
1630 bfd_elf_generic_reloc, /* special_function */
1631 "R_NDS32_GOTOFF_LO15", /* name */
1632 FALSE, /* partial_inplace */
1633 0x00007fff, /* src_mask */
1634 0x00007fff, /* dst_mask */
1635 FALSE), /* pcrel_offset */
1636 HOWTO (R_NDS32_GOTOFF_LO19, /* type */
1638 2, /* size (0 = byte, 1 = short, 2 = long) */
1640 FALSE, /* pc_relative */
1642 complain_overflow_dont,/* complain_on_overflow */
1643 bfd_elf_generic_reloc, /* special_function */
1644 "R_NDS32_GOTOFF_LO19", /* name */
1645 FALSE, /* partial_inplace */
1646 0x0007ffff, /* src_mask */
1647 0x0007ffff, /* dst_mask */
1648 FALSE), /* pcrel_offset */
1649 /* GOT 15 bits offset. */
1650 HOWTO (R_NDS32_GOT15S2_RELA, /* type */
1652 2, /* size (0 = byte, 1 = short, 2 = long) */
1654 FALSE, /* pc_relative */
1656 complain_overflow_signed, /* complain_on_overflow */
1657 bfd_elf_generic_reloc, /* special_function */
1658 "R_NDS32_GOT15S2_RELA",/* name */
1659 FALSE, /* partial_inplace */
1660 0x00007fff, /* src_mask */
1661 0x00007fff, /* dst_mask */
1662 FALSE), /* pcrel_offset */
1663 /* GOT 17 bits offset. */
1664 HOWTO (R_NDS32_GOT17S2_RELA, /* type */
1666 2, /* size (0 = byte, 1 = short, 2 = long) */
1668 FALSE, /* pc_relative */
1670 complain_overflow_signed, /* complain_on_overflow */
1671 bfd_elf_generic_reloc, /* special_function */
1672 "R_NDS32_GOT17S2_RELA",/* name */
1673 FALSE, /* partial_inplace */
1674 0x0001ffff, /* src_mask */
1675 0x0001ffff, /* dst_mask */
1676 FALSE), /* pcrel_offset */
1677 /* A 5 bit address. */
1678 HOWTO (R_NDS32_5_RELA, /* type */
1680 1, /* size (0 = byte, 1 = short, 2 = long) */
1682 FALSE, /* pc_relative */
1684 complain_overflow_signed, /* complain_on_overflow */
1685 bfd_elf_generic_reloc, /* special_function */
1686 "R_NDS32_5_RELA", /* name */
1687 FALSE, /* partial_inplace */
1688 0x1f, /* src_mask */
1689 0x1f, /* dst_mask */
1690 FALSE), /* pcrel_offset */
1691 HOWTO (R_NDS32_10_UPCREL_RELA,/* type */
1693 1, /* size (0 = byte, 1 = short, 2 = long) */
1695 TRUE, /* pc_relative */
1697 complain_overflow_unsigned, /* complain_on_overflow */
1698 bfd_elf_generic_reloc, /* special_function */
1699 "R_NDS32_10_UPCREL_RELA", /* name */
1700 FALSE, /* partial_inplace */
1701 0x1ff, /* src_mask */
1702 0x1ff, /* dst_mask */
1703 TRUE), /* pcrel_offset */
1704 HOWTO (R_NDS32_SDA_FP7U2_RELA,/* type */
1706 1, /* size (0 = byte, 1 = short, 2 = long) */
1708 FALSE, /* pc_relative */
1710 complain_overflow_unsigned, /* complain_on_overflow */
1711 bfd_elf_generic_reloc, /* special_function */
1712 "R_NDS32_SDA_FP7U2_RELA", /* name */
1713 FALSE, /* partial_inplace */
1714 0x0000007f, /* src_mask */
1715 0x0000007f, /* dst_mask */
1716 FALSE), /* pcrel_offset */
1717 HOWTO (R_NDS32_WORD_9_PCREL_RELA, /* type */
1719 2, /* size (0 = byte, 1 = short, 2 = long) */
1721 TRUE, /* pc_relative */
1723 complain_overflow_signed, /* complain_on_overflow */
1724 bfd_elf_generic_reloc, /* special_function */
1725 "R_NDS32_WORD_9_PCREL_RELA", /* name */
1726 FALSE, /* partial_inplace */
1727 0xff, /* src_mask */
1728 0xff, /* dst_mask */
1729 TRUE), /* pcrel_offset */
1730 HOWTO (R_NDS32_25_ABS_RELA, /* type */
1732 2, /* size (0 = byte, 1 = short, 2 = long) */
1734 FALSE, /* pc_relative */
1736 complain_overflow_dont,/* complain_on_overflow */
1737 bfd_elf_generic_reloc, /* special_function */
1738 "R_NDS32_25_ABS_RELA", /* name */
1739 FALSE, /* partial_inplace */
1740 0xffffff, /* src_mask */
1741 0xffffff, /* dst_mask */
1742 FALSE), /* pcrel_offset */
1744 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1745 HOWTO (R_NDS32_17IFC_PCREL_RELA, /* type */
1747 2, /* size (0 = byte, 1 = short, 2 = long) */
1749 TRUE, /* pc_relative */
1751 complain_overflow_signed, /* complain_on_overflow */
1752 bfd_elf_generic_reloc, /* special_function */
1753 "R_NDS32_17IFC_PCREL_RELA", /* name */
1754 FALSE, /* partial_inplace */
1755 0xffff, /* src_mask */
1756 0xffff, /* dst_mask */
1757 TRUE), /* pcrel_offset */
1759 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1760 HOWTO (R_NDS32_10IFCU_PCREL_RELA, /* type */
1762 1, /* size (0 = byte, 1 = short, 2 = long) */
1764 TRUE, /* pc_relative */
1766 complain_overflow_unsigned, /* complain_on_overflow */
1767 bfd_elf_generic_reloc, /* special_function */
1768 "R_NDS32_10IFCU_PCREL_RELA", /* name */
1769 FALSE, /* partial_inplace */
1770 0x1ff, /* src_mask */
1771 0x1ff, /* dst_mask */
1772 TRUE), /* pcrel_offset */
1774 /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol. */
1775 HOWTO (R_NDS32_TLS_LE_HI20, /* type */
1776 12, /* rightshift */
1777 2, /* size (0 = byte, 1 = short, 2 = long) */
1779 FALSE, /* pc_relative */
1781 complain_overflow_dont, /* complain_on_overflow */
1782 bfd_elf_generic_reloc, /* special_function */
1783 "R_NDS32_TLS_LE_HI20", /* name */
1784 FALSE, /* partial_inplace */
1785 0x000fffff, /* src_mask */
1786 0x000fffff, /* dst_mask */
1787 FALSE), /* pcrel_offset */
1788 HOWTO (R_NDS32_TLS_LE_LO12, /* type */
1790 2, /* size (0 = byte, 1 = short, 2 = long) */
1792 FALSE, /* pc_relative */
1794 complain_overflow_dont, /* complain_on_overflow */
1795 bfd_elf_generic_reloc, /* special_function */
1796 "R_NDS32_TLS_LE_LO12", /* name */
1797 FALSE, /* partial_inplace */
1798 0x00000fff, /* src_mask */
1799 0x00000fff, /* dst_mask */
1800 FALSE), /* pcrel_offset */
1802 /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol. */
1803 HOWTO (R_NDS32_TLS_IE_HI20, /* type */
1804 12, /* rightshift */
1805 2, /* size (0 = byte, 1 = short, 2 = long) */
1807 FALSE, /* pc_relative */
1809 complain_overflow_dont, /* complain_on_overflow */
1810 bfd_elf_generic_reloc, /* special_function */
1811 "R_NDS32_TLS_IE_HI20", /* name */
1812 FALSE, /* partial_inplace */
1813 0x000fffff, /* src_mask */
1814 0x000fffff, /* dst_mask */
1815 FALSE), /* pcrel_offset */
1816 HOWTO (R_NDS32_TLS_IE_LO12S2, /* type */
1818 2, /* size (0 = byte, 1 = short, 2 = long) */
1820 FALSE, /* pc_relative */
1822 complain_overflow_dont, /* complain_on_overflow */
1823 bfd_elf_generic_reloc, /* special_function */
1824 "R_NDS32_TLS_IE_LO12S2", /* name */
1825 FALSE, /* partial_inplace */
1826 0x000003ff, /* src_mask */
1827 0x000003ff, /* dst_mask */
1828 FALSE), /* pcrel_offset */
1829 /* Mark a TLS IE entry in GOT. */
1830 HOWTO (R_NDS32_TLS_TPOFF, /* type */
1832 2, /* size (0 = byte, 1 = short, 2 = long) */
1834 FALSE, /* pc_relative */
1836 complain_overflow_bitfield, /* complain_on_overflow */
1837 bfd_elf_generic_reloc, /* special_function */
1838 "R_NDS32_TLS_TPOFF", /* name */
1839 FALSE, /* partial_inplace */
1840 0xffffffff, /* src_mask */
1841 0xffffffff, /* dst_mask */
1842 FALSE), /* pcrel_offset */
1843 /* A 20 bit address. */
1844 HOWTO (R_NDS32_TLS_LE_20, /* type */
1846 2, /* size (0 = byte, 1 = short, 2 = long) */
1848 FALSE, /* pc_relative */
1850 complain_overflow_signed, /* complain_on_overflow */
1851 bfd_elf_generic_reloc, /* special_function */
1852 "R_NDS32_TLS_LE_20", /* name */
1853 FALSE, /* partial_inplace */
1854 0xfffff, /* src_mask */
1855 0xfffff, /* dst_mask */
1856 FALSE), /* pcrel_offset */
1857 HOWTO (R_NDS32_TLS_LE_15S0, /* type */
1859 2, /* size (0 = byte, 1 = short, 2 = long) */
1861 FALSE, /* pc_relative */
1863 complain_overflow_signed, /* complain_on_overflow */
1864 bfd_elf_generic_reloc, /* special_function */
1865 "R_NDS32_TLS_LE_15S0", /* name */
1866 FALSE, /* partial_inplace */
1867 0x7fff, /* src_mask */
1868 0x7fff, /* dst_mask */
1869 FALSE), /* pcrel_offset */
1870 HOWTO (R_NDS32_TLS_LE_15S1, /* type */
1872 2, /* size (0 = byte, 1 = short, 2 = long) */
1874 FALSE, /* pc_relative */
1876 complain_overflow_signed, /* complain_on_overflow */
1877 bfd_elf_generic_reloc, /* special_function */
1878 "R_NDS32_TLS_LE_15S1", /* name */
1879 FALSE, /* partial_inplace */
1880 0x7fff, /* src_mask */
1881 0x7fff, /* dst_mask */
1882 FALSE), /* pcrel_offset */
1883 HOWTO (R_NDS32_TLS_LE_15S2, /* type */
1885 2, /* size (0 = byte, 1 = short, 2 = long) */
1887 FALSE, /* pc_relative */
1889 complain_overflow_signed, /* complain_on_overflow */
1890 bfd_elf_generic_reloc, /* special_function */
1891 "R_NDS32_TLS_LE_15S2", /* name */
1892 FALSE, /* partial_inplace */
1893 0x7fff, /* src_mask */
1894 0x7fff, /* dst_mask */
1895 FALSE), /* pcrel_offset */
1897 /* Relax hint for unconditional call sequence */
1898 HOWTO (R_NDS32_LONGCALL4, /* type */
1900 2, /* size (0 = byte, 1 = short, 2 = long) */
1902 FALSE, /* pc_relative */
1904 complain_overflow_dont, /* complain_on_overflow */
1905 nds32_elf_ignore_reloc, /* special_function */
1906 "R_NDS32_LONGCALL4", /* name */
1907 FALSE, /* partial_inplace */
1908 0xffffffff, /* src_mask */
1909 0xffffffff, /* dst_mask */
1910 FALSE), /* pcrel_offset */
1912 /* Relax hint for conditional call sequence. */
1913 HOWTO (R_NDS32_LONGCALL5, /* type */
1915 2, /* size (0 = byte, 1 = short, 2 = long) */
1917 FALSE, /* pc_relative */
1919 complain_overflow_dont, /* complain_on_overflow */
1920 nds32_elf_ignore_reloc, /* special_function */
1921 "R_NDS32_LONGCALL5", /* name */
1922 FALSE, /* partial_inplace */
1923 0xffffffff, /* src_mask */
1924 0xffffffff, /* dst_mask */
1925 FALSE), /* pcrel_offset */
1927 /* Relax hint for conditional call sequence. */
1928 HOWTO (R_NDS32_LONGCALL6, /* type */
1930 2, /* size (0 = byte, 1 = short, 2 = long) */
1932 FALSE, /* pc_relative */
1934 complain_overflow_dont, /* complain_on_overflow */
1935 nds32_elf_ignore_reloc, /* special_function */
1936 "R_NDS32_LONGCALL6", /* name */
1937 FALSE, /* partial_inplace */
1938 0xffffffff, /* src_mask */
1939 0xffffffff, /* dst_mask */
1940 FALSE), /* pcrel_offset */
1942 /* Relax hint for unconditional branch sequence. */
1943 HOWTO (R_NDS32_LONGJUMP4, /* type */
1945 2, /* size (0 = byte, 1 = short, 2 = long) */
1947 FALSE, /* pc_relative */
1949 complain_overflow_dont, /* complain_on_overflow */
1950 nds32_elf_ignore_reloc, /* special_function */
1951 "R_NDS32_LONGJUMP4", /* name */
1952 FALSE, /* partial_inplace */
1953 0xffffffff, /* src_mask */
1954 0xffffffff, /* dst_mask */
1955 FALSE), /* pcrel_offset */
1957 /* Relax hint for conditional branch sequence. */
1958 HOWTO (R_NDS32_LONGJUMP5, /* type */
1960 2, /* size (0 = byte, 1 = short, 2 = long) */
1962 FALSE, /* pc_relative */
1964 complain_overflow_dont, /* complain_on_overflow */
1965 nds32_elf_ignore_reloc, /* special_function */
1966 "R_NDS32_LONGJUMP5", /* name */
1967 FALSE, /* partial_inplace */
1968 0xffffffff, /* src_mask */
1969 0xffffffff, /* dst_mask */
1970 FALSE), /* pcrel_offset */
1972 /* Relax hint for conditional branch sequence. */
1973 HOWTO (R_NDS32_LONGJUMP6, /* type */
1975 2, /* size (0 = byte, 1 = short, 2 = long) */
1977 FALSE, /* pc_relative */
1979 complain_overflow_dont, /* complain_on_overflow */
1980 nds32_elf_ignore_reloc, /* special_function */
1981 "R_NDS32_LONGJUMP6", /* name */
1982 FALSE, /* partial_inplace */
1983 0xffffffff, /* src_mask */
1984 0xffffffff, /* dst_mask */
1985 FALSE), /* pcrel_offset */
1987 /* Relax hint for conditional branch sequence. */
1988 HOWTO (R_NDS32_LONGJUMP7, /* type */
1990 2, /* size (0 = byte, 1 = short, 2 = long) */
1992 FALSE, /* pc_relative */
1994 complain_overflow_dont, /* complain_on_overflow */
1995 nds32_elf_ignore_reloc, /* special_function */
1996 "R_NDS32_LONGJUMP7", /* name */
1997 FALSE, /* partial_inplace */
1998 0xffffffff, /* src_mask */
1999 0xffffffff, /* dst_mask */
2000 FALSE), /* pcrel_offset */
2003 /* Relocations used for relaxation. */
2004 static reloc_howto_type nds32_elf_relax_howto_table[] =
2006 HOWTO (R_NDS32_RELAX_ENTRY, /* type */
2008 2, /* size (0 = byte, 1 = short, 2 = long) */
2010 FALSE, /* pc_relative */
2012 complain_overflow_dont,/* complain_on_overflow */
2013 nds32_elf_ignore_reloc,/* special_function */
2014 "R_NDS32_RELAX_ENTRY", /* name */
2015 FALSE, /* partial_inplace */
2016 0xffffffff, /* src_mask */
2017 0xffffffff, /* dst_mask */
2018 FALSE), /* pcrel_offset */
2019 HOWTO (R_NDS32_GOT_SUFF, /* type */
2021 2, /* size (0 = byte, 1 = short, 2 = long) */
2023 FALSE, /* pc_relative */
2025 complain_overflow_dont,/* complain_on_overflow */
2026 nds32_elf_ignore_reloc,/* special_function */
2027 "R_NDS32_GOT_SUFF", /* name */
2028 FALSE, /* partial_inplace */
2029 0xffffffff, /* src_mask */
2030 0xffffffff, /* dst_mask */
2031 FALSE), /* pcrel_offset */
2032 HOWTO (R_NDS32_GOTOFF_SUFF, /* type */
2034 2, /* size (0 = byte, 1 = short, 2 = long) */
2036 FALSE, /* pc_relative */
2038 complain_overflow_bitfield, /* complain_on_overflow */
2039 nds32_elf_ignore_reloc,/* special_function */
2040 "R_NDS32_GOTOFF_SUFF", /* name */
2041 FALSE, /* partial_inplace */
2042 0xffffffff, /* src_mask */
2043 0xffffffff, /* dst_mask */
2044 FALSE), /* pcrel_offset */
2045 HOWTO (R_NDS32_PLT_GOT_SUFF, /* type */
2047 2, /* size (0 = byte, 1 = short, 2 = long) */
2049 FALSE, /* pc_relative */
2051 complain_overflow_dont,/* complain_on_overflow */
2052 nds32_elf_ignore_reloc,/* special_function */
2053 "R_NDS32_PLT_GOT_SUFF",/* name */
2054 FALSE, /* partial_inplace */
2055 0xffffffff, /* src_mask */
2056 0xffffffff, /* dst_mask */
2057 FALSE), /* pcrel_offset */
2058 HOWTO (R_NDS32_MULCALL_SUFF, /* type */
2060 2, /* size (0 = byte, 1 = short, 2 = long) */
2062 FALSE, /* pc_relative */
2064 complain_overflow_dont,/* complain_on_overflow */
2065 nds32_elf_ignore_reloc,/* special_function */
2066 "R_NDS32_MULCALL_SUFF",/* name */
2067 FALSE, /* partial_inplace */
2068 0xffffffff, /* src_mask */
2069 0xffffffff, /* dst_mask */
2070 FALSE), /* pcrel_offset */
2071 HOWTO (R_NDS32_PTR, /* type */
2073 2, /* size (0 = byte, 1 = short, 2 = long) */
2075 FALSE, /* pc_relative */
2077 complain_overflow_dont,/* complain_on_overflow */
2078 nds32_elf_ignore_reloc,/* special_function */
2079 "R_NDS32_PTR", /* name */
2080 FALSE, /* partial_inplace */
2081 0xffffffff, /* src_mask */
2082 0xffffffff, /* dst_mask */
2083 FALSE), /* pcrel_offset */
2084 HOWTO (R_NDS32_PTR_COUNT, /* type */
2086 2, /* size (0 = byte, 1 = short, 2 = long) */
2088 FALSE, /* pc_relative */
2090 complain_overflow_dont,/* complain_on_overflow */
2091 nds32_elf_ignore_reloc,/* special_function */
2092 "R_NDS32_PTR_COUNT", /* name */
2093 FALSE, /* partial_inplace */
2094 0xffffffff, /* src_mask */
2095 0xffffffff, /* dst_mask */
2096 FALSE), /* pcrel_offset */
2097 HOWTO (R_NDS32_PTR_RESOLVED, /* type */
2099 2, /* size (0 = byte, 1 = short, 2 = long) */
2101 FALSE, /* pc_relative */
2103 complain_overflow_dont,/* complain_on_overflow */
2104 nds32_elf_ignore_reloc,/* special_function */
2105 "R_NDS32_PTR_RESOLVED",/* name */
2106 FALSE, /* partial_inplace */
2107 0xffffffff, /* src_mask */
2108 0xffffffff, /* dst_mask */
2109 FALSE), /* pcrel_offset */
2110 HOWTO (R_NDS32_PLTBLOCK, /* type */
2112 2, /* size (0 = byte, 1 = short, 2 = long) */
2114 FALSE, /* pc_relative */
2116 complain_overflow_dont,/* complain_on_overflow */
2117 nds32_elf_ignore_reloc,/* special_function */
2118 "R_NDS32_PLTBLOCK", /* name */
2119 FALSE, /* partial_inplace */
2120 0xffffffff, /* src_mask */
2121 0xffffffff, /* dst_mask */
2122 FALSE), /* pcrel_offset */
2123 HOWTO (R_NDS32_RELAX_REGION_BEGIN, /* type */
2125 2, /* size (0 = byte, 1 = short, 2 = long) */
2127 FALSE, /* pc_relative */
2129 complain_overflow_dont,/* complain_on_overflow */
2130 nds32_elf_ignore_reloc,/* special_function */
2131 "R_NDS32_RELAX_REGION_BEGIN", /* name */
2132 FALSE, /* partial_inplace */
2133 0xffffffff, /* src_mask */
2134 0xffffffff, /* dst_mask */
2135 FALSE), /* pcrel_offset */
2136 HOWTO (R_NDS32_RELAX_REGION_END, /* type */
2138 2, /* size (0 = byte, 1 = short, 2 = long) */
2140 FALSE, /* pc_relative */
2142 complain_overflow_dont,/* complain_on_overflow */
2143 nds32_elf_ignore_reloc,/* special_function */
2144 "R_NDS32_RELAX_REGION_END", /* name */
2145 FALSE, /* partial_inplace */
2146 0xffffffff, /* src_mask */
2147 0xffffffff, /* dst_mask */
2148 FALSE), /* pcrel_offset */
2149 HOWTO (R_NDS32_MINUEND, /* type */
2151 2, /* size (0 = byte, 1 = short, 2 = long) */
2153 FALSE, /* pc_relative */
2155 complain_overflow_dont,/* complain_on_overflow */
2156 nds32_elf_ignore_reloc,/* special_function */
2157 "R_NDS32_MINUEND", /* name */
2158 FALSE, /* partial_inplace */
2159 0xffffffff, /* src_mask */
2160 0xffffffff, /* dst_mask */
2161 FALSE), /* pcrel_offset */
2162 HOWTO (R_NDS32_SUBTRAHEND, /* type */
2164 2, /* size (0 = byte, 1 = short, 2 = long) */
2166 FALSE, /* pc_relative */
2168 complain_overflow_dont,/* complain_on_overflow */
2169 nds32_elf_ignore_reloc,/* special_function */
2170 "R_NDS32_SUBTRAHEND", /* name */
2171 FALSE, /* partial_inplace */
2172 0xffffffff, /* src_mask */
2173 0xffffffff, /* dst_mask */
2174 FALSE), /* pcrel_offset */
2175 HOWTO (R_NDS32_DIFF8, /* type */
2177 0, /* size (0 = byte, 1 = short, 2 = long) */
2179 FALSE, /* pc_relative */
2181 complain_overflow_dont,/* complain_on_overflow */
2182 nds32_elf_ignore_reloc,/* special_function */
2183 "R_NDS32_DIFF8", /* name */
2184 FALSE, /* partial_inplace */
2185 0x000000ff, /* src_mask */
2186 0x000000ff, /* dst_mask */
2187 FALSE), /* pcrel_offset */
2188 HOWTO (R_NDS32_DIFF16, /* type */
2190 1, /* size (0 = byte, 1 = short, 2 = long) */
2192 FALSE, /* pc_relative */
2194 complain_overflow_dont,/* complain_on_overflow */
2195 nds32_elf_ignore_reloc,/* special_function */
2196 "R_NDS32_DIFF16", /* name */
2197 FALSE, /* partial_inplace */
2198 0x0000ffff, /* src_mask */
2199 0x0000ffff, /* dst_mask */
2200 FALSE), /* pcrel_offset */
2201 HOWTO (R_NDS32_DIFF32, /* type */
2203 2, /* size (0 = byte, 1 = short, 2 = long) */
2205 FALSE, /* pc_relative */
2207 complain_overflow_dont,/* complain_on_overflow */
2208 nds32_elf_ignore_reloc,/* special_function */
2209 "R_NDS32_DIFF32", /* name */
2210 FALSE, /* partial_inplace */
2211 0xffffffff, /* src_mask */
2212 0xffffffff, /* dst_mask */
2213 FALSE), /* pcrel_offset */
2214 HOWTO (R_NDS32_DIFF_ULEB128, /* type */
2216 0, /* size (0 = byte, 1 = short, 2 = long) */
2218 FALSE, /* pc_relative */
2220 complain_overflow_dont,/* complain_on_overflow */
2221 nds32_elf_ignore_reloc,/* special_function */
2222 "R_NDS32_DIFF_ULEB128",/* name */
2223 FALSE, /* partial_inplace */
2224 0xffffffff, /* src_mask */
2225 0xffffffff, /* dst_mask */
2226 FALSE), /* pcrel_offset */
2227 HOWTO (R_NDS32_DATA, /* type */
2229 2, /* size (0 = byte, 1 = short, 2 = long) */
2231 FALSE, /* pc_relative */
2233 complain_overflow_dont,/* complain_on_overflow */
2234 nds32_elf_ignore_reloc,/* special_function */
2235 "R_NDS32_DATA", /* name */
2236 FALSE, /* partial_inplace */
2237 0xffffffff, /* src_mask */
2238 0xffffffff, /* dst_mask */
2239 FALSE), /* pcrel_offset */
2240 HOWTO (R_NDS32_TRAN, /* type */
2242 2, /* size (0 = byte, 1 = short, 2 = long) */
2244 FALSE, /* pc_relative */
2246 complain_overflow_dont,/* complain_on_overflow */
2247 nds32_elf_ignore_reloc,/* special_function */
2248 "R_NDS32_TRAN", /* name */
2249 FALSE, /* partial_inplace */
2250 0xffffffff, /* src_mask */
2251 0xffffffff, /* dst_mask */
2252 FALSE), /* pcrel_offset */
2253 HOWTO (R_NDS32_TLS_LE_ADD, /* type */
2255 2, /* size (0 = byte, 1 = short, 2 = long) */
2257 FALSE, /* pc_relative */
2259 complain_overflow_dont, /* complain_on_overflow */
2260 nds32_elf_ignore_reloc, /* special_function */
2261 "R_NDS32_TLS_LE_ADD", /* name */
2262 FALSE, /* partial_inplace */
2263 0xffffffff, /* src_mask */
2264 0xffffffff, /* dst_mask */
2265 FALSE), /* pcrel_offset */
2266 HOWTO (R_NDS32_TLS_LE_LS, /* type */
2268 2, /* size (0 = byte, 1 = short, 2 = long) */
2270 FALSE, /* pc_relative */
2272 complain_overflow_dont, /* complain_on_overflow */
2273 nds32_elf_ignore_reloc, /* special_function */
2274 "R_NDS32_TLS_LE_LS", /* name */
2275 FALSE, /* partial_inplace */
2276 0xffffffff, /* src_mask */
2277 0xffffffff, /* dst_mask */
2278 FALSE), /* pcrel_offset */
2279 HOWTO (R_NDS32_EMPTY, /* type */
2281 2, /* size (0 = byte, 1 = short, 2 = long) */
2283 FALSE, /* pc_relative */
2285 complain_overflow_dont, /* complain_on_overflow */
2286 nds32_elf_ignore_reloc, /* special_function */
2287 "R_NDS32_EMPTY", /* name */
2288 FALSE, /* partial_inplace */
2289 0xffffffff, /* src_mask */
2290 0xffffffff, /* dst_mask */
2291 FALSE), /* pcrel_offset */
2295 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2296 This prototype is the same as qsort (). */
2299 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2300 int (*compar) (const void *lhs, const void *rhs))
2302 char *ptr = (char *) base;
2304 char *tmp = xmalloc (size);
2306 /* If i is less than j, i is inserted before j.
2308 |---- j ----- i --------------|
2313 for (i = 1; i < (int) nmemb; i++)
2315 for (j = (i - 1); j >= 0; j--)
2316 if (compar (ptr + i * size, ptr + j * size) >= 0)
2322 continue; /* i is in order. */
2324 memcpy (tmp, ptr + i * size, size);
2325 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2326 memcpy (ptr + j * size, tmp, size);
2331 /* Sort relocation by r_offset.
2333 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2334 algorithm. Relocations at the same r_offset must keep their order.
2335 For example, RELAX_ENTRY must be the very first relocation entry.
2337 Currently, this function implements insertion-sort.
2339 FIXME: If we already sort them in assembler, why bother sort them
2343 compar_reloc (const void *lhs, const void *rhs)
2345 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2346 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2348 if (l->r_offset > r->r_offset)
2350 else if (l->r_offset == r->r_offset)
2356 /* Functions listed below are only used for old relocs.
2357 * nds32_elf_9_pcrel_reloc
2358 * nds32_elf_do_9_pcrel_reloc
2359 * nds32_elf_hi20_reloc
2360 * nds32_elf_relocate_hi20
2361 * nds32_elf_lo12_reloc
2362 * nds32_elf_sda15_reloc
2363 * nds32_elf_generic_reloc
2366 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2368 static bfd_reloc_status_type
2369 nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2370 void *data, asection *input_section, bfd *output_bfd,
2371 char **error_message ATTRIBUTE_UNUSED)
2373 /* This part is from bfd_elf_generic_reloc. */
2374 if (output_bfd != (bfd *) NULL
2375 && (symbol->flags & BSF_SECTION_SYM) == 0
2376 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2378 reloc_entry->address += input_section->output_offset;
2379 return bfd_reloc_ok;
2382 if (output_bfd != NULL)
2384 /* FIXME: See bfd_perform_relocation. Is this right? */
2385 return bfd_reloc_continue;
2388 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2390 data, reloc_entry->address,
2393 + symbol->section->output_section->vma
2394 + symbol->section->output_offset),
2395 reloc_entry->addend);
2398 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2399 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2401 static bfd_reloc_status_type
2402 nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
2403 asection *input_section, bfd_byte *data,
2405 asection *symbol_section ATTRIBUTE_UNUSED,
2406 bfd_vma symbol_value, bfd_vma addend)
2408 bfd_signed_vma relocation;
2410 bfd_reloc_status_type status;
2412 /* Sanity check the address (offset in section). */
2413 if (offset > bfd_get_section_limit (abfd, input_section))
2414 return bfd_reloc_outofrange;
2416 relocation = symbol_value + addend;
2417 /* Make it pc relative. */
2418 relocation -= (input_section->output_section->vma
2419 + input_section->output_offset);
2420 /* These jumps mask off the lower two bits of the current address
2421 before doing pcrel calculations. */
2422 relocation -= (offset & -(bfd_vma) 2);
2424 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2425 status = bfd_reloc_overflow;
2427 status = bfd_reloc_ok;
2429 x = bfd_getb16 (data + offset);
2431 relocation >>= howto->rightshift;
2432 relocation <<= howto->bitpos;
2433 x = (x & ~howto->dst_mask)
2434 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2436 bfd_putb16 ((bfd_vma) x, data + offset);
2441 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2442 HI20_SLO is for the add3 and load/store with displacement instructions.
2443 HI20 is for the or3 instruction.
2444 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2445 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2446 we must add one to the high 16 bytes (which will get subtracted off when
2447 the low 16 bits are added).
2448 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2449 because there is a carry from the LO12 to the HI20. Here we just save
2450 the information we need; we do the actual relocation when we see the LO12.
2451 This code is copied from the elf32-mips.c. We also support an arbitrary
2452 number of HI20 relocs to be associated with a single LO12 reloc. The
2453 assembler sorts the relocs to ensure each HI20 immediately precedes its
2454 LO12. However if there are multiple copies, the assembler may not find
2455 the real LO12 so it picks the first one it finds. */
2459 struct nds32_hi20 *next;
2464 static struct nds32_hi20 *nds32_hi20_list;
2466 static bfd_reloc_status_type
2467 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2468 asymbol *symbol, void *data, asection *input_section,
2469 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2471 bfd_reloc_status_type ret;
2473 struct nds32_hi20 *n;
2475 /* This part is from bfd_elf_generic_reloc.
2476 If we're relocating, and this an external symbol, we don't want
2477 to change anything. */
2478 if (output_bfd != (bfd *) NULL
2479 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2481 reloc_entry->address += input_section->output_offset;
2482 return bfd_reloc_ok;
2485 /* Sanity check the address (offset in section). */
2486 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2487 return bfd_reloc_outofrange;
2490 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2491 ret = bfd_reloc_undefined;
2493 if (bfd_is_com_section (symbol->section))
2496 relocation = symbol->value;
2498 relocation += symbol->section->output_section->vma;
2499 relocation += symbol->section->output_offset;
2500 relocation += reloc_entry->addend;
2502 /* Save the information, and let LO12 do the actual relocation. */
2503 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2505 return bfd_reloc_outofrange;
2507 n->addr = (bfd_byte *) data + reloc_entry->address;
2508 n->addend = relocation;
2509 n->next = nds32_hi20_list;
2510 nds32_hi20_list = n;
2512 if (output_bfd != (bfd *) NULL)
2513 reloc_entry->address += input_section->output_offset;
2518 /* Handle an NDS32 ELF HI20 reloc. */
2521 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2522 int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
2523 Elf_Internal_Rela *rello, bfd_byte *contents,
2529 insn = bfd_getb32 (contents + relhi->r_offset);
2531 addlo = bfd_getb32 (contents + rello->r_offset);
2534 addend += ((insn & 0xfffff) << 20) + addlo;
2536 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2537 bfd_putb32 (insn, contents + relhi->r_offset);
2540 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2541 inplace relocation; this function exists in order to do the
2542 R_NDS32_HI20_[SU]LO relocation described above. */
2544 static bfd_reloc_status_type
2545 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2546 void *data, asection *input_section, bfd *output_bfd,
2547 char **error_message)
2549 /* This part is from bfd_elf_generic_reloc.
2550 If we're relocating, and this an external symbol, we don't want
2551 to change anything. */
2552 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2553 && reloc_entry->addend == 0)
2555 reloc_entry->address += input_section->output_offset;
2556 return bfd_reloc_ok;
2559 if (nds32_hi20_list != NULL)
2561 struct nds32_hi20 *l;
2563 l = nds32_hi20_list;
2568 unsigned long vallo;
2569 struct nds32_hi20 *next;
2571 /* Do the HI20 relocation. Note that we actually don't need
2572 to know anything about the LO12 itself, except where to
2573 find the low 12 bits of the addend needed by the LO12. */
2574 insn = bfd_getb32 (l->addr);
2575 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2577 switch (reloc_entry->howto->type)
2579 case R_NDS32_LO12S3:
2583 case R_NDS32_LO12S2:
2587 case R_NDS32_LO12S1:
2591 case R_NDS32_LO12S0:
2596 val = ((insn & 0xfffff) << 12) + vallo;
2599 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2600 bfd_putb32 ((bfd_vma) insn, l->addr);
2607 nds32_hi20_list = NULL;
2610 /* Now do the LO12 reloc in the usual way.
2611 ??? It would be nice to call bfd_elf_generic_reloc here,
2612 but we have partial_inplace set. bfd_elf_generic_reloc will
2613 pass the handling back to bfd_install_relocation which will install
2614 a section relative addend which is wrong. */
2615 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2616 input_section, output_bfd, error_message);
2619 /* Do generic partial_inplace relocation.
2620 This is a local replacement for bfd_elf_generic_reloc. */
2622 static bfd_reloc_status_type
2623 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2624 asymbol *symbol, void *data, asection *input_section,
2625 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2627 bfd_reloc_status_type ret;
2629 bfd_byte *inplace_address;
2631 /* This part is from bfd_elf_generic_reloc.
2632 If we're relocating, and this an external symbol, we don't want
2633 to change anything. */
2634 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2635 && reloc_entry->addend == 0)
2637 reloc_entry->address += input_section->output_offset;
2638 return bfd_reloc_ok;
2641 /* Now do the reloc in the usual way.
2642 ??? It would be nice to call bfd_elf_generic_reloc here,
2643 but we have partial_inplace set. bfd_elf_generic_reloc will
2644 pass the handling back to bfd_install_relocation which will install
2645 a section relative addend which is wrong. */
2647 /* Sanity check the address (offset in section). */
2648 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2649 return bfd_reloc_outofrange;
2652 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2653 ret = bfd_reloc_undefined;
2655 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2658 relocation = symbol->value;
2660 /* Only do this for a final link. */
2661 if (output_bfd == (bfd *) NULL)
2663 relocation += symbol->section->output_section->vma;
2664 relocation += symbol->section->output_offset;
2667 relocation += reloc_entry->addend;
2668 switch (reloc_entry->howto->type)
2670 case R_NDS32_LO12S3:
2674 case R_NDS32_LO12S2:
2678 case R_NDS32_LO12S1:
2682 case R_NDS32_LO12S0:
2688 inplace_address = (bfd_byte *) data + reloc_entry->address;
2691 x = ((x & ~reloc_entry->howto->dst_mask) | \
2692 (((x & reloc_entry->howto->src_mask) + relocation) & \
2693 reloc_entry->howto->dst_mask))
2695 switch (reloc_entry->howto->size)
2699 short x = bfd_getb16 (inplace_address);
2702 bfd_putb16 ((bfd_vma) x, inplace_address);
2707 unsigned long x = bfd_getb32 (inplace_address);
2710 bfd_putb32 ((bfd_vma) x, inplace_address);
2717 if (output_bfd != (bfd *) NULL)
2718 reloc_entry->address += input_section->output_offset;
2723 /* Handle the R_NDS32_SDA15 reloc.
2724 This reloc is used to compute the address of objects in the small data area
2725 and to perform loads and stores from that area.
2726 The lower 15 bits are sign extended and added to the register specified
2727 in the instruction, which is assumed to point to _SDA_BASE_.
2729 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2730 the access size, this must be taken care of. */
2732 static bfd_reloc_status_type
2733 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2734 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2735 asection *input_section, bfd *output_bfd,
2736 char **error_message ATTRIBUTE_UNUSED)
2738 /* This part is from bfd_elf_generic_reloc. */
2739 if (output_bfd != (bfd *) NULL
2740 && (symbol->flags & BSF_SECTION_SYM) == 0
2741 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2743 reloc_entry->address += input_section->output_offset;
2744 return bfd_reloc_ok;
2747 if (output_bfd != NULL)
2749 /* FIXME: See bfd_perform_relocation. Is this right? */
2750 return bfd_reloc_continue;
2753 /* FIXME: not sure what to do here yet. But then again, the linker
2754 may never call us. */
2758 /* nds32_elf_ignore_reloc is the special function for
2759 relocation types which don't need to be relocated
2760 like relaxation relocation types.
2761 This function simply return bfd_reloc_ok when it is
2764 static bfd_reloc_status_type
2765 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2766 asymbol *symbol ATTRIBUTE_UNUSED,
2767 void *data ATTRIBUTE_UNUSED, asection *input_section,
2768 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2770 if (output_bfd != NULL)
2771 reloc_entry->address += input_section->output_offset;
2773 return bfd_reloc_ok;
2777 /* Map BFD reloc types to NDS32 ELF reloc types. */
2779 struct nds32_reloc_map_entry
2781 bfd_reloc_code_real_type bfd_reloc_val;
2782 unsigned char elf_reloc_val;
2785 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
2787 {BFD_RELOC_NONE, R_NDS32_NONE},
2788 {BFD_RELOC_16, R_NDS32_16_RELA},
2789 {BFD_RELOC_32, R_NDS32_32_RELA},
2790 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
2791 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
2792 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
2793 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
2794 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
2795 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
2796 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
2797 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
2798 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
2799 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
2800 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
2801 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
2802 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
2803 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
2804 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
2805 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
2806 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
2807 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
2808 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
2809 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
2811 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
2812 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
2813 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
2814 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
2815 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
2816 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
2817 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
2818 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
2819 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
2820 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
2821 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
2822 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
2823 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
2824 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
2825 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
2826 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
2827 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
2828 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
2829 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
2830 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
2831 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
2832 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
2833 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
2834 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
2835 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
2836 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
2837 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
2838 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
2839 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
2840 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
2841 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
2842 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
2843 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
2844 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
2845 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
2846 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
2847 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
2848 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
2849 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
2850 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
2851 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
2852 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
2853 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
2854 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
2855 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
2856 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
2857 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
2858 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
2859 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
2860 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
2861 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
2862 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
2863 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
2864 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
2865 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
2866 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
2867 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
2868 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
2869 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
2870 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
2871 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
2872 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
2873 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
2874 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
2875 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
2876 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
2877 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
2878 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
2879 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
2880 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
2881 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
2882 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
2883 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
2884 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
2885 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
2887 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
2888 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
2889 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
2890 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
2891 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
2892 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
2893 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
2894 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
2895 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
2896 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
2897 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
2898 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
2899 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
2900 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
2901 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
2902 {BFD_RELOC_NDS32_TLS_TPOFF, R_NDS32_TLS_TPOFF},
2903 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
2904 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
2905 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
2906 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
2911 static reloc_howto_type *
2912 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2917 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
2918 if (nds32_elf_howto_table[i].name != NULL
2919 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
2920 return &nds32_elf_howto_table[i];
2922 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
2923 if (nds32_elf_relax_howto_table[i].name != NULL
2924 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
2925 return &nds32_elf_relax_howto_table[i];
2930 static reloc_howto_type *
2931 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
2933 if (code < R_NDS32_RELAX_ENTRY)
2935 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
2936 return &nds32_elf_howto_table[code];
2940 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
2941 < ARRAY_SIZE (nds32_elf_relax_howto_table));
2942 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
2946 static reloc_howto_type *
2947 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2948 bfd_reloc_code_real_type code)
2952 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
2954 if (nds32_reloc_map[i].bfd_reloc_val == code)
2955 return bfd_elf32_bfd_reloc_type_table_lookup
2956 (nds32_reloc_map[i].elf_reloc_val);
2962 /* Set the howto pointer for an NDS32 ELF reloc. */
2965 nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2966 Elf_Internal_Rela *dst)
2968 enum elf_nds32_reloc_type r_type;
2970 r_type = ELF32_R_TYPE (dst->r_info);
2971 if (r_type > R_NDS32_GNU_VTENTRY)
2973 /* xgettext:c-format */
2974 _bfd_error_handler (_("%B: invalid NDS32 reloc number: %d"), abfd, r_type);
2977 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
2981 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2982 Elf_Internal_Rela *dst)
2984 BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
2985 || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
2986 && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
2987 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
2990 /* Support for core dump NOTE sections.
2991 Reference to include/linux/elfcore.h in Linux. */
2994 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2999 switch (note->descsz)
3002 /* Linux/NDS32 32-bit, ABI1 */
3005 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3008 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3016 /* Linux/NDS32 32-bit */
3019 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3022 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3033 /* Make a ".reg" section. */
3034 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3035 size, note->descpos + offset);
3039 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3041 switch (note->descsz)
3046 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3047 elf_tdata (abfd)->core->program =
3048 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3049 elf_tdata (abfd)->core->command =
3050 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3057 /* Note that for some reason, a spurious space is tacked
3058 onto the end of the args in some (at least one anyway)
3059 implementations, so strip it off if it exists. */
3061 char *command = elf_tdata (abfd)->core->command;
3062 int n = strlen (command);
3064 if (0 < n && command[n - 1] == ' ')
3065 command[n - 1] = '\0';
3071 /* Hook called by the linker routine which adds symbols from an object
3072 file. We must handle the special NDS32 section numbers here.
3073 We also keep watching for whether we need to create the sdata special
3077 nds32_elf_add_symbol_hook (bfd *abfd,
3078 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3079 Elf_Internal_Sym *sym,
3080 const char **namep ATTRIBUTE_UNUSED,
3081 flagword *flagsp ATTRIBUTE_UNUSED,
3082 asection **secp, bfd_vma *valp)
3084 switch (sym->st_shndx)
3087 /* Common symbols less than the GP size are automatically
3088 treated as SHN_MIPS_SCOMMON symbols. */
3089 if (sym->st_size > elf_gp_size (abfd)
3090 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3093 /* st_value is the alignemnt constraint.
3094 That might be its actual size if it is an array or structure. */
3095 switch (sym->st_value)
3098 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3101 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3104 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3107 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3113 (*secp)->flags |= SEC_IS_COMMON;
3114 *valp = sym->st_size;
3122 /* This function can figure out the best location for a base register to access
3123 data relative to this base register
3125 sda_d0: size of first DOUBLE WORD data section
3126 sda_w0: size of first WORD data section
3127 sda_h0: size of first HALF WORD data section
3128 sda_b : size of BYTE data section
3129 sda_hi: size of second HALF WORD data section
3130 sda_w1: size of second WORD data section
3131 sda_d1: size of second DOUBLE WORD data section
3133 offset (always positive) from the beginning of sda_d0 if OK
3134 a negative error value if fail
3136 these 7 sections have to be located back to back if exist
3137 a pass in 0 value for non-existing section */
3139 /* Due to the interpretation of simm15 field of load/store depending on
3140 data accessing size, the organization of base register relative data shall
3141 like the following figure
3142 -------------------------------------------
3143 | DOUBLE WORD sized data (range +/- 128K)
3144 -------------------------------------------
3145 | WORD sized data (range +/- 64K)
3146 -------------------------------------------
3147 | HALF WORD sized data (range +/- 32K)
3148 -------------------------------------------
3149 | BYTE sized data (range +/- 16K)
3150 -------------------------------------------
3151 | HALF WORD sized data (range +/- 32K)
3152 -------------------------------------------
3153 | WORD sized data (range +/- 64K)
3154 -------------------------------------------
3155 | DOUBLE WORD sized data (range +/- 128K)
3156 -------------------------------------------
3157 Its base register shall be set to access these data freely. */
3159 /* We have to figure out the SDA_BASE value, so that we can adjust the
3160 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3161 BFD. If we can't find it, we're stuck. We cache it in the ELF
3162 target data. We don't need to adjust the symbol value for an
3163 external symbol if we are producing relocatable output. */
3165 static asection *sda_rela_sec = NULL;
3167 #define SDA_SECTION_NUM 10
3169 static bfd_reloc_status_type
3170 nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
3171 bfd_vma *psb, bfd_boolean add_symbol)
3174 struct elf_nds32_link_hash_table *table;
3175 struct bfd_link_hash_entry *h, *h2;
3176 long unsigned int total = 0;
3178 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3179 if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
3181 asection *first = NULL, *final = NULL, *temp;
3183 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3184 4 byte-aligned. Therefore, it has to set the first section ".data"
3186 static const char sec_name[SDA_SECTION_NUM][10] =
3188 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3189 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3193 if (output_bfd->sections == NULL)
3195 *psb = elf_gp (output_bfd);
3196 return bfd_reloc_ok;
3199 /* Get the first and final section. */
3200 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3202 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3203 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3205 if (temp && (temp->size != 0 || temp->rawsize != 0))
3208 /* Summarize the sections in order to check if joining .bss. */
3209 if (temp && temp->size != 0)
3210 total += temp->size;
3211 else if (temp && temp->rawsize != 0)
3212 total += temp->rawsize;
3217 /* Check .bss size. */
3218 temp = bfd_get_section_by_name (output_bfd, ".bss");
3221 if (temp->size != 0)
3222 total += temp->size;
3223 else if (temp->rawsize != 0)
3224 total += temp->rawsize;
3226 if (total < 0x80000)
3228 if (!first && (temp->size != 0 || temp->rawsize != 0))
3230 if ((temp->size != 0 || temp->rawsize != 0))
3237 /* The middle of data region. */
3238 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3240 /* Find the section sda_base located. */
3242 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3244 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3245 if (final && (final->size != 0 || final->rawsize != 0)
3246 && sda_base >= final->vma)
3257 /* There is not any data section in output bfd, and set _SDA_BASE_ in
3258 first output section. */
3259 first = output_bfd->sections;
3260 while (first && first->size == 0 && first->rawsize == 0)
3261 first = first->next;
3264 *psb = elf_gp (output_bfd);
3265 return bfd_reloc_ok;
3267 sda_base = first->vma + first->rawsize;
3270 sda_base -= first->vma;
3271 sda_base = sda_base & (~7);
3273 if (!_bfd_generic_link_add_one_symbol
3274 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3275 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3276 get_elf_backend_data (output_bfd)->collect, &h))
3279 sda_rela_sec = first;
3281 table = nds32_elf_hash_table (info);
3282 relax_fp_as_gp = table->relax_fp_as_gp;
3285 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
3286 FALSE, FALSE, FALSE);
3287 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3288 And set FP equal to SDA_BASE to do relaxation for
3289 la $fp, _FP_BASE_. */
3290 if (!_bfd_generic_link_add_one_symbol
3291 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3292 first, (bfd_vma) sda_base, (const char *) NULL,
3293 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3298 if (add_symbol == TRUE)
3303 elf_gp (output_bfd) = (h->u.def.value
3304 + h->u.def.section->output_section->vma
3305 + h->u.def.section->output_offset);
3309 _bfd_error_handler (_("error: Can't find symbol: _SDA_BASE_."));
3310 return bfd_reloc_dangerous;
3314 *psb = h->u.def.value + h->u.def.section->output_section->vma
3315 + h->u.def.section->output_offset;
3316 return bfd_reloc_ok;
3320 /* Return size of a PLT entry. */
3321 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3324 /* Create an entry in an nds32 ELF linker hash table. */
3326 static struct bfd_hash_entry *
3327 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3328 struct bfd_hash_table *table,
3331 struct elf_nds32_link_hash_entry *ret;
3333 ret = (struct elf_nds32_link_hash_entry *) entry;
3335 /* Allocate the structure if it has not already been allocated by a
3338 ret = (struct elf_nds32_link_hash_entry *)
3339 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3342 return (struct bfd_hash_entry *) ret;
3344 /* Call the allocation method of the superclass. */
3345 ret = (struct elf_nds32_link_hash_entry *)
3346 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3350 struct elf_nds32_link_hash_entry *eh;
3352 eh = (struct elf_nds32_link_hash_entry *) ret;
3353 eh->dyn_relocs = NULL;
3354 eh->tls_type = GOT_UNKNOWN;
3357 return (struct bfd_hash_entry *) ret;
3360 /* Create an nds32 ELF linker hash table. */
3362 static struct bfd_link_hash_table *
3363 nds32_elf_link_hash_table_create (bfd *abfd)
3365 struct elf_nds32_link_hash_table *ret;
3367 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3369 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3374 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3375 nds32_elf_link_hash_newfunc,
3376 sizeof (struct elf_nds32_link_hash_entry),
3383 return &ret->root.root;
3386 /* Create dynamic sections when linking against a dynamic object. */
3389 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3391 struct elf_nds32_link_hash_table *htab;
3392 flagword flags, pltflags;
3393 register asection *s;
3394 const struct elf_backend_data *bed;
3395 int ptralign = 2; /* 32-bit */
3397 bed = get_elf_backend_data (abfd);
3399 htab = nds32_elf_hash_table (info);
3401 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3402 .rel[a].bss sections. */
3404 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3405 | SEC_LINKER_CREATED);
3408 pltflags |= SEC_CODE;
3409 if (bed->plt_not_loaded)
3410 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3411 if (bed->plt_readonly)
3412 pltflags |= SEC_READONLY;
3414 s = bfd_make_section (abfd, ".plt");
3415 htab->root.splt = s;
3417 || !bfd_set_section_flags (abfd, s, pltflags)
3418 || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3421 if (bed->want_plt_sym)
3423 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3425 struct bfd_link_hash_entry *bh = NULL;
3426 struct elf_link_hash_entry *h;
3428 if (!(_bfd_generic_link_add_one_symbol
3429 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3430 (bfd_vma) 0, (const char *) NULL, FALSE,
3431 get_elf_backend_data (abfd)->collect, &bh)))
3434 h = (struct elf_link_hash_entry *) bh;
3436 h->type = STT_OBJECT;
3438 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3442 s = bfd_make_section (abfd,
3443 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3444 htab->root.srelplt = s;
3446 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3447 || !bfd_set_section_alignment (abfd, s, ptralign))
3450 if (htab->root.sgot == NULL && !_bfd_elf_create_got_section (abfd, info))
3454 const char *secname;
3459 for (sec = abfd->sections; sec; sec = sec->next)
3461 secflags = bfd_get_section_flags (abfd, sec);
3462 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3463 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3465 secname = bfd_get_section_name (abfd, sec);
3466 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3467 strcpy (relname, ".rela");
3468 strcat (relname, secname);
3469 if (bfd_get_section_by_name (abfd, secname))
3471 s = bfd_make_section (abfd, relname);
3473 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3474 || !bfd_set_section_alignment (abfd, s, ptralign))
3479 if (bed->want_dynbss)
3481 /* The .dynbss section is a place to put symbols which are defined
3482 by dynamic objects, are referenced by regular objects, and are
3483 not functions. We must allocate space for them in the process
3484 image and use a R_*_COPY reloc to tell the dynamic linker to
3485 initialize them at run time. The linker script puts the .dynbss
3486 section into the .bss section of the final image. */
3487 s = bfd_make_section (abfd, ".dynbss");
3490 || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3492 /* The .rel[a].bss section holds copy relocs. This section is not
3493 normally needed. We need to create it here, though, so that the
3494 linker will map it to an output section. We can't just create it
3495 only if we need it, because we will not know whether we need it
3496 until we have seen all the input files, and the first time the
3497 main linker code calls BFD after examining all the input files
3498 (size_dynamic_sections) the input sections have already been
3499 mapped to the output sections. If the section turns out not to
3500 be needed, we can discard it later. We will never need this
3501 section when generating a shared object, since they do not use
3503 if (!bfd_link_pic (info))
3505 s = bfd_make_section (abfd, (bed->default_use_rela_p
3506 ? ".rela.bss" : ".rel.bss"));
3509 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3510 || !bfd_set_section_alignment (abfd, s, ptralign))
3518 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3520 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3521 struct elf_link_hash_entry *dir,
3522 struct elf_link_hash_entry *ind)
3524 struct elf_nds32_link_hash_entry *edir, *eind;
3526 edir = (struct elf_nds32_link_hash_entry *) dir;
3527 eind = (struct elf_nds32_link_hash_entry *) ind;
3529 if (eind->dyn_relocs != NULL)
3531 if (edir->dyn_relocs != NULL)
3533 struct elf_nds32_dyn_relocs **pp;
3534 struct elf_nds32_dyn_relocs *p;
3536 if (ind->root.type == bfd_link_hash_indirect)
3539 /* Add reloc counts against the weak sym to the strong sym
3540 list. Merge any entries against the same section. */
3541 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3543 struct elf_nds32_dyn_relocs *q;
3545 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3546 if (q->sec == p->sec)
3548 q->pc_count += p->pc_count;
3549 q->count += p->count;
3556 *pp = edir->dyn_relocs;
3559 edir->dyn_relocs = eind->dyn_relocs;
3560 eind->dyn_relocs = NULL;
3563 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3567 /* Adjust a symbol defined by a dynamic object and referenced by a
3568 regular object. The current definition is in some section of the
3569 dynamic object, but we're not including those sections. We have to
3570 change the definition to something the rest of the link can
3574 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3575 struct elf_link_hash_entry *h)
3577 struct elf_nds32_link_hash_table *htab;
3578 struct elf_nds32_link_hash_entry *eh;
3579 struct elf_nds32_dyn_relocs *p;
3582 unsigned int power_of_two;
3584 dynobj = elf_hash_table (info)->dynobj;
3586 /* Make sure we know what is going on here. */
3587 BFD_ASSERT (dynobj != NULL
3589 || h->u.weakdef != NULL
3590 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3593 /* If this is a function, put it in the procedure linkage table. We
3594 will fill in the contents of the procedure linkage table later,
3595 when we know the address of the .got section. */
3596 if (h->type == STT_FUNC || h->needs_plt)
3598 if (!bfd_link_pic (info)
3601 && h->root.type != bfd_link_hash_undefweak
3602 && h->root.type != bfd_link_hash_undefined)
3604 /* This case can occur if we saw a PLT reloc in an input
3605 file, but the symbol was never referred to by a dynamic
3606 object. In such a case, we don't actually need to build
3607 a procedure linkage table, and we can just do a PCREL
3609 h->plt.offset = (bfd_vma) - 1;
3616 h->plt.offset = (bfd_vma) - 1;
3618 /* If this is a weak symbol, and there is a real definition, the
3619 processor independent code will have arranged for us to see the
3620 real definition first, and we can just use the same value. */
3621 if (h->u.weakdef != NULL)
3623 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3624 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3625 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3626 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3630 /* This is a reference to a symbol defined by a dynamic object which
3631 is not a function. */
3633 /* If we are creating a shared library, we must presume that the
3634 only references to the symbol are via the global offset table.
3635 For such cases we need not do anything here; the relocations will
3636 be handled correctly by relocate_section. */
3637 if (bfd_link_pic (info))
3640 /* If there are no references to this symbol that do not use the
3641 GOT, we don't need to generate a copy reloc. */
3642 if (!h->non_got_ref)
3645 /* If -z nocopyreloc was given, we won't generate them either. */
3646 if (info->nocopyreloc)
3652 eh = (struct elf_nds32_link_hash_entry *) h;
3653 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3655 s = p->sec->output_section;
3656 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3660 /* If we didn't find any dynamic relocs in sections which needs the
3661 copy reloc, then we'll be keeping the dynamic relocs and avoiding
3669 /* We must allocate the symbol in our .dynbss section, which will
3670 become part of the .bss section of the executable. There will be
3671 an entry for this symbol in the .dynsym section. The dynamic
3672 object will contain position independent code, so all references
3673 from the dynamic object to this symbol will go through the global
3674 offset table. The dynamic linker will use the .dynsym entry to
3675 determine the address it must put in the global offset table, so
3676 both the dynamic object and the regular object will refer to the
3677 same memory location for the variable. */
3679 htab = nds32_elf_hash_table (info);
3681 BFD_ASSERT (s != NULL);
3683 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3684 to copy the initial value out of the dynamic object and into the
3685 runtime process image. We need to remember the offset into the
3686 .rela.bss section we are going to use. */
3687 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3691 srel = htab->srelbss;
3692 BFD_ASSERT (srel != NULL);
3693 srel->size += sizeof (Elf32_External_Rela);
3697 /* We need to figure out the alignment required for this symbol. I
3698 have no idea how ELF linkers handle this. */
3699 power_of_two = bfd_log2 (h->size);
3700 if (power_of_two > 3)
3703 /* Apply the required alignment. */
3704 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3705 if (power_of_two > bfd_get_section_alignment (dynobj, s))
3707 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
3711 /* Define the symbol as being at this point in the section. */
3712 h->root.u.def.section = s;
3713 h->root.u.def.value = s->size;
3715 /* Increment the section size to make room for the symbol. */
3721 /* Allocate space in .plt, .got and associated reloc sections for
3725 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3727 struct bfd_link_info *info;
3728 struct elf_nds32_link_hash_table *htab;
3729 struct elf_nds32_link_hash_entry *eh;
3730 struct elf_nds32_dyn_relocs *p;
3732 if (h->root.type == bfd_link_hash_indirect)
3735 if (h->root.type == bfd_link_hash_warning)
3736 /* When warning symbols are created, they **replace** the "real"
3737 entry in the hash table, thus we never get to see the real
3738 symbol in a hash traversal. So look at it now. */
3739 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3741 info = (struct bfd_link_info *) inf;
3742 htab = nds32_elf_hash_table (info);
3744 eh = (struct elf_nds32_link_hash_entry *) h;
3746 if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
3748 /* Make sure this symbol is output as a dynamic symbol.
3749 Undefined weak syms won't yet be marked as dynamic. */
3750 if (h->dynindx == -1 && !h->forced_local)
3752 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3756 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
3758 asection *s = htab->root.splt;
3760 /* If this is the first .plt entry, make room for the special
3763 s->size += PLT_ENTRY_SIZE;
3765 h->plt.offset = s->size;
3767 /* If this symbol is not defined in a regular file, and we are
3768 not generating a shared library, then set the symbol to this
3769 location in the .plt. This is required to make function
3770 pointers compare as equal between the normal executable and
3771 the shared library. */
3772 if (!bfd_link_pic (info) && !h->def_regular)
3774 h->root.u.def.section = s;
3775 h->root.u.def.value = h->plt.offset;
3778 /* Make room for this entry. */
3779 s->size += PLT_ENTRY_SIZE;
3781 /* We also need to make an entry in the .got.plt section, which
3782 will be placed in the .got section by the linker script. */
3783 htab->root.sgotplt->size += 4;
3785 /* We also need to make an entry in the .rel.plt section. */
3786 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3790 h->plt.offset = (bfd_vma) - 1;
3796 h->plt.offset = (bfd_vma) - 1;
3800 if (h->got.refcount > 0)
3804 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
3806 /* Make sure this symbol is output as a dynamic symbol.
3807 Undefined weak syms won't yet be marked as dynamic. */
3808 if (h->dynindx == -1 && !h->forced_local)
3810 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3814 s = htab->root.sgot;
3815 h->got.offset = s->size;
3817 if (tls_type == GOT_UNKNOWN)
3819 else if (tls_type == GOT_NORMAL
3820 || tls_type == GOT_TLS_IE)
3821 /* Need a GOT slot. */
3824 dyn = htab->root.dynamic_sections_created;
3825 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
3826 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3829 h->got.offset = (bfd_vma) - 1;
3831 if (eh->dyn_relocs == NULL)
3834 /* In the shared -Bsymbolic case, discard space allocated for
3835 dynamic pc-relative relocs against symbols which turn out to be
3836 defined in regular objects. For the normal shared case, discard
3837 space for pc-relative relocs that have become local due to symbol
3838 visibility changes. */
3840 if (bfd_link_pic (info))
3842 if (h->def_regular && (h->forced_local || info->symbolic))
3844 struct elf_nds32_dyn_relocs **pp;
3846 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
3848 p->count -= p->pc_count;
3859 /* For the non-shared case, discard space for relocs against
3860 symbols which turn out to need copy relocs or are not dynamic. */
3865 || (htab->root.dynamic_sections_created
3866 && (h->root.type == bfd_link_hash_undefweak
3867 || h->root.type == bfd_link_hash_undefined))))
3869 /* Make sure this symbol is output as a dynamic symbol.
3870 Undefined weak syms won't yet be marked as dynamic. */
3871 if (h->dynindx == -1 && !h->forced_local)
3873 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3877 /* If that succeeded, we know we'll be keeping all the
3879 if (h->dynindx != -1)
3883 eh->dyn_relocs = NULL;
3888 /* Finally, allocate space. */
3889 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3891 asection *sreloc = elf_section_data (p->sec)->sreloc;
3892 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3898 /* Find any dynamic relocs that apply to read-only sections. */
3901 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3903 struct elf_nds32_link_hash_entry *eh;
3904 struct elf_nds32_dyn_relocs *p;
3906 if (h->root.type == bfd_link_hash_warning)
3907 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3909 eh = (struct elf_nds32_link_hash_entry *) h;
3910 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3912 asection *s = p->sec->output_section;
3914 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3916 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3918 info->flags |= DF_TEXTREL;
3920 /* Not an error, just cut short the traversal. */
3927 /* Set the sizes of the dynamic sections. */
3930 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3931 struct bfd_link_info *info)
3933 struct elf_nds32_link_hash_table *htab;
3939 htab = nds32_elf_hash_table (info);
3940 dynobj = htab->root.dynobj;
3941 BFD_ASSERT (dynobj != NULL);
3943 if (htab->root.dynamic_sections_created)
3945 /* Set the contents of the .interp section to the interpreter. */
3946 if (!bfd_link_pic (info) && !info->nointerp)
3948 s = bfd_get_section_by_name (dynobj, ".interp");
3949 BFD_ASSERT (s != NULL);
3950 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3951 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3955 /* Set up .got offsets for local syms, and space for local dynamic
3957 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3959 bfd_signed_vma *local_got;
3960 bfd_signed_vma *end_local_got;
3961 bfd_size_type locsymcount;
3962 Elf_Internal_Shdr *symtab_hdr;
3965 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3968 for (s = ibfd->sections; s != NULL; s = s->next)
3970 struct elf_nds32_dyn_relocs *p;
3972 for (p = ((struct elf_nds32_dyn_relocs *)
3973 elf_section_data (s)->local_dynrel);
3974 p != NULL; p = p->next)
3976 if (!bfd_is_abs_section (p->sec)
3977 && bfd_is_abs_section (p->sec->output_section))
3979 /* Input section has been discarded, either because
3980 it is a copy of a linkonce section or due to
3981 linker script /DISCARD/, so we'll be discarding
3984 else if (p->count != 0)
3986 srel = elf_section_data (p->sec)->sreloc;
3987 srel->size += p->count * sizeof (Elf32_External_Rela);
3988 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3989 info->flags |= DF_TEXTREL;
3994 local_got = elf_local_got_refcounts (ibfd);
3998 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3999 locsymcount = symtab_hdr->sh_info;
4000 end_local_got = local_got + locsymcount;
4001 s = htab->root.sgot;
4002 srel = htab->root.srelgot;
4003 for (; local_got < end_local_got; ++local_got)
4007 *local_got = s->size;
4009 if (bfd_link_pic (info))
4010 srel->size += sizeof (Elf32_External_Rela);
4013 *local_got = (bfd_vma) - 1;
4017 /* Allocate global sym .plt and .got entries, and space for global
4018 sym dynamic relocs. */
4019 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4021 /* We now have determined the sizes of the various dynamic sections.
4022 Allocate memory for them. */
4024 for (s = dynobj->sections; s != NULL; s = s->next)
4026 if ((s->flags & SEC_LINKER_CREATED) == 0)
4029 if (s == htab->root.splt)
4031 /* Strip this section if we don't need it; see the
4034 else if (s == htab->root.sgot)
4036 got_size += s->size;
4038 else if (s == htab->root.sgotplt)
4040 got_size += s->size;
4042 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4044 if (s->size != 0 && s != htab->root.srelplt)
4047 /* We use the reloc_count field as a counter if we need
4048 to copy relocs into the output file. */
4053 /* It's not one of our sections, so don't allocate space. */
4059 /* If we don't need this section, strip it from the
4060 output file. This is mostly to handle .rela.bss and
4061 .rela.plt. We must create both sections in
4062 create_dynamic_sections, because they must be created
4063 before the linker maps input sections to output
4064 sections. The linker does that before
4065 adjust_dynamic_symbol is called, and it is that
4066 function which decides whether anything needs to go
4067 into these sections. */
4068 s->flags |= SEC_EXCLUDE;
4072 /* Allocate memory for the section contents. We use bfd_zalloc
4073 here in case unused entries are not reclaimed before the
4074 section's contents are written out. This should not happen,
4075 but this way if it does, we get a R_NDS32_NONE reloc instead
4077 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4078 if (s->contents == NULL)
4083 if (htab->root.dynamic_sections_created)
4085 /* Add some entries to the .dynamic section. We fill in the
4086 values later, in nds32_elf_finish_dynamic_sections, but we
4087 must add the entries now so that we get the correct size for
4088 the .dynamic section. The DT_DEBUG entry is filled in by the
4089 dynamic linker and used by the debugger. */
4090 #define add_dynamic_entry(TAG, VAL) \
4091 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4093 if (!bfd_link_pic (info))
4095 if (!add_dynamic_entry (DT_DEBUG, 0))
4099 if (htab->root.splt->size != 0)
4101 if (!add_dynamic_entry (DT_PLTGOT, 0)
4102 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4103 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4104 || !add_dynamic_entry (DT_JMPREL, 0))
4110 if (!add_dynamic_entry (DT_RELA, 0)
4111 || !add_dynamic_entry (DT_RELASZ, 0)
4112 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4115 /* If any dynamic relocs apply to a read-only section,
4116 then we need a DT_TEXTREL entry. */
4117 if ((info->flags & DF_TEXTREL) == 0)
4118 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
4121 if ((info->flags & DF_TEXTREL) != 0)
4123 if (!add_dynamic_entry (DT_TEXTREL, 0))
4128 #undef add_dynamic_entry
4133 static bfd_reloc_status_type
4134 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4135 bfd_vma relocation, bfd_byte *location)
4139 bfd_reloc_status_type flag;
4140 unsigned int rightshift = howto->rightshift;
4141 unsigned int bitpos = howto->bitpos;
4143 /* If the size is negative, negate RELOCATION. This isn't very
4145 if (howto->size < 0)
4146 relocation = -relocation;
4148 /* Get the value we are going to relocate. */
4149 size = bfd_get_reloc_size (howto);
4156 return bfd_reloc_ok;
4158 x = bfd_getb16 (location);
4161 x = bfd_getb32 (location);
4165 /* Check for overflow. FIXME: We may drop bits during the addition
4166 which we don't check for. We must either check at every single
4167 operation, which would be tedious, or we must do the computations
4168 in a type larger than bfd_vma, which would be inefficient. */
4169 flag = bfd_reloc_ok;
4170 if (howto->complain_on_overflow != complain_overflow_dont)
4172 bfd_vma addrmask, fieldmask, signmask, ss;
4175 /* Get the values to be added together. For signed and unsigned
4176 relocations, we assume that all values should be truncated to
4177 the size of an address. For bitfields, all the bits matter.
4178 See also bfd_check_overflow. */
4179 fieldmask = N_ONES (howto->bitsize);
4180 signmask = ~fieldmask;
4181 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4182 a = (relocation & addrmask) >> rightshift;
4183 b = (x & howto->src_mask & addrmask) >> bitpos;
4185 switch (howto->complain_on_overflow)
4187 case complain_overflow_signed:
4188 /* If any sign bits are set, all sign bits must be set.
4189 That is, A must be a valid negative address after
4191 signmask = ~(fieldmask >> 1);
4194 case complain_overflow_bitfield:
4195 /* Much like the signed check, but for a field one bit
4196 wider. We allow a bitfield to represent numbers in the
4197 range -2**n to 2**n-1, where n is the number of bits in the
4198 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4199 can't overflow, which is exactly what we want. */
4201 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4202 flag = bfd_reloc_overflow;
4204 /* We only need this next bit of code if the sign bit of B
4205 is below the sign bit of A. This would only happen if
4206 SRC_MASK had fewer bits than BITSIZE. Note that if
4207 SRC_MASK has more bits than BITSIZE, we can get into
4208 trouble; we would need to verify that B is in range, as
4209 we do for A above. */
4210 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4213 /* Set all the bits above the sign bit. */
4216 /* Now we can do the addition. */
4219 /* See if the result has the correct sign. Bits above the
4220 sign bit are junk now; ignore them. If the sum is
4221 positive, make sure we did not have all negative inputs;
4222 if the sum is negative, make sure we did not have all
4223 positive inputs. The test below looks only at the sign
4224 bits, and it really just
4225 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4227 We mask with addrmask here to explicitly allow an address
4228 wrap-around. The Linux kernel relies on it, and it is
4229 the only way to write assembler code which can run when
4230 loaded at a location 0x80000000 away from the location at
4231 which it is linked. */
4232 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4233 flag = bfd_reloc_overflow;
4237 case complain_overflow_unsigned:
4238 /* Checking for an unsigned overflow is relatively easy:
4239 trim the addresses and add, and trim the result as well.
4240 Overflow is normally indicated when the result does not
4241 fit in the field. However, we also need to consider the
4242 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4243 input is 0x80000000, and bfd_vma is only 32 bits; then we
4244 will get sum == 0, but there is an overflow, since the
4245 inputs did not fit in the field. Instead of doing a
4246 separate test, we can check for this by or-ing in the
4247 operands when testing for the sum overflowing its final
4249 sum = (a + b) & addrmask;
4250 if ((a | b | sum) & signmask)
4251 flag = bfd_reloc_overflow;
4259 /* Put RELOCATION in the right bits. */
4260 relocation >>= (bfd_vma) rightshift;
4261 relocation <<= (bfd_vma) bitpos;
4263 /* Add RELOCATION to the right bits of X. */
4265 Because the relaxation may generate duplicate relocation at one address,
4266 an addition to immediate in the instruction may cause the relocation added
4268 This bug should be fixed in assembler, but a check is also needed here. */
4269 if (howto->partial_inplace)
4270 x = ((x & ~howto->dst_mask)
4271 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4273 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4276 /* Put the relocated value back in the object file. */
4286 bfd_putb16 (x, location);
4289 bfd_putb32 (x, location);
4296 static bfd_reloc_status_type
4297 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4298 asection *input_section, bfd_byte *contents,
4299 bfd_vma address, bfd_vma value, bfd_vma addend)
4303 /* Sanity check the address. */
4304 if (address > bfd_get_section_limit (input_bfd, input_section))
4305 return bfd_reloc_outofrange;
4307 /* This function assumes that we are dealing with a basic relocation
4308 against a symbol. We want to compute the value of the symbol to
4309 relocate to. This is just VALUE, the value of the symbol, plus
4310 ADDEND, any addend associated with the reloc. */
4311 relocation = value + addend;
4313 /* If the relocation is PC relative, we want to set RELOCATION to
4314 the distance between the symbol (currently in RELOCATION) and the
4315 location we are relocating. Some targets (e.g., i386-aout)
4316 arrange for the contents of the section to be the negative of the
4317 offset of the location within the section; for such targets
4318 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4319 simply leave the contents of the section as zero; for such
4320 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4321 need to subtract out the offset of the location within the
4322 section (which is just ADDRESS). */
4323 if (howto->pc_relative)
4325 relocation -= (input_section->output_section->vma
4326 + input_section->output_offset);
4327 if (howto->pcrel_offset)
4328 relocation -= address;
4331 return nds32_relocate_contents (howto, input_bfd, relocation,
4332 contents + address);
4336 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4338 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4339 asection *input_sec,
4340 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4343 FILE *sym_ld_script = NULL;
4344 struct elf_nds32_link_hash_table *table;
4346 table = nds32_elf_hash_table (info);
4347 sym_ld_script = table->sym_ld_script;
4351 if (!h || !name || *name == '\0')
4354 if (input_sec->flags & SEC_EXCLUDE)
4357 if (!check_start_export_sym)
4359 fprintf (sym_ld_script, "SECTIONS\n{\n");
4360 check_start_export_sym = 1;
4363 if (h->root.type == bfd_link_hash_defined
4364 || h->root.type == bfd_link_hash_defweak)
4366 if (!h->root.u.def.section->output_section)
4369 if (bfd_is_const_section (input_sec))
4370 source = input_sec->name;
4372 source = input_sec->owner->filename;
4374 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4375 h->root.root.string,
4376 (long) (h->root.u.def.value
4377 + h->root.u.def.section->output_section->vma
4378 + h->root.u.def.section->output_offset), source);
4384 /* Relocate an NDS32/D ELF section.
4385 There is some attempt to make this function usable for many architectures,
4386 both for RELA and REL type relocs, if only to serve as a learning tool.
4388 The RELOCATE_SECTION function is called by the new ELF backend linker
4389 to handle the relocations for a section.
4391 The relocs are always passed as Rela structures; if the section
4392 actually uses Rel structures, the r_addend field will always be
4395 This function is responsible for adjust the section contents as
4396 necessary, and (if using Rela relocs and generating a
4397 relocatable output file) adjusting the reloc addend as
4400 This function does not have to worry about setting the reloc
4401 address or the reloc symbol index.
4403 LOCAL_SYMS is a pointer to the swapped in local symbols.
4405 LOCAL_SECTIONS is an array giving the section in the input file
4406 corresponding to the st_shndx field of each local symbol.
4408 The global hash table entry for the global symbols can be found
4409 via elf_sym_hashes (input_bfd).
4411 When generating relocatable output, this function must handle
4412 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4413 going to be the section symbol corresponding to the output
4414 section, which means that the addend must be adjusted
4418 dtpoff_base (struct bfd_link_info *info)
4420 /* If tls_sec is NULL, we should have signalled an error already. */
4421 if (elf_hash_table (info)->tls_sec == NULL)
4423 return elf_hash_table (info)->tls_sec->vma;
4427 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
4428 struct bfd_link_info * info,
4430 asection * input_section,
4431 bfd_byte * contents,
4432 Elf_Internal_Rela * relocs,
4433 Elf_Internal_Sym * local_syms,
4434 asection ** local_sections)
4436 Elf_Internal_Shdr *symtab_hdr;
4437 struct elf_link_hash_entry **sym_hashes;
4438 Elf_Internal_Rela *rel, *relend;
4439 bfd_boolean ret = TRUE; /* Assume success. */
4441 bfd_reloc_status_type r;
4442 const char *errmsg = NULL;
4444 struct elf_nds32_link_hash_table *htab;
4446 bfd_vma *local_got_offsets;
4447 asection *sgot, *splt, *sreloc;
4448 bfd_vma high_address;
4449 struct elf_nds32_link_hash_table *table;
4450 int eliminate_gc_relocs;
4451 bfd_vma fpbase_addr;
4453 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4454 sym_hashes = elf_sym_hashes (input_bfd);
4455 htab = nds32_elf_hash_table (info);
4456 high_address = bfd_get_section_limit (input_bfd, input_section);
4458 dynobj = htab->root.dynobj;
4459 local_got_offsets = elf_local_got_offsets (input_bfd);
4461 sgot = htab->root.sgot;
4462 splt = htab->root.splt;
4466 relend = relocs + input_section->reloc_count;
4468 table = nds32_elf_hash_table (info);
4469 eliminate_gc_relocs = table->eliminate_gc_relocs;
4470 /* By this time, we can adjust the value of _SDA_BASE_. */
4471 if ((!bfd_link_relocatable (info)))
4473 is_SDA_BASE_set = 1;
4474 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
4475 if (r != bfd_reloc_ok)
4479 if (is_ITB_BASE_set == 0)
4481 /* Set the _ITB_BASE_. */
4482 if (!nds32_elf_ex9_itb_base (info))
4484 _bfd_error_handler (_("%B: error: Cannot set _ITB_BASE_"),
4486 bfd_set_error (bfd_error_bad_value);
4490 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
4491 if (!nds32_elf_ifc_reloc ())
4492 _bfd_error_handler (_("error: IFC relocation error."));
4494 /* Relocation for .ex9.itable. */
4495 if (table->target_optimize & NDS32_RELAX_EX9_ON
4496 || (table->ex9_import_file && table->update_ex9_table))
4497 nds32_elf_ex9_reloc_jmp (info);
4499 /* Use gp as fp to prevent truncated fit. Because in relaxation time
4500 the fp value is set as gp, and it has be reverted for instruction
4502 fpbase_addr = elf_gp (output_bfd);
4504 for (rel = relocs; rel < relend; rel++)
4506 enum elf_nds32_reloc_type r_type;
4507 reloc_howto_type *howto = NULL;
4508 unsigned long r_symndx;
4509 struct elf_link_hash_entry *h = NULL;
4510 Elf_Internal_Sym *sym = NULL;
4514 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
4515 ensure it's zero (we use REL relocs, not RELA). Therefore this
4516 should be assigning zero to `addend', but for clarity we use
4519 bfd_vma addend = rel->r_addend;
4520 bfd_vma offset = rel->r_offset;
4522 r_type = ELF32_R_TYPE (rel->r_info);
4523 if (r_type >= R_NDS32_max)
4525 /* xgettext:c-format */
4526 _bfd_error_handler (_("%B: error: unknown relocation type %d."),
4528 bfd_set_error (bfd_error_bad_value);
4533 if (r_type == R_NDS32_GNU_VTENTRY
4534 || r_type == R_NDS32_GNU_VTINHERIT
4535 || r_type == R_NDS32_NONE
4536 || r_type == R_NDS32_RELA_GNU_VTENTRY
4537 || r_type == R_NDS32_RELA_GNU_VTINHERIT
4538 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
4539 || r_type == R_NDS32_DATA
4540 || r_type == R_NDS32_TRAN
4541 || (r_type >= R_NDS32_LONGCALL4 && r_type <= R_NDS32_LONGJUMP7))
4544 /* If we enter the fp-as-gp region. Resolve the address
4546 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
4547 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4551 /* Distance to relocation of best fp-base is encoded in R_SYM. */
4552 dist = rel->r_addend >> 16;
4553 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
4554 local_syms, symtab_hdr);
4556 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
4557 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4559 fpbase_addr = elf_gp (output_bfd);
4562 if (((r_type >= R_NDS32_DWARF2_OP1_RELA
4563 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4564 || r_type >= R_NDS32_RELAX_ENTRY) && !bfd_link_relocatable (info))
4567 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
4568 r_symndx = ELF32_R_SYM (rel->r_info);
4570 /* This is a final link. */
4575 if (r_symndx < symtab_hdr->sh_info)
4578 sym = local_syms + r_symndx;
4579 sec = local_sections[r_symndx];
4581 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4582 addend = rel->r_addend;
4586 /* External symbol. */
4587 bfd_boolean warned, ignored, unresolved_reloc;
4588 int symndx = r_symndx - symtab_hdr->sh_info;
4590 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4591 r_symndx, symtab_hdr, sym_hashes, h, sec,
4592 relocation, unresolved_reloc, warned,
4595 /* la $fp, _FP_BASE_ is per-function (region).
4596 Handle it specially. */
4597 switch ((int) r_type)
4599 case R_NDS32_SDA19S0_RELA:
4600 case R_NDS32_SDA15S0_RELA:
4601 case R_NDS32_20_RELA:
4602 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
4605 relocation = fpbase_addr;
4612 if (bfd_link_relocatable (info))
4614 /* This is a relocatable link. We don't have to change
4615 anything, unless the reloc is against a section symbol,
4616 in which case we have to adjust according to where the
4617 section symbol winds up in the output section. */
4618 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4619 rel->r_addend += sec->output_offset + sym->st_value;
4624 /* Sanity check the address. */
4625 if (offset > high_address)
4627 r = bfd_reloc_outofrange;
4631 if ((r_type >= R_NDS32_DWARF2_OP1_RELA
4632 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4633 || r_type >= R_NDS32_RELAX_ENTRY)
4636 switch ((int) r_type)
4638 case R_NDS32_GOTOFF:
4639 /* Relocation is relative to the start of the global offset
4640 table (for ld24 rx, #uimm24), e.g. access at label+addend
4642 ld24 rx. #label@GOTOFF + addend
4644 case R_NDS32_GOTOFF_HI20:
4645 case R_NDS32_GOTOFF_LO12:
4646 case R_NDS32_GOTOFF_LO15:
4647 case R_NDS32_GOTOFF_LO19:
4648 BFD_ASSERT (sgot != NULL);
4650 relocation -= elf_gp (output_bfd);
4653 case R_NDS32_9_PLTREL:
4654 case R_NDS32_25_PLTREL:
4655 /* Relocation is to the entry for this symbol in the
4656 procedure linkage table. */
4658 /* The native assembler will generate a 25_PLTREL reloc
4659 for a local symbol if you assemble a call from one
4660 section to another when using -K pic. */
4664 if (h->forced_local)
4667 /* We didn't make a PLT entry for this symbol. This
4668 happens when statically linking PIC code, or when
4669 using -Bsymbolic. */
4670 if (h->plt.offset == (bfd_vma) - 1)
4673 relocation = (splt->output_section->vma
4674 + splt->output_offset + h->plt.offset);
4677 case R_NDS32_PLT_GOTREL_HI20:
4678 case R_NDS32_PLT_GOTREL_LO12:
4679 case R_NDS32_PLT_GOTREL_LO15:
4680 case R_NDS32_PLT_GOTREL_LO19:
4681 case R_NDS32_PLT_GOTREL_LO20:
4682 if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
4684 /* We didn't make a PLT entry for this symbol. This
4685 happens when statically linking PIC code, or when
4686 using -Bsymbolic. */
4687 relocation -= elf_gp (output_bfd);
4691 relocation = (splt->output_section->vma
4692 + splt->output_offset + h->plt.offset);
4694 relocation -= elf_gp (output_bfd);
4697 case R_NDS32_PLTREL_HI20:
4698 case R_NDS32_PLTREL_LO12:
4700 /* Relocation is to the entry for this symbol in the
4701 procedure linkage table. */
4703 /* The native assembler will generate a 25_PLTREL reloc
4704 for a local symbol if you assemble a call from one
4705 section to another when using -K pic. */
4709 if (h->forced_local)
4712 if (h->plt.offset == (bfd_vma) - 1)
4713 /* We didn't make a PLT entry for this symbol. This
4714 happens when statically linking PIC code, or when
4715 using -Bsymbolic. */
4721 relocation = (splt->output_section->vma
4722 + splt->output_offset
4723 + h->plt.offset + 4)
4724 - (input_section->output_section->vma
4725 + input_section->output_offset
4730 case R_NDS32_GOTPC20:
4731 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4732 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
4733 relocation = elf_gp (output_bfd);
4736 case R_NDS32_GOTPC_HI20:
4737 case R_NDS32_GOTPC_LO12:
4739 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4741 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
4742 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4745 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
4746 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4748 relocation = elf_gp (output_bfd);
4749 relocation -= (input_section->output_section->vma
4750 + input_section->output_offset + rel->r_offset);
4756 case R_NDS32_GOT_HI20:
4757 case R_NDS32_GOT_LO12:
4758 case R_NDS32_GOT_LO15:
4759 case R_NDS32_GOT_LO19:
4760 /* Relocation is to the entry for this symbol in the global
4762 BFD_ASSERT (sgot != NULL);
4769 off = h->got.offset;
4770 BFD_ASSERT (off != (bfd_vma) - 1);
4771 dyn = htab->root.dynamic_sections_created;
4772 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4773 bfd_link_pic (info),
4775 || (bfd_link_pic (info)
4778 || h->forced_local) && h->def_regular))
4780 /* This is actually a static link, or it is a
4781 -Bsymbolic link and the symbol is defined
4782 locally, or the symbol was forced to be local
4783 because of a version file. We must initialize
4784 this entry in the global offset table. Since the
4785 offset must always be a multiple of 4, we use the
4786 least significant bit to record whether we have
4787 initialized it already.
4789 When doing a dynamic link, we create a .rela.got
4790 relocation entry to initialize the value. This
4791 is done in the finish_dynamic_symbol routine. */
4796 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4800 relocation = sgot->output_section->vma + sgot->output_offset + off
4801 - elf_gp (output_bfd);
4808 BFD_ASSERT (local_got_offsets != NULL
4809 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4811 off = local_got_offsets[r_symndx];
4813 /* The offset must always be a multiple of 4. We use
4814 the least significant bit to record whether we have
4815 already processed this entry. */
4820 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4822 if (bfd_link_pic (info))
4825 Elf_Internal_Rela outrel;
4827 /* We need to generate a R_NDS32_RELATIVE reloc
4828 for the dynamic linker. */
4829 srelgot = htab->root.srelgot;
4830 BFD_ASSERT (srelgot != NULL);
4832 outrel.r_offset = (elf_gp (output_bfd)
4833 + sgot->output_offset + off);
4834 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4835 outrel.r_addend = relocation;
4836 loc = srelgot->contents;
4838 srelgot->reloc_count * sizeof (Elf32_External_Rela);
4839 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4840 ++srelgot->reloc_count;
4842 local_got_offsets[r_symndx] |= 1;
4844 relocation = sgot->output_section->vma + sgot->output_offset + off
4845 - elf_gp (output_bfd);
4850 case R_NDS32_16_RELA:
4851 case R_NDS32_20_RELA:
4852 case R_NDS32_5_RELA:
4853 case R_NDS32_32_RELA:
4854 case R_NDS32_9_PCREL_RELA:
4855 case R_NDS32_WORD_9_PCREL_RELA:
4856 case R_NDS32_10_UPCREL_RELA:
4857 case R_NDS32_15_PCREL_RELA:
4858 case R_NDS32_17_PCREL_RELA:
4859 case R_NDS32_25_PCREL_RELA:
4860 case R_NDS32_HI20_RELA:
4861 case R_NDS32_LO12S3_RELA:
4862 case R_NDS32_LO12S2_RELA:
4863 case R_NDS32_LO12S2_DP_RELA:
4864 case R_NDS32_LO12S2_SP_RELA:
4865 case R_NDS32_LO12S1_RELA:
4866 case R_NDS32_LO12S0_RELA:
4867 case R_NDS32_LO12S0_ORI_RELA:
4868 if (bfd_link_pic (info) && r_symndx != 0
4869 && (input_section->flags & SEC_ALLOC) != 0
4870 && (eliminate_gc_relocs == 0
4871 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
4872 && ((r_type != R_NDS32_9_PCREL_RELA
4873 && r_type != R_NDS32_WORD_9_PCREL_RELA
4874 && r_type != R_NDS32_10_UPCREL_RELA
4875 && r_type != R_NDS32_15_PCREL_RELA
4876 && r_type != R_NDS32_17_PCREL_RELA
4877 && r_type != R_NDS32_25_PCREL_RELA
4878 && !(r_type == R_NDS32_32_RELA
4879 && strcmp (input_section->name, ".eh_frame") == 0))
4880 || (h != NULL && h->dynindx != -1
4881 && (!info->symbolic || !h->def_regular))))
4883 Elf_Internal_Rela outrel;
4884 bfd_boolean skip, relocate;
4887 /* When generating a shared object, these relocations
4888 are copied into the output file to be resolved at run
4895 name = bfd_elf_string_from_elf_section
4896 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
4897 elf_section_data (input_section)->rela.hdr->sh_name);
4901 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4902 && strcmp (bfd_get_section_name (input_bfd,
4906 sreloc = bfd_get_section_by_name (dynobj, name);
4907 BFD_ASSERT (sreloc != NULL);
4913 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4917 if (outrel.r_offset == (bfd_vma) - 1)
4919 else if (outrel.r_offset == (bfd_vma) - 2)
4920 skip = TRUE, relocate = TRUE;
4921 outrel.r_offset += (input_section->output_section->vma
4922 + input_section->output_offset);
4925 memset (&outrel, 0, sizeof outrel);
4926 else if (r_type == R_NDS32_17_PCREL_RELA
4927 || r_type == R_NDS32_15_PCREL_RELA
4928 || r_type == R_NDS32_25_PCREL_RELA)
4930 BFD_ASSERT (h != NULL && h->dynindx != -1);
4931 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4932 outrel.r_addend = rel->r_addend;
4936 /* h->dynindx may be -1 if this symbol was marked to
4939 || ((info->symbolic || h->dynindx == -1)
4943 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4944 outrel.r_addend = relocation + rel->r_addend;
4948 BFD_ASSERT (h->dynindx != -1);
4949 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4950 outrel.r_addend = rel->r_addend;
4954 loc = sreloc->contents;
4955 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
4956 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4957 ++sreloc->reloc_count;
4959 /* If this reloc is against an external symbol, we do
4960 not want to fiddle with the addend. Otherwise, we
4961 need to include the symbol value so that it becomes
4962 an addend for the dynamic reloc. */
4968 case R_NDS32_25_ABS_RELA:
4969 if (bfd_link_pic (info))
4972 (_("%s: warning: cannot deal R_NDS32_25_ABS_RELA in shared "
4973 "mode."), bfd_get_filename (input_bfd));
4978 case R_NDS32_9_PCREL:
4979 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
4981 sec, relocation, addend);
4986 Elf_Internal_Rela *lorel;
4988 /* We allow an arbitrary number of HI20 relocs before the
4989 LO12 reloc. This permits gcc to emit the HI and LO relocs
4991 for (lorel = rel + 1;
4993 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
4996 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
4997 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
4998 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
4999 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5001 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5002 contents, relocation + addend);
5006 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5007 contents, offset, relocation,
5013 case R_NDS32_GOT17S2_RELA:
5014 case R_NDS32_GOT15S2_RELA:
5018 BFD_ASSERT (sgot != NULL);
5024 off = h->got.offset;
5025 BFD_ASSERT (off != (bfd_vma) - 1);
5027 dyn = htab->root.dynamic_sections_created;
5028 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5029 (dyn, bfd_link_pic (info), h)
5030 || (bfd_link_pic (info)
5036 /* This is actually a static link, or it is a
5037 -Bsymbolic link and the symbol is defined
5038 locally, or the symbol was forced to be local
5039 because of a version file. We must initialize
5040 this entry in the global offset table. Since the
5041 offset must always be a multiple of 4, we use the
5042 least significant bit to record whether we have
5043 initialized it already.
5045 When doing a dynamic link, we create a .rela.got
5046 relocation entry to initialize the value. This
5047 is done in the finish_dynamic_symbol routine. */
5052 bfd_put_32 (output_bfd, relocation,
5053 sgot->contents + off);
5062 BFD_ASSERT (local_got_offsets != NULL
5063 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5065 off = local_got_offsets[r_symndx];
5067 /* The offset must always be a multiple of 4. We use
5068 the least significant bit to record whether we have
5069 already processed this entry. */
5074 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5076 if (bfd_link_pic (info))
5079 Elf_Internal_Rela outrel;
5081 /* We need to generate a R_NDS32_RELATIVE reloc
5082 for the dynamic linker. */
5083 srelgot = htab->root.srelgot;
5084 BFD_ASSERT (srelgot != NULL);
5086 outrel.r_offset = (elf_gp (output_bfd)
5087 + sgot->output_offset + off);
5088 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5089 outrel.r_addend = relocation;
5090 loc = srelgot->contents;
5092 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5093 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5094 ++srelgot->reloc_count;
5096 local_got_offsets[r_symndx] |= 1;
5099 relocation = sgot->output_section->vma + sgot->output_offset + off
5100 - elf_gp (output_bfd);
5102 if (relocation & align)
5104 /* Incorrect alignment. */
5106 (_("%B: warning: unaligned access to GOT entry."), input_bfd);
5108 r = bfd_reloc_dangerous;
5113 case R_NDS32_SDA16S3_RELA:
5114 case R_NDS32_SDA15S3_RELA:
5115 case R_NDS32_SDA15S3:
5119 case R_NDS32_SDA17S2_RELA:
5120 case R_NDS32_SDA15S2_RELA:
5121 case R_NDS32_SDA12S2_SP_RELA:
5122 case R_NDS32_SDA12S2_DP_RELA:
5123 case R_NDS32_SDA15S2:
5124 case R_NDS32_SDA_FP7U2_RELA:
5128 case R_NDS32_SDA18S1_RELA:
5129 case R_NDS32_SDA15S1_RELA:
5130 case R_NDS32_SDA15S1:
5134 case R_NDS32_SDA19S0_RELA:
5135 case R_NDS32_SDA15S0_RELA:
5136 case R_NDS32_SDA15S0:
5140 BFD_ASSERT (sec != NULL);
5142 /* If the symbol is in the abs section, the out_bfd will be null.
5143 This happens when the relocation has a symbol@GOTOFF. */
5144 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5145 if (r != bfd_reloc_ok)
5148 (_("%B: warning: relocate SDA_BASE failed."), input_bfd);
5153 /* At this point `relocation' contains the object's
5155 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5157 relocation -= fpbase_addr;
5161 /* Now it contains the offset from _SDA_BASE_. */
5163 /* Make sure alignment is correct. */
5165 if (relocation & align)
5167 /* Incorrect alignment. */
5169 /* xgettext:c-format */
5170 (_("%B(%A): warning: unaligned small data access of type %d."),
5171 input_bfd, input_section, r_type);
5178 case R_NDS32_17IFC_PCREL_RELA:
5179 case R_NDS32_10IFCU_PCREL_RELA:
5183 case R_NDS32_TLS_LE_HI20:
5184 case R_NDS32_TLS_LE_LO12:
5185 case R_NDS32_TLS_LE_20:
5186 case R_NDS32_TLS_LE_15S0:
5187 case R_NDS32_TLS_LE_15S1:
5188 case R_NDS32_TLS_LE_15S2:
5189 if (elf_hash_table (info)->tls_sec != NULL)
5190 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5192 case R_NDS32_TLS_IE_HI20:
5193 case R_NDS32_TLS_IE_LO12S2:
5195 /* Relocation is to the entry for this symbol in the global
5197 unsigned int tls_type;
5199 Elf_Internal_Rela outrel;
5204 BFD_ASSERT (sgot != NULL);
5209 off = h->got.offset;
5210 BFD_ASSERT (off != (bfd_vma) - 1);
5211 dyn = htab->root.dynamic_sections_created;
5212 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5213 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5214 && (!bfd_link_pic (info)
5215 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5220 /* Never happen currently. */
5221 BFD_ASSERT (local_got_offsets != NULL
5222 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5224 off = local_got_offsets[r_symndx];
5226 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5228 relocation = sgot->output_section->vma + sgot->output_offset + off;
5230 if (r_type == R_NDS32_TLS_IE_LO12S2)
5233 /* The offset must always be a multiple of 4. We use
5234 the least significant bit to record whether we have
5235 already processed this entry. */
5240 bfd_boolean need_relocs = FALSE;
5241 srelgot = htab->root.srelgot;
5242 if ((bfd_link_pic (info) || indx != 0)
5244 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5245 || h->root.type != bfd_link_hash_undefweak))
5248 BFD_ASSERT (srelgot != NULL);
5250 if (tls_type & GOT_TLS_IE)
5254 if (h->dynindx == 0)
5255 outrel.r_addend = relocation - dtpoff_base (info);
5257 outrel.r_addend = 0;
5258 outrel.r_offset = (sgot->output_section->vma
5259 + sgot->output_offset
5262 ELF32_R_INFO (h->dynindx, R_NDS32_TLS_TPOFF);
5264 loc = srelgot->contents;
5266 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5267 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5268 ++srelgot->reloc_count;
5271 bfd_put_32 (output_bfd, h->root.u.def.value - TP_OFFSET,
5272 sgot->contents + off);
5278 /* DON'T fall through. */
5281 /* OLD_NDS32_RELOC. */
5283 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5284 contents, offset, relocation, addend);
5288 switch ((int) r_type)
5290 case R_NDS32_20_RELA:
5291 case R_NDS32_5_RELA:
5292 case R_NDS32_9_PCREL_RELA:
5293 case R_NDS32_WORD_9_PCREL_RELA:
5294 case R_NDS32_10_UPCREL_RELA:
5295 case R_NDS32_15_PCREL_RELA:
5296 case R_NDS32_17_PCREL_RELA:
5297 case R_NDS32_25_PCREL_RELA:
5298 case R_NDS32_25_ABS_RELA:
5299 case R_NDS32_HI20_RELA:
5300 case R_NDS32_LO12S3_RELA:
5301 case R_NDS32_LO12S2_RELA:
5302 case R_NDS32_LO12S2_DP_RELA:
5303 case R_NDS32_LO12S2_SP_RELA:
5304 case R_NDS32_LO12S1_RELA:
5305 case R_NDS32_LO12S0_RELA:
5306 case R_NDS32_LO12S0_ORI_RELA:
5307 case R_NDS32_SDA16S3_RELA:
5308 case R_NDS32_SDA17S2_RELA:
5309 case R_NDS32_SDA18S1_RELA:
5310 case R_NDS32_SDA19S0_RELA:
5311 case R_NDS32_SDA15S3_RELA:
5312 case R_NDS32_SDA15S2_RELA:
5313 case R_NDS32_SDA12S2_DP_RELA:
5314 case R_NDS32_SDA12S2_SP_RELA:
5315 case R_NDS32_SDA15S1_RELA:
5316 case R_NDS32_SDA15S0_RELA:
5317 case R_NDS32_SDA_FP7U2_RELA:
5318 case R_NDS32_9_PLTREL:
5319 case R_NDS32_25_PLTREL:
5321 case R_NDS32_GOT_HI20:
5322 case R_NDS32_GOT_LO12:
5323 case R_NDS32_GOT_LO15:
5324 case R_NDS32_GOT_LO19:
5325 case R_NDS32_GOT15S2_RELA:
5326 case R_NDS32_GOT17S2_RELA:
5327 case R_NDS32_GOTPC20:
5328 case R_NDS32_GOTPC_HI20:
5329 case R_NDS32_GOTPC_LO12:
5330 case R_NDS32_GOTOFF:
5331 case R_NDS32_GOTOFF_HI20:
5332 case R_NDS32_GOTOFF_LO12:
5333 case R_NDS32_GOTOFF_LO15:
5334 case R_NDS32_GOTOFF_LO19:
5335 case R_NDS32_PLTREL_HI20:
5336 case R_NDS32_PLTREL_LO12:
5337 case R_NDS32_PLT_GOTREL_HI20:
5338 case R_NDS32_PLT_GOTREL_LO12:
5339 case R_NDS32_PLT_GOTREL_LO15:
5340 case R_NDS32_PLT_GOTREL_LO19:
5341 case R_NDS32_PLT_GOTREL_LO20:
5342 case R_NDS32_17IFC_PCREL_RELA:
5343 case R_NDS32_10IFCU_PCREL_RELA:
5344 case R_NDS32_TLS_LE_HI20:
5345 case R_NDS32_TLS_LE_LO12:
5346 case R_NDS32_TLS_IE_HI20:
5347 case R_NDS32_TLS_IE_LO12S2:
5348 case R_NDS32_TLS_LE_20:
5349 case R_NDS32_TLS_LE_15S0:
5350 case R_NDS32_TLS_LE_15S1:
5351 case R_NDS32_TLS_LE_15S2:
5352 /* Instruction related relocs must handle endian properly. */
5353 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
5354 r = nds32_elf_final_link_relocate (howto, input_bfd,
5355 input_section, contents,
5356 rel->r_offset, relocation,
5361 /* All other relocs can use default handler. */
5362 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5363 contents, rel->r_offset,
5364 relocation, rel->r_addend);
5370 if (r != bfd_reloc_ok)
5372 /* FIXME: This should be generic enough to go in a utility. */
5376 name = h->root.root.string;
5379 name = bfd_elf_string_from_elf_section
5380 (input_bfd, symtab_hdr->sh_link, sym->st_name);
5381 if (name == NULL || *name == '\0')
5382 name = bfd_section_name (input_bfd, sec);
5390 case bfd_reloc_overflow:
5391 (*info->callbacks->reloc_overflow)
5392 (info, (h ? &h->root : NULL), name, howto->name,
5393 (bfd_vma) 0, input_bfd, input_section, offset);
5396 case bfd_reloc_undefined:
5397 (*info->callbacks->undefined_symbol)
5398 (info, name, input_bfd, input_section, offset, TRUE);
5401 case bfd_reloc_outofrange:
5402 errmsg = _("internal error: out of range error");
5405 case bfd_reloc_notsupported:
5406 errmsg = _("internal error: unsupported relocation error");
5409 case bfd_reloc_dangerous:
5410 errmsg = _("internal error: dangerous error");
5414 errmsg = _("internal error: unknown error");
5418 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
5419 input_section, offset);
5428 /* Finish up dynamic symbol handling. We set the contents of various
5429 dynamic sections here. */
5432 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5433 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
5435 struct elf_nds32_link_hash_table *htab;
5438 htab = nds32_elf_hash_table (info);
5440 if (h->plt.offset != (bfd_vma) - 1)
5448 bfd_vma local_plt_offset;
5449 Elf_Internal_Rela rela;
5451 /* This symbol has an entry in the procedure linkage table. Set
5454 BFD_ASSERT (h->dynindx != -1);
5456 splt = htab->root.splt;
5457 sgot = htab->root.sgotplt;
5458 srela = htab->root.srelplt;
5459 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
5461 /* Get the index in the procedure linkage table which
5462 corresponds to this symbol. This is the index of this symbol
5463 in all the symbols for which we are making plt entries. The
5464 first entry in the procedure linkage table is reserved. */
5465 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
5467 /* Get the offset into the .got table of the entry that
5468 corresponds to this function. Each .got entry is 4 bytes.
5469 The first three are reserved. */
5470 got_offset = (plt_index + 3) * 4;
5472 /* Fill in the entry in the procedure linkage table. */
5473 if (!bfd_link_pic (info))
5477 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
5478 + sgot->output_offset + got_offset) >> 12)
5480 bfd_putb32 (insn, splt->contents + h->plt.offset);
5482 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
5483 + sgot->output_offset + got_offset) & 0x0fff)
5485 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5487 insn = PLT_ENTRY_WORD2;
5488 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5490 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
5491 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5493 insn = PLT_ENTRY_WORD4
5494 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
5495 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5496 local_plt_offset = 12;
5500 /* sda_base must be set at this time. */
5504 /* FIXME, sda_base is 65536, it will damage opcode. */
5505 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5506 offset = sgot->output_section->vma + sgot->output_offset + got_offset
5507 - elf_gp (output_bfd);
5508 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
5509 bfd_putb32 (insn, splt->contents + h->plt.offset);
5511 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
5512 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5514 insn = PLT_PIC_ENTRY_WORD2;
5515 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5517 insn = PLT_PIC_ENTRY_WORD3;
5518 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5520 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
5521 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5523 insn = PLT_PIC_ENTRY_WORD5
5524 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
5525 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
5527 local_plt_offset = 16;
5530 /* Fill in the entry in the global offset table,
5531 so it will fall through to the next instruction for the first time. */
5532 bfd_put_32 (output_bfd,
5533 (splt->output_section->vma + splt->output_offset
5534 + h->plt.offset + local_plt_offset),
5535 sgot->contents + got_offset);
5537 /* Fill in the entry in the .rela.plt section. */
5538 rela.r_offset = (sgot->output_section->vma
5539 + sgot->output_offset + got_offset);
5540 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
5542 loc = srela->contents;
5543 loc += plt_index * sizeof (Elf32_External_Rela);
5544 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5546 if (!h->def_regular)
5548 /* Mark the symbol as undefined, rather than as defined in
5549 the .plt section. Leave the value alone. */
5550 sym->st_shndx = SHN_UNDEF;
5551 if (!h->ref_regular_nonweak)
5556 if (h->got.offset != (bfd_vma) - 1)
5560 Elf_Internal_Rela rela;
5562 /* This symbol has an entry in the global offset table.
5565 sgot = htab->root.sgot;
5566 srela = htab->root.srelgot;
5567 BFD_ASSERT (sgot != NULL && srela != NULL);
5569 rela.r_offset = (sgot->output_section->vma
5570 + sgot->output_offset + (h->got.offset & ~1));
5572 /* If this is a -Bsymbolic link, and the symbol is defined
5573 locally, we just want to emit a RELATIVE reloc. Likewise if
5574 the symbol was forced to be local because of a version file.
5575 The entry in the global offset table will already have been
5576 initialized in the relocate_section function. */
5577 if (bfd_link_pic (info)
5579 || h->dynindx == -1 || h->forced_local) && h->def_regular)
5581 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5582 rela.r_addend = (h->root.u.def.value
5583 + h->root.u.def.section->output_section->vma
5584 + h->root.u.def.section->output_offset);
5588 BFD_ASSERT ((h->got.offset & 1) == 0);
5589 bfd_put_32 (output_bfd, (bfd_vma) 0,
5590 sgot->contents + h->got.offset);
5591 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
5595 loc = srela->contents;
5596 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
5597 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5598 ++srela->reloc_count;
5604 Elf_Internal_Rela rela;
5606 /* This symbols needs a copy reloc. Set it up. */
5608 BFD_ASSERT (h->dynindx != -1
5609 && (h->root.type == bfd_link_hash_defined
5610 || h->root.type == bfd_link_hash_defweak));
5612 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
5613 BFD_ASSERT (s != NULL);
5615 rela.r_offset = (h->root.u.def.value
5616 + h->root.u.def.section->output_section->vma
5617 + h->root.u.def.section->output_offset);
5618 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
5621 loc += s->reloc_count * sizeof (Elf32_External_Rela);
5622 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5626 /* Mark some specially defined symbols as absolute. */
5627 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5628 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5629 sym->st_shndx = SHN_ABS;
5635 /* Finish up the dynamic sections. */
5638 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
5640 struct elf_nds32_link_hash_table *htab;
5645 htab = nds32_elf_hash_table (info);
5646 dynobj = htab->root.dynobj;
5648 sgot = htab->root.sgotplt;
5649 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5651 if (htab->root.dynamic_sections_created)
5654 Elf32_External_Dyn *dyncon, *dynconend;
5656 BFD_ASSERT (sgot != NULL && sdyn != NULL);
5658 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5659 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5661 for (; dyncon < dynconend; dyncon++)
5663 Elf_Internal_Dyn dyn;
5666 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5674 s = htab->root.sgotplt;
5677 s = htab->root.srelplt;
5679 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5680 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5684 s = htab->root.srelplt;
5685 dyn.d_un.d_val = s->size;
5686 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5690 /* My reading of the SVR4 ABI indicates that the
5691 procedure linkage table relocs (DT_JMPREL) should be
5692 included in the overall relocs (DT_RELA). This is
5693 what Solaris does. However, UnixWare can not handle
5694 that case. Therefore, we override the DT_RELASZ entry
5695 here to make it not include the JMPREL relocs. Since
5696 the linker script arranges for .rela.plt to follow all
5697 other relocation sections, we don't have to worry
5698 about changing the DT_RELA entry. */
5699 if (htab->root.srelplt != NULL)
5701 s = htab->root.srelplt;
5702 dyn.d_un.d_val -= s->size;
5704 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5709 /* Fill in the first entry in the procedure linkage table. */
5710 splt = htab->root.splt;
5711 if (splt && splt->size > 0)
5713 if (bfd_link_pic (info))
5718 /* FIXME, sda_base is 65536, it will damage opcode. */
5719 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5720 offset = sgot->output_section->vma + sgot->output_offset + 4
5721 - elf_gp (output_bfd);
5722 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
5723 bfd_putb32 (insn, splt->contents);
5725 /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
5726 /* here has a typo? */
5727 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
5728 bfd_putb32 (insn, splt->contents + 4);
5730 insn = PLT0_PIC_ENTRY_WORD2;
5731 bfd_putb32 (insn, splt->contents + 8);
5733 insn = PLT0_PIC_ENTRY_WORD3;
5734 bfd_putb32 (insn, splt->contents + 12);
5736 insn = PLT0_PIC_ENTRY_WORD4;
5737 bfd_putb32 (insn, splt->contents + 16);
5739 insn = PLT0_PIC_ENTRY_WORD5;
5740 bfd_putb32 (insn, splt->contents + 20);
5747 /* addr = .got + 4 */
5748 addr = sgot->output_section->vma + sgot->output_offset + 4;
5749 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
5750 bfd_putb32 (insn, splt->contents);
5752 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
5753 bfd_putb32 (insn, splt->contents + 4);
5755 insn = PLT0_ENTRY_WORD2;
5756 bfd_putb32 (insn, splt->contents + 8);
5758 insn = PLT0_ENTRY_WORD3;
5759 bfd_putb32 (insn, splt->contents + 12);
5761 insn = PLT0_ENTRY_WORD4;
5762 bfd_putb32 (insn, splt->contents + 16);
5765 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
5770 /* Fill in the first three entries in the global offset table. */
5771 if (sgot && sgot->size > 0)
5774 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5776 bfd_put_32 (output_bfd,
5777 sdyn->output_section->vma + sdyn->output_offset,
5779 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5780 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5782 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5789 /* Set the right machine number. */
5792 nds32_elf_object_p (bfd *abfd)
5794 static unsigned int cur_arch = 0;
5796 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
5798 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
5799 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
5806 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
5809 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
5811 case E_NDS_ARCH_STAR_V2_0:
5812 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
5814 case E_NDS_ARCH_STAR_V3_0:
5815 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
5817 case E_NDS_ARCH_STAR_V3_M:
5818 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
5825 /* Store the machine number in the flags field. */
5828 nds32_elf_final_write_processing (bfd *abfd,
5829 bfd_boolean linker ATTRIBUTE_UNUSED)
5832 static unsigned int cur_mach = 0;
5834 if (bfd_mach_n1 != bfd_get_mach (abfd))
5836 cur_mach = bfd_get_mach (abfd);
5842 /* Only happen when object is empty, since the case is abandon. */
5844 val |= E_NDS_ABI_AABI;
5845 val |= E_NDS32_ELF_VER_1_4;
5850 case bfd_mach_n1h_v2:
5851 val = E_NDS_ARCH_STAR_V2_0;
5853 case bfd_mach_n1h_v3:
5854 val = E_NDS_ARCH_STAR_V3_0;
5856 case bfd_mach_n1h_v3m:
5857 val = E_NDS_ARCH_STAR_V3_M;
5864 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
5865 elf_elfheader (abfd)->e_flags |= val;
5868 /* Function to keep NDS32 specific file flags. */
5871 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
5873 BFD_ASSERT (!elf_flags_init (abfd)
5874 || elf_elfheader (abfd)->e_flags == flags);
5876 elf_elfheader (abfd)->e_flags = flags;
5877 elf_flags_init (abfd) = TRUE;
5882 convert_e_flags (unsigned int e_flags, unsigned int arch)
5884 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
5886 /* From 0.9 to 1.0. */
5887 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
5889 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5890 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5891 if (arch == E_NDS_ARCH_STAR_V1_0)
5898 /* From 1.0 to 2.0. */
5899 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
5901 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
5902 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
5904 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5905 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5910 nds32_check_vec_size (bfd *ibfd)
5912 static unsigned int nds32_vec_size = 0;
5914 asection *sec_t = NULL;
5915 bfd_byte *contents = NULL;
5917 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
5919 if (sec_t && sec_t->size >= 4)
5921 /* Get vec_size in file. */
5922 unsigned int flag_t;
5924 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
5925 flag_t = bfd_get_32 (ibfd, contents);
5927 /* The value could only be 4 or 16. */
5929 if (!nds32_vec_size)
5930 /* Set if not set yet. */
5931 nds32_vec_size = (flag_t & 0x3);
5932 else if (nds32_vec_size != (flag_t & 0x3))
5935 /* xgettext:c-format */
5936 (_("%B: ISR vector size mismatch"
5937 " with previous modules, previous %u-byte, current %u-byte"),
5939 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
5940 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
5944 /* Only keep the first vec_size section. */
5945 sec_t->flags |= SEC_EXCLUDE;
5951 /* Merge backend specific data from an object file to the output
5952 object file when linking. */
5955 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5957 bfd *obfd = info->output_bfd;
5960 flagword out_16regs;
5962 flagword out_no_mac;
5964 flagword out_version;
5965 flagword in_version;
5966 flagword out_fpu_config;
5967 flagword in_fpu_config;
5969 /* TODO: Revise to use object-attributes instead. */
5970 if (!nds32_check_vec_size (ibfd))
5973 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5974 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5977 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
5980 (_("%B: warning: Endian mismatch with previous modules."), ibfd);
5982 bfd_set_error (bfd_error_bad_value);
5986 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
5987 if (in_version == E_NDS32_ELF_VER_1_2)
5990 (_("%B: warning: Older version of object file encountered, "
5991 "Please recompile with current tool chain."), ibfd);
5994 /* We may need to merge V1 and V2 arch object files to V2. */
5995 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5996 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5998 /* Need to convert version. */
5999 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6000 == E_NDS_ARCH_STAR_RESERVED)
6002 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6004 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
6005 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6006 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6008 elf_elfheader (obfd)->e_flags =
6009 convert_e_flags (elf_elfheader (obfd)->e_flags,
6010 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6014 elf_elfheader (ibfd)->e_flags =
6015 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6016 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6020 /* Extract some flags. */
6021 in_flags = elf_elfheader (ibfd)->e_flags
6022 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6023 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6025 /* The following flags need special treatment. */
6026 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6027 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6028 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6030 /* Extract some flags. */
6031 out_flags = elf_elfheader (obfd)->e_flags
6032 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6033 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6035 /* The following flags need special treatment. */
6036 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6037 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6038 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6039 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6040 if (!elf_flags_init (obfd))
6042 /* If the input is the default architecture then do not
6043 bother setting the flags for the output architecture,
6044 instead allow future merges to do this. If no future
6045 merges ever set these flags then they will retain their
6046 unitialised values, which surprise surprise, correspond
6047 to the default values. */
6048 if (bfd_get_arch_info (ibfd)->the_default)
6051 elf_flags_init (obfd) = TRUE;
6052 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6054 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6055 && bfd_get_arch_info (obfd)->the_default)
6057 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6058 bfd_get_mach (ibfd));
6064 /* Check flag compatibility. */
6065 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6068 (_("%B: error: ABI mismatch with previous modules."), ibfd);
6070 bfd_set_error (bfd_error_bad_value);
6074 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6076 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6079 (_("%B: error: Instruction set mismatch with previous modules."), ibfd);
6081 bfd_set_error (bfd_error_bad_value);
6086 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6087 and perf ext1 and DIV are mergerd to perf ext1. */
6088 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6090 elf_elfheader (obfd)->e_flags =
6091 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6092 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6093 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6094 ? E_NDS32_HAS_EXT_INST : 0)
6095 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6096 ? E_NDS32_HAS_EXT_INST : 0)
6097 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6098 | ((in_version > out_version) ? out_version : in_version);
6102 if (in_version != out_version)
6104 /* xgettext:c-format */
6105 (_("%B: warning: Incompatible elf-versions %s and %s."),
6106 ibfd, nds32_elfver_strtab[out_version],
6107 nds32_elfver_strtab[in_version]);
6109 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6110 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6111 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6112 | (in_version > out_version ? out_version : in_version);
6118 /* Display the flags field. */
6121 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6123 FILE *file = (FILE *) ptr;
6125 BFD_ASSERT (abfd != NULL && ptr != NULL);
6127 _bfd_elf_print_private_bfd_data (abfd, ptr);
6129 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6131 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6135 fprintf (file, _(": n1 instructions"));
6138 fprintf (file, _(": n1h instructions"));
6148 nds32_elf_action_discarded (asection *sec)
6152 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6155 return _bfd_elf_default_action_discarded (sec);
6159 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6160 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6161 Elf_Internal_Sym *sym)
6164 switch (ELF32_R_TYPE (rel->r_info))
6166 case R_NDS32_GNU_VTINHERIT:
6167 case R_NDS32_GNU_VTENTRY:
6168 case R_NDS32_RELA_GNU_VTINHERIT:
6169 case R_NDS32_RELA_GNU_VTENTRY:
6173 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6177 nds32_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, asection *sec,
6178 const Elf_Internal_Rela *relocs)
6180 /* Update the got entry reference counts for the section being removed. */
6181 Elf_Internal_Shdr *symtab_hdr;
6182 struct elf_link_hash_entry **sym_hashes;
6183 bfd_signed_vma *local_got_refcounts;
6184 const Elf_Internal_Rela *rel, *relend;
6186 elf_section_data (sec)->local_dynrel = NULL;
6188 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6189 sym_hashes = elf_sym_hashes (abfd);
6190 local_got_refcounts = elf_local_got_refcounts (abfd);
6192 relend = relocs + sec->reloc_count;
6193 for (rel = relocs; rel < relend; rel++)
6195 unsigned long r_symndx;
6196 struct elf_link_hash_entry *h = NULL;
6198 r_symndx = ELF32_R_SYM (rel->r_info);
6199 if (r_symndx >= symtab_hdr->sh_info)
6201 /* External symbol. */
6202 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6203 while (h->root.type == bfd_link_hash_indirect
6204 || h->root.type == bfd_link_hash_warning)
6205 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6208 switch (ELF32_R_TYPE (rel->r_info))
6210 case R_NDS32_GOT_HI20:
6211 case R_NDS32_GOT_LO12:
6212 case R_NDS32_GOT_LO15:
6213 case R_NDS32_GOT_LO19:
6214 case R_NDS32_GOT17S2_RELA:
6215 case R_NDS32_GOT15S2_RELA:
6216 case R_NDS32_GOTOFF:
6217 case R_NDS32_GOTOFF_HI20:
6218 case R_NDS32_GOTOFF_LO12:
6219 case R_NDS32_GOTOFF_LO15:
6220 case R_NDS32_GOTOFF_LO19:
6222 case R_NDS32_GOTPC_HI20:
6223 case R_NDS32_GOTPC_LO12:
6224 case R_NDS32_GOTPC20:
6227 if (h->got.refcount > 0)
6232 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
6233 local_got_refcounts[r_symndx]--;
6237 case R_NDS32_16_RELA:
6238 case R_NDS32_20_RELA:
6239 case R_NDS32_5_RELA:
6240 case R_NDS32_32_RELA:
6241 case R_NDS32_HI20_RELA:
6242 case R_NDS32_LO12S3_RELA:
6243 case R_NDS32_LO12S2_RELA:
6244 case R_NDS32_LO12S2_DP_RELA:
6245 case R_NDS32_LO12S2_SP_RELA:
6246 case R_NDS32_LO12S1_RELA:
6247 case R_NDS32_LO12S0_RELA:
6248 case R_NDS32_LO12S0_ORI_RELA:
6249 case R_NDS32_SDA16S3_RELA:
6250 case R_NDS32_SDA17S2_RELA:
6251 case R_NDS32_SDA18S1_RELA:
6252 case R_NDS32_SDA19S0_RELA:
6253 case R_NDS32_SDA15S3_RELA:
6254 case R_NDS32_SDA15S2_RELA:
6255 case R_NDS32_SDA12S2_DP_RELA:
6256 case R_NDS32_SDA12S2_SP_RELA:
6257 case R_NDS32_SDA15S1_RELA:
6258 case R_NDS32_SDA15S0_RELA:
6259 case R_NDS32_SDA_FP7U2_RELA:
6260 case R_NDS32_15_PCREL_RELA:
6261 case R_NDS32_17_PCREL_RELA:
6262 case R_NDS32_25_PCREL_RELA:
6265 struct elf_nds32_link_hash_entry *eh;
6266 struct elf_nds32_dyn_relocs **pp;
6267 struct elf_nds32_dyn_relocs *p;
6269 if (!bfd_link_pic (info) && h->plt.refcount > 0)
6270 h->plt.refcount -= 1;
6272 eh = (struct elf_nds32_link_hash_entry *) h;
6274 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6277 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6278 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
6279 || ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA)
6289 case R_NDS32_9_PLTREL:
6290 case R_NDS32_25_PLTREL:
6293 if (h->plt.refcount > 0)
6306 /* Look through the relocs for a section during the first phase.
6307 Since we don't do .gots or .plts, we just need to consider the
6308 virtual table relocs for gc. */
6311 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6312 asection *sec, const Elf_Internal_Rela *relocs)
6314 Elf_Internal_Shdr *symtab_hdr;
6315 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6316 const Elf_Internal_Rela *rel;
6317 const Elf_Internal_Rela *rel_end;
6318 struct elf_nds32_link_hash_table *htab;
6320 asection *sreloc = NULL;
6322 if (bfd_link_relocatable (info))
6325 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6326 sym_hashes = elf_sym_hashes (abfd);
6328 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6329 if (!elf_bad_symtab (abfd))
6330 sym_hashes_end -= symtab_hdr->sh_info;
6332 htab = nds32_elf_hash_table (info);
6333 dynobj = htab->root.dynobj;
6335 rel_end = relocs + sec->reloc_count;
6336 for (rel = relocs; rel < rel_end; rel++)
6338 enum elf_nds32_reloc_type r_type;
6339 struct elf_link_hash_entry *h;
6340 unsigned long r_symndx;
6341 int tls_type, old_tls_type;
6343 r_symndx = ELF32_R_SYM (rel->r_info);
6344 r_type = ELF32_R_TYPE (rel->r_info);
6345 if (r_symndx < symtab_hdr->sh_info)
6349 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6350 while (h->root.type == bfd_link_hash_indirect
6351 || h->root.type == bfd_link_hash_warning)
6352 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6355 /* Some relocs require a global offset table. We create
6356 got section here, since these relocation need got section
6357 and it is not created yet. */
6358 if (htab->root.sgot == NULL)
6362 case R_NDS32_GOT_HI20:
6363 case R_NDS32_GOT_LO12:
6364 case R_NDS32_GOT_LO15:
6365 case R_NDS32_GOT_LO19:
6366 case R_NDS32_GOT17S2_RELA:
6367 case R_NDS32_GOT15S2_RELA:
6368 case R_NDS32_GOTOFF:
6369 case R_NDS32_GOTOFF_HI20:
6370 case R_NDS32_GOTOFF_LO12:
6371 case R_NDS32_GOTOFF_LO15:
6372 case R_NDS32_GOTOFF_LO19:
6373 case R_NDS32_GOTPC20:
6374 case R_NDS32_GOTPC_HI20:
6375 case R_NDS32_GOTPC_LO12:
6377 case R_NDS32_TLS_IE_HI20:
6378 case R_NDS32_TLS_IE_LO12S2:
6380 htab->root.dynobj = dynobj = abfd;
6381 if (!_bfd_elf_create_got_section (dynobj, info))
6390 switch ((int) r_type)
6392 case R_NDS32_GOT_HI20:
6393 case R_NDS32_GOT_LO12:
6394 case R_NDS32_GOT_LO15:
6395 case R_NDS32_GOT_LO19:
6397 case R_NDS32_TLS_IE_HI20:
6398 case R_NDS32_TLS_IE_LO12S2:
6401 case R_NDS32_TLS_IE_HI20:
6402 case R_NDS32_TLS_IE_LO12S2:
6403 tls_type = GOT_TLS_IE;
6406 tls_type = GOT_NORMAL;
6411 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
6412 h->got.refcount += 1;
6416 bfd_signed_vma *local_got_refcounts;
6418 /* This is a global offset table entry for a local
6420 local_got_refcounts = elf_local_got_refcounts (abfd);
6421 if (local_got_refcounts == NULL)
6425 size = symtab_hdr->sh_info;
6426 size *= sizeof (bfd_signed_vma);
6427 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
6428 if (local_got_refcounts == NULL)
6430 elf_local_got_refcounts (abfd) = local_got_refcounts;
6432 local_got_refcounts[r_symndx] += 1;
6433 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
6436 /* We will already have issued an error message if there
6437 is a TLS/non-TLS mismatch, based on the symbol
6438 type. So just combine any TLS types needed. */
6439 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
6440 && tls_type != GOT_NORMAL)
6441 tls_type |= old_tls_type;
6443 if (old_tls_type != tls_type)
6446 elf32_nds32_hash_entry (h)->tls_type = tls_type;
6448 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
6451 case R_NDS32_9_PLTREL:
6452 case R_NDS32_25_PLTREL:
6453 case R_NDS32_PLTREL_HI20:
6454 case R_NDS32_PLTREL_LO12:
6455 case R_NDS32_PLT_GOTREL_HI20:
6456 case R_NDS32_PLT_GOTREL_LO12:
6457 case R_NDS32_PLT_GOTREL_LO15:
6458 case R_NDS32_PLT_GOTREL_LO19:
6459 case R_NDS32_PLT_GOTREL_LO20:
6461 /* This symbol requires a procedure linkage table entry. We
6462 actually build the entry in adjust_dynamic_symbol,
6463 because this might be a case of linking PIC code without
6464 linking in any dynamic objects, in which case we don't
6465 need to generate a procedure linkage table after all. */
6467 /* If this is a local symbol, we resolve it directly without
6468 creating a procedure linkage table entry. */
6472 if (h->forced_local)
6475 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
6477 h->plt.refcount += 1;
6480 case R_NDS32_16_RELA:
6481 case R_NDS32_20_RELA:
6482 case R_NDS32_5_RELA:
6483 case R_NDS32_32_RELA:
6484 case R_NDS32_HI20_RELA:
6485 case R_NDS32_LO12S3_RELA:
6486 case R_NDS32_LO12S2_RELA:
6487 case R_NDS32_LO12S2_DP_RELA:
6488 case R_NDS32_LO12S2_SP_RELA:
6489 case R_NDS32_LO12S1_RELA:
6490 case R_NDS32_LO12S0_RELA:
6491 case R_NDS32_LO12S0_ORI_RELA:
6492 case R_NDS32_SDA16S3_RELA:
6493 case R_NDS32_SDA17S2_RELA:
6494 case R_NDS32_SDA18S1_RELA:
6495 case R_NDS32_SDA19S0_RELA:
6496 case R_NDS32_SDA15S3_RELA:
6497 case R_NDS32_SDA15S2_RELA:
6498 case R_NDS32_SDA12S2_DP_RELA:
6499 case R_NDS32_SDA12S2_SP_RELA:
6500 case R_NDS32_SDA15S1_RELA:
6501 case R_NDS32_SDA15S0_RELA:
6502 case R_NDS32_SDA_FP7U2_RELA:
6503 case R_NDS32_15_PCREL_RELA:
6504 case R_NDS32_17_PCREL_RELA:
6505 case R_NDS32_25_PCREL_RELA:
6507 if (h != NULL && !bfd_link_pic (info))
6510 h->plt.refcount += 1;
6513 /* If we are creating a shared library, and this is a reloc against
6514 a global symbol, or a non PC relative reloc against a local
6515 symbol, then we need to copy the reloc into the shared library.
6516 However, if we are linking with -Bsymbolic, we do not need to
6517 copy a reloc against a global symbol which is defined in an
6518 object we are including in the link (i.e., DEF_REGULAR is set).
6519 At this point we have not seen all the input files, so it is
6520 possible that DEF_REGULAR is not set now but will be set later
6521 (it is never cleared). We account for that possibility below by
6522 storing information in the dyn_relocs field of the hash table
6523 entry. A similar situation occurs when creating shared libraries
6524 and symbol visibility changes render the symbol local.
6526 If on the other hand, we are creating an executable, we may need
6527 to keep relocations for symbols satisfied by a dynamic library
6528 if we manage to avoid copy relocs for the symbol. */
6529 if ((bfd_link_pic (info)
6530 && (sec->flags & SEC_ALLOC) != 0
6531 && ((r_type != R_NDS32_25_PCREL_RELA
6532 && r_type != R_NDS32_15_PCREL_RELA
6533 && r_type != R_NDS32_17_PCREL_RELA
6534 && !(r_type == R_NDS32_32_RELA
6535 && strcmp (sec->name, ".eh_frame") == 0))
6538 || h->root.type == bfd_link_hash_defweak
6539 || !h->def_regular))))
6540 || (!bfd_link_pic (info)
6541 && (sec->flags & SEC_ALLOC) != 0
6543 && (h->root.type == bfd_link_hash_defweak
6544 || !h->def_regular)))
6546 struct elf_nds32_dyn_relocs *p;
6547 struct elf_nds32_dyn_relocs **head;
6550 htab->root.dynobj = dynobj = abfd;
6552 /* When creating a shared object, we must copy these
6553 relocs into the output file. We create a reloc
6554 section in dynobj and make room for the reloc. */
6559 name = bfd_elf_string_from_elf_section
6560 (abfd, elf_elfheader (abfd)->e_shstrndx,
6561 elf_section_data (sec)->rela.hdr->sh_name);
6565 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6566 && strcmp (bfd_get_section_name (abfd, sec),
6569 sreloc = bfd_get_section_by_name (dynobj, name);
6574 sreloc = bfd_make_section (dynobj, name);
6575 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6576 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6577 if ((sec->flags & SEC_ALLOC) != 0)
6578 flags |= SEC_ALLOC | SEC_LOAD;
6580 || !bfd_set_section_flags (dynobj, sreloc, flags)
6581 || !bfd_set_section_alignment (dynobj, sreloc, 2))
6584 elf_section_type (sreloc) = SHT_RELA;
6586 elf_section_data (sec)->sreloc = sreloc;
6589 /* If this is a global symbol, we count the number of
6590 relocations we need for this symbol. */
6592 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
6598 Elf_Internal_Sym *isym;
6599 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
6603 /* Track dynamic relocs needed for local syms too. */
6604 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6608 vpp = &elf_section_data (s)->local_dynrel;
6609 head = (struct elf_nds32_dyn_relocs **) vpp;
6613 if (p == NULL || p->sec != sec)
6615 bfd_size_type amt = sizeof (*p);
6616 p = (struct elf_nds32_dyn_relocs *) bfd_alloc (dynobj, amt);
6627 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
6628 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6629 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
6634 /* This relocation describes the C++ object vtable hierarchy.
6635 Reconstruct it for later use during GC. */
6636 case R_NDS32_RELA_GNU_VTINHERIT:
6637 case R_NDS32_GNU_VTINHERIT:
6638 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6642 /* This relocation describes which C++ vtable entries are actually
6643 used. Record for later use during GC. */
6644 case R_NDS32_GNU_VTENTRY:
6645 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6648 case R_NDS32_RELA_GNU_VTENTRY:
6649 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6658 /* Write VAL in uleb128 format to P, returning a pointer to the
6660 This code is copied from elf-attr.c. */
6663 write_uleb128 (bfd_byte *p, unsigned int val)
6678 static bfd_signed_vma
6679 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
6680 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
6681 int *pic_ext_target)
6683 bfd_signed_vma foff;
6684 bfd_vma symval, addend;
6687 /* Get the value of the symbol referred to by the reloc. */
6688 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6690 Elf_Internal_Sym *isym;
6692 /* A local symbol. */
6693 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6695 if (isym->st_shndx == SHN_UNDEF)
6696 sym_sec = bfd_und_section_ptr;
6697 else if (isym->st_shndx == SHN_ABS)
6698 sym_sec = bfd_abs_section_ptr;
6699 else if (isym->st_shndx == SHN_COMMON)
6700 sym_sec = bfd_com_section_ptr;
6702 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6703 symval = isym->st_value + sym_sec->output_section->vma
6704 + sym_sec->output_offset;
6709 struct elf_link_hash_entry *h;
6712 /* An external symbol. */
6713 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6714 h = elf_sym_hashes (abfd)[indx];
6715 BFD_ASSERT (h != NULL);
6717 if (h->root.type != bfd_link_hash_defined
6718 && h->root.type != bfd_link_hash_defweak)
6719 /* This appears to be a reference to an undefined
6720 symbol. Just ignore it--it will be caught by the
6721 regular reloc processing. */
6723 owner = h->root.u.def.section->owner;
6724 if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
6725 *pic_ext_target = 1;
6727 if (h->root.u.def.section->flags & SEC_MERGE)
6729 sym_sec = h->root.u.def.section;
6730 symval = _bfd_merged_section_offset (abfd, &sym_sec,
6731 elf_section_data (sym_sec)->sec_info,
6732 h->root.u.def.value);
6733 symval = symval + sym_sec->output_section->vma
6734 + sym_sec->output_offset;
6737 symval = (h->root.u.def.value
6738 + h->root.u.def.section->output_section->vma
6739 + h->root.u.def.section->output_offset);
6742 addend = irel->r_addend;
6744 foff = (symval + addend
6745 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
6750 calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
6751 Elf_Internal_Sym *isymbuf,
6752 Elf_Internal_Rela *irel,
6753 Elf_Internal_Shdr *symtab_hdr)
6757 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6759 Elf_Internal_Sym *isym;
6761 /* A local symbol. */
6762 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6764 if (isym->st_shndx == SHN_UNDEF)
6765 sym_sec = bfd_und_section_ptr;
6766 else if (isym->st_shndx == SHN_ABS)
6767 sym_sec = bfd_abs_section_ptr;
6768 else if (isym->st_shndx == SHN_COMMON)
6769 sym_sec = bfd_com_section_ptr;
6771 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6772 symval = isym->st_value + sym_sec->output_section->vma
6773 + sym_sec->output_offset;
6778 struct elf_link_hash_entry *h;
6779 struct elf_nds32_link_hash_table *htab;
6782 /* An external symbol. */
6783 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6784 h = elf_sym_hashes (abfd)[indx];
6785 BFD_ASSERT (h != NULL);
6786 htab = nds32_elf_hash_table (link_info);
6787 splt = htab->root.splt;
6789 while (h->root.type == bfd_link_hash_indirect
6790 || h->root.type == bfd_link_hash_warning)
6791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6793 if (h->plt.offset == (bfd_vma) - 1)
6795 if (h->root.type != bfd_link_hash_defined
6796 && h->root.type != bfd_link_hash_defweak)
6797 /* This appears to be a reference to an undefined
6798 * symbol. Just ignore it--it will be caught by the
6799 * regular reloc processing. */
6801 symval = (h->root.u.def.value
6802 + h->root.u.def.section->output_section->vma
6803 + h->root.u.def.section->output_offset);
6806 symval = splt->output_section->vma + h->plt.offset;
6812 static bfd_signed_vma
6813 calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
6814 Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
6815 Elf_Internal_Shdr *symtab_hdr)
6818 if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
6822 return foff - (irel->r_offset
6823 + sec->output_section->vma + sec->output_offset);
6826 /* Convert a 32-bit instruction to 16-bit one.
6827 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
6828 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
6829 type of INSN16. Return 1 if successful. */
6832 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6835 uint16_t insn16 = 0;
6837 unsigned long mach = bfd_get_mach (abfd);
6839 if (N32_SH5 (insn) != 0)
6842 switch (N32_SUB5 (insn))
6844 case N32_ALU1_ADD_SLLI:
6845 case N32_ALU1_ADD_SRLI:
6846 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6848 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
6850 insn_type = NDS32_INSN_ADD333;
6852 else if (N32_IS_RT4 (insn))
6854 if (N32_RT5 (insn) == N32_RA5 (insn))
6855 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
6856 else if (N32_RT5 (insn) == N32_RB5 (insn))
6857 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
6858 insn_type = NDS32_INSN_ADD45;
6862 case N32_ALU1_SUB_SLLI:
6863 case N32_ALU1_SUB_SRLI:
6864 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6866 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
6868 insn_type = NDS32_INSN_SUB333;
6870 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6872 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
6873 insn_type = NDS32_INSN_SUB45;
6877 case N32_ALU1_AND_SLLI:
6878 case N32_ALU1_AND_SRLI:
6879 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
6880 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6881 && N32_IS_RB3 (insn))
6883 if (N32_RT5 (insn) == N32_RA5 (insn))
6884 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
6885 else if (N32_RT5 (insn) == N32_RB5 (insn))
6886 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
6888 insn_type = NDS32_INSN_AND33;
6892 case N32_ALU1_XOR_SLLI:
6893 case N32_ALU1_XOR_SRLI:
6894 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
6895 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6896 && N32_IS_RB3 (insn))
6898 if (N32_RT5 (insn) == N32_RA5 (insn))
6899 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
6900 else if (N32_RT5 (insn) == N32_RB5 (insn))
6901 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
6903 insn_type = NDS32_INSN_XOR33;
6907 case N32_ALU1_OR_SLLI:
6908 case N32_ALU1_OR_SRLI:
6909 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
6910 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6911 && N32_IS_RB3 (insn))
6913 if (N32_RT5 (insn) == N32_RA5 (insn))
6914 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
6915 else if (N32_RT5 (insn) == N32_RB5 (insn))
6916 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
6918 insn_type = NDS32_INSN_OR33;
6922 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
6923 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
6924 && N32_RA5 (insn) == N32_RB5 (insn))
6926 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
6927 insn_type = NDS32_INSN_NOT33;
6931 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6933 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
6934 insn_type = NDS32_INSN_SRAI45;
6939 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6941 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
6942 insn_type = NDS32_INSN_SRLI45;
6947 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
6949 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
6951 insn_type = NDS32_INSN_SLLI333;
6956 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6958 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
6959 insn_type = NDS32_INSN_ZEH33;
6964 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6966 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
6967 insn_type = NDS32_INSN_SEB33;
6972 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6974 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
6975 insn_type = NDS32_INSN_SEH33;
6980 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6983 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
6984 insn_type = NDS32_INSN_SLT45;
6989 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6992 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
6993 insn_type = NDS32_INSN_SLTS45;
6998 if ((insn16 & 0x8000) == 0)
7004 *pinsn_type = insn_type;
7009 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7012 uint16_t insn16 = 0;
7014 unsigned long mach = bfd_get_mach (abfd);
7016 /* TODO: bset, bclr, btgl, btst. */
7017 if (__GF (insn, 6, 4) != 0)
7020 switch (N32_IMMU (insn, 6))
7023 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7024 && N32_IS_RB3 (insn))
7026 if (N32_RT5 (insn) == N32_RA5 (insn))
7027 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7028 else if (N32_RT5 (insn) == N32_RB5 (insn))
7029 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7031 insn_type = NDS32_INSN_MUL33;
7035 if ((insn16 & 0x8000) == 0)
7041 *pinsn_type = insn_type;
7046 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7050 uint16_t insn16 = 0;
7052 unsigned long mach = bfd_get_mach (abfd);
7054 /* Decode 32-bit instruction. */
7055 if (insn & 0x80000000)
7057 /* Not 32-bit insn. */
7061 op6 = N32_OP6 (insn);
7063 /* Convert it to 16-bit instruction. */
7067 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7069 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7070 insn_type = NDS32_INSN_MOVI55;
7072 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7073 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7075 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7076 N32_IMM20S (insn) - 16);
7077 insn_type = NDS32_INSN_MOVPI45;
7082 if (N32_IMM15S (insn) == 0)
7084 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7085 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7087 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7089 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7090 insn_type = NDS32_INSN_MOV55;
7093 else if (N32_IMM15S (insn) > 0)
7095 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7097 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7099 insn_type = NDS32_INSN_ADDI333;
7101 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7102 && N32_IMM15S (insn) < 32)
7104 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7105 insn_type = NDS32_INSN_ADDI45;
7107 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7108 && N32_RT5 (insn) == N32_RA5 (insn)
7109 && N32_IMM15S (insn) < 512)
7111 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7112 insn_type = NDS32_INSN_ADDI10_SP;
7114 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7115 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7116 && (N32_IMM15S (insn) % 4 == 0))
7118 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7119 N32_IMM15S (insn) >> 2);
7120 insn_type = NDS32_INSN_ADDRI36_SP;
7126 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7128 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7129 0 - N32_IMM15S (insn));
7130 insn_type = NDS32_INSN_SUBI333;
7132 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7133 && N32_IMM15S (insn) > -32)
7135 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7136 0 - N32_IMM15S (insn));
7137 insn_type = NDS32_INSN_SUBI45;
7139 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7140 && N32_RT5 (insn) == N32_RA5 (insn)
7141 && N32_IMM15S (insn) >= -512)
7143 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7144 insn_type = NDS32_INSN_ADDI10_SP;
7150 if (N32_IMM15S (insn) == 0)
7152 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7153 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7155 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7157 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7158 insn_type = NDS32_INSN_MOV55;
7164 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7165 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7167 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7168 insn_type = NDS32_INSN_NEG33;
7173 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7175 if (N32_IMM15U (insn) == 1)
7177 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7178 insn_type = NDS32_INSN_XLSB33;
7180 else if (N32_IMM15U (insn) == 0x7ff)
7182 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7183 insn_type = NDS32_INSN_X11B33;
7185 else if (N32_IMM15U (insn) == 0xff)
7187 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7188 insn_type = NDS32_INSN_ZEB33;
7190 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7191 && N32_IMM15U (insn) < 256)
7193 int imm15u = N32_IMM15U (insn);
7195 if (__builtin_popcount (imm15u) == 1)
7198 int imm3u = __builtin_ctz (imm15u);
7200 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7201 insn_type = NDS32_INSN_BMSKI33;
7203 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7206 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7208 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7209 insn_type = NDS32_INSN_FEXTI33;
7216 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7217 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7219 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7220 insn_type = NDS32_INSN_SLTI45;
7225 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7226 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7228 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7229 insn_type = NDS32_INSN_SLTSI45;
7234 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7236 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7237 insn_type = NDS32_INSN_LWI450;
7239 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7240 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7242 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7244 insn_type = NDS32_INSN_LWI333;
7246 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7247 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7249 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7250 insn_type = NDS32_INSN_LWI37;
7252 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7253 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7255 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7256 insn_type = NDS32_INSN_LWI37_SP;
7258 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7259 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7261 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7262 N32_IMM15S (insn) + 32);
7263 insn_type = NDS32_INSN_LWI45_FE;
7268 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7270 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7271 insn_type = NDS32_INSN_SWI450;
7273 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7274 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7276 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7278 insn_type = NDS32_INSN_SWI333;
7280 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7281 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7283 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7284 insn_type = NDS32_INSN_SWI37;
7286 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7287 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7289 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7290 insn_type = NDS32_INSN_SWI37_SP;
7294 case N32_OP6_LWI_BI:
7295 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7296 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7298 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7300 insn_type = NDS32_INSN_LWI333_BI;
7304 case N32_OP6_SWI_BI:
7305 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7306 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7308 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7310 insn_type = NDS32_INSN_SWI333_BI;
7315 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7316 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7318 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7320 insn_type = NDS32_INSN_LHI333;
7325 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7326 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7328 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
7330 insn_type = NDS32_INSN_SHI333;
7335 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7336 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7338 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
7340 insn_type = NDS32_INSN_LBI333;
7345 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7346 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7348 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
7350 insn_type = NDS32_INSN_SBI333;
7355 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
7358 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
7361 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
7364 if ((insn & __BIT (14)) == 0)
7367 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7368 && N32_RT5 (insn) != REG_R5)
7369 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
7370 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7371 && N32_RA5 (insn) != REG_R5)
7372 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
7373 insn_type = NDS32_INSN_BEQS38;
7379 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7380 && N32_RT5 (insn) != REG_R5)
7381 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
7382 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7383 && N32_RA5 (insn) != REG_R5)
7384 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
7385 insn_type = NDS32_INSN_BNES38;
7391 switch (N32_BR2_SUB (insn))
7394 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7396 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
7397 insn_type = NDS32_INSN_BEQZ38;
7399 else if (N32_RT5 (insn) == REG_R15
7400 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7402 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
7403 insn_type = NDS32_INSN_BEQZS8;
7408 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7410 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
7411 insn_type = NDS32_INSN_BNEZ38;
7413 else if (N32_RT5 (insn) == REG_R15
7414 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7416 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
7417 insn_type = NDS32_INSN_BNEZS8;
7421 case N32_BR2_IFCALL:
7422 if (IS_WITHIN_U (N32_IMM16S (insn), 9))
7424 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
7425 insn_type = NDS32_INSN_IFCALL9;
7432 if ((insn & __BIT (24)) == 0)
7435 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
7437 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
7438 insn_type = NDS32_INSN_J8;
7444 if (__GF (insn, 8, 2) != 0)
7447 switch (N32_IMMU (insn, 5))
7450 if (N32_JREG_HINT (insn) == 0)
7453 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
7454 insn_type = NDS32_INSN_JR5;
7456 else if (N32_JREG_HINT (insn) == 1)
7459 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
7460 insn_type = NDS32_INSN_RET5;
7462 else if (N32_JREG_HINT (insn) == 3)
7464 /* ifret = mov55 $sp, $sp */
7465 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
7466 insn_type = NDS32_INSN_IFRET;
7471 /* It's convertible when return rt5 is $lp and address
7472 translation is kept. */
7473 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
7475 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
7476 insn_type = NDS32_INSN_JRAL5;
7483 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
7485 /* For v3, swid above 31 are used for ex9.it. */
7486 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
7487 insn_type = NDS32_INSN_BREAK16;
7492 /* This instruction has no 16-bit variant. */
7497 /* Bit-15 of insn16 should be set for a valid instruction. */
7498 if ((insn16 & 0x8000) == 0)
7504 *pinsn_type = insn_type;
7509 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
7510 Elf_Internal_Rela *reloc)
7512 uint16_t insn16 = 0;
7514 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
7515 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
7518 if (!N32_IS_RT3 (insn))
7521 switch (N32_OP6 (insn))
7524 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7525 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7528 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7529 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7532 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
7535 if (__GF (insn, 17, 3) == 6)
7536 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
7537 else if (__GF (insn, 17, 3) == 7)
7538 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
7542 if ((insn16 & 0x8000) == 0)
7549 /* Convert a 16-bit instruction to 32-bit one.
7550 INSN16 it the input and PINSN it the point to output.
7551 Return non-zero on successful. Otherwise 0 is returned. */
7554 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
7556 uint32_t insn = 0xffffffff;
7557 unsigned long mach = bfd_get_mach (abfd);
7559 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
7561 switch (__GF (insn16, 9, 6))
7563 case 0x4: /* add45 */
7564 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
7567 case 0x5: /* sub45 */
7568 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
7571 case 0x6: /* addi45 */
7572 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7573 N16_IMM5U (insn16));
7575 case 0x7: /* subi45 */
7576 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7577 -N16_IMM5U (insn16));
7579 case 0x8: /* srai45 */
7580 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
7581 N16_IMM5U (insn16));
7583 case 0x9: /* srli45 */
7584 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
7585 N16_IMM5U (insn16));
7587 case 0xa: /* slli333 */
7588 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
7589 N16_IMM3U (insn16));
7591 case 0xc: /* add333 */
7592 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
7595 case 0xd: /* sub333 */
7596 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
7599 case 0xe: /* addi333 */
7600 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7601 N16_IMM3U (insn16));
7603 case 0xf: /* subi333 */
7604 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7605 -N16_IMM3U (insn16));
7607 case 0x10: /* lwi333 */
7608 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
7609 N16_IMM3U (insn16));
7611 case 0x12: /* lhi333 */
7612 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
7613 N16_IMM3U (insn16));
7615 case 0x13: /* lbi333 */
7616 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
7617 N16_IMM3U (insn16));
7619 case 0x11: /* lwi333.bi */
7620 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7621 N16_IMM3U (insn16));
7623 case 0x14: /* swi333 */
7624 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
7625 N16_IMM3U (insn16));
7627 case 0x16: /* shi333 */
7628 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
7629 N16_IMM3U (insn16));
7631 case 0x17: /* sbi333 */
7632 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
7633 N16_IMM3U (insn16));
7635 case 0x15: /* swi333.bi */
7636 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7637 N16_IMM3U (insn16));
7639 case 0x18: /* addri36.sp */
7640 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
7641 N16_IMM6U (insn16) << 2);
7643 case 0x19: /* lwi45.fe */
7644 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
7645 (N16_IMM5U (insn16) - 32));
7647 case 0x1a: /* lwi450 */
7648 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7650 case 0x1b: /* swi450 */
7651 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7654 /* These are r15 implied instructions. */
7655 case 0x30: /* slts45 */
7656 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7658 case 0x31: /* slt45 */
7659 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7661 case 0x32: /* sltsi45 */
7662 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7664 case 0x33: /* slti45 */
7665 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7667 case 0x34: /* beqzs8, bnezs8 */
7668 if (insn16 & __BIT (8))
7669 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
7671 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
7674 case 0x35: /* break16, ex9.it */
7675 /* Only consider range of v3 break16. */
7676 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
7679 case 0x3c: /* ifcall9 */
7680 insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
7682 case 0x3d: /* movpi45 */
7683 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
7686 case 0x3f: /* MISC33 */
7687 switch (insn16 & 0x7)
7690 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7693 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
7697 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
7701 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
7705 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
7709 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
7716 switch (insn16 & 0x7)
7719 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
7722 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7725 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7728 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7730 case 4: /* xlsb33 */
7731 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
7733 case 5: /* x11b33 */
7734 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
7736 case 6: /* bmski33 */
7737 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7738 1 << __GF (insn16, 3, 3));
7740 case 7: /* fexti33 */
7741 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7742 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
7748 switch (__GF (insn16, 10, 5))
7750 case 0x0: /* mov55 or ifret16 */
7751 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
7752 && N16_RT5 (insn16) == N16_RA5 (insn16))
7753 insn = N32_JREG (JR, 0, 0, 0, 3);
7755 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
7757 case 0x1: /* movi55 */
7758 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
7760 case 0x1b: /* addi10s (V2) */
7761 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
7765 switch (__GF (insn16, 11, 4))
7767 case 0x7: /* lwi37.fp/swi37.fp */
7768 if (insn16 & __BIT (7)) /* swi37.fp */
7769 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7771 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7773 case 0x8: /* beqz38 */
7774 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7776 case 0x9: /* bnez38 */
7777 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7779 case 0xa: /* beqs38/j8, implied r5 */
7780 if (N16_RT38 (insn16) == 5)
7781 insn = N32_JI (J, N16_IMM8S (insn16));
7783 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7785 case 0xb: /* bnes38 and others */
7786 if (N16_RT38 (insn16) == 5)
7788 switch (__GF (insn16, 5, 3))
7791 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
7794 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
7797 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
7799 case 2: /* ex9.it imm5 */
7800 /* ex9.it had no 32-bit variantl. */
7802 case 5: /* add5.pc */
7803 /* add5.pc had no 32-bit variantl. */
7808 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7810 case 0xe: /* lwi37/swi37 */
7811 if (insn16 & (1 << 7)) /* swi37.sp */
7812 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7814 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7819 if (insn & 0x80000000)
7828 is_sda_access_insn (unsigned long insn)
7830 switch (N32_OP6 (insn))
7851 static unsigned long
7852 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
7858 case R_NDS32_GOT_LO12:
7859 case R_NDS32_GOTOFF_LO12:
7860 case R_NDS32_PLTREL_LO12:
7861 case R_NDS32_PLT_GOTREL_LO12:
7862 case R_NDS32_LO12S0_RELA:
7863 switch (N32_OP6 (insn))
7867 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
7871 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
7875 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
7879 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
7884 case R_NDS32_LO12S1_RELA:
7885 switch (N32_OP6 (insn))
7889 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
7893 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
7897 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
7902 case R_NDS32_LO12S2_RELA:
7903 switch (N32_OP6 (insn))
7907 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
7911 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
7916 case R_NDS32_LO12S2_DP_RELA:
7917 case R_NDS32_LO12S2_SP_RELA:
7918 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
7928 /* Linker hasn't found the correct merge section for non-section symbol
7929 in relax time, this work is left to the function elf_link_input_bfd().
7930 So for non-section symbol, _bfd_merged_section_offset is also needed
7931 to find the correct symbol address. */
7934 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
7935 asection **psec, Elf_Internal_Rela *rel)
7937 asection *sec = *psec;
7940 relocation = (sec->output_section->vma
7941 + sec->output_offset + sym->st_value);
7942 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
7944 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7946 _bfd_merged_section_offset (abfd, psec,
7947 elf_section_data (sec)->sec_info,
7948 sym->st_value + rel->r_addend);
7951 _bfd_merged_section_offset (abfd, psec,
7952 elf_section_data (sec)->sec_info,
7953 sym->st_value) + rel->r_addend;
7957 /* If we have changed the section, and our original section is
7958 marked with SEC_EXCLUDE, it means that the original
7959 SEC_MERGE section has been completely subsumed in some
7960 other SEC_MERGE section. In this case, we need to leave
7961 some info around for --emit-relocs. */
7962 if ((sec->flags & SEC_EXCLUDE) != 0)
7963 sec->kept_section = *psec;
7966 rel->r_addend -= relocation;
7967 rel->r_addend += sec->output_section->vma + sec->output_offset;
7973 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
7974 Elf_Internal_Sym *isymbuf,
7975 Elf_Internal_Shdr *symtab_hdr)
7977 bfd_signed_vma foff;
7978 bfd_vma symval, addend;
7979 Elf_Internal_Rela irel_fn;
7980 Elf_Internal_Sym *isym;
7983 /* Get the value of the symbol referred to by the reloc. */
7984 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7986 /* A local symbol. */
7987 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7989 if (isym->st_shndx == SHN_UNDEF)
7990 sym_sec = bfd_und_section_ptr;
7991 else if (isym->st_shndx == SHN_ABS)
7992 sym_sec = bfd_abs_section_ptr;
7993 else if (isym->st_shndx == SHN_COMMON)
7994 sym_sec = bfd_com_section_ptr;
7996 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7997 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
7998 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
7999 addend = irel_fn.r_addend;
8004 struct elf_link_hash_entry *h;
8006 /* An external symbol. */
8007 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8008 h = elf_sym_hashes (abfd)[indx];
8009 BFD_ASSERT (h != NULL);
8011 while (h->root.type == bfd_link_hash_indirect
8012 || h->root.type == bfd_link_hash_warning)
8013 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8015 if (h->root.type != bfd_link_hash_defined
8016 && h->root.type != bfd_link_hash_defweak)
8017 /* This appears to be a reference to an undefined
8018 symbol. Just ignore it--it will be caught by the
8019 regular reloc processing. */
8022 if (h->root.u.def.section->flags & SEC_MERGE)
8024 sym_sec = h->root.u.def.section;
8025 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8026 (sym_sec)->sec_info, h->root.u.def.value);
8027 symval = symval + sym_sec->output_section->vma
8028 + sym_sec->output_offset;
8031 symval = (h->root.u.def.value
8032 + h->root.u.def.section->output_section->vma
8033 + h->root.u.def.section->output_offset);
8034 addend = irel->r_addend;
8037 foff = symval + addend;
8043 calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
8044 Elf_Internal_Rela *irel,
8045 Elf_Internal_Shdr *symtab_hdr)
8048 bfd_vma *local_got_offsets;
8049 /* Get the value of the symbol referred to by the reloc. */
8050 struct elf_link_hash_entry *h;
8051 struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);
8053 /* An external symbol. */
8054 symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8055 h = elf_sym_hashes (abfd)[symndx];
8056 while (h->root.type == bfd_link_hash_indirect
8057 || h->root.type == bfd_link_hash_warning)
8058 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8062 BFD_ASSERT (h != NULL);
8063 return (htab->root.sgot->output_section->vma
8064 + htab->root.sgot->output_offset
8069 local_got_offsets = elf_local_got_offsets (abfd);
8070 BFD_ASSERT (local_got_offsets != NULL);
8071 return (htab->root.sgot->output_section->vma
8072 + htab->root.sgot->output_offset
8073 + local_got_offsets[ELF32_R_SYM (irel->r_info)]);
8076 /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?). */
8077 /* The check of h->root.type is passed. */
8081 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8082 asection *sec, Elf_Internal_Rela *rel)
8085 unsigned short insn16;
8087 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8089 contents = elf_section_data (sec)->this_hdr.contents;
8090 insn16 = bfd_getb16 (contents + rel->r_offset);
8091 if (insn16 == NDS32_NOP16)
8096 /* It checks whether the instruction could be converted to
8097 16-bit form and returns the converted one.
8099 `internal_relocs' is supposed to be sorted. */
8102 is_convert_32_to_16 (bfd *abfd, asection *sec,
8103 Elf_Internal_Rela *reloc,
8104 Elf_Internal_Rela *internal_relocs,
8105 Elf_Internal_Rela *irelend,
8108 #define NORMAL_32_TO_16 (1 << 0)
8109 #define SPECIAL_32_TO_16 (1 << 1)
8110 bfd_byte *contents = NULL;
8114 Elf_Internal_Rela *pc_rel;
8115 int pic_ext_target = 0;
8116 Elf_Internal_Shdr *symtab_hdr;
8117 Elf_Internal_Sym *isymbuf = NULL;
8121 if (reloc->r_offset + 4 > sec->size)
8124 offset = reloc->r_offset;
8126 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
8128 insn = bfd_getb32 (contents + offset);
8130 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8131 convert_type = NORMAL_32_TO_16;
8132 else if (special_convert_32_to_16 (insn, insn16, reloc))
8133 convert_type = SPECIAL_32_TO_16;
8137 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8138 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8141 /* Find the first relocation of the same relocation-type,
8142 so we iteratie them forward. */
8144 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8147 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8149 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8150 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8151 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8152 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8154 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
8156 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8161 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8163 /* movi => movi55 */
8164 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8166 /* mem_addr is unsigned, but the value should
8167 be between [-16, 15]. */
8168 if ((mem_addr + 0x10) >> 5)
8172 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8173 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8175 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8176 because it can be relaxed to addi for TLS_LE_ADD. */
8179 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8180 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8181 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8182 && convert_type == SPECIAL_32_TO_16)
8185 We've selected a best fp-base for this access, so we can
8186 always resolve it anyway. Do nothing. */
8189 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8190 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8191 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8192 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8193 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8194 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8196 /* Prevent unresolved addi instruction translate
8197 to addi45 or addi333. */
8200 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8202 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
8204 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8214 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8215 Elf_Internal_Rela *reloc,
8216 Elf_Internal_Rela *internal_relocs,
8217 Elf_Internal_Rela *irelend,
8218 unsigned short insn16)
8220 Elf_Internal_Rela *pc_rel;
8223 offset = reloc->r_offset;
8224 bfd_putb16 (insn16, contents + offset);
8225 /* Find the first relocation of the same relocation-type,
8226 so we iteratie them forward. */
8228 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8231 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8233 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8234 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8235 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8238 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8240 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8242 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8243 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8245 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8246 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8247 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8249 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8250 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8252 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8256 /* Find a relocation of type specified by `reloc_type'
8257 of the same r_offset with reloc.
8258 If not found, return irelend.
8260 Assuming relocations are sorted by r_offset,
8261 we find the relocation from `reloc' backward untill relocs,
8262 or find it from `reloc' forward untill irelend. */
8264 static Elf_Internal_Rela *
8265 find_relocs_at_address (Elf_Internal_Rela *reloc,
8266 Elf_Internal_Rela *relocs,
8267 Elf_Internal_Rela *irelend,
8268 enum elf_nds32_reloc_type reloc_type)
8270 Elf_Internal_Rela *rel_t;
8272 /* Find backward. */
8274 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8276 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8279 /* We didn't find it backward. Try find it forward. */
8281 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8283 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8289 /* Find a relocation of specified type and offset.
8290 `reloc' is just a refence point to find a relocation at specified offset.
8291 If not found, return irelend.
8293 Assuming relocations are sorted by r_offset,
8294 we find the relocation from `reloc' backward untill relocs,
8295 or find it from `reloc' forward untill irelend. */
8297 static Elf_Internal_Rela *
8298 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8299 Elf_Internal_Rela *relocs,
8300 Elf_Internal_Rela *irelend,
8301 enum elf_nds32_reloc_type reloc_type,
8304 Elf_Internal_Rela *rel_t = NULL;
8306 /* First, we try to find a relocation of offset `offset_p',
8307 and then we use find_relocs_at_address to find specific type. */
8309 if (reloc->r_offset > offset_p)
8311 /* Find backward. */
8313 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8316 else if (reloc->r_offset < offset_p)
8320 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8327 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8330 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8334 nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
8335 Elf_Internal_Rela *internal_relocs,
8336 Elf_Internal_Rela *irelend,
8337 unsigned char reloc_type)
8339 Elf_Internal_Rela *rel_t;
8342 rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
8344 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8346 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8347 && rel_t->r_addend == reloc->r_addend)
8352 for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8354 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8356 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8357 && rel_t->r_addend == reloc->r_addend)
8365 typedef struct nds32_elf_blank nds32_elf_blank_t;
8366 struct nds32_elf_blank
8368 /* Where the blank begins. */
8370 /* The size of the blank. */
8372 /* The accumulative size before this blank. */
8374 nds32_elf_blank_t *next;
8375 nds32_elf_blank_t *prev;
8378 static nds32_elf_blank_t *blank_free_list = NULL;
8380 static nds32_elf_blank_t *
8381 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8383 nds32_elf_blank_t *blank_t;
8385 if (blank_free_list)
8387 blank_t = blank_free_list;
8388 blank_free_list = blank_free_list->next;
8391 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
8393 if (blank_t == NULL)
8396 blank_t->offset = offset_p;
8397 blank_t->size = size_p;
8398 blank_t->total_size = 0;
8399 blank_t->next = NULL;
8400 blank_t->prev = NULL;
8406 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
8408 if (blank_free_list)
8410 blank_free_list->prev = blank_p;
8411 blank_p->next = blank_free_list;
8414 blank_p->next = NULL;
8416 blank_p->prev = NULL;
8417 blank_free_list = blank_p;
8421 clean_nds32_elf_blank (void)
8423 nds32_elf_blank_t *blank_t;
8425 while (blank_free_list)
8427 blank_t = blank_free_list;
8428 blank_free_list = blank_free_list->next;
8433 static nds32_elf_blank_t *
8434 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
8436 nds32_elf_blank_t *blank_t;
8442 while (blank_t && addr < blank_t->offset)
8443 blank_t = blank_t->prev;
8444 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
8445 blank_t = blank_t->next;
8451 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8454 nds32_elf_blank_t *blank_t;
8456 blank_t = search_nds32_elf_blank (*blank_p, addr);
8463 if (addr < blank_t->offset + blank_t->size)
8464 return blank_t->total_size + (addr - blank_t->offset);
8466 return blank_t->total_size + blank_t->size;
8470 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
8472 nds32_elf_blank_t *blank_t, *blank_t2;
8476 *blank_p = create_nds32_elf_blank (addr, len);
8477 return *blank_p ? TRUE : FALSE;
8480 blank_t = search_nds32_elf_blank (*blank_p, addr);
8482 if (blank_t == NULL)
8484 blank_t = create_nds32_elf_blank (addr, len);
8487 while ((*blank_p)->prev != NULL)
8488 *blank_p = (*blank_p)->prev;
8489 blank_t->next = *blank_p;
8490 (*blank_p)->prev = blank_t;
8491 (*blank_p) = blank_t;
8495 if (addr < blank_t->offset + blank_t->size)
8497 if (addr > blank_t->offset + blank_t->size)
8498 blank_t->size = addr - blank_t->offset;
8502 blank_t2 = create_nds32_elf_blank (addr, len);
8507 blank_t->next->prev = blank_t2;
8508 blank_t2->next = blank_t->next;
8510 blank_t2->prev = blank_t;
8511 blank_t->next = blank_t2;
8512 *blank_p = blank_t2;
8519 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8522 nds32_elf_blank_t *blank_t;
8524 if (!insert_nds32_elf_blank (blank_p, addr, len))
8531 blank_t->total_size = 0;
8532 blank_t = blank_t->next;
8537 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
8538 blank_t = blank_t->next;
8545 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
8547 nds32_elf_blank_t *blank_t;
8548 bfd_vma total_size = 0;
8554 while (blank_t->prev)
8555 blank_t = blank_t->prev;
8558 blank_t->total_size = total_size;
8559 total_size += blank_t->size;
8560 blank_t = blank_t->next;
8565 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
8566 nds32_elf_blank_t *blank_p)
8568 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
8569 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
8570 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
8571 unsigned int sec_shndx; /* The section the be relaxed. */
8572 bfd_byte *contents; /* Contents data of iterating section. */
8573 Elf_Internal_Rela *internal_relocs;
8574 Elf_Internal_Rela *irel;
8575 Elf_Internal_Rela *irelend;
8576 struct elf_link_hash_entry **sym_hashes;
8577 struct elf_link_hash_entry **end_hashes;
8578 unsigned int symcount;
8580 nds32_elf_blank_t *blank_t;
8581 nds32_elf_blank_t *blank_t2;
8582 nds32_elf_blank_t *blank_head;
8584 blank_head = blank_t = blank_p;
8585 while (blank_head->prev != NULL)
8586 blank_head = blank_head->prev;
8587 while (blank_t->next != NULL)
8588 blank_t = blank_t->next;
8590 if (blank_t->offset + blank_t->size <= sec->size)
8592 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
8593 blank_t->next->prev = blank_t;
8595 if (blank_head->offset > 0)
8597 blank_head->prev = create_nds32_elf_blank (0, 0);
8598 blank_head->prev->next = blank_head;
8599 blank_head = blank_head->prev;
8602 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8604 /* The deletion must stop at the next ALIGN reloc for an alignment
8605 power larger than the number of bytes we are deleting. */
8607 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8608 if (!nds32_get_local_syms (abfd, sec, &isym))
8613 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8614 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
8615 symtab_hdr->contents = (bfd_byte *) isym;
8618 if (isym == NULL || symtab_hdr->sh_info == 0)
8621 blank_t = blank_head;
8622 calc_nds32_blank_total (blank_head);
8624 for (sect = abfd->sections; sect != NULL; sect = sect->next)
8626 /* Adjust all the relocs. */
8628 /* Relocations MUST be kept in memory, because relaxation adjust them. */
8629 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
8630 TRUE /* keep_memory */);
8631 irelend = internal_relocs + sect->reloc_count;
8633 blank_t = blank_head;
8634 blank_t2 = blank_head;
8636 if (!(sect->flags & SEC_RELOC))
8639 nds32_get_section_contents (abfd, sect, &contents, TRUE);
8641 for (irel = internal_relocs; irel < irelend; irel++)
8645 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
8646 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
8647 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8649 unsigned long val = 0;
8651 long before, between;
8654 switch (ELF32_R_TYPE (irel->r_info))
8657 offset = bfd_get_8 (abfd, contents + irel->r_offset);
8659 case R_NDS32_DIFF16:
8660 offset = bfd_get_16 (abfd, contents + irel->r_offset);
8662 case R_NDS32_DIFF32:
8663 val = bfd_get_32 (abfd, contents + irel->r_offset);
8664 /* Get the signed bit and mask for the high part. The
8665 gcc will alarm when right shift 32-bit since the
8666 type size of long may be 32-bit. */
8667 mask = 0 - (val >> 31);
8669 offset = (val | (mask - 0xffffffff));
8678 0 |encoded in location|
8679 |------------|-------------------|---------
8681 -- before ---| *****************
8682 --------------------- between ---|
8684 We only care how much data are relax between DIFF,
8687 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8688 between = get_nds32_elf_blank_total (&blank_t,
8689 irel->r_addend + offset, 0);
8690 if (between == before)
8691 goto done_adjust_diff;
8693 switch (ELF32_R_TYPE (irel->r_info))
8696 bfd_put_8 (abfd, offset - (between - before),
8697 contents + irel->r_offset);
8699 case R_NDS32_DIFF16:
8700 bfd_put_16 (abfd, offset - (between - before),
8701 contents + irel->r_offset);
8703 case R_NDS32_DIFF32:
8704 bfd_put_32 (abfd, offset - (between - before),
8705 contents + irel->r_offset);
8709 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
8710 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8713 unsigned int len = 0;
8714 unsigned long before, between;
8717 val = read_unsigned_leb128 (abfd, contents + irel->r_offset,
8720 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8721 between = get_nds32_elf_blank_total (&blank_t,
8722 irel->r_addend + val, 0);
8723 if (between == before)
8724 goto done_adjust_diff;
8726 p = contents + irel->r_offset;
8728 memset (p, 0x80, len);
8730 p = write_uleb128 (p, val - (between - before)) - 1;
8738 raddr = irel->r_offset;
8739 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
8742 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
8744 if (blank_t2 && blank_t2->next
8745 && (blank_t2->offset > raddr
8746 || blank_t2->next->offset <= raddr))
8748 (_("%B: Error: search_nds32_elf_blank reports wrong node\n"), abfd);
8750 /* Mark reloc in deleted portion as NONE.
8751 For some relocs like R_NDS32_LABEL that doesn't modify the
8752 content in the section. R_NDS32_LABEL doesn't belong to the
8753 instruction in the section, so we should preserve it. */
8754 if (raddr >= blank_t2->offset
8755 && raddr < blank_t2->offset + blank_t2->size
8756 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
8757 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
8758 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
8759 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
8760 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
8761 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
8763 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
8769 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
8770 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
8771 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
8774 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
8775 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
8776 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
8778 if (irel->r_addend <= sec->size)
8780 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
8785 /* Adjust the local symbols defined in this section. */
8786 blank_t = blank_head;
8787 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
8789 if (isym->st_shndx == sec_shndx)
8791 if (isym->st_value <= sec->size)
8794 bfd_vma orig_addr = isym->st_value;
8796 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
8797 isym->st_value -= ahead;
8799 /* Adjust function size. */
8800 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
8801 && isym->st_size > 0)
8803 get_nds32_elf_blank_total
8804 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
8809 /* Now adjust the global symbols defined in this section. */
8810 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
8811 - symtab_hdr->sh_info);
8812 sym_hashes = elf_sym_hashes (abfd);
8813 end_hashes = sym_hashes + symcount;
8814 blank_t = blank_head;
8815 for (; sym_hashes < end_hashes; sym_hashes++)
8817 struct elf_link_hash_entry *sym_hash = *sym_hashes;
8819 if ((sym_hash->root.type == bfd_link_hash_defined
8820 || sym_hash->root.type == bfd_link_hash_defweak)
8821 && sym_hash->root.u.def.section == sec)
8823 if (sym_hash->root.u.def.value <= sec->size)
8826 bfd_vma orig_addr = sym_hash->root.u.def.value;
8828 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
8829 sym_hash->root.u.def.value -= ahead;
8831 /* Adjust function size. */
8832 if (sym_hash->type == STT_FUNC)
8834 get_nds32_elf_blank_total
8835 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
8841 contents = elf_section_data (sec)->this_hdr.contents;
8842 blank_t = blank_head;
8843 while (blank_t->next)
8845 /* Actually delete the bytes. */
8847 /* If current blank is the last blank overlap with current section,
8848 go to finish process. */
8849 if (sec->size <= (blank_t->next->offset))
8852 memmove (contents + blank_t->offset - blank_t->total_size,
8853 contents + blank_t->offset + blank_t->size,
8854 blank_t->next->offset - (blank_t->offset + blank_t->size));
8856 blank_t = blank_t->next;
8859 if (sec->size > (blank_t->offset + blank_t->size))
8861 /* There are remaining code between blank and section boundary.
8862 Move the remaining code to appropriate location. */
8863 memmove (contents + blank_t->offset - blank_t->total_size,
8864 contents + blank_t->offset + blank_t->size,
8865 sec->size - (blank_t->offset + blank_t->size));
8866 sec->size -= blank_t->total_size + blank_t->size;
8869 /* This blank is not entirely included in the section,
8870 reduce the section size by only part of the blank size. */
8871 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
8875 blank_t = blank_head;
8876 blank_head = blank_head->next;
8877 remove_nds32_elf_blank (blank_t);
8883 /* Get the contents of a section. */
8886 nds32_get_section_contents (bfd *abfd, asection *sec,
8887 bfd_byte **contents_p, bfd_boolean cache)
8889 /* Get the section contents. */
8890 if (elf_section_data (sec)->this_hdr.contents != NULL)
8891 *contents_p = elf_section_data (sec)->this_hdr.contents;
8894 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
8897 elf_section_data (sec)->this_hdr.contents = *contents_p;
8903 /* Get the contents of the internal symbol of abfd. */
8906 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
8907 Elf_Internal_Sym **isymbuf_p)
8909 Elf_Internal_Shdr *symtab_hdr;
8910 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8912 /* Read this BFD's local symbols if we haven't done so already. */
8913 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
8915 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
8916 if (*isymbuf_p == NULL)
8918 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8919 symtab_hdr->sh_info, 0,
8921 if (*isymbuf_p == NULL)
8925 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
8930 /* Range of small data. */
8931 static bfd_vma sdata_range[2][2];
8932 static bfd_vma const sdata_init_range[2] =
8933 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
8936 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
8937 bfd_byte *contents, bfd_vma addr)
8939 unsigned long insn = bfd_getb32 (contents + addr);
8941 if (insn & 0x80000000)
8947 /* Set the gp relax range. We have to measure the safe range
8948 to do gp relaxation. */
8951 relax_range_measurement (bfd *abfd)
8953 asection *sec_f, *sec_b;
8954 /* For upper bound. */
8955 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
8957 static int decide_relax_range = 0;
8959 int range_number = sizeof (sdata_init_range) / sizeof (sdata_init_range[0]);
8961 if (decide_relax_range)
8963 decide_relax_range = 1;
8965 if (sda_rela_sec == NULL)
8967 /* Since there is no data sections, we assume the range is page size. */
8968 for (i = 0; i < range_number; i++)
8970 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
8971 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
8976 /* Get the biggest alignment power after the gp located section. */
8977 sec_f = sda_rela_sec->output_section;
8978 sec_b = sec_f->next;
8980 while (sec_b != NULL)
8982 if ((unsigned)(1 << sec_b->alignment_power) > align)
8983 align = (1 << sec_b->alignment_power);
8984 sec_b = sec_b->next;
8987 /* I guess we can not determine the section before
8988 gp located section, so we assume the align is max page size. */
8989 for (i = 0; i < range_number; i++)
8991 sdata_range[i][1] = sdata_init_range[i] - align;
8992 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
8993 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
8994 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
8998 /* These are macros used to check flags encoded in r_addend.
8999 They are only used by nds32_elf_relax_section (). */
9000 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9001 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9002 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9003 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9005 static const char * unrecognized_reloc_msg =
9006 /* xgettext:c-format */
9007 N_("%B: warning: %s points to unrecognized reloc at 0x%lx.");
9009 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9012 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9013 Elf_Internal_Rela *internal_relocs, int *insn_len,
9014 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9015 Elf_Internal_Shdr *symtab_hdr)
9017 /* There are 3 variations for LONGCALL1
9018 case 4-4-2; 16-bit on, optimize off or optimize for space
9019 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9020 ori ta, ta, lo12(symbol) ; LO12S0
9023 case 4-4-4; 16-bit off, optimize don't care
9024 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9025 ori ta, ta, lo12(symbol) ; LO12S0
9028 case 4-4-4; 16-bit on, optimize for speed
9029 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9030 ori ta, ta, lo12(symbol) ; LO12S0
9032 Check code for -mlong-calls output. */
9034 /* Get the reloc for the address from which the register is
9035 being loaded. This reloc will tell us which function is
9036 actually being called. */
9039 int seq_len; /* Original length of instruction sequence. */
9041 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9042 int pic_ext_target = 0;
9043 bfd_signed_vma foff;
9046 irelend = internal_relocs + sec->reloc_count;
9047 seq_len = GET_SEQ_LEN (irel->r_addend);
9048 laddr = irel->r_offset;
9049 *insn_len = seq_len;
9051 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9052 R_NDS32_HI20_RELA, laddr);
9053 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9054 R_NDS32_LO12S0_ORI_RELA,
9057 if (hi_irelfn == irelend || lo_irelfn == irelend)
9059 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9060 (long) irel->r_offset);
9064 /* Get the value of the symbol referred to by the reloc. */
9065 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9068 /* This condition only happened when symbol is undefined. */
9069 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9070 || foff >= CONSERVATIVE_24BIT_S1)
9073 /* Relax to: jal symbol; 25_PCREL */
9074 /* For simplicity of coding, we are going to modify the section
9075 contents, the section relocs, and the BFD symbol table. We
9076 must tell the rest of the code not to free up this
9077 information. It would be possible to instead create a table
9078 of changes which have to be made, as is done in coff-mips.c;
9079 that would be more work, but would require less memory when
9080 the linker is run. */
9082 /* Replace the long call with a jal. */
9083 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9084 R_NDS32_25_PCREL_RELA);
9085 irel->r_addend = hi_irelfn->r_addend;
9087 /* We don't resolve this here but resolve it in relocate_section. */
9089 bfd_putb32 (insn, contents + irel->r_offset);
9092 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9094 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9099 insn16 = NDS32_NOP16;
9100 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9102 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9103 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9109 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9110 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
9113 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9114 Elf_Internal_Rela *internal_relocs, int *insn_len,
9115 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9116 Elf_Internal_Shdr *symtab_hdr)
9118 /* bltz rt, .L1 ; LONGCALL2
9119 jal symbol ; 25_PCREL
9122 /* Get the reloc for the address from which the register is
9123 being loaded. This reloc will tell us which function is
9124 actually being called. */
9128 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9129 int pic_ext_target = 0;
9130 bfd_signed_vma foff;
9132 irelend = internal_relocs + sec->reloc_count;
9133 laddr = irel->r_offset;
9135 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9136 R_NDS32_25_PCREL_RELA, laddr + 4);
9138 if (i1_irelfn == irelend)
9140 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9141 (long) irel->r_offset);
9145 insn = bfd_getb32 (contents + laddr);
9147 /* Get the value of the symbol referred to by the reloc. */
9148 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
9151 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9152 || foff >= CONSERVATIVE_16BIT_S1)
9155 /* Relax to bgezal rt, label ; 17_PCREL
9156 or bltzal rt, label ; 17_PCREL */
9158 /* Convert to complimentary conditional call. */
9159 insn = CONVERT_CONDITION_CALL (insn);
9161 /* For simplicity of coding, we are going to modify the section
9162 contents, the section relocs, and the BFD symbol table. We
9163 must tell the rest of the code not to free up this
9164 information. It would be possible to instead create a table
9165 of changes which have to be made, as is done in coff-mips.c;
9166 that would be more work, but would require less memory when
9167 the linker is run. */
9169 /* Clean unnessary relocations. */
9171 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9173 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9174 R_NDS32_17_PCREL_RELA, laddr);
9175 if (cond_irelfn != irelend)
9176 cond_irelfn->r_info =
9177 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9179 /* Replace the long call with a bgezal. */
9180 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9181 R_NDS32_17_PCREL_RELA);
9182 irel->r_addend = i1_irelfn->r_addend;
9184 bfd_putb32 (insn, contents + irel->r_offset);
9190 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9193 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9194 Elf_Internal_Rela *internal_relocs, int *insn_len,
9195 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9196 Elf_Internal_Shdr *symtab_hdr)
9198 /* There are 3 variations for LONGCALL3
9199 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9200 bltz rt, $1 ; LONGCALL3
9201 sethi ta, hi20(symbol) ; HI20
9202 ori ta, ta, lo12(symbol) ; LO12S0
9206 case 4-4-4-4; 16-bit off, optimize don't care
9207 bltz rt, $1 ; LONGCALL3
9208 sethi ta, hi20(symbol) ; HI20
9209 ori ta, ta, lo12(symbol) ; LO12S0
9213 case 4-4-4-4; 16-bit on, optimize for speed
9214 bltz rt, $1 ; LONGCALL3
9215 sethi ta, hi20(symbol) ; HI20
9216 ori ta, ta, lo12(symbol) ; LO12S0
9220 /* Get the reloc for the address from which the register is
9221 being loaded. This reloc will tell us which function is
9222 actually being called. */
9225 int seq_len; /* Original length of instruction sequence. */
9227 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9228 int pic_ext_target = 0;
9229 bfd_signed_vma foff;
9232 irelend = internal_relocs + sec->reloc_count;
9233 seq_len = GET_SEQ_LEN (irel->r_addend);
9234 laddr = irel->r_offset;
9235 *insn_len = seq_len;
9238 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9239 R_NDS32_HI20_RELA, laddr + 4);
9241 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9242 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9244 if (hi_irelfn == irelend || lo_irelfn == irelend)
9246 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9247 (long) irel->r_offset);
9251 /* Get the value of the symbol referred to by the reloc. */
9252 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9255 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9256 || foff >= CONSERVATIVE_24BIT_S1)
9259 insn = bfd_getb32 (contents + laddr);
9260 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9262 /* Relax to bgezal rt, label ; 17_PCREL
9263 or bltzal rt, label ; 17_PCREL */
9265 /* Convert to complimentary conditional call. */
9266 insn = CONVERT_CONDITION_CALL (insn);
9267 bfd_putb32 (insn, contents + irel->r_offset);
9271 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9273 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9275 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9278 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9279 R_NDS32_17_PCREL_RELA, laddr);
9280 if (cond_irelfn != irelend)
9282 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9283 R_NDS32_17_PCREL_RELA);
9284 cond_irelfn->r_addend = hi_irelfn->r_addend;
9289 insn16 = NDS32_NOP16;
9290 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9291 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9293 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9297 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9299 /* Relax to the following instruction sequence
9300 bltz rt, $1 ; LONGCALL2
9301 jal symbol ; 25_PCREL
9305 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9307 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9308 R_NDS32_25_PCREL_RELA);
9310 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9313 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9317 insn16 = NDS32_NOP16;
9318 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9320 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9321 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9328 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
9331 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9332 Elf_Internal_Rela *internal_relocs, int *insn_len,
9333 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9334 Elf_Internal_Shdr *symtab_hdr)
9336 /* There are 3 variations for LONGJUMP1
9337 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9338 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9339 ori ta, ta, lo12(symbol) ; LO12S0
9342 case 4-4-4; 16-bit off, optimize don't care
9343 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9344 ori ta, ta, lo12(symbol) ; LO12S0
9347 case 4-4-4; 16-bit on, optimize for speed
9348 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9349 ori ta, ta, lo12(symbol) ; LO12S0
9352 /* Get the reloc for the address from which the register is
9353 being loaded. This reloc will tell us which function is
9354 actually being called. */
9357 int seq_len; /* Original length of instruction sequence. */
9358 int insn16_on; /* 16-bit on/off. */
9360 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9361 int pic_ext_target = 0;
9362 bfd_signed_vma foff;
9364 unsigned long reloc;
9366 irelend = internal_relocs + sec->reloc_count;
9367 seq_len = GET_SEQ_LEN (irel->r_addend);
9368 laddr = irel->r_offset;
9369 *insn_len = seq_len;
9370 insn16_on = IS_16BIT_ON (irel->r_addend);
9373 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9374 R_NDS32_HI20_RELA, laddr);
9376 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9377 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9378 if (hi_irelfn == irelend || lo_irelfn == irelend)
9380 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
9381 (long) irel->r_offset);
9385 /* Get the value of the symbol referred to by the reloc. */
9386 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9389 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
9390 || foff < -CONSERVATIVE_24BIT_S1)
9393 if (insn16_on && foff >= -ACCURATE_8BIT_S1
9394 && foff < ACCURATE_8BIT_S1 && (seq_len & 0x2))
9397 /* 16-bit on, but not optimized for speed. */
9398 reloc = R_NDS32_9_PCREL_RELA;
9400 bfd_putb16 (insn16, contents + irel->r_offset);
9403 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9408 reloc = R_NDS32_25_PCREL_RELA;
9410 bfd_putb32 (insn, contents + irel->r_offset);
9413 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9418 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9420 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9422 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
9424 insn16 = NDS32_NOP16;
9425 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9427 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9429 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9435 /* Revert condition branch. This function does not check if the input
9436 instruction is condition branch or not. */
9439 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
9440 uint16_t *re_insn16, uint32_t *re_insn)
9442 uint32_t comp_insn = 0;
9443 uint16_t comp_insn16 = 0;
9447 if (N32_OP6 (insn) == N32_OP6_BR1)
9450 comp_insn = (insn ^ 0x4000) & 0xffffc000;
9451 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
9453 /* Insn can be contracted to 16-bit implied r5. */
9455 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
9456 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9459 else if (N32_OP6 (insn) == N32_OP6_BR3)
9461 /* bnec $ta, imm11, label. */
9462 comp_insn = (insn ^ 0x80000) & 0xffffff00;
9466 comp_insn = (insn ^ 0x10000) & 0xffffc000;
9467 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
9468 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
9470 if (N32_IS_RT3 (insn))
9472 /* Insn can be contracted to 16-bit. */
9474 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
9475 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9477 else if (N32_RT5 (insn) == REG_R15)
9479 /* Insn can be contracted to 16-bit. */
9481 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
9488 switch ((insn16 & 0xf000) >> 12)
9491 /* beqz38 or bnez38 */
9492 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9493 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
9494 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
9498 /* beqs38 or bnes38 */
9499 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9500 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
9501 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
9506 /* beqzS8 or bnezS8 */
9507 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
9508 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
9509 comp_insn |= REG_R15 << 20;
9516 if (comp_insn && re_insn)
9517 *re_insn = comp_insn;
9518 if (comp_insn16 && re_insn16)
9519 *re_insn16 = comp_insn16;
9522 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
9525 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9526 Elf_Internal_Rela *internal_relocs, int *insn_len,
9527 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9528 Elf_Internal_Shdr *symtab_hdr)
9530 /* There are 3 variations for LONGJUMP2
9531 case 2-4; 1st insn convertible, 16-bit on,
9532 optimize off or optimize for space
9533 bnes38 rt, ra, $1 ; LONGJUMP2
9537 case 4-4; 1st insn not convertible
9538 bne rt, ra, $1 ; LONGJUMP2
9542 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9543 bne rt, ra, $1 ; LONGJUMP2
9547 /* Get the reloc for the address from which the register is
9548 being loaded. This reloc will tell us which function is
9549 actually being called. */
9552 int seq_len; /* Original length of instruction sequence. */
9553 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
9554 int pic_ext_target = 0, first_size;
9556 bfd_signed_vma foff;
9557 uint32_t insn, re_insn = 0;
9558 uint16_t insn16, re_insn16 = 0;
9559 unsigned long reloc, cond_reloc;
9561 enum elf_nds32_reloc_type checked_types[] =
9562 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9564 irelend = internal_relocs + sec->reloc_count;
9565 seq_len = GET_SEQ_LEN (irel->r_addend);
9566 laddr = irel->r_offset;
9567 *insn_len = seq_len;
9568 first_size = (seq_len == 6) ? 2 : 4;
9571 find_relocs_at_address_addr (irel, internal_relocs,
9572 irelend, R_NDS32_25_PCREL_RELA,
9573 laddr + first_size);
9575 for (i = 0; i < sizeof (checked_types) / sizeof(checked_types[0]); i++)
9578 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9579 checked_types[i], laddr);
9580 if (cond_irelfn != irelend)
9584 if (i2_irelfn == irelend || cond_irelfn == irelend)
9586 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
9587 (long) irel->r_offset);
9591 /* Get the value of the symbol referred to by the reloc. */
9593 calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
9595 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9596 || foff >= CONSERVATIVE_16BIT_S1)
9599 /* Get the all corresponding instructions. */
9600 if (first_size == 4)
9602 insn = bfd_getb32 (contents + laddr);
9603 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9607 insn16 = bfd_getb16 (contents + laddr);
9608 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9611 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
9612 && foff < ACCURATE_8BIT_S1 - first_size)
9614 if (first_size == 4)
9616 /* Don't convert it to 16-bit now, keep this as relaxable for
9617 ``label reloc; INSN16''. */
9619 /* Save comp_insn32 to buffer. */
9620 bfd_putb32 (re_insn, contents + irel->r_offset);
9622 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9623 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9624 cond_reloc = R_NDS32_INSN16;
9628 bfd_putb16 (re_insn16, contents + irel->r_offset);
9630 reloc = R_NDS32_9_PCREL_RELA;
9631 cond_reloc = R_NDS32_NONE;
9634 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9635 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9636 && foff < ACCURATE_14BIT_S1 - first_size))
9638 /* beqs label ; 15_PCREL */
9639 bfd_putb32 (re_insn, contents + irel->r_offset);
9641 reloc = R_NDS32_15_PCREL_RELA;
9642 cond_reloc = R_NDS32_NONE;
9644 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9645 && foff >= -CONSERVATIVE_16BIT_S1
9646 && foff < CONSERVATIVE_16BIT_S1)
9648 /* beqz label ; 17_PCREL */
9649 bfd_putb32 (re_insn, contents + irel->r_offset);
9651 reloc = R_NDS32_17_PCREL_RELA;
9652 cond_reloc = R_NDS32_NONE;
9657 /* Set all relocations. */
9658 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
9659 irel->r_addend = i2_irelfn->r_addend;
9661 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9663 cond_irelfn->r_addend = 0;
9665 if ((seq_len ^ *insn_len ) & 0x2)
9667 insn16 = NDS32_NOP16;
9668 bfd_putb16 (insn16, contents + irel->r_offset + 4);
9669 i2_irelfn->r_offset = 4;
9670 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9672 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9676 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9681 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
9684 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9685 Elf_Internal_Rela *internal_relocs, int *insn_len,
9686 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9687 Elf_Internal_Shdr *symtab_hdr)
9689 /* There are 5 variations for LONGJUMP3
9690 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
9691 optimize off or optimize for space
9692 bnes38 rt, ra, $1 ; LONGJUMP3
9693 sethi ta, hi20(symbol) ; HI20
9694 ori ta, ta, lo12(symbol) ; LO12S0
9698 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
9699 bnes38 rt, ra, $1 ; LONGJUMP3
9700 sethi ta, hi20(symbol) ; HI20
9701 ori ta, ta, lo12(symbol) ; LO12S0
9705 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
9706 optimize off or optimize for space
9707 bne rt, ra, $1 ; LONGJUMP3
9708 sethi ta, hi20(symbol) ; HI20
9709 ori ta, ta, lo12(symbol) ; LO12S0
9713 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
9714 16-bit off if no INSN16
9715 bne rt, ra, $1 ; LONGJUMP3
9716 sethi ta, hi20(symbol) ; HI20
9717 ori ta, ta, lo12(symbol) ; LO12S0
9721 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
9722 16-bit off if no INSN16
9723 bne rt, ra, $1 ; LONGJUMP3
9724 sethi ta, hi20(symbol) ; HI20
9725 ori ta, ta, lo12(symbol) ; LO12S0
9729 /* Get the reloc for the address from which the register is
9730 being loaded. This reloc will tell us which function is
9731 actually being called. */
9732 enum elf_nds32_reloc_type checked_types[] =
9733 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9735 int reloc_off = 0, cond_removed = 0, convertible;
9737 int seq_len; /* Original length of instruction sequence. */
9738 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9739 int pic_ext_target = 0, first_size;
9741 bfd_signed_vma foff;
9742 uint32_t insn, re_insn = 0;
9743 uint16_t insn16, re_insn16 = 0;
9744 unsigned long reloc, cond_reloc;
9746 irelend = internal_relocs + sec->reloc_count;
9747 seq_len = GET_SEQ_LEN (irel->r_addend);
9748 laddr = irel->r_offset;
9749 *insn_len = seq_len;
9751 convertible = IS_1ST_CONVERT (irel->r_addend);
9758 /* Get all needed relocations. */
9760 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9761 R_NDS32_HI20_RELA, laddr + first_size);
9763 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9764 R_NDS32_LO12S0_ORI_RELA,
9765 laddr + first_size + 4);
9767 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
9770 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9771 checked_types[i], laddr);
9772 if (cond_irelfn != irelend)
9776 if (hi_irelfn == irelend || lo_irelfn == irelend || cond_irelfn == irelend)
9778 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
9779 (long) irel->r_offset);
9783 /* Get the value of the symbol referred to by the reloc. */
9784 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9787 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9788 || foff >= CONSERVATIVE_24BIT_S1)
9791 /* Get the all corresponding instructions. */
9792 if (first_size == 4)
9794 insn = bfd_getb32 (contents + laddr);
9795 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9799 insn16 = bfd_getb16 (contents + laddr);
9800 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9803 /* For simplicity of coding, we are going to modify the section
9804 contents, the section relocs, and the BFD symbol table. We
9805 must tell the rest of the code not to free up this
9806 information. It would be possible to instead create a table
9807 of changes which have to be made, as is done in coff-mips.c;
9808 that would be more work, but would require less memory when
9809 the linker is run. */
9811 if (re_insn16 && foff >= -ACCURATE_8BIT_S1 - first_size
9812 && foff < ACCURATE_8BIT_S1 - first_size)
9814 if (!(seq_len & 0x2))
9816 /* Don't convert it to 16-bit now, keep this as relaxable
9817 for ``label reloc; INSN1a''6. */
9818 /* Save comp_insn32 to buffer. */
9819 bfd_putb32 (re_insn, contents + irel->r_offset);
9821 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9822 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9823 cond_reloc = R_NDS32_INSN16;
9827 /* Not optimize for speed; convert sequence to 16-bit. */
9828 /* Save comp_insn16 to buffer. */
9829 bfd_putb16 (re_insn16, contents + irel->r_offset);
9831 reloc = R_NDS32_9_PCREL_RELA;
9832 cond_reloc = R_NDS32_NONE;
9836 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9837 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9838 && foff < ACCURATE_14BIT_S1 - first_size))
9840 /* beqs label ; 15_PCREL */
9841 bfd_putb32 (re_insn, contents + irel->r_offset);
9843 reloc = R_NDS32_15_PCREL_RELA;
9844 cond_reloc = R_NDS32_NONE;
9847 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9848 && foff >= -CONSERVATIVE_16BIT_S1
9849 && foff < CONSERVATIVE_16BIT_S1)
9851 /* beqz label ; 17_PCREL */
9852 bfd_putb32 (re_insn, contents + irel->r_offset);
9854 reloc = R_NDS32_17_PCREL_RELA;
9855 cond_reloc = R_NDS32_NONE;
9858 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
9859 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
9861 /* Relax to one of the following 3 variations
9863 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
9865 bnes38 rt, $1 ; LONGJUMP2
9869 case 4-4; 1st insn not convertible, others don't care
9870 bne rt, ra, $1 ; LONGJUMP2
9874 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9875 bne rt, ra, $1 ; LONGJUMP2
9879 /* Offset for first instruction. */
9881 /* Use j label as second instruction. */
9882 *insn_len = 4 + first_size;
9884 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9885 reloc = R_NDS32_LONGJUMP2;
9886 cond_reloc = R_NDS32_25_PLTREL;
9891 if (cond_removed == 1)
9893 /* Set all relocations. */
9894 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9895 irel->r_addend = hi_irelfn->r_addend;
9897 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9899 cond_irelfn->r_addend = 0;
9900 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9905 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
9906 irel->r_addend = irel->r_addend;
9907 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9911 if ((seq_len ^ *insn_len ) & 0x2)
9913 insn16 = NDS32_NOP16;
9914 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9915 lo_irelfn->r_offset = *insn_len;
9916 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9918 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9922 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9927 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
9930 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9931 Elf_Internal_Rela *internal_relocs, int *insn_len,
9932 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9933 Elf_Internal_Shdr *symtab_hdr)
9935 /* The pattern for LONGCALL4. Support for function cse.
9936 sethi ta, hi20(symbol) ; LONGCALL4/HI20
9937 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
9938 jral ta ; PTR_RES/EMPTY/INSN16 */
9942 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
9943 Elf_Internal_Rela *irelend;
9944 int pic_ext_target = 0;
9945 bfd_signed_vma foff;
9947 irelend = internal_relocs + sec->reloc_count;
9948 laddr = irel->r_offset;
9950 /* Get the reloc for the address from which the register is
9951 being loaded. This reloc will tell us which function is
9952 actually being called. */
9953 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9954 R_NDS32_HI20_RELA, laddr);
9956 if (hi_irel == irelend)
9958 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9959 (long) irel->r_offset);
9963 /* Get the value of the symbol referred to by the reloc. */
9964 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
9967 /* This condition only happened when symbol is undefined. */
9968 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9969 || foff >= CONSERVATIVE_24BIT_S1)
9972 /* Relax to: jal symbol; 25_PCREL */
9973 /* For simplicity of coding, we are going to modify the section
9974 contents, the section relocs, and the BFD symbol table. We
9975 must tell the rest of the code not to free up this
9976 information. It would be possible to instead create a table
9977 of changes which have to be made, as is done in coff-mips.c;
9978 that would be more work, but would require less memory when
9979 the linker is run. */
9981 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9982 R_NDS32_PTR_RESOLVED, irel->r_addend);
9983 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9984 R_NDS32_EMPTY, irel->r_addend);
9986 if (ptr_irel == irelend || em_irel == irelend)
9988 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9989 (long) irel->r_offset);
9992 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
9993 insn = bfd_getb32 (contents + irel->r_addend);
9994 if (insn & 0x80000000)
9997 /* Replace the long call with a jal. */
9998 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
9999 R_NDS32_25_PCREL_RELA);
10000 ptr_irel->r_addend = 1;
10002 /* We don't resolve this here but resolve it in relocate_section. */
10004 bfd_putb32 (insn, contents + em_irel->r_offset);
10007 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10009 /* If there is function cse, HI20 can not remove now. */
10010 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10011 R_NDS32_LONGCALL4, laddr);
10012 if (call_irel == irelend)
10016 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10019 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10020 R_NDS32_INSN16, irel->r_addend);
10021 if (insn_irel != irelend)
10022 insn_irel->r_info =
10023 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10028 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
10031 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10032 Elf_Internal_Rela *internal_relocs, int *insn_len,
10033 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10034 Elf_Internal_Shdr *symtab_hdr)
10036 /* The pattern for LONGCALL5.
10037 bltz rt, .L1 ; LONGCALL5/17_PCREL
10038 jal symbol ; 25_PCREL
10043 Elf_Internal_Rela *cond_irel, *irelend;
10044 int pic_ext_target = 0;
10045 bfd_signed_vma foff;
10047 irelend = internal_relocs + sec->reloc_count;
10048 laddr = irel->r_offset;
10049 insn = bfd_getb32 (contents + laddr);
10051 /* Get the reloc for the address from which the register is
10052 being loaded. This reloc will tell us which function is
10053 actually being called. */
10055 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10056 R_NDS32_25_PCREL_RELA, irel->r_addend);
10057 if (cond_irel == irelend)
10059 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10060 (long) irel->r_offset);
10064 /* Get the value of the symbol referred to by the reloc. */
10065 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10068 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
10069 || foff >= CONSERVATIVE_16BIT_S1)
10072 /* Relax to bgezal rt, label ; 17_PCREL
10073 or bltzal rt, label ; 17_PCREL */
10075 /* Convert to complimentary conditional call. */
10076 insn = CONVERT_CONDITION_CALL (insn);
10078 /* For simplicity of coding, we are going to modify the section
10079 contents, the section relocs, and the BFD symbol table. We
10080 must tell the rest of the code not to free up this
10081 information. It would be possible to instead create a table
10082 of changes which have to be made, as is done in coff-mips.c;
10083 that would be more work, but would require less memory when
10084 the linker is run. */
10086 /* Modify relocation and contents. */
10087 cond_irel->r_info =
10088 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10090 /* Replace the long call with a bgezal. */
10091 bfd_putb32 (insn, contents + cond_irel->r_offset);
10094 /* Clean unnessary relocations. */
10095 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10097 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10098 R_NDS32_17_PCREL_RELA, laddr);
10099 cond_irel->r_info =
10100 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10105 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
10108 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10109 Elf_Internal_Rela *internal_relocs, int *insn_len,
10110 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10111 Elf_Internal_Shdr *symtab_hdr)
10113 /* The pattern for LONGCALL6.
10114 bltz rt, .L1 ; LONGCALL6/17_PCREL
10115 sethi ta, hi20(symbol) ; HI20/PTR
10116 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10117 jral ta ; PTR_RES/EMPTY/INSN16
10122 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10123 int pic_ext_target = 0;
10124 bfd_signed_vma foff;
10126 irelend = internal_relocs + sec->reloc_count;
10127 laddr = irel->r_offset;
10129 /* Get the reloc for the address from which the register is
10130 being loaded. This reloc will tell us which function is
10131 actually being called. */
10132 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10133 R_NDS32_EMPTY, irel->r_addend);
10135 if (em_irel == irelend)
10137 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10138 (long) irel->r_offset);
10142 /* Get the value of the symbol referred to by the reloc. */
10143 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
10146 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
10147 || foff >= CONSERVATIVE_24BIT_S1)
10150 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10151 insn = bfd_getb32 (contents + irel->r_addend);
10152 if (insn & 0x80000000)
10155 insn = bfd_getb32 (contents + laddr);
10156 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10158 /* Relax to bgezal rt, label ; 17_PCREL
10159 or bltzal rt, label ; 17_PCREL */
10161 /* Convert to complimentary conditional call. */
10163 insn = CONVERT_CONDITION_CALL (insn);
10164 bfd_putb32 (insn, contents + em_irel->r_offset);
10167 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10169 /* Set resolved relocation. */
10171 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10172 R_NDS32_PTR_RESOLVED, irel->r_addend);
10173 if (cond_irel == irelend)
10175 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10176 "R_NDS32_LONGCALL6", (long) irel->r_offset);
10179 cond_irel->r_addend = 1;
10181 /* Clear relocations. */
10184 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10187 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10188 R_NDS32_17_PCREL_RELA, laddr);
10189 if (cond_irel != irelend)
10190 cond_irel->r_info =
10191 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10194 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10195 R_NDS32_INSN16, irel->r_addend);
10196 if (cond_irel != irelend)
10197 cond_irel->r_info =
10198 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10201 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10203 /* Relax to the following instruction sequence
10204 bltz rt, .L1 ; LONGCALL2/17_PCREL
10205 jal symbol ; 25_PCREL/PTR_RES
10208 /* Convert instruction. */
10210 bfd_putb32 (insn, contents + em_irel->r_offset);
10212 /* Convert relocations. */
10213 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10214 R_NDS32_25_PCREL_RELA);
10216 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10218 /* Set resolved relocation. */
10220 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10221 R_NDS32_PTR_RESOLVED, irel->r_addend);
10222 if (cond_irel == irelend)
10224 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10225 "R_NDS32_LONGCALL6", (long) irel->r_offset);
10228 cond_irel->r_addend = 1;
10231 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10232 R_NDS32_INSN16, irel->r_addend);
10233 if (cond_irel != irelend)
10234 cond_irel->r_info =
10235 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10240 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10243 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10244 Elf_Internal_Rela *internal_relocs, int *insn_len,
10245 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10246 Elf_Internal_Shdr *symtab_hdr)
10248 /* The pattern for LONGJUMP4.
10249 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10250 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10251 jr ta ; PTR_RES/INSN16/EMPTY */
10254 int seq_len; /* Original length of instruction sequence. */
10256 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10257 int pic_ext_target = 0;
10258 bfd_signed_vma foff;
10260 irelend = internal_relocs + sec->reloc_count;
10261 seq_len = GET_SEQ_LEN (irel->r_addend);
10262 laddr = irel->r_offset;
10263 *insn_len = seq_len;
10265 /* Get the reloc for the address from which the register is
10266 being loaded. This reloc will tell us which function is
10267 actually being called. */
10269 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10270 R_NDS32_HI20_RELA, laddr);
10272 if (hi_irel == irelend)
10274 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10275 (long) irel->r_offset);
10279 /* Get the value of the symbol referred to by the reloc. */
10280 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
10283 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
10284 || foff < -CONSERVATIVE_24BIT_S1)
10287 /* Convert it to "j label", it may be converted to j8 in the final
10288 pass of relaxation. Therefore, we do not consider this currently. */
10289 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10290 R_NDS32_PTR_RESOLVED, irel->r_addend);
10291 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10292 R_NDS32_EMPTY, irel->r_addend);
10294 if (ptr_irel == irelend || em_irel == irelend)
10296 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10297 (long) irel->r_offset);
10302 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10303 ptr_irel->r_addend = 1;
10305 /* Write instruction. */
10307 bfd_putb32 (insn, contents + em_irel->r_offset);
10309 /* Clear relocations. */
10310 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10312 /* If there is function cse, HI20 can not remove now. */
10313 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10314 R_NDS32_LONGJUMP4, laddr);
10315 if (call_irel == irelend)
10319 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10325 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
10328 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10329 Elf_Internal_Rela *internal_relocs, int *insn_len,
10330 int *seq_len, bfd_byte *contents,
10331 Elf_Internal_Sym *isymbuf,
10332 Elf_Internal_Shdr *symtab_hdr)
10334 /* There are 2 variations for LONGJUMP5
10335 case 2-4; 1st insn convertible, 16-bit on.
10336 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10337 j label ; 25_PCREL/INSN16
10340 case 4-4; 1st insn not convertible
10341 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10342 j label ; 25_PCREL/INSN16
10346 Elf_Internal_Rela *cond_irel, *irelend;
10347 int pic_ext_target = 0;
10349 bfd_signed_vma foff;
10350 uint32_t insn, re_insn = 0;
10351 uint16_t insn16, re_insn16 = 0;
10352 unsigned long reloc;
10354 enum elf_nds32_reloc_type checked_types[] =
10355 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10356 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10358 irelend = internal_relocs + sec->reloc_count;
10359 laddr = irel->r_offset;
10361 /* Get the reloc for the address from which the register is
10362 being loaded. This reloc will tell us which function is
10363 actually being called. */
10366 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10367 R_NDS32_25_PCREL_RELA, irel->r_addend);
10368 if (cond_irel == irelend)
10370 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10371 (long) irel->r_offset);
10375 /* Get the value of the symbol referred to by the reloc. */
10376 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10379 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
10380 || foff >= CONSERVATIVE_16BIT_S1)
10383 /* Get the all corresponding instructions. */
10384 insn = bfd_getb32 (contents + laddr);
10385 /* Check instruction size. */
10386 if (insn & 0x80000000)
10389 insn16 = insn >> 16;
10390 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10393 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10395 if (N32_OP6 (re_insn) == N32_OP6_BR1
10396 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10398 /* beqs label ; 15_PCREL. */
10399 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10400 reloc = R_NDS32_15_PCREL_RELA;
10402 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10403 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10405 /* beqz label ; 17_PCREL. */
10406 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10407 reloc = R_NDS32_17_PCREL_RELA;
10409 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
10410 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
10412 /* beqc label ; 9_PCREL. */
10413 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10414 reloc = R_NDS32_WORD_9_PCREL_RELA;
10419 /* Set all relocations. */
10420 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
10422 /* Clean relocations. */
10423 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10424 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10426 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10427 checked_types[i], laddr);
10428 if (cond_irel != irelend)
10431 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10433 /* If the branch instruction is 2 byte, it cannot remove
10434 directly. Only convert it to nop16 and remove it after
10435 checking alignment issue. */
10436 insn16 = NDS32_NOP16;
10437 bfd_putb16 (insn16, contents + laddr);
10438 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10441 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10450 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
10453 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10454 Elf_Internal_Rela *internal_relocs, int *insn_len,
10455 int *seq_len, bfd_byte *contents,
10456 Elf_Internal_Sym *isymbuf,
10457 Elf_Internal_Shdr *symtab_hdr)
10459 /* There are 5 variations for LONGJUMP6
10460 case : 2-4-4-4; 1st insn convertible, 16-bit on.
10461 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10462 sethi ta, hi20(symbol) ; HI20/PTR
10463 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10464 jr ta ; PTR_RES/INSN16/EMPTY
10467 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
10468 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10469 sethi ta, hi20(symbol) ; HI20/PTR
10470 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10471 jr ta ; PTR_RES/INSN16/EMPTY
10474 enum elf_nds32_reloc_type checked_types[] =
10475 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10476 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10478 int reloc_off = 0, cond_removed = 0;
10480 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
10481 int pic_ext_target = 0;
10483 bfd_signed_vma foff;
10484 uint32_t insn, re_insn = 0;
10485 uint16_t insn16, re_insn16 = 0;
10486 unsigned long reloc;
10488 irelend = internal_relocs + sec->reloc_count;
10489 laddr = irel->r_offset;
10491 /* Get the reloc for the address from which the register is
10492 being loaded. This reloc will tell us which function is
10493 actually being called. */
10494 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10495 R_NDS32_EMPTY, irel->r_addend);
10497 if (em_irel == irelend)
10499 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
10500 (long) irel->r_offset);
10504 /* Get the value of the symbol referred to by the reloc. */
10505 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
10508 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
10509 || foff >= CONSERVATIVE_24BIT_S1)
10512 insn = bfd_getb32 (contents + laddr);
10513 /* Check instruction size. */
10514 if (insn & 0x80000000)
10517 insn16 = insn >> 16;
10518 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10521 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10523 /* For simplicity of coding, we are going to modify the section
10524 contents, the section relocs, and the BFD symbol table. We
10525 must tell the rest of the code not to free up this
10526 information. It would be possible to instead create a table
10527 of changes which have to be made, as is done in coff-mips.c;
10528 that would be more work, but would require less memory when
10529 the linker is run. */
10531 if (N32_OP6 (re_insn) == N32_OP6_BR1
10532 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10534 /* beqs label ; 15_PCREL */
10535 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10536 reloc = R_NDS32_15_PCREL_RELA;
10539 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10540 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10542 /* beqz label ; 17_PCREL */
10543 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10544 reloc = R_NDS32_17_PCREL_RELA;
10547 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10548 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10550 /* Relax to one of the following 2 variations
10552 case 2-4; 1st insn convertible, 16-bit on.
10553 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10554 j label ; 25_PCREL/INSN16
10557 case 4-4; 1st insn not convertible
10558 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10559 j label ; 25_PCREL/INSN16
10562 /* Use j label as second instruction. */
10564 reloc = R_NDS32_25_PCREL_RELA;
10565 bfd_putb32 (insn, contents + em_irel->r_offset);
10570 /* Set all relocations. */
10571 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
10574 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10575 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
10576 cond_irel->r_addend = 1;
10578 /* Use INSN16 of first branch instruction to distinguish if keeping
10579 INSN16 of final instruction or not. */
10580 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10581 R_NDS32_INSN16, irel->r_offset);
10582 if (insn_irel == irelend)
10584 /* Clean the final INSN16. */
10586 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10587 R_NDS32_INSN16, em_irel->r_offset);
10588 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10592 if (cond_removed == 1)
10596 /* Clear relocations. */
10597 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10599 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10602 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10603 checked_types[i], laddr);
10604 if (cond_irel != irelend)
10607 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10609 /* If the branch instruction is 2 byte, it cannot remove
10610 directly. Only convert it to nop16 and remove it after
10611 checking alignment issue. */
10612 insn16 = NDS32_NOP16;
10613 bfd_putb16 (insn16, contents + laddr);
10614 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10617 cond_irel->r_info =
10618 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10624 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10625 R_NDS32_LONGJUMP5);
10631 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
10634 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10635 Elf_Internal_Rela *internal_relocs, int *insn_len,
10636 int *seq_len, bfd_byte *contents,
10637 Elf_Internal_Sym *isymbuf,
10638 Elf_Internal_Shdr *symtab_hdr)
10640 /* There are 2 variations for LONGJUMP5
10641 case 2-4; 1st insn convertible, 16-bit on.
10642 movi55 ta, imm11 ; LONGJUMP7/INSN16
10643 beq rt, ta, label ; 15_PCREL
10645 case 4-4; 1st insn not convertible
10646 movi55 ta, imm11 ; LONGJUMP7/INSN16
10647 beq rt, ta, label ; 15_PCREL */
10650 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
10651 int pic_ext_target = 0;
10652 bfd_signed_vma foff;
10653 uint32_t insn, re_insn = 0;
10657 irelend = internal_relocs + sec->reloc_count;
10658 laddr = irel->r_offset;
10660 /* Get the reloc for the address from which the register is
10661 being loaded. This reloc will tell us which function is
10662 actually being called. */
10665 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10666 R_NDS32_15_PCREL_RELA, irel->r_addend);
10667 if (cond_irel == irelend)
10669 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
10670 (long) irel->r_offset);
10674 /* Get the value of the symbol referred to by the reloc. */
10675 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10678 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_8BIT_S1
10679 || foff >= CONSERVATIVE_8BIT_S1)
10682 /* Get the first instruction for its size. */
10683 insn = bfd_getb32 (contents + laddr);
10684 if (insn & 0x80000000)
10687 /* Get the immediate from movi55. */
10688 imm11 = N16_IMM5S (insn >> 16);
10692 /* Get the immediate from movi. */
10693 imm11 = N32_IMM20S (insn);
10696 /* Get the branch instruction. */
10697 insn = bfd_getb32 (contents + irel->r_addend);
10698 /* Convert instruction to BR3. */
10699 if ((insn >> 14) & 0x1)
10700 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
10702 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
10704 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10706 /* Set all relocations. */
10707 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10708 R_NDS32_WORD_9_PCREL_RELA);
10710 /* Clean relocations. */
10711 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10712 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10713 R_NDS32_INSN16, irel->r_offset);
10714 if (insn_irel != irelend)
10718 /* If the first insntruction is 16bit, convert it to nop16. */
10719 insn16 = NDS32_NOP16;
10720 bfd_putb16 (insn16, contents + laddr);
10721 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10724 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10732 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
10734 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
10737 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
10738 asection *sec, Elf_Internal_Rela *irel,
10739 Elf_Internal_Rela *internal_relocs, int *insn_len,
10740 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10741 Elf_Internal_Shdr *symtab_hdr, int load_store_relax)
10743 int eliminate_sethi = 0, range_type;
10745 bfd_vma local_sda, laddr;
10746 int seq_len; /* Original length of instruction sequence. */
10748 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
10749 bfd_vma access_addr = 0;
10750 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10751 enum elf_nds32_reloc_type checked_types[] =
10752 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
10753 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
10754 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
10755 R_NDS32_TLS_LE_HI20
10758 irelend = internal_relocs + sec->reloc_count;
10759 seq_len = GET_SEQ_LEN (irel->r_addend);
10760 laddr = irel->r_offset;
10761 *insn_len = seq_len;
10763 /* Get the high part relocation. */
10764 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10766 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10767 checked_types[i], laddr);
10768 if (hi_irelfn != irelend)
10772 if (hi_irelfn == irelend)
10774 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
10775 (long) irel->r_offset);
10779 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
10780 nds32_elf_final_sda_base (sec->output_section->owner,
10781 link_info, &local_sda, FALSE);
10783 switch (ELF32_R_TYPE (hi_irelfn->r_info))
10785 case R_NDS32_HI20_RELA:
10786 insn = bfd_getb32 (contents + laddr);
10788 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10790 if (range_type == NDS32_LOADSTORE_IMM)
10792 struct elf_link_hash_entry *h = NULL;
10795 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
10797 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
10798 h = elf_sym_hashes (abfd)[indx];
10801 if ((access_addr < CONSERVATIVE_20BIT)
10802 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10804 eliminate_sethi = 1;
10808 /* This is avoid to relax symbol address which is fixed
10809 relocations. Ex: _stack. */
10810 if (h && bfd_is_abs_section (h->root.u.def.section))
10814 if (!load_store_relax)
10817 /* Case for set gp register. */
10818 if (N32_RT5 (insn) == REG_GP)
10821 if (range_type == NDS32_LOADSTORE_FLOAT_S
10822 || range_type == NDS32_LOADSTORE_FLOAT_S)
10824 range_l = sdata_range[0][0];
10825 range_h = sdata_range[0][1];
10829 range_l = sdata_range[1][0];
10830 range_h = sdata_range[1][1];
10834 case R_NDS32_GOT_HI20:
10836 calculate_got_memory_address (abfd, link_info, hi_irelfn, symtab_hdr);
10838 /* If this symbol is not in .got, the return value will be -1.
10839 Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
10840 a negative offset is allowed. */
10841 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10842 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10843 eliminate_sethi = 1;
10846 case R_NDS32_PLT_GOTREL_HI20:
10847 access_addr = calculate_plt_memory_address (abfd, link_info, isymbuf,
10848 hi_irelfn, symtab_hdr);
10850 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10851 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10852 eliminate_sethi = 1;
10855 case R_NDS32_GOTOFF_HI20:
10857 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10859 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10860 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10861 eliminate_sethi = 1;
10864 case R_NDS32_GOTPC_HI20:
10865 /* The access_addr must consider r_addend of hi_irel. */
10866 access_addr = sec->output_section->vma + sec->output_offset
10867 + irel->r_offset + hi_irelfn->r_addend;
10869 if ((bfd_signed_vma) (local_sda - access_addr) < CONSERVATIVE_20BIT
10870 && (bfd_signed_vma) (local_sda - access_addr) >= -CONSERVATIVE_20BIT)
10871 eliminate_sethi = 1;
10874 case R_NDS32_TLS_LE_HI20:
10876 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10877 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10878 access_addr -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10879 if ((range_type == NDS32_LOADSTORE_IMM)
10880 && (bfd_signed_vma) (access_addr) < CONSERVATIVE_20BIT
10881 && (bfd_signed_vma) (access_addr) >= -CONSERVATIVE_20BIT)
10882 eliminate_sethi = 1;
10889 /* Delete sethi instruction. */
10890 if (eliminate_sethi == 1
10891 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
10892 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10894 hi_irelfn->r_info =
10895 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
10897 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10903 /* Relax LO12 relocation for nds32_elf_relax_section. */
10906 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
10907 asection *sec, Elf_Internal_Rela *irel,
10908 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
10909 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
10912 bfd_vma local_sda, laddr;
10913 unsigned long reloc;
10914 bfd_vma access_addr;
10915 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10916 Elf_Internal_Rela *irelfn = NULL, *irelend;
10917 struct elf_link_hash_entry *h = NULL;
10920 /* For SDA base relative relaxation. */
10921 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10922 &local_sda, FALSE);
10924 irelend = internal_relocs + sec->reloc_count;
10925 laddr = irel->r_offset;
10926 insn = bfd_getb32 (contents + laddr);
10928 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
10931 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10933 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
10935 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
10936 h = elf_sym_hashes (abfd)[indx];
10939 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
10940 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10942 reloc = R_NDS32_20_RELA;
10943 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10944 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10945 bfd_putb32 (insn, contents + laddr);
10947 /* This is avoid to relax symbol address which is fixed
10948 relocations. Ex: _stack. */
10949 else if (N32_OP6 (insn) == N32_OP6_ORI
10950 && h && bfd_is_abs_section (h->root.u.def.section))
10954 range_l = sdata_range[1][0];
10955 range_h = sdata_range[1][1];
10956 switch (ELF32_R_TYPE (irel->r_info))
10958 case R_NDS32_LO12S0_RELA:
10959 reloc = R_NDS32_SDA19S0_RELA;
10961 case R_NDS32_LO12S1_RELA:
10962 reloc = R_NDS32_SDA18S1_RELA;
10964 case R_NDS32_LO12S2_RELA:
10965 reloc = R_NDS32_SDA17S2_RELA;
10967 case R_NDS32_LO12S2_DP_RELA:
10968 range_l = sdata_range[0][0];
10969 range_h = sdata_range[0][1];
10970 reloc = R_NDS32_SDA12S2_DP_RELA;
10972 case R_NDS32_LO12S2_SP_RELA:
10973 range_l = sdata_range[0][0];
10974 range_h = sdata_range[0][1];
10975 reloc = R_NDS32_SDA12S2_SP_RELA;
10981 /* There are range_h and range_l because linker has to promise
10982 all sections move cross one page together. */
10983 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
10984 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10986 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
10988 /* Maybe we should add R_NDS32_INSN16 reloc type here
10989 or manually do some optimization. sethi can't be
10990 eliminated when updating $gp so the relative ori
10991 needs to be preserved. */
10994 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
10997 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10998 bfd_putb32 (insn, contents + laddr);
11000 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11002 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11003 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11005 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11012 /* Relax low part of PIC instruction pattern. */
11015 nds32_elf_relax_piclo12 (struct bfd_link_info *link_info, bfd *abfd,
11016 asection *sec, Elf_Internal_Rela *irel,
11017 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11018 Elf_Internal_Shdr *symtab_hdr)
11021 bfd_vma local_sda, laddr;
11022 bfd_signed_vma foff;
11023 unsigned long reloc;
11025 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11026 &local_sda, FALSE);
11027 laddr = irel->r_offset;
11028 insn = bfd_getb32 (contents + laddr);
11030 if (N32_OP6 (insn) != N32_OP6_ORI)
11033 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
11035 foff = calculate_got_memory_address (abfd, link_info, irel,
11036 symtab_hdr) - local_sda;
11037 reloc = R_NDS32_GOT20;
11039 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
11041 foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
11042 symtab_hdr) - local_sda;
11043 reloc = R_NDS32_PLT_GOTREL_LO20;
11045 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
11047 foff = calculate_memory_address (abfd, irel, isymbuf,
11048 symtab_hdr) - local_sda;
11049 reloc = R_NDS32_GOTOFF;
11051 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12)
11053 foff = local_sda - sec->output_section->vma + sec->output_offset
11054 + irel->r_offset + irel->r_addend;
11055 reloc = R_NDS32_GOTPC20;
11060 if ((foff < CONSERVATIVE_20BIT) && (foff >= -CONSERVATIVE_20BIT))
11062 /* Turn into MOVI. */
11063 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11064 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11065 bfd_putb32 (insn, contents + laddr);
11069 /* Relax low part of LE TLS instruction pattern. */
11072 nds32_elf_relax_letlslo12 (struct bfd_link_info *link_info, bfd *abfd,
11073 Elf_Internal_Rela *irel,
11074 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11075 Elf_Internal_Shdr *symtab_hdr)
11079 bfd_signed_vma foff;
11080 unsigned long reloc;
11082 laddr = irel->r_offset;
11083 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11084 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
11085 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
11086 insn = bfd_getb32 (contents + laddr);
11088 if ( (bfd_signed_vma) (foff) < CONSERVATIVE_20BIT
11089 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_20BIT)
11091 /* Pattern sethi-ori transform to movi. */
11092 reloc = R_NDS32_TLS_LE_20;
11093 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11094 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11095 bfd_putb32 (insn, contents + laddr);
11099 /* Relax LE TLS calculate address instruction pattern. */
11102 nds32_elf_relax_letlsadd (struct bfd_link_info *link_info, bfd *abfd,
11103 asection *sec, Elf_Internal_Rela *irel,
11104 Elf_Internal_Rela *internal_relocs,
11105 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11106 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11108 /* Local TLS non-pic
11109 sethi ta, hi20(symbol@tpoff) ; TLS_LE_HI20
11110 ori ta, ta, lo12(symbol@tpoff) ; TLS_LE_LO12
11111 add ra, ta, tp ; TLS_LE_ADD */
11115 bfd_signed_vma foff;
11116 Elf_Internal_Rela *i1_irelfn, *irelend;
11118 irelend = internal_relocs + sec->reloc_count;
11119 laddr = irel->r_offset;
11120 insn = bfd_getb32 (contents + laddr);
11121 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11122 R_NDS32_PTR_RESOLVED);
11123 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11124 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
11125 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
11127 /* The range is +/-16k. */
11128 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
11129 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
11131 /* Transform add to addi. */
11132 insn = N32_TYPE2 (ADDI, N32_RT5 (insn), N32_RB5 (insn), 0);
11134 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
11136 bfd_putb32 (insn, contents + laddr);
11137 if (i1_irelfn != irelend)
11139 i1_irelfn->r_addend |= 1;
11145 /* Relax LE TLS load store instruction pattern. */
11148 nds32_elf_relax_letlsls (struct bfd_link_info *link_info, bfd *abfd,
11149 asection *sec, Elf_Internal_Rela *irel,
11150 Elf_Internal_Rela *internal_relocs,
11151 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11152 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11157 bfd_signed_vma foff;
11158 Elf_Internal_Rela *i1_irelfn, *irelend;
11161 irelend = internal_relocs + sec->reloc_count;
11162 laddr = irel->r_offset;
11163 insn = bfd_getb32 (contents + laddr);
11164 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11165 R_NDS32_PTR_RESOLVED);
11166 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11167 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
11168 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
11170 switch ((N32_OP6 (insn) << 8) | (insn & 0xff))
11172 case (N32_OP6_MEM << 8) | N32_MEM_LB:
11173 case (N32_OP6_MEM << 8) | N32_MEM_SB:
11174 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11175 /* The range is +/-16k. */
11176 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
11177 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
11180 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11182 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
11186 /* Fall through. */
11187 case (N32_OP6_MEM << 8) | N32_MEM_LH:
11188 case (N32_OP6_MEM << 8) | N32_MEM_SH:
11189 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11190 /* The range is +/-32k. */
11191 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S1
11192 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S1)
11195 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11197 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S1);
11201 /* Fall through. */
11202 case (N32_OP6_MEM << 8) | N32_MEM_LW:
11203 case (N32_OP6_MEM << 8) | N32_MEM_SW:
11204 /* The range is +/-64k. */
11205 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S2
11206 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S2)
11209 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11211 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S2);
11215 /* Fall through. */
11222 bfd_putb32 (insn, contents + laddr);
11223 if (i1_irelfn != irelend)
11225 i1_irelfn->r_addend |= 1;
11231 /* Relax PTR relocation for nds32_elf_relax_section. */
11234 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11235 Elf_Internal_Rela *internal_relocs, int *insn_len,
11236 int *seq_len, bfd_byte *contents)
11238 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11240 irelend = internal_relocs + sec->reloc_count;
11243 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11244 R_NDS32_PTR_RESOLVED, irel->r_addend);
11246 if (re_irel == irelend)
11248 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11249 (long) irel->r_offset);
11253 if (re_irel->r_addend != 1)
11256 /* Pointed target is relaxed and no longer needs this void *,
11257 change the type to NONE. */
11258 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11260 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11261 not exist, it means only count 1 and remove it directly. */
11262 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11263 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11264 R_NDS32_PTR_COUNT);
11265 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11267 if (count_irel != irelend)
11269 if (--count_irel->r_addend > 0)
11273 if (ptr_irel != irelend)
11276 /* If the PTR_COUNT is already 0, remove current instruction. */
11277 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11282 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11285 nds32_elf_relax_pltgot_suff (struct bfd_link_info *link_info, bfd *abfd,
11286 asection *sec, Elf_Internal_Rela *irel,
11287 Elf_Internal_Rela *internal_relocs,
11288 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11289 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11292 bfd_signed_vma foff;
11293 Elf_Internal_Rela *i1_irelfn, *irelend;
11294 bfd_vma local_sda, laddr;
11296 irelend = internal_relocs + sec->reloc_count;
11297 laddr = irel->r_offset;
11298 insn = bfd_getb32 (contents + laddr);
11300 /* FIXME: It's a little trouble to turn JRAL5 to JAL since
11301 we need additional space. It might be help if we could
11302 borrow some space from instructions to be eliminated
11303 such as sethi, ori, add. */
11304 if (insn & 0x80000000)
11307 if (nds32_elf_check_dup_relocs
11308 (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
11312 find_relocs_at_address (irel, internal_relocs, irelend,
11313 R_NDS32_PTR_RESOLVED);
11316 The boundary should be reduced since the .plt section hasn't
11317 been created and the address of specific entry is still unknown
11318 Maybe the range between the function call and the begin of the
11319 .text section can be used to decide if the .plt is in the range
11320 of function call. */
11322 if (N32_OP6 (insn) == N32_OP6_ALU1
11323 && N32_SUB5 (insn) == N32_ALU1_ADD)
11325 /* Get the value of the symbol referred to by the reloc. */
11326 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11327 &local_sda, FALSE);
11328 foff = (bfd_signed_vma) (calculate_plt_memory_address
11329 (abfd, link_info, isymbuf, irel,
11330 symtab_hdr) - local_sda);
11331 /* This condition only happened when symbol is undefined. */
11335 if (foff < -CONSERVATIVE_19BIT || foff >= CONSERVATIVE_19BIT)
11337 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11338 R_NDS32_PLT_GOTREL_LO19);
11340 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
11342 else if (N32_OP6 (insn) == N32_OP6_JREG
11343 && N32_SUB5 (insn) == N32_JREG_JRAL)
11345 /* Get the value of the symbol referred to by the reloc. */
11347 calculate_plt_offset (abfd, sec, link_info, isymbuf, irel, symtab_hdr);
11348 /* This condition only happened when symbol is undefined. */
11351 if (foff < -CONSERVATIVE_24BIT_S1 || foff >= CONSERVATIVE_24BIT_S1)
11353 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
11359 bfd_putb32 (insn, contents + laddr);
11360 if (i1_irelfn != irelend)
11362 i1_irelfn->r_addend |= 1;
11367 /* Relax GOT_SUFF relocation for nds32_elf_relax_section. */
11370 nds32_elf_relax_got_suff (struct bfd_link_info *link_info, bfd *abfd,
11371 asection *sec, Elf_Internal_Rela *irel,
11372 Elf_Internal_Rela *internal_relocs,
11373 bfd_byte *contents, Elf_Internal_Shdr *symtab_hdr,
11374 bfd_boolean *again)
11377 bfd_signed_vma foff;
11378 Elf_Internal_Rela *i1_irelfn, *irelend;
11379 bfd_vma local_sda, laddr;
11381 irelend = internal_relocs + sec->reloc_count;
11382 laddr = irel->r_offset;
11383 insn = bfd_getb32 (contents + laddr);
11384 if (insn & 0x80000000)
11387 if (nds32_elf_check_dup_relocs
11388 (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
11391 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11392 R_NDS32_PTR_RESOLVED);
11394 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11395 &local_sda, FALSE);
11396 foff = calculate_got_memory_address (abfd, link_info, irel,
11397 symtab_hdr) - local_sda;
11399 if (foff < CONSERVATIVE_19BIT && foff >= -CONSERVATIVE_19BIT)
11401 /* Turn LW to LWI.GP. Change relocation type to R_NDS32_GOT_REL. */
11402 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11404 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
11405 bfd_putb32 (insn, contents + laddr);
11406 if (i1_irelfn != irelend)
11408 i1_irelfn->r_addend |= 1;
11414 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11417 nds32_elf_relax_gotoff_suff (struct bfd_link_info *link_info, bfd *abfd,
11418 asection *sec, Elf_Internal_Rela *irel,
11419 Elf_Internal_Rela *internal_relocs,
11420 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11421 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11423 int opc_insn_gotoff;
11425 bfd_signed_vma foff;
11426 Elf_Internal_Rela *i1_irelfn, *i2_irelfn, *irelend;
11427 bfd_vma local_sda, laddr;
11429 irelend = internal_relocs + sec->reloc_count;
11430 laddr = irel->r_offset;
11431 insn = bfd_getb32 (contents + laddr);
11433 if (insn & 0x80000000)
11436 if (nds32_elf_check_dup_relocs
11437 (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
11440 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11441 R_NDS32_PTR_RESOLVED);
11442 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11443 &local_sda, FALSE);
11444 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11445 foff = foff - local_sda;
11447 if (foff >= CONSERVATIVE_19BIT || foff < -CONSERVATIVE_19BIT)
11450 /* Concatenate opcode and sub-opcode for switch case.
11451 It may be MEM or ALU1. */
11452 opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
11453 switch (opc_insn_gotoff)
11455 case (N32_OP6_MEM << 8) | N32_MEM_LW:
11456 /* 4-byte aligned. */
11457 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11459 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11461 case (N32_OP6_MEM << 8) | N32_MEM_SW:
11462 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
11464 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11466 case (N32_OP6_MEM << 8) | N32_MEM_LH:
11467 /* 2-byte aligned. */
11468 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
11470 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11472 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11473 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
11475 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11477 case (N32_OP6_MEM << 8) | N32_MEM_SH:
11478 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
11480 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11482 case (N32_OP6_MEM << 8) | N32_MEM_LB:
11483 /* 1-byte aligned. */
11484 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
11486 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11488 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11489 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
11491 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11493 case (N32_OP6_MEM << 8) | N32_MEM_SB:
11494 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
11496 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11498 case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD:
11499 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
11501 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11507 bfd_putb32 (insn, contents + laddr);
11508 if (i1_irelfn != irelend)
11510 i1_irelfn->r_addend |= 1;
11513 if ((i2_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11514 R_NDS32_INSN16)) != irelend)
11515 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11520 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11521 Elf_Internal_Rela *internal_relocs,
11522 bfd_byte *contents,
11523 nds32_elf_blank_t **relax_blank_list,
11524 int optimize, int opt_size)
11526 /* This code block is used to adjust 4-byte alignment by relax a pair
11527 of instruction a time.
11529 It recognizes three types of relocations.
11530 1. R_NDS32_LABEL - a aligment.
11531 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11532 3. is_16bit_NOP () - remove a 16-bit instruction. */
11534 /* TODO: It seems currently implementation only support 4-byte aligment.
11535 We should handle any-aligment. */
11537 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11538 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11539 Elf_Internal_Rela rel_temp;
11540 Elf_Internal_Rela *irelend;
11544 /* Checking for branch relaxation relies on the relocations to
11545 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11546 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11547 sizeof (Elf_Internal_Rela), compar_reloc);
11549 irelend = internal_relocs + sec->reloc_count;
11551 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11552 /* FIXME: Can we generate the right order in assembler?
11553 So we don't have to swapping them here. */
11555 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11556 label_rel < irelend; label_rel++)
11558 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11561 /* Find the first reloc has the same offset with label_rel. */
11562 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11565 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11567 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11569 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11572 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11573 && insn_rel < label_rel)
11575 /* Swap the two reloc if the R_NDS32_INSN16 is
11576 before R_NDS32_LABEL. */
11577 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11578 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11579 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11585 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11586 or higher, remove other R_NDS32_LABEL with lower alignment.
11587 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11588 then the R_NDS32_LABEL sequence is broke. */
11589 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11591 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11593 if (label_rel == NULL)
11595 if (tmp_rel->r_addend < 2)
11596 label_rel = tmp_rel;
11599 else if (tmp_rel->r_addend > 1)
11601 /* Remove all LABEL relocation from label_rel to tmp_rel
11602 including relocations with same offset as tmp_rel. */
11603 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel
11604 || tmp2_rel->r_offset == tmp_rel->r_offset; tmp2_rel++)
11606 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11607 && tmp2_rel->r_addend < 2)
11609 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11615 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11617 /* A new INSN16 which can be converted, so clear label_rel. */
11618 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11620 || is_16bit_NOP (abfd, sec, tmp_rel))
11627 /* Optimized for speed and nothing has not been relaxed.
11628 It's time to align labels.
11629 We may convert a 16-bit instruction right before a label to
11630 32-bit, in order to align the label if necessary
11631 all reloc entries has been sorted by r_offset. */
11632 for (irel = internal_relocs; irel < irelend; irel++)
11634 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11635 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11638 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11640 /* A new INSN16 found, resize the old one. */
11641 if (is_convert_32_to_16
11642 (abfd, sec, irel, internal_relocs, irelend, &insn16)
11643 || is_16bit_NOP (abfd, sec, irel))
11647 /* Previous INSN16 reloc exists, reduce its
11649 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11652 nds32_elf_write_16 (abfd, contents, insn_rel,
11653 internal_relocs, irelend, insn16);
11655 if (!insert_nds32_elf_blank_recalc_total
11656 (relax_blank_list, insn_rel->r_offset + 2, 2))
11659 else if (is_16bit_NOP (abfd, sec, insn_rel))
11661 if (!insert_nds32_elf_blank_recalc_total
11662 (relax_blank_list, insn_rel->r_offset, 2))
11666 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11668 /* Save the new one for later use. */
11672 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11675 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11677 /* Search for label. */
11678 int force_relax = 0;
11680 /* Label on 16-bit instruction or optimization
11681 needless, just reset this reloc. */
11682 insn16 = bfd_getb16 (contents + irel->r_offset);
11683 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
11686 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11691 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
11692 irel->r_offset, 1);
11696 /* Check if there is case which can not be aligned. */
11697 if (irel->r_addend == 2 && address & 0x2)
11702 /* Try to align this label. */
11704 if ((irel->r_addend & 0x1f) < 2)
11706 /* Check if there is a INSN16 at the same address.
11707 Label_rel always seats before insn_rel after
11710 /* Search for INSN16 at LABEL location. If INSN16 is at
11711 same location and this LABEL alignment is lower than 2,
11712 the INSN16 can be converted to 2-byte. */
11713 for (tmp_rel = irel;
11714 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
11717 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
11718 && (is_convert_32_to_16
11719 (abfd, sec, tmp_rel, internal_relocs,
11721 || is_16bit_NOP (abfd, sec, tmp_rel)))
11729 if (force_relax || irel->r_addend == 1 || address & 0x2)
11731 /* Label not aligned. */
11732 /* Previous reloc exists, reduce its size to 16-bit. */
11733 if (is_convert_32_to_16 (abfd, sec, insn_rel,
11734 internal_relocs, irelend, &insn16))
11736 nds32_elf_write_16 (abfd, contents, insn_rel,
11737 internal_relocs, irelend, insn16);
11739 if (!insert_nds32_elf_blank_recalc_total
11740 (relax_blank_list, insn_rel->r_offset + 2, 2))
11743 else if (is_16bit_NOP (abfd, sec, insn_rel))
11745 if (!insert_nds32_elf_blank_recalc_total
11746 (relax_blank_list, insn_rel->r_offset, 2))
11751 /* INSN16 reloc is used. */
11757 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
11758 if (insn_rel && (address & 0x2 || opt_size))
11760 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11763 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
11765 if (!insert_nds32_elf_blank_recalc_total
11766 (relax_blank_list, insn_rel->r_offset + 2, 2))
11768 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11771 else if (is_16bit_NOP (abfd, sec, insn_rel))
11773 if (!insert_nds32_elf_blank_recalc_total
11774 (relax_blank_list, insn_rel->r_offset, 2))
11776 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11784 /* Pick relaxation round. */
11787 nds32_elf_pick_relax (bfd_boolean init, asection *sec, bfd_boolean *again,
11788 struct elf_nds32_link_hash_table *table,
11789 struct bfd_link_info *link_info)
11791 static asection *final_sec, *first_sec = NULL;
11792 static bfd_boolean normal_again = FALSE;
11793 static bfd_boolean set = FALSE;
11794 static bfd_boolean first = TRUE;
11795 int round_table[] = {
11796 NDS32_RELAX_NORMAL_ROUND,
11797 NDS32_RELAX_JUMP_IFC_ROUND,
11798 NDS32_RELAX_EX9_BUILD_ROUND,
11799 NDS32_RELAX_EX9_REPLACE_ROUND,
11801 static int pass = 0;
11802 static int relax_round;
11804 /* The new round. */
11805 if (init && first_sec == sec)
11808 normal_again = FALSE;
11813 /* Run an empty run to get the final section. */
11814 relax_round = NDS32_RELAX_EMPTY_ROUND;
11816 /* It has to enter relax again because we can
11817 not make sure what the final turn is. */
11826 /* Not reenter yet. */
11828 return relax_round;
11831 relax_round = round_table[pass];
11833 if (!init && relax_round == NDS32_RELAX_NORMAL_ROUND && *again)
11834 normal_again = TRUE;
11836 if (!init && final_sec == sec)
11838 switch (relax_round)
11840 case NDS32_RELAX_NORMAL_ROUND:
11843 /* Normal relaxation done. */
11844 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11849 else if (table->target_optimize & NDS32_RELAX_EX9_ON)
11851 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11854 else if (table->ex9_import_file)
11856 /* Import ex9 table. */
11857 if (table->update_ex9_table)
11858 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11860 pass += 3; /* NDS32_RELAX_EX9_REPLACE_ROUND */
11861 nds32_elf_ex9_import_table (link_info);
11866 case NDS32_RELAX_JUMP_IFC_ROUND:
11867 if (!nds32_elf_ifc_finish (link_info))
11868 _bfd_error_handler (_("error: Jump IFC Fail."));
11869 if (table->target_optimize & NDS32_RELAX_EX9_ON)
11875 case NDS32_RELAX_EX9_BUILD_ROUND:
11876 nds32_elf_ex9_finish (link_info);
11880 case NDS32_RELAX_EX9_REPLACE_ROUND:
11881 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11883 /* Do jump IFC optimization again. */
11884 if (!nds32_elf_ifc_finish (link_info))
11885 _bfd_error_handler (_("error: Jump IFC Fail."));
11893 return relax_round;
11897 nds32_elf_relax_section (bfd *abfd, asection *sec,
11898 struct bfd_link_info *link_info, bfd_boolean *again)
11900 nds32_elf_blank_t *relax_blank_list = NULL;
11901 Elf_Internal_Shdr *symtab_hdr;
11902 Elf_Internal_Rela *internal_relocs;
11903 Elf_Internal_Rela *irel;
11904 Elf_Internal_Rela *irelend;
11905 Elf_Internal_Sym *isymbuf = NULL;
11906 bfd_byte *contents = NULL;
11907 bfd_boolean result = TRUE;
11913 /* Target dependnet option. */
11914 struct elf_nds32_link_hash_table *table;
11915 int load_store_relax;
11918 relax_blank_list = NULL;
11922 /* Nothing to do for
11923 * relocatable link or
11924 * non-relocatable section or
11925 * non-code section or
11927 * no reloc entry. */
11928 if (bfd_link_relocatable (link_info)
11929 || (sec->flags & SEC_RELOC) == 0
11930 || (sec->flags & SEC_EXCLUDE) == 1
11931 || (sec->flags & SEC_CODE) == 0
11935 /* 09.12.11 Workaround. */
11936 /* We have to adjust align for R_NDS32_LABEL if needed.
11937 The adjust approach only can fix 2-byte align once. */
11938 if (sec->alignment_power > 2)
11941 /* The optimization type to do. */
11943 table = nds32_elf_hash_table (link_info);
11944 relax_round = nds32_elf_pick_relax (TRUE, sec, again, table, link_info);
11945 switch (relax_round)
11947 case NDS32_RELAX_JUMP_IFC_ROUND:
11948 /* Here is the entrance of ifc jump relaxation. */
11949 if (!nds32_elf_ifc_calc (link_info, abfd, sec))
11951 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11954 case NDS32_RELAX_EX9_BUILD_ROUND:
11955 /* Here is the entrance of ex9 relaxation. There are two pass of
11956 ex9 relaxation. The one is to traverse all instructions and build
11957 the hash table. The other one is to compare instructions and replace
11959 if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
11961 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11964 case NDS32_RELAX_EX9_REPLACE_ROUND:
11965 if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
11969 case NDS32_RELAX_EMPTY_ROUND:
11970 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11973 case NDS32_RELAX_NORMAL_ROUND:
11975 if (sec->reloc_count == 0)
11980 /* The begining of general relaxation. */
11982 if (is_SDA_BASE_set == 0)
11985 is_SDA_BASE_set = 1;
11986 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11988 relax_range_measurement (abfd);
11991 if (is_ITB_BASE_set == 0)
11993 /* Set the _ITB_BASE_. */
11994 if (!nds32_elf_ex9_itb_base (link_info))
11996 _bfd_error_handler (_("%B: error: Cannot set _ITB_BASE_"), abfd);
11997 bfd_set_error (bfd_error_bad_value);
12001 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12002 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12003 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12004 TRUE /* keep_memory */);
12005 if (internal_relocs == NULL)
12008 irelend = internal_relocs + sec->reloc_count;
12009 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12010 irelend, R_NDS32_RELAX_ENTRY);
12012 if (irel == irelend)
12015 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12017 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12019 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
12023 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12026 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12030 load_store_relax = table->load_store_relax;
12032 /* Get symbol table and section content. */
12033 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12034 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12037 /* Do relax loop only when finalize is not done.
12038 Take care of relaxable relocs except INSN16. */
12039 for (irel = internal_relocs; irel < irelend; irel++)
12041 int seq_len; /* Original length of instruction sequence. */
12042 int insn_len = 0; /* Final length of instruction sequence. */
12043 bfd_boolean removed;
12046 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12047 && (irel->r_addend & 0x1f) >= 2)
12050 /* Relocation Types
12051 R_NDS32_LONGCALL1 53
12052 R_NDS32_LONGCALL2 54
12053 R_NDS32_LONGCALL3 55
12054 R_NDS32_LONGJUMP1 56
12055 R_NDS32_LONGJUMP2 57
12056 R_NDS32_LONGJUMP3 58
12057 R_NDS32_LOADSTORE 59 */
12058 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12059 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12060 seq_len = GET_SEQ_LEN (irel->r_addend);
12062 /* Relocation Types
12063 R_NDS32_LONGCALL4 107
12064 R_NDS32_LONGCALL5 108
12065 R_NDS32_LONGCALL6 109
12066 R_NDS32_LONGJUMP4 110
12067 R_NDS32_LONGJUMP5 111
12068 R_NDS32_LONGJUMP6 112
12069 R_NDS32_LONGJUMP7 113 */
12070 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12071 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12074 /* Relocation Types
12075 R_NDS32_LO12S0_RELA 30
12076 R_NDS32_LO12S1_RELA 29
12077 R_NDS32_LO12S2_RELA 28
12078 R_NDS32_LO12S2_SP_RELA 71
12079 R_NDS32_LO12S2_DP_RELA 70
12080 R_NDS32_GOT_LO12 46
12081 R_NDS32_GOTOFF_LO12 50
12082 R_NDS32_PLTREL_LO12 65
12083 R_NDS32_PLT_GOTREL_LO12 67
12084 R_NDS32_17IFC_PCREL_RELA 96
12085 R_NDS32_GOT_SUFF 193
12086 R_NDS32_GOTOFF_SUFF 194
12087 R_NDS32_PLT_GOT_SUFF 195
12088 R_NDS32_MULCALL_SUFF 196
12090 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12091 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12092 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12093 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12094 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12095 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12096 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12097 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12098 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12099 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12100 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12101 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12102 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12103 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12104 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS)
12109 insn_len = seq_len;
12112 switch (ELF32_R_TYPE (irel->r_info))
12114 case R_NDS32_LONGCALL1:
12115 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12116 &insn_len, contents, isymbuf,
12119 case R_NDS32_LONGCALL2:
12120 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12121 &insn_len, contents, isymbuf,
12124 case R_NDS32_LONGCALL3:
12125 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12126 &insn_len, contents, isymbuf,
12129 case R_NDS32_LONGJUMP1:
12130 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12131 &insn_len, contents, isymbuf,
12134 case R_NDS32_LONGJUMP2:
12135 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12136 &insn_len, contents, isymbuf,
12139 case R_NDS32_LONGJUMP3:
12140 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12141 &insn_len, contents, isymbuf,
12144 case R_NDS32_LONGCALL4:
12145 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12146 &insn_len, contents, isymbuf,
12149 case R_NDS32_LONGCALL5:
12150 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12151 &insn_len, contents, isymbuf,
12154 case R_NDS32_LONGCALL6:
12155 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12156 &insn_len, contents, isymbuf,
12159 case R_NDS32_LONGJUMP4:
12160 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12161 &insn_len, contents, isymbuf,
12164 case R_NDS32_LONGJUMP5:
12165 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12166 &insn_len, &seq_len, contents,
12167 isymbuf, symtab_hdr);
12169 case R_NDS32_LONGJUMP6:
12170 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12171 &insn_len, &seq_len, contents,
12172 isymbuf, symtab_hdr);
12174 case R_NDS32_LONGJUMP7:
12175 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12176 &insn_len, &seq_len, contents,
12177 isymbuf, symtab_hdr);
12179 case R_NDS32_LOADSTORE:
12180 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12181 internal_relocs, &insn_len,
12182 contents, isymbuf, symtab_hdr,
12185 case R_NDS32_LO12S0_RELA:
12186 case R_NDS32_LO12S1_RELA:
12187 case R_NDS32_LO12S2_DP_RELA:
12188 case R_NDS32_LO12S2_SP_RELA:
12189 case R_NDS32_LO12S2_RELA:
12190 /* Relax for low part. */
12191 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12192 contents, isymbuf, symtab_hdr);
12194 /* It is impossible to delete blank, so just continue. */
12196 case R_NDS32_GOT_LO12:
12197 case R_NDS32_GOTOFF_LO12:
12198 case R_NDS32_PLTREL_LO12:
12199 case R_NDS32_PLT_GOTREL_LO12:
12200 case R_NDS32_GOTPC_LO12:
12201 /* Relax for PIC gp-relative low part. */
12202 nds32_elf_relax_piclo12 (link_info, abfd, sec, irel, contents,
12203 isymbuf, symtab_hdr);
12205 /* It is impossible to delete blank, so just continue. */
12207 case R_NDS32_TLS_LE_LO12:
12208 /* Relax for LE TLS low part. */
12209 nds32_elf_relax_letlslo12 (link_info, abfd, irel, contents,
12210 isymbuf, symtab_hdr);
12212 /* It is impossible to delete blank, so just continue. */
12214 case R_NDS32_TLS_LE_ADD:
12215 nds32_elf_relax_letlsadd (link_info, abfd, sec, irel, internal_relocs,
12216 contents, isymbuf, symtab_hdr, again);
12217 /* It is impossible to delete blank, so just continue. */
12219 case R_NDS32_TLS_LE_LS:
12220 nds32_elf_relax_letlsls (link_info, abfd, sec, irel, internal_relocs,
12221 contents, isymbuf, symtab_hdr, again);
12224 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12225 &insn_len, &seq_len, contents);
12227 case R_NDS32_PLT_GOT_SUFF:
12228 nds32_elf_relax_pltgot_suff (link_info, abfd, sec, irel,
12229 internal_relocs, contents,
12230 isymbuf, symtab_hdr, again);
12231 /* It is impossible to delete blank, so just continue. */
12233 case R_NDS32_GOT_SUFF:
12234 nds32_elf_relax_got_suff (link_info, abfd, sec, irel,
12235 internal_relocs, contents,
12236 symtab_hdr, again);
12237 /* It is impossible to delete blank, so just continue. */
12239 case R_NDS32_GOTOFF_SUFF:
12240 nds32_elf_relax_gotoff_suff (link_info, abfd, sec, irel,
12241 internal_relocs, contents,
12242 isymbuf, symtab_hdr, again);
12243 /* It is impossible to delete blank, so just continue. */
12249 if (removed && seq_len - insn_len > 0)
12251 if (!insert_nds32_elf_blank
12252 (&relax_blank_list, irel->r_offset + insn_len,
12253 seq_len - insn_len))
12259 calc_nds32_blank_total (relax_blank_list);
12261 if (table->relax_fp_as_gp)
12263 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12267 if (*again == FALSE)
12269 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12275 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
12277 if (*again == FALSE)
12279 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12280 &relax_blank_list, optimize, opt_size))
12284 /* It doesn't matter optimize_for_space_no_align anymore.
12285 If object file is assembled with flag '-Os',
12286 the we don't adjust jump-destination on 4-byte boundary. */
12288 if (relax_blank_list)
12290 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12291 relax_blank_list = NULL;
12294 if (*again == FALSE)
12296 /* Closing the section, so we don't relax it anymore. */
12297 bfd_vma sec_size_align;
12298 Elf_Internal_Rela *tmp_rel;
12300 /* Pad to alignment boundary. Only handle current section alignment. */
12301 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12302 & ((-1U) << sec->alignment_power);
12303 if ((sec_size_align - sec->size) & 0x2)
12305 insn16 = NDS32_NOP16;
12306 bfd_putb16 (insn16, contents + sec->size);
12310 while (sec_size_align != sec->size)
12312 insn = NDS32_NOP32;
12313 bfd_putb32 (insn, contents + sec->size);
12317 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12318 irelend, R_NDS32_RELAX_ENTRY);
12319 if (tmp_rel != irelend)
12320 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12322 clean_nds32_elf_blank ();
12326 if (internal_relocs != NULL
12327 && elf_section_data (sec)->relocs != internal_relocs)
12328 free (internal_relocs);
12330 if (contents != NULL
12331 && elf_section_data (sec)->this_hdr.contents != contents)
12334 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12344 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12346 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12347 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12352 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12353 struct bfd_link_info *info,
12354 void *finfo ATTRIBUTE_UNUSED,
12355 bfd_boolean (*func) (void *, const char *,
12356 Elf_Internal_Sym *,
12358 struct elf_link_hash_entry *)
12361 FILE *sym_ld_script = NULL;
12362 struct elf_nds32_link_hash_table *table;
12364 table = nds32_elf_hash_table (info);
12365 sym_ld_script = table->sym_ld_script;
12367 if (check_start_export_sym)
12368 fprintf (sym_ld_script, "}\n");
12373 static enum elf_reloc_type_class
12374 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12375 const asection *rel_sec ATTRIBUTE_UNUSED,
12376 const Elf_Internal_Rela *rela)
12378 switch ((int) ELF32_R_TYPE (rela->r_info))
12380 case R_NDS32_RELATIVE:
12381 return reloc_class_relative;
12382 case R_NDS32_JMP_SLOT:
12383 return reloc_class_plt;
12385 return reloc_class_copy;
12387 return reloc_class_normal;
12391 /* Put target dependent option into info hash table. */
12393 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12394 int relax_fp_as_gp,
12395 int eliminate_gc_relocs,
12396 FILE * sym_ld_script, int load_store_relax,
12397 int target_optimize, int relax_status,
12398 int relax_round, FILE * ex9_export_file,
12399 FILE * ex9_import_file,
12400 int update_ex9_table, int ex9_limit,
12401 bfd_boolean ex9_loop_aware,
12402 bfd_boolean ifc_loop_aware)
12404 struct elf_nds32_link_hash_table *table;
12406 table = nds32_elf_hash_table (link_info);
12410 table->relax_fp_as_gp = relax_fp_as_gp;
12411 table->eliminate_gc_relocs = eliminate_gc_relocs;
12412 table->sym_ld_script = sym_ld_script;
12413 table ->load_store_relax = load_store_relax;
12414 table->target_optimize = target_optimize;
12415 table->relax_status = relax_status;
12416 table->relax_round = relax_round;
12417 table->ex9_export_file = ex9_export_file;
12418 table->ex9_import_file = ex9_import_file;
12419 table->update_ex9_table = update_ex9_table;
12420 table->ex9_limit = ex9_limit;
12421 table->ex9_loop_aware = ex9_loop_aware;
12422 table->ifc_loop_aware = ifc_loop_aware;
12425 /* These functions and data-structures are used for fp-as-gp
12428 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12429 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12430 the read-only section and read-write section. */
12431 #define FAG_WINDOW (508 - 32)
12433 /* An nds32_fag represent a gp-relative access.
12434 We find best fp-base by using a sliding window
12435 to find a base address which can cover most gp-access. */
12438 struct nds32_fag *next; /* NULL-teminated linked list. */
12439 bfd_vma addr; /* The address of this fag. */
12440 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12441 It is used for applying FP7U2_FLAG. */
12442 int count; /* How many times this address is referred.
12443 There should be exactly `count' relocations
12445 int relas_capcity; /* The buffer size of relas.
12446 We use an array instead of linked-list,
12447 and realloc is used to adjust buffer size. */
12451 nds32_fag_init (struct nds32_fag *head)
12453 memset (head, 0, sizeof (struct nds32_fag));
12457 nds32_fag_verify (struct nds32_fag *head)
12459 struct nds32_fag *iter;
12460 struct nds32_fag *prev;
12466 if (prev && prev->addr >= iter->addr)
12467 puts ("Bug in fp-as-gp insertion.");
12473 /* Insert a fag in ascending order.
12474 If a fag of the same address already exists,
12475 they are chained by relas array. */
12478 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12479 Elf_Internal_Rela * rel)
12481 struct nds32_fag *iter;
12482 struct nds32_fag *new_fag;
12483 const int INIT_RELAS_CAP = 4;
12486 iter->next && iter->next->addr <= addr;
12488 /* Find somewhere to insert. */ ;
12490 /* `iter' will be equal to `head' if the list is empty. */
12491 if (iter != head && iter->addr == addr)
12493 /* The address exists in the list.
12494 Insert `rel' into relocation list, relas. */
12496 /* Check whether relas is big enough. */
12497 if (iter->count >= iter->relas_capcity)
12499 iter->relas_capcity *= 2;
12500 iter->relas = bfd_realloc
12501 (iter->relas, iter->relas_capcity * sizeof (void *));
12503 iter->relas[iter->count++] = rel;
12507 /* This is a new address. Create a fag node for it. */
12508 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12509 memset (new_fag, 0, sizeof (*new_fag));
12510 new_fag->addr = addr;
12511 new_fag->count = 1;
12512 new_fag->next = iter->next;
12513 new_fag->relas_capcity = INIT_RELAS_CAP;
12514 new_fag->relas = (Elf_Internal_Rela **)
12515 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12516 new_fag->relas[0] = rel;
12517 iter->next = new_fag;
12519 nds32_fag_verify (head);
12523 nds32_fag_free_list (struct nds32_fag *head)
12525 struct nds32_fag *iter;
12530 struct nds32_fag *tmp = iter;
12538 /* Find the best fp-base address.
12539 The relocation associated with that address is returned,
12540 so we can track the symbol instead of a fixed address.
12542 When relaxation, the address of an datum may change,
12543 because a text section is shrinked, so the data section
12544 moves forward. If the aligments of text and data section
12545 are different, their distance may change too.
12546 Therefore, tracking a fixed address is not appriate. */
12549 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12551 struct nds32_fag *base; /* First fag in the window. */
12552 struct nds32_fag *last; /* First fag outside the window. */
12553 int accu = 0; /* Usage accumulation. */
12554 struct nds32_fag *best; /* Best fag. */
12555 int baccu = 0; /* Best accumulation. */
12557 /* Use first fag for initial, and find the last fag in the window.
12559 In each iteration, we could simply subtract previous fag
12560 and accumulate following fags which are inside the window,
12561 untill we each the end. */
12563 if (head->next == NULL)
12569 /* Initialize base. */
12573 last && last->addr < base->addr + FAG_WINDOW;
12575 accu += last->count;
12579 /* Record the best base in each iteration. */
12582 accu -= base->count;
12584 /* Account fags in window. */
12585 for (/* Nothing. */;
12586 last && last->addr < base->addr + FAG_WINDOW;
12588 accu += last->count;
12590 /* A better fp-base? */
12603 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12604 so we can convert it fo fp-relative access later.
12605 `best_fag' is the best fp-base. Only those inside the window
12606 of best_fag is applied the flag. */
12609 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12610 bfd *abfd, struct nds32_fag *best_fag,
12611 Elf_Internal_Rela *internal_relocs,
12612 Elf_Internal_Rela *irelend)
12614 struct nds32_fag *ifag;
12615 bfd_vma best_fpbase, gp;
12618 output_bfd = abfd->sections->output_section->owner;
12619 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12620 best_fpbase = best_fag->addr;
12622 if (best_fpbase > gp + sdata_range[1][1]
12623 || best_fpbase < gp - sdata_range[1][0])
12626 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12627 so we know they can be converted to lwi37.fp. */
12628 for (ifag = best_fag;
12629 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12633 for (i = 0; i < ifag->count; i++)
12635 Elf_Internal_Rela *insn16_rel;
12636 Elf_Internal_Rela *fag_rel;
12638 fag_rel = ifag->relas[i];
12640 /* Only if this is within the WINDOWS, FP7U2_FLAG
12643 insn16_rel = find_relocs_at_address
12644 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12646 if (insn16_rel != irelend)
12647 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12653 /* Reset INSN16 to clean fp as gp. */
12656 nds32_fag_unmark_relax (struct nds32_fag *fag,
12657 Elf_Internal_Rela *internal_relocs,
12658 Elf_Internal_Rela *irelend)
12660 struct nds32_fag *ifag;
12662 Elf_Internal_Rela *insn16_rel;
12663 Elf_Internal_Rela *fag_rel;
12665 for (ifag = fag; ifag; ifag = ifag->next)
12667 for (i = 0; i < ifag->count; i++)
12669 fag_rel = ifag->relas[i];
12671 /* Restore the INSN16 relocation. */
12672 insn16_rel = find_relocs_at_address
12673 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12675 if (insn16_rel != irelend)
12676 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12681 /* This is the main function of fp-as-gp optimization.
12682 It should be called by relax_section. */
12685 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12686 bfd *abfd, asection *sec,
12687 Elf_Internal_Rela *internal_relocs,
12688 Elf_Internal_Rela *irelend,
12689 Elf_Internal_Sym *isymbuf)
12691 Elf_Internal_Rela *begin_rel = NULL;
12692 Elf_Internal_Rela *irel;
12693 struct nds32_fag fag_head;
12694 Elf_Internal_Shdr *symtab_hdr;
12695 bfd_byte *contents;
12696 bfd_boolean ifc_inside = FALSE;
12698 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12700 /* Per-function fp-base selection.
12701 1. Create a list for all the gp-relative access.
12702 2. Base on those gp-relative address,
12703 find a fp-base which can cover most access.
12704 3. Use the fp-base for fp-as-gp relaxation.
12706 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12708 1. delete the `la $fp, _FP_BASE_' instruction and
12709 2. not convert lwi.gp to lwi37.fp.
12711 To delete the _FP_BASE_ instruction, we simply apply
12712 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12714 To suppress the conversion, we simply NOT to apply
12715 R_NDS32_INSN16_FP7U2_FLAG flag. */
12717 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12719 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12720 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12723 /* Check whether it is worth for fp-as-gp optimization,
12724 i.e., at least 3 gp-load.
12726 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12727 apply this optimization. */
12729 for (irel = internal_relocs; irel < irelend; irel++)
12731 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12732 One we enter the begin of the region, we track all the LW/ST
12733 instructions, so when we leave the region, we try to find
12734 the best fp-base address for those LW/ST instructions. */
12736 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12737 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12739 /* Begin of the region. */
12741 /* xgettext:c-format */
12742 _bfd_error_handler (_("%B: Nested OMIT_FP in %A."), abfd, sec);
12745 nds32_fag_init (&fag_head);
12746 ifc_inside = FALSE;
12748 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12749 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12752 struct nds32_fag *best_fag, *tmp_fag;
12755 /* End of the region.
12756 Check whether it is worth to do fp-as-gp. */
12758 if (begin_rel == NULL)
12760 /* xgettext:c-format */
12761 _bfd_error_handler (_("%B: Unmatched OMIT_FP in %A."), abfd, sec);
12765 accu = nds32_fag_find_base (&fag_head, &best_fag);
12767 /* Clean FP7U2_FLAG because they may set ever. */
12768 tmp_fag = fag_head.next;
12769 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12771 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12772 if (accu < FAG_THRESHOLD
12773 || !nds32_fag_mark_relax (link_info, abfd, best_fag,
12774 internal_relocs, irelend))
12776 /* Not worth to do fp-as-gp. */
12777 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12778 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12779 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12780 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12781 nds32_fag_free_list (&fag_head);
12786 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12787 so we use it to record the distance to the reloction of best
12789 dist = best_fag->relas[0] - begin_rel;
12790 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12791 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12792 relocation. And get the base value when relocating. */
12793 begin_rel->r_addend &= (0x1 << 16) - 1;
12794 begin_rel->r_addend |= dist << 16;
12796 nds32_fag_free_list (&fag_head);
12800 if (begin_rel == NULL || ifc_inside)
12801 /* Skip if we are not in the region of fp-as-gp. */
12804 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12805 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12810 /* A gp-relative access is found. Insert it to the fag-list. */
12812 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
12813 insn = bfd_getb32 (contents + irel->r_offset);
12814 if (!N32_IS_RT3 (insn))
12817 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12818 nds32_fag_insert (&fag_head, addr, irel);
12820 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12824 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12825 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12827 /* Suppress fp as gp when encounter ifc. */
12835 /* Remove unused `la $fp, _FD_BASE_' instruction. */
12838 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12839 Elf_Internal_Rela *internal_relocs,
12840 Elf_Internal_Rela *irelend)
12842 Elf_Internal_Rela *irel;
12843 Elf_Internal_Shdr *symtab_hdr;
12844 bfd_byte *contents = NULL;
12845 nds32_elf_blank_t *relax_blank_list = NULL;
12846 bfd_boolean result = TRUE;
12847 bfd_boolean unused_region = FALSE;
12850 NOTE: Disable fp-as-gp if we encounter ifcall relocations.
12851 * R_NDS32_17IFC_PCREL_RELA
12852 * R_NDS32_10IFCU_PCREL_RELA
12857 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12858 nds32_get_section_contents (abfd, sec, &contents, TRUE);
12860 for (irel = internal_relocs; irel < irelend; irel++)
12862 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
12863 we marked to in previous pass.
12864 DO NOT scan relocations again, since we've alreadly decided it
12865 and set the flag. */
12866 const char *syname;
12870 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12871 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12872 unused_region = TRUE;
12873 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12874 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12875 unused_region = FALSE;
12877 /* We're not in the region. */
12878 if (!unused_region)
12881 /* _FP_BASE_ must be a GLOBAL symbol. */
12882 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
12883 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
12886 /* The symbol name must be _FP_BASE_. */
12887 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
12888 if (strcmp (syname, FP_BASE_NAME) != 0)
12891 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
12893 /* addi.gp $fp, -256 */
12894 insn = bfd_getb32 (contents + irel->r_offset);
12895 if (insn != INSN_ADDIGP_TO_FP)
12898 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
12900 /* addi $fp, $gp, -256 */
12901 insn = bfd_getb32 (contents + irel->r_offset);
12902 if (insn != INSN_ADDI_GP_TO_FP)
12905 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
12907 /* movi $fp, FP_BASE */
12908 insn = bfd_getb32 (contents + irel->r_offset);
12909 if (insn != INSN_MOVI_TO_FP)
12915 /* We got here because a FP_BASE instruction is found. */
12916 if (!insert_nds32_elf_blank_recalc_total
12917 (&relax_blank_list, irel->r_offset, 4))
12922 if (relax_blank_list)
12924 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12925 relax_blank_list = NULL;
12934 /* This is a version of bfd_generic_get_relocated_section_contents.
12935 We need this variety because relaxation will modify the dwarf
12936 infomation. When there is undefined symbol reference error mesage,
12937 linker need to dump line number where the symbol be used. However
12938 the address is be relaxed, it can not get the original dwarf contents.
12939 The variety only modify function call for reading in the section. */
12942 nds32_elf_get_relocated_section_contents (bfd *abfd,
12943 struct bfd_link_info *link_info,
12944 struct bfd_link_order *link_order,
12946 bfd_boolean relocatable,
12949 bfd *input_bfd = link_order->u.indirect.section->owner;
12950 asection *input_section = link_order->u.indirect.section;
12952 arelent **reloc_vector;
12955 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12956 if (reloc_size < 0)
12959 /* Read in the section. */
12960 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
12963 if (reloc_size == 0)
12966 reloc_vector = (arelent **) bfd_malloc (reloc_size);
12967 if (reloc_vector == NULL)
12970 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
12971 reloc_vector, symbols);
12972 if (reloc_count < 0)
12975 if (reloc_count > 0)
12978 for (parent = reloc_vector; *parent != NULL; parent++)
12980 char *error_message = NULL;
12982 bfd_reloc_status_type r;
12984 symbol = *(*parent)->sym_ptr_ptr;
12985 if (symbol->section && discarded_section (symbol->section))
12988 static reloc_howto_type none_howto
12989 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
12990 "unused", FALSE, 0, 0, FALSE);
12992 p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
12993 _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
12995 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
12996 (*parent)->addend = 0;
12997 (*parent)->howto = &none_howto;
13001 r = bfd_perform_relocation (input_bfd, *parent, data,
13003 relocatable ? abfd : NULL,
13008 asection *os = input_section->output_section;
13010 /* A partial link, so keep the relocs. */
13011 os->orelocation[os->reloc_count] = *parent;
13015 if (r != bfd_reloc_ok)
13019 case bfd_reloc_undefined:
13020 (*link_info->callbacks->undefined_symbol)
13021 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13022 input_bfd, input_section, (*parent)->address, TRUE);
13024 case bfd_reloc_dangerous:
13025 BFD_ASSERT (error_message != NULL);
13026 (*link_info->callbacks->reloc_dangerous)
13027 (link_info, error_message,
13028 input_bfd, input_section, (*parent)->address);
13030 case bfd_reloc_overflow:
13031 (*link_info->callbacks->reloc_overflow)
13033 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13034 (*parent)->howto->name, (*parent)->addend,
13035 input_bfd, input_section, (*parent)->address);
13037 case bfd_reloc_outofrange:
13039 This error can result when processing some partially
13040 complete binaries. Do not abort, but issue an error
13041 message instead. */
13042 link_info->callbacks->einfo
13043 /* xgettext:c-format */
13044 (_("%X%P: %B(%A): relocation \"%R\" goes out of range\n"),
13045 abfd, input_section, * parent);
13056 free (reloc_vector);
13060 free (reloc_vector);
13064 /* Link-time IFC relaxation.
13065 In this optimization, we chains jump instructions
13066 of the same destination with ifcall. */
13069 /* List to save jal and j relocation. */
13070 struct elf_nds32_ifc_symbol_entry
13073 struct elf_link_hash_entry *h;
13074 struct elf_nds32_ifc_irel_list *irel_head;
13075 unsigned long insn;
13077 int enable; /* Apply ifc. */
13078 int ex9_enable; /* Apply ifc after ex9. */
13079 struct elf_nds32_ifc_symbol_entry *next;
13082 struct elf_nds32_ifc_irel_list
13084 Elf_Internal_Rela *irel;
13087 /* If this is set, then it is the last instruction for
13088 ifc-chain, so it must be keep for the actual branching. */
13090 struct elf_nds32_ifc_irel_list *next;
13093 static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;
13095 /* Insert symbol of jal and j for ifc. */
13098 nds32_elf_ifc_insert_symbol (asection *sec,
13099 struct elf_link_hash_entry *h,
13100 Elf_Internal_Rela *irel,
13101 unsigned long insn)
13103 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13105 /* Check there is target of existing entry the same as the new one. */
13106 while (ptr != NULL)
13108 if (((h == NULL && ptr->sec == sec
13109 && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
13110 && ptr->irel_head->irel->r_addend == irel->r_addend)
13113 && ptr->insn == insn)
13115 /* The same target exist, so insert into list. */
13116 struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;
13118 while (irel_list->next != NULL)
13119 irel_list = irel_list->next;
13120 irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
13121 irel_list = irel_list->next;
13122 irel_list->irel = irel;
13123 irel_list->keep = 1;
13126 irel_list->sec = NULL;
13128 irel_list->sec = sec;
13129 irel_list->next = NULL;
13132 if (ptr->next == NULL)
13137 /* There is no same target entry, so build a new one. */
13138 if (ifc_symbol_head == NULL)
13140 ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
13141 ptr = ifc_symbol_head;
13145 ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
13150 ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
13151 ptr->irel_head->irel = irel;
13153 ptr->irel_head->keep = 1;
13157 /* Local symbols. */
13159 ptr->irel_head->sec = NULL;
13163 /* Global symbol. */
13165 ptr->irel_head->sec = sec;
13168 ptr->irel_head->next = NULL;
13171 ptr->ex9_enable = 0;
13175 /* Gather all jal and j instructions. */
13178 nds32_elf_ifc_calc (struct bfd_link_info *info,
13179 bfd *abfd, asection *sec)
13181 Elf_Internal_Rela *internal_relocs;
13182 Elf_Internal_Rela *irelend;
13183 Elf_Internal_Rela *irel;
13184 Elf_Internal_Shdr *symtab_hdr;
13185 bfd_byte *contents = NULL;
13186 uint32_t insn, insn_with_reg;
13187 unsigned long r_symndx;
13188 struct elf_link_hash_entry *h;
13189 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
13190 struct elf_nds32_link_hash_table *table;
13191 bfd_boolean ifc_loop_aware;
13193 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13194 TRUE /* keep_memory */);
13195 irelend = internal_relocs + sec->reloc_count;
13196 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13198 /* Check if the object enable ifc. */
13199 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
13200 R_NDS32_RELAX_ENTRY);
13204 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
13205 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
13206 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
13209 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
13212 table = nds32_elf_hash_table (info);
13213 ifc_loop_aware = table->ifc_loop_aware;
13214 while (irel != NULL && irel < irelend)
13216 /* Traverse all relocation and gather all of them to build the list. */
13218 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
13220 if (ifc_loop_aware == 1
13221 && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
13223 /* Check the region if loop or not. If it is true and
13224 ifc-loop-aware is true, ignore the region till region end. */
13225 while (irel != NULL
13227 && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
13228 || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
13233 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
13235 insn = bfd_getb32 (contents + irel->r_offset);
13236 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
13237 r_symndx = ELF32_R_SYM (irel->r_info);
13238 if (r_symndx < symtab_hdr->sh_info)
13240 /* Local symbol. */
13241 nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
13245 /* External symbol. */
13246 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13247 nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
13255 /* Determine whether j and jal should be substituted. */
13258 nds32_elf_ifc_filter (struct bfd_link_info *info)
13260 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13261 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13262 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13263 struct elf_nds32_link_hash_table *table;
13264 int target_optimize;
13267 table = nds32_elf_hash_table (info);
13268 target_optimize = table->target_optimize;
13271 irel_ptr = ptr->irel_head;
13272 if (ptr->h == NULL)
13274 /* Local symbol. */
13275 irel_keeper = irel_ptr;
13276 while (irel_ptr && irel_ptr->next)
13278 /* Check there is jump target can be used. */
13279 if ((irel_ptr->next->irel->r_offset
13280 - irel_keeper->irel->r_offset) > 1022)
13281 irel_keeper = irel_ptr->next;
13285 irel_ptr->keep = 0;
13287 irel_ptr = irel_ptr->next;
13292 /* Global symbol. */
13293 /* We have to get the absolute address and decide
13294 whether to keep it or not. */
13297 address = (irel_ptr->irel->r_offset
13298 + irel_ptr->sec->output_section->vma
13299 + irel_ptr->sec->output_offset);
13300 irel_ptr->addr = address;
13301 irel_ptr = irel_ptr->next;
13304 irel_ptr = ptr->irel_head;
13307 /* Sort by address. */
13308 struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
13309 struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
13310 struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
13311 struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;
13313 /* Get the smallest one. */
13314 while (irel_temp->next)
13316 if (irel_temp->next->addr < irel_dest->addr)
13318 irel_dest_prev = irel_temp;
13319 irel_dest = irel_temp->next;
13321 irel_temp = irel_temp->next;
13324 if (irel_dest != irel_ptr)
13327 irel_ptr_prev->next = irel_dest;
13328 if (irel_dest_prev)
13329 irel_dest_prev->next = irel_ptr;
13330 irel_temp = irel_ptr->next;
13331 irel_ptr->next = irel_dest->next;
13332 irel_dest->next = irel_temp;
13334 irel_ptr_prev = irel_ptr;
13335 irel_ptr = irel_ptr->next;
13338 irel_ptr = ptr->irel_head;
13339 irel_keeper = irel_ptr;
13340 while (irel_ptr && irel_ptr->next)
13342 if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
13343 irel_keeper = irel_ptr->next;
13347 irel_ptr->keep = 0;
13349 irel_ptr = irel_ptr->next;
13353 /* Ex9 enable. Reserve it for ex9. */
13354 if ((target_optimize & NDS32_RELAX_EX9_ON)
13355 && ptr->irel_head != irel_keeper)
13361 /* Determine whether j and jal should be substituted after ex9 done. */
13364 nds32_elf_ifc_filter_after_ex9 (void)
13366 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13367 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13371 if (ptr->enable == 0)
13373 /* Check whether ifc is applied or not. */
13374 irel_ptr = ptr->irel_head;
13375 ptr->ex9_enable = 1;
13378 if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
13381 ptr->ex9_enable = 0;
13384 irel_ptr = irel_ptr->next;
13391 /* Wrapper to do ifc relaxation. */
13394 nds32_elf_ifc_finish (struct bfd_link_info *info)
13397 struct elf_nds32_link_hash_table *table;
13399 table = nds32_elf_hash_table (info);
13400 relax_status = table->relax_status;
13402 if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
13403 nds32_elf_ifc_filter (info);
13405 nds32_elf_ifc_filter_after_ex9 ();
13407 if (!nds32_elf_ifc_replace (info))
13411 table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
13415 /* Traverse the result of ifc filter and replace it with ifcall9. */
13418 nds32_elf_ifc_replace (struct bfd_link_info *info)
13420 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13421 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13422 nds32_elf_blank_t *relax_blank_list = NULL;
13423 bfd_byte *contents = NULL;
13424 Elf_Internal_Rela *internal_relocs;
13425 Elf_Internal_Rela *irel;
13426 Elf_Internal_Rela *irelend;
13427 unsigned short insn16 = INSN_IFCALL9;
13428 struct elf_nds32_link_hash_table *table;
13431 table = nds32_elf_hash_table (info);
13432 relax_status = table->relax_status;
13436 /* Traverse the ifc gather list, and replace the
13437 filter entries by ifcall9. */
13438 if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
13439 || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE)
13440 && ptr->ex9_enable == 1))
13442 irel_ptr = ptr->irel_head;
13443 if (ptr->h == NULL)
13445 /* Local symbol. */
13446 internal_relocs = _bfd_elf_link_read_relocs
13447 (ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
13448 irelend = internal_relocs + ptr->sec->reloc_count;
13450 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13456 if (irel_ptr->keep == 0 && irel_ptr->next)
13458 /* The one can be replaced. We have to check whether
13459 there is any alignment point in the region. */
13460 irel = irel_ptr->irel;
13461 while (((irel_ptr->next->keep == 0
13462 && irel < irel_ptr->next->irel)
13463 || (irel_ptr->next->keep == 1 && irel < irelend))
13464 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13465 && (irel->r_addend & 0x1f) == 2))
13467 if (irel >= irelend
13468 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13469 && (irel->r_addend & 0x1f) == 2
13470 && ((irel->r_offset - get_nds32_elf_blank_total
13471 (&relax_blank_list, irel->r_offset, 1))
13474 /* Replace by ifcall9. */
13475 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13476 if (!insert_nds32_elf_blank_recalc_total
13477 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13479 irel_ptr->irel->r_info =
13480 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13481 R_NDS32_10IFCU_PCREL_RELA);
13484 irel_ptr = irel_ptr->next;
13487 /* Delete the redundant code. */
13488 if (relax_blank_list)
13490 nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
13492 relax_blank_list = NULL;
13497 /* Global symbol. */
13500 if (irel_ptr->keep == 0 && irel_ptr->next)
13502 /* The one can be replaced, and we have to check
13503 whether there is any alignment point in the region. */
13504 internal_relocs = _bfd_elf_link_read_relocs
13505 (irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
13506 TRUE /* keep_memory */);
13507 irelend = internal_relocs + irel_ptr->sec->reloc_count;
13508 if (!nds32_get_section_contents (irel_ptr->sec->owner,
13509 irel_ptr->sec, &contents,
13513 irel = irel_ptr->irel;
13514 while (((irel_ptr->sec == irel_ptr->next->sec
13515 && irel_ptr->next->keep == 0
13516 && irel < irel_ptr->next->irel)
13517 || ((irel_ptr->sec != irel_ptr->next->sec
13518 || irel_ptr->next->keep == 1)
13519 && irel < irelend))
13520 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13521 && (irel->r_addend & 0x1f) == 2))
13523 if (irel >= irelend
13524 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13525 && (irel->r_addend & 0x1f) == 2
13526 && ((irel->r_offset
13527 - get_nds32_elf_blank_total (&relax_blank_list,
13528 irel->r_offset, 1)) & 0x02) == 0))
13530 /* Replace by ifcall9. */
13531 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13532 if (!insert_nds32_elf_blank_recalc_total
13533 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13536 /* Delete the redundant code, and clear the relocation. */
13537 nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
13540 irel_ptr->irel->r_info =
13541 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13542 R_NDS32_10IFCU_PCREL_RELA);
13543 relax_blank_list = NULL;
13547 irel_ptr = irel_ptr->next;
13557 /* Relocate ifcall. */
13560 nds32_elf_ifc_reloc (void)
13562 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13563 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13564 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13565 bfd_vma relocation, address;
13566 unsigned short insn16;
13567 bfd_byte *contents = NULL;
13568 static bfd_boolean done = FALSE;
13577 /* Check the entry is enable ifcall. */
13578 if (ptr->enable == 1 || ptr->ex9_enable == 1)
13580 /* Get the reserve jump. */
13581 irel_ptr = ptr->irel_head;
13584 if (irel_ptr->keep == 1)
13586 irel_keeper = irel_ptr;
13589 irel_ptr = irel_ptr->next;
13592 irel_ptr = ptr->irel_head;
13593 if (ptr->h == NULL)
13595 /* Local symbol. */
13596 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13602 if (irel_ptr->keep == 0
13603 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13605 relocation = irel_keeper->irel->r_offset;
13606 relocation = relocation - irel_ptr->irel->r_offset;
13607 while (irel_keeper && relocation > 1022)
13609 irel_keeper = irel_keeper->next;
13610 if (irel_keeper && irel_keeper->keep == 1)
13612 relocation = irel_keeper->irel->r_offset;
13613 relocation = relocation - irel_ptr->irel->r_offset;
13616 if (relocation > 1022)
13618 /* Double check. */
13619 irel_keeper = ptr->irel_head;
13620 while (irel_keeper)
13622 if (irel_keeper->keep == 1)
13624 relocation = irel_keeper->irel->r_offset;
13625 relocation = relocation - irel_ptr->irel->r_offset;
13627 if (relocation <= 1022)
13629 irel_keeper = irel_keeper->next;
13634 irel_ptr->irel->r_info =
13635 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13637 insn16 = INSN_IFCALL9 | (relocation >> 1);
13638 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13640 irel_ptr = irel_ptr->next;
13645 /* Global symbol. */
13648 if (irel_ptr->keep == 0
13649 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13651 /* Get the distance between ifcall and jump. */
13652 relocation = (irel_keeper->irel->r_offset
13653 + irel_keeper->sec->output_section->vma
13654 + irel_keeper->sec->output_offset);
13655 address = (irel_ptr->irel->r_offset
13656 + irel_ptr->sec->output_section->vma
13657 + irel_ptr->sec->output_offset);
13658 relocation = relocation - address;
13660 /* The distance is over ragne, find callee again. */
13661 while (irel_keeper && relocation > 1022)
13663 irel_keeper = irel_keeper->next;
13664 if (irel_keeper && irel_keeper->keep ==1)
13666 relocation = (irel_keeper->irel->r_offset
13667 + irel_keeper->sec->output_section->vma
13668 + irel_keeper->sec->output_offset);
13669 relocation = relocation - address;
13673 if (relocation > 1022)
13675 /* Double check. */
13676 irel_keeper = ptr->irel_head;
13677 while (irel_keeper)
13679 if (irel_keeper->keep == 1)
13682 relocation = (irel_keeper->irel->r_offset
13683 + irel_keeper->sec->output_section->vma
13684 + irel_keeper->sec->output_offset);
13685 relocation = relocation - address;
13687 if (relocation <= 1022)
13689 irel_keeper = irel_keeper->next;
13694 if (!nds32_get_section_contents
13695 (irel_ptr->sec->owner, irel_ptr->sec, &contents, TRUE))
13697 insn16 = INSN_IFCALL9 | (relocation >> 1);
13698 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13699 irel_ptr->irel->r_info =
13700 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13703 irel_ptr =irel_ptr->next;
13713 /* End of IFC relaxation. */
13715 /* EX9 Instruction Table Relaxation. */
13717 /* Global hash list. */
13718 struct elf_link_hash_entry_list
13720 struct elf_link_hash_entry *h;
13721 struct elf_link_hash_entry_list *next;
13724 /* Save different destination but same insn. */
13725 struct elf_link_hash_entry_mul_list
13727 /* Global symbol times. */
13729 /* Save relocation for each global symbol but useful?? */
13730 Elf_Internal_Rela *irel;
13731 /* For sethi, two sethi may have the same high-part but different low-parts. */
13732 Elf_Internal_Rela rel_backup;
13733 struct elf_link_hash_entry_list *h_list;
13734 struct elf_link_hash_entry_mul_list *next;
13737 /* Instruction hash table. */
13738 struct elf_nds32_code_hash_entry
13740 struct bfd_hash_entry root;
13742 /* For insn that can use relocation or constant ex: sethi. */
13745 struct elf_link_hash_entry_mul_list *m_list;
13746 /* Using r_addend. */
13747 Elf_Internal_Rela *irel;
13748 /* Using r_info. */
13749 Elf_Internal_Rela rel_backup;
13752 /* Instruction count list. */
13753 struct elf_nds32_insn_times_entry
13755 const char *string;
13759 struct elf_link_hash_entry_mul_list *m_list;
13760 Elf_Internal_Rela *irel;
13761 Elf_Internal_Rela rel_backup;
13762 struct elf_nds32_insn_times_entry *next;
13765 /* J and JAL symbol list. */
13766 struct elf_nds32_symbol_entry
13769 unsigned long insn;
13770 struct elf_nds32_symbol_entry *next;
13773 /* Relocation list. */
13774 struct elf_nds32_irel_entry
13776 Elf_Internal_Rela *irel;
13777 struct elf_nds32_irel_entry *next;
13780 /* ex9.it insn need to be fixed. */
13781 struct elf_nds32_ex9_refix
13783 Elf_Internal_Rela *irel;
13785 struct elf_link_hash_entry *h;
13787 struct elf_nds32_ex9_refix *next;
13790 static struct bfd_hash_table ex9_code_table;
13791 static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
13792 static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;
13794 /* EX9 hash function. */
13796 static struct bfd_hash_entry *
13797 nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
13798 struct bfd_hash_table *table,
13799 const char *string)
13801 struct elf_nds32_code_hash_entry *ret;
13803 /* Allocate the structure if it has not already been allocated by a
13807 entry = (struct bfd_hash_entry *)
13808 bfd_hash_allocate (table, sizeof (*ret));
13813 /* Call the allocation method of the superclass. */
13814 entry = bfd_hash_newfunc (entry, table, string);
13818 ret = (struct elf_nds32_code_hash_entry*) entry;
13820 ret->const_insn = 0;
13821 ret->m_list = NULL;
13827 /* Insert ex9 entry
13828 this insert must be stable sorted by times. */
13831 nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
13833 struct elf_nds32_insn_times_entry *temp;
13834 struct elf_nds32_insn_times_entry *temp2;
13836 if (ex9_insn_head == NULL)
13838 ex9_insn_head = ptr;
13843 temp = ex9_insn_head;
13844 temp2 = ex9_insn_head;
13845 while (temp->next &&
13846 (temp->next->times >= ptr->times
13847 || temp->times == -1))
13849 if (temp->times == -1)
13853 if (ptr->times > temp->times && temp->times != -1)
13856 if (temp2->times == -1)
13859 ex9_insn_head = ptr;
13861 else if (temp->next == NULL)
13868 ptr->next = temp->next;
13874 /* Examine each insn times in hash table.
13875 Handle multi-link hash entry.
13877 TODO: This function doesn't assign so much info since it is fake. */
13880 nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
13882 struct elf_nds32_insn_times_entry *ptr;
13885 if (h->m_list == NULL)
13887 /* Local symbol insn or insn without relocation. */
13891 ptr = (struct elf_nds32_insn_times_entry *)
13892 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13893 ptr->times = h->times;
13894 ptr->string = h->root.string;
13895 ptr->m_list = NULL;
13897 ptr->irel = h->irel;
13898 ptr->rel_backup = h->rel_backup;
13899 nds32_elf_ex9_insert_entry (ptr);
13903 /* Global symbol insn. */
13904 /* Only sethi insn has multiple m_list. */
13905 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13910 times += m_list->times;
13911 m_list = m_list->next;
13915 m_list = h->m_list;
13916 ptr = (struct elf_nds32_insn_times_entry *)
13917 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13918 ptr->times = times; /* Use the total times. */
13919 ptr->string = h->root.string;
13920 ptr->m_list = m_list;
13922 ptr->irel = m_list->irel;
13923 ptr->rel_backup = m_list->rel_backup;
13924 nds32_elf_ex9_insert_entry (ptr);
13926 if (h->const_insn == 1)
13928 /* sethi with constant value. */
13932 ptr = (struct elf_nds32_insn_times_entry *)
13933 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13934 ptr->times = h->times;
13935 ptr->string = h->root.string;
13936 ptr->m_list = NULL;
13939 ptr->rel_backup = h->rel_backup;
13940 nds32_elf_ex9_insert_entry (ptr);
13946 /* Count each insn times in hash table.
13947 Handle multi-link hash entry. */
13950 nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
13952 int reservation, times;
13953 unsigned long relocation, min_relocation;
13954 struct elf_nds32_insn_times_entry *ptr;
13956 if (h->m_list == NULL)
13958 /* Local symbol insn or insn without relocation. */
13961 ptr = (struct elf_nds32_insn_times_entry *)
13962 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13963 ptr->times = h->times;
13964 ptr->string = h->root.string;
13965 ptr->m_list = NULL;
13967 ptr->irel = h->irel;
13968 ptr->rel_backup = h->rel_backup;
13969 nds32_elf_ex9_insert_entry (ptr);
13973 /* Global symbol insn. */
13974 /* Only sethi insn has multiple m_list. */
13975 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13977 if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
13978 && m_list->next != NULL)
13980 /* Sethi insn has different symbol or addend but has same hi20. */
13984 min_relocation = 0xffffffff;
13987 /* Get the minimum sethi address
13988 and calculate how many entry the sethi-list have to use. */
13989 if ((m_list->h_list->h->root.type == bfd_link_hash_defined
13990 || m_list->h_list->h->root.type == bfd_link_hash_defweak)
13991 && (m_list->h_list->h->root.u.def.section != NULL
13992 && m_list->h_list->h->root.u.def.section->output_section != NULL))
13994 relocation = (m_list->h_list->h->root.u.def.value +
13995 m_list->h_list->h->root.u.def.section->output_section->vma +
13996 m_list->h_list->h->root.u.def.section->output_offset);
13997 relocation += m_list->irel->r_addend;
14001 if (relocation < min_relocation)
14002 min_relocation = relocation;
14003 times += m_list->times;
14004 m_list = m_list->next;
14006 if (min_relocation < ex9_relax_size)
14007 reservation = (min_relocation >> 12) + 1;
14009 reservation = (min_relocation >> 12)
14010 - ((min_relocation - ex9_relax_size) >> 12) + 1;
14011 if (reservation < (times / 3))
14013 /* Efficient enough to use ex9. */
14016 for (i = reservation ; i > 0; i--)
14018 /* Allocate number of reservation ex9 entry. */
14019 ptr = (struct elf_nds32_insn_times_entry *)
14020 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14021 ptr->times = h->m_list->times / reservation;
14022 ptr->string = h->root.string;
14023 ptr->m_list = h->m_list;
14025 ptr->irel = h->m_list->irel;
14026 ptr->rel_backup = h->m_list->rel_backup;
14027 nds32_elf_ex9_insert_entry (ptr);
14033 /* Normal global symbol that means no different address symbol
14034 using same ex9 entry. */
14035 if (m_list->times >= 3)
14037 ptr = (struct elf_nds32_insn_times_entry *)
14038 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14039 ptr->times = m_list->times;
14040 ptr->string = h->root.string;
14041 ptr->m_list = h->m_list;
14043 ptr->irel = h->m_list->irel;
14044 ptr->rel_backup = h->m_list->rel_backup;
14045 nds32_elf_ex9_insert_entry (ptr);
14049 if (h->const_insn == 1)
14051 /* sethi with constant value. */
14055 ptr = (struct elf_nds32_insn_times_entry *)
14056 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14057 ptr->times = h->times;
14058 ptr->string = h->root.string;
14059 ptr->m_list = NULL;
14062 ptr->rel_backup = h->rel_backup;
14063 nds32_elf_ex9_insert_entry (ptr);
14070 /* Hash table traverse function. */
14073 nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
14077 ex9_code_table.frozen = 1;
14078 for (i = 0; i < ex9_code_table.size; i++)
14080 struct bfd_hash_entry *p;
14082 for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
14083 if (!func ((struct elf_nds32_code_hash_entry *) p))
14087 ex9_code_table.frozen = 0;
14091 /* Give order number to insn list. */
14094 nds32_elf_order_insn_times (struct bfd_link_info *info)
14096 struct elf_nds32_insn_times_entry *ex9_insn;
14097 struct elf_nds32_insn_times_entry *temp = NULL;
14098 struct elf_nds32_link_hash_table *table;
14102 if (ex9_insn_head == NULL)
14105 /* The max number of entries is 512. */
14106 ex9_insn = ex9_insn_head;
14107 table = nds32_elf_hash_table (info);
14108 ex9_limit = table->ex9_limit;
14110 ex9_insn = ex9_insn_head;
14112 while (ex9_insn != NULL && number < ex9_limit)
14114 ex9_insn->order = number;
14117 ex9_insn = ex9_insn->next;
14120 if (ex9_insn && temp)
14123 while (ex9_insn != NULL)
14125 /* Free useless entry. */
14127 ex9_insn = ex9_insn->next;
14132 /* Build .ex9.itable section. */
14135 nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
14137 asection *table_sec;
14138 struct elf_nds32_insn_times_entry *ptr;
14141 bfd_byte *contents = NULL;
14143 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
14144 it_abfd = it_abfd->link.next)
14146 /* Find the section .ex9.itable, and put all entries into it. */
14147 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
14148 if (table_sec != NULL)
14150 if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
14153 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
14156 table_sec->size = number * 4;
14161 elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
14163 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
14167 val = strtol (ptr->string, NULL, 16);
14168 bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
14176 /* Get insn with regs according to relocation type. */
14179 nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
14180 uint32_t insn, uint32_t *insn_with_reg)
14182 reloc_howto_type *howto = NULL;
14185 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
14186 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
14187 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
14189 *insn_with_reg = insn;
14193 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14194 *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
14197 /* Mask number of address bits according to relocation. */
14199 static unsigned long
14200 nds32_elf_irel_mask (Elf_Internal_Rela *irel)
14202 reloc_howto_type *howto = NULL;
14205 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
14206 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
14207 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
14210 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14211 return howto->dst_mask;
14215 nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
14216 struct elf_nds32_irel_entry *irel_ptr)
14218 if (*irel_list == NULL)
14220 *irel_list = irel_ptr;
14221 irel_ptr->next = NULL;
14225 irel_ptr->next = *irel_list;
14226 *irel_list = irel_ptr;
14231 nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
14232 struct elf_link_hash_entry *h, int order)
14234 struct elf_nds32_ex9_refix *ptr;
14236 ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
14240 ptr->order = order;
14243 if (ex9_refix_head == NULL)
14244 ex9_refix_head = ptr;
14247 struct elf_nds32_ex9_refix *temp = ex9_refix_head;
14249 while (temp->next != NULL)
14258 CLEAN_PRE = 1 << 1,
14262 /* Check relocation type if supporting for ex9. */
14265 nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
14266 Elf_Internal_Rela **irel,
14267 Elf_Internal_Rela *irelend,
14268 nds32_elf_blank_t *relax_blank_list,
14269 asection *sec,bfd_vma *off,
14270 bfd_byte *contents)
14272 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
14273 bfd_boolean nested_ex9, nested_loop;
14274 bfd_boolean ex9_loop_aware;
14275 /* We use the highest 1 byte of result to record
14276 how many bytes location counter has to move. */
14278 Elf_Internal_Rela *irel_save = NULL;
14279 struct elf_nds32_link_hash_table *table;
14281 table = nds32_elf_hash_table (info);
14282 ex9_loop_aware = table->ex9_loop_aware;
14284 while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
14286 switch (ELF32_R_TYPE ((*irel)->r_info))
14288 case R_NDS32_RELAX_REGION_BEGIN:
14289 /* Ignore code block. */
14290 nested_ex9 = FALSE;
14291 nested_loop = FALSE;
14292 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
14294 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
14296 /* Check the region if loop or not. If it is true and
14297 ex9-loop-aware is true, ignore the region till region end. */
14298 /* To save the status for in .no_relax ex9 region and
14299 loop region to conform the block can do ex9 relaxation. */
14300 nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
14301 nested_loop = (ex9_loop_aware
14302 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
14303 while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
14306 if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
14308 /* There may be nested region. */
14309 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14311 else if (ex9_loop_aware
14312 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14313 nested_loop = TRUE;
14315 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
14317 /* The end of region. */
14318 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14319 nested_ex9 = FALSE;
14320 else if (ex9_loop_aware
14321 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14322 nested_loop = FALSE;
14324 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
14325 && ((*irel)->r_addend & 0x1f) == 2)
14327 /* Alignment exist in the region. */
14328 result |= CLEAN_PRE;
14329 if (((*irel)->r_offset -
14330 get_nds32_elf_blank_total (&relax_blank_list,
14331 (*irel)->r_offset, 0)) & 0x02)
14332 result |= PUSH_PRE;
14335 if ((*irel) >= irelend)
14338 *off = (*irel)->r_offset;
14340 /* The final instruction in the region, regard this one as data to ignore it. */
14341 result |= DATA_EXIST;
14346 case R_NDS32_LABEL:
14347 if (((*irel)->r_addend & 0x1f) == 2)
14349 /* Check this point is align and decide to do ex9 or not. */
14350 result |= CLEAN_PRE;
14351 if (((*irel)->r_offset -
14352 get_nds32_elf_blank_total (&relax_blank_list,
14353 (*irel)->r_offset, 0)) & 0x02)
14354 result |= PUSH_PRE;
14357 case R_NDS32_32_RELA:
14359 result |= (4 << 24);
14360 result |= DATA_EXIST;
14362 case R_NDS32_16_RELA:
14364 result |= (2 << 24);
14365 result |= DATA_EXIST;
14369 /* The least code alignment is 2. If the data is only one byte,
14370 we have to shift one more byte. */
14371 if ((*irel)->r_addend == 1)
14372 result |= ((*irel)->r_addend << 25) ;
14374 result |= ((*irel)->r_addend << 24) ;
14376 result |= DATA_EXIST;
14379 case R_NDS32_25_PCREL_RELA:
14380 case R_NDS32_SDA16S3_RELA:
14381 case R_NDS32_SDA15S3_RELA:
14382 case R_NDS32_SDA15S3:
14383 case R_NDS32_SDA17S2_RELA:
14384 case R_NDS32_SDA15S2_RELA:
14385 case R_NDS32_SDA12S2_SP_RELA:
14386 case R_NDS32_SDA12S2_DP_RELA:
14387 case R_NDS32_SDA15S2:
14388 case R_NDS32_SDA18S1_RELA:
14389 case R_NDS32_SDA15S1_RELA:
14390 case R_NDS32_SDA15S1:
14391 case R_NDS32_SDA19S0_RELA:
14392 case R_NDS32_SDA15S0_RELA:
14393 case R_NDS32_SDA15S0:
14394 case R_NDS32_HI20_RELA:
14395 case R_NDS32_LO12S0_ORI_RELA:
14396 case R_NDS32_LO12S0_RELA:
14397 case R_NDS32_LO12S1_RELA:
14398 case R_NDS32_LO12S2_RELA:
14399 /* These relocation is supported ex9 relaxation currently. */
14400 /* We have to save the relocation for using later, since we have
14401 to check there is any alignment in the same address. */
14405 /* Not support relocations. */
14406 if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
14407 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE
14408 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_INSN16)
14410 /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
14411 But we have to consider if there is any side-effect. */
14412 if (!(result & DATA_EXIST))
14414 /* We have to confirm there is no data relocation in the
14415 same address. In general case, this won't happen. */
14416 /* We have to do ex9 conservative, for those relocation not
14417 considerd we ignore instruction. */
14418 result |= DATA_EXIST;
14419 if (*(contents + *off) & 0x80)
14420 result |= (2 << 24);
14422 result |= (4 << 24);
14427 if ((*irel) < irelend
14428 && ((*irel) + 1) < irelend
14429 && (*irel)->r_offset == ((*irel) + 1)->r_offset)
14430 /* There are relocations pointing to the same address, we have to
14431 check all of them. */
14443 /* Replace with ex9 instruction. */
14446 nds32_elf_ex9_push_insn (uint16_t insn16, bfd_byte *contents, bfd_vma pre_off,
14447 nds32_elf_blank_t **relax_blank_list,
14448 struct elf_nds32_irel_entry *pre_irel_ptr,
14449 struct elf_nds32_irel_entry **irel_list)
14453 /* Implement the ex9 relaxation. */
14454 bfd_putb16 (insn16, contents + pre_off);
14455 if (!insert_nds32_elf_blank_recalc_total (relax_blank_list,
14458 if (pre_irel_ptr != NULL)
14459 nds32_elf_insert_irel_entry (irel_list, pre_irel_ptr);
14464 /* Replace input file instruction which is in ex9 itable. */
14467 nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
14469 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14470 bfd_byte *contents = NULL;
14472 uint16_t insn16, insn_ex9;
14473 /* `pre_*' are used to track previous instruction that can use ex9.it. */
14474 bfd_vma pre_off = -1;
14475 uint16_t pre_insn16 = 0;
14476 struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
14477 Elf_Internal_Rela *internal_relocs;
14478 Elf_Internal_Rela *irel;
14479 Elf_Internal_Rela *irelend;
14480 Elf_Internal_Shdr *symtab_hdr;
14481 Elf_Internal_Sym *isym = NULL;
14482 nds32_elf_blank_t *relax_blank_list = NULL;
14484 uint32_t insn_with_reg = 0;
14486 uint32_t it_insn_with_reg;
14487 unsigned long r_symndx;
14489 struct elf_nds32_irel_entry *irel_list = NULL;
14490 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
14491 int data_flag, do_replace, save_irel;
14492 struct elf_link_hash_entry_list *h_list;
14495 /* Load section instructions, relocations, and symbol table. */
14496 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
14497 || !nds32_get_local_syms (abfd, sec, &isym))
14500 _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, TRUE /* keep_memory */);
14501 irelend = internal_relocs + sec->reloc_count;
14502 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14506 /* Check if the object enable ex9. */
14507 irel = find_relocs_at_address (internal_relocs, internal_relocs,
14508 irelend, R_NDS32_RELAX_ENTRY);
14510 /* Check this section trigger ex9 relaxation. */
14513 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
14514 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
14515 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
14518 irel = internal_relocs;
14520 /* Check alignment and fetch proper relocation. */
14521 while (off < sec->size)
14523 struct elf_link_hash_entry *h = NULL;
14524 struct elf_nds32_irel_entry *irel_ptr = NULL;
14526 /* Syn the instruction and the relocation. */
14527 while (irel != NULL && irel < irelend && irel->r_offset < off)
14530 data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
14531 relax_blank_list, sec,
14533 if (data_flag & PUSH_PRE)
14534 if (!nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14535 &relax_blank_list, pre_irel_ptr,
14539 if (data_flag & CLEAN_PRE)
14543 pre_irel_ptr = NULL;
14545 if (data_flag & DATA_EXIST)
14547 /* We save the move offset in the highest byte. */
14548 off += (data_flag >> 24);
14552 if (*(contents + off) & 0x80)
14554 /* 2-byte instruction. */
14559 /* Load the instruction and its opcode with register for comparing. */
14560 ex9_insn = ex9_insn_head;
14561 insn = bfd_getb32 (contents + off);
14565 it_insn = strtol (ex9_insn->string, NULL, 16);
14566 it_insn_with_reg = 0;
14570 if (irel != NULL && irel < irelend && irel->r_offset == off)
14572 /* Insn with relocation. */
14573 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
14575 if (ex9_insn->irel != NULL)
14576 nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn,
14577 &it_insn_with_reg);
14579 if (ex9_insn->irel != NULL
14580 && (ELF32_R_TYPE (irel->r_info) ==
14581 ELF32_R_TYPE (ex9_insn->irel->r_info))
14582 && (insn_with_reg == it_insn_with_reg))
14584 /* Insn relocation and format is the same as table entry. */
14586 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
14587 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
14588 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
14589 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
14590 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
14591 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
14592 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
14593 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
14594 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
14595 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
14596 && ELF32_R_TYPE (irel->r_info) <=
14597 R_NDS32_SDA12S2_SP_RELA)
14598 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
14599 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
14601 r_symndx = ELF32_R_SYM (irel->r_info);
14602 if (r_symndx < symtab_hdr->sh_info)
14604 /* Local symbol. */
14605 int shndx = isym[r_symndx].st_shndx;
14607 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14608 if (ex9_insn->sec == isec
14609 && ex9_insn->irel->r_addend == irel->r_addend
14610 && ex9_insn->irel->r_info == irel->r_info)
14618 /* External symbol. */
14619 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14620 if (ex9_insn->m_list)
14622 h_list = ex9_insn->m_list->h_list;
14626 && (ex9_insn->m_list->irel->r_addend ==
14633 h_list = h_list->next;
14638 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
14640 r_symndx = ELF32_R_SYM (irel->r_info);
14641 if (r_symndx < symtab_hdr->sh_info)
14643 /* Local symbols. Compare its base symbol and offset. */
14644 int shndx = isym[r_symndx].st_shndx;
14646 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14647 if (ex9_insn->sec == isec
14648 && ex9_insn->irel->r_addend == irel->r_addend
14649 && ex9_insn->irel->r_info == irel->r_info)
14657 /* External symbol. */
14658 struct elf_link_hash_entry_mul_list *m_list;
14660 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14661 m_list = ex9_insn->m_list;
14665 h_list = m_list->h_list;
14670 && (m_list->irel->r_addend
14671 == irel->r_addend))
14676 && ex9_insn->m_list
14677 && ex9_insn->m_list == ex9_insn->next->m_list)
14679 /* sethi multiple entry must be fixed */
14680 nds32_elf_ex9_insert_fix (sec, irel,
14681 h, ex9_insn->order);
14685 h_list = h_list->next;
14687 m_list = m_list->next;
14693 /* Import table: Check the symbol hash table and the
14694 jump target. Only R_NDS32_25_PCREL_RELA now. */
14695 else if (ex9_insn->times == -1
14696 && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
14698 nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
14699 if (insn_with_reg == it_insn_with_reg)
14702 bfd_vma relocation;
14704 r_symndx = ELF32_R_SYM (irel->r_info);
14705 if (r_symndx >= symtab_hdr->sh_info)
14707 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14708 if ((h->root.type == bfd_link_hash_defined
14709 || h->root.type == bfd_link_hash_defweak)
14710 && h->root.u.def.section != NULL
14711 && h->root.u.def.section->output_section != NULL
14712 && h->root.u.def.section->gc_mark == 1
14713 && bfd_is_abs_section (h->root.u.def.section)
14714 && h->root.u.def.value > sec->size)
14716 relocation = h->root.u.def.value +
14717 h->root.u.def.section->output_section->vma +
14718 h->root.u.def.section->output_offset;
14719 relocation += irel->r_addend;
14720 insn = insn_with_reg
14721 | ((relocation >> 1) & 0xffffff);
14722 snprintf (code, sizeof (code), "%08x", insn);
14723 if (strcmp (code, ex9_insn->string) == 0)
14732 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
14733 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
14734 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
14736 /* These relocations do not have to relocate contens, so it can
14737 be regard as instruction without relocation. */
14738 if (insn == it_insn && ex9_insn->irel == NULL)
14744 /* Instruction without relocation, we only
14745 have to compare their byte code. */
14746 if (insn == it_insn && ex9_insn->irel == NULL)
14750 /* Insntruction match so replacing the code here. */
14751 if (do_replace == 1)
14753 /* There are two formats of ex9 instruction. */
14754 if (ex9_insn->order < 32)
14755 insn_ex9 = INSN_EX9_IT_2;
14757 insn_ex9 = INSN_EX9_IT_1;
14758 insn16 = insn_ex9 | ex9_insn->order;
14760 /* Insert ex9 instruction. */
14761 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14762 &relax_blank_list, pre_irel_ptr,
14765 pre_insn16 = insn16;
14769 /* For instuction with relocation do relax. */
14770 irel_ptr = (struct elf_nds32_irel_entry *)
14771 bfd_malloc (sizeof (struct elf_nds32_irel_entry));
14772 irel_ptr->irel = irel;
14773 irel_ptr->next = NULL;
14774 pre_irel_ptr = irel_ptr;
14777 pre_irel_ptr = NULL;
14780 ex9_insn = ex9_insn->next;
14785 /* Insert ex9 instruction. */
14786 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14787 &relax_blank_list, pre_irel_ptr,
14790 /* Delete the redundant code. */
14791 if (relax_blank_list)
14793 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
14794 relax_blank_list = NULL;
14797 /* Clear the relocation that is replaced by ex9. */
14800 struct elf_nds32_irel_entry *irel_ptr;
14802 irel_ptr = irel_list;
14803 irel_list = irel_ptr->next;
14804 irel_ptr->irel->r_info =
14805 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
14811 /* Initialize ex9 hash table. */
14814 nds32_elf_ex9_init (void)
14816 if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
14817 sizeof (struct elf_nds32_code_hash_entry),
14820 _bfd_error_handler (_("Linker: cannot init ex9 hash table error \n"));
14826 /* Predict how many bytes will be relaxed with ex9 and ifc. */
14829 nds32_elf_ex9_total_relax (struct bfd_link_info *info)
14831 struct elf_nds32_insn_times_entry *ex9_insn;
14832 struct elf_nds32_insn_times_entry *temp;
14833 int target_optimize;
14834 struct elf_nds32_link_hash_table *table;
14836 if (ex9_insn_head == NULL)
14839 table = nds32_elf_hash_table (info);
14840 target_optimize = table->target_optimize;
14841 ex9_insn = ex9_insn_head;
14844 ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
14846 ex9_insn = ex9_insn->next;
14849 ex9_insn_head = NULL;
14851 if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
14853 /* Examine ifc reduce size. */
14854 struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
14855 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
14860 if (ifc_ent->enable == 0)
14863 irel_ptr = ifc_ent->irel_head;
14867 irel_ptr = irel_ptr->next;
14871 ifc_ent = ifc_ent->next;
14873 ex9_relax_size += size;
14877 /* Finish ex9 table. */
14880 nds32_elf_ex9_finish (struct bfd_link_info *link_info)
14882 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14883 nds32_elf_order_insn_times (link_info);
14884 nds32_elf_ex9_total_relax (link_info);
14885 /* Traverse the hash table and count its times. */
14886 nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
14887 nds32_elf_order_insn_times (link_info);
14888 nds32_elf_ex9_build_itable (link_info);
14891 /* Relocate the entries in ex9 table. */
14894 nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
14895 struct bfd_link_info *link_info)
14897 Elf_Internal_Sym *isym = NULL;
14898 bfd_vma relocation = -1;
14899 struct elf_link_hash_entry *h;
14901 if (ptr->m_list != NULL)
14903 /* Global symbol. */
14904 h = ptr->m_list->h_list->h;
14905 if ((h->root.type == bfd_link_hash_defined
14906 || h->root.type == bfd_link_hash_defweak)
14907 && h->root.u.def.section != NULL
14908 && h->root.u.def.section->output_section != NULL)
14911 relocation = h->root.u.def.value +
14912 h->root.u.def.section->output_section->vma +
14913 h->root.u.def.section->output_offset;
14914 relocation += ptr->m_list->irel->r_addend;
14919 else if (ptr->sec !=NULL)
14921 /* Local symbol. */
14922 Elf_Internal_Sym sym;
14923 asection *sec = NULL;
14925 asection *isec_ptr = &isec;
14926 Elf_Internal_Rela irel_backup = *(ptr->irel);
14927 asection *sec_backup = ptr->sec;
14928 bfd *abfd = ptr->sec->owner;
14930 if (!nds32_get_local_syms (abfd, sec, &isym))
14932 isym = isym + ELF32_R_SYM (ptr->irel->r_info);
14934 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14939 /* The purpose is same as elf_link_input_bfd. */
14940 if (isec_ptr != NULL
14941 && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
14942 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
14945 _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
14946 elf_section_data (isec_ptr)->sec_info,
14949 relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
14950 &ptr->sec, ptr->irel);
14951 if (ptr->irel != NULL)
14952 relocation += ptr->irel->r_addend;
14954 /* Restore origin value since there may be some insntructions that
14955 could not be replaced with ex9.it. */
14956 *(ptr->irel) = irel_backup;
14957 ptr->sec = sec_backup;
14963 /* Import ex9 table and build list. */
14966 nds32_elf_ex9_import_table (struct bfd_link_info *info)
14969 bfd_byte *contents;
14970 unsigned long insn;
14971 FILE *ex9_import_file;
14972 int update_ex9_table;
14973 struct elf_nds32_link_hash_table *table;
14975 table = nds32_elf_hash_table (info);
14976 ex9_import_file = table->ex9_import_file;
14977 rewind (table->ex9_import_file);
14979 contents = bfd_malloc (sizeof (bfd_byte) * 4);
14981 /* Read instructions from the input file and build the list. */
14982 while (!feof (ex9_import_file))
14985 struct elf_nds32_insn_times_entry *ptr;
14988 nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
14989 /* Ignore the final byte 0x0a. */
14992 insn = bfd_getb32 (contents);
14993 code = bfd_malloc (sizeof (char) * 9);
14994 snprintf (code, 9, "%08lx", insn);
14995 ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14996 ptr->string = code;
15000 ptr->m_list = NULL;
15001 ptr->rel_backup.r_offset = 0;
15002 ptr->rel_backup.r_info = 0;
15003 ptr->rel_backup.r_addend = 0;
15006 nds32_elf_ex9_insert_entry (ptr);
15010 update_ex9_table = table->update_ex9_table;
15011 if (update_ex9_table == 1)
15013 /* It has to consider of sethi need to use multiple page
15014 but it not be done yet. */
15015 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
15016 nds32_elf_order_insn_times (info);
15020 /* Export ex9 table. */
15023 nds32_elf_ex9_export (struct bfd_link_info *info,
15024 bfd_byte *contents, int size)
15026 FILE *ex9_export_file;
15027 struct elf_nds32_link_hash_table *table;
15029 table = nds32_elf_hash_table (info);
15030 ex9_export_file = table->ex9_export_file;
15031 fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
15032 fclose (ex9_export_file);
15035 /* Adjust relocations of J and JAL in ex9.itable.
15036 Export ex9 table. */
15039 nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
15041 asection *table_sec = NULL;
15042 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
15043 struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
15045 uint32_t insn, insn_with_reg, source_insn;
15046 bfd_byte *contents = NULL, *source_contents = NULL;
15049 int shift, update_ex9_table, offset = 0;
15050 reloc_howto_type *howto = NULL;
15051 Elf_Internal_Rela rel_backup;
15052 unsigned short insn_ex9;
15053 struct elf_nds32_link_hash_table *table;
15054 FILE *ex9_export_file;
15055 static bfd_boolean done = FALSE;
15062 table = nds32_elf_hash_table (link_info);
15064 table->relax_status |= NDS32_RELAX_EX9_DONE;
15067 update_ex9_table = table->update_ex9_table;
15068 /* Generated ex9.itable exactly. */
15069 if (update_ex9_table == 0)
15071 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
15072 it_abfd = it_abfd->link.next)
15074 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
15075 if (table_sec != NULL)
15079 if (table_sec != NULL)
15083 output_bfd = table_sec->output_section->owner;
15084 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15085 if (table_sec->size == 0)
15088 if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
15097 output_bfd = link_info->input_bfds->sections->output_section->owner;
15098 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15099 contents = bfd_malloc (sizeof (bfd_byte) * 2048);
15102 /* Relocate instruction. */
15105 bfd_vma relocation, min_relocation = 0xffffffff;
15107 insn = strtol (ex9_insn->string, NULL, 16);
15109 if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
15111 if (ex9_insn->m_list)
15112 rel_backup = ex9_insn->m_list->rel_backup;
15114 rel_backup = ex9_insn->rel_backup;
15116 nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
15118 bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
15119 (rel_backup.r_info));
15120 shift = howto->rightshift;
15121 if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
15122 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
15123 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
15124 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
15125 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
15127 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15129 insn_with_reg | ((relocation >> shift) &
15130 nds32_elf_irel_mask (&rel_backup));
15131 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15133 else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
15134 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
15135 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
15136 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
15137 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
15138 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
15139 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
15140 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
15142 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15144 insn_with_reg | (((relocation - gp) >> shift) &
15145 nds32_elf_irel_mask (&rel_backup));
15146 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15148 else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
15150 /* Sethi may be multiple entry for one insn. */
15151 if (ex9_insn->next && ex9_insn->m_list
15152 && ex9_insn->m_list == ex9_insn->next->m_list)
15154 struct elf_link_hash_entry_mul_list *m_list;
15155 struct elf_nds32_ex9_refix *fix_ptr;
15156 struct elf_link_hash_entry *h;
15158 temp_ptr = ex9_insn;
15159 temp_ptr2 = ex9_insn;
15160 m_list = ex9_insn->m_list;
15163 h = m_list->h_list->h;
15164 relocation = h->root.u.def.value +
15165 h->root.u.def.section->output_section->vma +
15166 h->root.u.def.section->output_offset;
15167 relocation += m_list->irel->r_addend;
15169 if (relocation < min_relocation)
15170 min_relocation = relocation;
15171 m_list = m_list->next;
15173 relocation = min_relocation;
15175 /* Put insntruction into ex9 table. */
15176 insn = insn_with_reg
15177 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15178 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15179 relocation = relocation + 0x1000; /* hi20 */
15181 while (ex9_insn->next && ex9_insn->m_list
15182 && ex9_insn->m_list == ex9_insn->next->m_list)
15184 /* Multiple sethi. */
15185 ex9_insn = ex9_insn->next;
15188 insn_with_reg | ((relocation >> shift) &
15189 nds32_elf_irel_mask (&rel_backup));
15190 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15191 relocation = relocation + 0x1000; /* hi20 */
15194 fix_ptr = ex9_refix_head;
15197 /* Fix ex9 insn. */
15198 /* temp_ptr2 points to the head of multiple sethi. */
15199 temp_ptr = temp_ptr2;
15200 while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
15202 fix_ptr = fix_ptr->next;
15204 if (fix_ptr->order != temp_ptr->order)
15207 /* Set source insn. */
15209 fix_ptr->h->root.u.def.value +
15210 fix_ptr->h->root.u.def.section->output_section->vma +
15211 fix_ptr->h->root.u.def.section->output_offset;
15212 relocation += fix_ptr->irel->r_addend;
15213 /* sethi imm is imm20s. */
15214 source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);
15218 /* Match entry and source code. */
15219 insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
15220 if (insn == source_insn)
15222 /* Fix the ex9 insn. */
15223 if (temp_ptr->order != fix_ptr->order)
15225 if (!nds32_get_section_contents
15226 (fix_ptr->sec->owner, fix_ptr->sec,
15227 &source_contents, TRUE))
15229 (_("Linker: error cannot fixed ex9 relocation \n"));
15230 if (temp_ptr->order < 32)
15231 insn_ex9 = INSN_EX9_IT_2;
15233 insn_ex9 = INSN_EX9_IT_1;
15234 insn_ex9 = insn_ex9 | temp_ptr->order;
15235 bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
15241 if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
15243 (_("Linker: error cannot fixed ex9 relocation \n"));
15245 temp_ptr = temp_ptr->next;
15248 fix_ptr = fix_ptr->next;
15253 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15254 insn = insn_with_reg
15255 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15256 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15262 /* Insn without relocation does not have to be fixed
15263 if need to update export table. */
15264 if (update_ex9_table == 1)
15265 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15267 ex9_insn = ex9_insn->next;
15271 ex9_export_file = table->ex9_export_file;
15272 if (ex9_export_file != NULL)
15273 nds32_elf_ex9_export (link_info, contents, table_sec->size);
15274 else if (update_ex9_table == 1)
15276 table->ex9_export_file = table->ex9_import_file;
15277 rewind (table->ex9_export_file);
15278 nds32_elf_ex9_export (link_info, contents, size);
15282 /* Generate ex9 hash table. */
15285 nds32_elf_ex9_build_hash_table (bfd *abfd, asection *sec,
15286 struct bfd_link_info *link_info)
15288 Elf_Internal_Rela *internal_relocs;
15289 Elf_Internal_Rela *irelend;
15290 Elf_Internal_Rela *irel;
15291 Elf_Internal_Rela *jrel;
15292 Elf_Internal_Rela rel_backup;
15293 Elf_Internal_Shdr *symtab_hdr;
15294 Elf_Internal_Sym *isym = NULL;
15296 struct elf_link_hash_entry **sym_hashes;
15297 bfd_byte *contents = NULL;
15299 unsigned long r_symndx;
15300 uint32_t insn, insn_with_reg;
15301 struct elf_link_hash_entry *h;
15302 int data_flag, shift, align;
15303 bfd_vma relocation;
15304 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
15305 reloc_howto_type *howto = NULL;
15307 sym_hashes = elf_sym_hashes (abfd);
15308 /* Load section instructions, relocations, and symbol table. */
15309 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
15312 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
15313 TRUE /* keep_memory */);
15314 irelend = internal_relocs + sec->reloc_count;
15315 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
15316 if (!nds32_get_local_syms (abfd, sec, &isym))
15319 /* Check the object if enable ex9. */
15320 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
15321 R_NDS32_RELAX_ENTRY);
15323 /* Check this section trigger ex9 relaxation. */
15326 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
15327 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
15328 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
15331 irel = internal_relocs;
15333 /* Push each insn into hash table. */
15334 while (off < sec->size)
15337 struct elf_nds32_code_hash_entry *entry;
15339 while (irel != NULL && irel < irelend && irel->r_offset < off)
15342 data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend,
15343 NULL, sec, &off, contents);
15344 if (data_flag & DATA_EXIST)
15346 /* We save the move offset in the highest byte. */
15347 off += (data_flag >> 24);
15351 if (*(contents + off) & 0x80)
15360 rel_backup.r_info = 0;
15361 rel_backup.r_offset = 0;
15362 rel_backup.r_addend = 0;
15363 /* Load the instruction and its opcode with register for comparing. */
15364 insn = bfd_getb32 (contents + off);
15366 if (irel != NULL && irel < irelend && irel->r_offset == off)
15368 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
15369 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
15370 shift = howto->rightshift;
15371 align = (1 << shift) - 1;
15372 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
15373 || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
15374 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
15375 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
15376 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
15377 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
15378 ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15379 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15380 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15381 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15382 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15383 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15384 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15385 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15387 r_symndx = ELF32_R_SYM (irel->r_info);
15389 rel_backup = *irel;
15390 if (r_symndx < symtab_hdr->sh_info)
15392 /* Local symbol. */
15393 int shndx = isym[r_symndx].st_shndx;
15395 bfd_vma st_value = (isym + r_symndx)->st_value;
15396 isec = elf_elfsections (abfd)[shndx]->bfd_section;
15397 relocation = (isec->output_section->vma + isec->output_offset
15398 + st_value + irel->r_addend);
15402 /* External symbol. */
15403 bfd_boolean warned ATTRIBUTE_UNUSED;
15404 bfd_boolean ignored ATTRIBUTE_UNUSED;
15405 bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
15408 /* Maybe there is a better way to get h and relocation */
15409 RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
15410 r_symndx, symtab_hdr, sym_hashes,
15411 h, sym_sec, relocation,
15412 unresolved_reloc, warned, ignored);
15413 relocation += irel->r_addend;
15414 if ((h->root.type != bfd_link_hash_defined
15415 && h->root.type != bfd_link_hash_defweak)
15416 || strcmp (h->root.root.string, "_FP_BASE_") == 0)
15423 /* Check for gp relative instruction alignment. */
15424 if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15425 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15426 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15427 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15428 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15429 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15430 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15431 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15434 bfd *output_bfd = sec->output_section->owner;
15435 bfd_reloc_status_type r;
15437 /* If the symbol is in the abs section, the out_bfd will be null.
15438 This happens when the relocation has a symbol@GOTOFF. */
15439 r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15440 if (r != bfd_reloc_ok)
15448 /* Make sure alignment is correct. */
15449 if (relocation & align)
15451 /* Incorrect alignment. */
15453 /* xgettext:c-format */
15454 (_("%s: warning: unaligned small data access. "
15455 "For entry: {%d, %d, %d}, addr = 0x%x, align = 0x%x."),
15456 bfd_get_filename (abfd), irel->r_offset,
15457 irel->r_info, irel->r_addend, relocation, align);
15463 insn = insn_with_reg
15464 | ((relocation >> shift) & nds32_elf_irel_mask (irel));
15466 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
15467 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
15468 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
15470 /* These relocations do not have to relocate contens, so it can
15471 be regard as instruction without relocation. */
15480 snprintf (code, sizeof (code), "%08x", insn);
15482 entry = (struct elf_nds32_code_hash_entry*)
15483 bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
15487 (_("%P%F: failed creating ex9.it %s hash table: %E\n"), code);
15492 if (h->root.type == bfd_link_hash_undefined)
15494 /* Global symbol. */
15495 /* In order to do sethi with different symbol but same value. */
15496 if (entry->m_list == NULL)
15498 struct elf_link_hash_entry_mul_list *m_list_new;
15499 struct elf_link_hash_entry_list *h_list_new;
15501 m_list_new = (struct elf_link_hash_entry_mul_list *)
15502 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15503 h_list_new = (struct elf_link_hash_entry_list *)
15504 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15505 entry->m_list = m_list_new;
15506 m_list_new->h_list = h_list_new;
15507 m_list_new->rel_backup = rel_backup;
15508 m_list_new->times = 1;
15509 m_list_new->irel = jrel;
15510 m_list_new->next = NULL;
15512 h_list_new->next = NULL;
15516 struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
15517 struct elf_link_hash_entry_list *h_list;
15521 /* Build the different symbols that point to the same address. */
15522 h_list = m_list->h_list;
15523 if (h_list->h->root.u.def.value == h->root.u.def.value
15524 && h_list->h->root.u.def.section->output_section->vma
15525 == h->root.u.def.section->output_section->vma
15526 && h_list->h->root.u.def.section->output_offset
15527 == h->root.u.def.section->output_offset
15528 && m_list->rel_backup.r_addend == rel_backup.r_addend)
15531 m_list->irel = jrel;
15532 while (h_list->h != h && h_list->next)
15533 h_list = h_list->next;
15534 if (h_list->h != h)
15536 struct elf_link_hash_entry_list *h_list_new;
15538 h_list_new = (struct elf_link_hash_entry_list *)
15539 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15540 h_list->next = h_list_new;
15542 h_list_new->next = NULL;
15546 /* The sethi case may have different address but the
15547 hi20 is the same. */
15548 else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
15549 && m_list->next == NULL)
15551 struct elf_link_hash_entry_mul_list *m_list_new;
15552 struct elf_link_hash_entry_list *h_list_new;
15554 m_list_new = (struct elf_link_hash_entry_mul_list *)
15555 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15556 h_list_new = (struct elf_link_hash_entry_list *)
15557 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15558 m_list->next = m_list_new;
15559 m_list_new->h_list = h_list_new;
15560 m_list_new->rel_backup = rel_backup;
15561 m_list_new->times = 1;
15562 m_list_new->irel = jrel;
15563 m_list_new->next = NULL;
15565 h_list_new->next = NULL;
15568 m_list = m_list->next;
15579 /* Local symbol and insn without relocation*/
15581 entry->rel_backup = rel_backup;
15584 /* Use in sethi insn with constant and global symbol in same format. */
15586 entry->const_insn = 1;
15588 entry->irel = jrel;
15596 /* Set the _ITB_BASE, and point it to ex9 table. */
15599 nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
15603 bfd *output_bfd = NULL;
15604 struct bfd_link_hash_entry *bh = NULL;
15606 if (is_ITB_BASE_set == 1)
15609 is_ITB_BASE_set = 1;
15611 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_", FALSE, FALSE, TRUE);
15613 if (bh && (bh->type == bfd_link_hash_defined
15614 || bh->type == bfd_link_hash_defweak))
15617 for (abfd = link_info->input_bfds; abfd != NULL;
15618 abfd = abfd->link.next)
15620 sec = bfd_get_section_by_name (abfd, ".ex9.itable");
15623 output_bfd = sec->output_section->owner;
15627 if (output_bfd == NULL)
15629 output_bfd = link_info->output_bfd;
15630 if (output_bfd->sections == NULL)
15633 sec = bfd_abs_section_ptr;
15635 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
15636 FALSE, FALSE, TRUE);
15637 return (_bfd_generic_link_add_one_symbol
15638 (link_info, output_bfd, "_ITB_BASE_",
15639 BSF_GLOBAL | BSF_WEAK, sec, 0,
15640 (const char *) NULL, FALSE, get_elf_backend_data
15641 (output_bfd)->collect, &bh));
15645 #define ELF_ARCH bfd_arch_nds32
15646 #define ELF_MACHINE_CODE EM_NDS32
15647 #define ELF_MAXPAGESIZE 0x1000
15648 #define ELF_TARGET_ID NDS32_ELF_DATA
15650 #define TARGET_BIG_SYM nds32_elf32_be_vec
15651 #define TARGET_BIG_NAME "elf32-nds32be"
15652 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
15653 #define TARGET_LITTLE_NAME "elf32-nds32le"
15655 #define elf_info_to_howto nds32_info_to_howto
15656 #define elf_info_to_howto_rel nds32_info_to_howto_rel
15658 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
15659 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
15660 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
15661 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
15662 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
15664 #define bfd_elf32_mkobject nds32_elf_mkobject
15665 #define elf_backend_action_discarded nds32_elf_action_discarded
15666 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
15667 #define elf_backend_check_relocs nds32_elf_check_relocs
15668 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
15669 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
15670 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
15671 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
15672 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
15673 #define elf_backend_relocate_section nds32_elf_relocate_section
15674 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
15675 #define elf_backend_gc_sweep_hook nds32_elf_gc_sweep_hook
15676 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
15677 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
15678 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
15679 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
15680 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
15681 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
15682 #define elf_backend_object_p nds32_elf_object_p
15683 #define elf_backend_final_write_processing nds32_elf_final_write_processing
15684 #define elf_backend_special_sections nds32_elf_special_sections
15685 #define bfd_elf32_bfd_get_relocated_section_contents \
15686 nds32_elf_get_relocated_section_contents
15688 #define elf_backend_can_gc_sections 1
15689 #define elf_backend_can_refcount 1
15690 #define elf_backend_want_got_plt 1
15691 #define elf_backend_plt_readonly 1
15692 #define elf_backend_want_plt_sym 0
15693 #define elf_backend_got_header_size 12
15694 #define elf_backend_may_use_rel_p 1
15695 #define elf_backend_default_use_rela_p 1
15696 #define elf_backend_may_use_rela_p 1
15698 #include "elf32-target.h"
15700 #undef ELF_MAXPAGESIZE
15701 #define ELF_MAXPAGESIZE 0x2000
15703 #undef TARGET_BIG_SYM
15704 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
15705 #undef TARGET_BIG_NAME
15706 #define TARGET_BIG_NAME "elf32-nds32be-linux"
15707 #undef TARGET_LITTLE_SYM
15708 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
15709 #undef TARGET_LITTLE_NAME
15710 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
15712 #define elf32_bed elf32_nds32_lin_bed
15714 #include "elf32-target.h"