1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2019 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
28 #include "libiberty.h"
29 #include "elf/nds32.h"
30 #include "opcode/nds32.h"
31 #include "elf32-nds32.h"
32 #include "opcode/cgen.h"
33 #include "../opcodes/nds32-opc.h"
35 /* Relocation HOWTO functions. */
36 static bfd_reloc_status_type nds32_elf_ignore_reloc
37 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
38 static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type nds32_elf_hi20_reloc
41 (bfd *, arelent *, asymbol *, void *,
42 asection *, bfd *, char **);
43 static bfd_reloc_status_type nds32_elf_lo12_reloc
44 (bfd *, arelent *, asymbol *, void *,
45 asection *, bfd *, char **);
46 static bfd_reloc_status_type nds32_elf_generic_reloc
47 (bfd *, arelent *, asymbol *, void *,
48 asection *, bfd *, char **);
49 static bfd_reloc_status_type nds32_elf_sda15_reloc
50 (bfd *, arelent *, asymbol *, void *,
51 asection *, bfd *, char **);
53 /* Helper functions for HOWTO. */
54 static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
55 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
56 asection *, bfd_vma, bfd_vma);
58 /* Nds32 helper functions. */
59 static bfd_vma calculate_memory_address
60 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
61 static int nds32_get_section_contents (bfd *, asection *,
62 bfd_byte **, bfd_boolean);
63 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
65 static bfd_boolean nds32_relax_fp_as_gp
66 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
67 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
68 Elf_Internal_Sym *isymbuf);
69 static bfd_boolean nds32_fag_remove_unused_fpbase
70 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
71 Elf_Internal_Rela *irelend);
75 MACH_V1 = bfd_mach_n1h,
76 MACH_V2 = bfd_mach_n1h_v2,
77 MACH_V3 = bfd_mach_n1h_v3,
78 MACH_V3M = bfd_mach_n1h_v3m
81 #define MIN(a, b) ((a) > (b) ? (b) : (a))
82 #define MAX(a, b) ((a) > (b) ? (a) : (b))
84 /* The name of the dynamic interpreter. This is put in the .interp
86 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
88 #define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
89 && (flags) & SEC_LOAD \
90 && (flags) & SEC_READONLY)
92 /* The nop opcode we use. */
93 #define NDS32_NOP32 0x40000009
94 #define NDS32_NOP16 0x9200
96 /* The size in bytes of an entry in the procedure linkage table. */
97 #define PLT_ENTRY_SIZE 24
98 #define PLT_HEADER_SIZE 24
100 /* The first entry in a procedure linkage table are reserved,
101 and the initial contents are unimportant (we zero them out).
102 Subsequent entries look like this. */
103 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
104 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
105 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
106 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
107 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
109 /* $ta is change to $r15 (from $r25). */
110 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
111 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
112 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
113 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
114 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
115 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
117 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
118 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
119 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
120 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
121 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
123 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
124 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
125 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
126 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
127 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
128 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
130 /* These are macros used to get the relocation accurate value. */
131 #define ACCURATE_8BIT_S1 (0x100)
132 #define ACCURATE_U9BIT_S1 (0x400)
133 #define ACCURATE_12BIT_S1 (0x2000)
134 #define ACCURATE_14BIT_S1 (0x4000)
135 #define ACCURATE_19BIT (0x40000)
137 /* These are macros used to get the relocation conservative value. */
138 #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
139 #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
140 #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
141 #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
142 /* These must be more conservative because the address may be in
143 different segment. */
144 #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
145 #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
146 #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
147 #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
148 #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
150 /* Size of small data/bss sections, used to calculate SDA_BASE. */
151 static long got_size = 0;
152 static int is_SDA_BASE_set = 0;
154 /* Convert ELF-VER in eflags to string for debugging purpose. */
155 static const char *const nds32_elfver_strtab[] =
162 /* The nds32 linker needs to keep track of the number of relocs that it
163 decides to copy in check_relocs for each symbol. This is so that
164 it can discard PC relative relocs if it doesn't need them when
165 linking with -Bsymbolic. We store the information in a field
166 extending the regular ELF linker hash table. */
168 /* This structure keeps track of the number of PC relative relocs we
169 have copied for a given symbol. */
171 struct elf_nds32_pcrel_relocs_copied
174 struct elf_nds32_pcrel_relocs_copied *next;
175 /* A section in dynobj. */
177 /* Number of relocs copied in this section. */
181 enum elf_nds32_tls_type
184 GOT_NORMAL = (1 << 0),
185 GOT_TLS_LE = (1 << 1),
186 GOT_TLS_IE = (1 << 2),
187 GOT_TLS_IEGP = (1 << 3),
188 GOT_TLS_LD = (1 << 4),
189 GOT_TLS_GD = (1 << 5),
190 GOT_TLS_DESC = (1 << 6),
193 /* Nds32 ELF linker hash entry. */
195 struct elf_nds32_link_hash_entry
197 struct elf_link_hash_entry root;
199 /* Track dynamic relocs copied for this symbol. */
200 struct elf_dyn_relocs *dyn_relocs;
202 /* For checking relocation type. */
203 enum elf_nds32_tls_type tls_type;
208 /* Get the nds32 ELF linker hash table from a link_info structure. */
210 #define FP_BASE_NAME "_FP_BASE_"
211 static int check_start_export_sym = 0;
213 /* The offset for executable tls relaxation. */
214 #define TP_OFFSET 0x0
223 } elf32_nds32_relax_group_t;
225 struct elf_nds32_obj_tdata
227 struct elf_obj_tdata root;
229 /* tls_type for each local got entry. */
230 char *local_got_tls_type;
232 /* GOTPLT entries for TLS descriptors. */
233 bfd_vma *local_tlsdesc_gotent;
235 /* for R_NDS32_RELAX_GROUP handling. */
236 elf32_nds32_relax_group_t relax_group;
238 unsigned int hdr_size;
242 #define elf_nds32_tdata(bfd) \
243 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
245 #define elf32_nds32_local_got_tls_type(bfd) \
246 (elf_nds32_tdata (bfd)->local_got_tls_type)
248 #define elf32_nds32_local_gp_offset(bfd) \
249 (elf_nds32_tdata (bfd)->offset_to_gp)
251 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
253 #define elf32_nds32_relax_group_ptr(bfd) \
254 &(elf_nds32_tdata (bfd)->relax_group)
257 nds32_elf_mkobject (bfd *abfd)
259 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
263 /* Relocations used for relocation. */
264 /* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
265 initialize array nds32_elf_howto_table in any order. The benefit
266 is that we can add any new relocations with any numbers and don't
267 need to fill the gap by lots of EMPTY_HOWTO. */
268 #define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
269 [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
271 static reloc_howto_type nds32_elf_howto_table[] =
273 /* This reloc does nothing. */
274 HOWTO2 (R_NDS32_NONE, /* type */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
278 FALSE, /* pc_relative */
280 complain_overflow_bitfield,/* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_NDS32_NONE", /* name */
283 FALSE, /* partial_inplace */
286 FALSE), /* pcrel_offset */
288 /* A 16 bit absolute relocation. */
289 HOWTO2 (R_NDS32_16, /* type */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
293 FALSE, /* pc_relative */
295 complain_overflow_bitfield,/* complain_on_overflow */
296 nds32_elf_generic_reloc,/* special_function */
297 "R_NDS32_16", /* name */
298 FALSE, /* partial_inplace */
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
301 FALSE), /* pcrel_offset */
303 /* A 32 bit absolute relocation. */
304 HOWTO2 (R_NDS32_32, /* type */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
308 FALSE, /* pc_relative */
310 complain_overflow_bitfield,/* complain_on_overflow */
311 nds32_elf_generic_reloc,/* special_function */
312 "R_NDS32_32", /* name */
313 FALSE, /* partial_inplace */
314 0xffffffff, /* src_mask */
315 0xffffffff, /* dst_mask */
316 FALSE), /* pcrel_offset */
318 /* A 20 bit address. */
319 HOWTO2 (R_NDS32_20, /* type */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
323 FALSE, /* pc_relative */
325 complain_overflow_unsigned,/* complain_on_overflow */
326 nds32_elf_generic_reloc,/* special_function */
327 "R_NDS32_20", /* name */
328 FALSE, /* partial_inplace */
329 0xfffff, /* src_mask */
330 0xfffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
333 /* An PC Relative 9-bit relocation, shifted by 2.
334 This reloc is complicated because relocations are relative to pc & -4.
335 i.e. branches in the right insn slot use the address of the left insn
337 /* It's not clear whether this should have partial_inplace set or not.
338 Branch relaxing in the assembler can store the addend in the insn,
339 and if bfd_install_relocation gets called the addend may get added
341 HOWTO2 (R_NDS32_9_PCREL, /* type */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
345 TRUE, /* pc_relative */
347 complain_overflow_signed,/* complain_on_overflow */
348 nds32_elf_9_pcrel_reloc,/* special_function */
349 "R_NDS32_9_PCREL", /* name */
350 FALSE, /* partial_inplace */
353 TRUE), /* pcrel_offset */
355 /* A relative 15 bit relocation, right shifted by 1. */
356 HOWTO2 (R_NDS32_15_PCREL, /* type */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
360 TRUE, /* pc_relative */
362 complain_overflow_signed,/* complain_on_overflow */
363 bfd_elf_generic_reloc, /* special_function */
364 "R_NDS32_15_PCREL", /* name */
365 FALSE, /* partial_inplace */
366 0x3fff, /* src_mask */
367 0x3fff, /* dst_mask */
368 TRUE), /* pcrel_offset */
370 /* A relative 17 bit relocation, right shifted by 1. */
371 HOWTO2 (R_NDS32_17_PCREL, /* type */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
375 TRUE, /* pc_relative */
377 complain_overflow_signed,/* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 "R_NDS32_17_PCREL", /* name */
380 FALSE, /* partial_inplace */
381 0xffff, /* src_mask */
382 0xffff, /* dst_mask */
383 TRUE), /* pcrel_offset */
385 /* A relative 25 bit relocation, right shifted by 1. */
386 /* It's not clear whether this should have partial_inplace set or not.
387 Branch relaxing in the assembler can store the addend in the insn,
388 and if bfd_install_relocation gets called the addend may get added
390 HOWTO2 (R_NDS32_25_PCREL, /* type */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
394 TRUE, /* pc_relative */
396 complain_overflow_signed,/* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 "R_NDS32_25_PCREL", /* name */
399 FALSE, /* partial_inplace */
400 0xffffff, /* src_mask */
401 0xffffff, /* dst_mask */
402 TRUE), /* pcrel_offset */
404 /* High 20 bits of address when lower 12 is or'd in. */
405 HOWTO2 (R_NDS32_HI20, /* type */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
409 FALSE, /* pc_relative */
411 complain_overflow_dont,/* complain_on_overflow */
412 nds32_elf_hi20_reloc, /* special_function */
413 "R_NDS32_HI20", /* name */
414 FALSE, /* partial_inplace */
415 0x000fffff, /* src_mask */
416 0x000fffff, /* dst_mask */
417 FALSE), /* pcrel_offset */
419 /* Lower 12 bits of address. */
420 HOWTO2 (R_NDS32_LO12S3, /* type */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
424 FALSE, /* pc_relative */
426 complain_overflow_dont,/* complain_on_overflow */
427 nds32_elf_lo12_reloc, /* special_function */
428 "R_NDS32_LO12S3", /* name */
429 FALSE, /* partial_inplace */
430 0x000001ff, /* src_mask */
431 0x000001ff, /* dst_mask */
432 FALSE), /* pcrel_offset */
434 /* Lower 12 bits of address. */
435 HOWTO2 (R_NDS32_LO12S2, /* type */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
439 FALSE, /* pc_relative */
441 complain_overflow_dont,/* complain_on_overflow */
442 nds32_elf_lo12_reloc, /* special_function */
443 "R_NDS32_LO12S2", /* name */
444 FALSE, /* partial_inplace */
445 0x000003ff, /* src_mask */
446 0x000003ff, /* dst_mask */
447 FALSE), /* pcrel_offset */
449 /* Lower 12 bits of address. */
450 HOWTO2 (R_NDS32_LO12S1, /* type */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
454 FALSE, /* pc_relative */
456 complain_overflow_dont,/* complain_on_overflow */
457 nds32_elf_lo12_reloc, /* special_function */
458 "R_NDS32_LO12S1", /* name */
459 FALSE, /* partial_inplace */
460 0x000007ff, /* src_mask */
461 0x000007ff, /* dst_mask */
462 FALSE), /* pcrel_offset */
464 /* Lower 12 bits of address. */
465 HOWTO2 (R_NDS32_LO12S0, /* type */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
469 FALSE, /* pc_relative */
471 complain_overflow_dont,/* complain_on_overflow */
472 nds32_elf_lo12_reloc, /* special_function */
473 "R_NDS32_LO12S0", /* name */
474 FALSE, /* partial_inplace */
475 0x00000fff, /* src_mask */
476 0x00000fff, /* dst_mask */
477 FALSE), /* pcrel_offset */
479 /* Small data area 15 bits offset. */
480 HOWTO2 (R_NDS32_SDA15S3, /* type */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
484 FALSE, /* pc_relative */
486 complain_overflow_signed,/* complain_on_overflow */
487 nds32_elf_sda15_reloc, /* special_function */
488 "R_NDS32_SDA15S3", /* name */
489 FALSE, /* partial_inplace */
490 0x00007fff, /* src_mask */
491 0x00007fff, /* dst_mask */
492 FALSE), /* pcrel_offset */
494 /* Small data area 15 bits offset. */
495 HOWTO2 (R_NDS32_SDA15S2, /* type */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
499 FALSE, /* pc_relative */
501 complain_overflow_signed,/* complain_on_overflow */
502 nds32_elf_sda15_reloc, /* special_function */
503 "R_NDS32_SDA15S2", /* name */
504 FALSE, /* partial_inplace */
505 0x00007fff, /* src_mask */
506 0x00007fff, /* dst_mask */
507 FALSE), /* pcrel_offset */
509 /* Small data area 15 bits offset. */
510 HOWTO2 (R_NDS32_SDA15S1, /* type */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
514 FALSE, /* pc_relative */
516 complain_overflow_signed,/* complain_on_overflow */
517 nds32_elf_sda15_reloc, /* special_function */
518 "R_NDS32_SDA15S1", /* name */
519 FALSE, /* partial_inplace */
520 0x00007fff, /* src_mask */
521 0x00007fff, /* dst_mask */
522 FALSE), /* pcrel_offset */
524 /* Small data area 15 bits offset. */
525 HOWTO2 (R_NDS32_SDA15S0, /* type */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
529 FALSE, /* pc_relative */
531 complain_overflow_signed,/* complain_on_overflow */
532 nds32_elf_sda15_reloc, /* special_function */
533 "R_NDS32_SDA15S0", /* name */
534 FALSE, /* partial_inplace */
535 0x00007fff, /* src_mask */
536 0x00007fff, /* dst_mask */
537 FALSE), /* pcrel_offset */
539 /* GNU extension to record C++ vtable hierarchy */
540 HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
544 FALSE, /* pc_relative */
546 complain_overflow_dont,/* complain_on_overflow */
547 NULL, /* special_function */
548 "R_NDS32_GNU_VTINHERIT",/* name */
549 FALSE, /* partial_inplace */
552 FALSE), /* pcrel_offset */
554 /* GNU extension to record C++ vtable member usage */
555 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
559 FALSE, /* pc_relative */
561 complain_overflow_dont,/* complain_on_overflow */
562 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
563 "R_NDS32_GNU_VTENTRY", /* name */
564 FALSE, /* partial_inplace */
567 FALSE), /* pcrel_offset */
569 /* A 16 bit absolute relocation. */
570 HOWTO2 (R_NDS32_16_RELA, /* type */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
574 FALSE, /* pc_relative */
576 complain_overflow_bitfield,/* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_NDS32_16_RELA", /* name */
579 FALSE, /* partial_inplace */
580 0xffff, /* src_mask */
581 0xffff, /* dst_mask */
582 FALSE), /* pcrel_offset */
584 /* A 32 bit absolute relocation. */
585 HOWTO2 (R_NDS32_32_RELA, /* type */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
589 FALSE, /* pc_relative */
591 complain_overflow_bitfield,/* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_NDS32_32_RELA", /* name */
594 FALSE, /* partial_inplace */
595 0xffffffff, /* src_mask */
596 0xffffffff, /* dst_mask */
597 FALSE), /* pcrel_offset */
599 /* A 20 bit address. */
600 HOWTO2 (R_NDS32_20_RELA, /* type */
602 2, /* size (0 = byte, 1 = short, 2 = long) */
604 FALSE, /* pc_relative */
606 complain_overflow_signed,/* complain_on_overflow */
607 bfd_elf_generic_reloc, /* special_function */
608 "R_NDS32_20_RELA", /* name */
609 FALSE, /* partial_inplace */
610 0xfffff, /* src_mask */
611 0xfffff, /* dst_mask */
612 FALSE), /* pcrel_offset */
614 HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
616 1, /* size (0 = byte, 1 = short, 2 = long) */
618 TRUE, /* pc_relative */
620 complain_overflow_signed,/* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_NDS32_9_PCREL_RELA",/* name */
623 FALSE, /* partial_inplace */
626 TRUE), /* pcrel_offset */
628 /* A relative 15 bit relocation, right shifted by 1. */
629 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
633 TRUE, /* pc_relative */
635 complain_overflow_signed,/* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_NDS32_15_PCREL_RELA",/* name */
638 FALSE, /* partial_inplace */
639 0x3fff, /* src_mask */
640 0x3fff, /* dst_mask */
641 TRUE), /* pcrel_offset */
643 /* A relative 17 bit relocation, right shifted by 1. */
644 HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
648 TRUE, /* pc_relative */
650 complain_overflow_signed,/* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_NDS32_17_PCREL_RELA",/* name */
653 FALSE, /* partial_inplace */
654 0xffff, /* src_mask */
655 0xffff, /* dst_mask */
656 TRUE), /* pcrel_offset */
658 /* A relative 25 bit relocation, right shifted by 2. */
659 HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
663 TRUE, /* pc_relative */
665 complain_overflow_signed,/* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_NDS32_25_PCREL_RELA",/* name */
668 FALSE, /* partial_inplace */
669 0xffffff, /* src_mask */
670 0xffffff, /* dst_mask */
671 TRUE), /* pcrel_offset */
673 /* High 20 bits of address when lower 16 is or'd in. */
674 HOWTO2 (R_NDS32_HI20_RELA, /* type */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
678 FALSE, /* pc_relative */
680 complain_overflow_dont,/* complain_on_overflow */
681 bfd_elf_generic_reloc, /* special_function */
682 "R_NDS32_HI20_RELA", /* name */
683 FALSE, /* partial_inplace */
684 0x000fffff, /* src_mask */
685 0x000fffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
688 /* Lower 12 bits of address. */
689 HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
693 FALSE, /* pc_relative */
695 complain_overflow_dont,/* complain_on_overflow */
696 bfd_elf_generic_reloc, /* special_function */
697 "R_NDS32_LO12S3_RELA", /* name */
698 FALSE, /* partial_inplace */
699 0x000001ff, /* src_mask */
700 0x000001ff, /* dst_mask */
701 FALSE), /* pcrel_offset */
703 /* Lower 12 bits of address. */
704 HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
708 FALSE, /* pc_relative */
710 complain_overflow_dont,/* complain_on_overflow */
711 bfd_elf_generic_reloc, /* special_function */
712 "R_NDS32_LO12S2_RELA", /* name */
713 FALSE, /* partial_inplace */
714 0x000003ff, /* src_mask */
715 0x000003ff, /* dst_mask */
716 FALSE), /* pcrel_offset */
718 /* Lower 12 bits of address. */
719 HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
723 FALSE, /* pc_relative */
725 complain_overflow_dont,/* complain_on_overflow */
726 bfd_elf_generic_reloc, /* special_function */
727 "R_NDS32_LO12S1_RELA", /* name */
728 FALSE, /* partial_inplace */
729 0x000007ff, /* src_mask */
730 0x000007ff, /* dst_mask */
731 FALSE), /* pcrel_offset */
733 /* Lower 12 bits of address. */
734 HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
738 FALSE, /* pc_relative */
740 complain_overflow_dont,/* complain_on_overflow */
741 bfd_elf_generic_reloc, /* special_function */
742 "R_NDS32_LO12S0_RELA", /* name */
743 FALSE, /* partial_inplace */
744 0x00000fff, /* src_mask */
745 0x00000fff, /* dst_mask */
746 FALSE), /* pcrel_offset */
748 /* Small data area 15 bits offset. */
749 HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
751 2, /* size (0 = byte, 1 = short, 2 = long) */
753 FALSE, /* pc_relative */
755 complain_overflow_signed,/* complain_on_overflow */
756 bfd_elf_generic_reloc, /* special_function */
757 "R_NDS32_SDA15S3_RELA",/* name */
758 FALSE, /* partial_inplace */
759 0x00007fff, /* src_mask */
760 0x00007fff, /* dst_mask */
761 FALSE), /* pcrel_offset */
763 /* Small data area 15 bits offset. */
764 HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
766 2, /* size (0 = byte, 1 = short, 2 = long) */
768 FALSE, /* pc_relative */
770 complain_overflow_signed,/* complain_on_overflow */
771 bfd_elf_generic_reloc, /* special_function */
772 "R_NDS32_SDA15S2_RELA",/* name */
773 FALSE, /* partial_inplace */
774 0x00007fff, /* src_mask */
775 0x00007fff, /* dst_mask */
776 FALSE), /* pcrel_offset */
778 HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
782 FALSE, /* pc_relative */
784 complain_overflow_signed,/* complain_on_overflow */
785 bfd_elf_generic_reloc, /* special_function */
786 "R_NDS32_SDA15S1_RELA",/* name */
787 FALSE, /* partial_inplace */
788 0x00007fff, /* src_mask */
789 0x00007fff, /* dst_mask */
790 FALSE), /* pcrel_offset */
792 HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
796 FALSE, /* pc_relative */
798 complain_overflow_signed,/* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_NDS32_SDA15S0_RELA",/* name */
801 FALSE, /* partial_inplace */
802 0x00007fff, /* src_mask */
803 0x00007fff, /* dst_mask */
804 FALSE), /* pcrel_offset */
806 /* GNU extension to record C++ vtable hierarchy */
807 HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
811 FALSE, /* pc_relative */
813 complain_overflow_dont,/* complain_on_overflow */
814 NULL, /* special_function */
815 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
816 FALSE, /* partial_inplace */
819 FALSE), /* pcrel_offset */
821 /* GNU extension to record C++ vtable member usage */
822 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
826 FALSE, /* pc_relative */
828 complain_overflow_dont,/* complain_on_overflow */
829 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
830 "R_NDS32_RELA_GNU_VTENTRY",/* name */
831 FALSE, /* partial_inplace */
834 FALSE), /* pcrel_offset */
836 /* Like R_NDS32_20, but referring to the GOT table entry for
838 HOWTO2 (R_NDS32_GOT20, /* type */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
842 FALSE, /* pc_relative */
844 complain_overflow_signed,/* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_NDS32_GOT20", /* name */
847 FALSE, /* partial_inplace */
848 0xfffff, /* src_mask */
849 0xfffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
852 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
853 entry for the symbol. */
854 HOWTO2 (R_NDS32_25_PLTREL, /* type */
856 2, /* size (0 = byte, 1 = short, 2 = long) */
858 TRUE, /* pc_relative */
860 complain_overflow_signed,/* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_NDS32_25_PLTREL", /* name */
863 FALSE, /* partial_inplace */
864 0xffffff, /* src_mask */
865 0xffffff, /* dst_mask */
866 TRUE), /* pcrel_offset */
868 /* This is used only by the dynamic linker. The symbol should exist
869 both in the object being run and in some shared library. The
870 dynamic linker copies the data addressed by the symbol from the
871 shared library into the object, because the object being
872 run has to have the data at some particular address. */
873 HOWTO2 (R_NDS32_COPY, /* type */
875 2, /* size (0 = byte, 1 = short, 2 = long) */
877 FALSE, /* pc_relative */
879 complain_overflow_bitfield,/* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_NDS32_COPY", /* name */
882 FALSE, /* partial_inplace */
883 0xffffffff, /* src_mask */
884 0xffffffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
887 /* Like R_NDS32_20, but used when setting global offset table
889 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
891 2, /* size (0 = byte, 1 = short, 2 = long) */
893 FALSE, /* pc_relative */
895 complain_overflow_bitfield,/* complain_on_overflow */
896 bfd_elf_generic_reloc, /* special_function */
897 "R_NDS32_GLOB_DAT", /* name */
898 FALSE, /* partial_inplace */
899 0xffffffff, /* src_mask */
900 0xffffffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
903 /* Marks a procedure linkage table entry for a symbol. */
904 HOWTO2 (R_NDS32_JMP_SLOT, /* type */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
908 FALSE, /* pc_relative */
910 complain_overflow_bitfield,/* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_NDS32_JMP_SLOT", /* name */
913 FALSE, /* partial_inplace */
914 0xffffffff, /* src_mask */
915 0xffffffff, /* dst_mask */
916 FALSE), /* pcrel_offset */
918 /* Used only by the dynamic linker. When the object is run, this
919 longword is set to the load address of the object, plus the
921 HOWTO2 (R_NDS32_RELATIVE, /* type */
923 2, /* size (0 = byte, 1 = short, 2 = long) */
925 FALSE, /* pc_relative */
927 complain_overflow_bitfield,/* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_NDS32_RELATIVE", /* name */
930 FALSE, /* partial_inplace */
931 0xffffffff, /* src_mask */
932 0xffffffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
935 HOWTO2 (R_NDS32_GOTOFF, /* type */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
939 FALSE, /* pc_relative */
941 complain_overflow_signed,/* complain_on_overflow */
942 bfd_elf_generic_reloc, /* special_function */
943 "R_NDS32_GOTOFF", /* name */
944 FALSE, /* partial_inplace */
945 0xfffff, /* src_mask */
946 0xfffff, /* dst_mask */
947 FALSE), /* pcrel_offset */
949 /* An PC Relative 20-bit relocation used when setting PIC offset
951 HOWTO2 (R_NDS32_GOTPC20, /* type */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
955 TRUE, /* pc_relative */
957 complain_overflow_signed,/* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_NDS32_GOTPC20", /* name */
960 FALSE, /* partial_inplace */
961 0xfffff, /* src_mask */
962 0xfffff, /* dst_mask */
963 TRUE), /* pcrel_offset */
965 /* Like R_NDS32_HI20, but referring to the GOT table entry for
967 HOWTO2 (R_NDS32_GOT_HI20, /* type */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
971 FALSE, /* pc_relative */
973 complain_overflow_dont,/* complain_on_overflow */
974 bfd_elf_generic_reloc, /* special_function */
975 "R_NDS32_GOT_HI20", /* name */
976 FALSE, /* partial_inplace */
977 0x000fffff, /* src_mask */
978 0x000fffff, /* dst_mask */
979 FALSE), /* pcrel_offset */
980 HOWTO2 (R_NDS32_GOT_LO12, /* type */
982 2, /* size (0 = byte, 1 = short, 2 = long) */
984 FALSE, /* pc_relative */
986 complain_overflow_dont,/* complain_on_overflow */
987 bfd_elf_generic_reloc, /* special_function */
988 "R_NDS32_GOT_LO12", /* name */
989 FALSE, /* partial_inplace */
990 0x00000fff, /* src_mask */
991 0x00000fff, /* dst_mask */
992 FALSE), /* pcrel_offset */
994 /* An PC Relative relocation used when setting PIC offset table register.
995 Like R_NDS32_HI20, but referring to the GOT table entry for
997 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1001 FALSE, /* pc_relative */
1003 complain_overflow_dont,/* complain_on_overflow */
1004 bfd_elf_generic_reloc, /* special_function */
1005 "R_NDS32_GOTPC_HI20", /* name */
1006 FALSE, /* partial_inplace */
1007 0x000fffff, /* src_mask */
1008 0x000fffff, /* dst_mask */
1009 TRUE), /* pcrel_offset */
1010 HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1014 FALSE, /* pc_relative */
1016 complain_overflow_dont,/* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_NDS32_GOTPC_LO12", /* name */
1019 FALSE, /* partial_inplace */
1020 0x00000fff, /* src_mask */
1021 0x00000fff, /* dst_mask */
1022 TRUE), /* pcrel_offset */
1024 HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
1025 12, /* rightshift */
1026 2, /* size (0 = byte, 1 = short, 2 = long) */
1028 FALSE, /* pc_relative */
1030 complain_overflow_dont,/* complain_on_overflow */
1031 bfd_elf_generic_reloc, /* special_function */
1032 "R_NDS32_GOTOFF_HI20", /* name */
1033 FALSE, /* partial_inplace */
1034 0x000fffff, /* src_mask */
1035 0x000fffff, /* dst_mask */
1036 FALSE), /* pcrel_offset */
1037 HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
1039 2, /* size (0 = byte, 1 = short, 2 = long) */
1041 FALSE, /* pc_relative */
1043 complain_overflow_dont,/* complain_on_overflow */
1044 bfd_elf_generic_reloc, /* special_function */
1045 "R_NDS32_GOTOFF_LO12", /* name */
1046 FALSE, /* partial_inplace */
1047 0x00000fff, /* src_mask */
1048 0x00000fff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
1051 /* Alignment hint for relaxable instruction. This is used with
1052 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1053 in order to make next label aligned on word boundary. */
1054 HOWTO2 (R_NDS32_INSN16, /* type */
1056 2, /* size (0 = byte, 1 = short, 2 = long) */
1058 FALSE, /* pc_relative */
1060 complain_overflow_dont,/* complain_on_overflow */
1061 nds32_elf_ignore_reloc,/* special_function */
1062 "R_NDS32_INSN16", /* name */
1063 FALSE, /* partial_inplace */
1064 0x00000fff, /* src_mask */
1065 0x00000fff, /* dst_mask */
1066 FALSE), /* pcrel_offset */
1068 /* Alignment hint for label. */
1069 HOWTO2 (R_NDS32_LABEL, /* type */
1071 2, /* size (0 = byte, 1 = short, 2 = long) */
1073 FALSE, /* pc_relative */
1075 complain_overflow_dont,/* complain_on_overflow */
1076 nds32_elf_ignore_reloc,/* special_function */
1077 "R_NDS32_LABEL", /* name */
1078 FALSE, /* partial_inplace */
1079 0xffffffff, /* src_mask */
1080 0xffffffff, /* dst_mask */
1081 FALSE), /* pcrel_offset */
1083 /* Relax hint for unconditional call sequence */
1084 HOWTO2 (R_NDS32_LONGCALL1, /* type */
1086 2, /* size (0 = byte, 1 = short, 2 = long) */
1088 FALSE, /* pc_relative */
1090 complain_overflow_dont,/* complain_on_overflow */
1091 nds32_elf_ignore_reloc,/* special_function */
1092 "R_NDS32_LONGCALL1", /* name */
1093 FALSE, /* partial_inplace */
1094 0xffffffff, /* src_mask */
1095 0xffffffff, /* dst_mask */
1096 FALSE), /* pcrel_offset */
1098 /* Relax hint for conditional call sequence. */
1099 HOWTO2 (R_NDS32_LONGCALL2, /* type */
1101 2, /* size (0 = byte, 1 = short, 2 = long) */
1103 FALSE, /* pc_relative */
1105 complain_overflow_dont,/* complain_on_overflow */
1106 nds32_elf_ignore_reloc,/* special_function */
1107 "R_NDS32_LONGCALL2", /* name */
1108 FALSE, /* partial_inplace */
1109 0xffffffff, /* src_mask */
1110 0xffffffff, /* dst_mask */
1111 FALSE), /* pcrel_offset */
1113 /* Relax hint for conditional call sequence. */
1114 HOWTO2 (R_NDS32_LONGCALL3, /* type */
1116 2, /* size (0 = byte, 1 = short, 2 = long) */
1118 FALSE, /* pc_relative */
1120 complain_overflow_dont,/* complain_on_overflow */
1121 nds32_elf_ignore_reloc,/* special_function */
1122 "R_NDS32_LONGCALL3", /* name */
1123 FALSE, /* partial_inplace */
1124 0xffffffff, /* src_mask */
1125 0xffffffff, /* dst_mask */
1126 FALSE), /* pcrel_offset */
1128 /* Relax hint for unconditional branch sequence. */
1129 HOWTO2 (R_NDS32_LONGJUMP1, /* type */
1131 2, /* size (0 = byte, 1 = short, 2 = long) */
1133 FALSE, /* pc_relative */
1135 complain_overflow_dont,/* complain_on_overflow */
1136 nds32_elf_ignore_reloc,/* special_function */
1137 "R_NDS32_LONGJUMP1", /* name */
1138 FALSE, /* partial_inplace */
1139 0xffffffff, /* src_mask */
1140 0xffffffff, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1143 /* Relax hint for conditional branch sequence. */
1144 HOWTO2 (R_NDS32_LONGJUMP2, /* type */
1146 2, /* size (0 = byte, 1 = short, 2 = long) */
1148 FALSE, /* pc_relative */
1150 complain_overflow_dont,/* complain_on_overflow */
1151 nds32_elf_ignore_reloc,/* special_function */
1152 "R_NDS32_LONGJUMP2", /* name */
1153 FALSE, /* partial_inplace */
1154 0xffffffff, /* src_mask */
1155 0xffffffff, /* dst_mask */
1156 FALSE), /* pcrel_offset */
1158 /* Relax hint for conditional branch sequence. */
1159 HOWTO2 (R_NDS32_LONGJUMP3, /* type */
1161 2, /* size (0 = byte, 1 = short, 2 = long) */
1163 FALSE, /* pc_relative */
1165 complain_overflow_dont,/* complain_on_overflow */
1166 nds32_elf_ignore_reloc,/* special_function */
1167 "R_NDS32_LONGJUMP3", /* name */
1168 FALSE, /* partial_inplace */
1169 0xffffffff, /* src_mask */
1170 0xffffffff, /* dst_mask */
1171 FALSE), /* pcrel_offset */
1173 /* Relax hint for load/store sequence. */
1174 HOWTO2 (R_NDS32_LOADSTORE, /* type */
1176 2, /* size (0 = byte, 1 = short, 2 = long) */
1178 FALSE, /* pc_relative */
1180 complain_overflow_dont,/* complain_on_overflow */
1181 nds32_elf_ignore_reloc,/* special_function */
1182 "R_NDS32_LOADSTORE", /* name */
1183 FALSE, /* partial_inplace */
1184 0xffffffff, /* src_mask */
1185 0xffffffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1188 /* Relax hint for load/store sequence. */
1189 HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1193 FALSE, /* pc_relative */
1195 complain_overflow_dont,/* complain_on_overflow */
1196 nds32_elf_ignore_reloc,/* special_function */
1197 "R_NDS32_9_FIXED_RELA",/* name */
1198 FALSE, /* partial_inplace */
1199 0x000000ff, /* src_mask */
1200 0x000000ff, /* dst_mask */
1201 FALSE), /* pcrel_offset */
1203 /* Relax hint for load/store sequence. */
1204 HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
1206 2, /* size (0 = byte, 1 = short, 2 = long) */
1208 FALSE, /* pc_relative */
1210 complain_overflow_dont,/* complain_on_overflow */
1211 nds32_elf_ignore_reloc,/* special_function */
1212 "R_NDS32_15_FIXED_RELA",/* name */
1213 FALSE, /* partial_inplace */
1214 0x00003fff, /* src_mask */
1215 0x00003fff, /* dst_mask */
1216 FALSE), /* pcrel_offset */
1218 /* Relax hint for load/store sequence. */
1219 HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1223 FALSE, /* pc_relative */
1225 complain_overflow_dont,/* complain_on_overflow */
1226 nds32_elf_ignore_reloc,/* special_function */
1227 "R_NDS32_17_FIXED_RELA",/* name */
1228 FALSE, /* partial_inplace */
1229 0x0000ffff, /* src_mask */
1230 0x0000ffff, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1233 /* Relax hint for load/store sequence. */
1234 HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1238 FALSE, /* pc_relative */
1240 complain_overflow_dont,/* complain_on_overflow */
1241 nds32_elf_ignore_reloc,/* special_function */
1242 "R_NDS32_25_FIXED_RELA",/* name */
1243 FALSE, /* partial_inplace */
1244 0x00ffffff, /* src_mask */
1245 0x00ffffff, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1248 /* High 20 bits of PLT symbol offset relative to PC. */
1249 HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
1250 12, /* rightshift */
1251 2, /* size (0 = byte, 1 = short, 2 = long) */
1253 FALSE, /* pc_relative */
1255 complain_overflow_dont,/* complain_on_overflow */
1256 bfd_elf_generic_reloc, /* special_function */
1257 "R_NDS32_PLTREL_HI20", /* name */
1258 FALSE, /* partial_inplace */
1259 0x000fffff, /* src_mask */
1260 0x000fffff, /* dst_mask */
1261 FALSE), /* pcrel_offset */
1263 /* Low 12 bits of PLT symbol offset relative to PC. */
1264 HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1268 FALSE, /* pc_relative */
1270 complain_overflow_dont,/* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 "R_NDS32_PLTREL_LO12", /* name */
1273 FALSE, /* partial_inplace */
1274 0x00000fff, /* src_mask */
1275 0x00000fff, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1278 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1279 HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
1280 12, /* rightshift */
1281 2, /* size (0 = byte, 1 = short, 2 = long) */
1283 FALSE, /* pc_relative */
1285 complain_overflow_dont,/* complain_on_overflow */
1286 bfd_elf_generic_reloc, /* special_function */
1287 "R_NDS32_PLT_GOTREL_HI20",/* name */
1288 FALSE, /* partial_inplace */
1289 0x000fffff, /* src_mask */
1290 0x000fffff, /* dst_mask */
1291 FALSE), /* pcrel_offset */
1293 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1294 HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
1296 2, /* size (0 = byte, 1 = short, 2 = long) */
1298 FALSE, /* pc_relative */
1300 complain_overflow_dont,/* complain_on_overflow */
1301 bfd_elf_generic_reloc, /* special_function */
1302 "R_NDS32_PLT_GOTREL_LO12",/* name */
1303 FALSE, /* partial_inplace */
1304 0x00000fff, /* src_mask */
1305 0x00000fff, /* dst_mask */
1306 FALSE), /* pcrel_offset */
1308 /* Small data area 12 bits offset. */
1309 HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1313 FALSE, /* pc_relative */
1315 complain_overflow_signed,/* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_NDS32_SDA12S2_DP_RELA",/* name */
1318 FALSE, /* partial_inplace */
1319 0x00000fff, /* src_mask */
1320 0x00000fff, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1323 /* Small data area 12 bits offset. */
1324 HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1328 FALSE, /* pc_relative */
1330 complain_overflow_signed,/* complain_on_overflow */
1331 bfd_elf_generic_reloc, /* special_function */
1332 "R_NDS32_SDA12S2_SP_RELA",/* name */
1333 FALSE, /* partial_inplace */
1334 0x00000fff, /* src_mask */
1335 0x00000fff, /* dst_mask */
1336 FALSE), /* pcrel_offset */
1337 /* Lower 12 bits of address. */
1339 HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1343 FALSE, /* pc_relative */
1345 complain_overflow_dont,/* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
1347 "R_NDS32_LO12S2_DP_RELA",/* name */
1348 FALSE, /* partial_inplace */
1349 0x000003ff, /* src_mask */
1350 0x000003ff, /* dst_mask */
1351 FALSE), /* pcrel_offset */
1353 /* Lower 12 bits of address. */
1354 HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
1356 2, /* size (0 = byte, 1 = short, 2 = long) */
1358 FALSE, /* pc_relative */
1360 complain_overflow_dont,/* complain_on_overflow */
1361 bfd_elf_generic_reloc, /* special_function */
1362 "R_NDS32_LO12S2_SP_RELA",/* name */
1363 FALSE, /* partial_inplace */
1364 0x000003ff, /* src_mask */
1365 0x000003ff, /* dst_mask */
1366 FALSE), /* pcrel_offset */
1367 /* Lower 12 bits of address. Special identity for or case. */
1368 HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
1370 2, /* size (0 = byte, 1 = short, 2 = long) */
1372 FALSE, /* pc_relative */
1374 complain_overflow_dont,/* complain_on_overflow */
1375 bfd_elf_generic_reloc, /* special_function */
1376 "R_NDS32_LO12S0_ORI_RELA",/* name */
1377 FALSE, /* partial_inplace */
1378 0x00000fff, /* src_mask */
1379 0x00000fff, /* dst_mask */
1380 FALSE), /* pcrel_offset */
1381 /* Small data area 19 bits offset. */
1382 HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
1384 2, /* size (0 = byte, 1 = short, 2 = long) */
1386 FALSE, /* pc_relative */
1388 complain_overflow_signed,/* complain_on_overflow */
1389 bfd_elf_generic_reloc, /* special_function */
1390 "R_NDS32_SDA16S3_RELA",/* name */
1391 FALSE, /* partial_inplace */
1392 0x0000ffff, /* src_mask */
1393 0x0000ffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1396 /* Small data area 15 bits offset. */
1397 HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
1399 2, /* size (0 = byte, 1 = short, 2 = long) */
1401 FALSE, /* pc_relative */
1403 complain_overflow_signed,/* complain_on_overflow */
1404 bfd_elf_generic_reloc, /* special_function */
1405 "R_NDS32_SDA17S2_RELA",/* name */
1406 FALSE, /* partial_inplace */
1407 0x0001ffff, /* src_mask */
1408 0x0001ffff, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1411 HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
1413 2, /* size (0 = byte, 1 = short, 2 = long) */
1415 FALSE, /* pc_relative */
1417 complain_overflow_signed,/* complain_on_overflow */
1418 bfd_elf_generic_reloc, /* special_function */
1419 "R_NDS32_SDA18S1_RELA",/* name */
1420 FALSE, /* partial_inplace */
1421 0x0003ffff, /* src_mask */
1422 0x0003ffff, /* dst_mask */
1423 FALSE), /* pcrel_offset */
1425 HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
1427 2, /* size (0 = byte, 1 = short, 2 = long) */
1429 FALSE, /* pc_relative */
1431 complain_overflow_signed,/* complain_on_overflow */
1432 bfd_elf_generic_reloc, /* special_function */
1433 "R_NDS32_SDA19S0_RELA",/* name */
1434 FALSE, /* partial_inplace */
1435 0x0007ffff, /* src_mask */
1436 0x0007ffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438 HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
1440 0, /* size (0 = byte, 1 = short, 2 = long) */
1442 FALSE, /* pc_relative */
1444 complain_overflow_dont,/* complain_on_overflow */
1445 nds32_elf_ignore_reloc,/* special_function */
1446 "R_NDS32_DWARF2_OP1_RELA",/* name */
1447 FALSE, /* partial_inplace */
1448 0xff, /* src_mask */
1449 0xff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451 HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
1453 1, /* size (0 = byte, 1 = short, 2 = long) */
1455 FALSE, /* pc_relative */
1457 complain_overflow_dont,/* complain_on_overflow */
1458 nds32_elf_ignore_reloc,/* special_function */
1459 "R_NDS32_DWARF2_OP2_RELA",/* name */
1460 FALSE, /* partial_inplace */
1461 0xffff, /* src_mask */
1462 0xffff, /* dst_mask */
1463 FALSE), /* pcrel_offset */
1464 HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
1466 2, /* size (0 = byte, 1 = short, 2 = long) */
1468 FALSE, /* pc_relative */
1470 complain_overflow_dont,/* complain_on_overflow */
1471 nds32_elf_ignore_reloc,/* special_function */
1472 "R_NDS32_DWARF2_LEB_RELA",/* name */
1473 FALSE, /* partial_inplace */
1474 0xffffffff, /* src_mask */
1475 0xffffffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477 HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
1479 1, /* size (0 = byte, 1 = short, 2 = long) */
1481 FALSE, /* pc_relative */
1483 complain_overflow_dont,/* complain_on_overflow */
1484 nds32_elf_ignore_reloc,/* special_function */
1485 "R_NDS32_UPDATE_TA_RELA",/* name */
1486 FALSE, /* partial_inplace */
1487 0xffff, /* src_mask */
1488 0xffff, /* dst_mask */
1489 FALSE), /* pcrel_offset */
1490 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1491 entry for the symbol. */
1492 HOWTO2 (R_NDS32_9_PLTREL, /* type */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1496 TRUE, /* pc_relative */
1498 complain_overflow_signed,/* complain_on_overflow */
1499 bfd_elf_generic_reloc, /* special_function */
1500 "R_NDS32_9_PLTREL", /* name */
1501 FALSE, /* partial_inplace */
1502 0xff, /* src_mask */
1503 0xff, /* dst_mask */
1504 TRUE), /* pcrel_offset */
1505 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1506 HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
1508 2, /* size (0 = byte, 1 = short, 2 = long) */
1510 FALSE, /* pc_relative */
1512 complain_overflow_dont,/* complain_on_overflow */
1513 bfd_elf_generic_reloc, /* special_function */
1514 "R_NDS32_PLT_GOTREL_LO20",/* name */
1515 FALSE, /* partial_inplace */
1516 0x000fffff, /* src_mask */
1517 0x000fffff, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1520 HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
1522 2, /* size (0 = byte, 1 = short, 2 = long) */
1524 FALSE, /* pc_relative */
1526 complain_overflow_dont,/* complain_on_overflow */
1527 bfd_elf_generic_reloc, /* special_function */
1528 "R_NDS32_PLT_GOTREL_LO15",/* name */
1529 FALSE, /* partial_inplace */
1530 0x00007fff, /* src_mask */
1531 0x00007fff, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1533 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1534 HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
1536 2, /* size (0 = byte, 1 = short, 2 = long) */
1538 FALSE, /* pc_relative */
1540 complain_overflow_dont,/* complain_on_overflow */
1541 bfd_elf_generic_reloc, /* special_function */
1542 "R_NDS32_PLT_GOTREL_LO19",/* name */
1543 FALSE, /* partial_inplace */
1544 0x0007ffff, /* src_mask */
1545 0x0007ffff, /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547 HOWTO2 (R_NDS32_GOT_LO15, /* type */
1549 2, /* size (0 = byte, 1 = short, 2 = long) */
1551 FALSE, /* pc_relative */
1553 complain_overflow_dont,/* complain_on_overflow */
1554 bfd_elf_generic_reloc, /* special_function */
1555 "R_NDS32_GOT_LO15", /* name */
1556 FALSE, /* partial_inplace */
1557 0x00007fff, /* src_mask */
1558 0x00007fff, /* dst_mask */
1559 FALSE), /* pcrel_offset */
1560 HOWTO2 (R_NDS32_GOT_LO19, /* type */
1562 2, /* size (0 = byte, 1 = short, 2 = long) */
1564 FALSE, /* pc_relative */
1566 complain_overflow_dont,/* complain_on_overflow */
1567 bfd_elf_generic_reloc, /* special_function */
1568 "R_NDS32_GOT_LO19", /* name */
1569 FALSE, /* partial_inplace */
1570 0x0007ffff, /* src_mask */
1571 0x0007ffff, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1573 HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1577 FALSE, /* pc_relative */
1579 complain_overflow_dont,/* complain_on_overflow */
1580 bfd_elf_generic_reloc, /* special_function */
1581 "R_NDS32_GOTOFF_LO15", /* name */
1582 FALSE, /* partial_inplace */
1583 0x00007fff, /* src_mask */
1584 0x00007fff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586 HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
1588 2, /* size (0 = byte, 1 = short, 2 = long) */
1590 FALSE, /* pc_relative */
1592 complain_overflow_dont,/* complain_on_overflow */
1593 bfd_elf_generic_reloc, /* special_function */
1594 "R_NDS32_GOTOFF_LO19", /* name */
1595 FALSE, /* partial_inplace */
1596 0x0007ffff, /* src_mask */
1597 0x0007ffff, /* dst_mask */
1598 FALSE), /* pcrel_offset */
1599 /* GOT 15 bits offset. */
1600 HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
1602 2, /* size (0 = byte, 1 = short, 2 = long) */
1604 FALSE, /* pc_relative */
1606 complain_overflow_signed,/* complain_on_overflow */
1607 bfd_elf_generic_reloc, /* special_function */
1608 "R_NDS32_GOT15S2_RELA",/* name */
1609 FALSE, /* partial_inplace */
1610 0x00007fff, /* src_mask */
1611 0x00007fff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
1613 /* GOT 17 bits offset. */
1614 HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
1616 2, /* size (0 = byte, 1 = short, 2 = long) */
1618 FALSE, /* pc_relative */
1620 complain_overflow_signed,/* complain_on_overflow */
1621 bfd_elf_generic_reloc, /* special_function */
1622 "R_NDS32_GOT17S2_RELA",/* name */
1623 FALSE, /* partial_inplace */
1624 0x0001ffff, /* src_mask */
1625 0x0001ffff, /* dst_mask */
1626 FALSE), /* pcrel_offset */
1627 /* A 5 bit address. */
1628 HOWTO2 (R_NDS32_5_RELA, /* type */
1630 1, /* size (0 = byte, 1 = short, 2 = long) */
1632 FALSE, /* pc_relative */
1634 complain_overflow_signed,/* complain_on_overflow */
1635 bfd_elf_generic_reloc, /* special_function */
1636 "R_NDS32_5_RELA", /* name */
1637 FALSE, /* partial_inplace */
1638 0x1f, /* src_mask */
1639 0x1f, /* dst_mask */
1640 FALSE), /* pcrel_offset */
1641 HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1645 TRUE, /* pc_relative */
1647 complain_overflow_unsigned,/* complain_on_overflow */
1648 bfd_elf_generic_reloc, /* special_function */
1649 "R_NDS32_10_UPCREL_RELA",/* name */
1650 FALSE, /* partial_inplace */
1651 0x1ff, /* src_mask */
1652 0x1ff, /* dst_mask */
1653 TRUE), /* pcrel_offset */
1654 HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1658 FALSE, /* pc_relative */
1660 complain_overflow_unsigned,/* complain_on_overflow */
1661 bfd_elf_generic_reloc, /* special_function */
1662 "R_NDS32_SDA_FP7U2_RELA",/* name */
1663 FALSE, /* partial_inplace */
1664 0x0000007f, /* src_mask */
1665 0x0000007f, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667 HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
1669 2, /* size (0 = byte, 1 = short, 2 = long) */
1671 TRUE, /* pc_relative */
1673 complain_overflow_signed,/* complain_on_overflow */
1674 bfd_elf_generic_reloc, /* special_function */
1675 "R_NDS32_WORD_9_PCREL_RELA",/* name */
1676 FALSE, /* partial_inplace */
1677 0xff, /* src_mask */
1678 0xff, /* dst_mask */
1679 TRUE), /* pcrel_offset */
1680 HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
1682 2, /* size (0 = byte, 1 = short, 2 = long) */
1684 FALSE, /* pc_relative */
1686 complain_overflow_dont,/* complain_on_overflow */
1687 bfd_elf_generic_reloc, /* special_function */
1688 "R_NDS32_25_ABS_RELA", /* name */
1689 FALSE, /* partial_inplace */
1690 0xffffff, /* src_mask */
1691 0xffffff, /* dst_mask */
1692 FALSE), /* pcrel_offset */
1694 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1695 HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
1697 2, /* size (0 = byte, 1 = short, 2 = long) */
1699 TRUE, /* pc_relative */
1701 complain_overflow_signed,/* complain_on_overflow */
1702 bfd_elf_generic_reloc, /* special_function */
1703 "R_NDS32_17IFC_PCREL_RELA",/* name */
1704 FALSE, /* partial_inplace */
1705 0xffff, /* src_mask */
1706 0xffff, /* dst_mask */
1707 TRUE), /* pcrel_offset */
1709 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1710 HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1714 TRUE, /* pc_relative */
1716 complain_overflow_unsigned,/* complain_on_overflow */
1717 bfd_elf_generic_reloc, /* special_function */
1718 "R_NDS32_10IFCU_PCREL_RELA",/* name */
1719 FALSE, /* partial_inplace */
1720 0x1ff, /* src_mask */
1721 0x1ff, /* dst_mask */
1722 TRUE), /* pcrel_offset */
1724 /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
1725 HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
1726 12, /* rightshift */
1727 2, /* size (0 = byte, 1 = short, 2 = long) */
1729 FALSE, /* pc_relative */
1731 complain_overflow_dont,/* complain_on_overflow */
1732 bfd_elf_generic_reloc, /* special_function */
1733 "R_NDS32_TLS_LE_HI20", /* name */
1734 FALSE, /* partial_inplace */
1735 0x000fffff, /* src_mask */
1736 0x000fffff, /* dst_mask */
1737 FALSE), /* pcrel_offset */
1739 HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
1741 2, /* size (0 = byte, 1 = short, 2 = long) */
1743 FALSE, /* pc_relative */
1745 complain_overflow_dont,/* complain_on_overflow */
1746 bfd_elf_generic_reloc, /* special_function */
1747 "R_NDS32_TLS_LE_LO12", /* name */
1748 FALSE, /* partial_inplace */
1749 0x00000fff, /* src_mask */
1750 0x00000fff, /* dst_mask */
1751 FALSE), /* pcrel_offset */
1753 /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
1754 HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
1755 12, /* rightshift */
1756 2, /* size (0 = byte, 1 = short, 2 = long) */
1758 FALSE, /* pc_relative */
1760 complain_overflow_dont,/* complain_on_overflow */
1761 bfd_elf_generic_reloc, /* special_function */
1762 "R_NDS32_TLS_IE_HI20", /* name */
1763 FALSE, /* partial_inplace */
1764 0x000fffff, /* src_mask */
1765 0x000fffff, /* dst_mask */
1766 FALSE), /* pcrel_offset */
1768 HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
1770 2, /* size (0 = byte, 1 = short, 2 = long) */
1772 FALSE, /* pc_relative */
1774 complain_overflow_dont,/* complain_on_overflow */
1775 bfd_elf_generic_reloc, /* special_function */
1776 "R_NDS32_TLS_IE_LO12S2",/* name */
1777 FALSE, /* partial_inplace */
1778 0x000003ff, /* src_mask */
1779 0x000003ff, /* dst_mask */
1780 FALSE), /* pcrel_offset */
1782 /* TLS LE TP offset relocation */
1783 HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
1785 2, /* size (0 = byte, 1 = short, 2 = long) */
1787 FALSE, /* pc_relative */
1789 complain_overflow_bitfield,/* complain_on_overflow */
1790 bfd_elf_generic_reloc, /* special_function */
1791 "R_NDS32_TLS_TPOFF", /* name */
1792 FALSE, /* partial_inplace */
1793 0xffffffff, /* src_mask */
1794 0xffffffff, /* dst_mask */
1795 FALSE), /* pcrel_offset */
1797 /* A 20 bit address. */
1798 HOWTO2 (R_NDS32_TLS_LE_20, /* type */
1800 2, /* size (0 = byte, 1 = short, 2 = long) */
1802 FALSE, /* pc_relative */
1804 complain_overflow_signed,/* complain_on_overflow */
1805 bfd_elf_generic_reloc, /* special_function */
1806 "R_NDS32_TLS_LE_20", /* name */
1807 FALSE, /* partial_inplace */
1808 0xfffff, /* src_mask */
1809 0xfffff, /* dst_mask */
1810 FALSE), /* pcrel_offset */
1812 HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
1814 2, /* size (0 = byte, 1 = short, 2 = long) */
1816 FALSE, /* pc_relative */
1818 complain_overflow_signed,/* complain_on_overflow */
1819 bfd_elf_generic_reloc, /* special_function */
1820 "R_NDS32_TLS_LE_15S0", /* name */
1821 FALSE, /* partial_inplace */
1822 0x7fff, /* src_mask */
1823 0x7fff, /* dst_mask */
1824 FALSE), /* pcrel_offset */
1825 HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
1827 2, /* size (0 = byte, 1 = short, 2 = long) */
1829 FALSE, /* pc_relative */
1831 complain_overflow_signed,/* complain_on_overflow */
1832 bfd_elf_generic_reloc, /* special_function */
1833 "R_NDS32_TLS_LE_15S1", /* name */
1834 FALSE, /* partial_inplace */
1835 0x7fff, /* src_mask */
1836 0x7fff, /* dst_mask */
1837 FALSE), /* pcrel_offset */
1838 HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
1840 2, /* size (0 = byte, 1 = short, 2 = long) */
1842 FALSE, /* pc_relative */
1844 complain_overflow_signed,/* complain_on_overflow */
1845 bfd_elf_generic_reloc, /* special_function */
1846 "R_NDS32_TLS_LE_15S2", /* name */
1847 FALSE, /* partial_inplace */
1848 0x7fff, /* src_mask */
1849 0x7fff, /* dst_mask */
1850 FALSE), /* pcrel_offset */
1852 /* Relax hint for unconditional call sequence */
1853 HOWTO2 (R_NDS32_LONGCALL4, /* type */
1855 2, /* size (0 = byte, 1 = short, 2 = long) */
1857 FALSE, /* pc_relative */
1859 complain_overflow_dont,/* complain_on_overflow */
1860 nds32_elf_ignore_reloc,/* special_function */
1861 "R_NDS32_LONGCALL4", /* name */
1862 FALSE, /* partial_inplace */
1863 0xffffffff, /* src_mask */
1864 0xffffffff, /* dst_mask */
1865 FALSE), /* pcrel_offset */
1867 /* Relax hint for conditional call sequence. */
1868 HOWTO2 (R_NDS32_LONGCALL5, /* type */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1872 FALSE, /* pc_relative */
1874 complain_overflow_dont,/* complain_on_overflow */
1875 nds32_elf_ignore_reloc,/* special_function */
1876 "R_NDS32_LONGCALL5", /* name */
1877 FALSE, /* partial_inplace */
1878 0xffffffff, /* src_mask */
1879 0xffffffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1882 /* Relax hint for conditional call sequence. */
1883 HOWTO2 (R_NDS32_LONGCALL6, /* type */
1885 2, /* size (0 = byte, 1 = short, 2 = long) */
1887 FALSE, /* pc_relative */
1889 complain_overflow_dont,/* complain_on_overflow */
1890 nds32_elf_ignore_reloc,/* special_function */
1891 "R_NDS32_LONGCALL6", /* name */
1892 FALSE, /* partial_inplace */
1893 0xffffffff, /* src_mask */
1894 0xffffffff, /* dst_mask */
1895 FALSE), /* pcrel_offset */
1897 /* Relax hint for unconditional branch sequence. */
1898 HOWTO2 (R_NDS32_LONGJUMP4, /* 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_LONGJUMP4", /* name */
1907 FALSE, /* partial_inplace */
1908 0xffffffff, /* src_mask */
1909 0xffffffff, /* dst_mask */
1910 FALSE), /* pcrel_offset */
1912 /* Relax hint for conditional branch sequence. */
1913 HOWTO2 (R_NDS32_LONGJUMP5, /* 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_LONGJUMP5", /* name */
1922 FALSE, /* partial_inplace */
1923 0xffffffff, /* src_mask */
1924 0xffffffff, /* dst_mask */
1925 FALSE), /* pcrel_offset */
1927 /* Relax hint for conditional branch sequence. */
1928 HOWTO2 (R_NDS32_LONGJUMP6, /* 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_LONGJUMP6", /* name */
1937 FALSE, /* partial_inplace */
1938 0xffffffff, /* src_mask */
1939 0xffffffff, /* dst_mask */
1940 FALSE), /* pcrel_offset */
1942 /* Relax hint for conditional branch sequence. */
1943 HOWTO2 (R_NDS32_LONGJUMP7, /* 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_LONGJUMP7", /* name */
1952 FALSE, /* partial_inplace */
1953 0xffffffff, /* src_mask */
1954 0xffffffff, /* dst_mask */
1955 FALSE), /* pcrel_offset */
1957 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1959 2, /* size (0 = byte, 1 = short, 2 = long) */
1961 FALSE, /* pc_relative */
1963 complain_overflow_dont,/* complain_on_overflow */
1964 bfd_elf_generic_reloc, /* special_function */
1965 "R_NDS32_TLS_IE_LO12", /* name */
1966 FALSE, /* partial_inplace */
1967 0x00000fff, /* src_mask */
1968 0x00000fff, /* dst_mask */
1969 FALSE), /* pcrel_offset */
1971 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1972 entry for the symbol. */
1973 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1974 12, /* rightshift */
1975 2, /* size (0 = byte, 1 = short, 2 = long) */
1977 FALSE, /* pc_relative */
1979 complain_overflow_dont,/* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_NDS32_TLS_IEGP_HI20",/* name */
1982 FALSE, /* partial_inplace */
1983 0x000fffff, /* src_mask */
1984 0x000fffff, /* dst_mask */
1985 FALSE), /* pcrel_offset */
1987 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1989 2, /* size (0 = byte, 1 = short, 2 = long) */
1991 FALSE, /* pc_relative */
1993 complain_overflow_dont,/* complain_on_overflow */
1994 bfd_elf_generic_reloc, /* special_function */
1995 "R_NDS32_TLS_IEGP_LO12",/* name */
1996 FALSE, /* partial_inplace */
1997 0x00000fff, /* src_mask */
1998 0x00000fff, /* dst_mask */
1999 FALSE), /* pcrel_offset */
2001 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2003 2, /* size (0 = byte, 1 = short, 2 = long) */
2005 FALSE, /* pc_relative */
2007 complain_overflow_dont,/* complain_on_overflow */
2008 bfd_elf_generic_reloc, /* special_function */
2009 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2010 FALSE, /* partial_inplace */
2011 0x000003ff, /* src_mask */
2012 0x000003ff, /* dst_mask */
2013 FALSE), /* pcrel_offset */
2015 /* TLS description relocation */
2016 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2017 12, /* rightshift */
2018 2, /* size (0 = byte, 1 = short, 2 = long) */
2020 FALSE, /* pc_relative */
2022 complain_overflow_dont,/* complain_on_overflow */
2023 nds32_elf_hi20_reloc, /* special_function */
2024 "R_NDS32_TLS_DESC_HI20",/* name */
2025 FALSE, /* partial_inplace */
2026 0x000fffff, /* src_mask */
2027 0x000fffff, /* dst_mask */
2028 FALSE), /* pcrel_offset */
2030 /* TLS GD/LD description offset high part. */
2031 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2032 12, /* rightshift */
2033 2, /* size (0 = byte, 1 = short, 2 = long) */
2035 FALSE, /* pc_relative */
2037 complain_overflow_dont,/* complain_on_overflow */
2038 nds32_elf_hi20_reloc, /* special_function */
2039 "R_NDS32_TLS_DESC_HI20",/* name */
2040 FALSE, /* partial_inplace */
2041 0x000fffff, /* src_mask */
2042 0x000fffff, /* dst_mask */
2043 FALSE), /* pcrel_offset */
2045 /* TLS GD/LD description offset low part. */
2046 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2048 2, /* size (0 = byte, 1 = short, 2 = long) */
2050 FALSE, /* pc_relative */
2052 complain_overflow_dont,/* complain_on_overflow */
2053 nds32_elf_lo12_reloc, /* special_function */
2054 "R_NDS32_TLS_DESC_LO12",/* name */
2055 FALSE, /* partial_inplace */
2056 0x00000fff, /* src_mask */
2057 0x00000fff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2060 /* TLS GD/LD description offset set (movi). */
2061 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2063 2, /* size (0 = byte, 1 = short, 2 = long) */
2065 FALSE, /* pc_relative */
2067 complain_overflow_signed,/* complain_on_overflow */
2068 bfd_elf_generic_reloc, /* special_function */
2069 "R_NDS32_TLS_DESC_20", /* name */
2070 FALSE, /* partial_inplace */
2071 0x000fffff, /* src_mask */
2072 0x000fffff, /* dst_mask */
2073 FALSE), /* pcrel_offset */
2075 /* TLS GD/LD description offset set (lwi.gp). */
2076 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2078 2, /* size (0 = byte, 1 = short, 2 = long) */
2080 FALSE, /* pc_relative */
2082 complain_overflow_signed,/* complain_on_overflow */
2083 bfd_elf_generic_reloc, /* special_function */
2084 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2085 FALSE, /* partial_inplace */
2086 0x0001ffff, /* src_mask */
2087 0x0001ffff, /* dst_mask */
2088 FALSE), /* pcrel_offset */
2091 /* Relocations used for relaxation. */
2092 #define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2093 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2095 static reloc_howto_type nds32_elf_relax_howto_table[] = {
2096 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2098 2, /* size (0 = byte, 1 = short, 2 = long) */
2100 FALSE, /* pc_relative */
2102 complain_overflow_dont,/* complain_on_overflow */
2103 nds32_elf_ignore_reloc,/* special_function */
2104 "R_NDS32_RELAX_ENTRY", /* name */
2105 FALSE, /* partial_inplace */
2106 0xffffffff, /* src_mask */
2107 0xffffffff, /* dst_mask */
2108 FALSE), /* pcrel_offset */
2109 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2113 FALSE, /* pc_relative */
2115 complain_overflow_dont,/* complain_on_overflow */
2116 nds32_elf_ignore_reloc,/* special_function */
2117 "R_NDS32_GOT_SUFF", /* name */
2118 FALSE, /* partial_inplace */
2119 0xffffffff, /* src_mask */
2120 0xffffffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2124 2, /* size (0 = byte, 1 = short, 2 = long) */
2126 FALSE, /* pc_relative */
2128 complain_overflow_bitfield,/* complain_on_overflow */
2129 nds32_elf_ignore_reloc,/* special_function */
2130 "R_NDS32_GOTOFF_SUFF", /* name */
2131 FALSE, /* partial_inplace */
2132 0xffffffff, /* src_mask */
2133 0xffffffff, /* dst_mask */
2134 FALSE), /* pcrel_offset */
2135 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2137 2, /* size (0 = byte, 1 = short, 2 = long) */
2139 FALSE, /* pc_relative */
2141 complain_overflow_dont,/* complain_on_overflow */
2142 nds32_elf_ignore_reloc,/* special_function */
2143 "R_NDS32_PLT_GOT_SUFF",/* name */
2144 FALSE, /* partial_inplace */
2145 0xffffffff, /* src_mask */
2146 0xffffffff, /* dst_mask */
2147 FALSE), /* pcrel_offset */
2148 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2150 2, /* size (0 = byte, 1 = short, 2 = long) */
2152 FALSE, /* pc_relative */
2154 complain_overflow_dont,/* complain_on_overflow */
2155 nds32_elf_ignore_reloc,/* special_function */
2156 "R_NDS32_MULCALL_SUFF",/* name */
2157 FALSE, /* partial_inplace */
2158 0xffffffff, /* src_mask */
2159 0xffffffff, /* dst_mask */
2160 FALSE), /* pcrel_offset */
2161 HOWTO3 (R_NDS32_PTR, /* type */
2163 2, /* size (0 = byte, 1 = short, 2 = long) */
2165 FALSE, /* pc_relative */
2167 complain_overflow_dont,/* complain_on_overflow */
2168 nds32_elf_ignore_reloc,/* special_function */
2169 "R_NDS32_PTR", /* name */
2170 FALSE, /* partial_inplace */
2171 0xffffffff, /* src_mask */
2172 0xffffffff, /* dst_mask */
2173 FALSE), /* pcrel_offset */
2174 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2176 2, /* size (0 = byte, 1 = short, 2 = long) */
2178 FALSE, /* pc_relative */
2180 complain_overflow_dont,/* complain_on_overflow */
2181 nds32_elf_ignore_reloc,/* special_function */
2182 "R_NDS32_PTR_COUNT", /* name */
2183 FALSE, /* partial_inplace */
2184 0xffffffff, /* src_mask */
2185 0xffffffff, /* dst_mask */
2186 FALSE), /* pcrel_offset */
2187 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2189 2, /* size (0 = byte, 1 = short, 2 = long) */
2191 FALSE, /* pc_relative */
2193 complain_overflow_dont,/* complain_on_overflow */
2194 nds32_elf_ignore_reloc,/* special_function */
2195 "R_NDS32_PTR_RESOLVED",/* name */
2196 FALSE, /* partial_inplace */
2197 0xffffffff, /* src_mask */
2198 0xffffffff, /* dst_mask */
2199 FALSE), /* pcrel_offset */
2200 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2202 2, /* size (0 = byte, 1 = short, 2 = long) */
2204 FALSE, /* pc_relative */
2206 complain_overflow_dont,/* complain_on_overflow */
2207 nds32_elf_ignore_reloc,/* special_function */
2208 "R_NDS32_PLTBLOCK", /* name */
2209 FALSE, /* partial_inplace */
2210 0xffffffff, /* src_mask */
2211 0xffffffff, /* dst_mask */
2212 FALSE), /* pcrel_offset */
2213 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2215 2, /* size (0 = byte, 1 = short, 2 = long) */
2217 FALSE, /* pc_relative */
2219 complain_overflow_dont,/* complain_on_overflow */
2220 nds32_elf_ignore_reloc,/* special_function */
2221 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2222 FALSE, /* partial_inplace */
2223 0xffffffff, /* src_mask */
2224 0xffffffff, /* dst_mask */
2225 FALSE), /* pcrel_offset */
2226 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2228 2, /* size (0 = byte, 1 = short, 2 = long) */
2230 FALSE, /* pc_relative */
2232 complain_overflow_dont,/* complain_on_overflow */
2233 nds32_elf_ignore_reloc,/* special_function */
2234 "R_NDS32_RELAX_REGION_END",/* name */
2235 FALSE, /* partial_inplace */
2236 0xffffffff, /* src_mask */
2237 0xffffffff, /* dst_mask */
2238 FALSE), /* pcrel_offset */
2239 HOWTO3 (R_NDS32_MINUEND, /* type */
2241 2, /* size (0 = byte, 1 = short, 2 = long) */
2243 FALSE, /* pc_relative */
2245 complain_overflow_dont,/* complain_on_overflow */
2246 nds32_elf_ignore_reloc,/* special_function */
2247 "R_NDS32_MINUEND", /* name */
2248 FALSE, /* partial_inplace */
2249 0xffffffff, /* src_mask */
2250 0xffffffff, /* dst_mask */
2251 FALSE), /* pcrel_offset */
2252 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2254 2, /* size (0 = byte, 1 = short, 2 = long) */
2256 FALSE, /* pc_relative */
2258 complain_overflow_dont,/* complain_on_overflow */
2259 nds32_elf_ignore_reloc,/* special_function */
2260 "R_NDS32_SUBTRAHEND", /* name */
2261 FALSE, /* partial_inplace */
2262 0xffffffff, /* src_mask */
2263 0xffffffff, /* dst_mask */
2264 FALSE), /* pcrel_offset */
2265 HOWTO3 (R_NDS32_DIFF8, /* type */
2267 0, /* size (0 = byte, 1 = short, 2 = long) */
2269 FALSE, /* pc_relative */
2271 complain_overflow_dont,/* complain_on_overflow */
2272 nds32_elf_ignore_reloc,/* special_function */
2273 "R_NDS32_DIFF8", /* name */
2274 FALSE, /* partial_inplace */
2275 0x000000ff, /* src_mask */
2276 0x000000ff, /* dst_mask */
2277 FALSE), /* pcrel_offset */
2278 HOWTO3 (R_NDS32_DIFF16, /* type */
2280 1, /* size (0 = byte, 1 = short, 2 = long) */
2282 FALSE, /* pc_relative */
2284 complain_overflow_dont,/* complain_on_overflow */
2285 nds32_elf_ignore_reloc,/* special_function */
2286 "R_NDS32_DIFF16", /* name */
2287 FALSE, /* partial_inplace */
2288 0x0000ffff, /* src_mask */
2289 0x0000ffff, /* dst_mask */
2290 FALSE), /* pcrel_offset */
2291 HOWTO3 (R_NDS32_DIFF32, /* type */
2293 2, /* size (0 = byte, 1 = short, 2 = long) */
2295 FALSE, /* pc_relative */
2297 complain_overflow_dont,/* complain_on_overflow */
2298 nds32_elf_ignore_reloc,/* special_function */
2299 "R_NDS32_DIFF32", /* name */
2300 FALSE, /* partial_inplace */
2301 0xffffffff, /* src_mask */
2302 0xffffffff, /* dst_mask */
2303 FALSE), /* pcrel_offset */
2304 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2306 0, /* size (0 = byte, 1 = short, 2 = long) */
2308 FALSE, /* pc_relative */
2310 complain_overflow_dont,/* complain_on_overflow */
2311 nds32_elf_ignore_reloc,/* special_function */
2312 "R_NDS32_DIFF_ULEB128",/* name */
2313 FALSE, /* partial_inplace */
2314 0xffffffff, /* src_mask */
2315 0xffffffff, /* dst_mask */
2316 FALSE), /* pcrel_offset */
2317 HOWTO3 (R_NDS32_DATA, /* type */
2319 2, /* size (0 = byte, 1 = short, 2 = long) */
2321 FALSE, /* pc_relative */
2323 complain_overflow_dont,/* complain_on_overflow */
2324 nds32_elf_ignore_reloc,/* special_function */
2325 "R_NDS32_DATA", /* name */
2326 FALSE, /* partial_inplace */
2327 0xffffffff, /* src_mask */
2328 0xffffffff, /* dst_mask */
2329 FALSE), /* pcrel_offset */
2330 HOWTO3 (R_NDS32_TRAN, /* type */
2332 2, /* size (0 = byte, 1 = short, 2 = long) */
2334 FALSE, /* pc_relative */
2336 complain_overflow_dont,/* complain_on_overflow */
2337 nds32_elf_ignore_reloc,/* special_function */
2338 "R_NDS32_TRAN", /* name */
2339 FALSE, /* partial_inplace */
2340 0xffffffff, /* src_mask */
2341 0xffffffff, /* dst_mask */
2342 FALSE), /* pcrel_offset */
2343 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2345 2, /* size (0 = byte, 1 = short, 2 = long) */
2347 FALSE, /* pc_relative */
2349 complain_overflow_dont,/* complain_on_overflow */
2350 nds32_elf_ignore_reloc,/* special_function */
2351 "R_NDS32_TLS_LE_ADD", /* name */
2352 FALSE, /* partial_inplace */
2353 0xffffffff, /* src_mask */
2354 0xffffffff, /* dst_mask */
2355 FALSE), /* pcrel_offset */
2356 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2358 2, /* size (0 = byte, 1 = short, 2 = long) */
2360 FALSE, /* pc_relative */
2362 complain_overflow_dont,/* complain_on_overflow */
2363 nds32_elf_ignore_reloc,/* special_function */
2364 "R_NDS32_TLS_LE_LS", /* name */
2365 FALSE, /* partial_inplace */
2366 0xffffffff, /* src_mask */
2367 0xffffffff, /* dst_mask */
2368 FALSE), /* pcrel_offset */
2369 HOWTO3 (R_NDS32_EMPTY, /* type */
2371 2, /* size (0 = byte, 1 = short, 2 = long) */
2373 FALSE, /* pc_relative */
2375 complain_overflow_dont,/* complain_on_overflow */
2376 nds32_elf_ignore_reloc,/* special_function */
2377 "R_NDS32_EMPTY", /* name */
2378 FALSE, /* partial_inplace */
2379 0xffffffff, /* src_mask */
2380 0xffffffff, /* dst_mask */
2381 FALSE), /* pcrel_offset */
2382 /* TLS GD/LD description address base addition. */
2383 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2385 2, /* size (0 = byte, 1 = short, 2 = long) */
2387 FALSE, /* pc_relative */
2389 complain_overflow_dont,/* complain_on_overflow */
2390 nds32_elf_ignore_reloc,/* special_function */
2391 "R_NDS32_TLS_DESC_ADD",/* name */
2392 FALSE, /* partial_inplace */
2393 0xffffffff, /* src_mask */
2394 0xffffffff, /* dst_mask */
2395 FALSE), /* pcrel_offset */
2396 /* TLS GD/LD description function load. */
2397 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2399 2, /* size (0 = byte, 1 = short, 2 = long) */
2401 FALSE, /* pc_relative */
2403 complain_overflow_dont,/* complain_on_overflow */
2404 nds32_elf_ignore_reloc,/* special_function */
2405 "R_NDS32_TLS_DESC_FUNC",/* name */
2406 FALSE, /* partial_inplace */
2407 0xffffffff, /* src_mask */
2408 0xffffffff, /* dst_mask */
2409 FALSE), /* pcrel_offset */
2410 /* TLS DESC resolve function call. */
2411 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2413 2, /* size (0 = byte, 1 = short, 2 = long) */
2415 FALSE, /* pc_relative */
2417 complain_overflow_dont,/* complain_on_overflow */
2418 nds32_elf_ignore_reloc,/* special_function */
2419 "R_NDS32_TLS_DESC_CALL",/* name */
2420 FALSE, /* partial_inplace */
2421 0xffffffff, /* src_mask */
2422 0xffffffff, /* dst_mask */
2423 FALSE), /* pcrel_offset */
2424 /* TLS DESC variable access. */
2425 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2427 2, /* size (0 = byte, 1 = short, 2 = long) */
2429 FALSE, /* pc_relative */
2431 complain_overflow_dont,/* complain_on_overflow */
2432 nds32_elf_ignore_reloc,/* special_function */
2433 "R_NDS32_TLS_DESC_MEM",/* name */
2434 FALSE, /* partial_inplace */
2435 0xffffffff, /* src_mask */
2436 0xffffffff, /* dst_mask */
2437 FALSE), /* pcrel_offset */
2438 /* TLS GD/LD description mark (@tlsdec). */
2439 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2441 2, /* size (0 = byte, 1 = short, 2 = long) */
2443 FALSE, /* pc_relative */
2445 complain_overflow_dont,/* complain_on_overflow */
2446 nds32_elf_ignore_reloc,/* special_function */
2447 "R_NDS32_REMOVE", /* name */
2448 FALSE, /* partial_inplace */
2449 0xffffffff, /* src_mask */
2450 0xffffffff, /* dst_mask */
2451 FALSE), /* pcrel_offset */
2452 /* TLS GD/LD description mark (@tlsdec). */
2453 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2455 2, /* size (0 = byte, 1 = short, 2 = long) */
2457 FALSE, /* pc_relative */
2459 complain_overflow_dont,/* complain_on_overflow */
2460 nds32_elf_ignore_reloc,/* special_function */
2461 "R_NDS32_GROUP", /* name */
2462 FALSE, /* partial_inplace */
2463 0xffffffff, /* src_mask */
2464 0xffffffff, /* dst_mask */
2465 FALSE), /* pcrel_offset */
2466 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2468 2, /* size (0 = byte, 1 = short, 2 = long) */
2470 FALSE, /* pc_relative */
2472 complain_overflow_dont,/* complain_on_overflow */
2473 nds32_elf_ignore_reloc,/* special_function */
2474 "R_NDS32_TLS_IEGP_LW", /* name */
2475 FALSE, /* partial_inplace */
2476 0xffffffff, /* src_mask */
2477 0xffffffff, /* dst_mask */
2478 FALSE), /* pcrel_offset */
2479 /* LA and FLSI relaxation. */
2480 HOWTO3 (R_NDS32_LSI, /* type */
2482 2, /* size (0 = byte, 1 = short, 2 = long) */
2484 FALSE, /* pc_relative */
2486 complain_overflow_dont,/* complain_on_overflow */
2487 nds32_elf_ignore_reloc,/* special_function */
2488 "R_NDS32_LSI", /* name */
2489 FALSE, /* partial_inplace */
2490 0xffffffff, /* src_mask */
2491 0xffffffff, /* dst_mask */
2495 static unsigned long dl_tlsdesc_lazy_trampoline[] =
2497 0x46200000, /* sethi $r2,#0x0 */
2498 0x58210000, /* ori $r2,$r2,#0x0 */
2499 0x40217400, /* add $r2,$r2,$gp */
2500 0x04210000, /* lwi $r2,[$r2+#0x0] */
2501 0x46300000, /* sethi $r3,#0x0 */
2502 0x58318000, /* ori $r3,$r3,#0x0 */
2503 0x4031f400, /* add $r3,$r3,$gp */
2504 0x4a000800, /* jr $r2 */
2508 nds32_put_trampoline (void *contents, const unsigned long *template,
2513 for (ix = 0; ix != count; ix++)
2515 unsigned long insn = template[ix];
2516 bfd_putb32 (insn, (char *) contents + ix * 4);
2520 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2521 This prototype is the same as qsort (). */
2524 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2525 int (*compar) (const void *lhs, const void *rhs))
2527 char *ptr = (char *) base;
2529 char *tmp = xmalloc (size);
2531 /* If i is less than j, i is inserted before j.
2533 |---- j ----- i --------------|
2538 for (i = 1; i < (int) nmemb; i++)
2540 for (j = (i - 1); j >= 0; j--)
2541 if (compar (ptr + i * size, ptr + j * size) >= 0)
2547 continue; /* i is in order. */
2549 memcpy (tmp, ptr + i * size, size);
2550 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2551 memcpy (ptr + j * size, tmp, size);
2556 /* Sort relocation by r_offset.
2558 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2559 algorithm. Relocations at the same r_offset must keep their order.
2560 For example, RELAX_ENTRY must be the very first relocation entry.
2562 Currently, this function implements insertion-sort.
2564 FIXME: If we already sort them in assembler, why bother sort them
2568 compar_reloc (const void *lhs, const void *rhs)
2570 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2571 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2573 if (l->r_offset > r->r_offset)
2575 else if (l->r_offset == r->r_offset)
2581 /* Functions listed below are only used for old relocs.
2582 nds32_elf_9_pcrel_reloc
2583 nds32_elf_do_9_pcrel_reloc
2584 nds32_elf_hi20_reloc
2585 nds32_elf_relocate_hi20
2586 nds32_elf_lo12_reloc
2587 nds32_elf_sda15_reloc
2588 nds32_elf_generic_reloc. */
2590 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2592 static bfd_reloc_status_type
2593 nds32_elf_9_pcrel_reloc (bfd * abfd,
2594 arelent * reloc_entry,
2597 asection * input_section,
2599 char ** error_message ATTRIBUTE_UNUSED)
2601 /* This part is from bfd_elf_generic_reloc. */
2602 if (output_bfd != (bfd *) NULL
2603 && (symbol->flags & BSF_SECTION_SYM) == 0
2604 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2606 reloc_entry->address += input_section->output_offset;
2607 return bfd_reloc_ok;
2610 if (output_bfd != NULL)
2612 /* FIXME: See bfd_perform_relocation. Is this right? */
2613 return bfd_reloc_continue;
2616 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2618 data, reloc_entry->address,
2621 + symbol->section->output_section->vma
2622 + symbol->section->output_offset),
2623 reloc_entry->addend);
2626 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2627 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2629 static bfd_reloc_status_type
2630 nds32_elf_do_9_pcrel_reloc (bfd * abfd,
2631 reloc_howto_type * howto,
2632 asection * input_section,
2635 asection * symbol_section ATTRIBUTE_UNUSED,
2636 bfd_vma symbol_value,
2639 bfd_signed_vma relocation;
2641 bfd_reloc_status_type status;
2643 /* Sanity check the address (offset in section). */
2644 if (offset > bfd_get_section_limit (abfd, input_section))
2645 return bfd_reloc_outofrange;
2647 relocation = symbol_value + addend;
2648 /* Make it pc relative. */
2649 relocation -= (input_section->output_section->vma
2650 + input_section->output_offset);
2651 /* These jumps mask off the lower two bits of the current address
2652 before doing pcrel calculations. */
2653 relocation -= (offset & -(bfd_vma) 2);
2655 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2656 status = bfd_reloc_overflow;
2658 status = bfd_reloc_ok;
2660 x = bfd_getb16 (data + offset);
2662 relocation >>= howto->rightshift;
2663 relocation <<= howto->bitpos;
2664 x = (x & ~howto->dst_mask)
2665 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2667 bfd_putb16 ((bfd_vma) x, data + offset);
2672 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2673 HI20_SLO is for the add3 and load/store with displacement instructions.
2674 HI20 is for the or3 instruction.
2675 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2676 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2677 we must add one to the high 16 bytes (which will get subtracted off when
2678 the low 16 bits are added).
2679 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2680 because there is a carry from the LO12 to the HI20. Here we just save
2681 the information we need; we do the actual relocation when we see the LO12.
2682 This code is copied from the elf32-mips.c. We also support an arbitrary
2683 number of HI20 relocs to be associated with a single LO12 reloc. The
2684 assembler sorts the relocs to ensure each HI20 immediately precedes its
2685 LO12. However if there are multiple copies, the assembler may not find
2686 the real LO12 so it picks the first one it finds. */
2690 struct nds32_hi20 *next;
2695 static struct nds32_hi20 *nds32_hi20_list;
2697 static bfd_reloc_status_type
2698 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2699 arelent *reloc_entry,
2702 asection *input_section,
2704 char **error_message ATTRIBUTE_UNUSED)
2706 bfd_reloc_status_type ret;
2708 struct nds32_hi20 *n;
2710 /* This part is from bfd_elf_generic_reloc.
2711 If we're relocating, and this an external symbol, we don't want
2712 to change anything. */
2713 if (output_bfd != (bfd *) NULL
2714 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2716 reloc_entry->address += input_section->output_offset;
2717 return bfd_reloc_ok;
2720 /* Sanity check the address (offset in section). */
2721 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2722 return bfd_reloc_outofrange;
2725 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2726 ret = bfd_reloc_undefined;
2728 if (bfd_is_com_section (symbol->section))
2731 relocation = symbol->value;
2733 relocation += symbol->section->output_section->vma;
2734 relocation += symbol->section->output_offset;
2735 relocation += reloc_entry->addend;
2737 /* Save the information, and let LO12 do the actual relocation. */
2738 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2740 return bfd_reloc_outofrange;
2742 n->addr = (bfd_byte *) data + reloc_entry->address;
2743 n->addend = relocation;
2744 n->next = nds32_hi20_list;
2745 nds32_hi20_list = n;
2747 if (output_bfd != (bfd *) NULL)
2748 reloc_entry->address += input_section->output_offset;
2753 /* Handle an NDS32 ELF HI20 reloc. */
2756 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2757 int type ATTRIBUTE_UNUSED,
2758 Elf_Internal_Rela *relhi,
2759 Elf_Internal_Rela *rello,
2766 insn = bfd_getb32 (contents + relhi->r_offset);
2768 addlo = bfd_getb32 (contents + rello->r_offset);
2771 addend += ((insn & 0xfffff) << 20) + addlo;
2773 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2774 bfd_putb32 (insn, contents + relhi->r_offset);
2777 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2778 inplace relocation; this function exists in order to do the
2779 R_NDS32_HI20_[SU]LO relocation described above. */
2781 static bfd_reloc_status_type
2782 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2783 void *data, asection *input_section, bfd *output_bfd,
2784 char **error_message)
2786 /* This part is from bfd_elf_generic_reloc.
2787 If we're relocating, and this an external symbol, we don't want
2788 to change anything. */
2789 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2790 && reloc_entry->addend == 0)
2792 reloc_entry->address += input_section->output_offset;
2793 return bfd_reloc_ok;
2796 if (nds32_hi20_list != NULL)
2798 struct nds32_hi20 *l;
2800 l = nds32_hi20_list;
2805 unsigned long vallo;
2806 struct nds32_hi20 *next;
2808 /* Do the HI20 relocation. Note that we actually don't need
2809 to know anything about the LO12 itself, except where to
2810 find the low 12 bits of the addend needed by the LO12. */
2811 insn = bfd_getb32 (l->addr);
2812 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2814 switch (reloc_entry->howto->type)
2816 case R_NDS32_LO12S3:
2820 case R_NDS32_LO12S2:
2824 case R_NDS32_LO12S1:
2828 case R_NDS32_LO12S0:
2833 val = ((insn & 0xfffff) << 12) + vallo;
2836 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2837 bfd_putb32 ((bfd_vma) insn, l->addr);
2844 nds32_hi20_list = NULL;
2847 /* Now do the LO12 reloc in the usual way.
2848 ??? It would be nice to call bfd_elf_generic_reloc here,
2849 but we have partial_inplace set. bfd_elf_generic_reloc will
2850 pass the handling back to bfd_install_relocation which will install
2851 a section relative addend which is wrong. */
2852 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2853 input_section, output_bfd, error_message);
2856 /* Do generic partial_inplace relocation.
2857 This is a local replacement for bfd_elf_generic_reloc. */
2859 static bfd_reloc_status_type
2860 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2861 asymbol *symbol, void *data, asection *input_section,
2862 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2864 bfd_reloc_status_type ret;
2866 bfd_byte *inplace_address;
2868 /* This part is from bfd_elf_generic_reloc.
2869 If we're relocating, and this an external symbol, we don't want
2870 to change anything. */
2871 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2872 && reloc_entry->addend == 0)
2874 reloc_entry->address += input_section->output_offset;
2875 return bfd_reloc_ok;
2878 /* Now do the reloc in the usual way.
2879 ??? It would be nice to call bfd_elf_generic_reloc here,
2880 but we have partial_inplace set. bfd_elf_generic_reloc will
2881 pass the handling back to bfd_install_relocation which will install
2882 a section relative addend which is wrong. */
2884 /* Sanity check the address (offset in section). */
2885 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2886 return bfd_reloc_outofrange;
2889 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2890 ret = bfd_reloc_undefined;
2892 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2895 relocation = symbol->value;
2897 /* Only do this for a final link. */
2898 if (output_bfd == (bfd *) NULL)
2900 relocation += symbol->section->output_section->vma;
2901 relocation += symbol->section->output_offset;
2904 relocation += reloc_entry->addend;
2905 switch (reloc_entry->howto->type)
2907 case R_NDS32_LO12S3:
2911 case R_NDS32_LO12S2:
2915 case R_NDS32_LO12S1:
2919 case R_NDS32_LO12S0:
2925 inplace_address = (bfd_byte *) data + reloc_entry->address;
2928 x = ((x & ~reloc_entry->howto->dst_mask) | \
2929 (((x & reloc_entry->howto->src_mask) + relocation) & \
2930 reloc_entry->howto->dst_mask))
2932 switch (reloc_entry->howto->size)
2936 short x = bfd_getb16 (inplace_address);
2939 bfd_putb16 ((bfd_vma) x, inplace_address);
2944 unsigned long x = bfd_getb32 (inplace_address);
2947 bfd_putb32 ((bfd_vma) x, inplace_address);
2954 if (output_bfd != (bfd *) NULL)
2955 reloc_entry->address += input_section->output_offset;
2960 /* Handle the R_NDS32_SDA15 reloc.
2961 This reloc is used to compute the address of objects in the small data area
2962 and to perform loads and stores from that area.
2963 The lower 15 bits are sign extended and added to the register specified
2964 in the instruction, which is assumed to point to _SDA_BASE_.
2966 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2967 the access size, this must be taken care of. */
2969 static bfd_reloc_status_type
2970 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2971 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2972 asection *input_section, bfd *output_bfd,
2973 char **error_message ATTRIBUTE_UNUSED)
2975 /* This part is from bfd_elf_generic_reloc. */
2976 if (output_bfd != (bfd *) NULL
2977 && (symbol->flags & BSF_SECTION_SYM) == 0
2978 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2980 reloc_entry->address += input_section->output_offset;
2981 return bfd_reloc_ok;
2984 if (output_bfd != NULL)
2986 /* FIXME: See bfd_perform_relocation. Is this right? */
2987 return bfd_reloc_continue;
2990 /* FIXME: not sure what to do here yet. But then again, the linker
2991 may never call us. */
2995 /* nds32_elf_ignore_reloc is the special function for
2996 relocation types which don't need to be relocated
2997 like relaxation relocation types.
2998 This function simply return bfd_reloc_ok when it is
3001 static bfd_reloc_status_type
3002 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3003 asymbol *symbol ATTRIBUTE_UNUSED,
3004 void *data ATTRIBUTE_UNUSED, asection *input_section,
3005 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3007 if (output_bfd != NULL)
3008 reloc_entry->address += input_section->output_offset;
3010 return bfd_reloc_ok;
3014 /* Map BFD reloc types to NDS32 ELF reloc types. */
3016 struct nds32_reloc_map_entry
3018 bfd_reloc_code_real_type bfd_reloc_val;
3019 unsigned char elf_reloc_val;
3022 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3024 {BFD_RELOC_NONE, R_NDS32_NONE},
3025 {BFD_RELOC_16, R_NDS32_16_RELA},
3026 {BFD_RELOC_32, R_NDS32_32_RELA},
3027 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3028 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3030 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3031 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3032 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3033 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3034 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3035 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3036 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3037 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3038 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3039 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3040 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3041 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3042 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3043 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3044 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3045 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3046 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3047 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3048 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3049 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3050 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3051 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3052 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3053 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3054 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3055 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3056 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3057 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3058 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3059 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3060 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3061 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3062 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3063 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3064 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3065 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3066 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3067 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3068 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3069 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3070 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3071 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3072 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3073 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3074 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3075 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3076 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3077 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3078 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3079 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3080 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3081 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3082 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3083 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3084 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3085 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3086 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3087 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3088 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3089 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3090 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3091 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3092 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3093 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3094 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3095 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3096 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3097 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3098 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3099 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3100 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3101 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3102 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3103 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3104 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3105 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3106 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3107 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3108 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3109 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3110 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3111 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3112 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3113 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3114 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3115 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3116 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3117 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3118 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3119 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3120 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3121 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3122 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3123 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3124 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3125 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3126 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3127 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3128 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3129 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3130 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3131 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3133 {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3134 /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
3135 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3136 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3137 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3138 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3139 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3140 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3141 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3142 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3143 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3144 {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3145 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3146 {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3147 {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3148 {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3149 {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3150 {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3151 {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3152 {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3153 {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3154 {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3155 {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3156 {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3157 {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3158 {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3159 {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3160 {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3161 {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3166 static reloc_howto_type *
3167 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3172 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3173 if (nds32_elf_howto_table[i].name != NULL
3174 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3175 return &nds32_elf_howto_table[i];
3177 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3178 if (nds32_elf_relax_howto_table[i].name != NULL
3179 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3180 return &nds32_elf_relax_howto_table[i];
3185 static reloc_howto_type *
3186 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3188 if (code < R_NDS32_RELAX_ENTRY)
3190 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3191 return &nds32_elf_howto_table[code];
3195 if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3196 >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3202 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3203 < ARRAY_SIZE (nds32_elf_relax_howto_table));
3204 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3208 static reloc_howto_type *
3209 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3210 bfd_reloc_code_real_type code)
3214 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3216 if (nds32_reloc_map[i].bfd_reloc_val == code)
3217 return bfd_elf32_bfd_reloc_type_table_lookup
3218 (nds32_reloc_map[i].elf_reloc_val);
3224 /* Set the howto pointer for an NDS32 ELF reloc. */
3227 nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3228 Elf_Internal_Rela *dst)
3230 enum elf_nds32_reloc_type r_type;
3232 r_type = ELF32_R_TYPE (dst->r_info);
3233 if (r_type > R_NDS32_GNU_VTENTRY)
3235 /* xgettext:c-format */
3236 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3238 bfd_set_error (bfd_error_bad_value);
3242 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
3243 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3248 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3249 Elf_Internal_Rela *dst)
3251 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3253 if ((r_type == R_NDS32_NONE)
3254 || ((r_type > R_NDS32_GNU_VTENTRY)
3255 && (r_type < R_NDS32_max)))
3257 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3261 /* xgettext:c-format */
3262 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3263 bfd_set_error (bfd_error_bad_value);
3267 /* Support for core dump NOTE sections.
3268 Reference to include/linux/elfcore.h in Linux. */
3271 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3276 switch (note->descsz)
3279 /* Linux/NDS32 32-bit, ABI1. */
3282 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3285 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3293 /* Linux/NDS32 32-bit. */
3296 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3299 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3310 /* Make a ".reg" section. */
3311 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3312 size, note->descpos + offset);
3316 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3318 switch (note->descsz)
3323 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3324 elf_tdata (abfd)->core->program =
3325 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3326 elf_tdata (abfd)->core->command =
3327 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3334 /* Note that for some reason, a spurious space is tacked
3335 onto the end of the args in some (at least one anyway)
3336 implementations, so strip it off if it exists. */
3338 char *command = elf_tdata (abfd)->core->command;
3339 int n = strlen (command);
3341 if (0 < n && command[n - 1] == ' ')
3342 command[n - 1] = '\0';
3348 /* Hook called by the linker routine which adds symbols from an object
3349 file. We must handle the special NDS32 section numbers here.
3350 We also keep watching for whether we need to create the sdata special
3354 nds32_elf_add_symbol_hook (bfd *abfd,
3355 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3356 Elf_Internal_Sym *sym,
3357 const char **namep ATTRIBUTE_UNUSED,
3358 flagword *flagsp ATTRIBUTE_UNUSED,
3359 asection **secp, bfd_vma *valp)
3361 switch (sym->st_shndx)
3364 /* Common symbols less than the GP size are automatically
3365 treated as SHN_MIPS_SCOMMON symbols. */
3366 if (sym->st_size > elf_gp_size (abfd)
3367 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3370 /* st_value is the alignemnt constraint.
3371 That might be its actual size if it is an array or structure. */
3372 switch (sym->st_value)
3375 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3378 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3381 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3384 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3390 (*secp)->flags |= SEC_IS_COMMON;
3391 *valp = sym->st_size;
3398 /* This function can figure out the best location for a base register to access
3399 data relative to this base register
3401 sda_d0: size of first DOUBLE WORD data section
3402 sda_w0: size of first WORD data section
3403 sda_h0: size of first HALF WORD data section
3404 sda_b : size of BYTE data section
3405 sda_hi: size of second HALF WORD data section
3406 sda_w1: size of second WORD data section
3407 sda_d1: size of second DOUBLE WORD data section
3409 offset (always positive) from the beginning of sda_d0 if OK
3410 a negative error value if fail
3412 these 7 sections have to be located back to back if exist
3413 a pass in 0 value for non-existing section */
3415 /* Due to the interpretation of simm15 field of load/store depending on
3416 data accessing size, the organization of base register relative data shall
3417 like the following figure
3418 -------------------------------------------
3419 | DOUBLE WORD sized data (range +/- 128K)
3420 -------------------------------------------
3421 | WORD sized data (range +/- 64K)
3422 -------------------------------------------
3423 | HALF WORD sized data (range +/- 32K)
3424 -------------------------------------------
3425 | BYTE sized data (range +/- 16K)
3426 -------------------------------------------
3427 | HALF WORD sized data (range +/- 32K)
3428 -------------------------------------------
3429 | WORD sized data (range +/- 64K)
3430 -------------------------------------------
3431 | DOUBLE WORD sized data (range +/- 128K)
3432 -------------------------------------------
3433 Its base register shall be set to access these data freely. */
3435 /* We have to figure out the SDA_BASE value, so that we can adjust the
3436 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3437 BFD. If we can't find it, we're stuck. We cache it in the ELF
3438 target data. We don't need to adjust the symbol value for an
3439 external symbol if we are producing relocatable output. */
3441 static asection *sda_rela_sec = NULL;
3443 #define SDA_SECTION_NUM 10
3445 static bfd_reloc_status_type
3446 nds32_elf_final_sda_base (bfd * output_bfd,
3447 struct bfd_link_info * info,
3449 bfd_boolean add_symbol)
3452 struct elf_nds32_link_hash_table *table;
3453 struct bfd_link_hash_entry *h, *h2;
3454 long unsigned int total = 0;
3455 asection *first = NULL, *final = NULL, *temp;
3456 bfd_vma sda_base = 0;
3458 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3459 if (!h || (h->type != bfd_link_hash_defined
3460 && h->type != bfd_link_hash_defweak))
3462 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3463 4 byte-aligned. Therefore, it has to set the first section ".data"
3465 static const char sec_name[SDA_SECTION_NUM][10] =
3467 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3468 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3472 if (output_bfd->sections == NULL)
3474 *psb = elf_gp (output_bfd);
3475 return bfd_reloc_ok;
3478 /* Get the first and final section. */
3479 while (i < ARRAY_SIZE (sec_name))
3481 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3482 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3484 if (temp && (temp->size != 0 || temp->rawsize != 0))
3487 /* Summarize the sections in order to check if joining .bss. */
3488 if (temp && temp->size != 0)
3489 total += temp->size;
3490 else if (temp && temp->rawsize != 0)
3491 total += temp->rawsize;
3496 /* Check .bss size. */
3497 temp = bfd_get_section_by_name (output_bfd, ".bss");
3500 if (temp->size != 0)
3501 total += temp->size;
3502 else if (temp->rawsize != 0)
3503 total += temp->rawsize;
3505 if (total < 0x80000)
3507 if (!first && (temp->size != 0 || temp->rawsize != 0))
3509 if ((temp->size != 0 || temp->rawsize != 0))
3516 /* The middle of data region. */
3517 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3519 /* Find the section sda_base located. */
3521 while (i < ARRAY_SIZE (sec_name))
3523 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3524 if (final && (final->size != 0 || final->rawsize != 0)
3525 && sda_base >= final->vma)
3536 /* If there is not any default data section in output bfd, try to find
3537 the first data section. If no data section be found, just simplily
3538 choose the first output section. */
3539 temp = output_bfd->sections;
3542 if (temp->flags & SEC_ALLOC
3543 && (((temp->flags & SEC_DATA)
3544 && ((temp->flags & SEC_READONLY) == 0))
3545 || (temp->flags & SEC_LOAD) == 0)
3546 && (temp->size != 0 || temp->rawsize != 0))
3555 /* There is no data or bss section. */
3556 if (!first || (first->size == 0 && first->rawsize == 0))
3558 first = output_bfd->sections;
3559 while (first && first->size == 0 && first->rawsize == 0)
3560 first = first->next;
3563 /* There is no concrete section. */
3566 *psb = elf_gp (output_bfd);
3567 return bfd_reloc_ok;
3570 if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3571 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3573 sda_base = first->vma + 0x2000;
3576 sda_base -= first->vma;
3577 sda_base = sda_base & (~7);
3579 if (!_bfd_generic_link_add_one_symbol
3580 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3581 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3582 get_elf_backend_data (output_bfd)->collect, &h))
3585 sda_rela_sec = first;
3588 /* Set _FP_BASE_ to _SDA_BASE_. */
3589 table = nds32_elf_hash_table (info);
3590 relax_fp_as_gp = table->relax_fp_as_gp;
3591 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3592 /* _SDA_BASE_ is difined in linker script. */
3595 first = h->u.def.section;
3596 sda_base = h->u.def.value;
3599 if (relax_fp_as_gp && h2
3600 && (h2->type == bfd_link_hash_undefweak
3601 || h2->type == bfd_link_hash_undefined))
3603 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3604 And set FP equal to SDA_BASE to do relaxation for
3605 la $fp, _FP_BASE_. */
3606 if (!_bfd_generic_link_add_one_symbol
3607 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3608 first, sda_base, (const char *) NULL,
3609 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3618 elf_gp (output_bfd) = (h->u.def.value
3619 + h->u.def.section->output_section->vma
3620 + h->u.def.section->output_offset);
3624 _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3625 return bfd_reloc_dangerous;
3629 *psb = h->u.def.value
3630 + h->u.def.section->output_section->vma
3631 + h->u.def.section->output_offset;
3632 return bfd_reloc_ok;
3636 /* Return size of a PLT entry. */
3637 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3639 /* Create an entry in an nds32 ELF linker hash table. */
3641 static struct bfd_hash_entry *
3642 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3643 struct bfd_hash_table *table,
3646 struct elf_nds32_link_hash_entry *ret;
3648 ret = (struct elf_nds32_link_hash_entry *) entry;
3650 /* Allocate the structure if it has not already been allocated by a
3653 ret = (struct elf_nds32_link_hash_entry *)
3654 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3657 return (struct bfd_hash_entry *) ret;
3659 /* Call the allocation method of the superclass. */
3660 ret = (struct elf_nds32_link_hash_entry *)
3661 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3665 struct elf_nds32_link_hash_entry *eh;
3667 eh = (struct elf_nds32_link_hash_entry *) ret;
3668 eh->dyn_relocs = NULL;
3669 eh->tls_type = GOT_UNKNOWN;
3670 eh->offset_to_gp = 0;
3673 return (struct bfd_hash_entry *) ret;
3676 /* Create an nds32 ELF linker hash table. */
3678 static struct bfd_link_hash_table *
3679 nds32_elf_link_hash_table_create (bfd *abfd)
3681 struct elf_nds32_link_hash_table *ret;
3683 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3685 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3690 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3691 nds32_elf_link_hash_newfunc,
3692 sizeof (struct elf_nds32_link_hash_entry),
3699 ret->sdynbss = NULL;
3700 ret->srelbss = NULL;
3701 ret->sym_ld_script = NULL;
3703 return &ret->root.root;
3706 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3707 shortcuts to them in our hash table. */
3710 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3712 struct elf_link_hash_table *ehtab;
3714 if (!_bfd_elf_create_got_section (dynobj, info))
3717 ehtab = elf_hash_table (info);
3718 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3719 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3720 if (!ehtab->sgot || !ehtab->sgotplt)
3723 /* _bfd_elf_create_got_section will create it for us. */
3724 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3725 if (ehtab->srelgot == NULL
3726 || !bfd_set_section_flags (dynobj, ehtab->srelgot,
3727 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3728 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3730 || !bfd_set_section_alignment (dynobj, ehtab->srelgot, 2))
3736 /* Create dynamic sections when linking against a dynamic object. */
3739 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3741 struct elf_link_hash_table *ehtab;
3742 struct elf_nds32_link_hash_table *htab;
3743 flagword flags, pltflags;
3744 register asection *s;
3745 const struct elf_backend_data *bed;
3746 int ptralign = 2; /* 32-bit */
3747 const char *secname;
3752 bed = get_elf_backend_data (abfd);
3753 ehtab = elf_hash_table (info);
3754 htab = nds32_elf_hash_table (info);
3756 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3757 .rel[a].bss sections. */
3759 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3760 | SEC_LINKER_CREATED);
3763 pltflags |= SEC_CODE;
3764 if (bed->plt_not_loaded)
3765 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3766 if (bed->plt_readonly)
3767 pltflags |= SEC_READONLY;
3769 s = bfd_make_section (abfd, ".plt");
3772 || !bfd_set_section_flags (abfd, s, pltflags)
3773 || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3776 if (bed->want_plt_sym)
3778 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3780 struct bfd_link_hash_entry *bh = NULL;
3781 struct elf_link_hash_entry *h;
3783 if (!(_bfd_generic_link_add_one_symbol
3784 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3785 (bfd_vma) 0, (const char *) NULL, FALSE,
3786 get_elf_backend_data (abfd)->collect, &bh)))
3789 h = (struct elf_link_hash_entry *) bh;
3791 h->type = STT_OBJECT;
3793 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3797 s = bfd_make_section (abfd,
3798 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3801 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3802 || !bfd_set_section_alignment (abfd, s, ptralign))
3805 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3808 for (sec = abfd->sections; sec; sec = sec->next)
3810 secflags = bfd_get_section_flags (abfd, sec);
3811 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3812 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3814 secname = bfd_get_section_name (abfd, sec);
3815 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3816 strcpy (relname, ".rela");
3817 strcat (relname, secname);
3818 if (bfd_get_section_by_name (abfd, secname))
3820 s = bfd_make_section (abfd, relname);
3822 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3823 || !bfd_set_section_alignment (abfd, s, ptralign))
3827 if (bed->want_dynbss)
3829 /* The .dynbss section is a place to put symbols which are defined
3830 by dynamic objects, are referenced by regular objects, and are
3831 not functions. We must allocate space for them in the process
3832 image and use a R_*_COPY reloc to tell the dynamic linker to
3833 initialize them at run time. The linker script puts the .dynbss
3834 section into the .bss section of the final image. */
3835 s = bfd_make_section (abfd, ".dynbss");
3838 || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3840 /* The .rel[a].bss section holds copy relocs. This section is not
3841 normally needed. We need to create it here, though, so that the
3842 linker will map it to an output section. We can't just create it
3843 only if we need it, because we will not know whether we need it
3844 until we have seen all the input files, and the first time the
3845 main linker code calls BFD after examining all the input files
3846 (size_dynamic_sections) the input sections have already been
3847 mapped to the output sections. If the section turns out not to
3848 be needed, we can discard it later. We will never need this
3849 section when generating a shared object, since they do not use
3851 if (!bfd_link_pic (info))
3853 s = bfd_make_section (abfd, (bed->default_use_rela_p
3854 ? ".rela.bss" : ".rel.bss"));
3857 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3858 || !bfd_set_section_alignment (abfd, s, ptralign))
3866 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3868 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3869 struct elf_link_hash_entry *dir,
3870 struct elf_link_hash_entry *ind)
3872 struct elf_nds32_link_hash_entry *edir, *eind;
3874 edir = (struct elf_nds32_link_hash_entry *) dir;
3875 eind = (struct elf_nds32_link_hash_entry *) ind;
3877 if (eind->dyn_relocs != NULL)
3879 if (edir->dyn_relocs != NULL)
3881 struct elf_dyn_relocs **pp;
3882 struct elf_dyn_relocs *p;
3884 if (ind->root.type == bfd_link_hash_indirect)
3887 /* Add reloc counts against the weak sym to the strong sym
3888 list. Merge any entries against the same section. */
3889 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3891 struct elf_dyn_relocs *q;
3893 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3894 if (q->sec == p->sec)
3896 q->pc_count += p->pc_count;
3897 q->count += p->count;
3904 *pp = edir->dyn_relocs;
3907 edir->dyn_relocs = eind->dyn_relocs;
3908 eind->dyn_relocs = NULL;
3911 if (ind->root.type == bfd_link_hash_indirect)
3913 if (dir->got.refcount <= 0)
3915 edir->tls_type = eind->tls_type;
3916 eind->tls_type = GOT_UNKNOWN;
3920 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3924 /* Find dynamic relocs for H that apply to read-only sections. */
3927 readonly_dynrelocs (struct elf_link_hash_entry *h)
3929 struct elf_dyn_relocs *p;
3931 for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3933 asection *s = p->sec->output_section;
3935 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3941 /* Adjust a symbol defined by a dynamic object and referenced by a
3942 regular object. The current definition is in some section of the
3943 dynamic object, but we're not including those sections. We have to
3944 change the definition to something the rest of the link can
3948 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3949 struct elf_link_hash_entry *h)
3951 struct elf_nds32_link_hash_table *htab;
3954 unsigned int power_of_two;
3956 dynobj = elf_hash_table (info)->dynobj;
3958 /* Make sure we know what is going on here. */
3959 BFD_ASSERT (dynobj != NULL
3962 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3965 /* If this is a function, put it in the procedure linkage table. We
3966 will fill in the contents of the procedure linkage table later,
3967 when we know the address of the .got section. */
3968 if (h->type == STT_FUNC || h->needs_plt)
3970 if (!bfd_link_pic (info)
3973 && h->root.type != bfd_link_hash_undefweak
3974 && h->root.type != bfd_link_hash_undefined)
3976 /* This case can occur if we saw a PLT reloc in an input
3977 file, but the symbol was never referred to by a dynamic
3978 object. In such a case, we don't actually need to build
3979 a procedure linkage table, and we can just do a PCREL
3981 h->plt.offset = (bfd_vma) - 1;
3988 h->plt.offset = (bfd_vma) - 1;
3990 /* If this is a weak symbol, and there is a real definition, the
3991 processor independent code will have arranged for us to see the
3992 real definition first, and we can just use the same value. */
3993 if (h->is_weakalias)
3995 struct elf_link_hash_entry *def = weakdef (h);
3996 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3997 h->root.u.def.section = def->root.u.def.section;
3998 h->root.u.def.value = def->root.u.def.value;
4002 /* This is a reference to a symbol defined by a dynamic object which
4003 is not a function. */
4005 /* If we are creating a shared library, we must presume that the
4006 only references to the symbol are via the global offset table.
4007 For such cases we need not do anything here; the relocations will
4008 be handled correctly by relocate_section. */
4009 if (bfd_link_pic (info))
4012 /* If there are no references to this symbol that do not use the
4013 GOT, we don't need to generate a copy reloc. */
4014 if (!h->non_got_ref)
4017 /* If -z nocopyreloc was given, we won't generate them either. */
4018 if (0 && info->nocopyreloc)
4024 /* If we don't find any dynamic relocs in read-only sections, then
4025 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
4026 if (!readonly_dynrelocs (h))
4032 /* We must allocate the symbol in our .dynbss section, which will
4033 become part of the .bss section of the executable. There will be
4034 an entry for this symbol in the .dynsym section. The dynamic
4035 object will contain position independent code, so all references
4036 from the dynamic object to this symbol will go through the global
4037 offset table. The dynamic linker will use the .dynsym entry to
4038 determine the address it must put in the global offset table, so
4039 both the dynamic object and the regular object will refer to the
4040 same memory location for the variable. */
4042 htab = nds32_elf_hash_table (info);
4044 BFD_ASSERT (s != NULL);
4046 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4047 to copy the initial value out of the dynamic object and into the
4048 runtime process image. We need to remember the offset into the
4049 .rela.bss section we are going to use. */
4050 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4054 srel = htab->srelbss;
4055 BFD_ASSERT (srel != NULL);
4056 srel->size += sizeof (Elf32_External_Rela);
4060 /* We need to figure out the alignment required for this symbol. I
4061 have no idea how ELF linkers handle this. */
4062 power_of_two = bfd_log2 (h->size);
4063 if (power_of_two > 3)
4066 /* Apply the required alignment. */
4067 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4068 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4070 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
4074 /* Define the symbol as being at this point in the section. */
4075 h->root.u.def.section = s;
4076 h->root.u.def.value = s->size;
4078 /* Increment the section size to make room for the symbol. */
4084 /* Allocate space in .plt, .got and associated reloc sections for
4088 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4090 struct bfd_link_info *info;
4091 struct elf_link_hash_table *ehtab;
4092 struct elf_nds32_link_hash_table *htab;
4093 struct elf_nds32_link_hash_entry *eh;
4094 struct elf_dyn_relocs *p;
4096 if (h->root.type == bfd_link_hash_indirect)
4099 /* When warning symbols are created, they **replace** the "real"
4100 entry in the hash table, thus we never get to see the real
4101 symbol in a hash traversal. So look at it now. */
4102 if (h->root.type == bfd_link_hash_warning)
4103 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4105 eh = (struct elf_nds32_link_hash_entry *) h;
4107 info = (struct bfd_link_info *) inf;
4108 ehtab = elf_hash_table (info);
4109 htab = nds32_elf_hash_table (info);
4113 eh = (struct elf_nds32_link_hash_entry *) h;
4115 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4116 && h->plt.refcount > 0
4117 && !(bfd_link_pie (info) && h->def_regular))
4119 /* Make sure this symbol is output as a dynamic symbol.
4120 Undefined weak syms won't yet be marked as dynamic. */
4121 if (h->dynindx == -1 && !h->forced_local)
4123 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4129 asection *s = ehtab->splt;
4131 /* If this is the first .plt entry, make room for the special
4134 s->size += PLT_ENTRY_SIZE;
4136 h->plt.offset = s->size;
4138 /* If this symbol is not defined in a regular file, and we are
4139 not generating a shared library, then set the symbol to this
4140 location in the .plt. This is required to make function
4141 pointers compare as equal between the normal executable and
4142 the shared library. */
4143 if (!bfd_link_pic (info) && !h->def_regular)
4145 h->root.u.def.section = s;
4146 h->root.u.def.value = h->plt.offset;
4149 /* Make room for this entry. */
4150 s->size += PLT_ENTRY_SIZE;
4152 /* We also need to make an entry in the .got.plt section, which
4153 will be placed in the .got section by the linker script. */
4154 ehtab->sgotplt->size += 4;
4156 /* We also need to make an entry in the .rel.plt section. */
4157 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4158 if (htab->tls_desc_trampoline)
4159 htab->next_tls_desc_index++;
4163 h->plt.offset = (bfd_vma) - 1;
4169 h->plt.offset = (bfd_vma) - 1;
4173 if (h->got.refcount > 0)
4177 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4179 /* Make sure this symbol is output as a dynamic symbol.
4180 Undefined weak syms won't yet be marked as dynamic. */
4181 if (h->dynindx == -1 && !h->forced_local)
4183 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4187 sgot = elf_hash_table (info)->sgot;
4188 h->got.offset = sgot->size;
4190 if (tls_type == GOT_UNKNOWN)
4193 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4194 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4198 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4199 if (tls_type & GOT_TLS_DESC)
4203 dyn = htab->root.dynamic_sections_created;
4205 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4207 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4209 /* TLS_DESC with trampoline needs a relocation slot
4210 within .rela.plt. */
4211 htab->num_tls_desc++;
4212 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4213 htab->tls_trampoline = -1;
4217 /* other relocations, including TLS_DESC without trampoline, need
4218 a relocation slot within .rela.got. */
4219 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4224 h->got.offset = (bfd_vma)-1;
4226 if (eh->dyn_relocs == NULL)
4229 /* In the shared -Bsymbolic case, discard space allocated for
4230 dynamic pc-relative relocs against symbols which turn out to be
4231 defined in regular objects. For the normal shared case, discard
4232 space for pc-relative relocs that have become local due to symbol
4233 visibility changes. */
4235 if (bfd_link_pic (info))
4237 if (h->def_regular && (h->forced_local || info->symbolic))
4239 struct elf_dyn_relocs **pp;
4241 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
4243 p->count -= p->pc_count;
4254 /* For the non-shared case, discard space for relocs against
4255 symbols which turn out to need copy relocs or are not dynamic. */
4260 || (htab->root.dynamic_sections_created
4261 && (h->root.type == bfd_link_hash_undefweak
4262 || h->root.type == bfd_link_hash_undefined))))
4264 /* Make sure this symbol is output as a dynamic symbol.
4265 Undefined weak syms won't yet be marked as dynamic. */
4266 if (h->dynindx == -1 && !h->forced_local)
4268 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4272 /* If that succeeded, we know we'll be keeping all the
4274 if (h->dynindx != -1)
4278 eh->dyn_relocs = NULL;
4283 /* Finally, allocate space. */
4284 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4286 asection *sreloc = elf_section_data (p->sec)->sreloc;
4287 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4293 /* Add relocation REL to the end of relocation section SRELOC. */
4296 elf32_nds32_add_dynreloc (bfd *output_bfd,
4297 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4298 asection *sreloc, Elf_Internal_Rela *rel)
4304 loc = sreloc->contents;
4305 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4306 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4309 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4312 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
4313 read-only sections. */
4316 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
4320 if (h->root.type == bfd_link_hash_indirect)
4323 sec = readonly_dynrelocs (h);
4326 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
4328 info->flags |= DF_TEXTREL;
4329 info->callbacks->minfo
4330 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
4331 sec->owner, h->root.root.string, sec);
4333 /* Not an error, just cut short the traversal. */
4339 /* Set the sizes of the dynamic sections. */
4342 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4343 struct bfd_link_info *info)
4345 struct elf_nds32_link_hash_table *htab;
4352 htab = nds32_elf_hash_table (info);
4356 dynobj = elf_hash_table (info)->dynobj;
4357 BFD_ASSERT (dynobj != NULL);
4359 if (elf_hash_table (info)->dynamic_sections_created)
4361 /* Set the contents of the .interp section to the interpreter. */
4362 if (bfd_link_executable (info) && !info->nointerp)
4364 s = bfd_get_section_by_name (dynobj, ".interp");
4365 BFD_ASSERT (s != NULL);
4366 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4367 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4371 /* Set up .got offsets for local syms, and space for local dynamic
4373 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4375 bfd_signed_vma *local_got;
4376 bfd_signed_vma *end_local_got;
4377 bfd_size_type locsymcount;
4378 Elf_Internal_Shdr *symtab_hdr;
4380 char *local_tls_type;
4381 unsigned long symndx;
4382 bfd_vma *local_tlsdesc_gotent;
4384 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4387 for (s = ibfd->sections; s != NULL; s = s->next)
4389 struct elf_dyn_relocs *p;
4391 for (p = ((struct elf_dyn_relocs *)
4392 elf_section_data (s)->local_dynrel);
4393 p != NULL; p = p->next)
4395 if (!bfd_is_abs_section (p->sec)
4396 && bfd_is_abs_section (p->sec->output_section))
4398 /* Input section has been discarded, either because
4399 it is a copy of a linkonce section or due to
4400 linker script /DISCARD/, so we'll be discarding
4403 else if (p->count != 0)
4405 asection *sreloc = elf_section_data (p->sec)->sreloc;
4406 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4407 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4408 info->flags |= DF_TEXTREL;
4413 local_got = elf_local_got_refcounts (ibfd);
4417 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4418 locsymcount = symtab_hdr->sh_info;
4419 end_local_got = local_got + locsymcount;
4420 sgot = elf_hash_table (info)->sgot;
4421 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4422 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4423 for (symndx = 0; local_got < end_local_got;
4424 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4428 int num_of_got_entry_needed = 0;
4429 *local_got = sgot->size;
4430 *local_tlsdesc_gotent = sgot->size;
4432 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4433 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4434 num_of_got_entry_needed = 1;
4435 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4436 else if (*local_tls_type & GOT_TLS_DESC)
4437 num_of_got_entry_needed = 2;
4439 sgot->size += (num_of_got_entry_needed << 2);
4441 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4442 others need a slot in .rela.got. */
4443 if (*local_tls_type == GOT_TLS_DESC)
4445 if (bfd_link_pic (info))
4447 if (htab->tls_desc_trampoline)
4449 htab->num_tls_desc++;
4450 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4451 htab->tls_trampoline = -1;
4454 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4458 /* TLS_DESC -> TLS_LE */
4463 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4468 *local_got = (bfd_vma) -1;
4469 *local_tlsdesc_gotent = (bfd_vma) -1;
4474 /* Allocate global sym .plt and .got entries, and space for global
4475 sym dynamic relocs. */
4476 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4478 /* For every jump slot reserved in the sgotplt, reloc_count is
4479 incremented. However, when we reserve space for TLS descriptors,
4480 it's not incremented, so in order to compute the space reserved
4481 for them, it suffices to multiply the reloc count by the jump
4483 if (htab->tls_desc_trampoline && htab->root.srelplt)
4484 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4486 if (htab->tls_trampoline)
4488 htab->tls_trampoline = htab->root.splt->size;
4490 /* If we're not using lazy TLS relocations, don't generate the
4491 PLT and GOT entries they require. */
4492 if (!(info->flags & DF_BIND_NOW))
4494 htab->dt_tlsdesc_got = htab->root.sgot->size;
4495 htab->root.sgot->size += 4;
4497 htab->dt_tlsdesc_plt = htab->root.splt->size;
4498 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4502 /* We now have determined the sizes of the various dynamic sections.
4503 Allocate memory for them. */
4504 /* The check_relocs and adjust_dynamic_symbol entry points have
4505 determined the sizes of the various dynamic sections. Allocate
4509 for (s = dynobj->sections; s != NULL; s = s->next)
4511 if ((s->flags & SEC_LINKER_CREATED) == 0)
4514 if (s == htab->root.splt)
4516 /* Strip this section if we don't need it; see the
4520 else if (s == elf_hash_table (info)->sgot)
4522 got_size += s->size;
4524 else if (s == elf_hash_table (info)->sgotplt)
4526 got_size += s->size;
4528 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4530 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4533 /* We use the reloc_count field as a counter if we need
4534 to copy relocs into the output file. */
4539 /* It's not one of our sections, so don't allocate space. */
4545 /* If we don't need this section, strip it from the
4546 output file. This is mostly to handle .rela.bss and
4547 .rela.plt. We must create both sections in
4548 create_dynamic_sections, because they must be created
4549 before the linker maps input sections to output
4550 sections. The linker does that before
4551 adjust_dynamic_symbol is called, and it is that
4552 function which decides whether anything needs to go
4553 into these sections. */
4554 s->flags |= SEC_EXCLUDE;
4558 /* Allocate memory for the section contents. We use bfd_zalloc
4559 here in case unused entries are not reclaimed before the
4560 section's contents are written out. This should not happen,
4561 but this way if it does, we get a R_NDS32_NONE reloc instead
4563 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4564 if (s->contents == NULL)
4569 if (htab->root.dynamic_sections_created)
4571 /* Add some entries to the .dynamic section. We fill in the
4572 values later, in nds32_elf_finish_dynamic_sections, but we
4573 must add the entries now so that we get the correct size for
4574 the .dynamic section. The DT_DEBUG entry is filled in by the
4575 dynamic linker and used by the debugger. */
4576 #define add_dynamic_entry(TAG, VAL) \
4577 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4579 if (bfd_link_executable (info))
4581 if (!add_dynamic_entry (DT_DEBUG, 0))
4585 if (elf_hash_table (info)->splt->size != 0)
4587 if (!add_dynamic_entry (DT_PLTGOT, 0)
4588 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4589 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4590 || !add_dynamic_entry (DT_JMPREL, 0))
4594 if (htab->tls_desc_trampoline && plt)
4596 if (htab->dt_tlsdesc_plt
4597 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4598 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4604 if (!add_dynamic_entry (DT_RELA, 0)
4605 || !add_dynamic_entry (DT_RELASZ, 0)
4606 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4609 /* If any dynamic relocs apply to a read-only section,
4610 then we need a DT_TEXTREL entry. */
4611 if ((info->flags & DF_TEXTREL) == 0)
4612 elf_link_hash_traverse (&htab->root, maybe_set_textrel,
4615 if ((info->flags & DF_TEXTREL) != 0)
4617 if (!add_dynamic_entry (DT_TEXTREL, 0))
4622 #undef add_dynamic_entry
4627 static bfd_reloc_status_type
4628 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4629 bfd_vma relocation, bfd_byte *location)
4633 bfd_reloc_status_type flag;
4634 unsigned int rightshift = howto->rightshift;
4635 unsigned int bitpos = howto->bitpos;
4637 /* If the size is negative, negate RELOCATION. This isn't very
4639 if (howto->size < 0)
4640 relocation = -relocation;
4642 /* Get the value we are going to relocate. */
4643 size = bfd_get_reloc_size (howto);
4650 return bfd_reloc_ok;
4652 x = bfd_getb16 (location);
4655 x = bfd_getb32 (location);
4659 /* Check for overflow. FIXME: We may drop bits during the addition
4660 which we don't check for. We must either check at every single
4661 operation, which would be tedious, or we must do the computations
4662 in a type larger than bfd_vma, which would be inefficient. */
4663 flag = bfd_reloc_ok;
4664 if (howto->complain_on_overflow != complain_overflow_dont)
4666 bfd_vma addrmask, fieldmask, signmask, ss;
4669 /* Get the values to be added together. For signed and unsigned
4670 relocations, we assume that all values should be truncated to
4671 the size of an address. For bitfields, all the bits matter.
4672 See also bfd_check_overflow. */
4673 fieldmask = N_ONES (howto->bitsize);
4674 signmask = ~fieldmask;
4675 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4676 a = (relocation & addrmask) >> rightshift;
4677 b = (x & howto->src_mask & addrmask) >> bitpos;
4679 switch (howto->complain_on_overflow)
4681 case complain_overflow_signed:
4682 /* If any sign bits are set, all sign bits must be set.
4683 That is, A must be a valid negative address after
4685 signmask = ~(fieldmask >> 1);
4688 case complain_overflow_bitfield:
4689 /* Much like the signed check, but for a field one bit
4690 wider. We allow a bitfield to represent numbers in the
4691 range -2**n to 2**n-1, where n is the number of bits in the
4692 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4693 can't overflow, which is exactly what we want. */
4695 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4696 flag = bfd_reloc_overflow;
4698 /* We only need this next bit of code if the sign bit of B
4699 is below the sign bit of A. This would only happen if
4700 SRC_MASK had fewer bits than BITSIZE. Note that if
4701 SRC_MASK has more bits than BITSIZE, we can get into
4702 trouble; we would need to verify that B is in range, as
4703 we do for A above. */
4704 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4707 /* Set all the bits above the sign bit. */
4710 /* Now we can do the addition. */
4713 /* See if the result has the correct sign. Bits above the
4714 sign bit are junk now; ignore them. If the sum is
4715 positive, make sure we did not have all negative inputs;
4716 if the sum is negative, make sure we did not have all
4717 positive inputs. The test below looks only at the sign
4718 bits, and it really just
4719 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4721 We mask with addrmask here to explicitly allow an address
4722 wrap-around. The Linux kernel relies on it, and it is
4723 the only way to write assembler code which can run when
4724 loaded at a location 0x80000000 away from the location at
4725 which it is linked. */
4726 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4727 flag = bfd_reloc_overflow;
4731 case complain_overflow_unsigned:
4732 /* Checking for an unsigned overflow is relatively easy:
4733 trim the addresses and add, and trim the result as well.
4734 Overflow is normally indicated when the result does not
4735 fit in the field. However, we also need to consider the
4736 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4737 input is 0x80000000, and bfd_vma is only 32 bits; then we
4738 will get sum == 0, but there is an overflow, since the
4739 inputs did not fit in the field. Instead of doing a
4740 separate test, we can check for this by or-ing in the
4741 operands when testing for the sum overflowing its final
4743 sum = (a + b) & addrmask;
4744 if ((a | b | sum) & signmask)
4745 flag = bfd_reloc_overflow;
4753 /* Put RELOCATION in the right bits. */
4754 relocation >>= (bfd_vma) rightshift;
4755 relocation <<= (bfd_vma) bitpos;
4757 /* Add RELOCATION to the right bits of X. */
4759 Because the relaxation may generate duplicate relocation at one address,
4760 an addition to immediate in the instruction may cause the relocation added
4762 This bug should be fixed in assembler, but a check is also needed here. */
4763 if (howto->partial_inplace)
4764 x = ((x & ~howto->dst_mask)
4765 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4767 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4770 /* Put the relocated value back in the object file. */
4780 bfd_putb16 (x, location);
4783 bfd_putb32 (x, location);
4790 static bfd_reloc_status_type
4791 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4792 asection *input_section, bfd_byte *contents,
4793 bfd_vma address, bfd_vma value, bfd_vma addend)
4797 /* Sanity check the address. */
4798 if (address > bfd_get_section_limit (input_bfd, input_section))
4799 return bfd_reloc_outofrange;
4801 /* This function assumes that we are dealing with a basic relocation
4802 against a symbol. We want to compute the value of the symbol to
4803 relocate to. This is just VALUE, the value of the symbol, plus
4804 ADDEND, any addend associated with the reloc. */
4805 relocation = value + addend;
4807 /* If the relocation is PC relative, we want to set RELOCATION to
4808 the distance between the symbol (currently in RELOCATION) and the
4809 location we are relocating. If pcrel_offset is FALSE we do not
4810 need to subtract out the offset of the location within the
4811 section (which is just ADDRESS). */
4812 if (howto->pc_relative)
4814 relocation -= (input_section->output_section->vma
4815 + input_section->output_offset);
4816 if (howto->pcrel_offset)
4817 relocation -= address;
4820 return nds32_relocate_contents (howto, input_bfd, relocation,
4821 contents + address);
4825 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4827 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4828 asection *input_sec,
4829 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4832 FILE *sym_ld_script = NULL;
4833 struct elf_nds32_link_hash_table *table;
4835 table = nds32_elf_hash_table (info);
4836 sym_ld_script = table->sym_ld_script;
4840 if (!h || !name || *name == '\0')
4843 if (input_sec->flags & SEC_EXCLUDE)
4846 if (!check_start_export_sym)
4848 fprintf (sym_ld_script, "SECTIONS\n{\n");
4849 check_start_export_sym = 1;
4852 if (h->root.type == bfd_link_hash_defined
4853 || h->root.type == bfd_link_hash_defweak)
4855 if (!h->root.u.def.section->output_section)
4858 if (bfd_is_const_section (input_sec))
4859 source = input_sec->name;
4861 source = input_sec->owner->filename;
4863 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4864 h->root.root.string,
4865 (long) (h->root.u.def.value
4866 + h->root.u.def.section->output_section->vma
4867 + h->root.u.def.section->output_offset), source);
4873 /* Relocate an NDS32/D ELF section.
4874 There is some attempt to make this function usable for many architectures,
4875 both for RELA and REL type relocs, if only to serve as a learning tool.
4877 The RELOCATE_SECTION function is called by the new ELF backend linker
4878 to handle the relocations for a section.
4880 The relocs are always passed as Rela structures; if the section
4881 actually uses Rel structures, the r_addend field will always be
4884 This function is responsible for adjust the section contents as
4885 necessary, and (if using Rela relocs and generating a
4886 relocatable output file) adjusting the reloc addend as
4889 This function does not have to worry about setting the reloc
4890 address or the reloc symbol index.
4892 LOCAL_SYMS is a pointer to the swapped in local symbols.
4894 LOCAL_SECTIONS is an array giving the section in the input file
4895 corresponding to the st_shndx field of each local symbol.
4897 The global hash table entry for the global symbols can be found
4898 via elf_sym_hashes (input_bfd).
4900 When generating relocatable output, this function must handle
4901 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4902 going to be the section symbol corresponding to the output
4903 section, which means that the addend must be adjusted
4906 /* Return the base VMA address which should be subtracted from real addresses
4907 when resolving @dtpoff relocation.
4908 This is PT_TLS segment p_vaddr. */
4910 /* Return the relocation value for @tpoff relocation
4911 if STT_TLS virtual address is ADDRESS. */
4913 /* Return the relocation value for @gottpoff relocation
4914 if STT_TLS virtual address is ADDRESS. */
4917 gottpoff (struct bfd_link_info *info, bfd_vma address)
4922 /* If tls_sec is NULL, we should have signalled an error already. */
4923 if (elf_hash_table (info)->tls_sec == NULL)
4926 tp_base = elf_hash_table (info)->tls_sec->vma;
4927 tp_offset = address - tp_base;
4933 patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4935 /* TLS_GD/TLS_LD model #1
4936 46 00 00 00 sethi $r0,#0x0
4937 58 00 00 00 ori $r0,$r0,#0x0
4938 40 00 74 00 add $r0,$r0,$gp
4939 04 10 00 00 lwi $r1,[$r0+#0x0]
4940 4b e0 04 01 jral $lp,$r1 */
4942 /* TLS_GD/TLS_LD model #2
4943 46 00 00 00 sethi $r0,#0x0
4944 58 00 00 00 ori $r0,$r0,#0x0
4945 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4946 40 00 74 00 add $r0,$r0,$gp
4947 4b e0 04 01 jral $lp,$r1 */
4949 /* TLS_IE model (non-PIC)
4950 46 00 00 00 sethi $r0,#0x0
4951 04 00 00 00 lwi $r0,[$r0+#0x0]
4952 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4954 /* TLS_IE model (PIC)
4955 46 00 00 00 sethi $r0,#0x0
4956 58 00 00 00 ori $r0,$r0,#0x0
4957 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4958 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4960 /* TLS_GD_TO_IE model
4961 46 00 00 00 sethi $r0,#0x0
4962 58 00 00 00 ori $r0,$r0,#0x0
4963 40 00 74 00 add $r0,$rM,$gp
4964 04 00 00 01 lwi $r0,[$r0+#0x4]
4965 40 00 64 00 add $r0,$r0,$r25 */
4967 bfd_boolean rz = FALSE;
4977 0x40007400, /* add $r0,$rM,$gp */
4978 0x04000001, /* lwi $r0,[$r0+#0x4] */
4979 0x40006400, /* add $r0,$r0,$r25 */
4984 { 0x40000000, 0xfe0003ff },
4985 { 0x04000000, 0xfe000000 },
4986 { 0x4be00001, 0xffff83ff },
4991 { 0x38007402, 0xfe007fff },
4992 { 0x40007400, 0xfe007fff },
4993 { 0x4be00001, 0xffff83ff },
4996 unsigned char *p = contents + rel->r_offset;
4999 uint32_t regidx = 0;
5000 insn = bfd_getb32 (p);
5001 if (INSN_SETHI == (0xfe0fffffu & insn))
5003 regidx = 0x1f & (insn >> 20);
5007 insn = bfd_getb32 (p);
5008 if (INSN_ORI == (0xfe007fffu & insn))
5010 regidx = 0x1f & (insn >> 20);
5014 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
5016 /* already patched? */
5017 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
5018 (patch[1] == bfd_getb32 (p + 4)))
5021 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
5023 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
5024 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
5026 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5027 bfd_putb32 (patch[1], p + 4);
5028 bfd_putb32 (patch[2], p + 8);
5032 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
5034 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
5035 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5037 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5038 bfd_putb32 (patch[1], p + 4);
5039 bfd_putb32 (patch[2], p + 8);
5046 printf ("%s: %s @ 0x%08x\n", __func__, ibfd->filename,
5047 (int) rel->r_offset);
5048 BFD_ASSERT(0); /* Unsupported pattern. */
5054 static enum elf_nds32_tls_type
5055 get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5058 ones32 (register unsigned int x)
5060 /* 32-bit recursive reduction using SWAR...
5061 but first step is mapping 2-bit values
5062 into sum of 2 1-bit values in sneaky way. */
5063 x -= ((x >> 1) & 0x55555555);
5064 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5065 x = (((x >> 4) + x) & 0x0f0f0f0f);
5068 return (x & 0x0000003f);
5073 fls (register unsigned int x)
5075 return ffs (x & (-x));
5077 #endif /* !HAVE_FLS */
5079 #define nds32_elf_local_tlsdesc_gotent(bfd) \
5080 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5083 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
5084 struct bfd_link_info * info,
5086 asection * input_section,
5087 bfd_byte * contents,
5088 Elf_Internal_Rela * relocs,
5089 Elf_Internal_Sym * local_syms,
5090 asection ** local_sections)
5092 Elf_Internal_Shdr *symtab_hdr;
5093 struct elf_link_hash_entry **sym_hashes;
5094 Elf_Internal_Rela *rel, *relend;
5095 bfd_boolean ret = TRUE; /* Assume success. */
5097 bfd_reloc_status_type r;
5098 const char *errmsg = NULL;
5100 struct elf_link_hash_table *ehtab;
5101 struct elf_nds32_link_hash_table *htab;
5103 bfd_vma *local_got_offsets;
5104 asection *sgot, *splt, *sreloc;
5105 bfd_vma high_address;
5106 struct elf_nds32_link_hash_table *table;
5107 int eliminate_gc_relocs;
5108 bfd_vma fpbase_addr;
5110 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5111 sym_hashes = elf_sym_hashes (input_bfd);
5112 ehtab = elf_hash_table (info);
5113 htab = nds32_elf_hash_table (info);
5114 high_address = bfd_get_section_limit (input_bfd, input_section);
5116 dynobj = htab->root.dynobj;
5117 local_got_offsets = elf_local_got_offsets (input_bfd);
5124 relend = relocs + input_section->reloc_count;
5126 table = nds32_elf_hash_table (info);
5127 eliminate_gc_relocs = table->eliminate_gc_relocs;
5129 /* By this time, we can adjust the value of _SDA_BASE_. */
5130 /* Explain _SDA_BASE_ */
5131 if ((!bfd_link_relocatable (info)))
5133 is_SDA_BASE_set = 1;
5134 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5135 if (r != bfd_reloc_ok)
5139 /* Do TLS model conversion once at first. */
5140 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5142 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5143 the fp value is set as gp, and it has be reverted for instruction
5145 fpbase_addr = elf_gp (output_bfd);
5147 /* Deal with (dynamic) relocations. */
5148 for (rel = relocs; rel < relend; rel++)
5150 enum elf_nds32_reloc_type r_type;
5151 reloc_howto_type *howto = NULL;
5152 unsigned long r_symndx;
5153 struct elf_link_hash_entry *h = NULL;
5154 Elf_Internal_Sym *sym = NULL;
5157 bfd_vma relocation_sym = 0xdeadbeef;
5158 Elf_Internal_Rela *lorel;
5161 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5162 ensure it's zero (we use REL relocs, not RELA). Therefore this
5163 should be assigning zero to `addend', but for clarity we use
5166 bfd_vma addend = rel->r_addend;
5167 bfd_vma offset = rel->r_offset;
5169 r_type = ELF32_R_TYPE (rel->r_info);
5170 if (r_type >= R_NDS32_max)
5172 /* xgettext:c-format */
5173 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5175 bfd_set_error (bfd_error_bad_value);
5180 if (r_type == R_NDS32_GNU_VTENTRY
5181 || r_type == R_NDS32_GNU_VTINHERIT
5182 || r_type == R_NDS32_NONE
5183 || r_type == R_NDS32_RELA_GNU_VTENTRY
5184 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5185 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5186 || r_type == R_NDS32_DATA
5187 || r_type == R_NDS32_TRAN)
5190 /* If we enter the fp-as-gp region. Resolve the address
5192 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5193 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5197 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5198 dist = rel->r_addend >> 16;
5199 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5200 local_syms, symtab_hdr);
5202 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5203 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5205 fpbase_addr = elf_gp (output_bfd);
5208 /* Skip the relocations used for relaxation. */
5209 /* We have to update LONGCALL and LONGJUMP
5210 relocations when generating the relocatable files. */
5211 if (!bfd_link_relocatable (info)
5212 && (r_type >= R_NDS32_RELAX_ENTRY
5213 || (r_type >= R_NDS32_LONGCALL4
5214 && r_type <= R_NDS32_LONGJUMP7)))
5217 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5218 r_symndx = ELF32_R_SYM (rel->r_info);
5220 /* This is a final link. */
5225 if (r_symndx < symtab_hdr->sh_info)
5228 sym = local_syms + r_symndx;
5229 sec = local_sections[r_symndx];
5231 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5232 addend = rel->r_addend;
5234 /* keep symbol location for static TLS_IE GOT entry */
5235 relocation_sym = relocation;
5236 if (bfd_link_relocatable (info))
5238 /* This is a relocatable link. We don't have to change
5239 anything, unless the reloc is against a section symbol,
5240 in which case we have to adjust according to where the
5241 section symbol winds up in the output section. */
5242 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5243 rel->r_addend += sec->output_offset + sym->st_value;
5250 /* External symbol. */
5251 if (bfd_link_relocatable (info))
5253 bfd_boolean warned, ignored, unresolved_reloc;
5254 int symndx = r_symndx - symtab_hdr->sh_info;
5256 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5257 r_symndx, symtab_hdr, sym_hashes, h, sec,
5258 relocation, unresolved_reloc, warned,
5261 /* keep symbol location for static TLS_IE GOT entry */
5262 relocation_sym = relocation;
5264 /* la $fp, _FP_BASE_ is per-function (region).
5265 Handle it specially. */
5266 switch ((int) r_type)
5268 case R_NDS32_HI20_RELA:
5269 case R_NDS32_LO12S0_RELA:
5270 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5273 if (!bfd_link_pie (info))
5276 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5279 relocation = fpbase_addr;
5282 case R_NDS32_SDA19S0_RELA:
5283 case R_NDS32_SDA15S0_RELA:
5284 case R_NDS32_20_RELA:
5285 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5288 relocation = fpbase_addr;
5294 /* Sanity check the address. */
5295 if (offset > high_address)
5297 r = bfd_reloc_outofrange;
5301 if (r_type >= R_NDS32_RELAX_ENTRY)
5304 switch ((int) r_type)
5306 case R_NDS32_GOTOFF:
5307 /* Relocation is relative to the start of the global offset
5308 table (for ld24 rx, #uimm24), e.g. access at label+addend
5310 ld24 rx. #label@GOTOFF + addend
5312 case R_NDS32_GOTOFF_HI20:
5313 case R_NDS32_GOTOFF_LO12:
5314 case R_NDS32_GOTOFF_LO15:
5315 case R_NDS32_GOTOFF_LO19:
5316 BFD_ASSERT (sgot != NULL);
5318 relocation -= elf_gp (output_bfd);
5321 case R_NDS32_9_PLTREL:
5322 case R_NDS32_25_PLTREL:
5323 /* Relocation is to the entry for this symbol in the
5324 procedure linkage table. */
5326 /* The native assembler will generate a 25_PLTREL reloc
5327 for a local symbol if you assemble a call from one
5328 section to another when using -K pic. */
5332 if (h->forced_local)
5335 /* We didn't make a PLT entry for this symbol. This
5336 happens when statically linking PIC code, or when
5337 using -Bsymbolic. */
5338 if (h->plt.offset == (bfd_vma) - 1)
5341 relocation = (splt->output_section->vma
5342 + splt->output_offset + h->plt.offset);
5345 case R_NDS32_PLT_GOTREL_HI20:
5346 case R_NDS32_PLT_GOTREL_LO12:
5347 case R_NDS32_PLT_GOTREL_LO15:
5348 case R_NDS32_PLT_GOTREL_LO19:
5349 case R_NDS32_PLT_GOTREL_LO20:
5352 || h->plt.offset == (bfd_vma) -1
5353 || (bfd_link_pie (info) && h->def_regular))
5355 /* Maybe we should find better checking to optimize
5356 PIE PLT relocations. */
5357 /* We didn't make a PLT entry for this symbol. This
5358 happens when statically linking PIC code, or when
5359 using -Bsymbolic. */
5361 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
5362 relocation -= elf_gp (output_bfd);
5366 relocation = (splt->output_section->vma
5367 + splt->output_offset + h->plt.offset);
5369 relocation -= elf_gp (output_bfd);
5372 case R_NDS32_PLTREL_HI20:
5373 case R_NDS32_PLTREL_LO12:
5375 /* Relocation is to the entry for this symbol in the
5376 procedure linkage table. */
5378 /* The native assembler will generate a 25_PLTREL reloc
5379 for a local symbol if you assemble a call from one
5380 section to another when using -K pic. */
5384 if (h->forced_local)
5387 if (h->plt.offset == (bfd_vma) - 1)
5388 /* We didn't make a PLT entry for this symbol. This
5389 happens when statically linking PIC code, or when
5390 using -Bsymbolic. */
5396 relocation = (splt->output_section->vma
5397 + splt->output_offset
5398 + h->plt.offset + 4)
5399 - (input_section->output_section->vma
5400 + input_section->output_offset
5405 case R_NDS32_GOTPC20:
5406 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5407 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5408 relocation = elf_gp (output_bfd);
5411 case R_NDS32_GOTPC_HI20:
5412 case R_NDS32_GOTPC_LO12:
5413 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5415 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5416 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5419 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5420 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5421 relocation = elf_gp (output_bfd);
5422 relocation -= (input_section->output_section->vma
5423 + input_section->output_offset + rel->r_offset);
5428 case R_NDS32_GOT_HI20:
5429 case R_NDS32_GOT_LO12:
5430 case R_NDS32_GOT_LO15:
5431 case R_NDS32_GOT_LO19:
5432 /* Relocation is to the entry for this symbol in the global
5434 BFD_ASSERT (sgot != NULL);
5438 /* External symbol */
5441 off = h->got.offset;
5442 BFD_ASSERT (off != (bfd_vma) - 1);
5443 dyn = htab->root.dynamic_sections_created;
5444 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5445 bfd_link_pic (info),
5447 || (bfd_link_pic (info)
5450 || h->forced_local) && h->def_regular))
5452 /* This is actually a static link, or it is a
5453 -Bsymbolic link and the symbol is defined
5454 locally, or the symbol was forced to be local
5455 because of a version file. We must initialize
5456 this entry in the global offset table. Since the
5457 offset must always be a multiple of 4, we use the
5458 least significant bit to record whether we have
5459 initialized it already.
5461 When doing a dynamic link, we create a .rela.got
5462 relocation entry to initialize the value. This
5463 is done in the finish_dynamic_symbol routine. */
5464 if ((off & 1) != 0) /* clear LSB */
5468 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5472 relocation = sgot->output_section->vma + sgot->output_offset + off
5473 - elf_gp (output_bfd);
5480 BFD_ASSERT (local_got_offsets != NULL
5481 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5483 off = local_got_offsets[r_symndx];
5485 /* The offset must always be a multiple of 4. We use
5486 the least significant bit to record whether we have
5487 already processed this entry. */
5488 if ((off & 1) != 0) /* clear LSB */
5492 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5494 if (bfd_link_pic (info))
5497 Elf_Internal_Rela outrel;
5499 /* We need to generate a R_NDS32_RELATIVE reloc
5500 for the dynamic linker. */
5501 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5502 BFD_ASSERT (srelgot != NULL);
5504 outrel.r_offset = (elf_gp (output_bfd)
5505 + sgot->output_offset + off);
5506 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5507 outrel.r_addend = relocation;
5508 loc = srelgot->contents;
5510 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5511 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5512 ++srelgot->reloc_count;
5514 local_got_offsets[r_symndx] |= 1;
5516 relocation = sgot->output_section->vma + sgot->output_offset + off
5517 - elf_gp (output_bfd);
5522 case R_NDS32_16_RELA:
5523 case R_NDS32_20_RELA:
5524 case R_NDS32_5_RELA:
5525 case R_NDS32_32_RELA:
5526 case R_NDS32_9_PCREL_RELA:
5527 case R_NDS32_WORD_9_PCREL_RELA:
5528 case R_NDS32_10_UPCREL_RELA:
5529 case R_NDS32_15_PCREL_RELA:
5530 case R_NDS32_17_PCREL_RELA:
5531 case R_NDS32_25_PCREL_RELA:
5532 case R_NDS32_HI20_RELA:
5533 case R_NDS32_LO12S3_RELA:
5534 case R_NDS32_LO12S2_RELA:
5535 case R_NDS32_LO12S2_DP_RELA:
5536 case R_NDS32_LO12S2_SP_RELA:
5537 case R_NDS32_LO12S1_RELA:
5538 case R_NDS32_LO12S0_RELA:
5539 case R_NDS32_LO12S0_ORI_RELA:
5540 if (bfd_link_pic (info) && r_symndx != 0
5541 && (input_section->flags & SEC_ALLOC) != 0
5542 && (eliminate_gc_relocs == 0
5543 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5544 && ((r_type != R_NDS32_9_PCREL_RELA
5545 && r_type != R_NDS32_WORD_9_PCREL_RELA
5546 && r_type != R_NDS32_10_UPCREL_RELA
5547 && r_type != R_NDS32_15_PCREL_RELA
5548 && r_type != R_NDS32_17_PCREL_RELA
5549 && r_type != R_NDS32_25_PCREL_RELA
5550 && !(r_type == R_NDS32_32_RELA
5551 && strcmp (input_section->name, ".eh_frame") == 0))
5552 || (h != NULL && h->dynindx != -1
5553 && (!info->symbolic || !h->def_regular))))
5555 Elf_Internal_Rela outrel;
5556 bfd_boolean skip, relocate;
5559 /* When generating a shared object, these relocations
5560 are copied into the output file to be resolved at run
5567 name = bfd_elf_string_from_elf_section
5568 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5569 elf_section_data (input_section)->rela.hdr->sh_name);
5573 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5574 && strcmp (bfd_get_section_name (input_bfd,
5578 sreloc = bfd_get_section_by_name (dynobj, name);
5579 BFD_ASSERT (sreloc != NULL);
5585 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5589 if (outrel.r_offset == (bfd_vma) - 1)
5591 else if (outrel.r_offset == (bfd_vma) - 2)
5592 skip = TRUE, relocate = TRUE;
5593 outrel.r_offset += (input_section->output_section->vma
5594 + input_section->output_offset);
5597 memset (&outrel, 0, sizeof outrel);
5598 else if (r_type == R_NDS32_17_PCREL_RELA
5599 || r_type == R_NDS32_15_PCREL_RELA
5600 || r_type == R_NDS32_25_PCREL_RELA)
5602 BFD_ASSERT (h != NULL && h->dynindx != -1);
5603 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5604 outrel.r_addend = rel->r_addend;
5608 /* h->dynindx may be -1 if this symbol was marked to
5611 || ((info->symbolic || h->dynindx == -1)
5613 || (bfd_link_pie (info) && h->def_regular))
5616 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5617 outrel.r_addend = relocation + rel->r_addend;
5621 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5623 BFD_ASSERT (sgot != NULL);
5624 /* If we did not allocate got entry for the symbol,
5625 we can not fill the nonexistent got entry. */
5626 if (h->got.offset != (bfd_vma) -1
5627 && (h->got.offset & 1) == 0)
5629 bfd_put_32 (output_bfd, outrel.r_addend,
5630 sgot->contents + h->got.offset);
5636 if (h->dynindx == -1)
5639 (_("%pB: relocation %s against `%s' can not be used when "
5640 "making a shared object; recompile with -fPIC"),
5641 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5642 bfd_set_error (bfd_error_bad_value);
5646 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5647 outrel.r_addend = rel->r_addend;
5651 loc = sreloc->contents;
5652 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5653 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5654 ++sreloc->reloc_count;
5656 /* If this reloc is against an external symbol, we do
5657 not want to fiddle with the addend. Otherwise, we
5658 need to include the symbol value so that it becomes
5659 an addend for the dynamic reloc. */
5665 case R_NDS32_25_ABS_RELA:
5666 if (bfd_link_pic (info))
5669 (_("%pB: warning: %s unsupported in shared mode"),
5670 input_bfd, "R_NDS32_25_ABS_RELA");
5675 case R_NDS32_9_PCREL:
5676 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5678 sec, relocation, addend);
5682 /* We allow an arbitrary number of HI20 relocs before the
5683 LO12 reloc. This permits gcc to emit the HI and LO relocs
5685 for (lorel = rel + 1;
5687 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5690 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5691 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5692 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5693 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5695 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5696 contents, relocation + addend);
5700 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5701 contents, offset, relocation,
5705 case R_NDS32_GOT17S2_RELA:
5706 case R_NDS32_GOT15S2_RELA:
5707 BFD_ASSERT (sgot != NULL);
5713 off = h->got.offset;
5714 BFD_ASSERT (off != (bfd_vma) - 1);
5716 dyn = htab->root.dynamic_sections_created;
5717 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5718 (dyn, bfd_link_pic (info), h)
5719 || (bfd_link_pic (info)
5725 /* This is actually a static link, or it is a
5726 -Bsymbolic link and the symbol is defined
5727 locally, or the symbol was forced to be local
5728 because of a version file. We must initialize
5729 this entry in the global offset table. Since the
5730 offset must always be a multiple of 4, we use the
5731 least significant bit to record whether we have
5732 initialized it already.
5734 When doing a dynamic link, we create a .rela.got
5735 relocation entry to initialize the value. This
5736 is done in the finish_dynamic_symbol routine. */
5741 bfd_put_32 (output_bfd, relocation,
5742 sgot->contents + off);
5751 BFD_ASSERT (local_got_offsets != NULL
5752 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5754 off = local_got_offsets[r_symndx];
5756 /* The offset must always be a multiple of 4. We use
5757 the least significant bit to record whether we have
5758 already processed this entry. */
5763 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5765 if (bfd_link_pic (info))
5768 Elf_Internal_Rela outrel;
5770 /* We need to generate a R_NDS32_RELATIVE reloc
5771 for the dynamic linker. */
5772 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5773 BFD_ASSERT (srelgot != NULL);
5775 outrel.r_offset = (elf_gp (output_bfd)
5776 + sgot->output_offset + off);
5777 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5778 outrel.r_addend = relocation;
5779 loc = srelgot->contents;
5781 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5782 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5783 ++srelgot->reloc_count;
5785 local_got_offsets[r_symndx] |= 1;
5788 relocation = sgot->output_section->vma + sgot->output_offset + off
5789 - elf_gp (output_bfd);
5791 if (relocation & align)
5793 /* Incorrect alignment. */
5795 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5797 r = bfd_reloc_dangerous;
5802 case R_NDS32_SDA16S3_RELA:
5803 case R_NDS32_SDA15S3_RELA:
5804 case R_NDS32_SDA15S3:
5808 case R_NDS32_SDA17S2_RELA:
5809 case R_NDS32_SDA15S2_RELA:
5810 case R_NDS32_SDA12S2_SP_RELA:
5811 case R_NDS32_SDA12S2_DP_RELA:
5812 case R_NDS32_SDA15S2:
5813 case R_NDS32_SDA_FP7U2_RELA:
5817 case R_NDS32_SDA18S1_RELA:
5818 case R_NDS32_SDA15S1_RELA:
5819 case R_NDS32_SDA15S1:
5823 case R_NDS32_SDA19S0_RELA:
5824 case R_NDS32_SDA15S0_RELA:
5825 case R_NDS32_SDA15S0:
5828 BFD_ASSERT (sec != NULL);
5830 /* If the symbol is in the abs section, the out_bfd will be null.
5831 This happens when the relocation has a symbol@GOTOFF. */
5832 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5833 if (r != bfd_reloc_ok)
5836 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5841 /* At this point `relocation' contains the object's
5843 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5845 relocation -= fpbase_addr;
5849 /* Now it contains the offset from _SDA_BASE_. */
5851 /* Make sure alignment is correct. */
5853 if (relocation & align)
5855 /* Incorrect alignment. */
5857 /* xgettext:c-format */
5858 (_("%pB(%pA): warning: unaligned small data access"
5860 input_bfd, input_section, r_type);
5866 case R_NDS32_17IFC_PCREL_RELA:
5867 case R_NDS32_10IFCU_PCREL_RELA:
5871 case R_NDS32_TLS_LE_HI20:
5872 case R_NDS32_TLS_LE_LO12:
5873 case R_NDS32_TLS_LE_20:
5874 case R_NDS32_TLS_LE_15S0:
5875 case R_NDS32_TLS_LE_15S1:
5876 case R_NDS32_TLS_LE_15S2:
5877 /* We do not have garbage collection for got entries.
5878 Therefore, IE to LE may have one empty entry, and DESC to
5880 if (elf_hash_table (info)->tls_sec != NULL)
5881 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5884 case R_NDS32_TLS_IE_HI20:
5885 case R_NDS32_TLS_IE_LO12S2:
5886 case R_NDS32_TLS_DESC_HI20:
5887 case R_NDS32_TLS_DESC_LO12:
5888 case R_NDS32_TLS_IE_LO12:
5889 case R_NDS32_TLS_IEGP_HI20:
5890 case R_NDS32_TLS_IEGP_LO12:
5891 case R_NDS32_TLS_IEGP_LO12S2:
5893 /* Relocation is to the entry for this symbol in the global
5895 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5897 Elf_Internal_Rela outrel;
5901 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5903 BFD_ASSERT (sgot != NULL);
5908 off = h->got.offset;
5909 BFD_ASSERT (off != (bfd_vma) -1);
5910 dyn = htab->root.dynamic_sections_created;
5911 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5912 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5913 && (!bfd_link_pic (info)
5914 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5919 BFD_ASSERT (local_got_offsets != NULL
5920 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5921 off = local_got_offsets[r_symndx];
5922 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5925 relocation = sgot->output_section->vma + sgot->output_offset + off;
5927 if (1 < ones32 (tls_type))
5929 eff_tls_type = 1 << (fls (tls_type) - 1);
5930 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5932 /* TLS model X -> LE is not implement yet!
5934 if (eff_tls_type == GOT_TLS_LE)
5936 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5940 /* The offset must always be a multiple of 4. We use
5941 the least significant bit to record whether we have
5942 already processed this entry. */
5943 bfd_boolean need_relocs = FALSE;
5944 srelgot = ehtab->srelgot;
5945 if ((bfd_link_pic (info) || indx != 0)
5946 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5947 || h->root.type != bfd_link_hash_undefweak))
5950 BFD_ASSERT (srelgot != NULL);
5958 if (eff_tls_type & GOT_TLS_DESC)
5960 relocation -= elf_gp (output_bfd);
5961 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5963 /* TLS model shall be converted. */
5967 else if (eff_tls_type & GOT_TLS_IEGP)
5969 relocation -= elf_gp (output_bfd);
5974 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5976 /* TLS model workaround shall be applied. */
5979 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5981 if (eff_tls_type & GOT_TLS_IEGP)
5982 relocation -= elf_gp(output_bfd);
5987 outrel.r_addend = gottpoff (info, relocation_sym);
5989 outrel.r_addend = 0;
5990 outrel.r_offset = (sgot->output_section->vma
5991 + sgot->output_offset + off);
5992 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5994 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5999 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6000 sgot->contents + off);
6003 else if (eff_tls_type & GOT_TLS_DESC)
6005 relocation -= elf_gp (output_bfd);
6009 outrel.r_addend = gottpoff (info, relocation_sym);
6011 outrel.r_addend = 0;
6012 outrel.r_offset = (sgot->output_section->vma
6013 + sgot->output_offset + off);
6014 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
6016 if (htab->tls_desc_trampoline)
6019 srelplt = ehtab->srelplt;
6020 loc = srelplt->contents;
6021 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
6022 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
6023 <= srelplt->contents + srelplt->size);
6025 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6029 loc = srelgot->contents;
6030 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
6031 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6032 ++srelgot->reloc_count;
6038 bfd_put_32 (output_bfd, 0xdeadbeef,
6039 sgot->contents + off);
6040 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6041 sgot->contents + off + 4);
6042 patch_tls_desc_to_ie (contents, rel, input_bfd);
6048 /* TLS model workaround shall be applied. */
6055 local_got_offsets[r_symndx] |= 1;
6059 /* DON'T fall through. */
6062 /* OLD_NDS32_RELOC. */
6064 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6065 contents, offset, relocation, addend);
6069 switch ((int) r_type)
6071 case R_NDS32_20_RELA:
6072 case R_NDS32_5_RELA:
6073 case R_NDS32_9_PCREL_RELA:
6074 case R_NDS32_WORD_9_PCREL_RELA:
6075 case R_NDS32_10_UPCREL_RELA:
6076 case R_NDS32_15_PCREL_RELA:
6077 case R_NDS32_17_PCREL_RELA:
6078 case R_NDS32_25_PCREL_RELA:
6079 case R_NDS32_25_ABS_RELA:
6080 case R_NDS32_HI20_RELA:
6081 case R_NDS32_LO12S3_RELA:
6082 case R_NDS32_LO12S2_RELA:
6083 case R_NDS32_LO12S2_DP_RELA:
6084 case R_NDS32_LO12S2_SP_RELA:
6085 case R_NDS32_LO12S1_RELA:
6086 case R_NDS32_LO12S0_RELA:
6087 case R_NDS32_LO12S0_ORI_RELA:
6088 case R_NDS32_SDA16S3_RELA:
6089 case R_NDS32_SDA17S2_RELA:
6090 case R_NDS32_SDA18S1_RELA:
6091 case R_NDS32_SDA19S0_RELA:
6092 case R_NDS32_SDA15S3_RELA:
6093 case R_NDS32_SDA15S2_RELA:
6094 case R_NDS32_SDA12S2_DP_RELA:
6095 case R_NDS32_SDA12S2_SP_RELA:
6096 case R_NDS32_SDA15S1_RELA:
6097 case R_NDS32_SDA15S0_RELA:
6098 case R_NDS32_SDA_FP7U2_RELA:
6099 case R_NDS32_9_PLTREL:
6100 case R_NDS32_25_PLTREL:
6102 case R_NDS32_GOT_HI20:
6103 case R_NDS32_GOT_LO12:
6104 case R_NDS32_GOT_LO15:
6105 case R_NDS32_GOT_LO19:
6106 case R_NDS32_GOT15S2_RELA:
6107 case R_NDS32_GOT17S2_RELA:
6108 case R_NDS32_GOTPC20:
6109 case R_NDS32_GOTPC_HI20:
6110 case R_NDS32_GOTPC_LO12:
6111 case R_NDS32_GOTOFF:
6112 case R_NDS32_GOTOFF_HI20:
6113 case R_NDS32_GOTOFF_LO12:
6114 case R_NDS32_GOTOFF_LO15:
6115 case R_NDS32_GOTOFF_LO19:
6116 case R_NDS32_PLTREL_HI20:
6117 case R_NDS32_PLTREL_LO12:
6118 case R_NDS32_PLT_GOTREL_HI20:
6119 case R_NDS32_PLT_GOTREL_LO12:
6120 case R_NDS32_PLT_GOTREL_LO15:
6121 case R_NDS32_PLT_GOTREL_LO19:
6122 case R_NDS32_PLT_GOTREL_LO20:
6123 case R_NDS32_17IFC_PCREL_RELA:
6124 case R_NDS32_10IFCU_PCREL_RELA:
6125 case R_NDS32_TLS_LE_HI20:
6126 case R_NDS32_TLS_LE_LO12:
6127 case R_NDS32_TLS_IE_HI20:
6128 case R_NDS32_TLS_IE_LO12S2:
6129 case R_NDS32_TLS_LE_20:
6130 case R_NDS32_TLS_LE_15S0:
6131 case R_NDS32_TLS_LE_15S1:
6132 case R_NDS32_TLS_LE_15S2:
6133 case R_NDS32_TLS_DESC_HI20:
6134 case R_NDS32_TLS_DESC_LO12:
6135 case R_NDS32_TLS_IE_LO12:
6136 case R_NDS32_TLS_IEGP_HI20:
6137 case R_NDS32_TLS_IEGP_LO12:
6138 case R_NDS32_TLS_IEGP_LO12S2:
6139 /* Instruction related relocs must handle endian properly. */
6140 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
6141 r = nds32_elf_final_link_relocate (howto, input_bfd,
6142 input_section, contents,
6143 rel->r_offset, relocation,
6148 /* All other relocs can use default handler. */
6149 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6150 contents, rel->r_offset,
6151 relocation, rel->r_addend);
6157 if (r != bfd_reloc_ok)
6159 /* FIXME: This should be generic enough to go in a utility. */
6163 name = h->root.root.string;
6166 name = bfd_elf_string_from_elf_section
6167 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6168 if (name == NULL || *name == '\0')
6169 name = bfd_section_name (input_bfd, sec);
6177 case bfd_reloc_overflow:
6178 (*info->callbacks->reloc_overflow)
6179 (info, (h ? &h->root : NULL), name, howto->name,
6180 (bfd_vma) 0, input_bfd, input_section, offset);
6183 case bfd_reloc_undefined:
6184 (*info->callbacks->undefined_symbol)
6185 (info, name, input_bfd, input_section, offset, TRUE);
6188 case bfd_reloc_outofrange:
6189 errmsg = _("internal error: out of range error");
6192 case bfd_reloc_notsupported:
6193 errmsg = _("internal error: unsupported relocation error");
6196 case bfd_reloc_dangerous:
6197 errmsg = _("internal error: dangerous error");
6201 errmsg = _("internal error: unknown error");
6205 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6206 input_section, offset);
6212 /* Resotre header size to avoid overflow load. */
6213 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6214 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6219 /* Finish up dynamic symbol handling. We set the contents of various
6220 dynamic sections here. */
6223 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6224 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6226 struct elf_link_hash_table *ehtab;
6227 struct elf_nds32_link_hash_entry *hent;
6230 ehtab = elf_hash_table (info);
6231 hent = (struct elf_nds32_link_hash_entry *) h;
6233 if (h->plt.offset != (bfd_vma) - 1)
6241 bfd_vma local_plt_offset;
6242 Elf_Internal_Rela rela;
6244 /* This symbol has an entry in the procedure linkage table. Set
6247 BFD_ASSERT (h->dynindx != -1);
6250 sgot = ehtab->sgotplt;
6251 srela = ehtab->srelplt;
6252 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6254 /* Get the index in the procedure linkage table which
6255 corresponds to this symbol. This is the index of this symbol
6256 in all the symbols for which we are making plt entries. The
6257 first entry in the procedure linkage table is reserved. */
6258 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6260 /* Get the offset into the .got table of the entry that
6261 corresponds to this function. Each .got entry is 4 bytes.
6262 The first three are reserved. */
6263 got_offset = (plt_index + 3) * 4;
6265 /* Fill in the entry in the procedure linkage table. */
6266 if (!bfd_link_pic (info))
6270 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6271 + sgot->output_offset + got_offset) >> 12)
6273 bfd_putb32 (insn, splt->contents + h->plt.offset);
6275 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6276 + sgot->output_offset + got_offset) & 0x0fff)
6278 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6280 insn = PLT_ENTRY_WORD2;
6281 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6283 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6284 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6286 insn = PLT_ENTRY_WORD4
6287 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6288 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6289 local_plt_offset = 12;
6293 /* sda_base must be set at this time. */
6297 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6298 - elf_gp (output_bfd);
6299 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6300 bfd_putb32 (insn, splt->contents + h->plt.offset);
6302 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6303 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6305 insn = PLT_PIC_ENTRY_WORD2;
6306 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6308 insn = PLT_PIC_ENTRY_WORD3;
6309 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6311 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6312 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6314 insn = PLT_PIC_ENTRY_WORD5
6315 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6316 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6318 local_plt_offset = 16;
6321 /* Fill in the entry in the global offset table,
6322 so it will fall through to the next instruction for the first time. */
6323 bfd_put_32 (output_bfd,
6324 (splt->output_section->vma + splt->output_offset
6325 + h->plt.offset + local_plt_offset),
6326 sgot->contents + got_offset);
6328 /* Fill in the entry in the .rela.plt section. */
6329 rela.r_offset = (sgot->output_section->vma
6330 + sgot->output_offset + got_offset);
6331 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6333 loc = srela->contents;
6334 loc += plt_index * sizeof (Elf32_External_Rela);
6335 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6337 if (!h->def_regular)
6339 /* Mark the symbol as undefined, rather than as defined in
6340 the .plt section. Leave the value alone. */
6341 sym->st_shndx = SHN_UNDEF;
6342 if (!h->ref_regular_nonweak)
6347 if (h->got.offset != (bfd_vma) - 1
6348 && hent->tls_type == GOT_NORMAL)
6352 Elf_Internal_Rela rela;
6354 /* This symbol has an entry in the global offset table.
6358 srelagot = ehtab->srelgot;
6359 BFD_ASSERT (sgot != NULL && srelagot != NULL);
6361 rela.r_offset = (sgot->output_section->vma
6362 + sgot->output_offset + (h->got.offset & ~1));
6364 /* If this is a -Bsymbolic link, and the symbol is defined
6365 locally, we just want to emit a RELATIVE reloc. Likewise if
6366 the symbol was forced to be local because of a version file.
6367 The entry in the global offset table will already have been
6368 initialized in the relocate_section function. */
6369 if ((bfd_link_pic (info)
6370 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6372 || (bfd_link_pie (info) && h->def_regular))
6374 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6375 rela.r_addend = (h->root.u.def.value
6376 + h->root.u.def.section->output_section->vma
6377 + h->root.u.def.section->output_offset);
6379 if ((h->got.offset & 1) == 0)
6381 bfd_put_32 (output_bfd, rela.r_addend,
6382 sgot->contents + h->got.offset);
6387 BFD_ASSERT ((h->got.offset & 1) == 0);
6388 bfd_put_32 (output_bfd, (bfd_vma) 0,
6389 sgot->contents + h->got.offset);
6390 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6394 loc = srelagot->contents;
6395 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6396 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6397 ++srelagot->reloc_count;
6398 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6404 Elf_Internal_Rela rela;
6406 /* This symbols needs a copy reloc. Set it up. */
6408 BFD_ASSERT (h->dynindx != -1
6409 && (h->root.type == bfd_link_hash_defined
6410 || h->root.type == bfd_link_hash_defweak));
6412 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6413 BFD_ASSERT (s != NULL);
6415 rela.r_offset = (h->root.u.def.value
6416 + h->root.u.def.section->output_section->vma
6417 + h->root.u.def.section->output_offset);
6418 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6421 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6422 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6426 /* Mark some specially defined symbols as absolute. */
6427 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6428 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6429 sym->st_shndx = SHN_ABS;
6435 /* Finish up the dynamic sections. */
6438 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6443 struct elf_link_hash_table *ehtab;
6444 struct elf_nds32_link_hash_table *htab;
6446 ehtab = elf_hash_table (info);
6447 htab = nds32_elf_hash_table (info);
6451 dynobj = elf_hash_table (info)->dynobj;
6453 sgotplt = ehtab->sgotplt;
6454 /* A broken linker script might have discarded the dynamic sections.
6455 Catch this here so that we do not seg-fault later on. */
6456 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6458 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6460 if (elf_hash_table (info)->dynamic_sections_created)
6463 Elf32_External_Dyn *dyncon, *dynconend;
6465 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6467 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6468 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6470 for (; dyncon < dynconend; dyncon++)
6472 Elf_Internal_Dyn dyn;
6475 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6483 /* name = ".got"; */
6484 s = ehtab->sgot->output_section;
6487 s = ehtab->srelplt->output_section;
6489 BFD_ASSERT (s != NULL);
6490 dyn.d_un.d_ptr = s->vma;
6491 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6495 s = ehtab->srelplt->output_section;
6496 BFD_ASSERT (s != NULL);
6497 dyn.d_un.d_val = s->size;
6498 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6502 /* My reading of the SVR4 ABI indicates that the
6503 procedure linkage table relocs (DT_JMPREL) should be
6504 included in the overall relocs (DT_RELA). This is
6505 what Solaris does. However, UnixWare can not handle
6506 that case. Therefore, we override the DT_RELASZ entry
6507 here to make it not include the JMPREL relocs. Since
6508 the linker script arranges for .rela.plt to follow all
6509 other relocation sections, we don't have to worry
6510 about changing the DT_RELA entry. */
6511 if (ehtab->srelplt != NULL)
6513 s = ehtab->srelplt->output_section;
6514 dyn.d_un.d_val -= s->size;
6516 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6519 case DT_TLSDESC_PLT:
6520 s = htab->root.splt;
6521 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6522 + htab->dt_tlsdesc_plt);
6523 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6526 case DT_TLSDESC_GOT:
6527 s = htab->root.sgot;
6528 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6529 + htab->dt_tlsdesc_got);
6530 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6535 /* Fill in the first entry in the procedure linkage table. */
6537 if (splt && splt->size > 0)
6539 if (bfd_link_pic (info))
6544 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6545 - elf_gp (output_bfd);
6546 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6547 bfd_putb32 (insn, splt->contents);
6549 /* here has a typo? */
6550 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6551 bfd_putb32 (insn, splt->contents + 4);
6553 insn = PLT0_PIC_ENTRY_WORD2;
6554 bfd_putb32 (insn, splt->contents + 8);
6556 insn = PLT0_PIC_ENTRY_WORD3;
6557 bfd_putb32 (insn, splt->contents + 12);
6559 insn = PLT0_PIC_ENTRY_WORD4;
6560 bfd_putb32 (insn, splt->contents + 16);
6562 insn = PLT0_PIC_ENTRY_WORD5;
6563 bfd_putb32 (insn, splt->contents + 20);
6570 /* addr = .got + 4 */
6571 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6572 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6573 bfd_putb32 (insn, splt->contents);
6575 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6576 bfd_putb32 (insn, splt->contents + 4);
6578 insn = PLT0_ENTRY_WORD2;
6579 bfd_putb32 (insn, splt->contents + 8);
6581 insn = PLT0_ENTRY_WORD3;
6582 bfd_putb32 (insn, splt->contents + 12);
6584 insn = PLT0_ENTRY_WORD4;
6585 bfd_putb32 (insn, splt->contents + 16);
6588 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6592 if (htab->dt_tlsdesc_plt)
6594 /* Calculate addresses. */
6595 asection *sgot = sgot = ehtab->sgot;
6596 bfd_vma pltgot = sgotplt->output_section->vma
6597 + sgotplt->output_offset;
6598 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6599 + htab->dt_tlsdesc_got;
6601 /* Get GP offset. */
6602 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6603 tlsdesc_got -= elf_gp (output_bfd);
6605 /* Do relocation. */
6606 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6607 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6608 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6609 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6612 nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6613 dl_tlsdesc_lazy_trampoline,
6614 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6618 /* Fill in the first three entries in the global offset table. */
6619 if (sgotplt && sgotplt->size > 0)
6622 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6624 bfd_put_32 (output_bfd,
6625 sdyn->output_section->vma + sdyn->output_offset,
6627 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6628 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6630 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6637 /* Set the right machine number. */
6640 nds32_elf_object_p (bfd *abfd)
6642 static unsigned int cur_arch = 0;
6644 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6646 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6647 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6654 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6657 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6659 case E_NDS_ARCH_STAR_V2_0:
6660 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6662 case E_NDS_ARCH_STAR_V3_0:
6663 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6665 case E_NDS_ARCH_STAR_V3_M:
6666 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6673 /* Store the machine number in the flags field. */
6676 nds32_elf_final_write_processing (bfd *abfd, bfd_boolean linker)
6679 static unsigned int cur_mach = 0;
6681 if (bfd_mach_n1 != bfd_get_mach (abfd))
6683 cur_mach = bfd_get_mach (abfd);
6689 /* Only happen when object is empty, since the case is abandon. */
6691 val |= E_NDS_ABI_AABI;
6692 val |= E_NDS32_ELF_VER_1_4;
6697 case bfd_mach_n1h_v2:
6698 val = E_NDS_ARCH_STAR_V2_0;
6700 case bfd_mach_n1h_v3:
6701 val = E_NDS_ARCH_STAR_V3_0;
6703 case bfd_mach_n1h_v3m:
6704 val = E_NDS_ARCH_STAR_V3_M;
6711 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6712 elf_elfheader (abfd)->e_flags |= val;
6713 _bfd_elf_final_write_processing (abfd, linker);
6716 /* Function to keep NDS32 specific file flags. */
6719 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6721 BFD_ASSERT (!elf_flags_init (abfd)
6722 || elf_elfheader (abfd)->e_flags == flags);
6724 elf_elfheader (abfd)->e_flags = flags;
6725 elf_flags_init (abfd) = TRUE;
6730 convert_e_flags (unsigned int e_flags, unsigned int arch)
6732 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6734 /* From 0.9 to 1.0. */
6735 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6737 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6738 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6739 if (arch == E_NDS_ARCH_STAR_V1_0)
6746 /* From 1.0 to 2.0. */
6747 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6749 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6750 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6752 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6753 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6758 nds32_check_vec_size (bfd *ibfd)
6760 static unsigned int nds32_vec_size = 0;
6762 asection *sec_t = NULL;
6763 bfd_byte *contents = NULL;
6765 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6767 if (sec_t && sec_t->size >= 4)
6769 /* Get vec_size in file. */
6770 unsigned int flag_t;
6772 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
6773 flag_t = bfd_get_32 (ibfd, contents);
6775 /* The value could only be 4 or 16. */
6777 if (!nds32_vec_size)
6778 /* Set if not set yet. */
6779 nds32_vec_size = (flag_t & 0x3);
6780 else if (nds32_vec_size != (flag_t & 0x3))
6783 /* xgettext:c-format */
6784 (_("%pB: ISR vector size mismatch"
6785 " with previous modules, previous %u-byte, current %u-byte"),
6787 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6788 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6792 /* Only keep the first vec_size section. */
6793 sec_t->flags |= SEC_EXCLUDE;
6799 /* Merge backend specific data from an object file to the output
6800 object file when linking. */
6803 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6805 bfd *obfd = info->output_bfd;
6808 flagword out_16regs;
6810 flagword out_no_mac;
6812 flagword out_version;
6813 flagword in_version;
6814 flagword out_fpu_config;
6815 flagword in_fpu_config;
6817 /* TODO: Revise to use object-attributes instead. */
6818 if (!nds32_check_vec_size (ibfd))
6821 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6822 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6825 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6828 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6830 bfd_set_error (bfd_error_bad_value);
6834 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6835 treat as generic one without checking and merging. */
6836 if (elf_elfheader (ibfd)->e_flags)
6838 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6839 if (in_version == E_NDS32_ELF_VER_1_2)
6842 (_("%pB: warning: older version of object file encountered, "
6843 "please recompile with current tool chain"), ibfd);
6846 /* We may need to merge V1 and V2 arch object files to V2. */
6847 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6848 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6850 /* Need to convert version. */
6851 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6852 == E_NDS_ARCH_STAR_RESERVED)
6854 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6856 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6857 == E_NDS_ARCH_STAR_V3_M
6858 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6859 == E_NDS_ARCH_STAR_V3_0)
6861 elf_elfheader (ibfd)->e_flags =
6862 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6863 | E_NDS_ARCH_STAR_V3_0;
6865 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6866 == E_NDS_ARCH_STAR_V0_9
6867 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6868 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6870 elf_elfheader (obfd)->e_flags =
6871 convert_e_flags (elf_elfheader (obfd)->e_flags,
6872 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6876 elf_elfheader (ibfd)->e_flags =
6877 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6878 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6882 /* Extract some flags. */
6883 in_flags = elf_elfheader (ibfd)->e_flags
6884 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6885 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6887 /* The following flags need special treatment. */
6888 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6889 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6890 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6892 /* Extract some flags. */
6893 out_flags = elf_elfheader (obfd)->e_flags
6894 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6895 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6897 /* The following flags need special treatment. */
6898 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6899 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6900 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6901 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6902 if (!elf_flags_init (obfd))
6904 /* If the input is the default architecture then do not
6905 bother setting the flags for the output architecture,
6906 instead allow future merges to do this. If no future
6907 merges ever set these flags then they will retain their
6908 unitialised values, which surprise surprise, correspond
6909 to the default values. */
6910 if (bfd_get_arch_info (ibfd)->the_default)
6913 elf_flags_init (obfd) = TRUE;
6914 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6916 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6917 && bfd_get_arch_info (obfd)->the_default)
6919 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6920 bfd_get_mach (ibfd));
6926 /* Check flag compatibility. */
6927 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6930 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6931 bfd_set_error (bfd_error_bad_value);
6935 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6937 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6940 (_("%pB: error: instruction set mismatch with previous modules"),
6943 bfd_set_error (bfd_error_bad_value);
6948 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6949 and perf ext1 and DIV are mergerd to perf ext1. */
6950 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6952 elf_elfheader (obfd)->e_flags =
6953 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6954 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6955 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6956 ? E_NDS32_HAS_EXT_INST : 0)
6957 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6958 ? E_NDS32_HAS_EXT_INST : 0)
6959 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6960 | ((in_version > out_version) ? out_version : in_version);
6964 if (in_version != out_version)
6966 /* xgettext:c-format */
6967 (_("%pB: warning: incompatible elf-versions %s and %s"),
6968 ibfd, nds32_elfver_strtab[out_version],
6969 nds32_elfver_strtab[in_version]);
6971 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6972 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6973 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6974 | (in_version > out_version ? out_version : in_version);
6981 /* Display the flags field. */
6984 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6986 FILE *file = (FILE *) ptr;
6988 BFD_ASSERT (abfd != NULL && ptr != NULL);
6990 _bfd_elf_print_private_bfd_data (abfd, ptr);
6992 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6994 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6998 fprintf (file, _(": n1 instructions"));
7001 fprintf (file, _(": n1h instructions"));
7011 nds32_elf_action_discarded (asection *sec)
7015 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7018 return _bfd_elf_default_action_discarded (sec);
7022 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7023 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7024 Elf_Internal_Sym *sym)
7027 switch (ELF32_R_TYPE (rel->r_info))
7029 case R_NDS32_GNU_VTINHERIT:
7030 case R_NDS32_GNU_VTENTRY:
7031 case R_NDS32_RELA_GNU_VTINHERIT:
7032 case R_NDS32_RELA_GNU_VTENTRY:
7036 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7039 static enum elf_nds32_tls_type
7040 get_tls_type (enum elf_nds32_reloc_type r_type,
7041 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7043 enum elf_nds32_tls_type tls_type;
7047 case R_NDS32_TLS_LE_HI20:
7048 case R_NDS32_TLS_LE_LO12:
7049 tls_type = GOT_TLS_LE;
7051 case R_NDS32_TLS_IE_HI20:
7052 case R_NDS32_TLS_IE_LO12S2:
7053 case R_NDS32_TLS_IE_LO12:
7054 tls_type = GOT_TLS_IE;
7056 case R_NDS32_TLS_IEGP_HI20:
7057 case R_NDS32_TLS_IEGP_LO12:
7058 case R_NDS32_TLS_IEGP_LO12S2:
7059 tls_type = GOT_TLS_IEGP;
7061 case R_NDS32_TLS_DESC_HI20:
7062 case R_NDS32_TLS_DESC_LO12:
7063 case R_NDS32_TLS_DESC_ADD:
7064 case R_NDS32_TLS_DESC_FUNC:
7065 case R_NDS32_TLS_DESC_CALL:
7066 tls_type = GOT_TLS_DESC;
7069 tls_type = GOT_NORMAL;
7076 /* Ensure that we have allocated bookkeeping structures for ABFD's local
7080 elf32_nds32_allocate_local_sym_info (bfd *abfd)
7082 if (elf_local_got_refcounts (abfd) == NULL)
7084 bfd_size_type num_syms;
7088 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7089 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7090 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7091 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7092 + sizeof (bfd_vma) + sizeof (int)
7093 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7094 data = bfd_zalloc (abfd, size);
7098 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7099 data += num_syms * sizeof (bfd_signed_vma);
7101 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7102 data += num_syms * sizeof (char);
7104 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7105 data += num_syms * sizeof (bfd_vma);
7107 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7108 data += num_syms * sizeof (int);
7114 /* Look through the relocs for a section during the first phase.
7115 Since we don't do .gots or .plts, we just need to consider the
7116 virtual table relocs for gc. */
7119 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7120 asection *sec, const Elf_Internal_Rela *relocs)
7122 Elf_Internal_Shdr *symtab_hdr;
7123 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7124 const Elf_Internal_Rela *rel;
7125 const Elf_Internal_Rela *rel_end;
7126 struct elf_link_hash_table *ehtab;
7127 struct elf_nds32_link_hash_table *htab;
7129 asection *sreloc = NULL;
7131 /* No need for relocation if relocatable already. */
7132 if (bfd_link_relocatable (info))
7134 elf32_nds32_check_relax_group (abfd, sec);
7138 /* Don't do anything special with non-loaded, non-alloced sections.
7139 In particular, any relocs in such sections should not affect GOT
7140 and PLT reference counting (ie. we don't allow them to create GOT
7141 or PLT entries), there's no possibility or desire to optimize TLS
7142 relocs, and there's not much point in propagating relocs to shared
7143 libs that the dynamic linker won't relocate. */
7144 if ((sec->flags & SEC_ALLOC) == 0)
7147 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7148 sym_hashes = elf_sym_hashes (abfd);
7150 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7151 if (!elf_bad_symtab (abfd))
7152 sym_hashes_end -= symtab_hdr->sh_info;
7154 ehtab = elf_hash_table (info);
7155 htab = nds32_elf_hash_table (info);
7156 dynobj = htab->root.dynobj;
7158 rel_end = relocs + sec->reloc_count;
7159 for (rel = relocs; rel < rel_end; rel++)
7161 enum elf_nds32_reloc_type r_type;
7162 struct elf_link_hash_entry *h;
7163 unsigned long r_symndx;
7164 enum elf_nds32_tls_type tls_type, old_tls_type;
7166 r_symndx = ELF32_R_SYM (rel->r_info);
7167 r_type = ELF32_R_TYPE (rel->r_info);
7168 if (r_symndx < symtab_hdr->sh_info)
7172 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7173 while (h->root.type == bfd_link_hash_indirect
7174 || h->root.type == bfd_link_hash_warning)
7175 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7178 /* Create .got section if necessary.
7179 Some relocs require a global offset table. We create
7180 got section here, since these relocation need a got section
7181 and if it is not created yet. */
7182 if (ehtab->sgot == NULL)
7186 case R_NDS32_GOT_HI20:
7187 case R_NDS32_GOT_LO12:
7188 case R_NDS32_GOT_LO15:
7189 case R_NDS32_GOT_LO19:
7190 case R_NDS32_GOT17S2_RELA:
7191 case R_NDS32_GOT15S2_RELA:
7192 case R_NDS32_GOTOFF:
7193 case R_NDS32_GOTOFF_HI20:
7194 case R_NDS32_GOTOFF_LO12:
7195 case R_NDS32_GOTOFF_LO15:
7196 case R_NDS32_GOTOFF_LO19:
7197 case R_NDS32_GOTPC20:
7198 case R_NDS32_GOTPC_HI20:
7199 case R_NDS32_GOTPC_LO12:
7201 case R_NDS32_TLS_IE_HI20:
7202 case R_NDS32_TLS_IE_LO12:
7203 case R_NDS32_TLS_IE_LO12S2:
7204 case R_NDS32_TLS_IEGP_HI20:
7205 case R_NDS32_TLS_IEGP_LO12:
7206 case R_NDS32_TLS_IEGP_LO12S2:
7207 case R_NDS32_TLS_DESC_HI20:
7208 case R_NDS32_TLS_DESC_LO12:
7210 htab->root.dynobj = dynobj = abfd;
7211 if (!create_got_section (dynobj, info))
7220 /* Check relocation type. */
7221 switch ((int) r_type)
7223 case R_NDS32_GOT_HI20:
7224 case R_NDS32_GOT_LO12:
7225 case R_NDS32_GOT_LO15:
7226 case R_NDS32_GOT_LO19:
7228 case R_NDS32_TLS_LE_HI20:
7229 case R_NDS32_TLS_LE_LO12:
7230 case R_NDS32_TLS_IE_HI20:
7231 case R_NDS32_TLS_IE_LO12:
7232 case R_NDS32_TLS_IE_LO12S2:
7233 case R_NDS32_TLS_IEGP_HI20:
7234 case R_NDS32_TLS_IEGP_LO12:
7235 case R_NDS32_TLS_IEGP_LO12S2:
7236 case R_NDS32_TLS_DESC_HI20:
7237 case R_NDS32_TLS_DESC_LO12:
7238 tls_type = get_tls_type (r_type, h);
7241 if (tls_type != GOT_TLS_LE)
7242 h->got.refcount += 1;
7243 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7247 /* This is a global offset table entry for a local symbol. */
7248 if (!elf32_nds32_allocate_local_sym_info (abfd))
7251 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7252 if (tls_type != GOT_TLS_LE)
7253 elf_local_got_refcounts (abfd)[r_symndx] += 1;
7254 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7257 /* We would already have issued an error message if there
7258 is a TLS/non-TLS mismatch, based on the symbol
7259 type. So just combine any TLS types needed. */
7260 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7261 && tls_type != GOT_NORMAL)
7262 tls_type |= old_tls_type;
7264 /* DESC to IE/IEGP if link to executable. */
7265 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7266 && (bfd_link_executable (info)))
7267 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7269 if (old_tls_type != tls_type)
7272 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7274 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7277 case R_NDS32_9_PLTREL:
7278 case R_NDS32_25_PLTREL:
7279 case R_NDS32_PLTREL_HI20:
7280 case R_NDS32_PLTREL_LO12:
7281 case R_NDS32_PLT_GOTREL_HI20:
7282 case R_NDS32_PLT_GOTREL_LO12:
7283 case R_NDS32_PLT_GOTREL_LO15:
7284 case R_NDS32_PLT_GOTREL_LO19:
7285 case R_NDS32_PLT_GOTREL_LO20:
7287 /* This symbol requires a procedure linkage table entry. We
7288 actually build the entry in adjust_dynamic_symbol,
7289 because this might be a case of linking PIC code without
7290 linking in any dynamic objects, in which case we don't
7291 need to generate a procedure linkage table after all. */
7293 /* If this is a local symbol, we resolve it directly without
7294 creating a procedure linkage table entry. */
7299 || (bfd_link_pie (info) && h->def_regular))
7302 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7304 h->plt.refcount += 1;
7307 case R_NDS32_16_RELA:
7308 case R_NDS32_20_RELA:
7309 case R_NDS32_5_RELA:
7310 case R_NDS32_32_RELA:
7311 case R_NDS32_HI20_RELA:
7312 case R_NDS32_LO12S3_RELA:
7313 case R_NDS32_LO12S2_RELA:
7314 case R_NDS32_LO12S2_DP_RELA:
7315 case R_NDS32_LO12S2_SP_RELA:
7316 case R_NDS32_LO12S1_RELA:
7317 case R_NDS32_LO12S0_RELA:
7318 case R_NDS32_LO12S0_ORI_RELA:
7319 case R_NDS32_SDA16S3_RELA:
7320 case R_NDS32_SDA17S2_RELA:
7321 case R_NDS32_SDA18S1_RELA:
7322 case R_NDS32_SDA19S0_RELA:
7323 case R_NDS32_SDA15S3_RELA:
7324 case R_NDS32_SDA15S2_RELA:
7325 case R_NDS32_SDA12S2_DP_RELA:
7326 case R_NDS32_SDA12S2_SP_RELA:
7327 case R_NDS32_SDA15S1_RELA:
7328 case R_NDS32_SDA15S0_RELA:
7329 case R_NDS32_SDA_FP7U2_RELA:
7330 case R_NDS32_15_PCREL_RELA:
7331 case R_NDS32_17_PCREL_RELA:
7332 case R_NDS32_25_PCREL_RELA:
7334 if (h != NULL && !bfd_link_pic (info))
7337 h->plt.refcount += 1;
7340 /* If we are creating a shared library, and this is a reloc against
7341 a global symbol, or a non PC relative reloc against a local
7342 symbol, then we need to copy the reloc into the shared library.
7343 However, if we are linking with -Bsymbolic, we do not need to
7344 copy a reloc against a global symbol which is defined in an
7345 object we are including in the link (i.e., DEF_REGULAR is set).
7346 At this point we have not seen all the input files, so it is
7347 possible that DEF_REGULAR is not set now but will be set later
7348 (it is never cleared). We account for that possibility below by
7349 storing information in the dyn_relocs field of the hash table
7350 entry. A similar situation occurs when creating shared libraries
7351 and symbol visibility changes render the symbol local.
7353 If on the other hand, we are creating an executable, we may need
7354 to keep relocations for symbols satisfied by a dynamic library
7355 if we manage to avoid copy relocs for the symbol. */
7356 if ((bfd_link_pic (info)
7357 && (sec->flags & SEC_ALLOC) != 0
7358 && ((r_type != R_NDS32_25_PCREL_RELA
7359 && r_type != R_NDS32_15_PCREL_RELA
7360 && r_type != R_NDS32_17_PCREL_RELA
7361 && !(r_type == R_NDS32_32_RELA
7362 && strcmp (sec->name, ".eh_frame") == 0))
7365 || h->root.type == bfd_link_hash_defweak
7366 || !h->def_regular))))
7367 || (!bfd_link_pic (info)
7368 && (sec->flags & SEC_ALLOC) != 0
7370 && (h->root.type == bfd_link_hash_defweak
7371 || !h->def_regular)))
7373 struct elf_dyn_relocs *p;
7374 struct elf_dyn_relocs **head;
7377 htab->root.dynobj = dynobj = abfd;
7379 /* When creating a shared object, we must copy these
7380 relocs into the output file. We create a reloc
7381 section in dynobj and make room for the reloc. */
7386 name = bfd_elf_string_from_elf_section
7387 (abfd, elf_elfheader (abfd)->e_shstrndx,
7388 elf_section_data (sec)->rela.hdr->sh_name);
7392 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
7393 && strcmp (bfd_get_section_name (abfd, sec),
7396 sreloc = bfd_get_section_by_name (dynobj, name);
7401 sreloc = bfd_make_section (dynobj, name);
7402 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7403 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7404 if ((sec->flags & SEC_ALLOC) != 0)
7405 flags |= SEC_ALLOC | SEC_LOAD;
7407 || !bfd_set_section_flags (dynobj, sreloc, flags)
7408 || !bfd_set_section_alignment (dynobj, sreloc, 2))
7411 elf_section_type (sreloc) = SHT_RELA;
7413 elf_section_data (sec)->sreloc = sreloc;
7416 /* If this is a global symbol, we count the number of
7417 relocations we need for this symbol. */
7419 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
7425 Elf_Internal_Sym *isym;
7426 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7430 /* Track dynamic relocs needed for local syms too. */
7431 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7435 vpp = &elf_section_data (s)->local_dynrel;
7436 head = (struct elf_dyn_relocs **) vpp;
7440 if (p == NULL || p->sec != sec)
7442 bfd_size_type amt = sizeof (*p);
7443 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7455 /* Since eh_frame is readonly, R_NDS32_32_RELA
7456 reloc for eh_frame will cause shared library has
7457 TEXTREL entry in the dynamic section. This lead glibc
7458 testsuites to failure (bug-13092) and cause kernel fail
7459 (bug-11819). I think the best solution is to replace
7460 absolute reloc with pc relative reloc in the eh_frame.
7461 To do that, we need to support the following issues:
7464 * gcc/config/nds32/nds32.h: Define
7465 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7466 and DW_EH_PE_sdata4 into DWARF exception header when
7467 option have '-fpic'.
7469 === For binutils ===
7470 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7471 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7472 may break our nds DIFF mechanism, therefore, we
7473 must disable all linker relaxations to ensure
7475 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7476 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7477 do the necessary modification.
7479 Unfortunately, it still have some problems for nds32
7480 to support pc relative reloc in the eh_frame. So I use
7481 another solution to fix this issue.
7483 However, I find that ld always emit TEXTREL marker for
7484 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7485 correspond to R_NDS32_32_RELA for .eh_frame section.
7486 It means that we always reserve redundant entries of rel.dyn
7487 for these relocs which actually do nothing in dynamic linker.
7489 Therefore, we regard these relocs as pc relative relocs
7490 here and increase the pc_count. */
7491 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7492 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7493 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7494 || (r_type == R_NDS32_32_RELA
7495 && strcmp (sec->name, ".eh_frame") == 0))
7500 /* This relocation describes the C++ object vtable hierarchy.
7501 Reconstruct it for later use during GC. */
7502 case R_NDS32_RELA_GNU_VTINHERIT:
7503 case R_NDS32_GNU_VTINHERIT:
7504 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7508 /* This relocation describes which C++ vtable entries are actually
7509 used. Record for later use during GC. */
7510 case R_NDS32_GNU_VTENTRY:
7511 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7514 case R_NDS32_RELA_GNU_VTENTRY:
7515 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7524 /* Write VAL in uleb128 format to P, returning a pointer to the
7526 This code is copied from elf-attr.c. */
7529 write_uleb128 (bfd_byte *p, unsigned int val)
7544 static bfd_signed_vma
7545 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7546 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7548 bfd_signed_vma foff;
7549 bfd_vma symval, addend;
7552 /* Get the value of the symbol referred to by the reloc. */
7553 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7555 Elf_Internal_Sym *isym;
7557 /* A local symbol. */
7558 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7560 if (isym->st_shndx == SHN_UNDEF)
7561 sym_sec = bfd_und_section_ptr;
7562 else if (isym->st_shndx == SHN_ABS)
7563 sym_sec = bfd_abs_section_ptr;
7564 else if (isym->st_shndx == SHN_COMMON)
7565 sym_sec = bfd_com_section_ptr;
7567 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7568 symval = isym->st_value + sym_sec->output_section->vma
7569 + sym_sec->output_offset;
7574 struct elf_link_hash_entry *h;
7576 /* An external symbol. */
7577 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7578 h = elf_sym_hashes (abfd)[indx];
7579 BFD_ASSERT (h != NULL);
7581 if (h->root.type != bfd_link_hash_defined
7582 && h->root.type != bfd_link_hash_defweak)
7583 /* This appears to be a reference to an undefined
7584 symbol. Just ignore it--it will be caught by the
7585 regular reloc processing. */
7588 if (h->root.u.def.section->flags & SEC_MERGE)
7590 sym_sec = h->root.u.def.section;
7591 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7592 elf_section_data (sym_sec)->sec_info,
7593 h->root.u.def.value);
7594 symval = symval + sym_sec->output_section->vma
7595 + sym_sec->output_offset;
7598 symval = (h->root.u.def.value
7599 + h->root.u.def.section->output_section->vma
7600 + h->root.u.def.section->output_offset);
7603 addend = irel->r_addend;
7605 foff = (symval + addend
7606 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7611 /* Convert a 32-bit instruction to 16-bit one.
7612 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7613 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7614 type of INSN16. Return 1 if successful. */
7617 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7620 uint16_t insn16 = 0;
7622 unsigned long mach = bfd_get_mach (abfd);
7624 if (N32_SH5 (insn) != 0)
7627 switch (N32_SUB5 (insn))
7629 case N32_ALU1_ADD_SLLI:
7630 case N32_ALU1_ADD_SRLI:
7631 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7633 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7635 insn_type = NDS32_INSN_ADD333;
7637 else if (N32_IS_RT4 (insn))
7639 if (N32_RT5 (insn) == N32_RA5 (insn))
7640 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7641 else if (N32_RT5 (insn) == N32_RB5 (insn))
7642 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7643 insn_type = NDS32_INSN_ADD45;
7647 case N32_ALU1_SUB_SLLI:
7648 case N32_ALU1_SUB_SRLI:
7649 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7651 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7653 insn_type = NDS32_INSN_SUB333;
7655 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7657 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7658 insn_type = NDS32_INSN_SUB45;
7662 case N32_ALU1_AND_SLLI:
7663 case N32_ALU1_AND_SRLI:
7664 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7665 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7666 && N32_IS_RB3 (insn))
7668 if (N32_RT5 (insn) == N32_RA5 (insn))
7669 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7670 else if (N32_RT5 (insn) == N32_RB5 (insn))
7671 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7673 insn_type = NDS32_INSN_AND33;
7677 case N32_ALU1_XOR_SLLI:
7678 case N32_ALU1_XOR_SRLI:
7679 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7680 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7681 && N32_IS_RB3 (insn))
7683 if (N32_RT5 (insn) == N32_RA5 (insn))
7684 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7685 else if (N32_RT5 (insn) == N32_RB5 (insn))
7686 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7688 insn_type = NDS32_INSN_XOR33;
7692 case N32_ALU1_OR_SLLI:
7693 case N32_ALU1_OR_SRLI:
7694 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7695 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7696 && N32_IS_RB3 (insn))
7698 if (N32_RT5 (insn) == N32_RA5 (insn))
7699 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7700 else if (N32_RT5 (insn) == N32_RB5 (insn))
7701 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7703 insn_type = NDS32_INSN_OR33;
7707 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7708 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7709 && N32_RA5 (insn) == N32_RB5 (insn))
7711 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7712 insn_type = NDS32_INSN_NOT33;
7716 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7718 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7719 insn_type = NDS32_INSN_SRAI45;
7724 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7726 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7727 insn_type = NDS32_INSN_SRLI45;
7732 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7734 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7736 insn_type = NDS32_INSN_SLLI333;
7741 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7743 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7744 insn_type = NDS32_INSN_ZEH33;
7749 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7751 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7752 insn_type = NDS32_INSN_SEB33;
7757 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7759 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7760 insn_type = NDS32_INSN_SEH33;
7765 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7768 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7769 insn_type = NDS32_INSN_SLT45;
7774 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7777 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7778 insn_type = NDS32_INSN_SLTS45;
7783 if ((insn16 & 0x8000) == 0)
7789 *pinsn_type = insn_type;
7794 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7797 uint16_t insn16 = 0;
7799 unsigned long mach = bfd_get_mach (abfd);
7801 /* TODO: bset, bclr, btgl, btst. */
7802 if (__GF (insn, 6, 4) != 0)
7805 switch (N32_IMMU (insn, 6))
7808 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7809 && N32_IS_RB3 (insn))
7811 if (N32_RT5 (insn) == N32_RA5 (insn))
7812 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7813 else if (N32_RT5 (insn) == N32_RB5 (insn))
7814 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7816 insn_type = NDS32_INSN_MUL33;
7820 if ((insn16 & 0x8000) == 0)
7826 *pinsn_type = insn_type;
7831 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7835 uint16_t insn16 = 0;
7837 unsigned long mach = bfd_get_mach (abfd);
7839 /* Decode 32-bit instruction. */
7840 if (insn & 0x80000000)
7842 /* Not 32-bit insn. */
7846 op6 = N32_OP6 (insn);
7848 /* Convert it to 16-bit instruction. */
7852 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7854 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7855 insn_type = NDS32_INSN_MOVI55;
7857 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7858 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7860 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7861 N32_IMM20S (insn) - 16);
7862 insn_type = NDS32_INSN_MOVPI45;
7867 if (N32_IMM15S (insn) == 0)
7869 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7870 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7872 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7874 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7875 insn_type = NDS32_INSN_MOV55;
7878 else if (N32_IMM15S (insn) > 0)
7880 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7882 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7884 insn_type = NDS32_INSN_ADDI333;
7886 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7887 && N32_IMM15S (insn) < 32)
7889 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7890 insn_type = NDS32_INSN_ADDI45;
7892 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7893 && N32_RT5 (insn) == N32_RA5 (insn)
7894 && N32_IMM15S (insn) < 512)
7896 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7897 insn_type = NDS32_INSN_ADDI10_SP;
7899 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7900 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7901 && (N32_IMM15S (insn) % 4 == 0))
7903 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7904 N32_IMM15S (insn) >> 2);
7905 insn_type = NDS32_INSN_ADDRI36_SP;
7911 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7913 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7914 0 - N32_IMM15S (insn));
7915 insn_type = NDS32_INSN_SUBI333;
7917 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7918 && N32_IMM15S (insn) > -32)
7920 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7921 0 - N32_IMM15S (insn));
7922 insn_type = NDS32_INSN_SUBI45;
7924 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7925 && N32_RT5 (insn) == N32_RA5 (insn)
7926 && N32_IMM15S (insn) >= -512)
7928 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7929 insn_type = NDS32_INSN_ADDI10_SP;
7935 if (N32_IMM15S (insn) == 0)
7937 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7938 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7940 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7942 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7943 insn_type = NDS32_INSN_MOV55;
7949 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7950 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7952 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7953 insn_type = NDS32_INSN_NEG33;
7958 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7960 if (N32_IMM15U (insn) == 1)
7962 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7963 insn_type = NDS32_INSN_XLSB33;
7965 else if (N32_IMM15U (insn) == 0x7ff)
7967 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7968 insn_type = NDS32_INSN_X11B33;
7970 else if (N32_IMM15U (insn) == 0xff)
7972 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7973 insn_type = NDS32_INSN_ZEB33;
7975 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7976 && N32_IMM15U (insn) < 256)
7978 int imm15u = N32_IMM15U (insn);
7980 if (__builtin_popcount (imm15u) == 1)
7983 int imm3u = __builtin_ctz (imm15u);
7985 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7986 insn_type = NDS32_INSN_BMSKI33;
7988 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7991 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7993 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7994 insn_type = NDS32_INSN_FEXTI33;
8001 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8002 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8004 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8005 insn_type = NDS32_INSN_SLTI45;
8010 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8011 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8013 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8014 insn_type = NDS32_INSN_SLTSI45;
8019 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8021 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8022 insn_type = NDS32_INSN_LWI450;
8024 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8025 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8027 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8029 insn_type = NDS32_INSN_LWI333;
8031 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8032 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8034 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8035 insn_type = NDS32_INSN_LWI37;
8037 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8038 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8040 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8041 insn_type = NDS32_INSN_LWI37_SP;
8043 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8044 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8046 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8047 N32_IMM15S (insn) + 32);
8048 insn_type = NDS32_INSN_LWI45_FE;
8053 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8055 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8056 insn_type = NDS32_INSN_SWI450;
8058 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8059 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8061 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8063 insn_type = NDS32_INSN_SWI333;
8065 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8066 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8068 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8069 insn_type = NDS32_INSN_SWI37;
8071 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8072 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8074 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8075 insn_type = NDS32_INSN_SWI37_SP;
8079 case N32_OP6_LWI_BI:
8080 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8081 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8083 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8085 insn_type = NDS32_INSN_LWI333_BI;
8089 case N32_OP6_SWI_BI:
8090 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8091 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8093 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8095 insn_type = NDS32_INSN_SWI333_BI;
8100 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8101 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8103 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8105 insn_type = NDS32_INSN_LHI333;
8110 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8111 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8113 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8115 insn_type = NDS32_INSN_SHI333;
8120 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8121 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8123 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8125 insn_type = NDS32_INSN_LBI333;
8130 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8131 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8133 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8135 insn_type = NDS32_INSN_SBI333;
8140 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8143 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8146 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8149 if ((insn & N32_BIT (14)) == 0)
8152 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8153 && N32_RT5 (insn) != REG_R5)
8154 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8155 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8156 && N32_RA5 (insn) != REG_R5)
8157 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8158 insn_type = NDS32_INSN_BEQS38;
8164 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8165 && N32_RT5 (insn) != REG_R5)
8166 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8167 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8168 && N32_RA5 (insn) != REG_R5)
8169 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8170 insn_type = NDS32_INSN_BNES38;
8176 switch (N32_BR2_SUB (insn))
8179 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8181 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8182 insn_type = NDS32_INSN_BEQZ38;
8184 else if (N32_RT5 (insn) == REG_R15
8185 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8187 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8188 insn_type = NDS32_INSN_BEQZS8;
8193 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8195 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8196 insn_type = NDS32_INSN_BNEZ38;
8198 else if (N32_RT5 (insn) == REG_R15
8199 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8201 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8202 insn_type = NDS32_INSN_BNEZS8;
8207 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8209 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8210 insn_type = NDS32_INSN_IFCALL9;
8217 if ((insn & N32_BIT (24)) == 0)
8220 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8222 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8223 insn_type = NDS32_INSN_J8;
8229 if (__GF (insn, 8, 2) != 0)
8232 switch (N32_IMMU (insn, 5))
8235 if (N32_JREG_HINT (insn) == 0)
8238 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8239 insn_type = NDS32_INSN_JR5;
8241 else if (N32_JREG_HINT (insn) == 1)
8244 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8245 insn_type = NDS32_INSN_RET5;
8247 else if (N32_JREG_HINT (insn) == 3)
8249 /* ifret = mov55 $sp, $sp */
8250 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8251 insn_type = NDS32_INSN_IFRET;
8256 /* It's convertible when return rt5 is $lp and address
8257 translation is kept. */
8258 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8260 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8261 insn_type = NDS32_INSN_JRAL5;
8268 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8270 /* For v3, swid above 31 are used for ex9.it. */
8271 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8272 insn_type = NDS32_INSN_BREAK16;
8277 /* This instruction has no 16-bit variant. */
8282 /* Bit-15 of insn16 should be set for a valid instruction. */
8283 if ((insn16 & 0x8000) == 0)
8289 *pinsn_type = insn_type;
8294 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8295 Elf_Internal_Rela *reloc)
8297 uint16_t insn16 = 0;
8299 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8300 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8303 if (!N32_IS_RT3 (insn))
8306 switch (N32_OP6 (insn))
8309 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8310 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8313 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8314 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8317 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8320 if (__GF (insn, 17, 3) == 6)
8321 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8322 else if (__GF (insn, 17, 3) == 7)
8323 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8327 if ((insn16 & 0x8000) == 0)
8334 /* Convert a 16-bit instruction to 32-bit one.
8335 INSN16 it the input and PINSN it the point to output.
8336 Return non-zero on successful. Otherwise 0 is returned. */
8339 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8341 uint32_t insn = 0xffffffff;
8342 unsigned long mach = bfd_get_mach (abfd);
8344 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8346 switch (__GF (insn16, 9, 6))
8348 case 0x4: /* add45 */
8349 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8352 case 0x5: /* sub45 */
8353 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8356 case 0x6: /* addi45 */
8357 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8358 N16_IMM5U (insn16));
8360 case 0x7: /* subi45 */
8361 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8362 -N16_IMM5U (insn16));
8364 case 0x8: /* srai45 */
8365 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8366 N16_IMM5U (insn16));
8368 case 0x9: /* srli45 */
8369 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8370 N16_IMM5U (insn16));
8372 case 0xa: /* slli333 */
8373 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8374 N16_IMM3U (insn16));
8376 case 0xc: /* add333 */
8377 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8380 case 0xd: /* sub333 */
8381 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8384 case 0xe: /* addi333 */
8385 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8386 N16_IMM3U (insn16));
8388 case 0xf: /* subi333 */
8389 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8390 -N16_IMM3U (insn16));
8392 case 0x10: /* lwi333 */
8393 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8394 N16_IMM3U (insn16));
8396 case 0x12: /* lhi333 */
8397 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8398 N16_IMM3U (insn16));
8400 case 0x13: /* lbi333 */
8401 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8402 N16_IMM3U (insn16));
8404 case 0x11: /* lwi333.bi */
8405 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8406 N16_IMM3U (insn16));
8408 case 0x14: /* swi333 */
8409 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8410 N16_IMM3U (insn16));
8412 case 0x16: /* shi333 */
8413 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8414 N16_IMM3U (insn16));
8416 case 0x17: /* sbi333 */
8417 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8418 N16_IMM3U (insn16));
8420 case 0x15: /* swi333.bi */
8421 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8422 N16_IMM3U (insn16));
8424 case 0x18: /* addri36.sp */
8425 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8426 N16_IMM6U (insn16) << 2);
8428 case 0x19: /* lwi45.fe */
8429 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8430 (N16_IMM5U (insn16) - 32));
8432 case 0x1a: /* lwi450 */
8433 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8435 case 0x1b: /* swi450 */
8436 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8439 /* These are r15 implied instructions. */
8440 case 0x30: /* slts45 */
8441 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8443 case 0x31: /* slt45 */
8444 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8446 case 0x32: /* sltsi45 */
8447 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8449 case 0x33: /* slti45 */
8450 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8452 case 0x34: /* beqzs8, bnezs8 */
8453 if (insn16 & N32_BIT (8))
8454 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8456 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8459 case 0x35: /* break16, ex9.it */
8460 /* Only consider range of v3 break16. */
8461 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8464 case 0x3c: /* ifcall9 */
8465 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8467 case 0x3d: /* movpi45 */
8468 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8471 case 0x3f: /* MISC33 */
8472 switch (insn16 & 0x7)
8475 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8478 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8482 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8486 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8490 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8494 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8501 switch (insn16 & 0x7)
8504 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8507 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8510 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8513 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8515 case 4: /* xlsb33 */
8516 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8518 case 5: /* x11b33 */
8519 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8521 case 6: /* bmski33 */
8522 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8523 1 << __GF (insn16, 3, 3));
8525 case 7: /* fexti33 */
8526 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8527 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8533 switch (__GF (insn16, 10, 5))
8535 case 0x0: /* mov55 or ifret16 */
8536 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8537 && N16_RT5 (insn16) == N16_RA5 (insn16))
8538 insn = N32_JREG (JR, 0, 0, 0, 3);
8540 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8542 case 0x1: /* movi55 */
8543 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8545 case 0x1b: /* addi10s (V2) */
8546 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8550 switch (__GF (insn16, 11, 4))
8552 case 0x7: /* lwi37.fp/swi37.fp */
8553 if (insn16 & N32_BIT (7)) /* swi37.fp */
8554 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8556 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8558 case 0x8: /* beqz38 */
8559 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8561 case 0x9: /* bnez38 */
8562 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8564 case 0xa: /* beqs38/j8, implied r5 */
8565 if (N16_RT38 (insn16) == 5)
8566 insn = N32_JI (J, N16_IMM8S (insn16));
8568 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8570 case 0xb: /* bnes38 and others. */
8571 if (N16_RT38 (insn16) == 5)
8573 switch (__GF (insn16, 5, 3))
8576 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8579 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8582 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8584 case 2: /* ex9.it imm5 */
8585 /* ex9.it had no 32-bit variantl. */
8587 case 5: /* add5.pc */
8588 /* add5.pc had no 32-bit variantl. */
8593 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8595 case 0xe: /* lwi37/swi37 */
8596 if (insn16 & (1 << 7)) /* swi37.sp */
8597 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8599 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8604 if (insn & 0x80000000)
8614 is_sda_access_insn (unsigned long insn)
8616 switch (N32_OP6 (insn))
8637 static unsigned long
8638 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8644 case R_NDS32_GOT_LO12:
8645 case R_NDS32_GOTOFF_LO12:
8646 case R_NDS32_PLTREL_LO12:
8647 case R_NDS32_PLT_GOTREL_LO12:
8648 case R_NDS32_LO12S0_RELA:
8649 switch (N32_OP6 (insn))
8653 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8657 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8661 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8665 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8670 case R_NDS32_LO12S1_RELA:
8671 switch (N32_OP6 (insn))
8675 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8679 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8683 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8688 case R_NDS32_LO12S2_RELA:
8689 switch (N32_OP6 (insn))
8693 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8697 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8702 case R_NDS32_LO12S2_DP_RELA:
8703 case R_NDS32_LO12S2_SP_RELA:
8704 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8714 /* Linker hasn't found the correct merge section for non-section symbol
8715 in relax time, this work is left to the function elf_link_input_bfd().
8716 So for non-section symbol, _bfd_merged_section_offset is also needed
8717 to find the correct symbol address. */
8720 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8721 asection **psec, Elf_Internal_Rela *rel)
8723 asection *sec = *psec;
8726 relocation = (sec->output_section->vma
8727 + sec->output_offset + sym->st_value);
8728 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8730 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8732 _bfd_merged_section_offset (abfd, psec,
8733 elf_section_data (sec)->sec_info,
8734 sym->st_value + rel->r_addend);
8737 _bfd_merged_section_offset (abfd, psec,
8738 elf_section_data (sec)->sec_info,
8739 sym->st_value) + rel->r_addend;
8743 /* If we have changed the section, and our original section is
8744 marked with SEC_EXCLUDE, it means that the original
8745 SEC_MERGE section has been completely subsumed in some
8746 other SEC_MERGE section. In this case, we need to leave
8747 some info around for --emit-relocs. */
8748 if ((sec->flags & SEC_EXCLUDE) != 0)
8749 sec->kept_section = *psec;
8752 rel->r_addend -= relocation;
8753 rel->r_addend += sec->output_section->vma + sec->output_offset;
8759 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8760 Elf_Internal_Sym *isymbuf,
8761 Elf_Internal_Shdr *symtab_hdr)
8763 bfd_signed_vma foff;
8764 bfd_vma symval, addend;
8765 Elf_Internal_Rela irel_fn;
8766 Elf_Internal_Sym *isym;
8769 /* Get the value of the symbol referred to by the reloc. */
8770 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8772 /* A local symbol. */
8773 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8775 if (isym->st_shndx == SHN_UNDEF)
8776 sym_sec = bfd_und_section_ptr;
8777 else if (isym->st_shndx == SHN_ABS)
8778 sym_sec = bfd_abs_section_ptr;
8779 else if (isym->st_shndx == SHN_COMMON)
8780 sym_sec = bfd_com_section_ptr;
8782 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8783 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8784 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8785 addend = irel_fn.r_addend;
8790 struct elf_link_hash_entry *h;
8792 /* An external symbol. */
8793 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8794 h = elf_sym_hashes (abfd)[indx];
8795 BFD_ASSERT (h != NULL);
8797 while (h->root.type == bfd_link_hash_indirect
8798 || h->root.type == bfd_link_hash_warning)
8799 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8801 if (h->root.type != bfd_link_hash_defined
8802 && h->root.type != bfd_link_hash_defweak)
8803 /* This appears to be a reference to an undefined
8804 symbol. Just ignore it--it will be caught by the
8805 regular reloc processing. */
8808 if (h->root.u.def.section->flags & SEC_MERGE)
8810 sym_sec = h->root.u.def.section;
8811 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8812 (sym_sec)->sec_info, h->root.u.def.value);
8813 symval = symval + sym_sec->output_section->vma
8814 + sym_sec->output_offset;
8817 symval = (h->root.u.def.value
8818 + h->root.u.def.section->output_section->vma
8819 + h->root.u.def.section->output_offset);
8820 addend = irel->r_addend;
8823 foff = symval + addend;
8829 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8830 asection *sec, Elf_Internal_Rela *rel)
8833 unsigned short insn16;
8835 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8837 contents = elf_section_data (sec)->this_hdr.contents;
8838 insn16 = bfd_getb16 (contents + rel->r_offset);
8839 if (insn16 == NDS32_NOP16)
8844 /* It checks whether the instruction could be converted to
8845 16-bit form and returns the converted one.
8847 `internal_relocs' is supposed to be sorted. */
8850 is_convert_32_to_16 (bfd *abfd, asection *sec,
8851 Elf_Internal_Rela *reloc,
8852 Elf_Internal_Rela *internal_relocs,
8853 Elf_Internal_Rela *irelend,
8856 #define NORMAL_32_TO_16 (1 << 0)
8857 #define SPECIAL_32_TO_16 (1 << 1)
8858 bfd_byte *contents = NULL;
8862 Elf_Internal_Rela *pc_rel;
8863 Elf_Internal_Shdr *symtab_hdr;
8864 Elf_Internal_Sym *isymbuf = NULL;
8868 if (reloc->r_offset + 4 > sec->size)
8871 offset = reloc->r_offset;
8873 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
8875 insn = bfd_getb32 (contents + offset);
8877 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8878 convert_type = NORMAL_32_TO_16;
8879 else if (special_convert_32_to_16 (insn, insn16, reloc))
8880 convert_type = SPECIAL_32_TO_16;
8884 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8885 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8888 /* Find the first relocation of the same relocation-type,
8889 so we iteratie them forward. */
8891 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8894 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8896 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8897 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8898 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8899 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8901 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8902 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8907 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8909 /* movi => movi55 */
8910 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8912 /* mem_addr is unsigned, but the value should
8913 be between [-16, 15]. */
8914 if ((mem_addr + 0x10) >> 5)
8918 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8919 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8921 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8922 because it can be relaxed to addi for TLS_LE_ADD. */
8925 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8926 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8927 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8928 && convert_type == SPECIAL_32_TO_16)
8931 We've selected a best fp-base for this access, so we can
8932 always resolve it anyway. Do nothing. */
8935 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8936 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8937 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8938 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8939 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8940 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8942 /* Prevent unresolved addi instruction translate
8943 to addi45 or addi333. */
8946 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8948 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8949 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8959 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8960 Elf_Internal_Rela *reloc,
8961 Elf_Internal_Rela *internal_relocs,
8962 Elf_Internal_Rela *irelend,
8963 unsigned short insn16)
8965 Elf_Internal_Rela *pc_rel;
8968 offset = reloc->r_offset;
8969 bfd_putb16 (insn16, contents + offset);
8970 /* Find the first relocation of the same relocation-type,
8971 so we iteratie them forward. */
8973 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8976 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8978 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8979 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8980 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8983 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8985 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8987 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8988 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8990 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8991 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8992 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8994 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8995 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8997 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
9001 /* Find a relocation of type specified by `reloc_type'
9002 of the same r_offset with reloc.
9003 If not found, return irelend.
9005 Assuming relocations are sorted by r_offset,
9006 we find the relocation from `reloc' backward untill relocs,
9007 or find it from `reloc' forward untill irelend. */
9009 static Elf_Internal_Rela *
9010 find_relocs_at_address (Elf_Internal_Rela *reloc,
9011 Elf_Internal_Rela *relocs,
9012 Elf_Internal_Rela *irelend,
9013 enum elf_nds32_reloc_type reloc_type)
9015 Elf_Internal_Rela *rel_t;
9017 /* Find backward. */
9019 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9021 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9024 /* We didn't find it backward. Try find it forward. */
9026 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9028 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9034 /* Find a relocation of specified type and offset.
9035 `reloc' is just a refence point to find a relocation at specified offset.
9036 If not found, return irelend.
9038 Assuming relocations are sorted by r_offset,
9039 we find the relocation from `reloc' backward untill relocs,
9040 or find it from `reloc' forward untill irelend. */
9042 static Elf_Internal_Rela *
9043 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9044 Elf_Internal_Rela *relocs,
9045 Elf_Internal_Rela *irelend,
9046 enum elf_nds32_reloc_type reloc_type,
9049 Elf_Internal_Rela *rel_t = NULL;
9051 /* First, we try to find a relocation of offset `offset_p',
9052 and then we use find_relocs_at_address to find specific type. */
9054 if (reloc->r_offset > offset_p)
9056 /* Find backward. */
9058 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9061 else if (reloc->r_offset < offset_p)
9065 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9072 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9075 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9078 typedef struct nds32_elf_blank nds32_elf_blank_t;
9079 struct nds32_elf_blank
9081 /* Where the blank begins. */
9083 /* The size of the blank. */
9085 /* The accumulative size before this blank. */
9087 nds32_elf_blank_t *next;
9088 nds32_elf_blank_t *prev;
9091 static nds32_elf_blank_t *blank_free_list = NULL;
9093 static nds32_elf_blank_t *
9094 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9096 nds32_elf_blank_t *blank_t;
9098 if (blank_free_list)
9100 blank_t = blank_free_list;
9101 blank_free_list = blank_free_list->next;
9104 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9106 if (blank_t == NULL)
9109 blank_t->offset = offset_p;
9110 blank_t->size = size_p;
9111 blank_t->total_size = 0;
9112 blank_t->next = NULL;
9113 blank_t->prev = NULL;
9119 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9121 if (blank_free_list)
9123 blank_free_list->prev = blank_p;
9124 blank_p->next = blank_free_list;
9127 blank_p->next = NULL;
9129 blank_p->prev = NULL;
9130 blank_free_list = blank_p;
9134 clean_nds32_elf_blank (void)
9136 nds32_elf_blank_t *blank_t;
9138 while (blank_free_list)
9140 blank_t = blank_free_list;
9141 blank_free_list = blank_free_list->next;
9146 static nds32_elf_blank_t *
9147 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9149 nds32_elf_blank_t *blank_t;
9155 while (blank_t && addr < blank_t->offset)
9156 blank_t = blank_t->prev;
9157 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9158 blank_t = blank_t->next;
9164 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9167 nds32_elf_blank_t *blank_t;
9169 blank_t = search_nds32_elf_blank (*blank_p, addr);
9176 if (addr < blank_t->offset + blank_t->size)
9177 return blank_t->total_size + (addr - blank_t->offset);
9179 return blank_t->total_size + blank_t->size;
9183 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9185 nds32_elf_blank_t *blank_t, *blank_t2;
9189 *blank_p = create_nds32_elf_blank (addr, len);
9190 return *blank_p ? TRUE : FALSE;
9193 blank_t = search_nds32_elf_blank (*blank_p, addr);
9195 if (blank_t == NULL)
9197 blank_t = create_nds32_elf_blank (addr, len);
9200 while ((*blank_p)->prev != NULL)
9201 *blank_p = (*blank_p)->prev;
9202 blank_t->next = *blank_p;
9203 (*blank_p)->prev = blank_t;
9204 (*blank_p) = blank_t;
9208 if (addr < blank_t->offset + blank_t->size)
9210 /* Extend the origin blank. */
9211 if (addr + len > blank_t->offset + blank_t->size)
9212 blank_t->size = addr + len - blank_t->offset;
9216 blank_t2 = create_nds32_elf_blank (addr, len);
9221 blank_t->next->prev = blank_t2;
9222 blank_t2->next = blank_t->next;
9224 blank_t2->prev = blank_t;
9225 blank_t->next = blank_t2;
9226 *blank_p = blank_t2;
9233 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9236 nds32_elf_blank_t *blank_t;
9238 if (!insert_nds32_elf_blank (blank_p, addr, len))
9245 blank_t->total_size = 0;
9246 blank_t = blank_t->next;
9251 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9252 blank_t = blank_t->next;
9259 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9261 nds32_elf_blank_t *blank_t;
9262 bfd_vma total_size = 0;
9268 while (blank_t->prev)
9269 blank_t = blank_t->prev;
9272 blank_t->total_size = total_size;
9273 total_size += blank_t->size;
9274 blank_t = blank_t->next;
9279 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9280 nds32_elf_blank_t *blank_p)
9282 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
9283 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
9284 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9285 unsigned int sec_shndx; /* The section the be relaxed. */
9286 bfd_byte *contents; /* Contents data of iterating section. */
9287 Elf_Internal_Rela *internal_relocs;
9288 Elf_Internal_Rela *irel;
9289 Elf_Internal_Rela *irelend;
9290 struct elf_link_hash_entry **sym_hashes;
9291 struct elf_link_hash_entry **end_hashes;
9292 unsigned int symcount;
9294 nds32_elf_blank_t *blank_t;
9295 nds32_elf_blank_t *blank_t2;
9296 nds32_elf_blank_t *blank_head;
9298 blank_head = blank_t = blank_p;
9299 while (blank_head->prev != NULL)
9300 blank_head = blank_head->prev;
9301 while (blank_t->next != NULL)
9302 blank_t = blank_t->next;
9304 if (blank_t->offset + blank_t->size <= sec->size)
9306 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9307 blank_t->next->prev = blank_t;
9309 if (blank_head->offset > 0)
9311 blank_head->prev = create_nds32_elf_blank (0, 0);
9312 blank_head->prev->next = blank_head;
9313 blank_head = blank_head->prev;
9316 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9318 /* The deletion must stop at the next ALIGN reloc for an alignment
9319 power larger than the number of bytes we are deleting. */
9321 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9322 if (!nds32_get_local_syms (abfd, sec, &isym))
9327 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9328 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9329 symtab_hdr->contents = (bfd_byte *) isym;
9332 if (isym == NULL || symtab_hdr->sh_info == 0)
9335 blank_t = blank_head;
9336 calc_nds32_blank_total (blank_head);
9338 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9340 /* Adjust all the relocs. */
9342 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9343 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9344 TRUE /* keep_memory */);
9345 irelend = internal_relocs + sect->reloc_count;
9347 blank_t = blank_head;
9348 blank_t2 = blank_head;
9350 if (!(sect->flags & SEC_RELOC))
9353 nds32_get_section_contents (abfd, sect, &contents, TRUE);
9355 for (irel = internal_relocs; irel < irelend; irel++)
9359 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9360 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9361 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9363 unsigned long val = 0;
9365 long before, between;
9368 switch (ELF32_R_TYPE (irel->r_info))
9371 offset = bfd_get_8 (abfd, contents + irel->r_offset);
9373 case R_NDS32_DIFF16:
9374 offset = bfd_get_16 (abfd, contents + irel->r_offset);
9376 case R_NDS32_DIFF32:
9377 val = bfd_get_32 (abfd, contents + irel->r_offset);
9378 /* Get the signed bit and mask for the high part. The
9379 gcc will alarm when right shift 32-bit since the
9380 type size of long may be 32-bit. */
9381 mask = 0 - (val >> 31);
9383 offset = (val | (mask - 0xffffffff));
9392 0 |encoded in location|
9393 |------------|-------------------|---------
9395 -- before ---| *****************
9396 --------------------- between ---|
9398 We only care how much data are relax between DIFF,
9401 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9402 between = get_nds32_elf_blank_total (&blank_t,
9403 irel->r_addend + offset, 0);
9404 if (between == before)
9405 goto done_adjust_diff;
9407 switch (ELF32_R_TYPE (irel->r_info))
9410 bfd_put_8 (abfd, offset - (between - before),
9411 contents + irel->r_offset);
9413 case R_NDS32_DIFF16:
9414 bfd_put_16 (abfd, offset - (between - before),
9415 contents + irel->r_offset);
9417 case R_NDS32_DIFF32:
9418 bfd_put_32 (abfd, offset - (between - before),
9419 contents + irel->r_offset);
9423 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9424 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9427 unsigned int len = 0;
9428 unsigned long before, between;
9431 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9434 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9435 between = get_nds32_elf_blank_total (&blank_t,
9436 irel->r_addend + val, 0);
9437 if (between == before)
9438 goto done_adjust_diff;
9440 p = contents + irel->r_offset;
9442 memset (p, 0x80, len);
9444 p = write_uleb128 (p, val - (between - before)) - 1;
9452 raddr = irel->r_offset;
9453 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9456 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9458 if (blank_t2 && blank_t2->next
9459 && (blank_t2->offset > raddr
9460 || blank_t2->next->offset <= raddr))
9462 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9465 /* Mark reloc in deleted portion as NONE.
9466 For some relocs like R_NDS32_LABEL that doesn't modify the
9467 content in the section. R_NDS32_LABEL doesn't belong to the
9468 instruction in the section, so we should preserve it. */
9469 if (raddr >= blank_t2->offset
9470 && raddr < blank_t2->offset + blank_t2->size
9471 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9472 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9473 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9474 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9475 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9476 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9478 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9484 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9485 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9486 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9489 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9490 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9491 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9493 if (irel->r_addend <= sec->size)
9495 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9500 /* Adjust the local symbols defined in this section. */
9501 blank_t = blank_head;
9502 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9504 if (isym->st_shndx == sec_shndx)
9506 if (isym->st_value <= sec->size)
9509 bfd_vma orig_addr = isym->st_value;
9511 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9512 isym->st_value -= ahead;
9514 /* Adjust function size. */
9515 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9516 && isym->st_size > 0)
9518 get_nds32_elf_blank_total
9519 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9524 /* Now adjust the global symbols defined in this section. */
9525 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9526 - symtab_hdr->sh_info);
9527 sym_hashes = elf_sym_hashes (abfd);
9528 end_hashes = sym_hashes + symcount;
9529 blank_t = blank_head;
9530 for (; sym_hashes < end_hashes; sym_hashes++)
9532 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9534 if ((sym_hash->root.type == bfd_link_hash_defined
9535 || sym_hash->root.type == bfd_link_hash_defweak)
9536 && sym_hash->root.u.def.section == sec)
9538 if (sym_hash->root.u.def.value <= sec->size)
9541 bfd_vma orig_addr = sym_hash->root.u.def.value;
9543 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9544 sym_hash->root.u.def.value -= ahead;
9546 /* Adjust function size. */
9547 if (sym_hash->type == STT_FUNC)
9549 get_nds32_elf_blank_total
9550 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9556 contents = elf_section_data (sec)->this_hdr.contents;
9557 blank_t = blank_head;
9558 while (blank_t->next)
9560 /* Actually delete the bytes. */
9562 /* If current blank is the last blank overlap with current section,
9563 go to finish process. */
9564 if (sec->size <= (blank_t->next->offset))
9567 memmove (contents + blank_t->offset - blank_t->total_size,
9568 contents + blank_t->offset + blank_t->size,
9569 blank_t->next->offset - (blank_t->offset + blank_t->size));
9571 blank_t = blank_t->next;
9574 if (sec->size > (blank_t->offset + blank_t->size))
9576 /* There are remaining code between blank and section boundary.
9577 Move the remaining code to appropriate location. */
9578 memmove (contents + blank_t->offset - blank_t->total_size,
9579 contents + blank_t->offset + blank_t->size,
9580 sec->size - (blank_t->offset + blank_t->size));
9581 sec->size -= blank_t->total_size + blank_t->size;
9584 /* This blank is not entirely included in the section,
9585 reduce the section size by only part of the blank size. */
9586 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9590 blank_t = blank_head;
9591 blank_head = blank_head->next;
9592 remove_nds32_elf_blank (blank_t);
9598 /* Get the contents of a section. */
9601 nds32_get_section_contents (bfd *abfd, asection *sec,
9602 bfd_byte **contents_p, bfd_boolean cache)
9604 /* Get the section contents. */
9605 if (elf_section_data (sec)->this_hdr.contents != NULL)
9606 *contents_p = elf_section_data (sec)->this_hdr.contents;
9609 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9612 elf_section_data (sec)->this_hdr.contents = *contents_p;
9618 /* Get the contents of the internal symbol of abfd. */
9621 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9622 Elf_Internal_Sym **isymbuf_p)
9624 Elf_Internal_Shdr *symtab_hdr;
9625 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9627 /* Read this BFD's local symbols if we haven't done so already. */
9628 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9630 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9631 if (*isymbuf_p == NULL)
9633 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9634 symtab_hdr->sh_info, 0,
9636 if (*isymbuf_p == NULL)
9640 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9645 /* Range of small data. */
9646 static bfd_vma sdata_range[2][2];
9647 static bfd_vma const sdata_init_range[2] =
9648 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
9651 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9652 bfd_byte *contents, bfd_vma addr)
9654 unsigned long insn = bfd_getb32 (contents + addr);
9656 if (insn & 0x80000000)
9662 /* Set the gp relax range. We have to measure the safe range
9663 to do gp relaxation. */
9666 relax_range_measurement (bfd *abfd)
9668 asection *sec_f, *sec_b;
9669 /* For upper bound. */
9670 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9672 static int decide_relax_range = 0;
9674 int range_number = ARRAY_SIZE (sdata_init_range);
9676 if (decide_relax_range)
9678 decide_relax_range = 1;
9680 if (sda_rela_sec == NULL)
9682 /* Since there is no data sections, we assume the range is page size. */
9683 for (i = 0; i < range_number; i++)
9685 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9686 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9691 /* Get the biggest alignment power after the gp located section. */
9692 sec_f = sda_rela_sec->output_section;
9693 sec_b = sec_f->next;
9695 while (sec_b != NULL)
9697 if ((unsigned)(1 << sec_b->alignment_power) > align)
9698 align = (1 << sec_b->alignment_power);
9699 sec_b = sec_b->next;
9702 /* I guess we can not determine the section before
9703 gp located section, so we assume the align is max page size. */
9704 for (i = 0; i < range_number; i++)
9706 sdata_range[i][1] = sdata_init_range[i] - align;
9707 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9708 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9709 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9713 /* These are macros used to check flags encoded in r_addend.
9714 They are only used by nds32_elf_relax_section (). */
9715 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9716 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9717 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9718 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9720 static const char * unrecognized_reloc_msg =
9721 /* xgettext:c-format */
9722 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9724 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9727 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9728 Elf_Internal_Rela *internal_relocs, int *insn_len,
9729 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9730 Elf_Internal_Shdr *symtab_hdr)
9732 /* There are 3 variations for LONGCALL1
9733 case 4-4-2; 16-bit on, optimize off or optimize for space
9734 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9735 ori ta, ta, lo12(symbol) ; LO12S0
9738 case 4-4-4; 16-bit off, optimize don't care
9739 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9740 ori ta, ta, lo12(symbol) ; LO12S0
9743 case 4-4-4; 16-bit on, optimize for speed
9744 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9745 ori ta, ta, lo12(symbol) ; LO12S0
9747 Check code for -mlong-calls output. */
9749 /* Get the reloc for the address from which the register is
9750 being loaded. This reloc will tell us which function is
9751 actually being called. */
9754 int seq_len; /* Original length of instruction sequence. */
9756 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9757 bfd_signed_vma foff;
9760 irelend = internal_relocs + sec->reloc_count;
9761 seq_len = GET_SEQ_LEN (irel->r_addend);
9762 laddr = irel->r_offset;
9763 *insn_len = seq_len;
9765 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9766 R_NDS32_HI20_RELA, laddr);
9767 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9768 R_NDS32_LO12S0_ORI_RELA,
9771 if (hi_irelfn == irelend || lo_irelfn == irelend)
9773 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9774 (uint64_t) irel->r_offset);
9778 /* Get the value of the symbol referred to by the reloc. */
9779 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9781 /* This condition only happened when symbol is undefined. */
9783 || foff < -CONSERVATIVE_24BIT_S1
9784 || foff >= CONSERVATIVE_24BIT_S1)
9787 /* Relax to: jal symbol; 25_PCREL. */
9788 /* For simplicity of coding, we are going to modify the section
9789 contents, the section relocs, and the BFD symbol table. We
9790 must tell the rest of the code not to free up this
9791 information. It would be possible to instead create a table
9792 of changes which have to be made, as is done in coff-mips.c;
9793 that would be more work, but would require less memory when
9794 the linker is run. */
9796 /* Replace the long call with a jal. */
9797 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9798 R_NDS32_25_PCREL_RELA);
9799 irel->r_addend = hi_irelfn->r_addend;
9801 /* We don't resolve this here but resolve it in relocate_section. */
9803 bfd_putb32 (insn, contents + irel->r_offset);
9806 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9808 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9813 insn16 = NDS32_NOP16;
9814 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9816 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9817 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9823 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9824 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
9827 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9828 Elf_Internal_Rela *internal_relocs, int *insn_len,
9829 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9830 Elf_Internal_Shdr *symtab_hdr)
9832 /* bltz rt, .L1 ; LONGCALL2
9833 jal symbol ; 25_PCREL
9836 /* Get the reloc for the address from which the register is
9837 being loaded. This reloc will tell us which function is
9838 actually being called. */
9842 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9843 bfd_signed_vma foff;
9845 irelend = internal_relocs + sec->reloc_count;
9846 laddr = irel->r_offset;
9848 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9849 R_NDS32_25_PCREL_RELA, laddr + 4);
9851 if (i1_irelfn == irelend)
9853 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9854 (uint64_t) irel->r_offset);
9858 insn = bfd_getb32 (contents + laddr);
9860 /* Get the value of the symbol referred to by the reloc. */
9861 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9864 || foff < -CONSERVATIVE_16BIT_S1
9865 || foff >= CONSERVATIVE_16BIT_S1)
9868 /* Relax to bgezal rt, label ; 17_PCREL
9869 or bltzal rt, label ; 17_PCREL */
9871 /* Convert to complimentary conditional call. */
9872 insn = CONVERT_CONDITION_CALL (insn);
9874 /* For simplicity of coding, we are going to modify the section
9875 contents, the section relocs, and the BFD symbol table. We
9876 must tell the rest of the code not to free up this
9877 information. It would be possible to instead create a table
9878 of changes which have to be made, as is done in coff-mips.c;
9879 that would be more work, but would require less memory when
9880 the linker is run. */
9882 /* Clean unnessary relocations. */
9884 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9886 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9887 R_NDS32_17_PCREL_RELA, laddr);
9888 if (cond_irelfn != irelend)
9889 cond_irelfn->r_info =
9890 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9892 /* Replace the long call with a bgezal. */
9893 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9894 R_NDS32_17_PCREL_RELA);
9895 irel->r_addend = i1_irelfn->r_addend;
9897 bfd_putb32 (insn, contents + irel->r_offset);
9903 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9906 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9907 Elf_Internal_Rela *internal_relocs, int *insn_len,
9908 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9909 Elf_Internal_Shdr *symtab_hdr)
9911 /* There are 3 variations for LONGCALL3
9912 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9913 bltz rt, $1 ; LONGCALL3
9914 sethi ta, hi20(symbol) ; HI20
9915 ori ta, ta, lo12(symbol) ; LO12S0
9919 case 4-4-4-4; 16-bit off, optimize don't care
9920 bltz rt, $1 ; LONGCALL3
9921 sethi ta, hi20(symbol) ; HI20
9922 ori ta, ta, lo12(symbol) ; LO12S0
9926 case 4-4-4-4; 16-bit on, optimize for speed
9927 bltz rt, $1 ; LONGCALL3
9928 sethi ta, hi20(symbol) ; HI20
9929 ori ta, ta, lo12(symbol) ; LO12S0
9933 /* Get the reloc for the address from which the register is
9934 being loaded. This reloc will tell us which function is
9935 actually being called. */
9938 int seq_len; /* Original length of instruction sequence. */
9940 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9941 bfd_signed_vma foff;
9944 irelend = internal_relocs + sec->reloc_count;
9945 seq_len = GET_SEQ_LEN (irel->r_addend);
9946 laddr = irel->r_offset;
9947 *insn_len = seq_len;
9950 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9951 R_NDS32_HI20_RELA, laddr + 4);
9953 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9954 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9956 if (hi_irelfn == irelend || lo_irelfn == irelend)
9958 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9959 (uint64_t) irel->r_offset);
9963 /* Get the value of the symbol referred to by the reloc. */
9964 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9967 || foff < -CONSERVATIVE_24BIT_S1
9968 || foff >= CONSERVATIVE_24BIT_S1)
9971 insn = bfd_getb32 (contents + laddr);
9972 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9974 /* Relax to bgezal rt, label ; 17_PCREL
9975 or bltzal rt, label ; 17_PCREL */
9977 /* Convert to complimentary conditional call. */
9978 insn = CONVERT_CONDITION_CALL (insn);
9979 bfd_putb32 (insn, contents + irel->r_offset);
9983 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9985 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9987 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9990 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9991 R_NDS32_17_PCREL_RELA, laddr);
9992 if (cond_irelfn != irelend)
9994 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9995 R_NDS32_17_PCREL_RELA);
9996 cond_irelfn->r_addend = hi_irelfn->r_addend;
10001 insn16 = NDS32_NOP16;
10002 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10003 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10005 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10009 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10011 /* Relax to the following instruction sequence
10012 bltz rt, $1 ; LONGCALL2
10013 jal symbol ; 25_PCREL
10017 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10019 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10020 R_NDS32_25_PCREL_RELA);
10022 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10024 lo_irelfn->r_info =
10025 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10029 insn16 = NDS32_NOP16;
10030 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10031 lo_irelfn->r_info =
10032 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10033 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10040 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
10043 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10044 Elf_Internal_Rela *internal_relocs, int *insn_len,
10045 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10046 Elf_Internal_Shdr *symtab_hdr)
10048 /* There are 3 variations for LONGJUMP1
10049 case 4-4-2; 16-bit bit on, optimize off or optimize for space
10050 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10051 ori ta, ta, lo12(symbol) ; LO12S0
10054 case 4-4-4; 16-bit off, optimize don't care
10055 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10056 ori ta, ta, lo12(symbol) ; LO12S0
10059 case 4-4-4; 16-bit on, optimize for speed
10060 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10061 ori ta, ta, lo12(symbol) ; LO12S0
10064 /* Get the reloc for the address from which the register is
10065 being loaded. This reloc will tell us which function is
10066 actually being called. */
10069 int seq_len; /* Original length of instruction sequence. */
10070 int insn16_on; /* 16-bit on/off. */
10072 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10073 bfd_signed_vma foff;
10075 unsigned long reloc;
10077 irelend = internal_relocs + sec->reloc_count;
10078 seq_len = GET_SEQ_LEN (irel->r_addend);
10079 laddr = irel->r_offset;
10080 *insn_len = seq_len;
10081 insn16_on = IS_16BIT_ON (irel->r_addend);
10084 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10085 R_NDS32_HI20_RELA, laddr);
10087 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10088 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10089 if (hi_irelfn == irelend || lo_irelfn == irelend)
10091 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10092 (uint64_t) irel->r_offset);
10096 /* Get the value of the symbol referred to by the reloc. */
10097 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10100 || foff >= CONSERVATIVE_24BIT_S1
10101 || foff < -CONSERVATIVE_24BIT_S1)
10105 && foff >= -ACCURATE_8BIT_S1
10106 && foff < ACCURATE_8BIT_S1
10107 && (seq_len & 0x2))
10110 /* 16-bit on, but not optimized for speed. */
10111 reloc = R_NDS32_9_PCREL_RELA;
10113 bfd_putb16 (insn16, contents + irel->r_offset);
10116 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10121 reloc = R_NDS32_25_PCREL_RELA;
10123 bfd_putb32 (insn, contents + irel->r_offset);
10126 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10127 irel->r_addend = 0;
10130 hi_irelfn->r_info =
10131 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10132 lo_irelfn->r_info =
10133 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10135 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10137 insn16 = NDS32_NOP16;
10138 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10139 lo_irelfn->r_info =
10140 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10142 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10148 /* Revert condition branch. This function does not check if the input
10149 instruction is condition branch or not. */
10152 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10153 uint16_t *re_insn16, uint32_t *re_insn)
10155 uint32_t comp_insn = 0;
10156 uint16_t comp_insn16 = 0;
10160 if (N32_OP6 (insn) == N32_OP6_BR1)
10163 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10164 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10166 /* Insn can be contracted to 16-bit implied r5. */
10168 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10169 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10172 else if (N32_OP6 (insn) == N32_OP6_BR3)
10174 /* bnec $ta, imm11, label. */
10175 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10179 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10180 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10181 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10183 if (N32_IS_RT3 (insn))
10185 /* Insn can be contracted to 16-bit. */
10187 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10188 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10190 else if (N32_RT5 (insn) == REG_R15)
10192 /* Insn can be contracted to 16-bit. */
10194 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10201 switch ((insn16 & 0xf000) >> 12)
10204 /* beqz38 or bnez38 */
10205 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10206 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10207 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10211 /* beqs38 or bnes38 */
10212 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10213 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10214 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10219 /* beqzS8 or bnezS8 */
10220 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10221 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10222 comp_insn |= REG_R15 << 20;
10229 if (comp_insn && re_insn)
10230 *re_insn = comp_insn;
10231 if (comp_insn16 && re_insn16)
10232 *re_insn16 = comp_insn16;
10235 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
10238 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10239 Elf_Internal_Rela *internal_relocs, int *insn_len,
10240 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10241 Elf_Internal_Shdr *symtab_hdr)
10243 /* There are 3 variations for LONGJUMP2
10244 case 2-4; 1st insn convertible, 16-bit on,
10245 optimize off or optimize for space
10246 bnes38 rt, ra, $1 ; LONGJUMP2
10250 case 4-4; 1st insn not convertible
10251 bne rt, ra, $1 ; LONGJUMP2
10255 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10256 bne rt, ra, $1 ; LONGJUMP2
10260 /* Get the reloc for the address from which the register is
10261 being loaded. This reloc will tell us which function is
10262 actually being called. */
10265 int seq_len; /* Original length of instruction sequence. */
10266 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10269 bfd_signed_vma foff;
10270 uint32_t insn, re_insn = 0;
10271 uint16_t insn16, re_insn16 = 0;
10272 unsigned long reloc, cond_reloc;
10274 enum elf_nds32_reloc_type checked_types[] =
10275 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10277 irelend = internal_relocs + sec->reloc_count;
10278 seq_len = GET_SEQ_LEN (irel->r_addend);
10279 laddr = irel->r_offset;
10280 *insn_len = seq_len;
10281 first_size = (seq_len == 6) ? 2 : 4;
10284 find_relocs_at_address_addr (irel, internal_relocs,
10285 irelend, R_NDS32_25_PCREL_RELA,
10286 laddr + first_size);
10288 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10291 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10292 checked_types[i], laddr);
10293 if (cond_irelfn != irelend)
10297 if (i2_irelfn == irelend || cond_irelfn == irelend)
10299 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10300 (uint64_t) irel->r_offset);
10304 /* Get the value of the symbol referred to by the reloc. */
10305 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10307 || foff < -CONSERVATIVE_16BIT_S1
10308 || foff >= CONSERVATIVE_16BIT_S1)
10311 /* Get the all corresponding instructions. */
10312 if (first_size == 4)
10314 insn = bfd_getb32 (contents + laddr);
10315 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10319 insn16 = bfd_getb16 (contents + laddr);
10320 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10323 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10324 && foff < ACCURATE_8BIT_S1 - first_size)
10326 if (first_size == 4)
10328 /* Don't convert it to 16-bit now, keep this as relaxable for
10329 ``label reloc; INSN16''. */
10331 /* Save comp_insn32 to buffer. */
10332 bfd_putb32 (re_insn, contents + irel->r_offset);
10334 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10335 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10336 cond_reloc = R_NDS32_INSN16;
10340 bfd_putb16 (re_insn16, contents + irel->r_offset);
10342 reloc = R_NDS32_9_PCREL_RELA;
10343 cond_reloc = R_NDS32_NONE;
10346 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10347 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10348 && foff < ACCURATE_14BIT_S1 - first_size))
10350 /* beqs label ; 15_PCREL */
10351 bfd_putb32 (re_insn, contents + irel->r_offset);
10353 reloc = R_NDS32_15_PCREL_RELA;
10354 cond_reloc = R_NDS32_NONE;
10356 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10357 && foff >= -CONSERVATIVE_16BIT_S1
10358 && foff < CONSERVATIVE_16BIT_S1)
10360 /* beqz label ; 17_PCREL */
10361 bfd_putb32 (re_insn, contents + irel->r_offset);
10363 reloc = R_NDS32_17_PCREL_RELA;
10364 cond_reloc = R_NDS32_NONE;
10369 /* Set all relocations. */
10370 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10371 irel->r_addend = i2_irelfn->r_addend;
10373 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10375 cond_irelfn->r_addend = 0;
10377 if ((seq_len ^ *insn_len ) & 0x2)
10379 insn16 = NDS32_NOP16;
10380 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10381 i2_irelfn->r_offset = 4;
10382 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10384 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10388 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10393 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
10396 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10397 Elf_Internal_Rela *internal_relocs, int *insn_len,
10398 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10399 Elf_Internal_Shdr *symtab_hdr)
10401 /* There are 5 variations for LONGJUMP3
10402 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10403 optimize off or optimize for space
10404 bnes38 rt, ra, $1 ; LONGJUMP3
10405 sethi ta, hi20(symbol) ; HI20
10406 ori ta, ta, lo12(symbol) ; LO12S0
10410 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10411 bnes38 rt, ra, $1 ; LONGJUMP3
10412 sethi ta, hi20(symbol) ; HI20
10413 ori ta, ta, lo12(symbol) ; LO12S0
10417 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10418 optimize off or optimize for space
10419 bne rt, ra, $1 ; LONGJUMP3
10420 sethi ta, hi20(symbol) ; HI20
10421 ori ta, ta, lo12(symbol) ; LO12S0
10425 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10426 16-bit off if no INSN16
10427 bne rt, ra, $1 ; LONGJUMP3
10428 sethi ta, hi20(symbol) ; HI20
10429 ori ta, ta, lo12(symbol) ; LO12S0
10433 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10434 16-bit off if no INSN16
10435 bne rt, ra, $1 ; LONGJUMP3
10436 sethi ta, hi20(symbol) ; HI20
10437 ori ta, ta, lo12(symbol) ; LO12S0
10441 /* Get the reloc for the address from which the register is
10442 being loaded. This reloc will tell us which function is
10443 actually being called. */
10444 enum elf_nds32_reloc_type checked_types[] =
10445 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10447 int reloc_off = 0, cond_removed = 0, convertible;
10449 int seq_len; /* Original length of instruction sequence. */
10450 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10453 bfd_signed_vma foff;
10454 uint32_t insn, re_insn = 0;
10455 uint16_t insn16, re_insn16 = 0;
10456 unsigned long reloc, cond_reloc;
10458 irelend = internal_relocs + sec->reloc_count;
10459 seq_len = GET_SEQ_LEN (irel->r_addend);
10460 laddr = irel->r_offset;
10461 *insn_len = seq_len;
10463 convertible = IS_1ST_CONVERT (irel->r_addend);
10470 /* Get all needed relocations. */
10472 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10473 R_NDS32_HI20_RELA, laddr + first_size);
10475 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10476 R_NDS32_LO12S0_ORI_RELA,
10477 laddr + first_size + 4);
10479 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10482 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10483 checked_types[i], laddr);
10484 if (cond_irelfn != irelend)
10488 if (hi_irelfn == irelend
10489 || lo_irelfn == irelend
10490 || cond_irelfn == irelend)
10492 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10493 (uint64_t) irel->r_offset);
10497 /* Get the value of the symbol referred to by the reloc. */
10498 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10501 || foff < -CONSERVATIVE_24BIT_S1
10502 || foff >= CONSERVATIVE_24BIT_S1)
10505 /* Get the all corresponding instructions. */
10506 if (first_size == 4)
10508 insn = bfd_getb32 (contents + laddr);
10509 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10513 insn16 = bfd_getb16 (contents + laddr);
10514 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10517 /* For simplicity of coding, we are going to modify the section
10518 contents, the section relocs, and the BFD symbol table. We
10519 must tell the rest of the code not to free up this
10520 information. It would be possible to instead create a table
10521 of changes which have to be made, as is done in coff-mips.c;
10522 that would be more work, but would require less memory when
10523 the linker is run. */
10526 && foff >= -ACCURATE_8BIT_S1 - first_size
10527 && foff < ACCURATE_8BIT_S1 - first_size)
10529 if (!(seq_len & 0x2))
10531 /* Don't convert it to 16-bit now, keep this as relaxable
10532 for ``label reloc; INSN1a''6. */
10533 /* Save comp_insn32 to buffer. */
10534 bfd_putb32 (re_insn, contents + irel->r_offset);
10536 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10537 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10538 cond_reloc = R_NDS32_INSN16;
10542 /* Not optimize for speed; convert sequence to 16-bit. */
10543 /* Save comp_insn16 to buffer. */
10544 bfd_putb16 (re_insn16, contents + irel->r_offset);
10546 reloc = R_NDS32_9_PCREL_RELA;
10547 cond_reloc = R_NDS32_NONE;
10551 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10552 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10553 && foff < ACCURATE_14BIT_S1 - first_size))
10555 /* beqs label ; 15_PCREL */
10556 bfd_putb32 (re_insn, contents + irel->r_offset);
10558 reloc = R_NDS32_15_PCREL_RELA;
10559 cond_reloc = R_NDS32_NONE;
10562 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10563 && foff >= -CONSERVATIVE_16BIT_S1
10564 && foff < CONSERVATIVE_16BIT_S1)
10566 /* beqz label ; 17_PCREL */
10567 bfd_putb32 (re_insn, contents + irel->r_offset);
10569 reloc = R_NDS32_17_PCREL_RELA;
10570 cond_reloc = R_NDS32_NONE;
10573 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10574 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10576 /* Relax to one of the following 3 variations
10578 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10580 bnes38 rt, $1 ; LONGJUMP2
10584 case 4-4; 1st insn not convertible, others don't care
10585 bne rt, ra, $1 ; LONGJUMP2
10589 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10590 bne rt, ra, $1 ; LONGJUMP2
10594 /* Offset for first instruction. */
10596 /* Use j label as second instruction. */
10597 *insn_len = 4 + first_size;
10599 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10600 reloc = R_NDS32_LONGJUMP2;
10601 cond_reloc = R_NDS32_25_PLTREL;
10606 if (cond_removed == 1)
10608 /* Set all relocations. */
10609 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10610 irel->r_addend = hi_irelfn->r_addend;
10612 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10614 cond_irelfn->r_addend = 0;
10615 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10620 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10621 irel->r_addend = irel->r_addend;
10622 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10626 if ((seq_len ^ *insn_len ) & 0x2)
10628 insn16 = NDS32_NOP16;
10629 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10630 lo_irelfn->r_offset = *insn_len;
10631 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10633 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10637 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10642 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
10645 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10646 Elf_Internal_Rela *internal_relocs, int *insn_len,
10647 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10648 Elf_Internal_Shdr *symtab_hdr)
10650 /* The pattern for LONGCALL4. Support for function cse.
10651 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10652 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10653 jral ta ; PTR_RES/EMPTY/INSN16 */
10657 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10658 Elf_Internal_Rela *irelend;
10659 bfd_signed_vma foff;
10661 irelend = internal_relocs + sec->reloc_count;
10662 laddr = irel->r_offset;
10664 /* Get the reloc for the address from which the register is
10665 being loaded. This reloc will tell us which function is
10666 actually being called. */
10667 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10668 R_NDS32_HI20_RELA, laddr);
10670 if (hi_irel == irelend)
10672 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10673 (uint64_t) irel->r_offset);
10677 /* Get the value of the symbol referred to by the reloc. */
10678 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10680 /* This condition only happened when symbol is undefined. */
10682 || foff < -CONSERVATIVE_24BIT_S1
10683 || foff >= CONSERVATIVE_24BIT_S1)
10686 /* Relax to: jal symbol; 25_PCREL. */
10687 /* For simplicity of coding, we are going to modify the section
10688 contents, the section relocs, and the BFD symbol table. We
10689 must tell the rest of the code not to free up this
10690 information. It would be possible to instead create a table
10691 of changes which have to be made, as is done in coff-mips.c;
10692 that would be more work, but would require less memory when
10693 the linker is run. */
10695 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10696 R_NDS32_PTR_RESOLVED, irel->r_addend);
10697 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10698 R_NDS32_EMPTY, irel->r_addend);
10700 if (ptr_irel == irelend || em_irel == irelend)
10702 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10703 (uint64_t) irel->r_offset);
10706 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10707 insn = bfd_getb32 (contents + irel->r_addend);
10708 if (insn & 0x80000000)
10711 /* Replace the long call with a jal. */
10712 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10713 R_NDS32_25_PCREL_RELA);
10714 ptr_irel->r_addend = 1;
10716 /* We don't resolve this here but resolve it in relocate_section. */
10718 bfd_putb32 (insn, contents + em_irel->r_offset);
10721 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10723 /* If there is function cse, HI20 can not remove now. */
10724 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10725 R_NDS32_LONGCALL4, laddr);
10726 if (call_irel == irelend)
10730 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10733 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10734 R_NDS32_INSN16, irel->r_addend);
10735 if (insn_irel != irelend)
10736 insn_irel->r_info =
10737 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10742 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
10745 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10746 Elf_Internal_Rela *internal_relocs, int *insn_len,
10747 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10748 Elf_Internal_Shdr *symtab_hdr)
10750 /* The pattern for LONGCALL5.
10751 bltz rt, .L1 ; LONGCALL5/17_PCREL
10752 jal symbol ; 25_PCREL
10757 Elf_Internal_Rela *cond_irel, *irelend;
10758 bfd_signed_vma foff;
10760 irelend = internal_relocs + sec->reloc_count;
10761 laddr = irel->r_offset;
10762 insn = bfd_getb32 (contents + laddr);
10764 /* Get the reloc for the address from which the register is
10765 being loaded. This reloc will tell us which function is
10766 actually being called. */
10768 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10769 R_NDS32_25_PCREL_RELA, irel->r_addend);
10770 if (cond_irel == irelend)
10772 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10773 (uint64_t) irel->r_offset);
10777 /* Get the value of the symbol referred to by the reloc. */
10778 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10781 || foff < -CONSERVATIVE_16BIT_S1
10782 || foff >= CONSERVATIVE_16BIT_S1)
10785 /* Relax to bgezal rt, label ; 17_PCREL
10786 or bltzal rt, label ; 17_PCREL. */
10788 /* Convert to complimentary conditional call. */
10789 insn = CONVERT_CONDITION_CALL (insn);
10791 /* For simplicity of coding, we are going to modify the section
10792 contents, the section relocs, and the BFD symbol table. We
10793 must tell the rest of the code not to free up this
10794 information. It would be possible to instead create a table
10795 of changes which have to be made, as is done in coff-mips.c;
10796 that would be more work, but would require less memory when
10797 the linker is run. */
10799 /* Modify relocation and contents. */
10800 cond_irel->r_info =
10801 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10803 /* Replace the long call with a bgezal. */
10804 bfd_putb32 (insn, contents + cond_irel->r_offset);
10807 /* Clean unnessary relocations. */
10808 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10810 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10811 R_NDS32_17_PCREL_RELA, laddr);
10812 cond_irel->r_info =
10813 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10818 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
10821 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10822 Elf_Internal_Rela *internal_relocs, int *insn_len,
10823 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10824 Elf_Internal_Shdr *symtab_hdr)
10826 /* The pattern for LONGCALL6.
10827 bltz rt, .L1 ; LONGCALL6/17_PCREL
10828 sethi ta, hi20(symbol) ; HI20/PTR
10829 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10830 jral ta ; PTR_RES/EMPTY/INSN16
10835 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10836 bfd_signed_vma foff;
10838 irelend = internal_relocs + sec->reloc_count;
10839 laddr = irel->r_offset;
10841 /* Get the reloc for the address from which the register is
10842 being loaded. This reloc will tell us which function is
10843 actually being called. */
10844 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10845 R_NDS32_EMPTY, irel->r_addend);
10847 if (em_irel == irelend)
10849 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10850 (uint64_t) irel->r_offset);
10854 /* Get the value of the symbol referred to by the reloc. */
10855 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10858 || foff < -CONSERVATIVE_24BIT_S1
10859 || foff >= CONSERVATIVE_24BIT_S1)
10862 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10863 insn = bfd_getb32 (contents + irel->r_addend);
10864 if (insn & 0x80000000)
10867 insn = bfd_getb32 (contents + laddr);
10868 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10870 /* Relax to bgezal rt, label ; 17_PCREL
10871 or bltzal rt, label ; 17_PCREL. */
10873 /* Convert to complimentary conditional call. */
10875 insn = CONVERT_CONDITION_CALL (insn);
10876 bfd_putb32 (insn, contents + em_irel->r_offset);
10879 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10881 /* Set resolved relocation. */
10883 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10884 R_NDS32_PTR_RESOLVED, irel->r_addend);
10885 if (cond_irel == irelend)
10887 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10888 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10891 cond_irel->r_addend = 1;
10893 /* Clear relocations. */
10896 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10899 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10900 R_NDS32_17_PCREL_RELA, laddr);
10901 if (cond_irel != irelend)
10902 cond_irel->r_info =
10903 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10906 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10907 R_NDS32_INSN16, irel->r_addend);
10908 if (cond_irel != irelend)
10909 cond_irel->r_info =
10910 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10913 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10915 /* Relax to the following instruction sequence
10916 bltz rt, .L1 ; LONGCALL2/17_PCREL
10917 jal symbol ; 25_PCREL/PTR_RES
10920 /* Convert instruction. */
10922 bfd_putb32 (insn, contents + em_irel->r_offset);
10924 /* Convert relocations. */
10925 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10926 R_NDS32_25_PCREL_RELA);
10928 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10930 /* Set resolved relocation. */
10932 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10933 R_NDS32_PTR_RESOLVED, irel->r_addend);
10934 if (cond_irel == irelend)
10936 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10937 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10940 cond_irel->r_addend = 1;
10943 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10944 R_NDS32_INSN16, irel->r_addend);
10945 if (cond_irel != irelend)
10946 cond_irel->r_info =
10947 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10952 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10955 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10956 Elf_Internal_Rela *internal_relocs, int *insn_len,
10957 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10958 Elf_Internal_Shdr *symtab_hdr)
10960 /* The pattern for LONGJUMP4.
10961 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10962 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10963 jr ta ; PTR_RES/INSN16/EMPTY */
10966 int seq_len; /* Original length of instruction sequence. */
10968 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10969 bfd_signed_vma foff;
10971 irelend = internal_relocs + sec->reloc_count;
10972 seq_len = GET_SEQ_LEN (irel->r_addend);
10973 laddr = irel->r_offset;
10974 *insn_len = seq_len;
10976 /* Get the reloc for the address from which the register is
10977 being loaded. This reloc will tell us which function is
10978 actually being called. */
10980 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10981 R_NDS32_HI20_RELA, laddr);
10983 if (hi_irel == irelend)
10985 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10986 (uint64_t) irel->r_offset);
10990 /* Get the value of the symbol referred to by the reloc. */
10991 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10994 || foff >= CONSERVATIVE_24BIT_S1
10995 || foff < -CONSERVATIVE_24BIT_S1)
10998 /* Convert it to "j label", it may be converted to j8 in the final
10999 pass of relaxation. Therefore, we do not consider this currently. */
11000 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11001 R_NDS32_PTR_RESOLVED, irel->r_addend);
11002 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11003 R_NDS32_EMPTY, irel->r_addend);
11005 if (ptr_irel == irelend || em_irel == irelend)
11007 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
11008 (uint64_t) irel->r_offset);
11013 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11014 ptr_irel->r_addend = 1;
11016 /* Write instruction. */
11018 bfd_putb32 (insn, contents + em_irel->r_offset);
11020 /* Clear relocations. */
11021 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11023 /* If there is function cse, HI20 can not remove now. */
11024 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11025 R_NDS32_LONGJUMP4, laddr);
11026 if (call_irel == irelend)
11030 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11036 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
11039 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11040 Elf_Internal_Rela *internal_relocs, int *insn_len,
11041 int *seq_len, bfd_byte *contents,
11042 Elf_Internal_Sym *isymbuf,
11043 Elf_Internal_Shdr *symtab_hdr)
11045 /* There are 2 variations for LONGJUMP5
11046 case 2-4; 1st insn convertible, 16-bit on.
11047 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11048 j label ; 25_PCREL/INSN16
11051 case 4-4; 1st insn not convertible
11052 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11053 j label ; 25_PCREL/INSN16
11057 Elf_Internal_Rela *cond_irel, *irelend;
11059 bfd_signed_vma foff;
11060 uint32_t insn, re_insn = 0;
11061 uint16_t insn16, re_insn16 = 0;
11062 unsigned long reloc;
11064 enum elf_nds32_reloc_type checked_types[] =
11065 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11066 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11068 irelend = internal_relocs + sec->reloc_count;
11069 laddr = irel->r_offset;
11071 /* Get the reloc for the address from which the register is
11072 being loaded. This reloc will tell us which function is
11073 actually being called. */
11076 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11077 R_NDS32_25_PCREL_RELA, irel->r_addend);
11078 if (cond_irel == irelend)
11080 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11081 (uint64_t) irel->r_offset);
11085 /* Get the value of the symbol referred to by the reloc. */
11086 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11089 || foff < -CONSERVATIVE_16BIT_S1
11090 || foff >= CONSERVATIVE_16BIT_S1)
11093 /* Get the all corresponding instructions. */
11094 insn = bfd_getb32 (contents + laddr);
11095 /* Check instruction size. */
11096 if (insn & 0x80000000)
11099 insn16 = insn >> 16;
11100 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11103 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11105 if (N32_OP6 (re_insn) == N32_OP6_BR1
11106 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11108 /* beqs label ; 15_PCREL. */
11109 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11110 reloc = R_NDS32_15_PCREL_RELA;
11112 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11113 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11115 /* beqz label ; 17_PCREL. */
11116 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11117 reloc = R_NDS32_17_PCREL_RELA;
11119 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11120 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11122 /* beqc label ; 9_PCREL. */
11123 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11124 reloc = R_NDS32_WORD_9_PCREL_RELA;
11129 /* Set all relocations. */
11130 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11132 /* Clean relocations. */
11133 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11134 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11136 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11137 checked_types[i], laddr);
11138 if (cond_irel != irelend)
11141 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11143 /* If the branch instruction is 2 byte, it cannot remove
11144 directly. Only convert it to nop16 and remove it after
11145 checking alignment issue. */
11146 insn16 = NDS32_NOP16;
11147 bfd_putb16 (insn16, contents + laddr);
11148 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11151 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11160 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
11163 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11164 Elf_Internal_Rela *internal_relocs, int *insn_len,
11165 int *seq_len, bfd_byte *contents,
11166 Elf_Internal_Sym *isymbuf,
11167 Elf_Internal_Shdr *symtab_hdr)
11169 /* There are 5 variations for LONGJUMP6
11170 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11171 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11172 sethi ta, hi20(symbol) ; HI20/PTR
11173 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11174 jr ta ; PTR_RES/INSN16/EMPTY
11177 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11178 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11179 sethi ta, hi20(symbol) ; HI20/PTR
11180 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11181 jr ta ; PTR_RES/INSN16/EMPTY
11184 enum elf_nds32_reloc_type checked_types[] =
11185 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11186 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11188 int reloc_off = 0, cond_removed = 0;
11190 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11192 bfd_signed_vma foff;
11193 uint32_t insn, re_insn = 0;
11194 uint16_t insn16, re_insn16 = 0;
11195 unsigned long reloc;
11197 irelend = internal_relocs + sec->reloc_count;
11198 laddr = irel->r_offset;
11200 /* Get the reloc for the address from which the register is
11201 being loaded. This reloc will tell us which function is
11202 actually being called. */
11203 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11204 R_NDS32_EMPTY, irel->r_addend);
11206 if (em_irel == irelend)
11208 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11209 (uint64_t) irel->r_offset);
11213 /* Get the value of the symbol referred to by the reloc. */
11214 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11217 || foff < -CONSERVATIVE_24BIT_S1
11218 || foff >= CONSERVATIVE_24BIT_S1)
11221 insn = bfd_getb32 (contents + laddr);
11222 /* Check instruction size. */
11223 if (insn & 0x80000000)
11226 insn16 = insn >> 16;
11227 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11230 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11232 /* For simplicity of coding, we are going to modify the section
11233 contents, the section relocs, and the BFD symbol table. We
11234 must tell the rest of the code not to free up this
11235 information. It would be possible to instead create a table
11236 of changes which have to be made, as is done in coff-mips.c;
11237 that would be more work, but would require less memory when
11238 the linker is run. */
11240 if (N32_OP6 (re_insn) == N32_OP6_BR1
11241 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11243 /* beqs label ; 15_PCREL. */
11244 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11245 reloc = R_NDS32_15_PCREL_RELA;
11248 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11249 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11251 /* beqz label ; 17_PCREL. */
11252 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11253 reloc = R_NDS32_17_PCREL_RELA;
11256 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11257 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11259 /* Relax to one of the following 2 variations
11261 case 2-4; 1st insn convertible, 16-bit on.
11262 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11263 j label ; 25_PCREL/INSN16
11266 case 4-4; 1st insn not convertible
11267 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11268 j label ; 25_PCREL/INSN16
11271 /* Use j label as second instruction. */
11273 reloc = R_NDS32_25_PCREL_RELA;
11274 bfd_putb32 (insn, contents + em_irel->r_offset);
11279 /* Set all relocations. */
11280 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11283 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11284 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11285 cond_irel->r_addend = 1;
11287 /* Use INSN16 of first branch instruction to distinguish if keeping
11288 INSN16 of final instruction or not. */
11289 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11290 R_NDS32_INSN16, irel->r_offset);
11291 if (insn_irel == irelend)
11293 /* Clean the final INSN16. */
11295 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11296 R_NDS32_INSN16, em_irel->r_offset);
11297 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11301 if (cond_removed == 1)
11305 /* Clear relocations. */
11306 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11308 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11311 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11312 checked_types[i], laddr);
11313 if (cond_irel != irelend)
11316 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11318 /* If the branch instruction is 2 byte, it cannot remove
11319 directly. Only convert it to nop16 and remove it after
11320 checking alignment issue. */
11321 insn16 = NDS32_NOP16;
11322 bfd_putb16 (insn16, contents + laddr);
11323 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11326 cond_irel->r_info =
11327 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11333 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11334 R_NDS32_LONGJUMP5);
11340 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
11343 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11344 Elf_Internal_Rela *internal_relocs, int *insn_len,
11345 int *seq_len, bfd_byte *contents,
11346 Elf_Internal_Sym *isymbuf,
11347 Elf_Internal_Shdr *symtab_hdr)
11349 /* There are 2 variations for LONGJUMP5
11350 case 2-4; 1st insn convertible, 16-bit on.
11351 movi55 ta, imm11 ; LONGJUMP7/INSN16
11352 beq rt, ta, label ; 15_PCREL
11354 case 4-4; 1st insn not convertible
11355 movi55 ta, imm11 ; LONGJUMP7/INSN16
11356 beq rt, ta, label ; 15_PCREL */
11359 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
11360 bfd_signed_vma foff;
11361 uint32_t insn, re_insn = 0;
11365 irelend = internal_relocs + sec->reloc_count;
11366 laddr = irel->r_offset;
11368 /* Get the reloc for the address from which the register is
11369 being loaded. This reloc will tell us which function is
11370 actually being called. */
11373 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11374 R_NDS32_15_PCREL_RELA, irel->r_addend);
11375 if (cond_irel == irelend)
11377 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11378 (uint64_t) irel->r_offset);
11382 /* Get the value of the symbol referred to by the reloc. */
11383 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11386 || foff < -CONSERVATIVE_8BIT_S1
11387 || foff >= CONSERVATIVE_8BIT_S1)
11390 /* Get the first instruction for its size. */
11391 insn = bfd_getb32 (contents + laddr);
11392 if (insn & 0x80000000)
11395 /* Get the immediate from movi55. */
11396 imm11 = N16_IMM5S (insn >> 16);
11400 /* Get the immediate from movi. */
11401 imm11 = N32_IMM20S (insn);
11404 /* Get the branch instruction. */
11405 insn = bfd_getb32 (contents + irel->r_addend);
11406 /* Convert instruction to BR3. */
11407 if ((insn >> 14) & 0x1)
11408 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11410 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11412 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11414 /* Set all relocations. */
11415 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11416 R_NDS32_WORD_9_PCREL_RELA);
11418 /* Clean relocations. */
11419 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11420 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11421 R_NDS32_INSN16, irel->r_offset);
11422 if (insn_irel != irelend)
11426 /* If the first insntruction is 16bit, convert it to nop16. */
11427 insn16 = NDS32_NOP16;
11428 bfd_putb16 (insn16, contents + laddr);
11429 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11432 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11440 /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11441 for each relax round. But the gp may changed dramatically and then cause
11442 the truncated to fit errors for the the converted gp instructions.
11443 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11446 nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11447 Elf_Internal_Rela *irel, bfd_boolean *again,
11449 struct elf_nds32_link_hash_table *table,
11450 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11454 static bfd_boolean sec_pass = FALSE;
11455 static asection *first_sec = NULL, *sym_sec;
11456 /* Record the number of instructions which may be removed. */
11457 static int count = 0, record_count;
11458 Elf_Internal_Sym *isym;
11459 struct elf_link_hash_entry *h = NULL;
11461 unsigned long r_symndx;
11462 bfd *abfd = sec->owner;
11463 static bfd_vma record_sda = 0;
11464 int sda_offset = 0;
11466 /* Force doing relaxation when hyper-relax is high. */
11467 if (table->hyper_relax == 2)
11470 /* Do not relax the load/store patterns for the first
11476 else if (first_sec == sec)
11478 record_count = count;
11489 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11490 DATA_SEGMENT_ALIGN size in the linker script may make it
11491 get even bigger. */
11492 if (record_sda == 0)
11493 record_sda = local_sda;
11494 else if (local_sda > record_sda)
11495 sda_offset = local_sda - record_sda;
11497 /* Assume the instruction will be removed in the best case. */
11500 /* We record the offset to gp for each symbol, and then check
11501 if it is changed dramatically after relaxing.
11502 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11503 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11504 r_symndx = ELF32_R_SYM (irel->r_info);
11505 if (r_symndx >= symtab_hdr->sh_info)
11507 /* Global symbols. */
11508 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11509 h = elf_sym_hashes (abfd)[indx];
11510 sym_sec = h->root.u.def.section;
11511 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11512 || bfd_is_abs_section (sym_sec))
11514 /* Forbid doing relaxation when hyper-relax is low. */
11515 if (table->hyper_relax == 0)
11518 offset_to_gp = *access_addr - local_sda;
11519 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11520 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11521 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11522 < abs (offset_to_gp) - sda_offset)
11524 /* This may cause the error, so we reserve the
11525 safe enough size for relaxing. */
11526 if (*access_addr >= local_sda)
11527 *access_addr += (record_count * 4);
11529 *access_addr -= (record_count * 4);
11536 /* Local symbols. */
11537 if (!elf32_nds32_allocate_local_sym_info (abfd))
11539 isym = isymbuf + r_symndx;
11541 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11542 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11544 /* Forbid doing relaxation when hyper-relax is low. */
11545 if (table->hyper_relax == 0)
11548 offset_to_gp = *access_addr - local_sda;
11549 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11550 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11551 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11552 < abs (offset_to_gp) - sda_offset)
11554 /* This may cause the error, so we reserve the
11555 safe enough size for relaxing. */
11556 if (*access_addr >= local_sda)
11557 *access_addr += (record_count * 4);
11559 *access_addr -= (record_count * 4);
11568 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11570 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
11573 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11574 asection *sec, Elf_Internal_Rela *irel,
11575 Elf_Internal_Rela *internal_relocs, int *insn_len,
11576 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11577 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11578 struct elf_nds32_link_hash_table *table)
11580 int eliminate_sethi = 0, range_type;
11582 bfd_vma local_sda, laddr;
11583 int seq_len; /* Original length of instruction sequence. */
11585 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11586 bfd_vma access_addr = 0;
11587 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11588 struct elf_link_hash_entry *h = NULL;
11590 enum elf_nds32_reloc_type checked_types[] =
11591 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11592 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11593 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11594 R_NDS32_TLS_LE_HI20
11597 irelend = internal_relocs + sec->reloc_count;
11598 seq_len = GET_SEQ_LEN (irel->r_addend);
11599 laddr = irel->r_offset;
11600 *insn_len = seq_len;
11602 /* Get the high part relocation. */
11603 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11605 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11606 checked_types[i], laddr);
11607 if (hi_irelfn != irelend)
11611 if (hi_irelfn == irelend)
11613 /* Not R_NDS32_HI20_RELA. */
11615 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11616 (uint64_t) irel->r_offset);
11620 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11621 nds32_elf_final_sda_base (sec->output_section->owner,
11622 link_info, &local_sda, FALSE);
11624 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11626 case R_NDS32_HI20_RELA:
11627 insn = bfd_getb32 (contents + laddr);
11629 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11631 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11633 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11634 h = elf_sym_hashes (abfd)[indx];
11638 if (range_type == NDS32_LOADSTORE_IMM
11639 && access_addr < CONSERVATIVE_20BIT
11640 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11642 eliminate_sethi = 1;
11646 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11648 eliminate_sethi = 1;
11651 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11652 NULL, FALSE, table, isymbuf, symtab_hdr))
11655 if (!load_store_relax)
11658 /* Case for set gp register. */
11659 if (N32_RT5 (insn) == REG_GP)
11662 if (range_type == NDS32_LOADSTORE_FLOAT_S
11663 || range_type == NDS32_LOADSTORE_FLOAT_D)
11665 range_l = sdata_range[0][0];
11666 range_h = sdata_range[0][1];
11670 range_l = sdata_range[1][0];
11671 range_h = sdata_range[1][1];
11679 /* Delete sethi instruction. */
11680 if (eliminate_sethi == 1
11681 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11682 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11684 hi_irelfn->r_info =
11685 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11687 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11695 /* Relax LO12 relocation for nds32_elf_relax_section. */
11698 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11699 asection *sec, Elf_Internal_Rela *irel,
11700 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11701 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11702 struct elf_nds32_link_hash_table *table)
11705 bfd_vma local_sda, laddr;
11706 unsigned long reloc;
11707 bfd_vma access_addr;
11708 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11709 Elf_Internal_Rela *irelfn = NULL, *irelend;
11710 struct elf_link_hash_entry *h = NULL;
11713 /* For SDA base relative relaxation. */
11714 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11715 &local_sda, FALSE);
11717 irelend = internal_relocs + sec->reloc_count;
11718 laddr = irel->r_offset;
11719 insn = bfd_getb32 (contents + laddr);
11721 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11724 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11726 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11728 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11729 h = elf_sym_hashes (abfd)[indx];
11733 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11734 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11736 reloc = R_NDS32_20_RELA;
11737 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11738 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11739 bfd_putb32 (insn, contents + laddr);
11743 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11745 /* Fall through. */
11747 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11748 FALSE, table, isymbuf, symtab_hdr))
11751 range_l = sdata_range[1][0];
11752 range_h = sdata_range[1][1];
11753 switch (ELF32_R_TYPE (irel->r_info))
11755 case R_NDS32_LO12S0_RELA:
11756 reloc = R_NDS32_SDA19S0_RELA;
11758 case R_NDS32_LO12S1_RELA:
11759 reloc = R_NDS32_SDA18S1_RELA;
11761 case R_NDS32_LO12S2_RELA:
11762 reloc = R_NDS32_SDA17S2_RELA;
11764 case R_NDS32_LO12S2_DP_RELA:
11765 range_l = sdata_range[0][0];
11766 range_h = sdata_range[0][1];
11767 reloc = R_NDS32_SDA12S2_DP_RELA;
11769 case R_NDS32_LO12S2_SP_RELA:
11770 range_l = sdata_range[0][0];
11771 range_h = sdata_range[0][1];
11772 reloc = R_NDS32_SDA12S2_SP_RELA;
11778 /* There are range_h and range_l because linker has to promise
11779 all sections move cross one page together. */
11780 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11781 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11782 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11784 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11786 /* Maybe we should add R_NDS32_INSN16 reloc type here
11787 or manually do some optimization. sethi can't be
11788 eliminated when updating $gp so the relative ori
11789 needs to be preserved. */
11792 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11795 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11796 bfd_putb32 (insn, contents + laddr);
11798 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11800 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11801 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11803 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11810 /* Relax PTR relocation for nds32_elf_relax_section. */
11813 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11814 Elf_Internal_Rela *internal_relocs, int *insn_len,
11815 int *seq_len, bfd_byte *contents)
11817 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11819 irelend = internal_relocs + sec->reloc_count;
11822 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11823 R_NDS32_PTR_RESOLVED, irel->r_addend);
11825 if (re_irel == irelend)
11827 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11828 (uint64_t) irel->r_offset);
11832 if (re_irel->r_addend != 1)
11835 /* Pointed target is relaxed and no longer needs this void *,
11836 change the type to NONE. */
11837 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11839 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11840 not exist, it means only count 1 and remove it directly. */
11841 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11842 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11843 R_NDS32_PTR_COUNT);
11844 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11846 if (count_irel != irelend)
11848 if (--count_irel->r_addend > 0)
11852 if (ptr_irel != irelend)
11855 /* If the PTR_COUNT is already 0, remove current instruction. */
11856 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11861 /* Relax LWC relocation for nds32_elf_relax_section. */
11864 nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11865 asection *sec, Elf_Internal_Rela *irel,
11866 Elf_Internal_Rela *internal_relocs,
11867 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11868 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11871 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11872 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11873 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11874 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11878 bfd_vma local_sda, laddr;
11879 unsigned long reloc;
11880 bfd_vma access_addr, flsi_offset;
11881 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11882 Elf_Internal_Rela *irelend, *re_irel;
11883 unsigned int opcode;
11885 irelend = internal_relocs + sec->reloc_count;
11886 laddr = irel->r_offset;
11887 insn = bfd_getb32 (contents + laddr);
11889 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11892 /* Can not do relaxation for bi format. */
11893 if ((insn & 0x1000))
11896 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11897 opcode = N32_OP6 (insn);
11898 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11899 reloc = R_NDS32_SDA12S2_SP_RELA;
11900 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11901 reloc = R_NDS32_SDA12S2_DP_RELA;
11905 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11906 R_NDS32_PTR_RESOLVED);
11907 if (re_irel == irelend)
11909 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11910 (uint64_t) irel->r_offset);
11914 /* For SDA base relative relaxation. */
11915 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11916 &local_sda, FALSE);
11917 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11918 flsi_offset = (insn & 0xfff) << 2;
11919 access_addr += flsi_offset;
11920 range_l = sdata_range[0][0];
11921 range_h = sdata_range[0][1];
11923 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11924 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11926 /* Turn flsi instruction into sda access format. */
11927 insn = (insn & 0x7ff07000) | (REG_GP << 15);
11929 /* Add relocation type to flsi. */
11930 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11931 irel->r_addend += flsi_offset;
11932 bfd_putb32 (insn, contents + re_irel->r_offset);
11934 re_irel->r_addend |= 1;
11940 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11941 Elf_Internal_Rela *internal_relocs,
11942 bfd_byte *contents,
11943 nds32_elf_blank_t **relax_blank_list,
11944 int optimize, int opt_size)
11946 /* This code block is used to adjust 4-byte alignment by relax a pair
11947 of instruction a time.
11949 It recognizes three types of relocations.
11950 1. R_NDS32_LABEL - a alignment.
11951 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11952 3. is_16bit_NOP () - remove a 16-bit instruction. */
11954 /* TODO: It seems currently implementation only support 4-byte alignment.
11955 We should handle any-alignment. */
11957 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11958 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11959 Elf_Internal_Rela rel_temp;
11960 Elf_Internal_Rela *irelend;
11964 /* Checking for branch relaxation relies on the relocations to
11965 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11966 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11967 sizeof (Elf_Internal_Rela), compar_reloc);
11969 irelend = internal_relocs + sec->reloc_count;
11971 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11972 /* FIXME: Can we generate the right order in assembler?
11973 So we don't have to swapping them here. */
11975 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11976 label_rel < irelend; label_rel++)
11978 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11981 /* Find the first reloc has the same offset with label_rel. */
11982 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11985 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11987 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11989 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11992 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11993 && insn_rel < label_rel)
11995 /* Swap the two reloc if the R_NDS32_INSN16 is
11996 before R_NDS32_LABEL. */
11997 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11998 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11999 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
12005 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12006 or higher, remove other R_NDS32_LABEL with lower alignment.
12007 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12008 then the R_NDS32_LABEL sequence is broke. */
12009 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12011 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12013 if (label_rel == NULL)
12015 if (tmp_rel->r_addend < 2)
12016 label_rel = tmp_rel;
12019 else if (tmp_rel->r_addend > 1)
12021 /* Remove all LABEL relocation from label_rel to tmp_rel
12022 including relocations with same offset as tmp_rel. */
12023 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
12025 if (tmp2_rel->r_offset == tmp_rel->r_offset)
12028 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12029 && tmp2_rel->r_addend < 2)
12031 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12037 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12039 /* A new INSN16 which can be converted, so clear label_rel. */
12040 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12042 || is_16bit_NOP (abfd, sec, tmp_rel))
12049 /* Optimized for speed and nothing has not been relaxed.
12050 It's time to align labels.
12051 We may convert a 16-bit instruction right before a label to
12052 32-bit, in order to align the label if necessary
12053 all reloc entries has been sorted by r_offset. */
12054 for (irel = internal_relocs;
12055 irel < irelend && irel->r_offset < sec->size; irel++)
12057 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12058 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12061 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12063 /* A new INSN16 found, resize the old one. */
12064 if (is_convert_32_to_16
12065 (abfd, sec, irel, internal_relocs, irelend, &insn16)
12066 || is_16bit_NOP (abfd, sec, irel))
12070 /* Previous INSN16 reloc exists, reduce its
12072 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12075 nds32_elf_write_16 (abfd, contents, insn_rel,
12076 internal_relocs, irelend, insn16);
12078 if (!insert_nds32_elf_blank_recalc_total
12079 (relax_blank_list, insn_rel->r_offset + 2, 2))
12082 else if (is_16bit_NOP (abfd, sec, insn_rel))
12084 if (!insert_nds32_elf_blank_recalc_total
12085 (relax_blank_list, insn_rel->r_offset, 2))
12089 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12091 /* Save the new one for later use. */
12095 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12098 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12100 /* Search for label. */
12101 int force_relax = 0;
12103 /* Label on 16-bit instruction or optimization
12104 needless, just reset this reloc. */
12105 insn16 = bfd_getb16 (contents + irel->r_offset);
12106 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12109 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12114 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12115 irel->r_offset, 1);
12119 /* Check if there is case which can not be aligned. */
12120 if (irel->r_addend == 2 && address & 0x2)
12125 /* Try to align this label. */
12127 if ((irel->r_addend & 0x1f) < 2)
12129 /* Check if there is a INSN16 at the same address.
12130 Label_rel always seats before insn_rel after
12133 /* Search for INSN16 at LABEL location. If INSN16 is at
12134 same location and this LABEL alignment is lower than 2,
12135 the INSN16 can be converted to 2-byte. */
12136 for (tmp_rel = irel;
12137 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12140 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12141 && (is_convert_32_to_16
12142 (abfd, sec, tmp_rel, internal_relocs,
12144 || is_16bit_NOP (abfd, sec, tmp_rel)))
12152 if (force_relax || irel->r_addend == 1 || address & 0x2)
12154 /* Label not aligned. */
12155 /* Previous reloc exists, reduce its size to 16-bit. */
12156 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12157 internal_relocs, irelend, &insn16))
12159 nds32_elf_write_16 (abfd, contents, insn_rel,
12160 internal_relocs, irelend, insn16);
12162 if (!insert_nds32_elf_blank_recalc_total
12163 (relax_blank_list, insn_rel->r_offset + 2, 2))
12166 else if (is_16bit_NOP (abfd, sec, insn_rel))
12168 if (!insert_nds32_elf_blank_recalc_total
12169 (relax_blank_list, insn_rel->r_offset, 2))
12174 /* INSN16 reloc is used. */
12180 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12181 if (insn_rel && (address & 0x2 || opt_size))
12183 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12186 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12188 if (!insert_nds32_elf_blank_recalc_total
12189 (relax_blank_list, insn_rel->r_offset + 2, 2))
12191 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12194 else if (is_16bit_NOP (abfd, sec, insn_rel))
12196 if (!insert_nds32_elf_blank_recalc_total
12197 (relax_blank_list, insn_rel->r_offset, 2))
12199 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12208 nds32_elf_relax_section (bfd *abfd, asection *sec,
12209 struct bfd_link_info *link_info, bfd_boolean *again)
12211 nds32_elf_blank_t *relax_blank_list = NULL;
12212 Elf_Internal_Shdr *symtab_hdr;
12213 Elf_Internal_Rela *internal_relocs;
12214 Elf_Internal_Rela *irel;
12215 Elf_Internal_Rela *irelend;
12216 Elf_Internal_Sym *isymbuf = NULL;
12217 bfd_byte *contents = NULL;
12218 bfd_boolean result = TRUE;
12224 /* Target dependnet option. */
12225 struct elf_nds32_link_hash_table *table;
12226 int load_store_relax;
12228 relax_blank_list = NULL;
12232 /* Nothing to do for
12233 * relocatable link or
12234 * non-relocatable section or
12235 * non-code section or
12237 * no reloc entry. */
12238 if (bfd_link_relocatable (link_info)
12239 || (sec->flags & SEC_RELOC) == 0
12240 || (sec->flags & SEC_EXCLUDE) != 0
12241 || (sec->flags & SEC_CODE) == 0
12243 || sec->reloc_count == 0)
12246 /* 09.12.11 Workaround. */
12247 /* We have to adjust align for R_NDS32_LABEL if needed.
12248 The adjust approach only can fix 2-byte align once. */
12249 if (sec->alignment_power > 2)
12252 /* Do TLS model conversion once at first. */
12253 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12255 /* The optimization type to do. */
12257 table = nds32_elf_hash_table (link_info);
12259 /* Save the first section for abs symbol relaxation.
12260 This is used for checking gp relaxation in the
12261 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12262 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12263 table, NULL, NULL);
12265 /* The begining of general relaxation. */
12267 if (is_SDA_BASE_set == 0)
12270 is_SDA_BASE_set = 1;
12271 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12273 relax_range_measurement (abfd);
12276 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12277 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12278 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12279 TRUE /* keep_memory */);
12280 if (internal_relocs == NULL)
12283 irelend = internal_relocs + sec->reloc_count;
12284 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12285 irelend, R_NDS32_RELAX_ENTRY);
12287 if (irel == irelend)
12290 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12292 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12295 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12298 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12302 load_store_relax = table->load_store_relax;
12304 /* Get symbol table and section content. */
12305 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12306 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12309 /* Do relax loop only when finalize is not done.
12310 Take care of relaxable relocs except INSN16. */
12311 for (irel = internal_relocs; irel < irelend; irel++)
12313 int seq_len; /* Original length of instruction sequence. */
12314 int insn_len = 0; /* Final length of instruction sequence. */
12315 bfd_boolean removed;
12318 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12319 && (irel->r_addend & 0x1f) >= 2)
12322 /* Relocation Types
12323 R_NDS32_LONGCALL1 53
12324 R_NDS32_LONGCALL2 54
12325 R_NDS32_LONGCALL3 55
12326 R_NDS32_LONGJUMP1 56
12327 R_NDS32_LONGJUMP2 57
12328 R_NDS32_LONGJUMP3 58
12329 R_NDS32_LOADSTORE 59 */
12330 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12331 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12332 seq_len = GET_SEQ_LEN (irel->r_addend);
12334 /* Relocation Types
12335 R_NDS32_LONGCALL4 107
12336 R_NDS32_LONGCALL5 108
12337 R_NDS32_LONGCALL6 109
12338 R_NDS32_LONGJUMP4 110
12339 R_NDS32_LONGJUMP5 111
12340 R_NDS32_LONGJUMP6 112
12341 R_NDS32_LONGJUMP7 113 */
12342 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12343 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12346 /* Relocation Types
12347 R_NDS32_LO12S0_RELA 30
12348 R_NDS32_LO12S1_RELA 29
12349 R_NDS32_LO12S2_RELA 28
12350 R_NDS32_LO12S2_SP_RELA 71
12351 R_NDS32_LO12S2_DP_RELA 70
12352 R_NDS32_GOT_LO12 46
12353 R_NDS32_GOTOFF_LO12 50
12354 R_NDS32_PLTREL_LO12 65
12355 R_NDS32_PLT_GOTREL_LO12 67
12356 R_NDS32_17IFC_PCREL_RELA 96
12357 R_NDS32_GOT_SUFF 193
12358 R_NDS32_GOTOFF_SUFF 194
12359 R_NDS32_PLT_GOT_SUFF 195
12360 R_NDS32_MULCALL_SUFF 196
12362 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12363 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12364 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12365 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12366 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12367 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12368 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12369 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12370 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12371 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12372 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12373 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12374 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12375 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12376 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12377 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12382 insn_len = seq_len;
12385 switch (ELF32_R_TYPE (irel->r_info))
12387 case R_NDS32_LONGCALL1:
12388 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12389 &insn_len, contents, isymbuf,
12392 case R_NDS32_LONGCALL2:
12393 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12394 &insn_len, contents, isymbuf,
12397 case R_NDS32_LONGCALL3:
12398 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12399 &insn_len, contents, isymbuf,
12402 case R_NDS32_LONGJUMP1:
12403 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12404 &insn_len, contents, isymbuf,
12407 case R_NDS32_LONGJUMP2:
12408 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12409 &insn_len, contents, isymbuf,
12412 case R_NDS32_LONGJUMP3:
12413 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12414 &insn_len, contents, isymbuf,
12417 case R_NDS32_LONGCALL4:
12418 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12419 &insn_len, contents, isymbuf,
12422 case R_NDS32_LONGCALL5:
12423 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12424 &insn_len, contents, isymbuf,
12427 case R_NDS32_LONGCALL6:
12428 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12429 &insn_len, contents, isymbuf,
12432 case R_NDS32_LONGJUMP4:
12433 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12434 &insn_len, contents, isymbuf,
12437 case R_NDS32_LONGJUMP5:
12438 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12439 &insn_len, &seq_len, contents,
12440 isymbuf, symtab_hdr);
12442 case R_NDS32_LONGJUMP6:
12443 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12444 &insn_len, &seq_len, contents,
12445 isymbuf, symtab_hdr);
12447 case R_NDS32_LONGJUMP7:
12448 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12449 &insn_len, &seq_len, contents,
12450 isymbuf, symtab_hdr);
12452 case R_NDS32_LOADSTORE:
12453 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12454 internal_relocs, &insn_len,
12455 contents, isymbuf, symtab_hdr,
12456 load_store_relax, table);
12458 case R_NDS32_LO12S0_RELA:
12459 case R_NDS32_LO12S1_RELA:
12460 case R_NDS32_LO12S2_RELA:
12461 case R_NDS32_LO12S2_DP_RELA:
12462 case R_NDS32_LO12S2_SP_RELA:
12463 /* Relax for low part. */
12464 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12465 contents, isymbuf, symtab_hdr, table);
12467 /* It is impossible to delete blank, so just continue. */
12470 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12471 &insn_len, &seq_len, contents);
12474 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12475 contents, isymbuf, symtab_hdr, again);
12477 case R_NDS32_GOT_LO12:
12478 case R_NDS32_GOTOFF_LO12:
12479 case R_NDS32_PLTREL_LO12:
12480 case R_NDS32_PLT_GOTREL_LO12:
12481 case R_NDS32_GOTPC_LO12:
12482 case R_NDS32_TLS_LE_LO12:
12483 case R_NDS32_TLS_LE_ADD:
12484 case R_NDS32_TLS_LE_LS:
12485 case R_NDS32_PLT_GOT_SUFF:
12486 case R_NDS32_GOT_SUFF:
12487 case R_NDS32_GOTOFF_SUFF:
12493 if (removed && seq_len - insn_len > 0)
12495 if (!insert_nds32_elf_blank
12496 (&relax_blank_list, irel->r_offset + insn_len,
12497 seq_len - insn_len))
12503 calc_nds32_blank_total (relax_blank_list);
12505 if (table->relax_fp_as_gp)
12507 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12513 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12521 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12522 &relax_blank_list, optimize, opt_size))
12526 /* It doesn't matter optimize_for_space_no_align anymore.
12527 If object file is assembled with flag '-Os',
12528 the we don't adjust jump-destination on 4-byte boundary. */
12530 if (relax_blank_list)
12532 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12533 relax_blank_list = NULL;
12538 /* Closing the section, so we don't relax it anymore. */
12539 bfd_vma sec_size_align;
12540 Elf_Internal_Rela *tmp_rel;
12542 /* Pad to alignment boundary. Only handle current section alignment. */
12543 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12544 & ((-1U) << sec->alignment_power);
12545 if ((sec_size_align - sec->size) & 0x2)
12547 insn16 = NDS32_NOP16;
12548 bfd_putb16 (insn16, contents + sec->size);
12552 while (sec_size_align != sec->size)
12554 insn = NDS32_NOP32;
12555 bfd_putb32 (insn, contents + sec->size);
12559 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12560 irelend, R_NDS32_RELAX_ENTRY);
12561 if (tmp_rel != irelend)
12562 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12564 clean_nds32_elf_blank ();
12568 if (internal_relocs != NULL
12569 && elf_section_data (sec)->relocs != internal_relocs)
12570 free (internal_relocs);
12572 if (contents != NULL
12573 && elf_section_data (sec)->this_hdr.contents != contents)
12576 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12586 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12588 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12589 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12594 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12595 struct bfd_link_info *info,
12596 void *finfo ATTRIBUTE_UNUSED,
12597 bfd_boolean (*func) (void *, const char *,
12598 Elf_Internal_Sym *,
12600 struct elf_link_hash_entry *)
12603 FILE *sym_ld_script = NULL;
12604 struct elf_nds32_link_hash_table *table;
12606 table = nds32_elf_hash_table (info);
12607 sym_ld_script = table->sym_ld_script;
12609 if (check_start_export_sym)
12610 fprintf (sym_ld_script, "}\n");
12615 static enum elf_reloc_type_class
12616 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12617 const asection *rel_sec ATTRIBUTE_UNUSED,
12618 const Elf_Internal_Rela *rela)
12620 switch ((int) ELF32_R_TYPE (rela->r_info))
12622 case R_NDS32_RELATIVE:
12623 return reloc_class_relative;
12624 case R_NDS32_JMP_SLOT:
12625 return reloc_class_plt;
12627 return reloc_class_copy;
12629 return reloc_class_normal;
12633 /* Put target dependent option into info hash table. */
12635 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12636 int relax_fp_as_gp,
12637 int eliminate_gc_relocs,
12638 FILE * sym_ld_script,
12640 int tls_desc_trampoline,
12641 int load_store_relax)
12643 struct elf_nds32_link_hash_table *table;
12645 table = nds32_elf_hash_table (link_info);
12649 table->relax_fp_as_gp = relax_fp_as_gp;
12650 table->eliminate_gc_relocs = eliminate_gc_relocs;
12651 table->sym_ld_script = sym_ld_script;
12652 table->hyper_relax = hyper_relax;
12653 table->tls_desc_trampoline = tls_desc_trampoline;
12654 table ->load_store_relax = load_store_relax;
12658 /* These functions and data-structures are used for fp-as-gp
12661 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12662 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12663 the read-only section and read-write section. */
12664 #define FAG_WINDOW (508 - 32)
12666 /* An nds32_fag represent a gp-relative access.
12667 We find best fp-base by using a sliding window
12668 to find a base address which can cover most gp-access. */
12671 struct nds32_fag *next; /* NULL-teminated linked list. */
12672 bfd_vma addr; /* The address of this fag. */
12673 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12674 It is used for applying FP7U2_FLAG. */
12675 int count; /* How many times this address is referred.
12676 There should be exactly `count' relocations
12678 int relas_capcity; /* The buffer size of relas.
12679 We use an array instead of linked-list,
12680 and realloc is used to adjust buffer size. */
12684 nds32_fag_init (struct nds32_fag *head)
12686 memset (head, 0, sizeof (struct nds32_fag));
12690 nds32_fag_verify (struct nds32_fag *head)
12692 struct nds32_fag *iter;
12693 struct nds32_fag *prev;
12699 if (prev && prev->addr >= iter->addr)
12700 puts ("Bug in fp-as-gp insertion.");
12706 /* Insert a fag in ascending order.
12707 If a fag of the same address already exists,
12708 they are chained by relas array. */
12711 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12712 Elf_Internal_Rela * rel)
12714 struct nds32_fag *iter;
12715 struct nds32_fag *new_fag;
12716 const int INIT_RELAS_CAP = 4;
12719 iter->next && iter->next->addr <= addr;
12721 /* Find somewhere to insert. */ ;
12723 /* `iter' will be equal to `head' if the list is empty. */
12724 if (iter != head && iter->addr == addr)
12726 /* The address exists in the list.
12727 Insert `rel' into relocation list, relas. */
12729 /* Check whether relas is big enough. */
12730 if (iter->count >= iter->relas_capcity)
12732 iter->relas_capcity *= 2;
12733 iter->relas = bfd_realloc
12734 (iter->relas, iter->relas_capcity * sizeof (void *));
12736 iter->relas[iter->count++] = rel;
12740 /* This is a new address. Create a fag node for it. */
12741 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12742 memset (new_fag, 0, sizeof (*new_fag));
12743 new_fag->addr = addr;
12744 new_fag->count = 1;
12745 new_fag->next = iter->next;
12746 new_fag->relas_capcity = INIT_RELAS_CAP;
12747 new_fag->relas = (Elf_Internal_Rela **)
12748 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12749 new_fag->relas[0] = rel;
12750 iter->next = new_fag;
12752 nds32_fag_verify (head);
12756 nds32_fag_free_list (struct nds32_fag *head)
12758 struct nds32_fag *iter;
12763 struct nds32_fag *tmp = iter;
12771 /* Find the best fp-base address.
12772 The relocation associated with that address is returned,
12773 so we can track the symbol instead of a fixed address.
12775 When relaxation, the address of an datum may change,
12776 because a text section is shrinked, so the data section
12777 moves forward. If the aligments of text and data section
12778 are different, their distance may change too.
12779 Therefore, tracking a fixed address is not appriate. */
12782 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12784 struct nds32_fag *base; /* First fag in the window. */
12785 struct nds32_fag *last; /* First fag outside the window. */
12786 int accu = 0; /* Usage accumulation. */
12787 struct nds32_fag *best; /* Best fag. */
12788 int baccu = 0; /* Best accumulation. */
12790 /* Use first fag for initial, and find the last fag in the window.
12792 In each iteration, we could simply subtract previous fag
12793 and accumulate following fags which are inside the window,
12794 untill we each the end. */
12796 if (head->next == NULL)
12802 /* Initialize base. */
12806 last && last->addr < base->addr + FAG_WINDOW;
12808 accu += last->count;
12812 /* Record the best base in each iteration. */
12815 accu -= base->count;
12817 /* Account fags in window. */
12818 for (/* Nothing. */;
12819 last && last->addr < base->addr + FAG_WINDOW;
12821 accu += last->count;
12823 /* A better fp-base? */
12836 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12837 so we can convert it fo fp-relative access later.
12838 `best_fag' is the best fp-base. Only those inside the window
12839 of best_fag is applied the flag. */
12842 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12843 asection *sec, struct nds32_fag *best_fag,
12844 Elf_Internal_Rela *internal_relocs,
12845 Elf_Internal_Rela *irelend)
12847 struct nds32_fag *ifag;
12848 bfd_vma best_fpbase, gp;
12851 output_bfd = sec->output_section->owner;
12852 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12853 best_fpbase = best_fag->addr;
12855 if (best_fpbase > gp + sdata_range[1][1]
12856 || best_fpbase < gp - sdata_range[1][0])
12859 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12860 so we know they can be converted to lwi37.fp. */
12861 for (ifag = best_fag;
12862 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12866 for (i = 0; i < ifag->count; i++)
12868 Elf_Internal_Rela *insn16_rel;
12869 Elf_Internal_Rela *fag_rel;
12871 fag_rel = ifag->relas[i];
12873 /* Only if this is within the WINDOWS, FP7U2_FLAG
12876 insn16_rel = find_relocs_at_address
12877 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12879 if (insn16_rel != irelend)
12880 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12886 /* Reset INSN16 to clean fp as gp. */
12889 nds32_fag_unmark_relax (struct nds32_fag *fag,
12890 Elf_Internal_Rela *internal_relocs,
12891 Elf_Internal_Rela *irelend)
12893 struct nds32_fag *ifag;
12895 Elf_Internal_Rela *insn16_rel;
12896 Elf_Internal_Rela *fag_rel;
12898 for (ifag = fag; ifag; ifag = ifag->next)
12900 for (i = 0; i < ifag->count; i++)
12902 fag_rel = ifag->relas[i];
12904 /* Restore the INSN16 relocation. */
12905 insn16_rel = find_relocs_at_address
12906 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12908 if (insn16_rel != irelend)
12909 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12914 /* This is the main function of fp-as-gp optimization.
12915 It should be called by relax_section. */
12918 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12919 bfd *abfd, asection *sec,
12920 Elf_Internal_Rela *internal_relocs,
12921 Elf_Internal_Rela *irelend,
12922 Elf_Internal_Sym *isymbuf)
12924 Elf_Internal_Rela *begin_rel = NULL;
12925 Elf_Internal_Rela *irel;
12926 struct nds32_fag fag_head;
12927 Elf_Internal_Shdr *symtab_hdr;
12928 bfd_byte *contents;
12929 bfd_boolean ifc_inside = FALSE;
12931 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12933 /* Per-function fp-base selection.
12934 1. Create a list for all the gp-relative access.
12935 2. Base on those gp-relative address,
12936 find a fp-base which can cover most access.
12937 3. Use the fp-base for fp-as-gp relaxation.
12939 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12941 1. delete the `la $fp, _FP_BASE_' instruction and
12942 2. not convert lwi.gp to lwi37.fp.
12944 To delete the _FP_BASE_ instruction, we simply apply
12945 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12947 To suppress the conversion, we simply NOT to apply
12948 R_NDS32_INSN16_FP7U2_FLAG flag. */
12950 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12952 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12953 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12956 /* Check whether it is worth for fp-as-gp optimization,
12957 i.e., at least 3 gp-load.
12959 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12960 apply this optimization. */
12962 for (irel = internal_relocs; irel < irelend; irel++)
12964 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12965 One we enter the begin of the region, we track all the LW/ST
12966 instructions, so when we leave the region, we try to find
12967 the best fp-base address for those LW/ST instructions. */
12969 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12970 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12972 /* Begin of the region. */
12974 /* xgettext:c-format */
12975 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12978 nds32_fag_init (&fag_head);
12979 ifc_inside = FALSE;
12981 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12982 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12985 struct nds32_fag *best_fag, *tmp_fag;
12988 /* End of the region.
12989 Check whether it is worth to do fp-as-gp. */
12991 if (begin_rel == NULL)
12993 /* xgettext:c-format */
12994 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12999 accu = nds32_fag_find_base (&fag_head, &best_fag);
13001 /* Clean FP7U2_FLAG because they may set ever. */
13002 tmp_fag = fag_head.next;
13003 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13005 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
13006 if (accu < FAG_THRESHOLD
13007 || !nds32_fag_mark_relax (link_info, sec, best_fag,
13008 internal_relocs, irelend))
13010 /* Not worth to do fp-as-gp. */
13011 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13012 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13013 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13014 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13015 nds32_fag_free_list (&fag_head);
13020 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13021 so we use it to record the distance to the reloction of best
13023 dist = best_fag->relas[0] - begin_rel;
13024 BFD_ASSERT (dist > 0 && dist < 0xffffff);
13025 /* Use high 16 bits of addend to record the _FP_BASE_ matched
13026 relocation. And get the base value when relocating. */
13027 begin_rel->r_addend &= (0x1 << 16) - 1;
13028 begin_rel->r_addend |= dist << 16;
13030 nds32_fag_free_list (&fag_head);
13034 if (begin_rel == NULL || ifc_inside)
13035 /* Skip if we are not in the region of fp-as-gp. */
13038 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13039 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13044 /* A gp-relative access is found. Insert it to the fag-list. */
13046 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
13047 insn = bfd_getb32 (contents + irel->r_offset);
13048 if (!N32_IS_RT3 (insn))
13051 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13052 nds32_fag_insert (&fag_head, addr, irel);
13054 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13058 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13059 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13061 /* Suppress fp as gp when encounter ifc. */
13069 /* Remove unused `la $fp, _FD_BASE_' instruction. */
13072 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13073 Elf_Internal_Rela *internal_relocs,
13074 Elf_Internal_Rela *irelend)
13076 Elf_Internal_Rela *irel;
13077 Elf_Internal_Shdr *symtab_hdr;
13078 bfd_byte *contents = NULL;
13079 nds32_elf_blank_t *relax_blank_list = NULL;
13080 bfd_boolean result = TRUE;
13081 bfd_boolean unused_region = FALSE;
13084 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13085 R_NDS32_17IFC_PCREL_RELA
13086 R_NDS32_10IFCU_PCREL_RELA. */
13088 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13089 nds32_get_section_contents (abfd, sec, &contents, TRUE);
13091 for (irel = internal_relocs; irel < irelend; irel++)
13093 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13094 we marked to in previous pass.
13095 DO NOT scan relocations again, since we've alreadly decided it
13096 and set the flag. */
13097 const char *syname;
13101 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13102 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13103 unused_region = TRUE;
13104 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13105 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13106 unused_region = FALSE;
13108 /* We're not in the region. */
13109 if (!unused_region)
13112 /* _FP_BASE_ must be a GLOBAL symbol. */
13113 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13114 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13117 /* The symbol name must be _FP_BASE_. */
13118 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13119 if (strcmp (syname, FP_BASE_NAME) != 0)
13122 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13124 /* addi.gp $fp, -256 */
13125 insn = bfd_getb32 (contents + irel->r_offset);
13126 if (insn != INSN_ADDIGP_TO_FP)
13129 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13131 /* addi $fp, $gp, -256 */
13132 insn = bfd_getb32 (contents + irel->r_offset);
13133 if (insn != INSN_ADDI_GP_TO_FP)
13136 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13138 /* movi $fp, FP_BASE */
13139 insn = bfd_getb32 (contents + irel->r_offset);
13140 if (insn != INSN_MOVI_TO_FP)
13146 /* We got here because a FP_BASE instruction is found. */
13147 if (!insert_nds32_elf_blank_recalc_total
13148 (&relax_blank_list, irel->r_offset, 4))
13153 if (relax_blank_list)
13155 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13156 relax_blank_list = NULL;
13165 /* This is a version of bfd_generic_get_relocated_section_contents.
13166 We need this variety because relaxation will modify the dwarf
13167 infomation. When there is undefined symbol reference error mesage,
13168 linker need to dump line number where the symbol be used. However
13169 the address is be relaxed, it can not get the original dwarf contents.
13170 The variety only modify function call for reading in the section. */
13173 nds32_elf_get_relocated_section_contents (bfd *abfd,
13174 struct bfd_link_info *link_info,
13175 struct bfd_link_order *link_order,
13177 bfd_boolean relocatable,
13180 bfd *input_bfd = link_order->u.indirect.section->owner;
13181 asection *input_section = link_order->u.indirect.section;
13183 arelent **reloc_vector;
13186 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13187 if (reloc_size < 0)
13190 /* Read in the section. */
13191 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
13194 if (reloc_size == 0)
13197 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13198 if (reloc_vector == NULL)
13201 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13202 reloc_vector, symbols);
13203 if (reloc_count < 0)
13206 if (reloc_count > 0)
13209 for (parent = reloc_vector; *parent != NULL; parent++)
13211 char *error_message = NULL;
13213 bfd_reloc_status_type r;
13215 symbol = *(*parent)->sym_ptr_ptr;
13216 if (symbol->section && discarded_section (symbol->section))
13219 static reloc_howto_type none_howto
13220 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13221 "unused", FALSE, 0, 0, FALSE);
13223 off = (*parent)->address * bfd_octets_per_byte (input_bfd);
13224 _bfd_clear_contents ((*parent)->howto, input_bfd,
13225 input_section, data, off);
13226 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13227 (*parent)->addend = 0;
13228 (*parent)->howto = &none_howto;
13232 r = bfd_perform_relocation (input_bfd, *parent, data,
13234 relocatable ? abfd : NULL,
13239 asection *os = input_section->output_section;
13241 /* A partial link, so keep the relocs. */
13242 os->orelocation[os->reloc_count] = *parent;
13246 if (r != bfd_reloc_ok)
13250 case bfd_reloc_undefined:
13251 (*link_info->callbacks->undefined_symbol)
13252 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13253 input_bfd, input_section, (*parent)->address, TRUE);
13255 case bfd_reloc_dangerous:
13256 BFD_ASSERT (error_message != NULL);
13257 (*link_info->callbacks->reloc_dangerous)
13258 (link_info, error_message,
13259 input_bfd, input_section, (*parent)->address);
13261 case bfd_reloc_overflow:
13262 (*link_info->callbacks->reloc_overflow)
13264 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13265 (*parent)->howto->name, (*parent)->addend,
13266 input_bfd, input_section, (*parent)->address);
13268 case bfd_reloc_outofrange:
13270 This error can result when processing some partially
13271 complete binaries. Do not abort, but issue an error
13272 message instead. */
13273 link_info->callbacks->einfo
13274 /* xgettext:c-format */
13275 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13276 abfd, input_section, * parent);
13287 free (reloc_vector);
13291 free (reloc_vector);
13295 /* Check target symbol. */
13298 nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13300 if (!sym || !sym->name || sym->name[0] != '$')
13305 /* nds32 find maybe function sym. Ignore target special symbol
13306 first, and then go the general function. */
13308 static bfd_size_type
13309 nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13312 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13315 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13319 /* Do TLS model conversion. */
13321 typedef struct relax_group_list_t
13323 Elf_Internal_Rela *relo;
13324 struct relax_group_list_t *next;
13325 struct relax_group_list_t *next_sibling;
13327 } relax_group_list_t;
13330 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13333 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13336 dump_chain (relax_group_list_t *pHead);
13339 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13341 relax_group_list_t *pNext = pHead;
13344 while (pNext->next)
13346 if (pNext->next->id > (int) pElem->r_addend)
13349 pNext = pNext->next;
13353 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13357 relax_group_list_t *tmp = pNext->next;
13358 pNext->next = pNew;
13360 pNew->id = pElem->r_addend;
13361 pNew->relo = pElem;
13363 pNew->next_sibling = NULL;
13369 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13371 relax_group_list_t *pNext = pNode;
13374 while (pNext->next_sibling)
13376 pNext = pNext->next_sibling;
13380 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13384 relax_group_list_t *tmp = pNext->next_sibling;
13385 pNext->next_sibling = pNew;
13388 pNew->relo = pElem;
13390 pNew->next_sibling = tmp;
13396 dump_chain (relax_group_list_t *pHead)
13398 relax_group_list_t *pNext = pHead->next;
13401 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13402 relax_group_list_t *pNextSib = pNext->next_sibling;
13405 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13406 pNextSib = pNextSib->next_sibling;
13408 pNext = pNext->next;
13413 /* Check R_NDS32_RELAX_GROUP of each section.
13414 There might be multiple sections in one object file. */
13417 elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13419 elf32_nds32_relax_group_t *relax_group_ptr =
13420 elf32_nds32_relax_group_ptr (abfd);
13422 int min_id = relax_group_ptr->min_id;
13423 int max_id = relax_group_ptr->max_id;
13425 Elf_Internal_Rela *rel;
13426 Elf_Internal_Rela *relend;
13427 Elf_Internal_Rela *relocs;
13428 enum elf_nds32_reloc_type rtype;
13432 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13433 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13434 TRUE /* keep_memory */);
13435 if (relocs == NULL)
13438 /* Check R_NDS32_RELAX_GROUP. */
13439 relend = relocs + asec->reloc_count;
13440 for (rel = relocs; rel < relend; rel++)
13443 rtype = ELF32_R_TYPE (rel->r_info);
13444 if (rtype != R_NDS32_RELAX_GROUP)
13447 id = rel->r_addend;
13450 else if (id > max_id)
13456 if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs))
13459 if ((min_id != relax_group_ptr->min_id)
13460 || (max_id != relax_group_ptr->max_id))
13462 relax_group_ptr->count = max_id - min_id + 1;
13463 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13464 relax_group_ptr->min_id = min_id;
13465 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13466 relax_group_ptr->max_id = max_id;
13469 return relax_group_ptr->count;
13472 /* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13473 struct section_id_list_t *relax_group_section_id_list = NULL;
13475 struct section_id_list_t *
13476 elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13478 struct section_id_list_t *result = NULL;
13479 struct section_id_list_t *lst = *lst_ptr;
13483 result = (struct section_id_list_t *) calloc
13484 (1, sizeof (struct section_id_list_t));
13485 BFD_ASSERT (result); /* Feed me. */
13491 struct section_id_list_t *cur = lst;
13492 struct section_id_list_t *prv = NULL;
13493 struct section_id_list_t *sec = NULL;
13506 cur = NULL; /* To insert after prv. */
13507 sec = cur; /* In case prv == NULL. */
13515 /* Insert after prv. */
13516 result = (struct section_id_list_t *) calloc
13517 (1, sizeof (struct section_id_list_t));
13518 BFD_ASSERT (result); /* Feed me. */
13522 result->next = prv->next;
13523 prv->next = result;
13528 result->next = sec;
13537 elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13539 static int next_relax_group_bias = 0;
13541 elf32_nds32_relax_group_t *relax_group_ptr =
13542 elf32_nds32_relax_group_ptr (abfd);
13544 bfd_boolean result = TRUE;
13545 Elf_Internal_Rela *rel;
13546 Elf_Internal_Rela *relend;
13547 Elf_Internal_Rela *relocs = NULL;
13548 enum elf_nds32_reloc_type rtype;
13549 struct section_id_list_t *node = NULL;
13554 if (0 == relax_group_ptr->count)
13557 /* Check if this section has been handled. */
13558 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13560 break; /* Hit, the section id has handled. */
13562 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13563 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13564 TRUE /* keep_memory */);
13565 if (relocs == NULL)
13567 BFD_ASSERT (0); /* feed me */
13571 /* Allocate group id bias for this bfd! */
13572 if (0 == relax_group_ptr->init)
13574 relax_group_ptr->bias = next_relax_group_bias;
13575 next_relax_group_bias += relax_group_ptr->count;
13576 relax_group_ptr->init = 1;
13579 /* Reorder relax group groups. */
13580 relend = relocs + asec->reloc_count;
13581 for (rel = relocs; rel < relend; rel++)
13583 rtype = ELF32_R_TYPE(rel->r_info);
13584 if (rtype != R_NDS32_RELAX_GROUP)
13588 rel->r_addend += relax_group_ptr->bias;
13589 /* Debugging count. */
13595 if (relocs != NULL && elf_section_data (asec)->relocs != relocs)
13602 nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13603 struct bfd_link_info *lnkinfo)
13605 bfd_boolean result = TRUE;
13606 Elf_Internal_Rela *irel;
13607 Elf_Internal_Rela *irelend;
13608 Elf_Internal_Rela *internal_relocs;
13609 unsigned long r_symndx;
13610 enum elf_nds32_reloc_type r_type;
13612 Elf_Internal_Sym *local_syms = NULL;
13613 bfd_byte *contents = NULL;
13615 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13617 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13618 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13619 sym_hashes = elf_sym_hashes (inbfd);
13621 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13622 if (!elf_bad_symtab (inbfd))
13623 sym_hashes_end -= symtab_hdr->sh_info;
13625 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13626 if (bfd_link_relocatable (lnkinfo))
13628 elf32_nds32_unify_relax_group (inbfd, insec);
13632 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13633 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13634 TRUE /* keep_memory */);
13635 if (internal_relocs == NULL)
13638 irelend = internal_relocs + insec->reloc_count;
13639 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13640 irelend, R_NDS32_RELAX_ENTRY);
13641 if (irel == irelend)
13644 /* Chain/remove groups. */
13645 for (irel = internal_relocs; irel < irelend; irel++)
13647 r_symndx = ELF32_R_SYM (irel->r_info);
13648 r_type = ELF32_R_TYPE (irel->r_info);
13649 if (r_type != R_NDS32_RELAX_GROUP)
13653 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13654 /* Chain it now. */
13655 if (!list_insert (&chain, irel))
13659 /* Collect group relocations. */
13660 /* Presume relocations are sorted. */
13661 relax_group_list_t *pNext = chain.next;
13664 for (irel = internal_relocs; irel < irelend; irel++)
13666 if (irel->r_offset == pNext->relo->r_offset)
13668 /* Ignore Non-TLS relocation types. */
13669 r_type = ELF32_R_TYPE (irel->r_info);
13670 if ((R_NDS32_TLS_LE_HI20 > r_type)
13671 || (R_NDS32_RELAX_ENTRY == r_type))
13674 if (!list_insert_sibling (pNext, irel))
13677 else if (irel->r_offset > pNext->relo->r_offset)
13679 pNext = pNext->next;
13683 bfd_vma current_offset = pNext->relo->r_offset;
13684 if (irel->r_offset > current_offset)
13685 irel = internal_relocs; /* restart from head */
13687 --irel; /* Check current irel again. */
13692 /* This shouldn't be reached. */
13696 pNext = pNext->next;
13699 #ifdef DUBUG_VERBOSE
13700 dump_chain(&chain);
13703 /* Get symbol table and section content. */
13705 contents = incontents;
13706 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13707 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13710 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13712 /* Convert TLS model each group if necessary. */
13713 pNext = chain.next;
13715 int cur_grp_id = -1;
13718 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13720 tls_type = org_tls_type = eff_tls_type = 0;
13724 relax_group_list_t *pNextSig = pNext->next_sibling;
13727 struct elf_link_hash_entry *h = NULL;
13729 irel = pNextSig->relo;
13730 r_symndx = ELF32_R_SYM(irel->r_info);
13731 r_type = ELF32_R_TYPE(irel->r_info);
13733 if (pNext->id != cur_grp_id)
13735 cur_grp_id = pNext->id;
13736 org_tls_type = get_tls_type (r_type, NULL);
13737 if (r_symndx >= symtab_hdr->sh_info)
13739 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13740 while (h->root.type == bfd_link_hash_indirect
13741 || h->root.type == bfd_link_hash_warning)
13742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13743 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13747 tls_type = local_got_tls_type
13748 ? local_got_tls_type[r_symndx]
13752 eff_tls_type = 1 << (fls (tls_type) - 1);
13753 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13756 if (eff_tls_type != org_tls_type)
13758 switch (org_tls_type)
13760 /* DESC to IEGP/IE/LE. */
13762 switch (eff_tls_type)
13767 case R_NDS32_TLS_DESC_HI20:
13768 irel->r_info = ELF32_R_INFO(r_symndx,
13769 R_NDS32_TLS_IE_HI20);
13771 case R_NDS32_TLS_DESC_LO12:
13772 irel->r_info = ELF32_R_INFO(r_symndx,
13773 R_NDS32_TLS_IE_LO12);
13775 case R_NDS32_TLS_DESC_ADD:
13777 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13778 add_rt = N32_RT5 (insn);
13779 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13780 bfd_putb32 (insn, contents + irel->r_offset);
13782 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13785 case R_NDS32_TLS_DESC_FUNC:
13786 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13787 irel->r_info = ELF32_R_INFO(r_symndx,
13788 R_NDS32_RELAX_REMOVE);
13790 case R_NDS32_TLS_DESC_CALL:
13792 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13794 bfd_putb32 (insn, contents + irel->r_offset);
13796 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13799 case R_NDS32_LOADSTORE:
13801 case R_NDS32_PTR_RESOLVED:
13803 case R_NDS32_LABEL:
13813 case R_NDS32_TLS_DESC_HI20:
13814 irel->r_info = ELF32_R_INFO(r_symndx,
13815 R_NDS32_TLS_IEGP_HI20);
13817 case R_NDS32_TLS_DESC_LO12:
13818 irel->r_info = ELF32_R_INFO(r_symndx,
13819 R_NDS32_TLS_IEGP_LO12);
13821 case R_NDS32_TLS_DESC_ADD:
13823 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13824 add_rt = N32_RT5 (insn);
13825 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13826 bfd_putb32 (insn, contents + irel->r_offset);
13828 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13831 case R_NDS32_TLS_DESC_FUNC:
13832 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13833 irel->r_info = ELF32_R_INFO(r_symndx,
13834 R_NDS32_RELAX_REMOVE);
13836 case R_NDS32_TLS_DESC_CALL:
13838 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13840 bfd_putb32 (insn, contents + irel->r_offset);
13842 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13845 case R_NDS32_LOADSTORE:
13847 case R_NDS32_PTR_RESOLVED:
13849 case R_NDS32_LABEL:
13859 case R_NDS32_TLS_DESC_HI20:
13860 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13862 case R_NDS32_TLS_DESC_LO12:
13863 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13865 case R_NDS32_TLS_DESC_ADD:
13867 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13869 add_rt = N32_RT5 (insn);
13870 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13871 bfd_putb32 (insn, contents + irel->r_offset);
13873 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13876 case R_NDS32_TLS_DESC_FUNC:
13877 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13878 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13880 case R_NDS32_TLS_DESC_CALL:
13881 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13882 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13884 case R_NDS32_LOADSTORE:
13886 case R_NDS32_PTR_RESOLVED:
13888 case R_NDS32_LABEL:
13899 /* IEGP to IE/LE. */
13901 switch (eff_tls_type)
13906 case R_NDS32_TLS_IEGP_HI20:
13907 irel->r_info = ELF32_R_INFO(r_symndx,
13908 R_NDS32_TLS_IE_HI20);
13910 case R_NDS32_TLS_IEGP_LO12:
13911 irel->r_info = ELF32_R_INFO(r_symndx,
13912 R_NDS32_TLS_IE_LO12);
13914 case R_NDS32_PTR_RESOLVED:
13916 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13918 add_rt = N32_RT5 (insn);
13919 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13920 bfd_putb32 (insn, contents + irel->r_offset);
13923 case R_NDS32_TLS_IEGP_LW:
13925 case R_NDS32_LOADSTORE:
13928 case R_NDS32_LABEL:
13938 case R_NDS32_TLS_IEGP_HI20:
13939 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13941 case R_NDS32_TLS_IEGP_LO12:
13942 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13944 case R_NDS32_TLS_IEGP_LW:
13945 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13946 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13948 case R_NDS32_LOADSTORE:
13951 case R_NDS32_LABEL:
13952 case R_NDS32_PTR_RESOLVED:
13965 switch (eff_tls_type)
13970 case R_NDS32_TLS_IE_HI20:
13971 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13973 case R_NDS32_TLS_IE_LO12S2:
13975 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13977 add_rt = N32_RT5 (insn);
13978 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13979 bfd_putb32 (insn, contents + irel->r_offset);
13981 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13984 case R_NDS32_LOADSTORE:
13987 case R_NDS32_LABEL:
14002 pNextSig = pNextSig->next_sibling;
14006 pNext = pNext->next;
14010 if (pNext->id != cur_grp_id)
14012 pNext = pNext->next;
14021 if (internal_relocs != NULL
14022 && elf_section_data (insec)->relocs != internal_relocs)
14023 free (internal_relocs);
14025 if (contents != NULL
14026 && elf_section_data (insec)->this_hdr.contents != contents)
14029 if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms)
14034 pNext = chain.next;
14035 relax_group_list_t *pDel;
14039 pNext = pNext->next;
14051 /* End TLS model conversion. */
14053 #define ELF_ARCH bfd_arch_nds32
14054 #define ELF_MACHINE_CODE EM_NDS32
14055 #define ELF_MAXPAGESIZE 0x1000
14056 #define ELF_TARGET_ID NDS32_ELF_DATA
14058 #define TARGET_BIG_SYM nds32_elf32_be_vec
14059 #define TARGET_BIG_NAME "elf32-nds32be"
14060 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
14061 #define TARGET_LITTLE_NAME "elf32-nds32le"
14063 #define elf_info_to_howto nds32_info_to_howto
14064 #define elf_info_to_howto_rel nds32_info_to_howto_rel
14066 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14067 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14068 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14069 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14070 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14072 #define bfd_elf32_mkobject nds32_elf_mkobject
14073 #define elf_backend_action_discarded nds32_elf_action_discarded
14074 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14075 #define elf_backend_check_relocs nds32_elf_check_relocs
14076 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14077 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14078 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14079 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14080 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14081 #define elf_backend_relocate_section nds32_elf_relocate_section
14082 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
14083 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14084 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14085 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14086 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14087 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14088 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14089 #define elf_backend_object_p nds32_elf_object_p
14090 #define elf_backend_final_write_processing nds32_elf_final_write_processing
14091 #define elf_backend_special_sections nds32_elf_special_sections
14092 #define bfd_elf32_bfd_get_relocated_section_contents \
14093 nds32_elf_get_relocated_section_contents
14094 #define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14095 #define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
14097 #define elf_backend_can_gc_sections 1
14098 #define elf_backend_can_refcount 1
14099 #define elf_backend_want_got_plt 1
14100 #define elf_backend_plt_readonly 1
14101 #define elf_backend_want_plt_sym 0
14102 #define elf_backend_got_header_size 12
14103 #define elf_backend_may_use_rel_p 1
14104 #define elf_backend_default_use_rela_p 1
14105 #define elf_backend_may_use_rela_p 1
14106 #define elf_backend_dtrel_excludes_plt 0
14108 #include "elf32-target.h"
14110 #undef ELF_MAXPAGESIZE
14111 #define ELF_MAXPAGESIZE 0x2000
14113 #undef TARGET_BIG_SYM
14114 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
14115 #undef TARGET_BIG_NAME
14116 #define TARGET_BIG_NAME "elf32-nds32be-linux"
14117 #undef TARGET_LITTLE_SYM
14118 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
14119 #undef TARGET_LITTLE_NAME
14120 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
14122 #define elf32_bed elf32_nds32_lin_bed
14124 #include "elf32-target.h"