gprofng: fix build with -mx32
[binutils-gdb/blckswan.git] / bfd / elf32-nds32.c
blob519c82833615a34522d41e2d2ee068076a1d2a0a
1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2022 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
20 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
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 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
36 #define OCTETS_PER_BYTE(ABFD, SEC) 1
38 /* Relocation HOWTO functions. */
39 static bfd_reloc_status_type nds32_elf_ignore_reloc
40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type nds32_elf_hi20_reloc
44 (bfd *, arelent *, asymbol *, void *,
45 asection *, bfd *, char **);
46 static bfd_reloc_status_type nds32_elf_lo12_reloc
47 (bfd *, arelent *, asymbol *, void *,
48 asection *, bfd *, char **);
49 static bfd_reloc_status_type nds32_elf_generic_reloc
50 (bfd *, arelent *, asymbol *, void *,
51 asection *, bfd *, char **);
52 static bfd_reloc_status_type nds32_elf_sda15_reloc
53 (bfd *, arelent *, asymbol *, void *,
54 asection *, bfd *, char **);
56 /* Helper functions for HOWTO. */
57 static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
58 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
59 asection *, bfd_vma, bfd_vma);
61 /* Nds32 helper functions. */
62 static bfd_vma calculate_memory_address
63 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
64 static int nds32_get_section_contents (bfd *, asection *,
65 bfd_byte **, bool);
66 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67 Elf_Internal_Sym **);
68 static bool nds32_relax_fp_as_gp
69 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
70 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
71 Elf_Internal_Sym *isymbuf);
72 static bool nds32_fag_remove_unused_fpbase
73 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74 Elf_Internal_Rela *irelend);
76 enum
78 MACH_V1 = bfd_mach_n1h,
79 MACH_V2 = bfd_mach_n1h_v2,
80 MACH_V3 = bfd_mach_n1h_v3,
81 MACH_V3M = bfd_mach_n1h_v3m
84 #define MIN(a, b) ((a) > (b) ? (b) : (a))
85 #define MAX(a, b) ((a) > (b) ? (a) : (b))
87 /* The name of the dynamic interpreter. This is put in the .interp
88 section. */
89 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
91 #define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92 && (flags) & SEC_LOAD \
93 && (flags) & SEC_READONLY)
95 /* The nop opcode we use. */
96 #define NDS32_NOP32 0x40000009
97 #define NDS32_NOP16 0x9200
99 /* The size in bytes of an entry in the procedure linkage table. */
100 #define PLT_ENTRY_SIZE 24
101 #define PLT_HEADER_SIZE 24
103 /* The first entry in a procedure linkage table are reserved,
104 and the initial contents are unimportant (we zero them out).
105 Subsequent entries look like this. */
106 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
107 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
108 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
109 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
110 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
112 /* $ta is change to $r15 (from $r25). */
113 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
114 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
115 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
116 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
117 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
118 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
120 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
121 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
122 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
123 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
124 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
126 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
127 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
128 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
129 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
130 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
131 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
133 /* These are macros used to get the relocation accurate value. */
134 #define ACCURATE_8BIT_S1 (0x100)
135 #define ACCURATE_U9BIT_S1 (0x400)
136 #define ACCURATE_12BIT_S1 (0x2000)
137 #define ACCURATE_14BIT_S1 (0x4000)
138 #define ACCURATE_19BIT (0x40000)
140 /* These are macros used to get the relocation conservative value. */
141 #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
142 #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143 #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144 #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
145 /* These must be more conservative because the address may be in
146 different segment. */
147 #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
148 #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
149 #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
150 #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
151 #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
153 /* Size of small data/bss sections, used to calculate SDA_BASE. */
154 static long got_size = 0;
155 static int is_SDA_BASE_set = 0;
157 /* Convert ELF-VER in eflags to string for debugging purpose. */
158 static const char *const nds32_elfver_strtab[] =
160 "ELF-1.2",
161 "ELF-1.3",
162 "ELF-1.4",
165 /* The nds32 linker needs to keep track of the number of relocs that it
166 decides to copy in check_relocs for each symbol. This is so that
167 it can discard PC relative relocs if it doesn't need them when
168 linking with -Bsymbolic. We store the information in a field
169 extending the regular ELF linker hash table. */
171 /* This structure keeps track of the number of PC relative relocs we
172 have copied for a given symbol. */
174 struct elf_nds32_pcrel_relocs_copied
176 /* Next section. */
177 struct elf_nds32_pcrel_relocs_copied *next;
178 /* A section in dynobj. */
179 asection *section;
180 /* Number of relocs copied in this section. */
181 bfd_size_type count;
184 enum elf_nds32_tls_type
186 GOT_UNKNOWN = (0),
187 GOT_NORMAL = (1 << 0),
188 GOT_TLS_LE = (1 << 1),
189 GOT_TLS_IE = (1 << 2),
190 GOT_TLS_IEGP = (1 << 3),
191 GOT_TLS_LD = (1 << 4),
192 GOT_TLS_GD = (1 << 5),
193 GOT_TLS_DESC = (1 << 6),
196 /* Nds32 ELF linker hash entry. */
198 struct elf_nds32_link_hash_entry
200 struct elf_link_hash_entry root;
202 /* For checking relocation type. */
203 enum elf_nds32_tls_type tls_type;
205 int offset_to_gp;
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
216 typedef struct
218 int min_id;
219 int max_id;
220 int count;
221 int bias;
222 int init;
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;
239 int* offset_to_gp;
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)
256 static bool
257 nds32_elf_mkobject (bfd *abfd)
259 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
260 NDS32_ELF_DATA);
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 */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 32, /* bitsize */
278 false, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_bitfield,/* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_NDS32_NONE", /* name */
283 false, /* partial_inplace */
284 0, /* src_mask */
285 0, /* dst_mask */
286 false), /* pcrel_offset */
288 /* A 16 bit absolute relocation. */
289 HOWTO2 (R_NDS32_16, /* type */
290 0, /* rightshift */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
292 16, /* bitsize */
293 false, /* pc_relative */
294 0, /* bitpos */
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 */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 32, /* bitsize */
308 false, /* pc_relative */
309 0, /* bitpos */
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 */
320 0, /* rightshift */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
322 20, /* bitsize */
323 false, /* pc_relative */
324 0, /* bitpos */
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
336 slot for pc. */
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
340 again. */
341 HOWTO2 (R_NDS32_9_PCREL, /* type */
342 1, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 8, /* bitsize */
345 true, /* pc_relative */
346 0, /* bitpos */
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 */
351 0xff, /* src_mask */
352 0xff, /* dst_mask */
353 true), /* pcrel_offset */
355 /* A relative 15 bit relocation, right shifted by 1. */
356 HOWTO2 (R_NDS32_15_PCREL, /* type */
357 1, /* rightshift */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
359 14, /* bitsize */
360 true, /* pc_relative */
361 0, /* bitpos */
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 */
372 1, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 16, /* bitsize */
375 true, /* pc_relative */
376 0, /* bitpos */
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
389 again. */
390 HOWTO2 (R_NDS32_25_PCREL, /* type */
391 1, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 24, /* bitsize */
394 true, /* pc_relative */
395 0, /* bitpos */
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 */
406 12, /* rightshift */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
408 20, /* bitsize */
409 false, /* pc_relative */
410 0, /* bitpos */
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 */
421 3, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 9, /* bitsize */
424 false, /* pc_relative */
425 0, /* bitpos */
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 */
436 2, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 10, /* bitsize */
439 false, /* pc_relative */
440 0, /* bitpos */
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 */
451 1, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 11, /* bitsize */
454 false, /* pc_relative */
455 0, /* bitpos */
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 */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 12, /* bitsize */
469 false, /* pc_relative */
470 0, /* bitpos */
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 */
481 3, /* rightshift */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
483 15, /* bitsize */
484 false, /* pc_relative */
485 0, /* bitpos */
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 */
496 2, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 15, /* bitsize */
499 false, /* pc_relative */
500 0, /* bitpos */
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 */
511 1, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 15, /* bitsize */
514 false, /* pc_relative */
515 0, /* bitpos */
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 */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 15, /* bitsize */
529 false, /* pc_relative */
530 0, /* bitpos */
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 */
541 0, /* rightshift */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
543 0, /* bitsize */
544 false, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_dont,/* complain_on_overflow */
547 NULL, /* special_function */
548 "R_NDS32_GNU_VTINHERIT",/* name */
549 false, /* partial_inplace */
550 0, /* src_mask */
551 0, /* dst_mask */
552 false), /* pcrel_offset */
554 /* GNU extension to record C++ vtable member usage */
555 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 0, /* bitsize */
559 false, /* pc_relative */
560 0, /* bitpos */
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 */
565 0, /* src_mask */
566 0, /* dst_mask */
567 false), /* pcrel_offset */
569 /* A 16 bit absolute relocation. */
570 HOWTO2 (R_NDS32_16_RELA, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 false, /* pc_relative */
575 0, /* bitpos */
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 */
586 0, /* rightshift */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
588 32, /* bitsize */
589 false, /* pc_relative */
590 0, /* bitpos */
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 */
601 0, /* rightshift */
602 2, /* size (0 = byte, 1 = short, 2 = long) */
603 20, /* bitsize */
604 false, /* pc_relative */
605 0, /* bitpos */
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 */
615 1, /* rightshift */
616 1, /* size (0 = byte, 1 = short, 2 = long) */
617 8, /* bitsize */
618 true, /* pc_relative */
619 0, /* bitpos */
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 */
624 0xff, /* src_mask */
625 0xff, /* dst_mask */
626 true), /* pcrel_offset */
628 /* A relative 15 bit relocation, right shifted by 1. */
629 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
630 1, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 14, /* bitsize */
633 true, /* pc_relative */
634 0, /* bitpos */
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 */
645 1, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 16, /* bitsize */
648 true, /* pc_relative */
649 0, /* bitpos */
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 */
660 1, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 24, /* bitsize */
663 true, /* pc_relative */
664 0, /* bitpos */
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 */
675 12, /* rightshift */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
677 20, /* bitsize */
678 false, /* pc_relative */
679 0, /* bitpos */
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 */
690 3, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 9, /* bitsize */
693 false, /* pc_relative */
694 0, /* bitpos */
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 */
705 2, /* rightshift */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
707 10, /* bitsize */
708 false, /* pc_relative */
709 0, /* bitpos */
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 */
720 1, /* rightshift */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
722 11, /* bitsize */
723 false, /* pc_relative */
724 0, /* bitpos */
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 */
735 0, /* rightshift */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
737 12, /* bitsize */
738 false, /* pc_relative */
739 0, /* bitpos */
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 */
750 3, /* rightshift */
751 2, /* size (0 = byte, 1 = short, 2 = long) */
752 15, /* bitsize */
753 false, /* pc_relative */
754 0, /* bitpos */
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 */
765 2, /* rightshift */
766 2, /* size (0 = byte, 1 = short, 2 = long) */
767 15, /* bitsize */
768 false, /* pc_relative */
769 0, /* bitpos */
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 */
779 1, /* rightshift */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
781 15, /* bitsize */
782 false, /* pc_relative */
783 0, /* bitpos */
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 */
793 0, /* rightshift */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
795 15, /* bitsize */
796 false, /* pc_relative */
797 0, /* bitpos */
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 */
808 0, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 0, /* bitsize */
811 false, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont,/* complain_on_overflow */
814 NULL, /* special_function */
815 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
816 false, /* partial_inplace */
817 0, /* src_mask */
818 0, /* dst_mask */
819 false), /* pcrel_offset */
821 /* GNU extension to record C++ vtable member usage */
822 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
823 0, /* rightshift */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
825 0, /* bitsize */
826 false, /* pc_relative */
827 0, /* bitpos */
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 */
832 0, /* src_mask */
833 0, /* dst_mask */
834 false), /* pcrel_offset */
836 /* Like R_NDS32_20, but referring to the GOT table entry for
837 the symbol. */
838 HOWTO2 (R_NDS32_GOT20, /* type */
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 20, /* bitsize */
842 false, /* pc_relative */
843 0, /* bitpos */
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 */
855 1, /* rightshift */
856 2, /* size (0 = byte, 1 = short, 2 = long) */
857 24, /* bitsize */
858 true, /* pc_relative */
859 0, /* bitpos */
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 */
874 0, /* rightshift */
875 2, /* size (0 = byte, 1 = short, 2 = long) */
876 32, /* bitsize */
877 false, /* pc_relative */
878 0, /* bitpos */
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
888 entries. */
889 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
890 0, /* rightshift */
891 2, /* size (0 = byte, 1 = short, 2 = long) */
892 32, /* bitsize */
893 false, /* pc_relative */
894 0, /* bitpos */
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 */
905 0, /* rightshift */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
907 32, /* bitsize */
908 false, /* pc_relative */
909 0, /* bitpos */
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
920 addend. */
921 HOWTO2 (R_NDS32_RELATIVE, /* type */
922 0, /* rightshift */
923 2, /* size (0 = byte, 1 = short, 2 = long) */
924 32, /* bitsize */
925 false, /* pc_relative */
926 0, /* bitpos */
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 */
936 0, /* rightshift */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
938 20, /* bitsize */
939 false, /* pc_relative */
940 0, /* bitpos */
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
950 table register. */
951 HOWTO2 (R_NDS32_GOTPC20, /* type */
952 0, /* rightshift */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
954 20, /* bitsize */
955 true, /* pc_relative */
956 0, /* bitpos */
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
966 the symbol. */
967 HOWTO2 (R_NDS32_GOT_HI20, /* type */
968 12, /* rightshift */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
970 20, /* bitsize */
971 false, /* pc_relative */
972 0, /* bitpos */
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 */
981 0, /* rightshift */
982 2, /* size (0 = byte, 1 = short, 2 = long) */
983 12, /* bitsize */
984 false, /* pc_relative */
985 0, /* bitpos */
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
996 the symbol. */
997 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
998 12, /* rightshift */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1000 20, /* bitsize */
1001 false, /* pc_relative */
1002 0, /* bitpos */
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 */
1011 0, /* rightshift */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1013 12, /* bitsize */
1014 false, /* pc_relative */
1015 0, /* bitpos */
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) */
1027 20, /* bitsize */
1028 false, /* pc_relative */
1029 0, /* bitpos */
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 */
1038 0, /* rightshift */
1039 2, /* size (0 = byte, 1 = short, 2 = long) */
1040 12, /* bitsize */
1041 false, /* pc_relative */
1042 0, /* bitpos */
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 */
1055 0, /* rightshift */
1056 2, /* size (0 = byte, 1 = short, 2 = long) */
1057 32, /* bitsize */
1058 false, /* pc_relative */
1059 0, /* bitpos */
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 */
1070 0, /* rightshift */
1071 2, /* size (0 = byte, 1 = short, 2 = long) */
1072 32, /* bitsize */
1073 false, /* pc_relative */
1074 0, /* bitpos */
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 */
1085 0, /* rightshift */
1086 2, /* size (0 = byte, 1 = short, 2 = long) */
1087 32, /* bitsize */
1088 false, /* pc_relative */
1089 0, /* bitpos */
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 */
1100 0, /* rightshift */
1101 2, /* size (0 = byte, 1 = short, 2 = long) */
1102 32, /* bitsize */
1103 false, /* pc_relative */
1104 0, /* bitpos */
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 */
1115 0, /* rightshift */
1116 2, /* size (0 = byte, 1 = short, 2 = long) */
1117 32, /* bitsize */
1118 false, /* pc_relative */
1119 0, /* bitpos */
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 */
1130 0, /* rightshift */
1131 2, /* size (0 = byte, 1 = short, 2 = long) */
1132 32, /* bitsize */
1133 false, /* pc_relative */
1134 0, /* bitpos */
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 */
1145 0, /* rightshift */
1146 2, /* size (0 = byte, 1 = short, 2 = long) */
1147 32, /* bitsize */
1148 false, /* pc_relative */
1149 0, /* bitpos */
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 */
1160 0, /* rightshift */
1161 2, /* size (0 = byte, 1 = short, 2 = long) */
1162 32, /* bitsize */
1163 false, /* pc_relative */
1164 0, /* bitpos */
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 */
1175 0, /* rightshift */
1176 2, /* size (0 = byte, 1 = short, 2 = long) */
1177 32, /* bitsize */
1178 false, /* pc_relative */
1179 0, /* bitpos */
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 */
1190 0, /* rightshift */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 16, /* bitsize */
1193 false, /* pc_relative */
1194 0, /* bitpos */
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 */
1205 0, /* rightshift */
1206 2, /* size (0 = byte, 1 = short, 2 = long) */
1207 32, /* bitsize */
1208 false, /* pc_relative */
1209 0, /* bitpos */
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 */
1220 0, /* rightshift */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 32, /* bitsize */
1223 false, /* pc_relative */
1224 0, /* bitpos */
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 */
1235 0, /* rightshift */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 32, /* bitsize */
1238 false, /* pc_relative */
1239 0, /* bitpos */
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) */
1252 20, /* bitsize */
1253 false, /* pc_relative */
1254 0, /* bitpos */
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 */
1265 0, /* rightshift */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 12, /* bitsize */
1268 false, /* pc_relative */
1269 0, /* bitpos */
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) */
1282 20, /* bitsize */
1283 false, /* pc_relative */
1284 0, /* bitpos */
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 */
1295 0, /* rightshift */
1296 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 12, /* bitsize */
1298 false, /* pc_relative */
1299 0, /* bitpos */
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 */
1310 2, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 12, /* bitsize */
1313 false, /* pc_relative */
1314 0, /* bitpos */
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 */
1325 2, /* rightshift */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 12, /* bitsize */
1328 false, /* pc_relative */
1329 0, /* bitpos */
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 */
1340 2, /* rightshift */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 10, /* bitsize */
1343 false, /* pc_relative */
1344 0, /* bitpos */
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 */
1355 2, /* rightshift */
1356 2, /* size (0 = byte, 1 = short, 2 = long) */
1357 10, /* bitsize */
1358 false, /* pc_relative */
1359 0, /* bitpos */
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 */
1369 0, /* rightshift */
1370 2, /* size (0 = byte, 1 = short, 2 = long) */
1371 12, /* bitsize */
1372 false, /* pc_relative */
1373 0, /* bitpos */
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 */
1383 3, /* rightshift */
1384 2, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 false, /* pc_relative */
1387 0, /* bitpos */
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 */
1398 2, /* rightshift */
1399 2, /* size (0 = byte, 1 = short, 2 = long) */
1400 17, /* bitsize */
1401 false, /* pc_relative */
1402 0, /* bitpos */
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 */
1412 1, /* rightshift */
1413 2, /* size (0 = byte, 1 = short, 2 = long) */
1414 18, /* bitsize */
1415 false, /* pc_relative */
1416 0, /* bitpos */
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 */
1426 0, /* rightshift */
1427 2, /* size (0 = byte, 1 = short, 2 = long) */
1428 19, /* bitsize */
1429 false, /* pc_relative */
1430 0, /* bitpos */
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 */
1439 0, /* rightshift */
1440 0, /* size (0 = byte, 1 = short, 2 = long) */
1441 8, /* bitsize */
1442 false, /* pc_relative */
1443 0, /* bitpos */
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 */
1452 0, /* rightshift */
1453 1, /* size (0 = byte, 1 = short, 2 = long) */
1454 16, /* bitsize */
1455 false, /* pc_relative */
1456 0, /* bitpos */
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 */
1465 0, /* rightshift */
1466 2, /* size (0 = byte, 1 = short, 2 = long) */
1467 32, /* bitsize */
1468 false, /* pc_relative */
1469 0, /* bitpos */
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 */
1478 0, /* rightshift */
1479 1, /* size (0 = byte, 1 = short, 2 = long) */
1480 16, /* bitsize */
1481 false, /* pc_relative */
1482 0, /* bitpos */
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 */
1493 1, /* rightshift */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1495 8, /* bitsize */
1496 true, /* pc_relative */
1497 0, /* bitpos */
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 */
1507 0, /* rightshift */
1508 2, /* size (0 = byte, 1 = short, 2 = long) */
1509 20, /* bitsize */
1510 false, /* pc_relative */
1511 0, /* bitpos */
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 */
1521 0, /* rightshift */
1522 2, /* size (0 = byte, 1 = short, 2 = long) */
1523 15, /* bitsize */
1524 false, /* pc_relative */
1525 0, /* bitpos */
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 */
1535 0, /* rightshift */
1536 2, /* size (0 = byte, 1 = short, 2 = long) */
1537 19, /* bitsize */
1538 false, /* pc_relative */
1539 0, /* bitpos */
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 */
1548 0, /* rightshift */
1549 2, /* size (0 = byte, 1 = short, 2 = long) */
1550 15, /* bitsize */
1551 false, /* pc_relative */
1552 0, /* bitpos */
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 */
1561 0, /* rightshift */
1562 2, /* size (0 = byte, 1 = short, 2 = long) */
1563 19, /* bitsize */
1564 false, /* pc_relative */
1565 0, /* bitpos */
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 */
1574 0, /* rightshift */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1576 15, /* bitsize */
1577 false, /* pc_relative */
1578 0, /* bitpos */
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 */
1587 0, /* rightshift */
1588 2, /* size (0 = byte, 1 = short, 2 = long) */
1589 19, /* bitsize */
1590 false, /* pc_relative */
1591 0, /* bitpos */
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 */
1601 2, /* rightshift */
1602 2, /* size (0 = byte, 1 = short, 2 = long) */
1603 15, /* bitsize */
1604 false, /* pc_relative */
1605 0, /* bitpos */
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 */
1615 2, /* rightshift */
1616 2, /* size (0 = byte, 1 = short, 2 = long) */
1617 17, /* bitsize */
1618 false, /* pc_relative */
1619 0, /* bitpos */
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 */
1629 0, /* rightshift */
1630 1, /* size (0 = byte, 1 = short, 2 = long) */
1631 5, /* bitsize */
1632 false, /* pc_relative */
1633 0, /* bitpos */
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 */
1642 1, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 9, /* bitsize */
1645 true, /* pc_relative */
1646 0, /* bitpos */
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 */
1655 2, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 7, /* bitsize */
1658 false, /* pc_relative */
1659 0, /* bitpos */
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 */
1668 1, /* rightshift */
1669 2, /* size (0 = byte, 1 = short, 2 = long) */
1670 8, /* bitsize */
1671 true, /* pc_relative */
1672 0, /* bitpos */
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 */
1681 1, /* rightshift */
1682 2, /* size (0 = byte, 1 = short, 2 = long) */
1683 24, /* bitsize */
1684 false, /* pc_relative */
1685 0, /* bitpos */
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 */
1696 1, /* rightshift */
1697 2, /* size (0 = byte, 1 = short, 2 = long) */
1698 16, /* bitsize */
1699 true, /* pc_relative */
1700 0, /* bitpos */
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 */
1711 1, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 9, /* bitsize */
1714 true, /* pc_relative */
1715 0, /* bitpos */
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) */
1728 20, /* bitsize */
1729 false, /* pc_relative */
1730 0, /* bitpos */
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 */
1740 0, /* rightshift */
1741 2, /* size (0 = byte, 1 = short, 2 = long) */
1742 12, /* bitsize */
1743 false, /* pc_relative */
1744 0, /* bitpos */
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) */
1757 20, /* bitsize */
1758 false, /* pc_relative */
1759 0, /* bitpos */
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 */
1769 2, /* rightshift */
1770 2, /* size (0 = byte, 1 = short, 2 = long) */
1771 10, /* bitsize */
1772 false, /* pc_relative */
1773 0, /* bitpos */
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 */
1784 0, /* rightshift */
1785 2, /* size (0 = byte, 1 = short, 2 = long) */
1786 32, /* bitsize */
1787 false, /* pc_relative */
1788 0, /* bitpos */
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 */
1799 0, /* rightshift */
1800 2, /* size (0 = byte, 1 = short, 2 = long) */
1801 20, /* bitsize */
1802 false, /* pc_relative */
1803 0, /* bitpos */
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 */
1813 0, /* rightshift */
1814 2, /* size (0 = byte, 1 = short, 2 = long) */
1815 15, /* bitsize */
1816 false, /* pc_relative */
1817 0, /* bitpos */
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 */
1826 1, /* rightshift */
1827 2, /* size (0 = byte, 1 = short, 2 = long) */
1828 15, /* bitsize */
1829 false, /* pc_relative */
1830 0, /* bitpos */
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 */
1839 2, /* rightshift */
1840 2, /* size (0 = byte, 1 = short, 2 = long) */
1841 15, /* bitsize */
1842 false, /* pc_relative */
1843 0, /* bitpos */
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 */
1854 0, /* rightshift */
1855 2, /* size (0 = byte, 1 = short, 2 = long) */
1856 32, /* bitsize */
1857 false, /* pc_relative */
1858 0, /* bitpos */
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 */
1869 0, /* rightshift */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1871 32, /* bitsize */
1872 false, /* pc_relative */
1873 0, /* bitpos */
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 */
1884 0, /* rightshift */
1885 2, /* size (0 = byte, 1 = short, 2 = long) */
1886 32, /* bitsize */
1887 false, /* pc_relative */
1888 0, /* bitpos */
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 */
1899 0, /* rightshift */
1900 2, /* size (0 = byte, 1 = short, 2 = long) */
1901 32, /* bitsize */
1902 false, /* pc_relative */
1903 0, /* bitpos */
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 */
1914 0, /* rightshift */
1915 2, /* size (0 = byte, 1 = short, 2 = long) */
1916 32, /* bitsize */
1917 false, /* pc_relative */
1918 0, /* bitpos */
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 */
1929 0, /* rightshift */
1930 2, /* size (0 = byte, 1 = short, 2 = long) */
1931 32, /* bitsize */
1932 false, /* pc_relative */
1933 0, /* bitpos */
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 */
1944 0, /* rightshift */
1945 2, /* size (0 = byte, 1 = short, 2 = long) */
1946 32, /* bitsize */
1947 false, /* pc_relative */
1948 0, /* bitpos */
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 EMPTY_HOWTO (114),
1959 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1960 0, /* rightshift */
1961 2, /* size (0 = byte, 1 = short, 2 = long) */
1962 12, /* bitsize */
1963 false, /* pc_relative */
1964 0, /* bitpos */
1965 complain_overflow_dont,/* complain_on_overflow */
1966 bfd_elf_generic_reloc, /* special_function */
1967 "R_NDS32_TLS_IE_LO12", /* name */
1968 false, /* partial_inplace */
1969 0x00000fff, /* src_mask */
1970 0x00000fff, /* dst_mask */
1971 false), /* pcrel_offset */
1973 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1974 entry for the symbol. */
1975 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1976 12, /* rightshift */
1977 2, /* size (0 = byte, 1 = short, 2 = long) */
1978 20, /* bitsize */
1979 false, /* pc_relative */
1980 0, /* bitpos */
1981 complain_overflow_dont,/* complain_on_overflow */
1982 bfd_elf_generic_reloc, /* special_function */
1983 "R_NDS32_TLS_IEGP_HI20",/* name */
1984 false, /* partial_inplace */
1985 0x000fffff, /* src_mask */
1986 0x000fffff, /* dst_mask */
1987 false), /* pcrel_offset */
1989 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1990 0, /* rightshift */
1991 2, /* size (0 = byte, 1 = short, 2 = long) */
1992 12, /* bitsize */
1993 false, /* pc_relative */
1994 0, /* bitpos */
1995 complain_overflow_dont,/* complain_on_overflow */
1996 bfd_elf_generic_reloc, /* special_function */
1997 "R_NDS32_TLS_IEGP_LO12",/* name */
1998 false, /* partial_inplace */
1999 0x00000fff, /* src_mask */
2000 0x00000fff, /* dst_mask */
2001 false), /* pcrel_offset */
2003 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2004 2, /* rightshift */
2005 2, /* size (0 = byte, 1 = short, 2 = long) */
2006 10, /* bitsize */
2007 false, /* pc_relative */
2008 0, /* bitpos */
2009 complain_overflow_dont,/* complain_on_overflow */
2010 bfd_elf_generic_reloc, /* special_function */
2011 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2012 false, /* partial_inplace */
2013 0x000003ff, /* src_mask */
2014 0x000003ff, /* dst_mask */
2015 false), /* pcrel_offset */
2017 /* TLS description relocation */
2018 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2019 12, /* rightshift */
2020 2, /* size (0 = byte, 1 = short, 2 = long) */
2021 20, /* bitsize */
2022 false, /* pc_relative */
2023 0, /* bitpos */
2024 complain_overflow_dont,/* complain_on_overflow */
2025 nds32_elf_hi20_reloc, /* special_function */
2026 "R_NDS32_TLS_DESC_HI20",/* name */
2027 false, /* partial_inplace */
2028 0x000fffff, /* src_mask */
2029 0x000fffff, /* dst_mask */
2030 false), /* pcrel_offset */
2032 /* TLS GD/LD description offset high part. */
2033 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2034 12, /* rightshift */
2035 2, /* size (0 = byte, 1 = short, 2 = long) */
2036 20, /* bitsize */
2037 false, /* pc_relative */
2038 0, /* bitpos */
2039 complain_overflow_dont,/* complain_on_overflow */
2040 nds32_elf_hi20_reloc, /* special_function */
2041 "R_NDS32_TLS_DESC_HI20",/* name */
2042 false, /* partial_inplace */
2043 0x000fffff, /* src_mask */
2044 0x000fffff, /* dst_mask */
2045 false), /* pcrel_offset */
2047 /* TLS GD/LD description offset low part. */
2048 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2049 0, /* rightshift */
2050 2, /* size (0 = byte, 1 = short, 2 = long) */
2051 12, /* bitsize */
2052 false, /* pc_relative */
2053 0, /* bitpos */
2054 complain_overflow_dont,/* complain_on_overflow */
2055 nds32_elf_lo12_reloc, /* special_function */
2056 "R_NDS32_TLS_DESC_LO12",/* name */
2057 false, /* partial_inplace */
2058 0x00000fff, /* src_mask */
2059 0x00000fff, /* dst_mask */
2060 false), /* pcrel_offset */
2062 /* TLS GD/LD description offset set (movi). */
2063 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2064 0, /* rightshift */
2065 2, /* size (0 = byte, 1 = short, 2 = long) */
2066 20, /* bitsize */
2067 false, /* pc_relative */
2068 0, /* bitpos */
2069 complain_overflow_signed,/* complain_on_overflow */
2070 bfd_elf_generic_reloc, /* special_function */
2071 "R_NDS32_TLS_DESC_20", /* name */
2072 false, /* partial_inplace */
2073 0x000fffff, /* src_mask */
2074 0x000fffff, /* dst_mask */
2075 false), /* pcrel_offset */
2077 /* TLS GD/LD description offset set (lwi.gp). */
2078 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2079 2, /* rightshift */
2080 2, /* size (0 = byte, 1 = short, 2 = long) */
2081 17, /* bitsize */
2082 false, /* pc_relative */
2083 0, /* bitpos */
2084 complain_overflow_signed,/* complain_on_overflow */
2085 bfd_elf_generic_reloc, /* special_function */
2086 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2087 false, /* partial_inplace */
2088 0x0001ffff, /* src_mask */
2089 0x0001ffff, /* dst_mask */
2090 false), /* pcrel_offset */
2093 /* Relocations used for relaxation. */
2094 #define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2095 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2097 static reloc_howto_type nds32_elf_relax_howto_table[] = {
2098 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2099 0, /* rightshift */
2100 2, /* size (0 = byte, 1 = short, 2 = long) */
2101 32, /* bitsize */
2102 false, /* pc_relative */
2103 0, /* bitpos */
2104 complain_overflow_dont,/* complain_on_overflow */
2105 nds32_elf_ignore_reloc,/* special_function */
2106 "R_NDS32_RELAX_ENTRY", /* name */
2107 false, /* partial_inplace */
2108 0xffffffff, /* src_mask */
2109 0xffffffff, /* dst_mask */
2110 false), /* pcrel_offset */
2111 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2112 0, /* rightshift */
2113 2, /* size (0 = byte, 1 = short, 2 = long) */
2114 32, /* bitsize */
2115 false, /* pc_relative */
2116 0, /* bitpos */
2117 complain_overflow_dont,/* complain_on_overflow */
2118 nds32_elf_ignore_reloc,/* special_function */
2119 "R_NDS32_GOT_SUFF", /* name */
2120 false, /* partial_inplace */
2121 0xffffffff, /* src_mask */
2122 0xffffffff, /* dst_mask */
2123 false), /* pcrel_offset */
2124 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2125 0, /* rightshift */
2126 2, /* size (0 = byte, 1 = short, 2 = long) */
2127 32, /* bitsize */
2128 false, /* pc_relative */
2129 0, /* bitpos */
2130 complain_overflow_bitfield,/* complain_on_overflow */
2131 nds32_elf_ignore_reloc,/* special_function */
2132 "R_NDS32_GOTOFF_SUFF", /* name */
2133 false, /* partial_inplace */
2134 0xffffffff, /* src_mask */
2135 0xffffffff, /* dst_mask */
2136 false), /* pcrel_offset */
2137 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2138 0, /* rightshift */
2139 2, /* size (0 = byte, 1 = short, 2 = long) */
2140 32, /* bitsize */
2141 false, /* pc_relative */
2142 0, /* bitpos */
2143 complain_overflow_dont,/* complain_on_overflow */
2144 nds32_elf_ignore_reloc,/* special_function */
2145 "R_NDS32_PLT_GOT_SUFF",/* name */
2146 false, /* partial_inplace */
2147 0xffffffff, /* src_mask */
2148 0xffffffff, /* dst_mask */
2149 false), /* pcrel_offset */
2150 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2151 0, /* rightshift */
2152 2, /* size (0 = byte, 1 = short, 2 = long) */
2153 32, /* bitsize */
2154 false, /* pc_relative */
2155 0, /* bitpos */
2156 complain_overflow_dont,/* complain_on_overflow */
2157 nds32_elf_ignore_reloc,/* special_function */
2158 "R_NDS32_MULCALL_SUFF",/* name */
2159 false, /* partial_inplace */
2160 0xffffffff, /* src_mask */
2161 0xffffffff, /* dst_mask */
2162 false), /* pcrel_offset */
2163 HOWTO3 (R_NDS32_PTR, /* type */
2164 0, /* rightshift */
2165 2, /* size (0 = byte, 1 = short, 2 = long) */
2166 32, /* bitsize */
2167 false, /* pc_relative */
2168 0, /* bitpos */
2169 complain_overflow_dont,/* complain_on_overflow */
2170 nds32_elf_ignore_reloc,/* special_function */
2171 "R_NDS32_PTR", /* name */
2172 false, /* partial_inplace */
2173 0xffffffff, /* src_mask */
2174 0xffffffff, /* dst_mask */
2175 false), /* pcrel_offset */
2176 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2177 0, /* rightshift */
2178 2, /* size (0 = byte, 1 = short, 2 = long) */
2179 32, /* bitsize */
2180 false, /* pc_relative */
2181 0, /* bitpos */
2182 complain_overflow_dont,/* complain_on_overflow */
2183 nds32_elf_ignore_reloc,/* special_function */
2184 "R_NDS32_PTR_COUNT", /* name */
2185 false, /* partial_inplace */
2186 0xffffffff, /* src_mask */
2187 0xffffffff, /* dst_mask */
2188 false), /* pcrel_offset */
2189 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2190 0, /* rightshift */
2191 2, /* size (0 = byte, 1 = short, 2 = long) */
2192 32, /* bitsize */
2193 false, /* pc_relative */
2194 0, /* bitpos */
2195 complain_overflow_dont,/* complain_on_overflow */
2196 nds32_elf_ignore_reloc,/* special_function */
2197 "R_NDS32_PTR_RESOLVED",/* name */
2198 false, /* partial_inplace */
2199 0xffffffff, /* src_mask */
2200 0xffffffff, /* dst_mask */
2201 false), /* pcrel_offset */
2202 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2203 0, /* rightshift */
2204 2, /* size (0 = byte, 1 = short, 2 = long) */
2205 32, /* bitsize */
2206 false, /* pc_relative */
2207 0, /* bitpos */
2208 complain_overflow_dont,/* complain_on_overflow */
2209 nds32_elf_ignore_reloc,/* special_function */
2210 "R_NDS32_PLTBLOCK", /* name */
2211 false, /* partial_inplace */
2212 0xffffffff, /* src_mask */
2213 0xffffffff, /* dst_mask */
2214 false), /* pcrel_offset */
2215 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2216 0, /* rightshift */
2217 2, /* size (0 = byte, 1 = short, 2 = long) */
2218 32, /* bitsize */
2219 false, /* pc_relative */
2220 0, /* bitpos */
2221 complain_overflow_dont,/* complain_on_overflow */
2222 nds32_elf_ignore_reloc,/* special_function */
2223 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2224 false, /* partial_inplace */
2225 0xffffffff, /* src_mask */
2226 0xffffffff, /* dst_mask */
2227 false), /* pcrel_offset */
2228 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2229 0, /* rightshift */
2230 2, /* size (0 = byte, 1 = short, 2 = long) */
2231 32, /* bitsize */
2232 false, /* pc_relative */
2233 0, /* bitpos */
2234 complain_overflow_dont,/* complain_on_overflow */
2235 nds32_elf_ignore_reloc,/* special_function */
2236 "R_NDS32_RELAX_REGION_END",/* name */
2237 false, /* partial_inplace */
2238 0xffffffff, /* src_mask */
2239 0xffffffff, /* dst_mask */
2240 false), /* pcrel_offset */
2241 HOWTO3 (R_NDS32_MINUEND, /* type */
2242 0, /* rightshift */
2243 2, /* size (0 = byte, 1 = short, 2 = long) */
2244 32, /* bitsize */
2245 false, /* pc_relative */
2246 0, /* bitpos */
2247 complain_overflow_dont,/* complain_on_overflow */
2248 nds32_elf_ignore_reloc,/* special_function */
2249 "R_NDS32_MINUEND", /* name */
2250 false, /* partial_inplace */
2251 0xffffffff, /* src_mask */
2252 0xffffffff, /* dst_mask */
2253 false), /* pcrel_offset */
2254 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2255 0, /* rightshift */
2256 2, /* size (0 = byte, 1 = short, 2 = long) */
2257 32, /* bitsize */
2258 false, /* pc_relative */
2259 0, /* bitpos */
2260 complain_overflow_dont,/* complain_on_overflow */
2261 nds32_elf_ignore_reloc,/* special_function */
2262 "R_NDS32_SUBTRAHEND", /* name */
2263 false, /* partial_inplace */
2264 0xffffffff, /* src_mask */
2265 0xffffffff, /* dst_mask */
2266 false), /* pcrel_offset */
2267 HOWTO3 (R_NDS32_DIFF8, /* type */
2268 0, /* rightshift */
2269 0, /* size (0 = byte, 1 = short, 2 = long) */
2270 8, /* bitsize */
2271 false, /* pc_relative */
2272 0, /* bitpos */
2273 complain_overflow_dont,/* complain_on_overflow */
2274 nds32_elf_ignore_reloc,/* special_function */
2275 "R_NDS32_DIFF8", /* name */
2276 false, /* partial_inplace */
2277 0x000000ff, /* src_mask */
2278 0x000000ff, /* dst_mask */
2279 false), /* pcrel_offset */
2280 HOWTO3 (R_NDS32_DIFF16, /* type */
2281 0, /* rightshift */
2282 1, /* size (0 = byte, 1 = short, 2 = long) */
2283 16, /* bitsize */
2284 false, /* pc_relative */
2285 0, /* bitpos */
2286 complain_overflow_dont,/* complain_on_overflow */
2287 nds32_elf_ignore_reloc,/* special_function */
2288 "R_NDS32_DIFF16", /* name */
2289 false, /* partial_inplace */
2290 0x0000ffff, /* src_mask */
2291 0x0000ffff, /* dst_mask */
2292 false), /* pcrel_offset */
2293 HOWTO3 (R_NDS32_DIFF32, /* type */
2294 0, /* rightshift */
2295 2, /* size (0 = byte, 1 = short, 2 = long) */
2296 32, /* bitsize */
2297 false, /* pc_relative */
2298 0, /* bitpos */
2299 complain_overflow_dont,/* complain_on_overflow */
2300 nds32_elf_ignore_reloc,/* special_function */
2301 "R_NDS32_DIFF32", /* name */
2302 false, /* partial_inplace */
2303 0xffffffff, /* src_mask */
2304 0xffffffff, /* dst_mask */
2305 false), /* pcrel_offset */
2306 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2307 0, /* rightshift */
2308 0, /* size (0 = byte, 1 = short, 2 = long) */
2309 0, /* bitsize */
2310 false, /* pc_relative */
2311 0, /* bitpos */
2312 complain_overflow_dont,/* complain_on_overflow */
2313 nds32_elf_ignore_reloc,/* special_function */
2314 "R_NDS32_DIFF_ULEB128",/* name */
2315 false, /* partial_inplace */
2316 0xffffffff, /* src_mask */
2317 0xffffffff, /* dst_mask */
2318 false), /* pcrel_offset */
2319 HOWTO3 (R_NDS32_DATA, /* type */
2320 0, /* rightshift */
2321 2, /* size (0 = byte, 1 = short, 2 = long) */
2322 32, /* bitsize */
2323 false, /* pc_relative */
2324 0, /* bitpos */
2325 complain_overflow_dont,/* complain_on_overflow */
2326 nds32_elf_ignore_reloc,/* special_function */
2327 "R_NDS32_DATA", /* name */
2328 false, /* partial_inplace */
2329 0xffffffff, /* src_mask */
2330 0xffffffff, /* dst_mask */
2331 false), /* pcrel_offset */
2332 HOWTO3 (R_NDS32_TRAN, /* type */
2333 0, /* rightshift */
2334 2, /* size (0 = byte, 1 = short, 2 = long) */
2335 32, /* bitsize */
2336 false, /* pc_relative */
2337 0, /* bitpos */
2338 complain_overflow_dont,/* complain_on_overflow */
2339 nds32_elf_ignore_reloc,/* special_function */
2340 "R_NDS32_TRAN", /* name */
2341 false, /* partial_inplace */
2342 0xffffffff, /* src_mask */
2343 0xffffffff, /* dst_mask */
2344 false), /* pcrel_offset */
2345 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2346 0, /* rightshift */
2347 2, /* size (0 = byte, 1 = short, 2 = long) */
2348 32, /* bitsize */
2349 false, /* pc_relative */
2350 0, /* bitpos */
2351 complain_overflow_dont,/* complain_on_overflow */
2352 nds32_elf_ignore_reloc,/* special_function */
2353 "R_NDS32_TLS_LE_ADD", /* name */
2354 false, /* partial_inplace */
2355 0xffffffff, /* src_mask */
2356 0xffffffff, /* dst_mask */
2357 false), /* pcrel_offset */
2358 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2359 0, /* rightshift */
2360 2, /* size (0 = byte, 1 = short, 2 = long) */
2361 32, /* bitsize */
2362 false, /* pc_relative */
2363 0, /* bitpos */
2364 complain_overflow_dont,/* complain_on_overflow */
2365 nds32_elf_ignore_reloc,/* special_function */
2366 "R_NDS32_TLS_LE_LS", /* name */
2367 false, /* partial_inplace */
2368 0xffffffff, /* src_mask */
2369 0xffffffff, /* dst_mask */
2370 false), /* pcrel_offset */
2371 HOWTO3 (R_NDS32_EMPTY, /* type */
2372 0, /* rightshift */
2373 2, /* size (0 = byte, 1 = short, 2 = long) */
2374 32, /* bitsize */
2375 false, /* pc_relative */
2376 0, /* bitpos */
2377 complain_overflow_dont,/* complain_on_overflow */
2378 nds32_elf_ignore_reloc,/* special_function */
2379 "R_NDS32_EMPTY", /* name */
2380 false, /* partial_inplace */
2381 0xffffffff, /* src_mask */
2382 0xffffffff, /* dst_mask */
2383 false), /* pcrel_offset */
2384 /* TLS GD/LD description address base addition. */
2385 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2386 0, /* rightshift */
2387 2, /* size (0 = byte, 1 = short, 2 = long) */
2388 32, /* bitsize */
2389 false, /* pc_relative */
2390 0, /* bitpos */
2391 complain_overflow_dont,/* complain_on_overflow */
2392 nds32_elf_ignore_reloc,/* special_function */
2393 "R_NDS32_TLS_DESC_ADD",/* name */
2394 false, /* partial_inplace */
2395 0xffffffff, /* src_mask */
2396 0xffffffff, /* dst_mask */
2397 false), /* pcrel_offset */
2398 /* TLS GD/LD description function load. */
2399 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2400 0, /* rightshift */
2401 2, /* size (0 = byte, 1 = short, 2 = long) */
2402 32, /* bitsize */
2403 false, /* pc_relative */
2404 0, /* bitpos */
2405 complain_overflow_dont,/* complain_on_overflow */
2406 nds32_elf_ignore_reloc,/* special_function */
2407 "R_NDS32_TLS_DESC_FUNC",/* name */
2408 false, /* partial_inplace */
2409 0xffffffff, /* src_mask */
2410 0xffffffff, /* dst_mask */
2411 false), /* pcrel_offset */
2412 /* TLS DESC resolve function call. */
2413 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2414 0, /* rightshift */
2415 2, /* size (0 = byte, 1 = short, 2 = long) */
2416 32, /* bitsize */
2417 false, /* pc_relative */
2418 0, /* bitpos */
2419 complain_overflow_dont,/* complain_on_overflow */
2420 nds32_elf_ignore_reloc,/* special_function */
2421 "R_NDS32_TLS_DESC_CALL",/* name */
2422 false, /* partial_inplace */
2423 0xffffffff, /* src_mask */
2424 0xffffffff, /* dst_mask */
2425 false), /* pcrel_offset */
2426 /* TLS DESC variable access. */
2427 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2428 0, /* rightshift */
2429 2, /* size (0 = byte, 1 = short, 2 = long) */
2430 32, /* bitsize */
2431 false, /* pc_relative */
2432 0, /* bitpos */
2433 complain_overflow_dont,/* complain_on_overflow */
2434 nds32_elf_ignore_reloc,/* special_function */
2435 "R_NDS32_TLS_DESC_MEM",/* name */
2436 false, /* partial_inplace */
2437 0xffffffff, /* src_mask */
2438 0xffffffff, /* dst_mask */
2439 false), /* pcrel_offset */
2440 /* TLS GD/LD description mark (@tlsdec). */
2441 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2442 0, /* rightshift */
2443 2, /* size (0 = byte, 1 = short, 2 = long) */
2444 32, /* bitsize */
2445 false, /* pc_relative */
2446 0, /* bitpos */
2447 complain_overflow_dont,/* complain_on_overflow */
2448 nds32_elf_ignore_reloc,/* special_function */
2449 "R_NDS32_REMOVE", /* name */
2450 false, /* partial_inplace */
2451 0xffffffff, /* src_mask */
2452 0xffffffff, /* dst_mask */
2453 false), /* pcrel_offset */
2454 /* TLS GD/LD description mark (@tlsdec). */
2455 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2456 0, /* rightshift */
2457 2, /* size (0 = byte, 1 = short, 2 = long) */
2458 32, /* bitsize */
2459 false, /* pc_relative */
2460 0, /* bitpos */
2461 complain_overflow_dont,/* complain_on_overflow */
2462 nds32_elf_ignore_reloc,/* special_function */
2463 "R_NDS32_GROUP", /* name */
2464 false, /* partial_inplace */
2465 0xffffffff, /* src_mask */
2466 0xffffffff, /* dst_mask */
2467 false), /* pcrel_offset */
2468 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2469 0, /* rightshift */
2470 2, /* size (0 = byte, 1 = short, 2 = long) */
2471 32, /* bitsize */
2472 false, /* pc_relative */
2473 0, /* bitpos */
2474 complain_overflow_dont,/* complain_on_overflow */
2475 nds32_elf_ignore_reloc,/* special_function */
2476 "R_NDS32_TLS_IEGP_LW", /* name */
2477 false, /* partial_inplace */
2478 0xffffffff, /* src_mask */
2479 0xffffffff, /* dst_mask */
2480 false), /* pcrel_offset */
2481 /* LA and FLSI relaxation. */
2482 HOWTO3 (R_NDS32_LSI, /* type */
2483 0, /* rightshift */
2484 2, /* size (0 = byte, 1 = short, 2 = long) */
2485 32, /* bitsize */
2486 false, /* pc_relative */
2487 0, /* bitpos */
2488 complain_overflow_dont,/* complain_on_overflow */
2489 nds32_elf_ignore_reloc,/* special_function */
2490 "R_NDS32_LSI", /* name */
2491 false, /* partial_inplace */
2492 0xffffffff, /* src_mask */
2493 0xffffffff, /* dst_mask */
2494 false),
2497 static unsigned long dl_tlsdesc_lazy_trampoline[] =
2499 0x46200000, /* sethi $r2,#0x0 */
2500 0x58210000, /* ori $r2,$r2,#0x0 */
2501 0x40217400, /* add $r2,$r2,$gp */
2502 0x04210000, /* lwi $r2,[$r2+#0x0] */
2503 0x46300000, /* sethi $r3,#0x0 */
2504 0x58318000, /* ori $r3,$r3,#0x0 */
2505 0x4031f400, /* add $r3,$r3,$gp */
2506 0x4a000800, /* jr $r2 */
2509 static void
2510 nds32_put_trampoline (void *contents, const unsigned long *template,
2511 unsigned count)
2513 unsigned ix;
2515 for (ix = 0; ix != count; ix++)
2517 unsigned long insn = template[ix];
2518 bfd_putb32 (insn, (char *) contents + ix * 4);
2522 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2523 This prototype is the same as qsort (). */
2525 static void
2526 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2527 int (*compar) (const void *lhs, const void *rhs))
2529 char *ptr = (char *) base;
2530 int i, j;
2531 char tmp[sizeof (Elf_Internal_Rela)];
2533 BFD_ASSERT (size <= sizeof (tmp));
2535 /* If i is less than j, i is inserted before j.
2537 |---- j ----- i --------------|
2538 \ / \ /
2539 sorted unsorted
2542 for (i = 1; i < (int) nmemb; i++)
2544 for (j = (i - 1); j >= 0; j--)
2545 if (compar (ptr + i * size, ptr + j * size) >= 0)
2546 break;
2548 j++;
2550 if (i == j)
2551 continue; /* i is in order. */
2553 memcpy (tmp, ptr + i * size, size);
2554 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2555 memcpy (ptr + j * size, tmp, size);
2559 /* Sort relocation by r_offset.
2561 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2562 algorithm. Relocations at the same r_offset must keep their order.
2563 For example, RELAX_ENTRY must be the very first relocation entry.
2565 Currently, this function implements insertion-sort.
2567 FIXME: If we already sort them in assembler, why bother sort them
2568 here again? */
2570 static int
2571 compar_reloc (const void *lhs, const void *rhs)
2573 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2574 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2576 if (l->r_offset > r->r_offset)
2577 return 1;
2578 else if (l->r_offset == r->r_offset)
2579 return 0;
2580 else
2581 return -1;
2584 /* Functions listed below are only used for old relocs.
2585 nds32_elf_9_pcrel_reloc
2586 nds32_elf_do_9_pcrel_reloc
2587 nds32_elf_hi20_reloc
2588 nds32_elf_relocate_hi20
2589 nds32_elf_lo12_reloc
2590 nds32_elf_sda15_reloc
2591 nds32_elf_generic_reloc. */
2593 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2595 static bfd_reloc_status_type
2596 nds32_elf_9_pcrel_reloc (bfd * abfd,
2597 arelent * reloc_entry,
2598 asymbol * symbol,
2599 void * data,
2600 asection * input_section,
2601 bfd * output_bfd,
2602 char ** error_message ATTRIBUTE_UNUSED)
2604 /* This part is from bfd_elf_generic_reloc. */
2605 if (output_bfd != (bfd *) NULL
2606 && (symbol->flags & BSF_SECTION_SYM) == 0
2607 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2609 reloc_entry->address += input_section->output_offset;
2610 return bfd_reloc_ok;
2613 if (output_bfd != NULL)
2615 /* FIXME: See bfd_perform_relocation. Is this right? */
2616 return bfd_reloc_continue;
2619 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2620 input_section,
2621 data, reloc_entry->address,
2622 symbol->section,
2623 (symbol->value
2624 + symbol->section->output_section->vma
2625 + symbol->section->output_offset),
2626 reloc_entry->addend);
2629 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2630 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2632 static bfd_reloc_status_type
2633 nds32_elf_do_9_pcrel_reloc (bfd * abfd,
2634 reloc_howto_type * howto,
2635 asection * input_section,
2636 bfd_byte * data,
2637 bfd_vma offset,
2638 asection * symbol_section ATTRIBUTE_UNUSED,
2639 bfd_vma symbol_value,
2640 bfd_vma addend)
2642 bfd_signed_vma relocation;
2643 unsigned short x;
2644 bfd_reloc_status_type status;
2646 /* Sanity check the address (offset in section). */
2647 if (offset > bfd_get_section_limit (abfd, input_section))
2648 return bfd_reloc_outofrange;
2650 relocation = symbol_value + addend;
2651 /* Make it pc relative. */
2652 relocation -= (input_section->output_section->vma
2653 + input_section->output_offset);
2654 /* These jumps mask off the lower two bits of the current address
2655 before doing pcrel calculations. */
2656 relocation -= (offset & -(bfd_vma) 2);
2658 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2659 status = bfd_reloc_overflow;
2660 else
2661 status = bfd_reloc_ok;
2663 x = bfd_getb16 (data + offset);
2665 relocation >>= howto->rightshift;
2666 relocation <<= howto->bitpos;
2667 x = (x & ~howto->dst_mask)
2668 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2670 bfd_putb16 ((bfd_vma) x, data + offset);
2672 return status;
2675 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2676 HI20_SLO is for the add3 and load/store with displacement instructions.
2677 HI20 is for the or3 instruction.
2678 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2679 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2680 we must add one to the high 16 bytes (which will get subtracted off when
2681 the low 16 bits are added).
2682 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2683 because there is a carry from the LO12 to the HI20. Here we just save
2684 the information we need; we do the actual relocation when we see the LO12.
2685 This code is copied from the elf32-mips.c. We also support an arbitrary
2686 number of HI20 relocs to be associated with a single LO12 reloc. The
2687 assembler sorts the relocs to ensure each HI20 immediately precedes its
2688 LO12. However if there are multiple copies, the assembler may not find
2689 the real LO12 so it picks the first one it finds. */
2691 struct nds32_hi20
2693 struct nds32_hi20 *next;
2694 bfd_byte *addr;
2695 bfd_vma addend;
2698 static struct nds32_hi20 *nds32_hi20_list;
2700 static bfd_reloc_status_type
2701 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2702 arelent *reloc_entry,
2703 asymbol *symbol,
2704 void *data,
2705 asection *input_section,
2706 bfd *output_bfd,
2707 char **error_message ATTRIBUTE_UNUSED)
2709 bfd_reloc_status_type ret;
2710 bfd_vma relocation;
2711 struct nds32_hi20 *n;
2713 /* This part is from bfd_elf_generic_reloc.
2714 If we're relocating, and this an external symbol, we don't want
2715 to change anything. */
2716 if (output_bfd != (bfd *) NULL
2717 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2719 reloc_entry->address += input_section->output_offset;
2720 return bfd_reloc_ok;
2723 /* Sanity check the address (offset in section). */
2724 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2725 return bfd_reloc_outofrange;
2727 ret = bfd_reloc_ok;
2728 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2729 ret = bfd_reloc_undefined;
2731 if (bfd_is_com_section (symbol->section))
2732 relocation = 0;
2733 else
2734 relocation = symbol->value;
2736 relocation += symbol->section->output_section->vma;
2737 relocation += symbol->section->output_offset;
2738 relocation += reloc_entry->addend;
2740 /* Save the information, and let LO12 do the actual relocation. */
2741 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2742 if (n == NULL)
2743 return bfd_reloc_outofrange;
2745 n->addr = (bfd_byte *) data + reloc_entry->address;
2746 n->addend = relocation;
2747 n->next = nds32_hi20_list;
2748 nds32_hi20_list = n;
2750 if (output_bfd != (bfd *) NULL)
2751 reloc_entry->address += input_section->output_offset;
2753 return ret;
2756 /* Handle an NDS32 ELF HI20 reloc. */
2758 static void
2759 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2760 int type ATTRIBUTE_UNUSED,
2761 Elf_Internal_Rela *relhi,
2762 Elf_Internal_Rela *rello,
2763 bfd_byte *contents,
2764 bfd_vma addend)
2766 unsigned long insn;
2767 bfd_vma addlo;
2769 insn = bfd_getb32 (contents + relhi->r_offset);
2771 addlo = bfd_getb32 (contents + rello->r_offset);
2772 addlo &= 0xfff;
2774 addend += ((insn & 0xfffff) << 20) + addlo;
2776 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2777 bfd_putb32 (insn, contents + relhi->r_offset);
2780 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2781 inplace relocation; this function exists in order to do the
2782 R_NDS32_HI20_[SU]LO relocation described above. */
2784 static bfd_reloc_status_type
2785 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2786 void *data, asection *input_section, bfd *output_bfd,
2787 char **error_message)
2789 /* This part is from bfd_elf_generic_reloc.
2790 If we're relocating, and this an external symbol, we don't want
2791 to change anything. */
2792 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2793 && reloc_entry->addend == 0)
2795 reloc_entry->address += input_section->output_offset;
2796 return bfd_reloc_ok;
2799 if (nds32_hi20_list != NULL)
2801 struct nds32_hi20 *l;
2803 l = nds32_hi20_list;
2804 while (l != NULL)
2806 unsigned long insn;
2807 unsigned long val;
2808 unsigned long vallo;
2809 struct nds32_hi20 *next;
2811 /* Do the HI20 relocation. Note that we actually don't need
2812 to know anything about the LO12 itself, except where to
2813 find the low 12 bits of the addend needed by the LO12. */
2814 insn = bfd_getb32 (l->addr);
2815 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2816 vallo &= 0xfff;
2817 switch (reloc_entry->howto->type)
2819 case R_NDS32_LO12S3:
2820 vallo <<= 3;
2821 break;
2823 case R_NDS32_LO12S2:
2824 vallo <<= 2;
2825 break;
2827 case R_NDS32_LO12S1:
2828 vallo <<= 1;
2829 break;
2831 case R_NDS32_LO12S0:
2832 vallo <<= 0;
2833 break;
2836 val = ((insn & 0xfffff) << 12) + vallo;
2837 val += l->addend;
2839 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2840 bfd_putb32 ((bfd_vma) insn, l->addr);
2842 next = l->next;
2843 free (l);
2844 l = next;
2847 nds32_hi20_list = NULL;
2850 /* Now do the LO12 reloc in the usual way.
2851 ??? It would be nice to call bfd_elf_generic_reloc here,
2852 but we have partial_inplace set. bfd_elf_generic_reloc will
2853 pass the handling back to bfd_install_relocation which will install
2854 a section relative addend which is wrong. */
2855 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2856 input_section, output_bfd, error_message);
2859 /* Do generic partial_inplace relocation.
2860 This is a local replacement for bfd_elf_generic_reloc. */
2862 static bfd_reloc_status_type
2863 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2864 asymbol *symbol, void *data, asection *input_section,
2865 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2867 bfd_reloc_status_type ret;
2868 bfd_vma relocation;
2869 bfd_byte *inplace_address;
2871 /* This part is from bfd_elf_generic_reloc.
2872 If we're relocating, and this an external symbol, we don't want
2873 to change anything. */
2874 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2875 && reloc_entry->addend == 0)
2877 reloc_entry->address += input_section->output_offset;
2878 return bfd_reloc_ok;
2881 /* Now do the reloc in the usual way.
2882 ??? It would be nice to call bfd_elf_generic_reloc here,
2883 but we have partial_inplace set. bfd_elf_generic_reloc will
2884 pass the handling back to bfd_install_relocation which will install
2885 a section relative addend which is wrong. */
2887 /* Sanity check the address (offset in section). */
2888 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2889 return bfd_reloc_outofrange;
2891 ret = bfd_reloc_ok;
2892 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2893 ret = bfd_reloc_undefined;
2895 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2896 relocation = 0;
2897 else
2898 relocation = symbol->value;
2900 /* Only do this for a final link. */
2901 if (output_bfd == (bfd *) NULL)
2903 relocation += symbol->section->output_section->vma;
2904 relocation += symbol->section->output_offset;
2907 relocation += reloc_entry->addend;
2908 switch (reloc_entry->howto->type)
2910 case R_NDS32_LO12S3:
2911 relocation >>= 3;
2912 break;
2914 case R_NDS32_LO12S2:
2915 relocation >>= 2;
2916 break;
2918 case R_NDS32_LO12S1:
2919 relocation >>= 1;
2920 break;
2922 case R_NDS32_LO12S0:
2923 default:
2924 relocation >>= 0;
2925 break;
2928 inplace_address = (bfd_byte *) data + reloc_entry->address;
2930 #define DOIT(x) \
2931 x = ((x & ~reloc_entry->howto->dst_mask) | \
2932 (((x & reloc_entry->howto->src_mask) + relocation) & \
2933 reloc_entry->howto->dst_mask))
2935 switch (reloc_entry->howto->size)
2937 case 1:
2939 short x = bfd_getb16 (inplace_address);
2941 DOIT (x);
2942 bfd_putb16 ((bfd_vma) x, inplace_address);
2944 break;
2945 case 2:
2947 unsigned long x = bfd_getb32 (inplace_address);
2949 DOIT (x);
2950 bfd_putb32 ((bfd_vma) x, inplace_address);
2952 break;
2953 default:
2954 BFD_ASSERT (0);
2957 if (output_bfd != (bfd *) NULL)
2958 reloc_entry->address += input_section->output_offset;
2960 return ret;
2963 /* Handle the R_NDS32_SDA15 reloc.
2964 This reloc is used to compute the address of objects in the small data area
2965 and to perform loads and stores from that area.
2966 The lower 15 bits are sign extended and added to the register specified
2967 in the instruction, which is assumed to point to _SDA_BASE_.
2969 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2970 the access size, this must be taken care of. */
2972 static bfd_reloc_status_type
2973 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2974 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2975 asection *input_section, bfd *output_bfd,
2976 char **error_message ATTRIBUTE_UNUSED)
2978 /* This part is from bfd_elf_generic_reloc. */
2979 if (output_bfd != (bfd *) NULL
2980 && (symbol->flags & BSF_SECTION_SYM) == 0
2981 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2983 reloc_entry->address += input_section->output_offset;
2984 return bfd_reloc_ok;
2987 if (output_bfd != NULL)
2989 /* FIXME: See bfd_perform_relocation. Is this right? */
2990 return bfd_reloc_continue;
2993 /* FIXME: not sure what to do here yet. But then again, the linker
2994 may never call us. */
2995 abort ();
2998 /* nds32_elf_ignore_reloc is the special function for
2999 relocation types which don't need to be relocated
3000 like relaxation relocation types.
3001 This function simply return bfd_reloc_ok when it is
3002 invoked. */
3004 static bfd_reloc_status_type
3005 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3006 asymbol *symbol ATTRIBUTE_UNUSED,
3007 void *data ATTRIBUTE_UNUSED, asection *input_section,
3008 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3010 if (output_bfd != NULL)
3011 reloc_entry->address += input_section->output_offset;
3013 return bfd_reloc_ok;
3017 /* Map BFD reloc types to NDS32 ELF reloc types. */
3019 struct nds32_reloc_map_entry
3021 bfd_reloc_code_real_type bfd_reloc_val;
3022 unsigned char elf_reloc_val;
3025 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3027 {BFD_RELOC_NONE, R_NDS32_NONE},
3028 {BFD_RELOC_16, R_NDS32_16_RELA},
3029 {BFD_RELOC_32, R_NDS32_32_RELA},
3030 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3031 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3033 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3034 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3035 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3036 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3037 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3038 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3039 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3040 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3041 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3042 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3043 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3044 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3045 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3046 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3047 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3048 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3049 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3050 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3051 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3052 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3053 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3054 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3055 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3056 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3057 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3058 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3059 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3060 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3061 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3062 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3063 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3064 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3065 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3066 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3067 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3068 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3069 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3070 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3071 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3072 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3073 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3074 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3075 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3076 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3077 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3078 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3079 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3080 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3081 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3082 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3083 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3084 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3085 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3086 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3087 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3088 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3089 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3090 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3091 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3092 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3093 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3094 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3095 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3096 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3097 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3098 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3099 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3100 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3101 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3102 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3103 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3104 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3105 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3106 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3107 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3108 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3109 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3110 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3111 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3112 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3113 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3114 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3115 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3116 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3117 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3118 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3119 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3120 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3121 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3122 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3123 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3124 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3125 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3126 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3127 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3128 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3129 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3130 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3131 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3132 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3133 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3134 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3135 /* Not sure. */
3136 {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3137 /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
3138 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3139 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3140 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3141 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3142 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3143 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3144 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3145 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3146 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3147 {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3148 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3149 {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3150 {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3151 {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3152 {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3153 {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3154 {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3155 {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3156 {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3157 {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3158 {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3159 {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3160 {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3161 {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3162 {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3163 {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3164 {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3167 /* Patch tag. */
3169 static reloc_howto_type *
3170 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3171 const char *r_name)
3173 unsigned int i;
3175 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3176 if (nds32_elf_howto_table[i].name != NULL
3177 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3178 return &nds32_elf_howto_table[i];
3180 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3181 if (nds32_elf_relax_howto_table[i].name != NULL
3182 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3183 return &nds32_elf_relax_howto_table[i];
3185 return NULL;
3188 static reloc_howto_type *
3189 bfd_elf32_bfd_reloc_type_table_lookup (unsigned int code)
3191 if (code < R_NDS32_RELAX_ENTRY)
3193 if (code < ARRAY_SIZE (nds32_elf_howto_table))
3194 return &nds32_elf_howto_table[code];
3196 else
3198 if (code - R_NDS32_RELAX_ENTRY < ARRAY_SIZE (nds32_elf_relax_howto_table))
3199 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3201 return NULL;
3204 static reloc_howto_type *
3205 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3206 bfd_reloc_code_real_type code)
3208 unsigned int i;
3210 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3212 if (nds32_reloc_map[i].bfd_reloc_val == code)
3213 return bfd_elf32_bfd_reloc_type_table_lookup
3214 (nds32_reloc_map[i].elf_reloc_val);
3217 return NULL;
3220 /* Set the howto pointer for an NDS32 ELF reloc. */
3222 static bool
3223 nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3224 Elf_Internal_Rela *dst)
3226 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3228 cache_ptr->howto = NULL;
3229 if (r_type <= R_NDS32_GNU_VTENTRY)
3230 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3231 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3233 /* xgettext:c-format */
3234 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3235 abfd, r_type);
3236 bfd_set_error (bfd_error_bad_value);
3237 return false;
3239 return true;
3242 static bool
3243 nds32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3244 Elf_Internal_Rela *dst)
3246 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3248 cache_ptr->howto = NULL;
3249 if (r_type == R_NDS32_NONE
3250 || r_type > R_NDS32_GNU_VTENTRY)
3251 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3252 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3254 /* xgettext:c-format */
3255 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3256 abfd, r_type);
3257 bfd_set_error (bfd_error_bad_value);
3258 return false;
3260 return true;
3263 /* Support for core dump NOTE sections.
3264 Reference to include/linux/elfcore.h in Linux. */
3266 static bool
3267 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3269 int offset;
3270 size_t size;
3272 switch (note->descsz)
3274 case 0x114:
3275 /* Linux/NDS32 32-bit, ABI1. */
3277 /* pr_cursig */
3278 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3280 /* pr_pid */
3281 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3283 /* pr_reg */
3284 offset = 72;
3285 size = 200;
3286 break;
3288 case 0xfc:
3289 /* Linux/NDS32 32-bit. */
3291 /* pr_cursig */
3292 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3294 /* pr_pid */
3295 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3297 /* pr_reg */
3298 offset = 72;
3299 size = 176;
3300 break;
3302 default:
3303 return false;
3306 /* Make a ".reg" section. */
3307 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3308 size, note->descpos + offset);
3311 static bool
3312 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3314 switch (note->descsz)
3316 case 124:
3317 /* Linux/NDS32. */
3319 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3320 elf_tdata (abfd)->core->program =
3321 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3322 elf_tdata (abfd)->core->command =
3323 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3324 break;
3326 default:
3327 return false;
3330 /* Note that for some reason, a spurious space is tacked
3331 onto the end of the args in some (at least one anyway)
3332 implementations, so strip it off if it exists. */
3334 char *command = elf_tdata (abfd)->core->command;
3335 int n = strlen (command);
3337 if (0 < n && command[n - 1] == ' ')
3338 command[n - 1] = '\0';
3341 return true;
3344 /* Hook called by the linker routine which adds symbols from an object
3345 file. We must handle the special NDS32 section numbers here.
3346 We also keep watching for whether we need to create the sdata special
3347 linker sections. */
3349 static bool
3350 nds32_elf_add_symbol_hook (bfd *abfd,
3351 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3352 Elf_Internal_Sym *sym,
3353 const char **namep ATTRIBUTE_UNUSED,
3354 flagword *flagsp ATTRIBUTE_UNUSED,
3355 asection **secp, bfd_vma *valp)
3357 switch (sym->st_shndx)
3359 case SHN_COMMON:
3360 /* Common symbols less than the GP size are automatically
3361 treated as SHN_MIPS_SCOMMON symbols. */
3362 if (sym->st_size > elf_gp_size (abfd)
3363 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3364 break;
3366 /* st_value is the alignment constraint.
3367 That might be its actual size if it is an array or structure. */
3368 switch (sym->st_value)
3370 case 1:
3371 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3372 break;
3373 case 2:
3374 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3375 break;
3376 case 4:
3377 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3378 break;
3379 case 8:
3380 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3381 break;
3382 default:
3383 return true;
3386 (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
3387 *valp = sym->st_size;
3388 break;
3391 return true;
3394 /* This function can figure out the best location for a base register to access
3395 data relative to this base register
3396 INPUT:
3397 sda_d0: size of first DOUBLE WORD data section
3398 sda_w0: size of first WORD data section
3399 sda_h0: size of first HALF WORD data section
3400 sda_b : size of BYTE data section
3401 sda_hi: size of second HALF WORD data section
3402 sda_w1: size of second WORD data section
3403 sda_d1: size of second DOUBLE WORD data section
3404 OUTPUT:
3405 offset (always positive) from the beginning of sda_d0 if OK
3406 a negative error value if fail
3407 NOTE:
3408 these 7 sections have to be located back to back if exist
3409 a pass in 0 value for non-existing section */
3411 /* Due to the interpretation of simm15 field of load/store depending on
3412 data accessing size, the organization of base register relative data shall
3413 like the following figure
3414 -------------------------------------------
3415 | DOUBLE WORD sized data (range +/- 128K)
3416 -------------------------------------------
3417 | WORD sized data (range +/- 64K)
3418 -------------------------------------------
3419 | HALF WORD sized data (range +/- 32K)
3420 -------------------------------------------
3421 | BYTE sized data (range +/- 16K)
3422 -------------------------------------------
3423 | HALF WORD sized data (range +/- 32K)
3424 -------------------------------------------
3425 | WORD sized data (range +/- 64K)
3426 -------------------------------------------
3427 | DOUBLE WORD sized data (range +/- 128K)
3428 -------------------------------------------
3429 Its base register shall be set to access these data freely. */
3431 /* We have to figure out the SDA_BASE value, so that we can adjust the
3432 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3433 BFD. If we can't find it, we're stuck. We cache it in the ELF
3434 target data. We don't need to adjust the symbol value for an
3435 external symbol if we are producing relocatable output. */
3437 static asection *sda_rela_sec = NULL;
3439 #define SDA_SECTION_NUM 10
3441 static bfd_reloc_status_type
3442 nds32_elf_final_sda_base (bfd *output_bfd,
3443 struct bfd_link_info *info,
3444 bfd_vma *psb,
3445 bool add_symbol)
3447 int relax_fp_as_gp;
3448 struct elf_nds32_link_hash_table *table;
3449 struct bfd_link_hash_entry *h, *h2;
3450 long unsigned int total = 0;
3451 asection *first = NULL, *final = NULL, *temp;
3452 bfd_vma sda_base = 0;
3454 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
3455 if (!h || (h->type != bfd_link_hash_defined
3456 && h->type != bfd_link_hash_defweak))
3458 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3459 4 byte-aligned. Therefore, it has to set the first section ".data"
3460 4 byte-aligned. */
3461 static const char sec_name[SDA_SECTION_NUM][10] =
3463 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3464 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3466 size_t i = 0;
3468 if (output_bfd->sections == NULL)
3470 *psb = elf_gp (output_bfd);
3471 return bfd_reloc_ok;
3474 /* Get the first and final section. */
3475 while (i < ARRAY_SIZE (sec_name))
3477 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3478 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3479 first = temp;
3480 if (temp && (temp->size != 0 || temp->rawsize != 0))
3481 final = temp;
3483 /* Summarize the sections in order to check if joining .bss. */
3484 if (temp && temp->size != 0)
3485 total += temp->size;
3486 else if (temp && temp->rawsize != 0)
3487 total += temp->rawsize;
3489 i++;
3492 /* Check .bss size. */
3493 temp = bfd_get_section_by_name (output_bfd, ".bss");
3494 if (temp)
3496 if (temp->size != 0)
3497 total += temp->size;
3498 else if (temp->rawsize != 0)
3499 total += temp->rawsize;
3501 if (total < 0x80000)
3503 if (!first && (temp->size != 0 || temp->rawsize != 0))
3504 first = temp;
3505 if ((temp->size != 0 || temp->rawsize != 0))
3506 final = temp;
3510 if (first && final)
3512 /* The middle of data region. */
3513 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3515 /* Find the section sda_base located. */
3516 i = 0;
3517 while (i < ARRAY_SIZE (sec_name))
3519 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3520 if (final && (final->size != 0 || final->rawsize != 0)
3521 && sda_base >= final->vma)
3523 first = final;
3524 i++;
3526 else
3527 break;
3530 else
3532 /* If there is not any default data section in output bfd, try to find
3533 the first data section. If no data section be found, just simplily
3534 choose the first output section. */
3535 temp = output_bfd->sections;
3536 while (temp)
3538 if (temp->flags & SEC_ALLOC
3539 && (((temp->flags & SEC_DATA)
3540 && ((temp->flags & SEC_READONLY) == 0))
3541 || (temp->flags & SEC_LOAD) == 0)
3542 && (temp->size != 0 || temp->rawsize != 0))
3544 if (!first)
3545 first = temp;
3546 final = temp;
3548 temp = temp->next;
3551 /* There is no data or bss section. */
3552 if (!first || (first->size == 0 && first->rawsize == 0))
3554 first = output_bfd->sections;
3555 while (first && first->size == 0 && first->rawsize == 0)
3556 first = first->next;
3559 /* There is no concrete section. */
3560 if (!first)
3562 *psb = elf_gp (output_bfd);
3563 return bfd_reloc_ok;
3566 if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3567 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3568 else
3569 sda_base = first->vma + 0x2000;
3572 sda_base -= first->vma;
3573 sda_base = sda_base & (~7);
3575 if (!_bfd_generic_link_add_one_symbol
3576 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3577 (bfd_vma) sda_base, (const char *) NULL, false,
3578 get_elf_backend_data (output_bfd)->collect, &h))
3579 return false;
3581 sda_rela_sec = first;
3584 /* Set _FP_BASE_ to _SDA_BASE_. */
3585 table = nds32_elf_hash_table (info);
3586 relax_fp_as_gp = table->relax_fp_as_gp;
3587 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, false, false, false);
3588 /* _SDA_BASE_ is difined in linker script. */
3589 if (!first)
3591 first = h->u.def.section;
3592 sda_base = h->u.def.value;
3595 if (relax_fp_as_gp && h2
3596 && (h2->type == bfd_link_hash_undefweak
3597 || h2->type == bfd_link_hash_undefined))
3599 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3600 And set FP equal to SDA_BASE to do relaxation for
3601 la $fp, _FP_BASE_. */
3602 if (!_bfd_generic_link_add_one_symbol
3603 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3604 first, sda_base, (const char *) NULL,
3605 false, get_elf_backend_data (output_bfd)->collect, &h2))
3606 return false;
3609 if (add_symbol)
3611 if (h)
3613 /* Now set gp. */
3614 elf_gp (output_bfd) = (h->u.def.value
3615 + h->u.def.section->output_section->vma
3616 + h->u.def.section->output_offset);
3618 else
3620 _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3621 return bfd_reloc_dangerous;
3625 *psb = h->u.def.value
3626 + h->u.def.section->output_section->vma
3627 + h->u.def.section->output_offset;
3628 return bfd_reloc_ok;
3632 /* Return size of a PLT entry. */
3633 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3635 /* Create an entry in an nds32 ELF linker hash table. */
3637 static struct bfd_hash_entry *
3638 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3639 struct bfd_hash_table *table,
3640 const char *string)
3642 struct elf_nds32_link_hash_entry *ret;
3644 ret = (struct elf_nds32_link_hash_entry *) entry;
3646 /* Allocate the structure if it has not already been allocated by a
3647 subclass. */
3648 if (ret == NULL)
3649 ret = (struct elf_nds32_link_hash_entry *)
3650 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3652 if (ret == NULL)
3653 return (struct bfd_hash_entry *) ret;
3655 /* Call the allocation method of the superclass. */
3656 ret = (struct elf_nds32_link_hash_entry *)
3657 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3659 if (ret != NULL)
3661 struct elf_nds32_link_hash_entry *eh;
3663 eh = (struct elf_nds32_link_hash_entry *) ret;
3664 eh->tls_type = GOT_UNKNOWN;
3665 eh->offset_to_gp = 0;
3668 return (struct bfd_hash_entry *) ret;
3671 /* Create an nds32 ELF linker hash table. */
3673 static struct bfd_link_hash_table *
3674 nds32_elf_link_hash_table_create (bfd *abfd)
3676 struct elf_nds32_link_hash_table *ret;
3678 size_t amt = sizeof (struct elf_nds32_link_hash_table);
3680 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3681 if (ret == NULL)
3682 return NULL;
3684 /* Patch tag. */
3685 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3686 nds32_elf_link_hash_newfunc,
3687 sizeof (struct elf_nds32_link_hash_entry),
3688 NDS32_ELF_DATA))
3690 free (ret);
3691 return NULL;
3694 ret->sym_ld_script = NULL;
3696 return &ret->root.root;
3699 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3700 shortcuts to them in our hash table. */
3702 static bool
3703 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3705 struct elf_link_hash_table *ehtab;
3707 if (!_bfd_elf_create_got_section (dynobj, info))
3708 return false;
3710 ehtab = elf_hash_table (info);
3711 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3712 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3713 if (!ehtab->sgot || !ehtab->sgotplt)
3714 abort ();
3716 /* _bfd_elf_create_got_section will create it for us. */
3717 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3718 if (ehtab->srelgot == NULL
3719 || !bfd_set_section_flags (ehtab->srelgot,
3720 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3721 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3722 | SEC_READONLY))
3723 || !bfd_set_section_alignment (ehtab->srelgot, 2))
3724 return false;
3726 return true;
3729 /* Create dynamic sections when linking against a dynamic object. */
3731 static bool
3732 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3734 struct elf_link_hash_table *ehtab;
3735 struct elf_nds32_link_hash_table *htab;
3736 flagword flags, pltflags;
3737 register asection *s;
3738 const struct elf_backend_data *bed;
3739 int ptralign = 2; /* 32-bit */
3740 const char *secname;
3741 char *relname;
3742 flagword secflags;
3743 asection *sec;
3745 bed = get_elf_backend_data (abfd);
3746 ehtab = elf_hash_table (info);
3747 htab = nds32_elf_hash_table (info);
3749 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3750 .rel[a].bss sections. */
3752 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3753 | SEC_LINKER_CREATED);
3755 pltflags = flags;
3756 pltflags |= SEC_CODE;
3757 if (bed->plt_not_loaded)
3758 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3759 if (bed->plt_readonly)
3760 pltflags |= SEC_READONLY;
3762 s = bfd_make_section (abfd, ".plt");
3763 ehtab->splt = s;
3764 if (s == NULL
3765 || !bfd_set_section_flags (s, pltflags)
3766 || !bfd_set_section_alignment (s, bed->plt_alignment))
3767 return false;
3769 if (bed->want_plt_sym)
3771 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3772 .plt section. */
3773 struct bfd_link_hash_entry *bh = NULL;
3774 struct elf_link_hash_entry *h;
3776 if (!(_bfd_generic_link_add_one_symbol
3777 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3778 (bfd_vma) 0, (const char *) NULL, false,
3779 get_elf_backend_data (abfd)->collect, &bh)))
3780 return false;
3782 h = (struct elf_link_hash_entry *) bh;
3783 h->def_regular = 1;
3784 h->type = STT_OBJECT;
3786 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3787 return false;
3790 s = bfd_make_section (abfd,
3791 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3792 ehtab->srelplt = s;
3793 if (s == NULL
3794 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3795 || !bfd_set_section_alignment (s, ptralign))
3796 return false;
3798 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3799 return false;
3801 for (sec = abfd->sections; sec; sec = sec->next)
3803 secflags = bfd_section_flags (sec);
3804 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3805 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3806 continue;
3807 secname = bfd_section_name (sec);
3808 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3809 strcpy (relname, ".rela");
3810 strcat (relname, secname);
3811 if (bfd_get_section_by_name (abfd, secname))
3812 continue;
3813 s = bfd_make_section (abfd, relname);
3814 if (s == NULL
3815 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3816 || !bfd_set_section_alignment (s, ptralign))
3817 return false;
3820 if (bed->want_dynbss)
3822 /* The .dynbss section is a place to put symbols which are defined
3823 by dynamic objects, are referenced by regular objects, and are
3824 not functions. We must allocate space for them in the process
3825 image and use a R_*_COPY reloc to tell the dynamic linker to
3826 initialize them at run time. The linker script puts the .dynbss
3827 section into the .bss section of the final image. */
3828 s = bfd_make_section (abfd, ".dynbss");
3829 htab->root.sdynbss = s;
3830 if (s == NULL
3831 || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3832 return false;
3833 /* The .rel[a].bss section holds copy relocs. This section is not
3834 normally needed. We need to create it here, though, so that the
3835 linker will map it to an output section. We can't just create it
3836 only if we need it, because we will not know whether we need it
3837 until we have seen all the input files, and the first time the
3838 main linker code calls BFD after examining all the input files
3839 (size_dynamic_sections) the input sections have already been
3840 mapped to the output sections. If the section turns out not to
3841 be needed, we can discard it later. We will never need this
3842 section when generating a shared object, since they do not use
3843 copy relocs. */
3844 if (!bfd_link_pic (info))
3846 s = bfd_make_section (abfd, (bed->default_use_rela_p
3847 ? ".rela.bss" : ".rel.bss"));
3848 htab->root.srelbss = s;
3849 if (s == NULL
3850 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3851 || !bfd_set_section_alignment (s, ptralign))
3852 return false;
3856 return true;
3859 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3860 static void
3861 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3862 struct elf_link_hash_entry *dir,
3863 struct elf_link_hash_entry *ind)
3865 struct elf_nds32_link_hash_entry *edir, *eind;
3867 edir = (struct elf_nds32_link_hash_entry *) dir;
3868 eind = (struct elf_nds32_link_hash_entry *) ind;
3870 if (ind->root.type == bfd_link_hash_indirect)
3872 if (dir->got.refcount <= 0)
3874 edir->tls_type = eind->tls_type;
3875 eind->tls_type = GOT_UNKNOWN;
3879 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3882 /* Adjust a symbol defined by a dynamic object and referenced by a
3883 regular object. The current definition is in some section of the
3884 dynamic object, but we're not including those sections. We have to
3885 change the definition to something the rest of the link can
3886 understand. */
3888 static bool
3889 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3890 struct elf_link_hash_entry *h)
3892 struct elf_nds32_link_hash_table *htab;
3893 bfd *dynobj;
3894 asection *s;
3895 unsigned int power_of_two;
3897 dynobj = elf_hash_table (info)->dynobj;
3899 /* Make sure we know what is going on here. */
3900 BFD_ASSERT (dynobj != NULL
3901 && (h->needs_plt
3902 || h->is_weakalias
3903 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3906 /* If this is a function, put it in the procedure linkage table. We
3907 will fill in the contents of the procedure linkage table later,
3908 when we know the address of the .got section. */
3909 if (h->type == STT_FUNC || h->needs_plt)
3911 if (!bfd_link_pic (info)
3912 && !h->def_dynamic
3913 && !h->ref_dynamic
3914 && h->root.type != bfd_link_hash_undefweak
3915 && h->root.type != bfd_link_hash_undefined)
3917 /* This case can occur if we saw a PLT reloc in an input
3918 file, but the symbol was never referred to by a dynamic
3919 object. In such a case, we don't actually need to build
3920 a procedure linkage table, and we can just do a PCREL
3921 reloc instead. */
3922 h->plt.offset = (bfd_vma) - 1;
3923 h->needs_plt = 0;
3926 return true;
3928 else
3929 h->plt.offset = (bfd_vma) - 1;
3931 /* If this is a weak symbol, and there is a real definition, the
3932 processor independent code will have arranged for us to see the
3933 real definition first, and we can just use the same value. */
3934 if (h->is_weakalias)
3936 struct elf_link_hash_entry *def = weakdef (h);
3937 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3938 h->root.u.def.section = def->root.u.def.section;
3939 h->root.u.def.value = def->root.u.def.value;
3940 return true;
3943 /* This is a reference to a symbol defined by a dynamic object which
3944 is not a function. */
3946 /* If we are creating a shared library, we must presume that the
3947 only references to the symbol are via the global offset table.
3948 For such cases we need not do anything here; the relocations will
3949 be handled correctly by relocate_section. */
3950 if (bfd_link_pic (info))
3951 return true;
3953 /* If there are no references to this symbol that do not use the
3954 GOT, we don't need to generate a copy reloc. */
3955 if (!h->non_got_ref)
3956 return true;
3958 /* If -z nocopyreloc was given, we won't generate them either. */
3959 if (0 && info->nocopyreloc)
3961 h->non_got_ref = 0;
3962 return true;
3965 /* If we don't find any dynamic relocs in read-only sections, then
3966 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3967 if (!_bfd_elf_readonly_dynrelocs (h))
3969 h->non_got_ref = 0;
3970 return true;
3973 /* We must allocate the symbol in our .dynbss section, which will
3974 become part of the .bss section of the executable. There will be
3975 an entry for this symbol in the .dynsym section. The dynamic
3976 object will contain position independent code, so all references
3977 from the dynamic object to this symbol will go through the global
3978 offset table. The dynamic linker will use the .dynsym entry to
3979 determine the address it must put in the global offset table, so
3980 both the dynamic object and the regular object will refer to the
3981 same memory location for the variable. */
3983 htab = nds32_elf_hash_table (info);
3984 s = htab->root.sdynbss;
3985 BFD_ASSERT (s != NULL);
3987 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3988 to copy the initial value out of the dynamic object and into the
3989 runtime process image. We need to remember the offset into the
3990 .rela.bss section we are going to use. */
3991 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3993 asection *srel;
3995 srel = htab->root.srelbss;
3996 BFD_ASSERT (srel != NULL);
3997 srel->size += sizeof (Elf32_External_Rela);
3998 h->needs_copy = 1;
4001 /* We need to figure out the alignment required for this symbol. I
4002 have no idea how ELF linkers handle this. */
4003 power_of_two = bfd_log2 (h->size);
4004 if (power_of_two > 3)
4005 power_of_two = 3;
4007 /* Apply the required alignment. */
4008 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4009 if (power_of_two > bfd_section_alignment (s))
4011 if (!bfd_set_section_alignment (s, power_of_two))
4012 return false;
4015 /* Define the symbol as being at this point in the section. */
4016 h->root.u.def.section = s;
4017 h->root.u.def.value = s->size;
4019 /* Increment the section size to make room for the symbol. */
4020 s->size += h->size;
4022 return true;
4025 /* Allocate space in .plt, .got and associated reloc sections for
4026 dynamic relocs. */
4028 static bool
4029 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4031 struct bfd_link_info *info;
4032 struct elf_link_hash_table *ehtab;
4033 struct elf_nds32_link_hash_table *htab;
4034 struct elf_dyn_relocs *p;
4036 if (h->root.type == bfd_link_hash_indirect)
4037 return true;
4039 /* When warning symbols are created, they **replace** the "real"
4040 entry in the hash table, thus we never get to see the real
4041 symbol in a hash traversal. So look at it now. */
4042 if (h->root.type == bfd_link_hash_warning)
4043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4045 info = (struct bfd_link_info *) inf;
4046 ehtab = elf_hash_table (info);
4047 htab = nds32_elf_hash_table (info);
4048 if (htab == NULL)
4049 return false;
4051 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4052 && h->plt.refcount > 0
4053 && !(bfd_link_pie (info) && h->def_regular))
4055 /* Make sure this symbol is output as a dynamic symbol.
4056 Undefined weak syms won't yet be marked as dynamic. */
4057 if (h->dynindx == -1 && !h->forced_local)
4059 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4060 return false;
4063 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4065 asection *s = ehtab->splt;
4067 /* If this is the first .plt entry, make room for the special
4068 first entry. */
4069 if (s->size == 0)
4070 s->size += PLT_ENTRY_SIZE;
4072 h->plt.offset = s->size;
4074 /* If this symbol is not defined in a regular file, and we are
4075 not generating a shared library, then set the symbol to this
4076 location in the .plt. This is required to make function
4077 pointers compare as equal between the normal executable and
4078 the shared library. */
4079 if (!bfd_link_pic (info) && !h->def_regular)
4081 h->root.u.def.section = s;
4082 h->root.u.def.value = h->plt.offset;
4085 /* Make room for this entry. */
4086 s->size += PLT_ENTRY_SIZE;
4088 /* We also need to make an entry in the .got.plt section, which
4089 will be placed in the .got section by the linker script. */
4090 ehtab->sgotplt->size += 4;
4092 /* We also need to make an entry in the .rel.plt section. */
4093 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4094 if (htab->tls_desc_trampoline)
4095 htab->next_tls_desc_index++;
4097 else
4099 h->plt.offset = (bfd_vma) - 1;
4100 h->needs_plt = 0;
4103 else
4105 h->plt.offset = (bfd_vma) - 1;
4106 h->needs_plt = 0;
4109 if (h->got.refcount > 0)
4111 asection *sgot;
4112 bool dyn;
4113 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4115 /* Make sure this symbol is output as a dynamic symbol.
4116 Undefined weak syms won't yet be marked as dynamic. */
4117 if (h->dynindx == -1 && !h->forced_local)
4119 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4120 return false;
4123 sgot = elf_hash_table (info)->sgot;
4124 h->got.offset = sgot->size;
4126 if (tls_type == GOT_UNKNOWN)
4127 abort ();
4129 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4130 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4131 sgot->size += 4;
4132 else
4134 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4135 if (tls_type & GOT_TLS_DESC)
4136 sgot->size += 8;
4139 dyn = htab->root.dynamic_sections_created;
4141 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4143 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4145 /* TLS_DESC with trampoline needs a relocation slot
4146 within .rela.plt. */
4147 htab->num_tls_desc++;
4148 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4149 htab->tls_trampoline = -1;
4151 else
4153 /* other relocations, including TLS_DESC without trampoline, need
4154 a relocation slot within .rela.got. */
4155 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4159 else
4160 h->got.offset = (bfd_vma)-1;
4162 if (h->dyn_relocs == NULL)
4163 return true;
4165 /* In the shared -Bsymbolic case, discard space allocated for
4166 dynamic pc-relative relocs against symbols which turn out to be
4167 defined in regular objects. For the normal shared case, discard
4168 space for pc-relative relocs that have become local due to symbol
4169 visibility changes. */
4171 if (bfd_link_pic (info))
4173 if (h->def_regular && (h->forced_local || info->symbolic))
4175 struct elf_dyn_relocs **pp;
4177 for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4179 p->count -= p->pc_count;
4180 p->pc_count = 0;
4181 if (p->count == 0)
4182 *pp = p->next;
4183 else
4184 pp = &p->next;
4188 else
4190 /* For the non-shared case, discard space for relocs against
4191 symbols which turn out to need copy relocs or are not dynamic. */
4193 if (!h->non_got_ref
4194 && ((h->def_dynamic
4195 && !h->def_regular)
4196 || (htab->root.dynamic_sections_created
4197 && (h->root.type == bfd_link_hash_undefweak
4198 || h->root.type == bfd_link_hash_undefined))))
4200 /* Make sure this symbol is output as a dynamic symbol.
4201 Undefined weak syms won't yet be marked as dynamic. */
4202 if (h->dynindx == -1 && !h->forced_local)
4204 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4205 return false;
4208 /* If that succeeded, we know we'll be keeping all the
4209 relocs. */
4210 if (h->dynindx != -1)
4211 goto keep;
4214 h->dyn_relocs = NULL;
4216 keep:;
4219 /* Finally, allocate space. */
4220 for (p = h->dyn_relocs; p != NULL; p = p->next)
4222 asection *sreloc = elf_section_data (p->sec)->sreloc;
4223 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4226 return true;
4229 /* Add relocation REL to the end of relocation section SRELOC. */
4231 static void
4232 elf32_nds32_add_dynreloc (bfd *output_bfd,
4233 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4234 asection *sreloc, Elf_Internal_Rela *rel)
4236 bfd_byte *loc;
4237 if (sreloc == NULL)
4238 abort ();
4240 loc = sreloc->contents;
4241 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4242 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4243 abort ();
4245 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4248 /* Set the sizes of the dynamic sections. */
4250 static bool
4251 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4252 struct bfd_link_info *info)
4254 struct elf_nds32_link_hash_table *htab;
4255 bfd *dynobj;
4256 asection *s;
4257 bool relocs;
4258 bfd *ibfd;
4260 htab = nds32_elf_hash_table (info);
4261 if (htab == NULL)
4262 return false;
4264 dynobj = elf_hash_table (info)->dynobj;
4265 BFD_ASSERT (dynobj != NULL);
4267 if (elf_hash_table (info)->dynamic_sections_created)
4269 /* Set the contents of the .interp section to the interpreter. */
4270 if (bfd_link_executable (info) && !info->nointerp)
4272 s = bfd_get_section_by_name (dynobj, ".interp");
4273 BFD_ASSERT (s != NULL);
4274 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4275 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4279 /* Set up .got offsets for local syms, and space for local dynamic
4280 relocs. */
4281 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4283 bfd_signed_vma *local_got;
4284 bfd_signed_vma *end_local_got;
4285 bfd_size_type locsymcount;
4286 Elf_Internal_Shdr *symtab_hdr;
4287 asection *sgot;
4288 char *local_tls_type;
4289 unsigned long symndx;
4290 bfd_vma *local_tlsdesc_gotent;
4292 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4293 continue;
4295 for (s = ibfd->sections; s != NULL; s = s->next)
4297 struct elf_dyn_relocs *p;
4299 for (p = ((struct elf_dyn_relocs *)
4300 elf_section_data (s)->local_dynrel);
4301 p != NULL; p = p->next)
4303 if (!bfd_is_abs_section (p->sec)
4304 && bfd_is_abs_section (p->sec->output_section))
4306 /* Input section has been discarded, either because
4307 it is a copy of a linkonce section or due to
4308 linker script /DISCARD/, so we'll be discarding
4309 the relocs too. */
4311 else if (p->count != 0)
4313 asection *sreloc = elf_section_data (p->sec)->sreloc;
4314 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4315 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4316 info->flags |= DF_TEXTREL;
4321 local_got = elf_local_got_refcounts (ibfd);
4322 if (!local_got)
4323 continue;
4325 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4326 locsymcount = symtab_hdr->sh_info;
4327 end_local_got = local_got + locsymcount;
4328 sgot = elf_hash_table (info)->sgot;
4329 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4330 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4331 for (symndx = 0; local_got < end_local_got;
4332 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4334 if (*local_got > 0)
4336 int num_of_got_entry_needed = 0;
4337 *local_got = sgot->size;
4338 *local_tlsdesc_gotent = sgot->size;
4340 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4341 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4342 num_of_got_entry_needed = 1;
4343 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4344 else if (*local_tls_type & GOT_TLS_DESC)
4345 num_of_got_entry_needed = 2;
4347 sgot->size += (num_of_got_entry_needed << 2);
4349 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4350 others need a slot in .rela.got. */
4351 if (*local_tls_type == GOT_TLS_DESC)
4353 if (bfd_link_pic (info))
4355 if (htab->tls_desc_trampoline)
4357 htab->num_tls_desc++;
4358 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4359 htab->tls_trampoline = -1;
4361 else
4362 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4364 else
4366 /* TLS_DESC -> TLS_LE */
4369 else
4371 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4374 else
4376 *local_got = (bfd_vma) -1;
4377 *local_tlsdesc_gotent = (bfd_vma) -1;
4382 /* Allocate global sym .plt and .got entries, and space for global
4383 sym dynamic relocs. */
4384 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4386 /* For every jump slot reserved in the sgotplt, reloc_count is
4387 incremented. However, when we reserve space for TLS descriptors,
4388 it's not incremented, so in order to compute the space reserved
4389 for them, it suffices to multiply the reloc count by the jump
4390 slot size. */
4391 if (htab->tls_desc_trampoline && htab->root.srelplt)
4392 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4394 if (htab->tls_trampoline)
4396 htab->tls_trampoline = htab->root.splt->size;
4398 /* If we're not using lazy TLS relocations, don't generate the
4399 PLT and GOT entries they require. */
4400 if ((info->flags & DF_BIND_NOW))
4401 htab->root.tlsdesc_plt = 0;
4402 else
4404 htab->root.tlsdesc_got = htab->root.sgot->size;
4405 htab->root.sgot->size += 4;
4407 htab->root.tlsdesc_plt = htab->root.splt->size;
4408 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4412 /* We now have determined the sizes of the various dynamic sections.
4413 Allocate memory for them. */
4414 /* The check_relocs and adjust_dynamic_symbol entry points have
4415 determined the sizes of the various dynamic sections. Allocate
4416 memory for them. */
4417 relocs = false;
4418 for (s = dynobj->sections; s != NULL; s = s->next)
4420 if ((s->flags & SEC_LINKER_CREATED) == 0)
4421 continue;
4423 if (s == htab->root.splt)
4425 /* Strip this section if we don't need it; see the
4426 comment below. */
4429 else if (s == elf_hash_table (info)->sgot)
4431 got_size += s->size;
4433 else if (s == elf_hash_table (info)->sgotplt)
4435 got_size += s->size;
4437 else if (startswith (bfd_section_name (s), ".rela"))
4439 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4440 relocs = true;
4442 /* We use the reloc_count field as a counter if we need
4443 to copy relocs into the output file. */
4444 s->reloc_count = 0;
4446 else
4448 /* It's not one of our sections, so don't allocate space. */
4449 continue;
4452 if (s->size == 0)
4454 /* If we don't need this section, strip it from the
4455 output file. This is mostly to handle .rela.bss and
4456 .rela.plt. We must create both sections in
4457 create_dynamic_sections, because they must be created
4458 before the linker maps input sections to output
4459 sections. The linker does that before
4460 adjust_dynamic_symbol is called, and it is that
4461 function which decides whether anything needs to go
4462 into these sections. */
4463 s->flags |= SEC_EXCLUDE;
4464 continue;
4467 /* Allocate memory for the section contents. We use bfd_zalloc
4468 here in case unused entries are not reclaimed before the
4469 section's contents are written out. This should not happen,
4470 but this way if it does, we get a R_NDS32_NONE reloc instead
4471 of garbage. */
4472 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4473 if (s->contents == NULL)
4474 return false;
4477 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
4480 static bfd_reloc_status_type
4481 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4482 bfd_vma relocation, bfd_byte *location)
4484 int size;
4485 bfd_vma x = 0;
4486 bfd_reloc_status_type flag;
4487 unsigned int rightshift = howto->rightshift;
4488 unsigned int bitpos = howto->bitpos;
4490 if (howto->negate)
4491 relocation = -relocation;
4493 /* Get the value we are going to relocate. */
4494 size = bfd_get_reloc_size (howto);
4495 switch (size)
4497 default:
4498 abort ();
4499 break;
4500 case 0:
4501 return bfd_reloc_ok;
4502 case 2:
4503 x = bfd_getb16 (location);
4504 break;
4505 case 4:
4506 x = bfd_getb32 (location);
4507 break;
4510 /* Check for overflow. FIXME: We may drop bits during the addition
4511 which we don't check for. We must either check at every single
4512 operation, which would be tedious, or we must do the computations
4513 in a type larger than bfd_vma, which would be inefficient. */
4514 flag = bfd_reloc_ok;
4515 if (howto->complain_on_overflow != complain_overflow_dont)
4517 bfd_vma addrmask, fieldmask, signmask, ss;
4518 bfd_vma a, b, sum;
4520 /* Get the values to be added together. For signed and unsigned
4521 relocations, we assume that all values should be truncated to
4522 the size of an address. For bitfields, all the bits matter.
4523 See also bfd_check_overflow. */
4524 fieldmask = N_ONES (howto->bitsize);
4525 signmask = ~fieldmask;
4526 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4527 a = (relocation & addrmask) >> rightshift;
4528 b = (x & howto->src_mask & addrmask) >> bitpos;
4530 switch (howto->complain_on_overflow)
4532 case complain_overflow_signed:
4533 /* If any sign bits are set, all sign bits must be set.
4534 That is, A must be a valid negative address after
4535 shifting. */
4536 signmask = ~(fieldmask >> 1);
4537 /* Fall through. */
4539 case complain_overflow_bitfield:
4540 /* Much like the signed check, but for a field one bit
4541 wider. We allow a bitfield to represent numbers in the
4542 range -2**n to 2**n-1, where n is the number of bits in the
4543 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4544 can't overflow, which is exactly what we want. */
4545 ss = a & signmask;
4546 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4547 flag = bfd_reloc_overflow;
4549 /* We only need this next bit of code if the sign bit of B
4550 is below the sign bit of A. This would only happen if
4551 SRC_MASK had fewer bits than BITSIZE. Note that if
4552 SRC_MASK has more bits than BITSIZE, we can get into
4553 trouble; we would need to verify that B is in range, as
4554 we do for A above. */
4555 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4556 ss >>= bitpos;
4558 /* Set all the bits above the sign bit. */
4559 b = (b ^ ss) - ss;
4561 /* Now we can do the addition. */
4562 sum = a + b;
4564 /* See if the result has the correct sign. Bits above the
4565 sign bit are junk now; ignore them. If the sum is
4566 positive, make sure we did not have all negative inputs;
4567 if the sum is negative, make sure we did not have all
4568 positive inputs. The test below looks only at the sign
4569 bits, and it really just
4570 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4572 We mask with addrmask here to explicitly allow an address
4573 wrap-around. The Linux kernel relies on it, and it is
4574 the only way to write assembler code which can run when
4575 loaded at a location 0x80000000 away from the location at
4576 which it is linked. */
4577 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4578 flag = bfd_reloc_overflow;
4580 break;
4582 case complain_overflow_unsigned:
4583 /* Checking for an unsigned overflow is relatively easy:
4584 trim the addresses and add, and trim the result as well.
4585 Overflow is normally indicated when the result does not
4586 fit in the field. However, we also need to consider the
4587 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4588 input is 0x80000000, and bfd_vma is only 32 bits; then we
4589 will get sum == 0, but there is an overflow, since the
4590 inputs did not fit in the field. Instead of doing a
4591 separate test, we can check for this by or-ing in the
4592 operands when testing for the sum overflowing its final
4593 field. */
4594 sum = (a + b) & addrmask;
4595 if ((a | b | sum) & signmask)
4596 flag = bfd_reloc_overflow;
4597 break;
4599 default:
4600 abort ();
4604 /* Put RELOCATION in the right bits. */
4605 relocation >>= (bfd_vma) rightshift;
4606 relocation <<= (bfd_vma) bitpos;
4608 /* Add RELOCATION to the right bits of X. */
4609 /* FIXME : 090616
4610 Because the relaxation may generate duplicate relocation at one address,
4611 an addition to immediate in the instruction may cause the relocation added
4612 several times.
4613 This bug should be fixed in assembler, but a check is also needed here. */
4614 if (howto->partial_inplace)
4615 x = ((x & ~howto->dst_mask)
4616 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4617 else
4618 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4621 /* Put the relocated value back in the object file. */
4622 switch (size)
4624 default:
4625 case 0:
4626 case 1:
4627 case 8:
4628 abort ();
4629 break;
4630 case 2:
4631 bfd_putb16 (x, location);
4632 break;
4633 case 4:
4634 bfd_putb32 (x, location);
4635 break;
4638 return flag;
4641 static bfd_reloc_status_type
4642 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4643 asection *input_section, bfd_byte *contents,
4644 bfd_vma address, bfd_vma value, bfd_vma addend)
4646 bfd_vma relocation;
4648 /* Sanity check the address. */
4649 if (address > bfd_get_section_limit (input_bfd, input_section))
4650 return bfd_reloc_outofrange;
4652 /* This function assumes that we are dealing with a basic relocation
4653 against a symbol. We want to compute the value of the symbol to
4654 relocate to. This is just VALUE, the value of the symbol, plus
4655 ADDEND, any addend associated with the reloc. */
4656 relocation = value + addend;
4658 /* If the relocation is PC relative, we want to set RELOCATION to
4659 the distance between the symbol (currently in RELOCATION) and the
4660 location we are relocating. If pcrel_offset is FALSE we do not
4661 need to subtract out the offset of the location within the
4662 section (which is just ADDRESS). */
4663 if (howto->pc_relative)
4665 relocation -= (input_section->output_section->vma
4666 + input_section->output_offset);
4667 if (howto->pcrel_offset)
4668 relocation -= address;
4671 return nds32_relocate_contents (howto, input_bfd, relocation,
4672 contents + address);
4675 static int
4676 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4677 const char *name,
4678 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4679 asection *input_sec,
4680 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4682 const char *source;
4683 FILE *sym_ld_script = NULL;
4684 struct elf_nds32_link_hash_table *table;
4686 table = nds32_elf_hash_table (info);
4687 sym_ld_script = table->sym_ld_script;
4688 if (!sym_ld_script)
4689 return true;
4691 if (!h || !name || *name == '\0')
4692 return true;
4694 if (input_sec->flags & SEC_EXCLUDE)
4695 return true;
4697 if (!check_start_export_sym)
4699 fprintf (sym_ld_script, "SECTIONS\n{\n");
4700 check_start_export_sym = 1;
4703 if (h->root.type == bfd_link_hash_defined
4704 || h->root.type == bfd_link_hash_defweak)
4706 if (!h->root.u.def.section->output_section)
4707 return true;
4709 if (bfd_is_const_section (input_sec))
4710 source = input_sec->name;
4711 else
4712 source = bfd_get_filename (input_sec->owner);
4714 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4715 h->root.root.string,
4716 (long) (h->root.u.def.value
4717 + h->root.u.def.section->output_section->vma
4718 + h->root.u.def.section->output_offset), source);
4721 return true;
4724 /* Relocate an NDS32/D ELF section.
4725 There is some attempt to make this function usable for many architectures,
4726 both for RELA and REL type relocs, if only to serve as a learning tool.
4728 The RELOCATE_SECTION function is called by the new ELF backend linker
4729 to handle the relocations for a section.
4731 The relocs are always passed as Rela structures; if the section
4732 actually uses Rel structures, the r_addend field will always be
4733 zero.
4735 This function is responsible for adjust the section contents as
4736 necessary, and (if using Rela relocs and generating a
4737 relocatable output file) adjusting the reloc addend as
4738 necessary.
4740 This function does not have to worry about setting the reloc
4741 address or the reloc symbol index.
4743 LOCAL_SYMS is a pointer to the swapped in local symbols.
4745 LOCAL_SECTIONS is an array giving the section in the input file
4746 corresponding to the st_shndx field of each local symbol.
4748 The global hash table entry for the global symbols can be found
4749 via elf_sym_hashes (input_bfd).
4751 When generating relocatable output, this function must handle
4752 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4753 going to be the section symbol corresponding to the output
4754 section, which means that the addend must be adjusted
4755 accordingly. */
4757 /* Return the base VMA address which should be subtracted from real addresses
4758 when resolving @dtpoff relocation.
4759 This is PT_TLS segment p_vaddr. */
4761 /* Return the relocation value for @tpoff relocation
4762 if STT_TLS virtual address is ADDRESS. */
4764 /* Return the relocation value for @gottpoff relocation
4765 if STT_TLS virtual address is ADDRESS. */
4767 static bfd_vma
4768 gottpoff (struct bfd_link_info *info, bfd_vma address)
4770 bfd_vma tp_base;
4771 bfd_vma tp_offset;
4773 /* If tls_sec is NULL, we should have signalled an error already. */
4774 if (elf_hash_table (info)->tls_sec == NULL)
4775 return 0;
4777 tp_base = elf_hash_table (info)->tls_sec->vma;
4778 tp_offset = address - tp_base;
4780 return tp_offset;
4783 static bool
4784 patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4786 /* TLS_GD/TLS_LD model #1
4787 46 00 00 00 sethi $r0,#0x0
4788 58 00 00 00 ori $r0,$r0,#0x0
4789 40 00 74 00 add $r0,$r0,$gp
4790 04 10 00 00 lwi $r1,[$r0+#0x0]
4791 4b e0 04 01 jral $lp,$r1 */
4793 /* TLS_GD/TLS_LD model #2
4794 46 00 00 00 sethi $r0,#0x0
4795 58 00 00 00 ori $r0,$r0,#0x0
4796 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4797 40 00 74 00 add $r0,$r0,$gp
4798 4b e0 04 01 jral $lp,$r1 */
4800 /* TLS_IE model (non-PIC)
4801 46 00 00 00 sethi $r0,#0x0
4802 04 00 00 00 lwi $r0,[$r0+#0x0]
4803 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4805 /* TLS_IE model (PIC)
4806 46 00 00 00 sethi $r0,#0x0
4807 58 00 00 00 ori $r0,$r0,#0x0
4808 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4809 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4811 /* TLS_GD_TO_IE model
4812 46 00 00 00 sethi $r0,#0x0
4813 58 00 00 00 ori $r0,$r0,#0x0
4814 40 00 74 00 add $r0,$rM,$gp
4815 04 00 00 01 lwi $r0,[$r0+#0x4]
4816 40 00 64 00 add $r0,$r0,$r25 */
4818 bool rz = false;
4820 typedef struct
4822 uint32_t opcode;
4823 uint32_t mask;
4824 } pat_t;
4826 uint32_t patch[3] =
4828 0x40007400, /* add $r0,$rM,$gp */
4829 0x04000001, /* lwi $r0,[$r0+#0x4] */
4830 0x40006400, /* add $r0,$r0,$r25 */
4833 pat_t mode0[3] =
4835 { 0x40000000, 0xfe0003ff },
4836 { 0x04000000, 0xfe000000 },
4837 { 0x4be00001, 0xffff83ff },
4840 pat_t mode1[3] =
4842 { 0x38007402, 0xfe007fff },
4843 { 0x40007400, 0xfe007fff },
4844 { 0x4be00001, 0xffff83ff },
4847 unsigned char *p = contents + rel->r_offset;
4849 uint32_t insn;
4850 uint32_t regidx = 0;
4851 insn = bfd_getb32 (p);
4852 if (INSN_SETHI == (0xfe0fffffu & insn))
4854 regidx = 0x1f & (insn >> 20);
4855 p += 4;
4858 insn = bfd_getb32 (p);
4859 if (INSN_ORI == (0xfe007fffu & insn))
4861 regidx = 0x1f & (insn >> 20);
4862 p += 4;
4865 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
4867 /* already patched? */
4868 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4869 (patch[1] == bfd_getb32 (p + 4)))
4870 rz = true;
4872 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4874 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4875 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4877 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4878 bfd_putb32 (patch[1], p + 4);
4879 bfd_putb32 (patch[2], p + 8);
4880 rz = true;
4883 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4885 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4886 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
4888 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4889 bfd_putb32 (patch[1], p + 4);
4890 bfd_putb32 (patch[2], p + 8);
4891 rz = true;
4895 if (!rz)
4897 printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
4898 (int) rel->r_offset);
4899 BFD_ASSERT(0); /* Unsupported pattern. */
4902 return rz;
4905 static enum elf_nds32_tls_type
4906 get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
4908 static unsigned int
4909 ones32 (register unsigned int x)
4911 /* 32-bit recursive reduction using SWAR...
4912 but first step is mapping 2-bit values
4913 into sum of 2 1-bit values in sneaky way. */
4914 x -= ((x >> 1) & 0x55555555);
4915 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
4916 x = (((x >> 4) + x) & 0x0f0f0f0f);
4917 x += (x >> 8);
4918 x += (x >> 16);
4919 return (x & 0x0000003f);
4922 #if !HAVE_FLS
4923 static unsigned int
4924 fls (register unsigned int x)
4926 return ffs (x & (-x));
4928 #endif /* !HAVE_FLS */
4930 #define nds32_elf_local_tlsdesc_gotent(bfd) \
4931 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
4933 static int
4934 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
4935 struct bfd_link_info * info,
4936 bfd * input_bfd,
4937 asection * input_section,
4938 bfd_byte * contents,
4939 Elf_Internal_Rela * relocs,
4940 Elf_Internal_Sym * local_syms,
4941 asection ** local_sections)
4943 Elf_Internal_Shdr *symtab_hdr;
4944 struct elf_link_hash_entry **sym_hashes;
4945 Elf_Internal_Rela *rel, *relend;
4946 bool ret = true; /* Assume success. */
4947 int align = 0;
4948 bfd_reloc_status_type r;
4949 const char *errmsg = NULL;
4950 bfd_vma gp;
4951 struct elf_link_hash_table *ehtab;
4952 struct elf_nds32_link_hash_table *htab;
4953 bfd *dynobj;
4954 bfd_vma *local_got_offsets;
4955 asection *sgot, *splt, *sreloc;
4956 bfd_vma high_address;
4957 struct elf_nds32_link_hash_table *table;
4958 int eliminate_gc_relocs;
4959 bfd_vma fpbase_addr;
4961 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4962 sym_hashes = elf_sym_hashes (input_bfd);
4963 ehtab = elf_hash_table (info);
4964 htab = nds32_elf_hash_table (info);
4965 high_address = bfd_get_section_limit (input_bfd, input_section);
4967 dynobj = htab->root.dynobj;
4968 local_got_offsets = elf_local_got_offsets (input_bfd);
4970 sgot = ehtab->sgot;
4971 splt = ehtab->splt;
4972 sreloc = NULL;
4974 rel = relocs;
4975 relend = relocs + input_section->reloc_count;
4977 table = nds32_elf_hash_table (info);
4978 eliminate_gc_relocs = table->eliminate_gc_relocs;
4980 /* By this time, we can adjust the value of _SDA_BASE_. */
4981 /* Explain _SDA_BASE_ */
4982 if ((!bfd_link_relocatable (info)))
4984 is_SDA_BASE_set = 1;
4985 r = nds32_elf_final_sda_base (output_bfd, info, &gp, true);
4986 if (r != bfd_reloc_ok)
4987 return false;
4990 /* Do TLS model conversion once at first. */
4991 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
4993 /* Use gp as fp to prevent truncated fit. Because in relaxation time
4994 the fp value is set as gp, and it has be reverted for instruction
4995 setting fp. */
4996 fpbase_addr = elf_gp (output_bfd);
4998 /* Deal with (dynamic) relocations. */
4999 for (rel = relocs; rel < relend; rel++)
5001 enum elf_nds32_reloc_type r_type;
5002 reloc_howto_type *howto = NULL;
5003 unsigned long r_symndx;
5004 struct elf_link_hash_entry *h = NULL;
5005 Elf_Internal_Sym *sym = NULL;
5006 asection *sec;
5007 bfd_vma relocation;
5008 bfd_vma relocation_sym = 0xdeadbeef;
5009 Elf_Internal_Rela *lorel;
5010 bfd_vma off;
5012 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5013 ensure it's zero (we use REL relocs, not RELA). Therefore this
5014 should be assigning zero to `addend', but for clarity we use
5015 `r_addend'. */
5017 bfd_vma addend = rel->r_addend;
5018 bfd_vma offset = rel->r_offset;
5020 r_type = ELF32_R_TYPE (rel->r_info);
5021 if (r_type >= R_NDS32_max)
5023 /* xgettext:c-format */
5024 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5025 input_bfd, r_type);
5026 bfd_set_error (bfd_error_bad_value);
5027 ret = false;
5028 continue;
5031 if (r_type == R_NDS32_GNU_VTENTRY
5032 || r_type == R_NDS32_GNU_VTINHERIT
5033 || r_type == R_NDS32_NONE
5034 || r_type == R_NDS32_RELA_GNU_VTENTRY
5035 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5036 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5037 || r_type == R_NDS32_DATA
5038 || r_type == R_NDS32_TRAN)
5039 continue;
5041 /* If we enter the fp-as-gp region. Resolve the address
5042 of best fp-base. */
5043 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5044 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5046 int dist;
5048 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5049 dist = rel->r_addend >> 16;
5050 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5051 local_syms, symtab_hdr);
5053 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5054 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5056 fpbase_addr = elf_gp (output_bfd);
5059 /* Skip the relocations used for relaxation. */
5060 /* We have to update LONGCALL and LONGJUMP
5061 relocations when generating the relocatable files. */
5062 if (!bfd_link_relocatable (info)
5063 && (r_type >= R_NDS32_RELAX_ENTRY
5064 || (r_type >= R_NDS32_LONGCALL4
5065 && r_type <= R_NDS32_LONGJUMP7)))
5066 continue;
5068 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5069 r_symndx = ELF32_R_SYM (rel->r_info);
5071 /* This is a final link. */
5072 sym = NULL;
5073 sec = NULL;
5074 h = NULL;
5076 if (r_symndx < symtab_hdr->sh_info)
5078 /* Local symbol. */
5079 sym = local_syms + r_symndx;
5080 sec = local_sections[r_symndx];
5082 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5083 addend = rel->r_addend;
5085 /* keep symbol location for static TLS_IE GOT entry */
5086 relocation_sym = relocation;
5087 if (bfd_link_relocatable (info))
5089 /* This is a relocatable link. We don't have to change
5090 anything, unless the reloc is against a section symbol,
5091 in which case we have to adjust according to where the
5092 section symbol winds up in the output section. */
5093 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5094 rel->r_addend += sec->output_offset + sym->st_value;
5096 continue;
5099 else
5101 /* External symbol. */
5102 if (bfd_link_relocatable (info))
5103 continue;
5104 bool warned, ignored, unresolved_reloc;
5105 int symndx = r_symndx - symtab_hdr->sh_info;
5107 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5108 r_symndx, symtab_hdr, sym_hashes, h, sec,
5109 relocation, unresolved_reloc, warned,
5110 ignored);
5112 /* keep symbol location for static TLS_IE GOT entry */
5113 relocation_sym = relocation;
5115 /* la $fp, _FP_BASE_ is per-function (region).
5116 Handle it specially. */
5117 switch ((int) r_type)
5119 case R_NDS32_HI20_RELA:
5120 case R_NDS32_LO12S0_RELA:
5121 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5122 FP_BASE_NAME) == 0)
5124 if (!bfd_link_pie (info))
5126 _bfd_error_handler
5127 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5128 input_bfd);
5130 relocation = fpbase_addr;
5132 break;
5133 case R_NDS32_SDA19S0_RELA:
5134 case R_NDS32_SDA15S0_RELA:
5135 case R_NDS32_20_RELA:
5136 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5137 FP_BASE_NAME) == 0)
5139 relocation = fpbase_addr;
5140 break;
5145 /* Sanity check the address. */
5146 if (offset > high_address)
5148 r = bfd_reloc_outofrange;
5149 goto check_reloc;
5152 if (r_type >= R_NDS32_RELAX_ENTRY)
5153 continue;
5155 switch ((int) r_type)
5157 case R_NDS32_GOTOFF:
5158 /* Relocation is relative to the start of the global offset
5159 table (for ld24 rx, #uimm24), e.g. access at label+addend
5161 ld24 rx. #label@GOTOFF + addend
5162 sub rx, r12. */
5163 case R_NDS32_GOTOFF_HI20:
5164 case R_NDS32_GOTOFF_LO12:
5165 case R_NDS32_GOTOFF_LO15:
5166 case R_NDS32_GOTOFF_LO19:
5167 BFD_ASSERT (sgot != NULL);
5169 relocation -= elf_gp (output_bfd);
5170 break;
5172 case R_NDS32_9_PLTREL:
5173 case R_NDS32_25_PLTREL:
5174 /* Relocation is to the entry for this symbol in the
5175 procedure linkage table. */
5177 /* The native assembler will generate a 25_PLTREL reloc
5178 for a local symbol if you assemble a call from one
5179 section to another when using -K pic. */
5180 if (h == NULL)
5181 break;
5183 if (h->forced_local)
5184 break;
5186 /* We didn't make a PLT entry for this symbol. This
5187 happens when statically linking PIC code, or when
5188 using -Bsymbolic. */
5189 if (h->plt.offset == (bfd_vma) - 1)
5190 break;
5192 relocation = (splt->output_section->vma
5193 + splt->output_offset + h->plt.offset);
5194 break;
5196 case R_NDS32_PLT_GOTREL_HI20:
5197 case R_NDS32_PLT_GOTREL_LO12:
5198 case R_NDS32_PLT_GOTREL_LO15:
5199 case R_NDS32_PLT_GOTREL_LO19:
5200 case R_NDS32_PLT_GOTREL_LO20:
5201 if (h == NULL
5202 || h->forced_local
5203 || h->plt.offset == (bfd_vma) -1
5204 || (bfd_link_pie (info) && h->def_regular))
5206 /* Maybe we should find better checking to optimize
5207 PIE PLT relocations. */
5208 /* We didn't make a PLT entry for this symbol. This
5209 happens when statically linking PIC code, or when
5210 using -Bsymbolic. */
5211 if (h)
5212 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
5213 relocation -= elf_gp (output_bfd);
5214 break;
5217 relocation = (splt->output_section->vma
5218 + splt->output_offset + h->plt.offset);
5220 relocation -= elf_gp (output_bfd);
5221 break;
5223 case R_NDS32_PLTREL_HI20:
5224 case R_NDS32_PLTREL_LO12:
5226 /* Relocation is to the entry for this symbol in the
5227 procedure linkage table. */
5229 /* The native assembler will generate a 25_PLTREL reloc
5230 for a local symbol if you assemble a call from one
5231 section to another when using -K pic. */
5232 if (h == NULL)
5233 break;
5235 if (h->forced_local)
5236 break;
5238 if (h->plt.offset == (bfd_vma) - 1)
5239 /* We didn't make a PLT entry for this symbol. This
5240 happens when statically linking PIC code, or when
5241 using -Bsymbolic. */
5242 break;
5244 if (splt == NULL)
5245 break;
5247 relocation = (splt->output_section->vma
5248 + splt->output_offset
5249 + h->plt.offset + 4)
5250 - (input_section->output_section->vma
5251 + input_section->output_offset
5252 + rel->r_offset);
5254 break;
5256 case R_NDS32_GOTPC20:
5257 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5258 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5259 relocation = elf_gp (output_bfd);
5260 break;
5262 case R_NDS32_GOTPC_HI20:
5263 case R_NDS32_GOTPC_LO12:
5264 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5265 bl .+4
5266 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5267 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5269 bl .+4
5270 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5271 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5272 relocation = elf_gp (output_bfd);
5273 relocation -= (input_section->output_section->vma
5274 + input_section->output_offset + rel->r_offset);
5275 break;
5277 case R_NDS32_GOT20:
5278 /* Fall through. */
5279 case R_NDS32_GOT_HI20:
5280 case R_NDS32_GOT_LO12:
5281 case R_NDS32_GOT_LO15:
5282 case R_NDS32_GOT_LO19:
5283 /* Relocation is to the entry for this symbol in the global
5284 offset table. */
5285 BFD_ASSERT (sgot != NULL);
5287 if (h != NULL)
5289 /* External symbol */
5290 bool dyn;
5292 off = h->got.offset;
5293 BFD_ASSERT (off != (bfd_vma) - 1);
5294 dyn = htab->root.dynamic_sections_created;
5295 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5296 bfd_link_pic (info),
5298 || (bfd_link_pic (info)
5299 && (info->symbolic
5300 || h->dynindx == -1
5301 || h->forced_local) && h->def_regular))
5303 /* This is actually a static link, or it is a
5304 -Bsymbolic link and the symbol is defined
5305 locally, or the symbol was forced to be local
5306 because of a version file. We must initialize
5307 this entry in the global offset table. Since the
5308 offset must always be a multiple of 4, we use the
5309 least significant bit to record whether we have
5310 initialized it already.
5312 When doing a dynamic link, we create a .rela.got
5313 relocation entry to initialize the value. This
5314 is done in the finish_dynamic_symbol routine. */
5315 if ((off & 1) != 0) /* clear LSB */
5316 off &= ~1;
5317 else
5319 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5320 h->got.offset |= 1;
5323 relocation = sgot->output_section->vma + sgot->output_offset + off
5324 - elf_gp (output_bfd);
5326 else
5328 /* Local symbol */
5329 bfd_byte *loc;
5331 BFD_ASSERT (local_got_offsets != NULL
5332 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5334 off = local_got_offsets[r_symndx];
5336 /* The offset must always be a multiple of 4. We use
5337 the least significant bit to record whether we have
5338 already processed this entry. */
5339 if ((off & 1) != 0) /* clear LSB */
5340 off &= ~1;
5341 else
5343 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5345 if (bfd_link_pic (info))
5347 asection *srelgot;
5348 Elf_Internal_Rela outrel;
5350 /* We need to generate a R_NDS32_RELATIVE reloc
5351 for the dynamic linker. */
5352 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5353 BFD_ASSERT (srelgot != NULL);
5355 outrel.r_offset = (elf_gp (output_bfd)
5356 + sgot->output_offset + off);
5357 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5358 outrel.r_addend = relocation;
5359 loc = srelgot->contents;
5360 loc +=
5361 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5362 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5363 ++srelgot->reloc_count;
5365 local_got_offsets[r_symndx] |= 1;
5367 relocation = sgot->output_section->vma + sgot->output_offset + off
5368 - elf_gp (output_bfd);
5371 break;
5373 case R_NDS32_16_RELA:
5374 case R_NDS32_20_RELA:
5375 case R_NDS32_5_RELA:
5376 case R_NDS32_32_RELA:
5377 case R_NDS32_9_PCREL_RELA:
5378 case R_NDS32_WORD_9_PCREL_RELA:
5379 case R_NDS32_10_UPCREL_RELA:
5380 case R_NDS32_15_PCREL_RELA:
5381 case R_NDS32_17_PCREL_RELA:
5382 case R_NDS32_25_PCREL_RELA:
5383 case R_NDS32_HI20_RELA:
5384 case R_NDS32_LO12S3_RELA:
5385 case R_NDS32_LO12S2_RELA:
5386 case R_NDS32_LO12S2_DP_RELA:
5387 case R_NDS32_LO12S2_SP_RELA:
5388 case R_NDS32_LO12S1_RELA:
5389 case R_NDS32_LO12S0_RELA:
5390 case R_NDS32_LO12S0_ORI_RELA:
5391 if (bfd_link_pic (info) && r_symndx != 0
5392 && (input_section->flags & SEC_ALLOC) != 0
5393 && (eliminate_gc_relocs == 0
5394 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5395 && ((r_type != R_NDS32_9_PCREL_RELA
5396 && r_type != R_NDS32_WORD_9_PCREL_RELA
5397 && r_type != R_NDS32_10_UPCREL_RELA
5398 && r_type != R_NDS32_15_PCREL_RELA
5399 && r_type != R_NDS32_17_PCREL_RELA
5400 && r_type != R_NDS32_25_PCREL_RELA
5401 && !(r_type == R_NDS32_32_RELA
5402 && strcmp (input_section->name, ".eh_frame") == 0))
5403 || (h != NULL && h->dynindx != -1
5404 && (!info->symbolic || !h->def_regular))))
5406 Elf_Internal_Rela outrel;
5407 bool skip, relocate;
5408 bfd_byte *loc;
5410 /* When generating a shared object, these relocations
5411 are copied into the output file to be resolved at run
5412 time. */
5414 if (sreloc == NULL)
5416 const char *name;
5418 name = bfd_elf_string_from_elf_section
5419 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5420 elf_section_data (input_section)->rela.hdr->sh_name);
5421 if (name == NULL)
5422 return false;
5424 BFD_ASSERT (startswith (name, ".rela")
5425 && strcmp (bfd_section_name (input_section),
5426 name + 5) == 0);
5428 sreloc = bfd_get_section_by_name (dynobj, name);
5429 BFD_ASSERT (sreloc != NULL);
5432 skip = false;
5433 relocate = false;
5435 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5436 info,
5437 input_section,
5438 rel->r_offset);
5439 if (outrel.r_offset == (bfd_vma) - 1)
5440 skip = true;
5441 else if (outrel.r_offset == (bfd_vma) - 2)
5442 skip = true, relocate = true;
5443 outrel.r_offset += (input_section->output_section->vma
5444 + input_section->output_offset);
5446 if (skip)
5447 memset (&outrel, 0, sizeof outrel);
5448 else if (r_type == R_NDS32_17_PCREL_RELA
5449 || r_type == R_NDS32_15_PCREL_RELA
5450 || r_type == R_NDS32_25_PCREL_RELA)
5452 BFD_ASSERT (h != NULL && h->dynindx != -1);
5453 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5454 outrel.r_addend = rel->r_addend;
5456 else
5458 /* h->dynindx may be -1 if this symbol was marked to
5459 become local. */
5460 if (h == NULL
5461 || ((info->symbolic || h->dynindx == -1)
5462 && h->def_regular)
5463 || (bfd_link_pie (info) && h->def_regular))
5465 relocate = true;
5466 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5467 outrel.r_addend = relocation + rel->r_addend;
5469 if (h)
5471 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5473 BFD_ASSERT (sgot != NULL);
5474 /* If we did not allocate got entry for the symbol,
5475 we can not fill the nonexistent got entry. */
5476 if (h->got.offset != (bfd_vma) -1
5477 && (h->got.offset & 1) == 0)
5479 bfd_put_32 (output_bfd, outrel.r_addend,
5480 sgot->contents + h->got.offset);
5484 else
5486 if (h->dynindx == -1)
5488 _bfd_error_handler
5489 (_("%pB: relocation %s against `%s' can not be used when "
5490 "making a shared object; recompile with -fPIC"),
5491 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5492 bfd_set_error (bfd_error_bad_value);
5493 return false;
5496 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5497 outrel.r_addend = rel->r_addend;
5501 loc = sreloc->contents;
5502 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5503 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5504 ++sreloc->reloc_count;
5506 /* If this reloc is against an external symbol, we do
5507 not want to fiddle with the addend. Otherwise, we
5508 need to include the symbol value so that it becomes
5509 an addend for the dynamic reloc. */
5510 if (!relocate)
5511 continue;
5513 break;
5515 case R_NDS32_25_ABS_RELA:
5516 if (bfd_link_pic (info))
5518 _bfd_error_handler
5519 (_("%pB: warning: %s unsupported in shared mode"),
5520 input_bfd, "R_NDS32_25_ABS_RELA");
5521 return false;
5523 break;
5525 case R_NDS32_9_PCREL:
5526 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5527 contents, offset,
5528 sec, relocation, addend);
5529 goto check_reloc;
5531 case R_NDS32_HI20:
5532 /* We allow an arbitrary number of HI20 relocs before the
5533 LO12 reloc. This permits gcc to emit the HI and LO relocs
5534 itself. */
5535 for (lorel = rel + 1;
5536 (lorel < relend
5537 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5538 continue;
5539 if (lorel < relend
5540 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5541 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5542 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5543 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5545 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5546 contents, relocation + addend);
5547 r = bfd_reloc_ok;
5549 else
5550 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5551 contents, offset, relocation,
5552 addend);
5553 goto check_reloc;
5555 case R_NDS32_GOT17S2_RELA:
5556 case R_NDS32_GOT15S2_RELA:
5557 BFD_ASSERT (sgot != NULL);
5559 if (h != NULL)
5561 bool dyn;
5563 off = h->got.offset;
5564 BFD_ASSERT (off != (bfd_vma) - 1);
5566 dyn = htab->root.dynamic_sections_created;
5567 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5568 (dyn, bfd_link_pic (info), h)
5569 || (bfd_link_pic (info)
5570 && (info->symbolic
5571 || h->dynindx == -1
5572 || h->forced_local)
5573 && h->def_regular))
5575 /* This is actually a static link, or it is a
5576 -Bsymbolic link and the symbol is defined
5577 locally, or the symbol was forced to be local
5578 because of a version file. We must initialize
5579 this entry in the global offset table. Since the
5580 offset must always be a multiple of 4, we use the
5581 least significant bit to record whether we have
5582 initialized it already.
5584 When doing a dynamic link, we create a .rela.got
5585 relocation entry to initialize the value. This
5586 is done in the finish_dynamic_symbol routine. */
5587 if ((off & 1) != 0)
5588 off &= ~1;
5589 else
5591 bfd_put_32 (output_bfd, relocation,
5592 sgot->contents + off);
5593 h->got.offset |= 1;
5597 else
5599 bfd_byte *loc;
5601 BFD_ASSERT (local_got_offsets != NULL
5602 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5604 off = local_got_offsets[r_symndx];
5606 /* The offset must always be a multiple of 4. We use
5607 the least significant bit to record whether we have
5608 already processed this entry. */
5609 if ((off & 1) != 0)
5610 off &= ~1;
5611 else
5613 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5615 if (bfd_link_pic (info))
5617 asection *srelgot;
5618 Elf_Internal_Rela outrel;
5620 /* We need to generate a R_NDS32_RELATIVE reloc
5621 for the dynamic linker. */
5622 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5623 BFD_ASSERT (srelgot != NULL);
5625 outrel.r_offset = (elf_gp (output_bfd)
5626 + sgot->output_offset + off);
5627 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5628 outrel.r_addend = relocation;
5629 loc = srelgot->contents;
5630 loc +=
5631 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5632 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5633 ++srelgot->reloc_count;
5635 local_got_offsets[r_symndx] |= 1;
5638 relocation = sgot->output_section->vma + sgot->output_offset + off
5639 - elf_gp (output_bfd);
5641 if (relocation & align)
5643 /* Incorrect alignment. */
5644 _bfd_error_handler
5645 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5646 ret = false;
5647 r = bfd_reloc_dangerous;
5648 goto check_reloc;
5650 break;
5652 case R_NDS32_SDA16S3_RELA:
5653 case R_NDS32_SDA15S3_RELA:
5654 case R_NDS32_SDA15S3:
5655 align = 0x7;
5656 goto handle_sda;
5658 case R_NDS32_SDA17S2_RELA:
5659 case R_NDS32_SDA15S2_RELA:
5660 case R_NDS32_SDA12S2_SP_RELA:
5661 case R_NDS32_SDA12S2_DP_RELA:
5662 case R_NDS32_SDA15S2:
5663 case R_NDS32_SDA_FP7U2_RELA:
5664 align = 0x3;
5665 goto handle_sda;
5667 case R_NDS32_SDA18S1_RELA:
5668 case R_NDS32_SDA15S1_RELA:
5669 case R_NDS32_SDA15S1:
5670 align = 0x1;
5671 goto handle_sda;
5673 case R_NDS32_SDA19S0_RELA:
5674 case R_NDS32_SDA15S0_RELA:
5675 case R_NDS32_SDA15S0:
5676 align = 0x0;
5677 handle_sda:
5678 BFD_ASSERT (sec != NULL);
5680 /* If the symbol is in the abs section, the out_bfd will be null.
5681 This happens when the relocation has a symbol@GOTOFF. */
5682 r = nds32_elf_final_sda_base (output_bfd, info, &gp, false);
5683 if (r != bfd_reloc_ok)
5685 _bfd_error_handler
5686 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5687 ret = false;
5688 goto check_reloc;
5691 /* At this point `relocation' contains the object's
5692 address. */
5693 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5695 relocation -= fpbase_addr;
5697 else
5698 relocation -= gp;
5699 /* Now it contains the offset from _SDA_BASE_. */
5701 /* Make sure alignment is correct. */
5703 if (relocation & align)
5705 /* Incorrect alignment. */
5706 _bfd_error_handler
5707 /* xgettext:c-format */
5708 (_("%pB(%pA): warning: unaligned small data access"
5709 " of type %d"),
5710 input_bfd, input_section, r_type);
5711 ret = false;
5712 goto check_reloc;
5714 break;
5716 case R_NDS32_17IFC_PCREL_RELA:
5717 case R_NDS32_10IFCU_PCREL_RELA:
5718 /* Do nothing. */
5719 break;
5721 case R_NDS32_TLS_LE_HI20:
5722 case R_NDS32_TLS_LE_LO12:
5723 case R_NDS32_TLS_LE_20:
5724 case R_NDS32_TLS_LE_15S0:
5725 case R_NDS32_TLS_LE_15S1:
5726 case R_NDS32_TLS_LE_15S2:
5727 /* We do not have garbage collection for got entries.
5728 Therefore, IE to LE may have one empty entry, and DESC to
5729 LE may have two. */
5730 if (elf_hash_table (info)->tls_sec != NULL)
5731 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5732 break;
5734 case R_NDS32_TLS_IE_HI20:
5735 case R_NDS32_TLS_IE_LO12S2:
5736 case R_NDS32_TLS_DESC_HI20:
5737 case R_NDS32_TLS_DESC_LO12:
5738 case R_NDS32_TLS_IE_LO12:
5739 case R_NDS32_TLS_IEGP_HI20:
5740 case R_NDS32_TLS_IEGP_LO12:
5741 case R_NDS32_TLS_IEGP_LO12S2:
5743 /* Relocation is to the entry for this symbol in the global
5744 offset table. */
5745 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5746 asection *srelgot;
5747 Elf_Internal_Rela outrel;
5748 bfd_byte *loc;
5749 int indx = 0;
5751 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5753 BFD_ASSERT (sgot != NULL);
5754 if (h != NULL)
5756 bool dyn;
5758 off = h->got.offset;
5759 BFD_ASSERT (off != (bfd_vma) -1);
5760 dyn = htab->root.dynamic_sections_created;
5761 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5762 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5763 && (!bfd_link_pic (info)
5764 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5765 indx = h->dynindx;
5767 else
5769 BFD_ASSERT (local_got_offsets != NULL
5770 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5771 off = local_got_offsets[r_symndx];
5772 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5775 relocation = sgot->output_section->vma + sgot->output_offset + off;
5777 if (1 < ones32 (tls_type))
5779 eff_tls_type = 1 << (fls (tls_type) - 1);
5780 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5782 /* TLS model X -> LE is not implement yet!
5783 workaround here! */
5784 if (eff_tls_type == GOT_TLS_LE)
5786 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5790 /* The offset must always be a multiple of 4. We use
5791 the least significant bit to record whether we have
5792 already processed this entry. */
5793 bool need_relocs = false;
5794 srelgot = ehtab->srelgot;
5795 if ((bfd_link_pic (info) || indx != 0)
5796 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5797 || h->root.type != bfd_link_hash_undefweak))
5799 need_relocs = true;
5800 BFD_ASSERT (srelgot != NULL);
5803 if (off & 1)
5805 off &= ~1;
5806 relocation &= ~1;
5808 if (eff_tls_type & GOT_TLS_DESC)
5810 relocation -= elf_gp (output_bfd);
5811 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5813 /* TLS model shall be converted. */
5814 BFD_ASSERT(0);
5817 else if (eff_tls_type & GOT_TLS_IEGP)
5819 relocation -= elf_gp (output_bfd);
5822 else
5824 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5826 /* TLS model workaround shall be applied. */
5827 BFD_ASSERT(0);
5829 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5831 if (eff_tls_type & GOT_TLS_IEGP)
5832 relocation -= elf_gp(output_bfd);
5834 if (need_relocs)
5836 if (indx == 0)
5837 outrel.r_addend = gottpoff (info, relocation_sym);
5838 else
5839 outrel.r_addend = 0;
5840 outrel.r_offset = (sgot->output_section->vma
5841 + sgot->output_offset + off);
5842 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5844 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5845 &outrel);
5847 else
5849 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5850 sgot->contents + off);
5853 else if (eff_tls_type & GOT_TLS_DESC)
5855 relocation -= elf_gp (output_bfd);
5856 if (need_relocs)
5858 if (indx == 0)
5859 outrel.r_addend = gottpoff (info, relocation_sym);
5860 else
5861 outrel.r_addend = 0;
5862 outrel.r_offset = (sgot->output_section->vma
5863 + sgot->output_offset + off);
5864 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5866 if (htab->tls_desc_trampoline)
5868 asection *srelplt;
5869 srelplt = ehtab->srelplt;
5870 loc = srelplt->contents;
5871 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5872 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5873 <= srelplt->contents + srelplt->size);
5875 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5877 else
5879 loc = srelgot->contents;
5880 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5881 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5882 ++srelgot->reloc_count;
5885 else
5887 /* feed me! */
5888 bfd_put_32 (output_bfd, 0xdeadbeef,
5889 sgot->contents + off);
5890 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5891 sgot->contents + off + 4);
5892 patch_tls_desc_to_ie (contents, rel, input_bfd);
5893 BFD_ASSERT(0);
5896 else
5898 /* TLS model workaround shall be applied. */
5899 BFD_ASSERT(0);
5902 if (h != NULL)
5903 h->got.offset |= 1;
5904 else
5905 local_got_offsets[r_symndx] |= 1;
5908 break;
5909 /* DON'T fall through. */
5911 default:
5912 /* OLD_NDS32_RELOC. */
5914 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5915 contents, offset, relocation, addend);
5916 goto check_reloc;
5919 switch ((int) r_type)
5921 case R_NDS32_20_RELA:
5922 case R_NDS32_5_RELA:
5923 case R_NDS32_9_PCREL_RELA:
5924 case R_NDS32_WORD_9_PCREL_RELA:
5925 case R_NDS32_10_UPCREL_RELA:
5926 case R_NDS32_15_PCREL_RELA:
5927 case R_NDS32_17_PCREL_RELA:
5928 case R_NDS32_25_PCREL_RELA:
5929 case R_NDS32_25_ABS_RELA:
5930 case R_NDS32_HI20_RELA:
5931 case R_NDS32_LO12S3_RELA:
5932 case R_NDS32_LO12S2_RELA:
5933 case R_NDS32_LO12S2_DP_RELA:
5934 case R_NDS32_LO12S2_SP_RELA:
5935 case R_NDS32_LO12S1_RELA:
5936 case R_NDS32_LO12S0_RELA:
5937 case R_NDS32_LO12S0_ORI_RELA:
5938 case R_NDS32_SDA16S3_RELA:
5939 case R_NDS32_SDA17S2_RELA:
5940 case R_NDS32_SDA18S1_RELA:
5941 case R_NDS32_SDA19S0_RELA:
5942 case R_NDS32_SDA15S3_RELA:
5943 case R_NDS32_SDA15S2_RELA:
5944 case R_NDS32_SDA12S2_DP_RELA:
5945 case R_NDS32_SDA12S2_SP_RELA:
5946 case R_NDS32_SDA15S1_RELA:
5947 case R_NDS32_SDA15S0_RELA:
5948 case R_NDS32_SDA_FP7U2_RELA:
5949 case R_NDS32_9_PLTREL:
5950 case R_NDS32_25_PLTREL:
5951 case R_NDS32_GOT20:
5952 case R_NDS32_GOT_HI20:
5953 case R_NDS32_GOT_LO12:
5954 case R_NDS32_GOT_LO15:
5955 case R_NDS32_GOT_LO19:
5956 case R_NDS32_GOT15S2_RELA:
5957 case R_NDS32_GOT17S2_RELA:
5958 case R_NDS32_GOTPC20:
5959 case R_NDS32_GOTPC_HI20:
5960 case R_NDS32_GOTPC_LO12:
5961 case R_NDS32_GOTOFF:
5962 case R_NDS32_GOTOFF_HI20:
5963 case R_NDS32_GOTOFF_LO12:
5964 case R_NDS32_GOTOFF_LO15:
5965 case R_NDS32_GOTOFF_LO19:
5966 case R_NDS32_PLTREL_HI20:
5967 case R_NDS32_PLTREL_LO12:
5968 case R_NDS32_PLT_GOTREL_HI20:
5969 case R_NDS32_PLT_GOTREL_LO12:
5970 case R_NDS32_PLT_GOTREL_LO15:
5971 case R_NDS32_PLT_GOTREL_LO19:
5972 case R_NDS32_PLT_GOTREL_LO20:
5973 case R_NDS32_17IFC_PCREL_RELA:
5974 case R_NDS32_10IFCU_PCREL_RELA:
5975 case R_NDS32_TLS_LE_HI20:
5976 case R_NDS32_TLS_LE_LO12:
5977 case R_NDS32_TLS_IE_HI20:
5978 case R_NDS32_TLS_IE_LO12S2:
5979 case R_NDS32_TLS_LE_20:
5980 case R_NDS32_TLS_LE_15S0:
5981 case R_NDS32_TLS_LE_15S1:
5982 case R_NDS32_TLS_LE_15S2:
5983 case R_NDS32_TLS_DESC_HI20:
5984 case R_NDS32_TLS_DESC_LO12:
5985 case R_NDS32_TLS_IE_LO12:
5986 case R_NDS32_TLS_IEGP_HI20:
5987 case R_NDS32_TLS_IEGP_LO12:
5988 case R_NDS32_TLS_IEGP_LO12S2:
5989 /* Instruction related relocs must handle endian properly. */
5990 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
5991 r = nds32_elf_final_link_relocate (howto, input_bfd,
5992 input_section, contents,
5993 rel->r_offset, relocation,
5994 rel->r_addend);
5995 break;
5997 default:
5998 /* All other relocs can use default handler. */
5999 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6000 contents, rel->r_offset,
6001 relocation, rel->r_addend);
6002 break;
6005 check_reloc:
6007 if (r != bfd_reloc_ok)
6009 /* FIXME: This should be generic enough to go in a utility. */
6010 const char *name;
6012 if (h != NULL)
6013 name = h->root.root.string;
6014 else
6016 name = bfd_elf_string_from_elf_section
6017 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6018 if (name == NULL || *name == '\0')
6019 name = bfd_section_name (sec);
6022 if (errmsg != NULL)
6023 goto common_error;
6025 switch (r)
6027 case bfd_reloc_overflow:
6028 (*info->callbacks->reloc_overflow)
6029 (info, (h ? &h->root : NULL), name, howto->name,
6030 (bfd_vma) 0, input_bfd, input_section, offset);
6031 break;
6033 case bfd_reloc_undefined:
6034 (*info->callbacks->undefined_symbol)
6035 (info, name, input_bfd, input_section, offset, true);
6036 break;
6038 case bfd_reloc_outofrange:
6039 errmsg = _("internal error: out of range error");
6040 goto common_error;
6042 case bfd_reloc_notsupported:
6043 errmsg = _("internal error: unsupported relocation error");
6044 goto common_error;
6046 case bfd_reloc_dangerous:
6047 errmsg = _("internal error: dangerous error");
6048 goto common_error;
6050 default:
6051 errmsg = _("internal error: unknown error");
6052 /* Fall through. */
6054 common_error:
6055 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6056 input_section, offset);
6057 break;
6062 /* Resotre header size to avoid overflow load. */
6063 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6064 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6066 return ret;
6069 /* Finish up dynamic symbol handling. We set the contents of various
6070 dynamic sections here. */
6072 static bool
6073 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6074 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6076 struct elf_link_hash_table *ehtab;
6077 struct elf_nds32_link_hash_entry *hent;
6078 bfd_byte *loc;
6080 ehtab = elf_hash_table (info);
6081 hent = (struct elf_nds32_link_hash_entry *) h;
6083 if (h->plt.offset != (bfd_vma) - 1)
6085 asection *splt;
6086 asection *sgot;
6087 asection *srela;
6089 bfd_vma plt_index;
6090 bfd_vma got_offset;
6091 bfd_vma local_plt_offset;
6092 Elf_Internal_Rela rela;
6094 /* This symbol has an entry in the procedure linkage table. Set
6095 it up. */
6097 BFD_ASSERT (h->dynindx != -1);
6099 splt = ehtab->splt;
6100 sgot = ehtab->sgotplt;
6101 srela = ehtab->srelplt;
6102 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6104 /* Get the index in the procedure linkage table which
6105 corresponds to this symbol. This is the index of this symbol
6106 in all the symbols for which we are making plt entries. The
6107 first entry in the procedure linkage table is reserved. */
6108 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6110 /* Get the offset into the .got table of the entry that
6111 corresponds to this function. Each .got entry is 4 bytes.
6112 The first three are reserved. */
6113 got_offset = (plt_index + 3) * 4;
6115 /* Fill in the entry in the procedure linkage table. */
6116 if (!bfd_link_pic (info))
6118 unsigned long insn;
6120 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6121 + sgot->output_offset + got_offset) >> 12)
6122 & 0xfffff);
6123 bfd_putb32 (insn, splt->contents + h->plt.offset);
6125 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6126 + sgot->output_offset + got_offset) & 0x0fff)
6127 >> 2);
6128 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6130 insn = PLT_ENTRY_WORD2;
6131 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6133 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6134 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6136 insn = PLT_ENTRY_WORD4
6137 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6138 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6139 local_plt_offset = 12;
6141 else
6143 /* sda_base must be set at this time. */
6144 unsigned long insn;
6145 long offset;
6147 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6148 - elf_gp (output_bfd);
6149 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6150 bfd_putb32 (insn, splt->contents + h->plt.offset);
6152 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6153 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6155 insn = PLT_PIC_ENTRY_WORD2;
6156 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6158 insn = PLT_PIC_ENTRY_WORD3;
6159 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6161 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6162 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6164 insn = PLT_PIC_ENTRY_WORD5
6165 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6166 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6168 local_plt_offset = 16;
6171 /* Fill in the entry in the global offset table,
6172 so it will fall through to the next instruction for the first time. */
6173 bfd_put_32 (output_bfd,
6174 (splt->output_section->vma + splt->output_offset
6175 + h->plt.offset + local_plt_offset),
6176 sgot->contents + got_offset);
6178 /* Fill in the entry in the .rela.plt section. */
6179 rela.r_offset = (sgot->output_section->vma
6180 + sgot->output_offset + got_offset);
6181 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6182 rela.r_addend = 0;
6183 loc = srela->contents;
6184 loc += plt_index * sizeof (Elf32_External_Rela);
6185 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6187 if (!h->def_regular)
6189 /* Mark the symbol as undefined, rather than as defined in
6190 the .plt section. Leave the value alone. */
6191 sym->st_shndx = SHN_UNDEF;
6192 if (!h->ref_regular_nonweak)
6193 sym->st_value = 0;
6197 if (h->got.offset != (bfd_vma) - 1
6198 && hent->tls_type == GOT_NORMAL)
6200 asection *sgot;
6201 asection *srelagot;
6202 Elf_Internal_Rela rela;
6204 /* This symbol has an entry in the global offset table.
6205 Set it up. */
6207 sgot = ehtab->sgot;
6208 srelagot = ehtab->srelgot;
6209 BFD_ASSERT (sgot != NULL && srelagot != NULL);
6211 rela.r_offset = (sgot->output_section->vma
6212 + sgot->output_offset + (h->got.offset & ~1));
6214 /* If this is a -Bsymbolic link, and the symbol is defined
6215 locally, we just want to emit a RELATIVE reloc. Likewise if
6216 the symbol was forced to be local because of a version file.
6217 The entry in the global offset table will already have been
6218 initialized in the relocate_section function. */
6219 if ((bfd_link_pic (info)
6220 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6221 && h->def_regular)
6222 || (bfd_link_pie (info) && h->def_regular))
6224 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6225 rela.r_addend = (h->root.u.def.value
6226 + h->root.u.def.section->output_section->vma
6227 + h->root.u.def.section->output_offset);
6229 if ((h->got.offset & 1) == 0)
6231 bfd_put_32 (output_bfd, rela.r_addend,
6232 sgot->contents + h->got.offset);
6235 else
6237 BFD_ASSERT ((h->got.offset & 1) == 0);
6238 bfd_put_32 (output_bfd, (bfd_vma) 0,
6239 sgot->contents + h->got.offset);
6240 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6241 rela.r_addend = 0;
6244 loc = srelagot->contents;
6245 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6246 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6247 ++srelagot->reloc_count;
6248 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6251 if (h->needs_copy)
6253 asection *s;
6254 Elf_Internal_Rela rela;
6256 /* This symbols needs a copy reloc. Set it up. */
6258 BFD_ASSERT (h->dynindx != -1
6259 && (h->root.type == bfd_link_hash_defined
6260 || h->root.type == bfd_link_hash_defweak));
6262 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6263 BFD_ASSERT (s != NULL);
6265 rela.r_offset = (h->root.u.def.value
6266 + h->root.u.def.section->output_section->vma
6267 + h->root.u.def.section->output_offset);
6268 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6269 rela.r_addend = 0;
6270 loc = s->contents;
6271 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6272 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6273 ++s->reloc_count;
6276 /* Mark some specially defined symbols as absolute. */
6277 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6278 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6279 sym->st_shndx = SHN_ABS;
6281 return true;
6285 /* Finish up the dynamic sections. */
6287 static bool
6288 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6290 bfd *dynobj;
6291 asection *sdyn;
6292 asection *sgotplt;
6293 struct elf_link_hash_table *ehtab;
6294 struct elf_nds32_link_hash_table *htab;
6296 ehtab = elf_hash_table (info);
6297 htab = nds32_elf_hash_table (info);
6298 if (htab == NULL)
6299 return false;
6301 dynobj = elf_hash_table (info)->dynobj;
6303 sgotplt = ehtab->sgotplt;
6304 /* A broken linker script might have discarded the dynamic sections.
6305 Catch this here so that we do not seg-fault later on. */
6306 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6307 return false;
6308 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6310 if (elf_hash_table (info)->dynamic_sections_created)
6312 asection *splt;
6313 Elf32_External_Dyn *dyncon, *dynconend;
6315 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6317 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6318 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6320 for (; dyncon < dynconend; dyncon++)
6322 Elf_Internal_Dyn dyn;
6323 asection *s;
6325 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6327 switch (dyn.d_tag)
6329 default:
6330 break;
6332 case DT_PLTGOT:
6333 /* name = ".got"; */
6334 s = ehtab->sgot->output_section;
6335 goto get_vma;
6336 case DT_JMPREL:
6337 s = ehtab->srelplt->output_section;
6338 get_vma:
6339 BFD_ASSERT (s != NULL);
6340 dyn.d_un.d_ptr = s->vma;
6341 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6342 break;
6344 case DT_PLTRELSZ:
6345 s = ehtab->srelplt->output_section;
6346 BFD_ASSERT (s != NULL);
6347 dyn.d_un.d_val = s->size;
6348 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6349 break;
6351 case DT_RELASZ:
6352 /* My reading of the SVR4 ABI indicates that the
6353 procedure linkage table relocs (DT_JMPREL) should be
6354 included in the overall relocs (DT_RELA). This is
6355 what Solaris does. However, UnixWare can not handle
6356 that case. Therefore, we override the DT_RELASZ entry
6357 here to make it not include the JMPREL relocs. Since
6358 the linker script arranges for .rela.plt to follow all
6359 other relocation sections, we don't have to worry
6360 about changing the DT_RELA entry. */
6361 if (ehtab->srelplt != NULL)
6363 s = ehtab->srelplt->output_section;
6364 dyn.d_un.d_val -= s->size;
6366 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6367 break;
6369 case DT_TLSDESC_PLT:
6370 s = htab->root.splt;
6371 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6372 + htab->root.tlsdesc_plt);
6373 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6374 break;
6376 case DT_TLSDESC_GOT:
6377 s = htab->root.sgot;
6378 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6379 + htab->root.tlsdesc_got);
6380 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6381 break;
6385 /* Fill in the first entry in the procedure linkage table. */
6386 splt = ehtab->splt;
6387 if (splt && splt->size > 0)
6389 if (bfd_link_pic (info))
6391 unsigned long insn;
6392 long offset;
6394 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6395 - elf_gp (output_bfd);
6396 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6397 bfd_putb32 (insn, splt->contents);
6399 /* here has a typo? */
6400 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6401 bfd_putb32 (insn, splt->contents + 4);
6403 insn = PLT0_PIC_ENTRY_WORD2;
6404 bfd_putb32 (insn, splt->contents + 8);
6406 insn = PLT0_PIC_ENTRY_WORD3;
6407 bfd_putb32 (insn, splt->contents + 12);
6409 insn = PLT0_PIC_ENTRY_WORD4;
6410 bfd_putb32 (insn, splt->contents + 16);
6412 insn = PLT0_PIC_ENTRY_WORD5;
6413 bfd_putb32 (insn, splt->contents + 20);
6415 else
6417 unsigned long insn;
6418 unsigned long addr;
6420 /* addr = .got + 4 */
6421 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6422 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6423 bfd_putb32 (insn, splt->contents);
6425 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6426 bfd_putb32 (insn, splt->contents + 4);
6428 insn = PLT0_ENTRY_WORD2;
6429 bfd_putb32 (insn, splt->contents + 8);
6431 insn = PLT0_ENTRY_WORD3;
6432 bfd_putb32 (insn, splt->contents + 12);
6434 insn = PLT0_ENTRY_WORD4;
6435 bfd_putb32 (insn, splt->contents + 16);
6438 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6439 PLT_ENTRY_SIZE;
6442 if (htab->root.tlsdesc_plt)
6444 /* Calculate addresses. */
6445 asection *sgot = sgot = ehtab->sgot;
6446 bfd_vma pltgot = sgotplt->output_section->vma
6447 + sgotplt->output_offset;
6448 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6449 + htab->root.tlsdesc_got;
6451 /* Get GP offset. */
6452 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6453 tlsdesc_got -= elf_gp (output_bfd);
6455 /* Do relocation. */
6456 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6457 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6458 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6459 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6461 /* Insert .plt. */
6462 nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
6463 dl_tlsdesc_lazy_trampoline,
6464 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6468 /* Fill in the first three entries in the global offset table. */
6469 if (sgotplt && sgotplt->size > 0)
6471 if (sdyn == NULL)
6472 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6473 else
6474 bfd_put_32 (output_bfd,
6475 sdyn->output_section->vma + sdyn->output_offset,
6476 sgotplt->contents);
6477 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6478 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6480 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6483 return true;
6487 /* Set the right machine number. */
6489 static bool
6490 nds32_elf_object_p (bfd *abfd)
6492 static unsigned int cur_arch = 0;
6494 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6496 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6497 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6500 switch (cur_arch)
6502 default:
6503 case E_N1_ARCH:
6504 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6505 break;
6506 case E_N1H_ARCH:
6507 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6508 break;
6509 case E_NDS_ARCH_STAR_V2_0:
6510 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6511 break;
6512 case E_NDS_ARCH_STAR_V3_0:
6513 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6514 break;
6515 case E_NDS_ARCH_STAR_V3_M:
6516 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6517 break;
6520 return true;
6523 /* Store the machine number in the flags field. */
6525 static bool
6526 nds32_elf_final_write_processing (bfd *abfd)
6528 unsigned long val;
6529 static unsigned int cur_mach = 0;
6531 if (bfd_mach_n1 != bfd_get_mach (abfd))
6533 cur_mach = bfd_get_mach (abfd);
6536 switch (cur_mach)
6538 case bfd_mach_n1:
6539 /* Only happen when object is empty, since the case is abandon. */
6540 val = E_N1_ARCH;
6541 val |= E_NDS_ABI_AABI;
6542 val |= E_NDS32_ELF_VER_1_4;
6543 break;
6544 case bfd_mach_n1h:
6545 val = E_N1H_ARCH;
6546 break;
6547 case bfd_mach_n1h_v2:
6548 val = E_NDS_ARCH_STAR_V2_0;
6549 break;
6550 case bfd_mach_n1h_v3:
6551 val = E_NDS_ARCH_STAR_V3_0;
6552 break;
6553 case bfd_mach_n1h_v3m:
6554 val = E_NDS_ARCH_STAR_V3_M;
6555 break;
6556 default:
6557 val = 0;
6558 break;
6561 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6562 elf_elfheader (abfd)->e_flags |= val;
6563 return _bfd_elf_final_write_processing (abfd);
6566 /* Function to keep NDS32 specific file flags. */
6568 static bool
6569 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6571 BFD_ASSERT (!elf_flags_init (abfd)
6572 || elf_elfheader (abfd)->e_flags == flags);
6574 elf_elfheader (abfd)->e_flags = flags;
6575 elf_flags_init (abfd) = true;
6576 return true;
6579 static unsigned int
6580 convert_e_flags (unsigned int e_flags, unsigned int arch)
6582 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6584 /* From 0.9 to 1.0. */
6585 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6587 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6588 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6589 if (arch == E_NDS_ARCH_STAR_V1_0)
6591 /* Done. */
6592 return e_flags;
6596 /* From 1.0 to 2.0. */
6597 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6599 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6600 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6602 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6603 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6604 return e_flags;
6607 static bool
6608 nds32_check_vec_size (bfd *ibfd)
6610 static unsigned int nds32_vec_size = 0;
6612 asection *sec_t = NULL;
6613 bfd_byte *contents = NULL;
6615 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6617 if (sec_t && sec_t->size >= 4)
6619 /* Get vec_size in file. */
6620 unsigned int flag_t;
6622 nds32_get_section_contents (ibfd, sec_t, &contents, true);
6623 flag_t = bfd_get_32 (ibfd, contents);
6625 /* The value could only be 4 or 16. */
6627 if (!nds32_vec_size)
6628 /* Set if not set yet. */
6629 nds32_vec_size = (flag_t & 0x3);
6630 else if (nds32_vec_size != (flag_t & 0x3))
6632 _bfd_error_handler
6633 /* xgettext:c-format */
6634 (_("%pB: ISR vector size mismatch"
6635 " with previous modules, previous %u-byte, current %u-byte"),
6636 ibfd,
6637 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6638 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6639 return false;
6641 else
6642 /* Only keep the first vec_size section. */
6643 sec_t->flags |= SEC_EXCLUDE;
6646 return true;
6649 /* Merge backend specific data from an object file to the output
6650 object file when linking. */
6652 static bool
6653 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6655 bfd *obfd = info->output_bfd;
6656 flagword out_flags;
6657 flagword in_flags;
6658 flagword out_16regs;
6659 flagword in_no_mac;
6660 flagword out_no_mac;
6661 flagword in_16regs;
6662 flagword out_version;
6663 flagword in_version;
6664 flagword out_fpu_config;
6665 flagword in_fpu_config;
6667 /* FIXME: What should be checked when linking shared libraries? */
6668 if ((ibfd->flags & DYNAMIC) != 0)
6669 return true;
6671 /* TODO: Revise to use object-attributes instead. */
6672 if (!nds32_check_vec_size (ibfd))
6673 return false;
6675 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6676 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6677 return true;
6679 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6681 _bfd_error_handler
6682 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6684 bfd_set_error (bfd_error_bad_value);
6685 return false;
6688 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6689 treat as generic one without checking and merging. */
6690 if (elf_elfheader (ibfd)->e_flags)
6692 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6693 if (in_version == E_NDS32_ELF_VER_1_2)
6695 _bfd_error_handler
6696 (_("%pB: warning: older version of object file encountered, "
6697 "please recompile with current tool chain"), ibfd);
6700 /* We may need to merge V1 and V2 arch object files to V2. */
6701 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6702 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6704 /* Need to convert version. */
6705 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6706 == E_NDS_ARCH_STAR_RESERVED)
6708 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6710 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6711 == E_NDS_ARCH_STAR_V3_M
6712 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6713 == E_NDS_ARCH_STAR_V3_0)
6715 elf_elfheader (ibfd)->e_flags =
6716 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6717 | E_NDS_ARCH_STAR_V3_0;
6719 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6720 == E_NDS_ARCH_STAR_V0_9
6721 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6722 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6724 elf_elfheader (obfd)->e_flags =
6725 convert_e_flags (elf_elfheader (obfd)->e_flags,
6726 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6728 else
6730 elf_elfheader (ibfd)->e_flags =
6731 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6732 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6736 /* Extract some flags. */
6737 in_flags = elf_elfheader (ibfd)->e_flags
6738 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6739 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6741 /* The following flags need special treatment. */
6742 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6743 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6744 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6746 /* Extract some flags. */
6747 out_flags = elf_elfheader (obfd)->e_flags
6748 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6749 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6751 /* The following flags need special treatment. */
6752 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6753 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6754 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6755 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6756 if (!elf_flags_init (obfd))
6758 /* If the input is the default architecture then do not
6759 bother setting the flags for the output architecture,
6760 instead allow future merges to do this. If no future
6761 merges ever set these flags then they will retain their
6762 unitialised values, which surprise surprise, correspond
6763 to the default values. */
6764 if (bfd_get_arch_info (ibfd)->the_default)
6765 return true;
6767 elf_flags_init (obfd) = true;
6768 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6770 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6771 && bfd_get_arch_info (obfd)->the_default)
6773 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6774 bfd_get_mach (ibfd));
6777 return true;
6780 /* Check flag compatibility. */
6781 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6783 _bfd_error_handler
6784 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6785 bfd_set_error (bfd_error_bad_value);
6786 return false;
6789 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6791 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6793 _bfd_error_handler
6794 (_("%pB: error: instruction set mismatch with previous modules"),
6795 ibfd);
6797 bfd_set_error (bfd_error_bad_value);
6798 return false;
6802 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6803 and perf ext1 and DIV are mergerd to perf ext1. */
6804 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6806 elf_elfheader (obfd)->e_flags =
6807 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6808 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6809 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6810 ? E_NDS32_HAS_EXT_INST : 0)
6811 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6812 ? E_NDS32_HAS_EXT_INST : 0)
6813 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6814 | ((in_version > out_version) ? out_version : in_version);
6816 else
6818 if (in_version != out_version)
6819 _bfd_error_handler
6820 /* xgettext:c-format */
6821 (_("%pB: warning: incompatible elf-versions %s and %s"),
6822 ibfd, nds32_elfver_strtab[out_version],
6823 nds32_elfver_strtab[in_version]);
6825 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6826 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6827 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6828 | (in_version > out_version ? out_version : in_version);
6832 return true;
6835 /* Display the flags field. */
6837 static bool
6838 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6840 FILE *file = (FILE *) ptr;
6842 BFD_ASSERT (abfd != NULL && ptr != NULL);
6844 _bfd_elf_print_private_bfd_data (abfd, ptr);
6846 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6848 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6850 default:
6851 case E_N1_ARCH:
6852 fprintf (file, _(": n1 instructions"));
6853 break;
6854 case E_N1H_ARCH:
6855 fprintf (file, _(": n1h instructions"));
6856 break;
6859 fputc ('\n', file);
6861 return true;
6864 static unsigned int
6865 nds32_elf_action_discarded (asection *sec)
6868 if (startswith (sec->name, ".gcc_except_table"))
6869 return 0;
6871 return _bfd_elf_default_action_discarded (sec);
6874 static asection *
6875 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6876 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6877 Elf_Internal_Sym *sym)
6879 if (h != NULL)
6880 switch (ELF32_R_TYPE (rel->r_info))
6882 case R_NDS32_GNU_VTINHERIT:
6883 case R_NDS32_GNU_VTENTRY:
6884 case R_NDS32_RELA_GNU_VTINHERIT:
6885 case R_NDS32_RELA_GNU_VTENTRY:
6886 return NULL;
6889 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6892 static enum elf_nds32_tls_type
6893 get_tls_type (enum elf_nds32_reloc_type r_type,
6894 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6896 enum elf_nds32_tls_type tls_type;
6898 switch (r_type)
6900 case R_NDS32_TLS_LE_HI20:
6901 case R_NDS32_TLS_LE_LO12:
6902 tls_type = GOT_TLS_LE;
6903 break;
6904 case R_NDS32_TLS_IE_HI20:
6905 case R_NDS32_TLS_IE_LO12S2:
6906 case R_NDS32_TLS_IE_LO12:
6907 tls_type = GOT_TLS_IE;
6908 break;
6909 case R_NDS32_TLS_IEGP_HI20:
6910 case R_NDS32_TLS_IEGP_LO12:
6911 case R_NDS32_TLS_IEGP_LO12S2:
6912 tls_type = GOT_TLS_IEGP;
6913 break;
6914 case R_NDS32_TLS_DESC_HI20:
6915 case R_NDS32_TLS_DESC_LO12:
6916 case R_NDS32_TLS_DESC_ADD:
6917 case R_NDS32_TLS_DESC_FUNC:
6918 case R_NDS32_TLS_DESC_CALL:
6919 tls_type = GOT_TLS_DESC;
6920 break;
6921 default:
6922 tls_type = GOT_NORMAL;
6923 break;
6926 return tls_type;
6929 /* Ensure that we have allocated bookkeeping structures for ABFD's local
6930 symbols. */
6932 static bool
6933 elf32_nds32_allocate_local_sym_info (bfd *abfd)
6935 if (elf_local_got_refcounts (abfd) == NULL)
6937 bfd_size_type num_syms;
6938 bfd_size_type size;
6939 char *data;
6941 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
6942 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
6943 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
6944 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
6945 + sizeof (bfd_vma) + sizeof (int)
6946 + sizeof (bool) + sizeof (bfd_vma));
6947 data = bfd_zalloc (abfd, size);
6948 if (data == NULL)
6949 return false;
6951 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
6952 data += num_syms * sizeof (bfd_signed_vma);
6954 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
6955 data += num_syms * sizeof (char);
6957 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
6958 data += num_syms * sizeof (bfd_vma);
6960 elf32_nds32_local_gp_offset (abfd) = (int *) data;
6961 data += num_syms * sizeof (int);
6964 return true;
6967 /* Look through the relocs for a section during the first phase.
6968 Since we don't do .gots or .plts, we just need to consider the
6969 virtual table relocs for gc. */
6971 static bool
6972 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6973 asection *sec, const Elf_Internal_Rela *relocs)
6975 Elf_Internal_Shdr *symtab_hdr;
6976 struct elf_link_hash_entry **sym_hashes;
6977 const Elf_Internal_Rela *rel;
6978 const Elf_Internal_Rela *rel_end;
6979 struct elf_link_hash_table *ehtab;
6980 struct elf_nds32_link_hash_table *htab;
6981 bfd *dynobj;
6982 asection *sreloc = NULL;
6984 /* No need for relocation if relocatable already. */
6985 if (bfd_link_relocatable (info))
6987 elf32_nds32_check_relax_group (abfd, sec);
6988 return true;
6991 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6992 sym_hashes = elf_sym_hashes (abfd);
6994 ehtab = elf_hash_table (info);
6995 htab = nds32_elf_hash_table (info);
6996 dynobj = htab->root.dynobj;
6998 rel_end = relocs + sec->reloc_count;
6999 for (rel = relocs; rel < rel_end; rel++)
7001 enum elf_nds32_reloc_type r_type;
7002 struct elf_link_hash_entry *h;
7003 unsigned long r_symndx;
7004 enum elf_nds32_tls_type tls_type, old_tls_type;
7006 r_symndx = ELF32_R_SYM (rel->r_info);
7007 r_type = ELF32_R_TYPE (rel->r_info);
7008 if (r_symndx < symtab_hdr->sh_info)
7009 h = NULL;
7010 else
7012 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7013 while (h->root.type == bfd_link_hash_indirect
7014 || h->root.type == bfd_link_hash_warning)
7015 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7018 /* Create .got section if necessary.
7019 Some relocs require a global offset table. We create
7020 got section here, since these relocation need a got section
7021 and if it is not created yet. */
7022 if (ehtab->sgot == NULL)
7024 switch (r_type)
7026 case R_NDS32_GOT_HI20:
7027 case R_NDS32_GOT_LO12:
7028 case R_NDS32_GOT_LO15:
7029 case R_NDS32_GOT_LO19:
7030 case R_NDS32_GOT17S2_RELA:
7031 case R_NDS32_GOT15S2_RELA:
7032 case R_NDS32_GOTOFF:
7033 case R_NDS32_GOTOFF_HI20:
7034 case R_NDS32_GOTOFF_LO12:
7035 case R_NDS32_GOTOFF_LO15:
7036 case R_NDS32_GOTOFF_LO19:
7037 case R_NDS32_GOTPC20:
7038 case R_NDS32_GOTPC_HI20:
7039 case R_NDS32_GOTPC_LO12:
7040 case R_NDS32_GOT20:
7041 case R_NDS32_TLS_IE_HI20:
7042 case R_NDS32_TLS_IE_LO12:
7043 case R_NDS32_TLS_IE_LO12S2:
7044 case R_NDS32_TLS_IEGP_HI20:
7045 case R_NDS32_TLS_IEGP_LO12:
7046 case R_NDS32_TLS_IEGP_LO12S2:
7047 case R_NDS32_TLS_DESC_HI20:
7048 case R_NDS32_TLS_DESC_LO12:
7049 if (dynobj == NULL)
7050 htab->root.dynobj = dynobj = abfd;
7051 if (!create_got_section (dynobj, info))
7052 return false;
7053 break;
7055 default:
7056 break;
7060 /* Check relocation type. */
7061 switch ((int) r_type)
7063 case R_NDS32_GOT_HI20:
7064 case R_NDS32_GOT_LO12:
7065 case R_NDS32_GOT_LO15:
7066 case R_NDS32_GOT_LO19:
7067 case R_NDS32_GOT20:
7068 case R_NDS32_TLS_LE_HI20:
7069 case R_NDS32_TLS_LE_LO12:
7070 case R_NDS32_TLS_IE_HI20:
7071 case R_NDS32_TLS_IE_LO12:
7072 case R_NDS32_TLS_IE_LO12S2:
7073 case R_NDS32_TLS_IEGP_HI20:
7074 case R_NDS32_TLS_IEGP_LO12:
7075 case R_NDS32_TLS_IEGP_LO12S2:
7076 case R_NDS32_TLS_DESC_HI20:
7077 case R_NDS32_TLS_DESC_LO12:
7078 tls_type = get_tls_type (r_type, h);
7079 if (h)
7081 if (tls_type != GOT_TLS_LE)
7082 h->got.refcount += 1;
7083 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7085 else
7087 /* This is a global offset table entry for a local symbol. */
7088 if (!elf32_nds32_allocate_local_sym_info (abfd))
7089 return false;
7091 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7092 if (tls_type != GOT_TLS_LE)
7093 elf_local_got_refcounts (abfd)[r_symndx] += 1;
7094 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7097 /* We would already have issued an error message if there
7098 is a TLS/non-TLS mismatch, based on the symbol
7099 type. So just combine any TLS types needed. */
7100 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7101 && tls_type != GOT_NORMAL)
7102 tls_type |= old_tls_type;
7104 /* DESC to IE/IEGP if link to executable. */
7105 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7106 && (bfd_link_executable (info)))
7107 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7109 if (old_tls_type != tls_type)
7111 if (h != NULL)
7112 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7113 else
7114 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7116 break;
7117 case R_NDS32_9_PLTREL:
7118 case R_NDS32_25_PLTREL:
7119 case R_NDS32_PLTREL_HI20:
7120 case R_NDS32_PLTREL_LO12:
7121 case R_NDS32_PLT_GOTREL_HI20:
7122 case R_NDS32_PLT_GOTREL_LO12:
7123 case R_NDS32_PLT_GOTREL_LO15:
7124 case R_NDS32_PLT_GOTREL_LO19:
7125 case R_NDS32_PLT_GOTREL_LO20:
7127 /* This symbol requires a procedure linkage table entry. We
7128 actually build the entry in adjust_dynamic_symbol,
7129 because this might be a case of linking PIC code without
7130 linking in any dynamic objects, in which case we don't
7131 need to generate a procedure linkage table after all. */
7133 /* If this is a local symbol, we resolve it directly without
7134 creating a procedure linkage table entry. */
7135 if (h == NULL)
7136 continue;
7138 if (h->forced_local
7139 || (bfd_link_pie (info) && h->def_regular))
7140 break;
7142 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7143 h->needs_plt = 1;
7144 h->plt.refcount += 1;
7145 break;
7147 case R_NDS32_16_RELA:
7148 case R_NDS32_20_RELA:
7149 case R_NDS32_5_RELA:
7150 case R_NDS32_32_RELA:
7151 case R_NDS32_HI20_RELA:
7152 case R_NDS32_LO12S3_RELA:
7153 case R_NDS32_LO12S2_RELA:
7154 case R_NDS32_LO12S2_DP_RELA:
7155 case R_NDS32_LO12S2_SP_RELA:
7156 case R_NDS32_LO12S1_RELA:
7157 case R_NDS32_LO12S0_RELA:
7158 case R_NDS32_LO12S0_ORI_RELA:
7159 case R_NDS32_SDA16S3_RELA:
7160 case R_NDS32_SDA17S2_RELA:
7161 case R_NDS32_SDA18S1_RELA:
7162 case R_NDS32_SDA19S0_RELA:
7163 case R_NDS32_SDA15S3_RELA:
7164 case R_NDS32_SDA15S2_RELA:
7165 case R_NDS32_SDA12S2_DP_RELA:
7166 case R_NDS32_SDA12S2_SP_RELA:
7167 case R_NDS32_SDA15S1_RELA:
7168 case R_NDS32_SDA15S0_RELA:
7169 case R_NDS32_SDA_FP7U2_RELA:
7170 case R_NDS32_15_PCREL_RELA:
7171 case R_NDS32_17_PCREL_RELA:
7172 case R_NDS32_25_PCREL_RELA:
7174 if (h != NULL && !bfd_link_pic (info))
7176 h->non_got_ref = 1;
7177 h->plt.refcount += 1;
7180 /* If we are creating a shared library, and this is a reloc against
7181 a global symbol, or a non PC relative reloc against a local
7182 symbol, then we need to copy the reloc into the shared library.
7183 However, if we are linking with -Bsymbolic, we do not need to
7184 copy a reloc against a global symbol which is defined in an
7185 object we are including in the link (i.e., DEF_REGULAR is set).
7186 At this point we have not seen all the input files, so it is
7187 possible that DEF_REGULAR is not set now but will be set later
7188 (it is never cleared). We account for that possibility below by
7189 storing information in the dyn_relocs field of the hash table
7190 entry. A similar situation occurs when creating shared libraries
7191 and symbol visibility changes render the symbol local.
7193 If on the other hand, we are creating an executable, we may need
7194 to keep relocations for symbols satisfied by a dynamic library
7195 if we manage to avoid copy relocs for the symbol. */
7196 if ((bfd_link_pic (info)
7197 && (sec->flags & SEC_ALLOC) != 0
7198 && ((r_type != R_NDS32_25_PCREL_RELA
7199 && r_type != R_NDS32_15_PCREL_RELA
7200 && r_type != R_NDS32_17_PCREL_RELA
7201 && !(r_type == R_NDS32_32_RELA
7202 && strcmp (sec->name, ".eh_frame") == 0))
7203 || (h != NULL
7204 && (!info->symbolic
7205 || h->root.type == bfd_link_hash_defweak
7206 || !h->def_regular))))
7207 || (!bfd_link_pic (info)
7208 && (sec->flags & SEC_ALLOC) != 0
7209 && h != NULL
7210 && (h->root.type == bfd_link_hash_defweak
7211 || !h->def_regular)))
7213 struct elf_dyn_relocs *p;
7214 struct elf_dyn_relocs **head;
7216 if (dynobj == NULL)
7217 htab->root.dynobj = dynobj = abfd;
7219 /* When creating a shared object, we must copy these
7220 relocs into the output file. We create a reloc
7221 section in dynobj and make room for the reloc. */
7222 if (sreloc == NULL)
7224 const char *name;
7226 name = bfd_elf_string_from_elf_section
7227 (abfd, elf_elfheader (abfd)->e_shstrndx,
7228 elf_section_data (sec)->rela.hdr->sh_name);
7229 if (name == NULL)
7230 return false;
7232 BFD_ASSERT (startswith (name, ".rela")
7233 && strcmp (bfd_section_name (sec),
7234 name + 5) == 0);
7236 sreloc = bfd_get_section_by_name (dynobj, name);
7237 if (sreloc == NULL)
7239 flagword flags;
7241 sreloc = bfd_make_section (dynobj, name);
7242 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7243 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7244 if ((sec->flags & SEC_ALLOC) != 0)
7245 flags |= SEC_ALLOC | SEC_LOAD;
7246 if (sreloc == NULL
7247 || !bfd_set_section_flags (sreloc, flags)
7248 || !bfd_set_section_alignment (sreloc, 2))
7249 return false;
7251 elf_section_type (sreloc) = SHT_RELA;
7253 elf_section_data (sec)->sreloc = sreloc;
7256 /* If this is a global symbol, we count the number of
7257 relocations we need for this symbol. */
7258 if (h != NULL)
7259 head = &h->dyn_relocs;
7260 else
7262 asection *s;
7263 void *vpp;
7265 Elf_Internal_Sym *isym;
7266 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
7267 abfd, r_symndx);
7268 if (isym == NULL)
7269 return false;
7271 /* Track dynamic relocs needed for local syms too. */
7272 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7273 if (s == NULL)
7274 return false;
7276 vpp = &elf_section_data (s)->local_dynrel;
7277 head = (struct elf_dyn_relocs **) vpp;
7280 p = *head;
7281 if (p == NULL || p->sec != sec)
7283 size_t amt = sizeof (*p);
7284 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7285 if (p == NULL)
7286 return false;
7287 p->next = *head;
7288 *head = p;
7289 p->sec = sec;
7290 p->count = 0;
7291 p->pc_count = 0;
7294 p->count += 1;
7296 /* Since eh_frame is readonly, R_NDS32_32_RELA
7297 reloc for eh_frame will cause shared library has
7298 TEXTREL entry in the dynamic section. This lead glibc
7299 testsuites to failure (bug-13092) and cause kernel fail
7300 (bug-11819). I think the best solution is to replace
7301 absolute reloc with pc relative reloc in the eh_frame.
7302 To do that, we need to support the following issues:
7304 === For GCC ===
7305 * gcc/config/nds32/nds32.h: Define
7306 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7307 and DW_EH_PE_sdata4 into DWARF exception header when
7308 option have '-fpic'.
7310 === For binutils ===
7311 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7312 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7313 may break our nds DIFF mechanism, therefore, we
7314 must disable all linker relaxations to ensure
7315 correctness.
7316 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7317 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7318 do the necessary modification.
7320 Unfortunately, it still have some problems for nds32
7321 to support pc relative reloc in the eh_frame. So I use
7322 another solution to fix this issue.
7324 However, I find that ld always emit TEXTREL marker for
7325 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7326 correspond to R_NDS32_32_RELA for .eh_frame section.
7327 It means that we always reserve redundant entries of rel.dyn
7328 for these relocs which actually do nothing in dynamic linker.
7330 Therefore, we regard these relocs as pc relative relocs
7331 here and increase the pc_count. */
7332 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7333 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7334 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7335 || (r_type == R_NDS32_32_RELA
7336 && strcmp (sec->name, ".eh_frame") == 0))
7337 p->pc_count += 1;
7339 break;
7341 /* This relocation describes the C++ object vtable hierarchy.
7342 Reconstruct it for later use during GC. */
7343 case R_NDS32_RELA_GNU_VTINHERIT:
7344 case R_NDS32_GNU_VTINHERIT:
7345 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7346 return false;
7347 break;
7349 /* This relocation describes which C++ vtable entries are actually
7350 used. Record for later use during GC. */
7351 case R_NDS32_GNU_VTENTRY:
7352 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7353 return false;
7354 break;
7355 case R_NDS32_RELA_GNU_VTENTRY:
7356 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7357 return false;
7358 break;
7362 return true;
7365 /* Write VAL in uleb128 format to P, returning a pointer to the
7366 following byte.
7367 This code is copied from elf-attr.c. */
7369 static bfd_byte *
7370 write_uleb128 (bfd_byte *p, unsigned int val)
7372 bfd_byte c;
7375 c = val & 0x7f;
7376 val >>= 7;
7377 if (val)
7378 c |= 0x80;
7379 *(p++) = c;
7381 while (val);
7382 return p;
7385 static bfd_signed_vma
7386 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7387 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7389 bfd_signed_vma foff;
7390 bfd_vma symval, addend;
7391 asection *sym_sec;
7393 /* Get the value of the symbol referred to by the reloc. */
7394 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7396 Elf_Internal_Sym *isym;
7398 /* A local symbol. */
7399 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7401 if (isym->st_shndx == SHN_UNDEF)
7402 sym_sec = bfd_und_section_ptr;
7403 else if (isym->st_shndx == SHN_ABS)
7404 sym_sec = bfd_abs_section_ptr;
7405 else if (isym->st_shndx == SHN_COMMON)
7406 sym_sec = bfd_com_section_ptr;
7407 else
7408 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7409 symval = isym->st_value + sym_sec->output_section->vma
7410 + sym_sec->output_offset;
7412 else
7414 unsigned long indx;
7415 struct elf_link_hash_entry *h;
7417 /* An external symbol. */
7418 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7419 h = elf_sym_hashes (abfd)[indx];
7420 BFD_ASSERT (h != NULL);
7422 if (h->root.type != bfd_link_hash_defined
7423 && h->root.type != bfd_link_hash_defweak)
7424 /* This appears to be a reference to an undefined
7425 symbol. Just ignore it--it will be caught by the
7426 regular reloc processing. */
7427 return 0;
7429 if (h->root.u.def.section->flags & SEC_MERGE)
7431 sym_sec = h->root.u.def.section;
7432 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7433 elf_section_data (sym_sec)->sec_info,
7434 h->root.u.def.value);
7435 symval = symval + sym_sec->output_section->vma
7436 + sym_sec->output_offset;
7438 else
7439 symval = (h->root.u.def.value
7440 + h->root.u.def.section->output_section->vma
7441 + h->root.u.def.section->output_offset);
7444 addend = irel->r_addend;
7446 foff = (symval + addend
7447 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7448 return foff;
7452 /* Convert a 32-bit instruction to 16-bit one.
7453 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7454 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7455 type of INSN16. Return 1 if successful. */
7457 static int
7458 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7459 int *pinsn_type)
7461 uint16_t insn16 = 0;
7462 int insn_type = 0;
7463 unsigned long mach = bfd_get_mach (abfd);
7465 if (N32_SH5 (insn) != 0)
7466 return 0;
7468 switch (N32_SUB5 (insn))
7470 case N32_ALU1_ADD_SLLI:
7471 case N32_ALU1_ADD_SRLI:
7472 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7474 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7475 N32_RB5 (insn));
7476 insn_type = NDS32_INSN_ADD333;
7478 else if (N32_IS_RT4 (insn))
7480 if (N32_RT5 (insn) == N32_RA5 (insn))
7481 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7482 else if (N32_RT5 (insn) == N32_RB5 (insn))
7483 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7484 insn_type = NDS32_INSN_ADD45;
7486 break;
7488 case N32_ALU1_SUB_SLLI:
7489 case N32_ALU1_SUB_SRLI:
7490 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7492 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7493 N32_RB5 (insn));
7494 insn_type = NDS32_INSN_SUB333;
7496 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7498 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7499 insn_type = NDS32_INSN_SUB45;
7501 break;
7503 case N32_ALU1_AND_SLLI:
7504 case N32_ALU1_AND_SRLI:
7505 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7506 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7507 && N32_IS_RB3 (insn))
7509 if (N32_RT5 (insn) == N32_RA5 (insn))
7510 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7511 else if (N32_RT5 (insn) == N32_RB5 (insn))
7512 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7513 if (insn16)
7514 insn_type = NDS32_INSN_AND33;
7516 break;
7518 case N32_ALU1_XOR_SLLI:
7519 case N32_ALU1_XOR_SRLI:
7520 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7521 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7522 && N32_IS_RB3 (insn))
7524 if (N32_RT5 (insn) == N32_RA5 (insn))
7525 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7526 else if (N32_RT5 (insn) == N32_RB5 (insn))
7527 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7528 if (insn16)
7529 insn_type = NDS32_INSN_XOR33;
7531 break;
7533 case N32_ALU1_OR_SLLI:
7534 case N32_ALU1_OR_SRLI:
7535 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7536 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7537 && N32_IS_RB3 (insn))
7539 if (N32_RT5 (insn) == N32_RA5 (insn))
7540 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7541 else if (N32_RT5 (insn) == N32_RB5 (insn))
7542 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7543 if (insn16)
7544 insn_type = NDS32_INSN_OR33;
7546 break;
7547 case N32_ALU1_NOR:
7548 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7549 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7550 && N32_RA5 (insn) == N32_RB5 (insn))
7552 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7553 insn_type = NDS32_INSN_NOT33;
7555 break;
7556 case N32_ALU1_SRAI:
7557 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7559 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7560 insn_type = NDS32_INSN_SRAI45;
7562 break;
7564 case N32_ALU1_SRLI:
7565 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7567 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7568 insn_type = NDS32_INSN_SRLI45;
7570 break;
7572 case N32_ALU1_SLLI:
7573 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7575 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7576 N32_UB5 (insn));
7577 insn_type = NDS32_INSN_SLLI333;
7579 break;
7581 case N32_ALU1_ZEH:
7582 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7584 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7585 insn_type = NDS32_INSN_ZEH33;
7587 break;
7589 case N32_ALU1_SEB:
7590 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7592 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7593 insn_type = NDS32_INSN_SEB33;
7595 break;
7597 case N32_ALU1_SEH:
7598 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7600 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7601 insn_type = NDS32_INSN_SEH33;
7603 break;
7605 case N32_ALU1_SLT:
7606 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7608 /* Implicit r15. */
7609 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7610 insn_type = NDS32_INSN_SLT45;
7612 break;
7614 case N32_ALU1_SLTS:
7615 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7617 /* Implicit r15. */
7618 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7619 insn_type = NDS32_INSN_SLTS45;
7621 break;
7624 if ((insn16 & 0x8000) == 0)
7625 return 0;
7627 if (pinsn16)
7628 *pinsn16 = insn16;
7629 if (pinsn_type)
7630 *pinsn_type = insn_type;
7631 return 1;
7634 static int
7635 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7636 int *pinsn_type)
7638 uint16_t insn16 = 0;
7639 int insn_type;
7640 unsigned long mach = bfd_get_mach (abfd);
7642 /* TODO: bset, bclr, btgl, btst. */
7643 if (__GF (insn, 6, 4) != 0)
7644 return 0;
7646 switch (N32_IMMU (insn, 6))
7648 case N32_ALU2_MUL:
7649 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7650 && N32_IS_RB3 (insn))
7652 if (N32_RT5 (insn) == N32_RA5 (insn))
7653 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7654 else if (N32_RT5 (insn) == N32_RB5 (insn))
7655 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7656 if (insn16)
7657 insn_type = NDS32_INSN_MUL33;
7661 if ((insn16 & 0x8000) == 0)
7662 return 0;
7664 if (pinsn16)
7665 *pinsn16 = insn16;
7666 if (pinsn_type)
7667 *pinsn_type = insn_type;
7668 return 1;
7672 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7673 int *pinsn_type)
7675 int op6;
7676 uint16_t insn16 = 0;
7677 int insn_type = 0;
7678 unsigned long mach = bfd_get_mach (abfd);
7680 /* Decode 32-bit instruction. */
7681 if (insn & 0x80000000)
7683 /* Not 32-bit insn. */
7684 return 0;
7687 op6 = N32_OP6 (insn);
7689 /* Convert it to 16-bit instruction. */
7690 switch (op6)
7692 case N32_OP6_MOVI:
7693 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7695 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7696 insn_type = NDS32_INSN_MOVI55;
7698 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7699 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7701 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7702 N32_IMM20S (insn) - 16);
7703 insn_type = NDS32_INSN_MOVPI45;
7705 break;
7707 case N32_OP6_ADDI:
7708 if (N32_IMM15S (insn) == 0)
7710 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7711 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7712 if (mach <= MACH_V2
7713 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7715 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7716 insn_type = NDS32_INSN_MOV55;
7719 else if (N32_IMM15S (insn) > 0)
7721 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7723 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7724 N32_IMM15S (insn));
7725 insn_type = NDS32_INSN_ADDI333;
7727 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7728 && N32_IMM15S (insn) < 32)
7730 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7731 insn_type = NDS32_INSN_ADDI45;
7733 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7734 && N32_RT5 (insn) == N32_RA5 (insn)
7735 && N32_IMM15S (insn) < 512)
7737 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7738 insn_type = NDS32_INSN_ADDI10_SP;
7740 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7741 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7742 && (N32_IMM15S (insn) % 4 == 0))
7744 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7745 N32_IMM15S (insn) >> 2);
7746 insn_type = NDS32_INSN_ADDRI36_SP;
7749 else
7751 /* Less than 0. */
7752 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7754 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7755 0 - N32_IMM15S (insn));
7756 insn_type = NDS32_INSN_SUBI333;
7758 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7759 && N32_IMM15S (insn) > -32)
7761 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7762 0 - N32_IMM15S (insn));
7763 insn_type = NDS32_INSN_SUBI45;
7765 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7766 && N32_RT5 (insn) == N32_RA5 (insn)
7767 && N32_IMM15S (insn) >= -512)
7769 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7770 insn_type = NDS32_INSN_ADDI10_SP;
7773 break;
7775 case N32_OP6_ORI:
7776 if (N32_IMM15S (insn) == 0)
7778 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7779 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7780 if (mach <= MACH_V2
7781 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7783 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7784 insn_type = NDS32_INSN_MOV55;
7787 break;
7789 case N32_OP6_SUBRI:
7790 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7791 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7793 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7794 insn_type = NDS32_INSN_NEG33;
7796 break;
7798 case N32_OP6_ANDI:
7799 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7801 if (N32_IMM15U (insn) == 1)
7803 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7804 insn_type = NDS32_INSN_XLSB33;
7806 else if (N32_IMM15U (insn) == 0x7ff)
7808 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7809 insn_type = NDS32_INSN_X11B33;
7811 else if (N32_IMM15U (insn) == 0xff)
7813 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7814 insn_type = NDS32_INSN_ZEB33;
7816 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7817 && N32_IMM15U (insn) < 256)
7819 int imm15u = N32_IMM15U (insn);
7821 if (__builtin_popcount (imm15u) == 1)
7823 /* BMSKI33 */
7824 int imm3u = __builtin_ctz (imm15u);
7826 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7827 insn_type = NDS32_INSN_BMSKI33;
7829 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7831 /* FEXTI33 */
7832 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7834 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7835 insn_type = NDS32_INSN_FEXTI33;
7839 break;
7841 case N32_OP6_SLTI:
7842 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7843 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7845 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7846 insn_type = NDS32_INSN_SLTI45;
7848 break;
7850 case N32_OP6_SLTSI:
7851 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7852 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7854 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7855 insn_type = NDS32_INSN_SLTSI45;
7857 break;
7859 case N32_OP6_LWI:
7860 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7862 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7863 insn_type = NDS32_INSN_LWI450;
7865 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7866 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7868 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7869 N32_IMM15S (insn));
7870 insn_type = NDS32_INSN_LWI333;
7872 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7873 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7875 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7876 insn_type = NDS32_INSN_LWI37;
7878 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7879 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7881 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7882 insn_type = NDS32_INSN_LWI37_SP;
7884 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7885 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7887 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7888 N32_IMM15S (insn) + 32);
7889 insn_type = NDS32_INSN_LWI45_FE;
7891 break;
7893 case N32_OP6_SWI:
7894 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7896 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7897 insn_type = NDS32_INSN_SWI450;
7899 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7900 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7902 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7903 N32_IMM15S (insn));
7904 insn_type = NDS32_INSN_SWI333;
7906 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7907 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7909 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7910 insn_type = NDS32_INSN_SWI37;
7912 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7913 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7915 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7916 insn_type = NDS32_INSN_SWI37_SP;
7918 break;
7920 case N32_OP6_LWI_BI:
7921 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7922 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7924 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7925 N32_IMM15S (insn));
7926 insn_type = NDS32_INSN_LWI333_BI;
7928 break;
7930 case N32_OP6_SWI_BI:
7931 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7932 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7934 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7935 N32_IMM15S (insn));
7936 insn_type = NDS32_INSN_SWI333_BI;
7938 break;
7940 case N32_OP6_LHI:
7941 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7942 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7944 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7945 N32_IMM15S (insn));
7946 insn_type = NDS32_INSN_LHI333;
7948 break;
7950 case N32_OP6_SHI:
7951 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7952 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7954 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
7955 N32_IMM15S (insn));
7956 insn_type = NDS32_INSN_SHI333;
7958 break;
7960 case N32_OP6_LBI:
7961 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7962 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7964 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
7965 N32_IMM15S (insn));
7966 insn_type = NDS32_INSN_LBI333;
7968 break;
7970 case N32_OP6_SBI:
7971 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7972 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7974 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
7975 N32_IMM15S (insn));
7976 insn_type = NDS32_INSN_SBI333;
7978 break;
7980 case N32_OP6_ALU1:
7981 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
7983 case N32_OP6_ALU2:
7984 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
7986 case N32_OP6_BR1:
7987 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
7988 goto done;
7990 if ((insn & N32_BIT (14)) == 0)
7992 /* N32_BR1_BEQ */
7993 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7994 && N32_RT5 (insn) != REG_R5)
7995 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
7996 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7997 && N32_RA5 (insn) != REG_R5)
7998 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
7999 insn_type = NDS32_INSN_BEQS38;
8000 break;
8002 else
8004 /* N32_BR1_BNE */
8005 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8006 && N32_RT5 (insn) != REG_R5)
8007 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8008 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8009 && N32_RA5 (insn) != REG_R5)
8010 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8011 insn_type = NDS32_INSN_BNES38;
8012 break;
8014 break;
8016 case N32_OP6_BR2:
8017 switch (N32_BR2_SUB (insn))
8019 case N32_BR2_BEQZ:
8020 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8022 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8023 insn_type = NDS32_INSN_BEQZ38;
8025 else if (N32_RT5 (insn) == REG_R15
8026 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8028 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8029 insn_type = NDS32_INSN_BEQZS8;
8031 break;
8033 case N32_BR2_BNEZ:
8034 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8036 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8037 insn_type = NDS32_INSN_BNEZ38;
8039 else if (N32_RT5 (insn) == REG_R15
8040 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8042 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8043 insn_type = NDS32_INSN_BNEZS8;
8045 break;
8047 case N32_BR2_SOP0:
8048 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8050 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8051 insn_type = NDS32_INSN_IFCALL9;
8053 break;
8055 break;
8057 case N32_OP6_JI:
8058 if ((insn & N32_BIT (24)) == 0)
8060 /* N32_JI_J */
8061 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8063 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8064 insn_type = NDS32_INSN_J8;
8067 break;
8069 case N32_OP6_JREG:
8070 if (__GF (insn, 8, 2) != 0)
8071 goto done;
8073 switch (N32_IMMU (insn, 5))
8075 case N32_JREG_JR:
8076 if (N32_JREG_HINT (insn) == 0)
8078 /* jr */
8079 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8080 insn_type = NDS32_INSN_JR5;
8082 else if (N32_JREG_HINT (insn) == 1)
8084 /* ret */
8085 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8086 insn_type = NDS32_INSN_RET5;
8088 else if (N32_JREG_HINT (insn) == 3)
8090 /* ifret = mov55 $sp, $sp */
8091 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8092 insn_type = NDS32_INSN_IFRET;
8094 break;
8096 case N32_JREG_JRAL:
8097 /* It's convertible when return rt5 is $lp and address
8098 translation is kept. */
8099 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8101 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8102 insn_type = NDS32_INSN_JRAL5;
8104 break;
8106 break;
8108 case N32_OP6_MISC:
8109 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8111 /* For v3, swid above 31 are used for ex9.it. */
8112 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8113 insn_type = NDS32_INSN_BREAK16;
8115 break;
8117 default:
8118 /* This instruction has no 16-bit variant. */
8119 goto done;
8122 done:
8123 /* Bit-15 of insn16 should be set for a valid instruction. */
8124 if ((insn16 & 0x8000) == 0)
8125 return 0;
8127 if (pinsn16)
8128 *pinsn16 = insn16;
8129 if (pinsn_type)
8130 *pinsn_type = insn_type;
8131 return 1;
8134 static int
8135 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8136 Elf_Internal_Rela *reloc)
8138 uint16_t insn16 = 0;
8140 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8141 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8142 return 0;
8144 if (!N32_IS_RT3 (insn))
8145 return 0;
8147 switch (N32_OP6 (insn))
8149 case N32_OP6_LWI:
8150 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8151 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8152 break;
8153 case N32_OP6_SWI:
8154 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8155 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8156 break;
8157 case N32_OP6_HWGP:
8158 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8159 break;
8161 if (__GF (insn, 17, 3) == 6)
8162 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8163 else if (__GF (insn, 17, 3) == 7)
8164 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8165 break;
8168 if ((insn16 & 0x8000) == 0)
8169 return 0;
8171 *pinsn16 = insn16;
8172 return 1;
8175 /* Convert a 16-bit instruction to 32-bit one.
8176 INSN16 it the input and PINSN it the point to output.
8177 Return non-zero on successful. Otherwise 0 is returned. */
8180 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8182 uint32_t insn = 0xffffffff;
8183 unsigned long mach = bfd_get_mach (abfd);
8185 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8187 switch (__GF (insn16, 9, 6))
8189 case 0x4: /* add45 */
8190 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8191 N16_RA5 (insn16));
8192 goto done;
8193 case 0x5: /* sub45 */
8194 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8195 N16_RA5 (insn16));
8196 goto done;
8197 case 0x6: /* addi45 */
8198 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8199 N16_IMM5U (insn16));
8200 goto done;
8201 case 0x7: /* subi45 */
8202 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8203 -N16_IMM5U (insn16));
8204 goto done;
8205 case 0x8: /* srai45 */
8206 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8207 N16_IMM5U (insn16));
8208 goto done;
8209 case 0x9: /* srli45 */
8210 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8211 N16_IMM5U (insn16));
8212 goto done;
8213 case 0xa: /* slli333 */
8214 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8215 N16_IMM3U (insn16));
8216 goto done;
8217 case 0xc: /* add333 */
8218 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8219 N16_RB3 (insn16));
8220 goto done;
8221 case 0xd: /* sub333 */
8222 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8223 N16_RB3 (insn16));
8224 goto done;
8225 case 0xe: /* addi333 */
8226 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8227 N16_IMM3U (insn16));
8228 goto done;
8229 case 0xf: /* subi333 */
8230 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8231 -N16_IMM3U (insn16));
8232 goto done;
8233 case 0x10: /* lwi333 */
8234 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8235 N16_IMM3U (insn16));
8236 goto done;
8237 case 0x12: /* lhi333 */
8238 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8239 N16_IMM3U (insn16));
8240 goto done;
8241 case 0x13: /* lbi333 */
8242 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8243 N16_IMM3U (insn16));
8244 goto done;
8245 case 0x11: /* lwi333.bi */
8246 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8247 N16_IMM3U (insn16));
8248 goto done;
8249 case 0x14: /* swi333 */
8250 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8251 N16_IMM3U (insn16));
8252 goto done;
8253 case 0x16: /* shi333 */
8254 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8255 N16_IMM3U (insn16));
8256 goto done;
8257 case 0x17: /* sbi333 */
8258 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8259 N16_IMM3U (insn16));
8260 goto done;
8261 case 0x15: /* swi333.bi */
8262 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8263 N16_IMM3U (insn16));
8264 goto done;
8265 case 0x18: /* addri36.sp */
8266 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8267 N16_IMM6U (insn16) << 2);
8268 goto done;
8269 case 0x19: /* lwi45.fe */
8270 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8271 (N16_IMM5U (insn16) - 32));
8272 goto done;
8273 case 0x1a: /* lwi450 */
8274 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8275 goto done;
8276 case 0x1b: /* swi450 */
8277 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8278 goto done;
8280 /* These are r15 implied instructions. */
8281 case 0x30: /* slts45 */
8282 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8283 goto done;
8284 case 0x31: /* slt45 */
8285 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8286 goto done;
8287 case 0x32: /* sltsi45 */
8288 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8289 goto done;
8290 case 0x33: /* slti45 */
8291 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8292 goto done;
8293 case 0x34: /* beqzs8, bnezs8 */
8294 if (insn16 & N32_BIT (8))
8295 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8296 else
8297 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8298 goto done;
8300 case 0x35: /* break16, ex9.it */
8301 /* Only consider range of v3 break16. */
8302 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8303 goto done;
8305 case 0x3c: /* ifcall9 */
8306 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8307 goto done;
8308 case 0x3d: /* movpi45 */
8309 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8310 goto done;
8312 case 0x3f: /* MISC33 */
8313 switch (insn16 & 0x7)
8315 case 2: /* neg33 */
8316 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8317 break;
8318 case 3: /* not33 */
8319 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8320 N16_RA3 (insn16));
8321 break;
8322 case 4: /* mul33 */
8323 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8324 N16_RA3 (insn16));
8325 break;
8326 case 5: /* xor33 */
8327 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8328 N16_RA3 (insn16));
8329 break;
8330 case 6: /* and33 */
8331 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8332 N16_RA3 (insn16));
8333 break;
8334 case 7: /* or33 */
8335 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8336 N16_RA3 (insn16));
8337 break;
8339 goto done;
8341 case 0xb:
8342 switch (insn16 & 0x7)
8344 case 0: /* zeb33 */
8345 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8346 break;
8347 case 1: /* zeh33 */
8348 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8349 break;
8350 case 2: /* seb33 */
8351 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8352 break;
8353 case 3: /* seh33 */
8354 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8355 break;
8356 case 4: /* xlsb33 */
8357 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8358 break;
8359 case 5: /* x11b33 */
8360 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8361 break;
8362 case 6: /* bmski33 */
8363 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8364 1 << __GF (insn16, 3, 3));
8365 break;
8366 case 7: /* fexti33 */
8367 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8368 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8369 break;
8371 goto done;
8374 switch (__GF (insn16, 10, 5))
8376 case 0x0: /* mov55 or ifret16 */
8377 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8378 && N16_RT5 (insn16) == N16_RA5 (insn16))
8379 insn = N32_JREG (JR, 0, 0, 0, 3);
8380 else
8381 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8382 goto done;
8383 case 0x1: /* movi55 */
8384 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8385 goto done;
8386 case 0x1b: /* addi10s (V2) */
8387 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8388 goto done;
8391 switch (__GF (insn16, 11, 4))
8393 case 0x7: /* lwi37.fp/swi37.fp */
8394 if (insn16 & N32_BIT (7)) /* swi37.fp */
8395 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8396 else /* lwi37.fp */
8397 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8398 goto done;
8399 case 0x8: /* beqz38 */
8400 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8401 goto done;
8402 case 0x9: /* bnez38 */
8403 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8404 goto done;
8405 case 0xa: /* beqs38/j8, implied r5 */
8406 if (N16_RT38 (insn16) == 5)
8407 insn = N32_JI (J, N16_IMM8S (insn16));
8408 else
8409 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8410 goto done;
8411 case 0xb: /* bnes38 and others. */
8412 if (N16_RT38 (insn16) == 5)
8414 switch (__GF (insn16, 5, 3))
8416 case 0: /* jr5 */
8417 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8418 break;
8419 case 4: /* ret5 */
8420 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8421 break;
8422 case 1: /* jral5 */
8423 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8424 break;
8425 case 2: /* ex9.it imm5 */
8426 /* ex9.it had no 32-bit variantl. */
8427 break;
8428 case 5: /* add5.pc */
8429 /* add5.pc had no 32-bit variantl. */
8430 break;
8433 else /* bnes38 */
8434 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8435 goto done;
8436 case 0xe: /* lwi37/swi37 */
8437 if (insn16 & (1 << 7)) /* swi37.sp */
8438 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8439 else /* lwi37.sp */
8440 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8441 goto done;
8444 done:
8445 if (insn & 0x80000000)
8446 return 0;
8448 if (pinsn)
8449 *pinsn = insn;
8450 return 1;
8454 static bool
8455 is_sda_access_insn (unsigned long insn)
8457 switch (N32_OP6 (insn))
8459 case N32_OP6_LWI:
8460 case N32_OP6_LHI:
8461 case N32_OP6_LHSI:
8462 case N32_OP6_LBI:
8463 case N32_OP6_LBSI:
8464 case N32_OP6_SWI:
8465 case N32_OP6_SHI:
8466 case N32_OP6_SBI:
8467 case N32_OP6_LWC:
8468 case N32_OP6_LDC:
8469 case N32_OP6_SWC:
8470 case N32_OP6_SDC:
8471 return true;
8472 default:
8475 return false;
8478 static unsigned long
8479 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8481 uint32_t oinsn = 0;
8483 switch (type)
8485 case R_NDS32_GOT_LO12:
8486 case R_NDS32_GOTOFF_LO12:
8487 case R_NDS32_PLTREL_LO12:
8488 case R_NDS32_PLT_GOTREL_LO12:
8489 case R_NDS32_LO12S0_RELA:
8490 switch (N32_OP6 (insn))
8492 case N32_OP6_LBI:
8493 /* lbi.gp */
8494 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8495 break;
8496 case N32_OP6_LBSI:
8497 /* lbsi.gp */
8498 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8499 break;
8500 case N32_OP6_SBI:
8501 /* sbi.gp */
8502 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8503 break;
8504 case N32_OP6_ORI:
8505 /* addi.gp */
8506 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8507 break;
8509 break;
8511 case R_NDS32_LO12S1_RELA:
8512 switch (N32_OP6 (insn))
8514 case N32_OP6_LHI:
8515 /* lhi.gp */
8516 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8517 break;
8518 case N32_OP6_LHSI:
8519 /* lhsi.gp */
8520 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8521 break;
8522 case N32_OP6_SHI:
8523 /* shi.gp */
8524 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8525 break;
8527 break;
8529 case R_NDS32_LO12S2_RELA:
8530 switch (N32_OP6 (insn))
8532 case N32_OP6_LWI:
8533 /* lwi.gp */
8534 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8535 break;
8536 case N32_OP6_SWI:
8537 /* swi.gp */
8538 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8539 break;
8541 break;
8543 case R_NDS32_LO12S2_DP_RELA:
8544 case R_NDS32_LO12S2_SP_RELA:
8545 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8546 break;
8549 if (oinsn)
8550 *pinsn = oinsn;
8552 return oinsn != 0;
8555 /* Linker hasn't found the correct merge section for non-section symbol
8556 in relax time, this work is left to the function elf_link_input_bfd().
8557 So for non-section symbol, _bfd_merged_section_offset is also needed
8558 to find the correct symbol address. */
8560 static bfd_vma
8561 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8562 asection **psec, Elf_Internal_Rela *rel)
8564 asection *sec = *psec;
8565 bfd_vma relocation;
8567 relocation = (sec->output_section->vma
8568 + sec->output_offset + sym->st_value);
8569 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8571 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8572 rel->r_addend =
8573 _bfd_merged_section_offset (abfd, psec,
8574 elf_section_data (sec)->sec_info,
8575 sym->st_value + rel->r_addend);
8576 else
8577 rel->r_addend =
8578 _bfd_merged_section_offset (abfd, psec,
8579 elf_section_data (sec)->sec_info,
8580 sym->st_value) + rel->r_addend;
8582 if (sec != *psec)
8584 /* If we have changed the section, and our original section is
8585 marked with SEC_EXCLUDE, it means that the original
8586 SEC_MERGE section has been completely subsumed in some
8587 other SEC_MERGE section. In this case, we need to leave
8588 some info around for --emit-relocs. */
8589 if ((sec->flags & SEC_EXCLUDE) != 0)
8590 sec->kept_section = *psec;
8591 sec = *psec;
8593 rel->r_addend -= relocation;
8594 rel->r_addend += sec->output_section->vma + sec->output_offset;
8596 return relocation;
8599 static bfd_vma
8600 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8601 Elf_Internal_Sym *isymbuf,
8602 Elf_Internal_Shdr *symtab_hdr)
8604 bfd_signed_vma foff;
8605 bfd_vma symval, addend;
8606 Elf_Internal_Rela irel_fn;
8607 Elf_Internal_Sym *isym;
8608 asection *sym_sec;
8610 /* Get the value of the symbol referred to by the reloc. */
8611 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8613 /* A local symbol. */
8614 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8616 if (isym->st_shndx == SHN_UNDEF)
8617 sym_sec = bfd_und_section_ptr;
8618 else if (isym->st_shndx == SHN_ABS)
8619 sym_sec = bfd_abs_section_ptr;
8620 else if (isym->st_shndx == SHN_COMMON)
8621 sym_sec = bfd_com_section_ptr;
8622 else
8623 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8624 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8625 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8626 addend = irel_fn.r_addend;
8628 else
8630 unsigned long indx;
8631 struct elf_link_hash_entry *h;
8633 /* An external symbol. */
8634 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8635 h = elf_sym_hashes (abfd)[indx];
8636 BFD_ASSERT (h != NULL);
8638 while (h->root.type == bfd_link_hash_indirect
8639 || h->root.type == bfd_link_hash_warning)
8640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8642 if (h->root.type != bfd_link_hash_defined
8643 && h->root.type != bfd_link_hash_defweak)
8644 /* This appears to be a reference to an undefined
8645 symbol. Just ignore it--it will be caught by the
8646 regular reloc processing. */
8647 return 0;
8649 if (h->root.u.def.section->flags & SEC_MERGE)
8651 sym_sec = h->root.u.def.section;
8652 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8653 (sym_sec)->sec_info, h->root.u.def.value);
8654 symval = symval + sym_sec->output_section->vma
8655 + sym_sec->output_offset;
8657 else
8658 symval = (h->root.u.def.value
8659 + h->root.u.def.section->output_section->vma
8660 + h->root.u.def.section->output_offset);
8661 addend = irel->r_addend;
8664 foff = symval + addend;
8666 return foff;
8669 static int
8670 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8671 asection *sec, Elf_Internal_Rela *rel)
8673 bfd_byte *contents;
8674 unsigned short insn16;
8676 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8677 return false;
8678 contents = elf_section_data (sec)->this_hdr.contents;
8679 insn16 = bfd_getb16 (contents + rel->r_offset);
8680 if (insn16 == NDS32_NOP16)
8681 return true;
8682 return false;
8685 /* It checks whether the instruction could be converted to
8686 16-bit form and returns the converted one.
8688 `internal_relocs' is supposed to be sorted. */
8690 static int
8691 is_convert_32_to_16 (bfd *abfd, asection *sec,
8692 Elf_Internal_Rela *reloc,
8693 Elf_Internal_Rela *internal_relocs,
8694 Elf_Internal_Rela *irelend,
8695 uint16_t *insn16)
8697 #define NORMAL_32_TO_16 (1 << 0)
8698 #define SPECIAL_32_TO_16 (1 << 1)
8699 bfd_byte *contents = NULL;
8700 bfd_signed_vma off;
8701 bfd_vma mem_addr;
8702 uint32_t insn = 0;
8703 Elf_Internal_Rela *pc_rel;
8704 Elf_Internal_Shdr *symtab_hdr;
8705 Elf_Internal_Sym *isymbuf = NULL;
8706 int convert_type;
8707 bfd_vma offset;
8709 if (reloc->r_offset + 4 > sec->size)
8710 return false;
8712 offset = reloc->r_offset;
8714 if (!nds32_get_section_contents (abfd, sec, &contents, true))
8715 return false;
8716 insn = bfd_getb32 (contents + offset);
8718 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8719 convert_type = NORMAL_32_TO_16;
8720 else if (special_convert_32_to_16 (insn, insn16, reloc))
8721 convert_type = SPECIAL_32_TO_16;
8722 else
8723 return false;
8725 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8726 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8727 return false;
8729 /* Find the first relocation of the same relocation-type,
8730 so we iteratie them forward. */
8731 pc_rel = reloc;
8732 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8733 pc_rel--;
8735 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8737 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8738 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8739 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8740 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8742 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8743 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8744 || off == 0)
8745 return false;
8746 break;
8748 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8750 /* movi => movi55 */
8751 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8752 symtab_hdr);
8753 /* mem_addr is unsigned, but the value should
8754 be between [-16, 15]. */
8755 if ((mem_addr + 0x10) >> 5)
8756 return false;
8757 break;
8759 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8760 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8762 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8763 because it can be relaxed to addi for TLS_LE_ADD. */
8764 return false;
8766 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8767 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8768 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8769 && convert_type == SPECIAL_32_TO_16)
8771 /* fp-as-gp
8772 We've selected a best fp-base for this access, so we can
8773 always resolve it anyway. Do nothing. */
8774 break;
8776 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8777 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8778 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8779 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8780 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8781 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8783 /* Prevent unresolved addi instruction translate
8784 to addi45 or addi333. */
8785 return false;
8787 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8789 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8790 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8791 return false;
8792 break;
8796 return true;
8799 static void
8800 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8801 Elf_Internal_Rela *reloc,
8802 Elf_Internal_Rela *internal_relocs,
8803 Elf_Internal_Rela *irelend,
8804 unsigned short insn16)
8806 Elf_Internal_Rela *pc_rel;
8807 bfd_vma offset;
8809 offset = reloc->r_offset;
8810 bfd_putb16 (insn16, contents + offset);
8811 /* Find the first relocation of the same relocation-type,
8812 so we iteratie them forward. */
8813 pc_rel = reloc;
8814 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8815 pc_rel--;
8817 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8819 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8820 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8821 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8823 pc_rel->r_info =
8824 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8826 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8827 pc_rel->r_info =
8828 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8829 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8830 pc_rel->r_info =
8831 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8832 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8833 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8834 pc_rel->r_info =
8835 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8836 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8837 pc_rel->r_info =
8838 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8842 /* Find a relocation of type specified by `reloc_type'
8843 of the same r_offset with reloc.
8844 If not found, return irelend.
8846 Assuming relocations are sorted by r_offset,
8847 we find the relocation from `reloc' backward untill relocs,
8848 or find it from `reloc' forward untill irelend. */
8850 static Elf_Internal_Rela *
8851 find_relocs_at_address (Elf_Internal_Rela *reloc,
8852 Elf_Internal_Rela *relocs,
8853 Elf_Internal_Rela *irelend,
8854 enum elf_nds32_reloc_type reloc_type)
8856 Elf_Internal_Rela *rel_t;
8858 /* Find backward. */
8859 for (rel_t = reloc;
8860 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8861 rel_t--)
8862 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8863 return rel_t;
8865 /* We didn't find it backward. Try find it forward. */
8866 for (rel_t = reloc;
8867 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8868 rel_t++)
8869 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8870 return rel_t;
8872 return irelend;
8875 /* Find a relocation of specified type and offset.
8876 `reloc' is just a refence point to find a relocation at specified offset.
8877 If not found, return irelend.
8879 Assuming relocations are sorted by r_offset,
8880 we find the relocation from `reloc' backward untill relocs,
8881 or find it from `reloc' forward untill irelend. */
8883 static Elf_Internal_Rela *
8884 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8885 Elf_Internal_Rela *relocs,
8886 Elf_Internal_Rela *irelend,
8887 enum elf_nds32_reloc_type reloc_type,
8888 bfd_vma offset_p)
8890 Elf_Internal_Rela *rel_t = NULL;
8892 /* First, we try to find a relocation of offset `offset_p',
8893 and then we use find_relocs_at_address to find specific type. */
8895 if (reloc->r_offset > offset_p)
8897 /* Find backward. */
8898 for (rel_t = reloc;
8899 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8900 /* Do nothing. */;
8902 else if (reloc->r_offset < offset_p)
8904 /* Find forward. */
8905 for (rel_t = reloc;
8906 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8907 /* Do nothing. */;
8909 else
8910 rel_t = reloc;
8912 /* Not found? */
8913 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8914 return irelend;
8916 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8919 typedef struct nds32_elf_blank nds32_elf_blank_t;
8920 struct nds32_elf_blank
8922 /* Where the blank begins. */
8923 bfd_vma offset;
8924 /* The size of the blank. */
8925 bfd_vma size;
8926 /* The accumulative size before this blank. */
8927 bfd_vma total_size;
8928 nds32_elf_blank_t *next;
8929 nds32_elf_blank_t *prev;
8932 static nds32_elf_blank_t *blank_free_list = NULL;
8934 static nds32_elf_blank_t *
8935 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8937 nds32_elf_blank_t *blank_t;
8939 if (blank_free_list)
8941 blank_t = blank_free_list;
8942 blank_free_list = blank_free_list->next;
8944 else
8945 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
8947 if (blank_t == NULL)
8948 return NULL;
8950 blank_t->offset = offset_p;
8951 blank_t->size = size_p;
8952 blank_t->total_size = 0;
8953 blank_t->next = NULL;
8954 blank_t->prev = NULL;
8956 return blank_t;
8959 static void
8960 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
8962 if (blank_free_list)
8964 blank_free_list->prev = blank_p;
8965 blank_p->next = blank_free_list;
8967 else
8968 blank_p->next = NULL;
8970 blank_p->prev = NULL;
8971 blank_free_list = blank_p;
8974 static void
8975 clean_nds32_elf_blank (void)
8977 nds32_elf_blank_t *blank_t;
8979 while (blank_free_list)
8981 blank_t = blank_free_list;
8982 blank_free_list = blank_free_list->next;
8983 free (blank_t);
8987 static nds32_elf_blank_t *
8988 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
8990 nds32_elf_blank_t *blank_t;
8992 if (!blank_p)
8993 return NULL;
8994 blank_t = blank_p;
8996 while (blank_t && addr < blank_t->offset)
8997 blank_t = blank_t->prev;
8998 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
8999 blank_t = blank_t->next;
9001 return blank_t;
9004 static bfd_vma
9005 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9006 int overwrite)
9008 nds32_elf_blank_t *blank_t;
9010 blank_t = search_nds32_elf_blank (*blank_p, addr);
9011 if (!blank_t)
9012 return 0;
9014 if (overwrite)
9015 *blank_p = blank_t;
9017 if (addr < blank_t->offset + blank_t->size)
9018 return blank_t->total_size + (addr - blank_t->offset);
9019 else
9020 return blank_t->total_size + blank_t->size;
9023 static bool
9024 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9026 nds32_elf_blank_t *blank_t, *blank_t2;
9028 if (!*blank_p)
9030 *blank_p = create_nds32_elf_blank (addr, len);
9031 return *blank_p != NULL;
9034 blank_t = search_nds32_elf_blank (*blank_p, addr);
9036 if (blank_t == NULL)
9038 blank_t = create_nds32_elf_blank (addr, len);
9039 if (!blank_t)
9040 return false;
9041 while ((*blank_p)->prev != NULL)
9042 *blank_p = (*blank_p)->prev;
9043 blank_t->next = *blank_p;
9044 (*blank_p)->prev = blank_t;
9045 (*blank_p) = blank_t;
9046 return true;
9049 if (addr < blank_t->offset + blank_t->size)
9051 /* Extend the origin blank. */
9052 if (addr + len > blank_t->offset + blank_t->size)
9053 blank_t->size = addr + len - blank_t->offset;
9055 else
9057 blank_t2 = create_nds32_elf_blank (addr, len);
9058 if (!blank_t2)
9059 return false;
9060 if (blank_t->next)
9062 blank_t->next->prev = blank_t2;
9063 blank_t2->next = blank_t->next;
9065 blank_t2->prev = blank_t;
9066 blank_t->next = blank_t2;
9067 *blank_p = blank_t2;
9070 return true;
9073 static bool
9074 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9075 bfd_vma len)
9077 nds32_elf_blank_t *blank_t;
9079 if (!insert_nds32_elf_blank (blank_p, addr, len))
9080 return false;
9082 blank_t = *blank_p;
9084 if (!blank_t->prev)
9086 blank_t->total_size = 0;
9087 blank_t = blank_t->next;
9090 while (blank_t)
9092 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9093 blank_t = blank_t->next;
9096 return true;
9099 static void
9100 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9102 nds32_elf_blank_t *blank_t;
9103 bfd_vma total_size = 0;
9105 if (!blank_p)
9106 return;
9108 blank_t = blank_p;
9109 while (blank_t->prev)
9110 blank_t = blank_t->prev;
9111 while (blank_t)
9113 blank_t->total_size = total_size;
9114 total_size += blank_t->size;
9115 blank_t = blank_t->next;
9119 static bool
9120 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9121 nds32_elf_blank_t *blank_p)
9123 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
9124 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
9125 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9126 unsigned int sec_shndx; /* The section the be relaxed. */
9127 bfd_byte *contents; /* Contents data of iterating section. */
9128 Elf_Internal_Rela *internal_relocs;
9129 Elf_Internal_Rela *irel;
9130 Elf_Internal_Rela *irelend;
9131 struct elf_link_hash_entry **sym_hashes;
9132 struct elf_link_hash_entry **end_hashes;
9133 unsigned int symcount;
9134 asection *sect;
9135 nds32_elf_blank_t *blank_t;
9136 nds32_elf_blank_t *blank_t2;
9137 nds32_elf_blank_t *blank_head;
9139 blank_head = blank_t = blank_p;
9140 while (blank_head->prev != NULL)
9141 blank_head = blank_head->prev;
9142 while (blank_t->next != NULL)
9143 blank_t = blank_t->next;
9145 if (blank_t->offset + blank_t->size <= sec->size)
9147 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9148 blank_t->next->prev = blank_t;
9150 if (blank_head->offset > 0)
9152 blank_head->prev = create_nds32_elf_blank (0, 0);
9153 blank_head->prev->next = blank_head;
9154 blank_head = blank_head->prev;
9157 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9159 /* The deletion must stop at the next ALIGN reloc for an alignment
9160 power larger than the number of bytes we are deleting. */
9162 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9163 if (!nds32_get_local_syms (abfd, sec, &isym))
9164 return false;
9166 if (isym == NULL)
9168 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9169 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9170 symtab_hdr->contents = (bfd_byte *) isym;
9173 if (isym == NULL || symtab_hdr->sh_info == 0)
9174 return false;
9176 blank_t = blank_head;
9177 calc_nds32_blank_total (blank_head);
9179 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9181 /* Adjust all the relocs. */
9183 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9184 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9185 true /* keep_memory */);
9186 irelend = internal_relocs + sect->reloc_count;
9188 blank_t = blank_head;
9189 blank_t2 = blank_head;
9191 if (!(sect->flags & SEC_RELOC))
9192 continue;
9194 contents = NULL;
9195 nds32_get_section_contents (abfd, sect, &contents, true);
9197 for (irel = internal_relocs; irel < irelend; irel++)
9199 bfd_vma raddr;
9201 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9202 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9203 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9205 unsigned long val = 0;
9206 unsigned long mask;
9207 long before, between;
9208 long offset = 0;
9210 switch (ELF32_R_TYPE (irel->r_info))
9212 case R_NDS32_DIFF8:
9213 offset = bfd_get_8 (abfd, contents + irel->r_offset);
9214 break;
9215 case R_NDS32_DIFF16:
9216 offset = bfd_get_16 (abfd, contents + irel->r_offset);
9217 break;
9218 case R_NDS32_DIFF32:
9219 val = bfd_get_32 (abfd, contents + irel->r_offset);
9220 /* Get the signed bit and mask for the high part. The
9221 gcc will alarm when right shift 32-bit since the
9222 type size of long may be 32-bit. */
9223 mask = 0 - (val >> 31);
9224 if (mask)
9225 offset = (val | (mask - 0xffffffff));
9226 else
9227 offset = val;
9228 break;
9229 default:
9230 BFD_ASSERT (0);
9233 /* DIFF value
9234 0 |encoded in location|
9235 |------------|-------------------|---------
9236 sym+off(addend)
9237 -- before ---| *****************
9238 --------------------- between ---|
9240 We only care how much data are relax between DIFF,
9241 marked as ***. */
9243 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9244 between = get_nds32_elf_blank_total (&blank_t,
9245 irel->r_addend + offset, 0);
9246 if (between == before)
9247 goto done_adjust_diff;
9249 switch (ELF32_R_TYPE (irel->r_info))
9251 case R_NDS32_DIFF8:
9252 bfd_put_8 (abfd, offset - (between - before),
9253 contents + irel->r_offset);
9254 break;
9255 case R_NDS32_DIFF16:
9256 bfd_put_16 (abfd, offset - (between - before),
9257 contents + irel->r_offset);
9258 break;
9259 case R_NDS32_DIFF32:
9260 bfd_put_32 (abfd, offset - (between - before),
9261 contents + irel->r_offset);
9262 break;
9265 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9266 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9268 bfd_vma val = 0;
9269 unsigned int len = 0;
9270 unsigned long before, between;
9271 bfd_byte *endp, *p;
9273 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9274 &len);
9276 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9277 between = get_nds32_elf_blank_total (&blank_t,
9278 irel->r_addend + val, 0);
9279 if (between == before)
9280 goto done_adjust_diff;
9282 p = contents + irel->r_offset;
9283 endp = p + len -1;
9284 memset (p, 0x80, len);
9285 *(endp) = 0;
9286 p = write_uleb128 (p, val - (between - before)) - 1;
9287 if (p < endp)
9288 *p |= 0x80;
9290 done_adjust_diff:
9292 if (sec == sect)
9294 raddr = irel->r_offset;
9295 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9296 irel->r_offset, 1);
9298 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9299 continue;
9300 if (blank_t2 && blank_t2->next
9301 && (blank_t2->offset > raddr
9302 || blank_t2->next->offset <= raddr))
9303 _bfd_error_handler
9304 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9305 abfd);
9307 /* Mark reloc in deleted portion as NONE.
9308 For some relocs like R_NDS32_LABEL that doesn't modify the
9309 content in the section. R_NDS32_LABEL doesn't belong to the
9310 instruction in the section, so we should preserve it. */
9311 if (raddr >= blank_t2->offset
9312 && raddr < blank_t2->offset + blank_t2->size
9313 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9314 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9315 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9316 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9317 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9318 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9320 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9321 R_NDS32_NONE);
9322 continue;
9326 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9327 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9328 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9329 continue;
9331 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9332 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9333 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9335 if (irel->r_addend <= sec->size)
9336 irel->r_addend -=
9337 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9342 /* Adjust the local symbols defined in this section. */
9343 blank_t = blank_head;
9344 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9346 if (isym->st_shndx == sec_shndx)
9348 if (isym->st_value <= sec->size)
9350 bfd_vma ahead;
9351 bfd_vma orig_addr = isym->st_value;
9353 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9354 isym->st_value -= ahead;
9356 /* Adjust function size. */
9357 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9358 && isym->st_size > 0)
9359 isym->st_size -=
9360 get_nds32_elf_blank_total
9361 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9366 /* Now adjust the global symbols defined in this section. */
9367 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9368 - symtab_hdr->sh_info);
9369 sym_hashes = elf_sym_hashes (abfd);
9370 end_hashes = sym_hashes + symcount;
9371 blank_t = blank_head;
9372 for (; sym_hashes < end_hashes; sym_hashes++)
9374 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9376 if ((sym_hash->root.type == bfd_link_hash_defined
9377 || sym_hash->root.type == bfd_link_hash_defweak)
9378 && sym_hash->root.u.def.section == sec)
9380 if (sym_hash->root.u.def.value <= sec->size)
9382 bfd_vma ahead;
9383 bfd_vma orig_addr = sym_hash->root.u.def.value;
9385 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9386 sym_hash->root.u.def.value -= ahead;
9388 /* Adjust function size. */
9389 if (sym_hash->type == STT_FUNC)
9390 sym_hash->size -=
9391 get_nds32_elf_blank_total
9392 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9398 contents = elf_section_data (sec)->this_hdr.contents;
9399 blank_t = blank_head;
9400 while (blank_t->next)
9402 /* Actually delete the bytes. */
9404 /* If current blank is the last blank overlap with current section,
9405 go to finish process. */
9406 if (sec->size <= (blank_t->next->offset))
9407 break;
9409 memmove (contents + blank_t->offset - blank_t->total_size,
9410 contents + blank_t->offset + blank_t->size,
9411 blank_t->next->offset - (blank_t->offset + blank_t->size));
9413 blank_t = blank_t->next;
9416 if (sec->size > (blank_t->offset + blank_t->size))
9418 /* There are remaining code between blank and section boundary.
9419 Move the remaining code to appropriate location. */
9420 memmove (contents + blank_t->offset - blank_t->total_size,
9421 contents + blank_t->offset + blank_t->size,
9422 sec->size - (blank_t->offset + blank_t->size));
9423 sec->size -= blank_t->total_size + blank_t->size;
9425 else
9426 /* This blank is not entirely included in the section,
9427 reduce the section size by only part of the blank size. */
9428 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9430 while (blank_head)
9432 blank_t = blank_head;
9433 blank_head = blank_head->next;
9434 remove_nds32_elf_blank (blank_t);
9437 return true;
9440 /* Get the contents of a section. */
9442 static int
9443 nds32_get_section_contents (bfd *abfd, asection *sec,
9444 bfd_byte **contents_p, bool cache)
9446 /* Get the section contents. */
9447 if (elf_section_data (sec)->this_hdr.contents != NULL)
9448 *contents_p = elf_section_data (sec)->this_hdr.contents;
9449 else
9451 if (!bfd_get_full_section_contents (abfd, sec, contents_p))
9452 return false;
9453 if (cache)
9454 elf_section_data (sec)->this_hdr.contents = *contents_p;
9457 return true;
9460 /* Get the contents of the internal symbol of abfd. */
9462 static int
9463 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9464 Elf_Internal_Sym **isymbuf_p)
9466 Elf_Internal_Shdr *symtab_hdr;
9467 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9469 /* Read this BFD's local symbols if we haven't done so already. */
9470 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9472 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9473 if (*isymbuf_p == NULL)
9475 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9476 symtab_hdr->sh_info, 0,
9477 NULL, NULL, NULL);
9478 if (*isymbuf_p == NULL)
9479 return false;
9482 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9484 return true;
9487 /* Range of small data. */
9488 static bfd_vma sdata_range[2][2];
9489 static bfd_vma const sdata_init_range[2] =
9490 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
9492 static int
9493 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9494 bfd_byte *contents, bfd_vma addr)
9496 unsigned long insn = bfd_getb32 (contents + addr);
9498 if (insn & 0x80000000)
9499 return 2;
9501 return 4;
9504 /* Set the gp relax range. We have to measure the safe range
9505 to do gp relaxation. */
9507 static void
9508 relax_range_measurement (bfd *abfd, struct bfd_link_info *link_info)
9510 asection *sec_f, *sec_b;
9511 /* For upper bound. */
9512 bfd_vma maxpgsz;
9513 bfd_vma align;
9514 static int decide_relax_range = 0;
9515 int i;
9516 int range_number = ARRAY_SIZE (sdata_init_range);
9518 if (decide_relax_range)
9519 return;
9520 decide_relax_range = 1;
9522 if (sda_rela_sec == NULL)
9524 /* Since there is no data sections, we assume the range is page size. */
9525 for (i = 0; i < range_number; i++)
9527 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9528 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9530 return;
9533 /* Get the biggest alignment power after the gp located section. */
9534 sec_f = sda_rela_sec->output_section;
9535 sec_b = sec_f->next;
9536 align = 0;
9537 while (sec_b != NULL)
9539 if ((unsigned)(1 << sec_b->alignment_power) > align)
9540 align = (1 << sec_b->alignment_power);
9541 sec_b = sec_b->next;
9544 if (link_info != NULL)
9545 maxpgsz = link_info->maxpagesize;
9546 else
9547 maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9548 /* I guess we can not determine the section before
9549 gp located section, so we assume the align is max page size. */
9550 for (i = 0; i < range_number; i++)
9552 sdata_range[i][1] = sdata_init_range[i] - align;
9553 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9554 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9555 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9559 /* These are macros used to check flags encoded in r_addend.
9560 They are only used by nds32_elf_relax_section (). */
9561 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9562 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9563 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9564 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9566 static const char * unrecognized_reloc_msg =
9567 /* xgettext:c-format */
9568 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9570 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9572 static bool
9573 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9574 Elf_Internal_Rela *internal_relocs, int *insn_len,
9575 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9576 Elf_Internal_Shdr *symtab_hdr)
9578 /* There are 3 variations for LONGCALL1
9579 case 4-4-2; 16-bit on, optimize off or optimize for space
9580 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9581 ori ta, ta, lo12(symbol) ; LO12S0
9582 jral5 ta ;
9584 case 4-4-4; 16-bit off, optimize don't care
9585 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9586 ori ta, ta, lo12(symbol) ; LO12S0
9587 jral ta ;
9589 case 4-4-4; 16-bit on, optimize for speed
9590 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9591 ori ta, ta, lo12(symbol) ; LO12S0
9592 jral ta ;
9593 Check code for -mlong-calls output. */
9595 /* Get the reloc for the address from which the register is
9596 being loaded. This reloc will tell us which function is
9597 actually being called. */
9599 bfd_vma laddr;
9600 int seq_len; /* Original length of instruction sequence. */
9601 uint32_t insn;
9602 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9603 bfd_signed_vma foff;
9604 uint16_t insn16;
9606 irelend = internal_relocs + sec->reloc_count;
9607 seq_len = GET_SEQ_LEN (irel->r_addend);
9608 laddr = irel->r_offset;
9609 *insn_len = seq_len;
9611 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9612 R_NDS32_HI20_RELA, laddr);
9613 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9614 R_NDS32_LO12S0_ORI_RELA,
9615 laddr + 4);
9617 if (hi_irelfn == irelend || lo_irelfn == irelend)
9619 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9620 (uint64_t) irel->r_offset);
9621 return false;
9624 /* Get the value of the symbol referred to by the reloc. */
9625 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9627 /* This condition only happened when symbol is undefined. */
9628 if (foff == 0
9629 || foff < -CONSERVATIVE_24BIT_S1
9630 || foff >= CONSERVATIVE_24BIT_S1)
9631 return false;
9633 /* Relax to: jal symbol; 25_PCREL. */
9634 /* For simplicity of coding, we are going to modify the section
9635 contents, the section relocs, and the BFD symbol table. We
9636 must tell the rest of the code not to free up this
9637 information. It would be possible to instead create a table
9638 of changes which have to be made, as is done in coff-mips.c;
9639 that would be more work, but would require less memory when
9640 the linker is run. */
9642 /* Replace the long call with a jal. */
9643 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9644 R_NDS32_25_PCREL_RELA);
9645 irel->r_addend = hi_irelfn->r_addend;
9647 /* We don't resolve this here but resolve it in relocate_section. */
9648 insn = INSN_JAL;
9649 bfd_putb32 (insn, contents + irel->r_offset);
9651 hi_irelfn->r_info =
9652 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9653 lo_irelfn->r_info =
9654 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9655 *insn_len = 4;
9657 if (seq_len & 0x2)
9659 insn16 = NDS32_NOP16;
9660 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9661 lo_irelfn->r_info =
9662 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9663 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9664 *insn_len += 2;
9666 return true;
9669 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9670 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
9672 static bool
9673 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9674 Elf_Internal_Rela *internal_relocs, int *insn_len,
9675 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9676 Elf_Internal_Shdr *symtab_hdr)
9678 /* bltz rt, .L1 ; LONGCALL2
9679 jal symbol ; 25_PCREL
9680 .L1: */
9682 /* Get the reloc for the address from which the register is
9683 being loaded. This reloc will tell us which function is
9684 actually being called. */
9686 bfd_vma laddr;
9687 uint32_t insn;
9688 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9689 bfd_signed_vma foff;
9691 irelend = internal_relocs + sec->reloc_count;
9692 laddr = irel->r_offset;
9693 i1_irelfn =
9694 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9695 R_NDS32_25_PCREL_RELA, laddr + 4);
9697 if (i1_irelfn == irelend)
9699 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9700 (uint64_t) irel->r_offset);
9701 return false;
9704 insn = bfd_getb32 (contents + laddr);
9706 /* Get the value of the symbol referred to by the reloc. */
9707 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9709 if (foff == 0
9710 || foff < -CONSERVATIVE_16BIT_S1
9711 || foff >= CONSERVATIVE_16BIT_S1)
9712 return false;
9714 /* Relax to bgezal rt, label ; 17_PCREL
9715 or bltzal rt, label ; 17_PCREL */
9717 /* Convert to complimentary conditional call. */
9718 insn = CONVERT_CONDITION_CALL (insn);
9720 /* For simplicity of coding, we are going to modify the section
9721 contents, the section relocs, and the BFD symbol table. We
9722 must tell the rest of the code not to free up this
9723 information. It would be possible to instead create a table
9724 of changes which have to be made, as is done in coff-mips.c;
9725 that would be more work, but would require less memory when
9726 the linker is run. */
9728 /* Clean unnessary relocations. */
9729 i1_irelfn->r_info =
9730 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9731 cond_irelfn =
9732 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9733 R_NDS32_17_PCREL_RELA, laddr);
9734 if (cond_irelfn != irelend)
9735 cond_irelfn->r_info =
9736 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9738 /* Replace the long call with a bgezal. */
9739 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9740 R_NDS32_17_PCREL_RELA);
9741 irel->r_addend = i1_irelfn->r_addend;
9743 bfd_putb32 (insn, contents + irel->r_offset);
9745 *insn_len = 4;
9746 return true;
9749 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9751 static bool
9752 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9753 Elf_Internal_Rela *internal_relocs, int *insn_len,
9754 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9755 Elf_Internal_Shdr *symtab_hdr)
9757 /* There are 3 variations for LONGCALL3
9758 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9759 bltz rt, $1 ; LONGCALL3
9760 sethi ta, hi20(symbol) ; HI20
9761 ori ta, ta, lo12(symbol) ; LO12S0
9762 jral5 ta ;
9765 case 4-4-4-4; 16-bit off, optimize don't care
9766 bltz rt, $1 ; LONGCALL3
9767 sethi ta, hi20(symbol) ; HI20
9768 ori ta, ta, lo12(symbol) ; LO12S0
9769 jral ta ;
9772 case 4-4-4-4; 16-bit on, optimize for speed
9773 bltz rt, $1 ; LONGCALL3
9774 sethi ta, hi20(symbol) ; HI20
9775 ori ta, ta, lo12(symbol) ; LO12S0
9776 jral ta ;
9777 $1 */
9779 /* Get the reloc for the address from which the register is
9780 being loaded. This reloc will tell us which function is
9781 actually being called. */
9783 bfd_vma laddr;
9784 int seq_len; /* Original length of instruction sequence. */
9785 uint32_t insn;
9786 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9787 bfd_signed_vma foff;
9788 uint16_t insn16;
9790 irelend = internal_relocs + sec->reloc_count;
9791 seq_len = GET_SEQ_LEN (irel->r_addend);
9792 laddr = irel->r_offset;
9793 *insn_len = seq_len;
9795 hi_irelfn =
9796 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9797 R_NDS32_HI20_RELA, laddr + 4);
9798 lo_irelfn =
9799 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9800 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9802 if (hi_irelfn == irelend || lo_irelfn == irelend)
9804 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9805 (uint64_t) irel->r_offset);
9806 return false;
9809 /* Get the value of the symbol referred to by the reloc. */
9810 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9812 if (foff == 0
9813 || foff < -CONSERVATIVE_24BIT_S1
9814 || foff >= CONSERVATIVE_24BIT_S1)
9815 return false;
9817 insn = bfd_getb32 (contents + laddr);
9818 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9820 /* Relax to bgezal rt, label ; 17_PCREL
9821 or bltzal rt, label ; 17_PCREL */
9823 /* Convert to complimentary conditional call. */
9824 insn = CONVERT_CONDITION_CALL (insn);
9825 bfd_putb32 (insn, contents + irel->r_offset);
9827 *insn_len = 4;
9828 irel->r_info =
9829 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9830 hi_irelfn->r_info =
9831 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9832 lo_irelfn->r_info =
9833 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9835 cond_irelfn =
9836 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9837 R_NDS32_17_PCREL_RELA, laddr);
9838 if (cond_irelfn != irelend)
9840 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9841 R_NDS32_17_PCREL_RELA);
9842 cond_irelfn->r_addend = hi_irelfn->r_addend;
9845 if (seq_len & 0x2)
9847 insn16 = NDS32_NOP16;
9848 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9849 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9850 R_NDS32_INSN16);
9851 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9852 insn_len += 2;
9855 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9857 /* Relax to the following instruction sequence
9858 bltz rt, $1 ; LONGCALL2
9859 jal symbol ; 25_PCREL
9860 $1 */
9861 *insn_len = 8;
9862 insn = INSN_JAL;
9863 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9865 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9866 R_NDS32_25_PCREL_RELA);
9867 irel->r_info =
9868 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9870 lo_irelfn->r_info =
9871 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9873 if (seq_len & 0x2)
9875 insn16 = NDS32_NOP16;
9876 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9877 lo_irelfn->r_info =
9878 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9879 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9880 insn_len += 2;
9883 return true;
9886 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
9888 static bool
9889 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9890 Elf_Internal_Rela *internal_relocs, int *insn_len,
9891 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9892 Elf_Internal_Shdr *symtab_hdr)
9894 /* There are 3 variations for LONGJUMP1
9895 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9896 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9897 ori ta, ta, lo12(symbol) ; LO12S0
9898 jr5 ta ;
9900 case 4-4-4; 16-bit off, optimize don't care
9901 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9902 ori ta, ta, lo12(symbol) ; LO12S0
9903 jr ta ;
9905 case 4-4-4; 16-bit on, optimize for speed
9906 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9907 ori ta, ta, lo12(symbol) ; LO12S0
9908 jr ta ; */
9910 /* Get the reloc for the address from which the register is
9911 being loaded. This reloc will tell us which function is
9912 actually being called. */
9914 bfd_vma laddr;
9915 int seq_len; /* Original length of instruction sequence. */
9916 int insn16_on; /* 16-bit on/off. */
9917 uint32_t insn;
9918 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9919 bfd_signed_vma foff;
9920 uint16_t insn16;
9921 unsigned long reloc;
9923 irelend = internal_relocs + sec->reloc_count;
9924 seq_len = GET_SEQ_LEN (irel->r_addend);
9925 laddr = irel->r_offset;
9926 *insn_len = seq_len;
9927 insn16_on = IS_16BIT_ON (irel->r_addend);
9929 hi_irelfn =
9930 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9931 R_NDS32_HI20_RELA, laddr);
9932 lo_irelfn =
9933 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9934 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9935 if (hi_irelfn == irelend || lo_irelfn == irelend)
9937 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
9938 (uint64_t) irel->r_offset);
9939 return false;
9942 /* Get the value of the symbol referred to by the reloc. */
9943 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9945 if (foff == 0
9946 || foff >= CONSERVATIVE_24BIT_S1
9947 || foff < -CONSERVATIVE_24BIT_S1)
9948 return false;
9950 if (insn16_on
9951 && foff >= -ACCURATE_8BIT_S1
9952 && foff < ACCURATE_8BIT_S1
9953 && (seq_len & 0x2))
9955 /* j8 label */
9956 /* 16-bit on, but not optimized for speed. */
9957 reloc = R_NDS32_9_PCREL_RELA;
9958 insn16 = INSN_J8;
9959 bfd_putb16 (insn16, contents + irel->r_offset);
9960 *insn_len = 2;
9961 irel->r_info =
9962 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9964 else
9966 /* j label */
9967 reloc = R_NDS32_25_PCREL_RELA;
9968 insn = INSN_J;
9969 bfd_putb32 (insn, contents + irel->r_offset);
9970 *insn_len = 4;
9971 irel->r_info =
9972 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9973 irel->r_addend = 0;
9976 hi_irelfn->r_info =
9977 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9978 lo_irelfn->r_info =
9979 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9981 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
9983 insn16 = NDS32_NOP16;
9984 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9985 lo_irelfn->r_info =
9986 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9987 R_NDS32_INSN16);
9988 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9989 *insn_len += 2;
9991 return true;
9994 /* Revert condition branch. This function does not check if the input
9995 instruction is condition branch or not. */
9997 static void
9998 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
9999 uint16_t *re_insn16, uint32_t *re_insn)
10001 uint32_t comp_insn = 0;
10002 uint16_t comp_insn16 = 0;
10004 if (insn)
10006 if (N32_OP6 (insn) == N32_OP6_BR1)
10008 /* beqs label. */
10009 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10010 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10012 /* Insn can be contracted to 16-bit implied r5. */
10013 comp_insn16 =
10014 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10015 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10018 else if (N32_OP6 (insn) == N32_OP6_BR3)
10020 /* bnec $ta, imm11, label. */
10021 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10023 else
10025 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10026 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10027 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10029 if (N32_IS_RT3 (insn))
10031 /* Insn can be contracted to 16-bit. */
10032 comp_insn16 =
10033 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10034 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10036 else if (N32_RT5 (insn) == REG_R15)
10038 /* Insn can be contracted to 16-bit. */
10039 comp_insn16 =
10040 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10045 else
10047 switch ((insn16 & 0xf000) >> 12)
10049 case 0xc:
10050 /* beqz38 or bnez38 */
10051 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10052 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10053 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10054 break;
10056 case 0xd:
10057 /* beqs38 or bnes38 */
10058 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10059 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10060 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10061 | (REG_R5 << 15);
10062 break;
10064 case 0xe:
10065 /* beqzS8 or bnezS8 */
10066 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10067 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10068 comp_insn |= REG_R15 << 20;
10069 break;
10071 default:
10072 break;
10075 if (comp_insn && re_insn)
10076 *re_insn = comp_insn;
10077 if (comp_insn16 && re_insn16)
10078 *re_insn16 = comp_insn16;
10081 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
10083 static bool
10084 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10085 Elf_Internal_Rela *internal_relocs, int *insn_len,
10086 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10087 Elf_Internal_Shdr *symtab_hdr)
10089 /* There are 3 variations for LONGJUMP2
10090 case 2-4; 1st insn convertible, 16-bit on,
10091 optimize off or optimize for space
10092 bnes38 rt, ra, $1 ; LONGJUMP2
10093 j label ; 25_PCREL
10096 case 4-4; 1st insn not convertible
10097 bne rt, ra, $1 ; LONGJUMP2
10098 j label ; 25_PCREL
10101 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10102 bne rt, ra, $1 ; LONGJUMP2
10103 j label ; 25_PCREL
10104 $1: */
10106 /* Get the reloc for the address from which the register is
10107 being loaded. This reloc will tell us which function is
10108 actually being called. */
10110 bfd_vma laddr;
10111 int seq_len; /* Original length of instruction sequence. */
10112 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10113 int first_size;
10114 unsigned int i;
10115 bfd_signed_vma foff;
10116 uint32_t insn, re_insn = 0;
10117 uint16_t insn16, re_insn16 = 0;
10118 unsigned long reloc, cond_reloc;
10120 enum elf_nds32_reloc_type checked_types[] =
10121 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10123 irelend = internal_relocs + sec->reloc_count;
10124 seq_len = GET_SEQ_LEN (irel->r_addend);
10125 laddr = irel->r_offset;
10126 *insn_len = seq_len;
10127 first_size = (seq_len == 6) ? 2 : 4;
10129 i2_irelfn =
10130 find_relocs_at_address_addr (irel, internal_relocs,
10131 irelend, R_NDS32_25_PCREL_RELA,
10132 laddr + first_size);
10134 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10136 cond_irelfn =
10137 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10138 checked_types[i], laddr);
10139 if (cond_irelfn != irelend)
10140 break;
10143 if (i2_irelfn == irelend || cond_irelfn == irelend)
10145 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10146 (uint64_t) irel->r_offset);
10147 return false;
10150 /* Get the value of the symbol referred to by the reloc. */
10151 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10152 if (foff == 0
10153 || foff < -CONSERVATIVE_16BIT_S1
10154 || foff >= CONSERVATIVE_16BIT_S1)
10155 return false;
10157 /* Get the all corresponding instructions. */
10158 if (first_size == 4)
10160 insn = bfd_getb32 (contents + laddr);
10161 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10163 else
10165 insn16 = bfd_getb16 (contents + laddr);
10166 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10169 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10170 && foff < ACCURATE_8BIT_S1 - first_size)
10172 if (first_size == 4)
10174 /* Don't convert it to 16-bit now, keep this as relaxable for
10175 ``label reloc; INSN16''. */
10177 /* Save comp_insn32 to buffer. */
10178 bfd_putb32 (re_insn, contents + irel->r_offset);
10179 *insn_len = 4;
10180 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10181 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10182 cond_reloc = R_NDS32_INSN16;
10184 else
10186 bfd_putb16 (re_insn16, contents + irel->r_offset);
10187 *insn_len = 2;
10188 reloc = R_NDS32_9_PCREL_RELA;
10189 cond_reloc = R_NDS32_NONE;
10192 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10193 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10194 && foff < ACCURATE_14BIT_S1 - first_size))
10196 /* beqs label ; 15_PCREL */
10197 bfd_putb32 (re_insn, contents + irel->r_offset);
10198 *insn_len = 4;
10199 reloc = R_NDS32_15_PCREL_RELA;
10200 cond_reloc = R_NDS32_NONE;
10202 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10203 && foff >= -CONSERVATIVE_16BIT_S1
10204 && foff < CONSERVATIVE_16BIT_S1)
10206 /* beqz label ; 17_PCREL */
10207 bfd_putb32 (re_insn, contents + irel->r_offset);
10208 *insn_len = 4;
10209 reloc = R_NDS32_17_PCREL_RELA;
10210 cond_reloc = R_NDS32_NONE;
10212 else
10213 return false;
10215 /* Set all relocations. */
10216 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10217 irel->r_addend = i2_irelfn->r_addend;
10219 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10220 cond_reloc);
10221 cond_irelfn->r_addend = 0;
10223 if ((seq_len ^ *insn_len ) & 0x2)
10225 insn16 = NDS32_NOP16;
10226 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10227 i2_irelfn->r_offset = 4;
10228 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10229 R_NDS32_INSN16);
10230 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10231 *insn_len += 2;
10233 else
10234 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10235 R_NDS32_NONE);
10236 return true;
10239 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
10241 static bool
10242 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10243 Elf_Internal_Rela *internal_relocs, int *insn_len,
10244 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10245 Elf_Internal_Shdr *symtab_hdr)
10247 /* There are 5 variations for LONGJUMP3
10248 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10249 optimize off or optimize for space
10250 bnes38 rt, ra, $1 ; LONGJUMP3
10251 sethi ta, hi20(symbol) ; HI20
10252 ori ta, ta, lo12(symbol) ; LO12S0
10253 jr5 ta ;
10254 $1: ;
10256 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10257 bnes38 rt, ra, $1 ; LONGJUMP3
10258 sethi ta, hi20(symbol) ; HI20
10259 ori ta, ta, lo12(symbol) ; LO12S0
10260 jr5 ta ;
10261 $1: ; LABEL
10263 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10264 optimize off or optimize for space
10265 bne rt, ra, $1 ; LONGJUMP3
10266 sethi ta, hi20(symbol) ; HI20
10267 ori ta, ta, lo12(symbol) ; LO12S0
10268 jr5 ta ;
10269 $1: ;
10271 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10272 16-bit off if no INSN16
10273 bne rt, ra, $1 ; LONGJUMP3
10274 sethi ta, hi20(symbol) ; HI20
10275 ori ta, ta, lo12(symbol) ; LO12S0
10276 jr ta ;
10277 $1: ;
10279 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10280 16-bit off if no INSN16
10281 bne rt, ra, $1 ; LONGJUMP3
10282 sethi ta, hi20(symbol) ; HI20
10283 ori ta, ta, lo12(symbol) ; LO12S0
10284 jr ta ;
10285 $1: ; LABEL */
10287 /* Get the reloc for the address from which the register is
10288 being loaded. This reloc will tell us which function is
10289 actually being called. */
10290 enum elf_nds32_reloc_type checked_types[] =
10291 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10293 int reloc_off = 0, cond_removed = 0, convertible;
10294 bfd_vma laddr;
10295 int seq_len; /* Original length of instruction sequence. */
10296 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10297 int first_size;
10298 unsigned int i;
10299 bfd_signed_vma foff;
10300 uint32_t insn, re_insn = 0;
10301 uint16_t insn16, re_insn16 = 0;
10302 unsigned long reloc, cond_reloc;
10304 irelend = internal_relocs + sec->reloc_count;
10305 seq_len = GET_SEQ_LEN (irel->r_addend);
10306 laddr = irel->r_offset;
10307 *insn_len = seq_len;
10309 convertible = IS_1ST_CONVERT (irel->r_addend);
10311 if (convertible)
10312 first_size = 2;
10313 else
10314 first_size = 4;
10316 /* Get all needed relocations. */
10317 hi_irelfn =
10318 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10319 R_NDS32_HI20_RELA, laddr + first_size);
10320 lo_irelfn =
10321 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10322 R_NDS32_LO12S0_ORI_RELA,
10323 laddr + first_size + 4);
10325 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10327 cond_irelfn =
10328 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10329 checked_types[i], laddr);
10330 if (cond_irelfn != irelend)
10331 break;
10334 if (hi_irelfn == irelend
10335 || lo_irelfn == irelend
10336 || cond_irelfn == irelend)
10338 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10339 (uint64_t) irel->r_offset);
10340 return false;
10343 /* Get the value of the symbol referred to by the reloc. */
10344 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10346 if (foff == 0
10347 || foff < -CONSERVATIVE_24BIT_S1
10348 || foff >= CONSERVATIVE_24BIT_S1)
10349 return false;
10351 /* Get the all corresponding instructions. */
10352 if (first_size == 4)
10354 insn = bfd_getb32 (contents + laddr);
10355 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10357 else
10359 insn16 = bfd_getb16 (contents + laddr);
10360 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10363 /* For simplicity of coding, we are going to modify the section
10364 contents, the section relocs, and the BFD symbol table. We
10365 must tell the rest of the code not to free up this
10366 information. It would be possible to instead create a table
10367 of changes which have to be made, as is done in coff-mips.c;
10368 that would be more work, but would require less memory when
10369 the linker is run. */
10371 if (re_insn16
10372 && foff >= -ACCURATE_8BIT_S1 - first_size
10373 && foff < ACCURATE_8BIT_S1 - first_size)
10375 if (!(seq_len & 0x2))
10377 /* Don't convert it to 16-bit now, keep this as relaxable
10378 for ``label reloc; INSN1a''6. */
10379 /* Save comp_insn32 to buffer. */
10380 bfd_putb32 (re_insn, contents + irel->r_offset);
10381 *insn_len = 4;
10382 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10383 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10384 cond_reloc = R_NDS32_INSN16;
10386 else
10388 /* Not optimize for speed; convert sequence to 16-bit. */
10389 /* Save comp_insn16 to buffer. */
10390 bfd_putb16 (re_insn16, contents + irel->r_offset);
10391 *insn_len = 2;
10392 reloc = R_NDS32_9_PCREL_RELA;
10393 cond_reloc = R_NDS32_NONE;
10395 cond_removed = 1;
10397 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10398 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10399 && foff < ACCURATE_14BIT_S1 - first_size))
10401 /* beqs label ; 15_PCREL */
10402 bfd_putb32 (re_insn, contents + irel->r_offset);
10403 *insn_len = 4;
10404 reloc = R_NDS32_15_PCREL_RELA;
10405 cond_reloc = R_NDS32_NONE;
10406 cond_removed = 1;
10408 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10409 && foff >= -CONSERVATIVE_16BIT_S1
10410 && foff < CONSERVATIVE_16BIT_S1)
10412 /* beqz label ; 17_PCREL */
10413 bfd_putb32 (re_insn, contents + irel->r_offset);
10414 *insn_len = 4;
10415 reloc = R_NDS32_17_PCREL_RELA;
10416 cond_reloc = R_NDS32_NONE;
10417 cond_removed = 1;
10419 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10420 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10422 /* Relax to one of the following 3 variations
10424 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10425 for space
10426 bnes38 rt, $1 ; LONGJUMP2
10427 j label ; 25_PCREL
10430 case 4-4; 1st insn not convertible, others don't care
10431 bne rt, ra, $1 ; LONGJUMP2
10432 j label ; 25_PCREL
10435 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10436 bne rt, ra, $1 ; LONGJUMP2
10437 j label ; 25_PCREL
10438 $1 */
10440 /* Offset for first instruction. */
10442 /* Use j label as second instruction. */
10443 *insn_len = 4 + first_size;
10444 insn = INSN_J;
10445 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10446 reloc = R_NDS32_LONGJUMP2;
10447 cond_reloc = R_NDS32_25_PLTREL;
10449 else
10450 return false;
10452 if (cond_removed == 1)
10454 /* Set all relocations. */
10455 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10456 irel->r_addend = hi_irelfn->r_addend;
10458 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10459 cond_reloc);
10460 cond_irelfn->r_addend = 0;
10461 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10462 R_NDS32_NONE);
10464 else
10466 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10467 irel->r_addend = irel->r_addend;
10468 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10469 cond_reloc);
10472 if ((seq_len ^ *insn_len ) & 0x2)
10474 insn16 = NDS32_NOP16;
10475 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10476 lo_irelfn->r_offset = *insn_len;
10477 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10478 R_NDS32_INSN16);
10479 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10480 *insn_len += 2;
10482 else
10483 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10484 R_NDS32_NONE);
10485 return true;
10488 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
10490 static bool
10491 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10492 Elf_Internal_Rela *internal_relocs, int *insn_len,
10493 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10494 Elf_Internal_Shdr *symtab_hdr)
10496 /* The pattern for LONGCALL4. Support for function cse.
10497 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10498 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10499 jral ta ; PTR_RES/EMPTY/INSN16 */
10501 bfd_vma laddr;
10502 uint32_t insn;
10503 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10504 Elf_Internal_Rela *irelend;
10505 bfd_signed_vma foff;
10507 irelend = internal_relocs + sec->reloc_count;
10508 laddr = irel->r_offset;
10510 /* Get the reloc for the address from which the register is
10511 being loaded. This reloc will tell us which function is
10512 actually being called. */
10513 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10514 R_NDS32_HI20_RELA, laddr);
10516 if (hi_irel == irelend)
10518 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10519 (uint64_t) irel->r_offset);
10520 return false;
10523 /* Get the value of the symbol referred to by the reloc. */
10524 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10526 /* This condition only happened when symbol is undefined. */
10527 if (foff == 0
10528 || foff < -CONSERVATIVE_24BIT_S1
10529 || foff >= CONSERVATIVE_24BIT_S1)
10530 return false;
10532 /* Relax to: jal symbol; 25_PCREL. */
10533 /* For simplicity of coding, we are going to modify the section
10534 contents, the section relocs, and the BFD symbol table. We
10535 must tell the rest of the code not to free up this
10536 information. It would be possible to instead create a table
10537 of changes which have to be made, as is done in coff-mips.c;
10538 that would be more work, but would require less memory when
10539 the linker is run. */
10541 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10542 R_NDS32_PTR_RESOLVED, irel->r_addend);
10543 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10544 R_NDS32_EMPTY, irel->r_addend);
10546 if (ptr_irel == irelend || em_irel == irelend)
10548 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10549 (uint64_t) irel->r_offset);
10550 return false;
10552 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10553 insn = bfd_getb32 (contents + irel->r_addend);
10554 if (insn & 0x80000000)
10555 return false;
10557 /* Replace the long call with a jal. */
10558 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10559 R_NDS32_25_PCREL_RELA);
10560 ptr_irel->r_addend = 1;
10562 /* We don't resolve this here but resolve it in relocate_section. */
10563 insn = INSN_JAL;
10564 bfd_putb32 (insn, contents + em_irel->r_offset);
10566 irel->r_info =
10567 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10569 /* If there is function cse, HI20 can not remove now. */
10570 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10571 R_NDS32_LONGCALL4, laddr);
10572 if (call_irel == irelend)
10574 *insn_len = 0;
10575 hi_irel->r_info =
10576 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10579 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10580 R_NDS32_INSN16, irel->r_addend);
10581 if (insn_irel != irelend)
10582 insn_irel->r_info =
10583 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10585 return true;
10588 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
10590 static bool
10591 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10592 Elf_Internal_Rela *internal_relocs, int *insn_len,
10593 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10594 Elf_Internal_Shdr *symtab_hdr)
10596 /* The pattern for LONGCALL5.
10597 bltz rt, .L1 ; LONGCALL5/17_PCREL
10598 jal symbol ; 25_PCREL
10599 .L1: */
10601 bfd_vma laddr;
10602 uint32_t insn;
10603 Elf_Internal_Rela *cond_irel, *irelend;
10604 bfd_signed_vma foff;
10606 irelend = internal_relocs + sec->reloc_count;
10607 laddr = irel->r_offset;
10608 insn = bfd_getb32 (contents + laddr);
10610 /* Get the reloc for the address from which the register is
10611 being loaded. This reloc will tell us which function is
10612 actually being called. */
10613 cond_irel =
10614 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10615 R_NDS32_25_PCREL_RELA, irel->r_addend);
10616 if (cond_irel == irelend)
10618 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10619 (uint64_t) irel->r_offset);
10620 return false;
10623 /* Get the value of the symbol referred to by the reloc. */
10624 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10626 if (foff == 0
10627 || foff < -CONSERVATIVE_16BIT_S1
10628 || foff >= CONSERVATIVE_16BIT_S1)
10629 return false;
10631 /* Relax to bgezal rt, label ; 17_PCREL
10632 or bltzal rt, label ; 17_PCREL. */
10634 /* Convert to complimentary conditional call. */
10635 insn = CONVERT_CONDITION_CALL (insn);
10637 /* For simplicity of coding, we are going to modify the section
10638 contents, the section relocs, and the BFD symbol table. We
10639 must tell the rest of the code not to free up this
10640 information. It would be possible to instead create a table
10641 of changes which have to be made, as is done in coff-mips.c;
10642 that would be more work, but would require less memory when
10643 the linker is run. */
10645 /* Modify relocation and contents. */
10646 cond_irel->r_info =
10647 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10649 /* Replace the long call with a bgezal. */
10650 bfd_putb32 (insn, contents + cond_irel->r_offset);
10651 *insn_len = 0;
10653 /* Clean unnessary relocations. */
10654 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10656 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10657 R_NDS32_17_PCREL_RELA, laddr);
10658 cond_irel->r_info =
10659 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10661 return true;
10664 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
10666 static bool
10667 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10668 Elf_Internal_Rela *internal_relocs, int *insn_len,
10669 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10670 Elf_Internal_Shdr *symtab_hdr)
10672 /* The pattern for LONGCALL6.
10673 bltz rt, .L1 ; LONGCALL6/17_PCREL
10674 sethi ta, hi20(symbol) ; HI20/PTR
10675 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10676 jral ta ; PTR_RES/EMPTY/INSN16
10677 .L1 */
10679 bfd_vma laddr;
10680 uint32_t insn;
10681 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10682 bfd_signed_vma foff;
10684 irelend = internal_relocs + sec->reloc_count;
10685 laddr = irel->r_offset;
10687 /* Get the reloc for the address from which the register is
10688 being loaded. This reloc will tell us which function is
10689 actually being called. */
10690 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10691 R_NDS32_EMPTY, irel->r_addend);
10693 if (em_irel == irelend)
10695 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10696 (uint64_t) irel->r_offset);
10697 return false;
10700 /* Get the value of the symbol referred to by the reloc. */
10701 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10703 if (foff == 0
10704 || foff < -CONSERVATIVE_24BIT_S1
10705 || foff >= CONSERVATIVE_24BIT_S1)
10706 return false;
10708 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10709 insn = bfd_getb32 (contents + irel->r_addend);
10710 if (insn & 0x80000000)
10711 return false;
10713 insn = bfd_getb32 (contents + laddr);
10714 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10716 /* Relax to bgezal rt, label ; 17_PCREL
10717 or bltzal rt, label ; 17_PCREL. */
10719 /* Convert to complimentary conditional call. */
10720 *insn_len = 0;
10721 insn = CONVERT_CONDITION_CALL (insn);
10722 bfd_putb32 (insn, contents + em_irel->r_offset);
10724 em_irel->r_info =
10725 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10727 /* Set resolved relocation. */
10728 cond_irel =
10729 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10730 R_NDS32_PTR_RESOLVED, irel->r_addend);
10731 if (cond_irel == irelend)
10733 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10734 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10735 return false;
10737 cond_irel->r_addend = 1;
10739 /* Clear relocations. */
10741 irel->r_info =
10742 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10744 cond_irel =
10745 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10746 R_NDS32_17_PCREL_RELA, laddr);
10747 if (cond_irel != irelend)
10748 cond_irel->r_info =
10749 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10751 cond_irel =
10752 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10753 R_NDS32_INSN16, irel->r_addend);
10754 if (cond_irel != irelend)
10755 cond_irel->r_info =
10756 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10759 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10761 /* Relax to the following instruction sequence
10762 bltz rt, .L1 ; LONGCALL2/17_PCREL
10763 jal symbol ; 25_PCREL/PTR_RES
10764 .L1 */
10765 *insn_len = 4;
10766 /* Convert instruction. */
10767 insn = INSN_JAL;
10768 bfd_putb32 (insn, contents + em_irel->r_offset);
10770 /* Convert relocations. */
10771 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10772 R_NDS32_25_PCREL_RELA);
10773 irel->r_info =
10774 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10776 /* Set resolved relocation. */
10777 cond_irel =
10778 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10779 R_NDS32_PTR_RESOLVED, irel->r_addend);
10780 if (cond_irel == irelend)
10782 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10783 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10784 return false;
10786 cond_irel->r_addend = 1;
10788 cond_irel =
10789 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10790 R_NDS32_INSN16, irel->r_addend);
10791 if (cond_irel != irelend)
10792 cond_irel->r_info =
10793 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10795 return true;
10798 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10800 static bool
10801 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10802 Elf_Internal_Rela *internal_relocs, int *insn_len,
10803 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10804 Elf_Internal_Shdr *symtab_hdr)
10806 /* The pattern for LONGJUMP4.
10807 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10808 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10809 jr ta ; PTR_RES/INSN16/EMPTY */
10811 bfd_vma laddr;
10812 int seq_len; /* Original length of instruction sequence. */
10813 uint32_t insn;
10814 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10815 bfd_signed_vma foff;
10817 irelend = internal_relocs + sec->reloc_count;
10818 seq_len = GET_SEQ_LEN (irel->r_addend);
10819 laddr = irel->r_offset;
10820 *insn_len = seq_len;
10822 /* Get the reloc for the address from which the register is
10823 being loaded. This reloc will tell us which function is
10824 actually being called. */
10826 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10827 R_NDS32_HI20_RELA, laddr);
10829 if (hi_irel == irelend)
10831 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10832 (uint64_t) irel->r_offset);
10833 return false;
10836 /* Get the value of the symbol referred to by the reloc. */
10837 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10839 if (foff == 0
10840 || foff >= CONSERVATIVE_24BIT_S1
10841 || foff < -CONSERVATIVE_24BIT_S1)
10842 return false;
10844 /* Convert it to "j label", it may be converted to j8 in the final
10845 pass of relaxation. Therefore, we do not consider this currently. */
10846 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10847 R_NDS32_PTR_RESOLVED, irel->r_addend);
10848 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10849 R_NDS32_EMPTY, irel->r_addend);
10851 if (ptr_irel == irelend || em_irel == irelend)
10853 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10854 (uint64_t) irel->r_offset);
10855 return false;
10858 em_irel->r_info =
10859 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10860 ptr_irel->r_addend = 1;
10862 /* Write instruction. */
10863 insn = INSN_J;
10864 bfd_putb32 (insn, contents + em_irel->r_offset);
10866 /* Clear relocations. */
10867 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10869 /* If there is function cse, HI20 can not remove now. */
10870 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10871 R_NDS32_LONGJUMP4, laddr);
10872 if (call_irel == irelend)
10874 *insn_len = 0;
10875 hi_irel->r_info =
10876 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10879 return true;
10882 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
10884 static bool
10885 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10886 Elf_Internal_Rela *internal_relocs, int *insn_len,
10887 int *seq_len, bfd_byte *contents,
10888 Elf_Internal_Sym *isymbuf,
10889 Elf_Internal_Shdr *symtab_hdr)
10891 /* There are 2 variations for LONGJUMP5
10892 case 2-4; 1st insn convertible, 16-bit on.
10893 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10894 j label ; 25_PCREL/INSN16
10897 case 4-4; 1st insn not convertible
10898 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10899 j label ; 25_PCREL/INSN16
10900 .L1: */
10902 bfd_vma laddr;
10903 Elf_Internal_Rela *cond_irel, *irelend;
10904 unsigned int i;
10905 bfd_signed_vma foff;
10906 uint32_t insn, re_insn = 0;
10907 uint16_t insn16, re_insn16 = 0;
10908 unsigned long reloc;
10910 enum elf_nds32_reloc_type checked_types[] =
10911 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10912 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10914 irelend = internal_relocs + sec->reloc_count;
10915 laddr = irel->r_offset;
10917 /* Get the reloc for the address from which the register is
10918 being loaded. This reloc will tell us which function is
10919 actually being called. */
10921 cond_irel =
10922 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10923 R_NDS32_25_PCREL_RELA, irel->r_addend);
10924 if (cond_irel == irelend)
10926 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10927 (uint64_t) irel->r_offset);
10928 return false;
10931 /* Get the value of the symbol referred to by the reloc. */
10932 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10934 if (foff == 0
10935 || foff < -CONSERVATIVE_16BIT_S1
10936 || foff >= CONSERVATIVE_16BIT_S1)
10937 return false;
10939 /* Get the all corresponding instructions. */
10940 insn = bfd_getb32 (contents + laddr);
10941 /* Check instruction size. */
10942 if (insn & 0x80000000)
10944 *seq_len = 0;
10945 insn16 = insn >> 16;
10946 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10948 else
10949 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10951 if (N32_OP6 (re_insn) == N32_OP6_BR1
10952 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10954 /* beqs label ; 15_PCREL. */
10955 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10956 reloc = R_NDS32_15_PCREL_RELA;
10958 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10959 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10961 /* beqz label ; 17_PCREL. */
10962 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10963 reloc = R_NDS32_17_PCREL_RELA;
10965 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
10966 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
10968 /* beqc label ; 9_PCREL. */
10969 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10970 reloc = R_NDS32_WORD_9_PCREL_RELA;
10972 else
10973 return false;
10975 /* Set all relocations. */
10976 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
10978 /* Clean relocations. */
10979 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10980 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10982 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10983 checked_types[i], laddr);
10984 if (cond_irel != irelend)
10986 if (*seq_len == 0
10987 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10989 /* If the branch instruction is 2 byte, it cannot remove
10990 directly. Only convert it to nop16 and remove it after
10991 checking alignment issue. */
10992 insn16 = NDS32_NOP16;
10993 bfd_putb16 (insn16, contents + laddr);
10994 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10996 else
10997 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10998 R_NDS32_NONE);
11001 *insn_len = 0;
11003 return true;
11006 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
11008 static bool
11009 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11010 Elf_Internal_Rela *internal_relocs, int *insn_len,
11011 int *seq_len, bfd_byte *contents,
11012 Elf_Internal_Sym *isymbuf,
11013 Elf_Internal_Shdr *symtab_hdr)
11015 /* There are 5 variations for LONGJUMP6
11016 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11017 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11018 sethi ta, hi20(symbol) ; HI20/PTR
11019 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11020 jr ta ; PTR_RES/INSN16/EMPTY
11021 .L1:
11023 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11024 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11025 sethi ta, hi20(symbol) ; HI20/PTR
11026 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11027 jr ta ; PTR_RES/INSN16/EMPTY
11028 .L1: */
11030 enum elf_nds32_reloc_type checked_types[] =
11031 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11032 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11034 int reloc_off = 0, cond_removed = 0;
11035 bfd_vma laddr;
11036 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11037 unsigned int i;
11038 bfd_signed_vma foff;
11039 uint32_t insn, re_insn = 0;
11040 uint16_t insn16, re_insn16 = 0;
11041 unsigned long reloc;
11043 irelend = internal_relocs + sec->reloc_count;
11044 laddr = irel->r_offset;
11046 /* Get the reloc for the address from which the register is
11047 being loaded. This reloc will tell us which function is
11048 actually being called. */
11049 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11050 R_NDS32_EMPTY, irel->r_addend);
11052 if (em_irel == irelend)
11054 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11055 (uint64_t) irel->r_offset);
11056 return false;
11059 /* Get the value of the symbol referred to by the reloc. */
11060 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11062 if (foff == 0
11063 || foff < -CONSERVATIVE_24BIT_S1
11064 || foff >= CONSERVATIVE_24BIT_S1)
11065 return false;
11067 insn = bfd_getb32 (contents + laddr);
11068 /* Check instruction size. */
11069 if (insn & 0x80000000)
11071 *seq_len = 0;
11072 insn16 = insn >> 16;
11073 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11075 else
11076 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11078 /* For simplicity of coding, we are going to modify the section
11079 contents, the section relocs, and the BFD symbol table. We
11080 must tell the rest of the code not to free up this
11081 information. It would be possible to instead create a table
11082 of changes which have to be made, as is done in coff-mips.c;
11083 that would be more work, but would require less memory when
11084 the linker is run. */
11086 if (N32_OP6 (re_insn) == N32_OP6_BR1
11087 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11089 /* beqs label ; 15_PCREL. */
11090 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11091 reloc = R_NDS32_15_PCREL_RELA;
11092 cond_removed = 1;
11094 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11095 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11097 /* beqz label ; 17_PCREL. */
11098 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11099 reloc = R_NDS32_17_PCREL_RELA;
11100 cond_removed = 1;
11102 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11103 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11105 /* Relax to one of the following 2 variations
11107 case 2-4; 1st insn convertible, 16-bit on.
11108 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11109 j label ; 25_PCREL/INSN16
11112 case 4-4; 1st insn not convertible
11113 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11114 j label ; 25_PCREL/INSN16
11115 .L1: */
11117 /* Use j label as second instruction. */
11118 insn = INSN_J;
11119 reloc = R_NDS32_25_PCREL_RELA;
11120 bfd_putb32 (insn, contents + em_irel->r_offset);
11122 else
11123 return false;
11125 /* Set all relocations. */
11126 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11128 cond_irel =
11129 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11130 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11131 cond_irel->r_addend = 1;
11133 /* Use INSN16 of first branch instruction to distinguish if keeping
11134 INSN16 of final instruction or not. */
11135 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11136 R_NDS32_INSN16, irel->r_offset);
11137 if (insn_irel == irelend)
11139 /* Clean the final INSN16. */
11140 insn_irel =
11141 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11142 R_NDS32_INSN16, em_irel->r_offset);
11143 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11144 R_NDS32_NONE);
11147 if (cond_removed == 1)
11149 *insn_len = 0;
11151 /* Clear relocations. */
11152 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11154 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11156 cond_irel =
11157 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11158 checked_types[i], laddr);
11159 if (cond_irel != irelend)
11161 if (*seq_len == 0
11162 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11164 /* If the branch instruction is 2 byte, it cannot remove
11165 directly. Only convert it to nop16 and remove it after
11166 checking alignment issue. */
11167 insn16 = NDS32_NOP16;
11168 bfd_putb16 (insn16, contents + laddr);
11169 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11171 else
11172 cond_irel->r_info =
11173 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11177 else
11179 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11180 R_NDS32_LONGJUMP5);
11183 return true;
11186 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
11188 static bool
11189 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11190 Elf_Internal_Rela *internal_relocs, int *insn_len,
11191 int *seq_len, bfd_byte *contents,
11192 Elf_Internal_Sym *isymbuf,
11193 Elf_Internal_Shdr *symtab_hdr)
11195 /* There are 2 variations for LONGJUMP5
11196 case 2-4; 1st insn convertible, 16-bit on.
11197 movi55 ta, imm11 ; LONGJUMP7/INSN16
11198 beq rt, ta, label ; 15_PCREL
11200 case 4-4; 1st insn not convertible
11201 movi55 ta, imm11 ; LONGJUMP7/INSN16
11202 beq rt, ta, label ; 15_PCREL */
11204 bfd_vma laddr;
11205 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
11206 bfd_signed_vma foff;
11207 uint32_t insn, re_insn = 0;
11208 uint16_t insn16;
11209 uint32_t imm11;
11211 irelend = internal_relocs + sec->reloc_count;
11212 laddr = irel->r_offset;
11214 /* Get the reloc for the address from which the register is
11215 being loaded. This reloc will tell us which function is
11216 actually being called. */
11218 cond_irel =
11219 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11220 R_NDS32_15_PCREL_RELA, irel->r_addend);
11221 if (cond_irel == irelend)
11223 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11224 (uint64_t) irel->r_offset);
11225 return false;
11228 /* Get the value of the symbol referred to by the reloc. */
11229 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11231 if (foff == 0
11232 || foff < -CONSERVATIVE_8BIT_S1
11233 || foff >= CONSERVATIVE_8BIT_S1)
11234 return false;
11236 /* Get the first instruction for its size. */
11237 insn = bfd_getb32 (contents + laddr);
11238 if (insn & 0x80000000)
11240 *seq_len = 0;
11241 /* Get the immediate from movi55. */
11242 imm11 = N16_IMM5S (insn >> 16);
11244 else
11246 /* Get the immediate from movi. */
11247 imm11 = N32_IMM20S (insn);
11250 /* Get the branch instruction. */
11251 insn = bfd_getb32 (contents + irel->r_addend);
11252 /* Convert instruction to BR3. */
11253 if ((insn >> 14) & 0x1)
11254 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11255 else
11256 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11258 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11260 /* Set all relocations. */
11261 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11262 R_NDS32_WORD_9_PCREL_RELA);
11264 /* Clean relocations. */
11265 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11266 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11267 R_NDS32_INSN16, irel->r_offset);
11268 if (insn_irel != irelend)
11270 if (*seq_len == 0)
11272 /* If the first insntruction is 16bit, convert it to nop16. */
11273 insn16 = NDS32_NOP16;
11274 bfd_putb16 (insn16, contents + laddr);
11275 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11277 else
11278 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11279 R_NDS32_NONE);
11281 *insn_len = 0;
11283 return true;
11286 /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11287 for each relax round. But the gp may changed dramatically and then cause
11288 the truncated to fit errors for the the converted gp instructions.
11289 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11291 static bool
11292 nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11293 Elf_Internal_Rela *irel, bool *again,
11294 bool init,
11295 struct elf_nds32_link_hash_table *table,
11296 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11299 int offset_to_gp;
11300 static bool sec_pass = false;
11301 static asection *first_sec = NULL, *sym_sec;
11302 /* Record the number of instructions which may be removed. */
11303 static int count = 0, record_count;
11304 Elf_Internal_Sym *isym;
11305 struct elf_link_hash_entry *h = NULL;
11306 int indx;
11307 unsigned long r_symndx;
11308 bfd *abfd = sec->owner;
11309 static bfd_vma record_sda = 0;
11310 int sda_offset = 0;
11312 /* Force doing relaxation when hyper-relax is high. */
11313 if (table->hyper_relax == 2)
11314 return true;
11316 /* Do not relax the load/store patterns for the first
11317 relax round. */
11318 if (init)
11320 if (!first_sec)
11321 first_sec = sec;
11322 else if (first_sec == sec)
11324 record_count = count;
11325 count = 0;
11326 sec_pass = true;
11329 if (!sec_pass)
11330 *again = true;
11332 return true;
11335 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11336 DATA_SEGMENT_ALIGN size in the linker script may make it
11337 get even bigger. */
11338 if (record_sda == 0)
11339 record_sda = local_sda;
11340 else if (local_sda > record_sda)
11341 sda_offset = local_sda - record_sda;
11343 /* Assume the instruction will be removed in the best case. */
11344 count++;
11346 /* We record the offset to gp for each symbol, and then check
11347 if it is changed dramatically after relaxing.
11348 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11349 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11350 r_symndx = ELF32_R_SYM (irel->r_info);
11351 if (r_symndx >= symtab_hdr->sh_info)
11353 /* Global symbols. */
11354 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11355 h = elf_sym_hashes (abfd)[indx];
11356 sym_sec = h->root.u.def.section;
11357 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11358 || bfd_is_abs_section (sym_sec))
11360 /* Forbid doing relaxation when hyper-relax is low. */
11361 if (table->hyper_relax == 0)
11362 return false;
11364 offset_to_gp = *access_addr - local_sda;
11365 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11366 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11367 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11368 < abs (offset_to_gp) - sda_offset)
11370 /* This may cause the error, so we reserve the
11371 safe enough size for relaxing. */
11372 if (*access_addr >= local_sda)
11373 *access_addr += (record_count * 4);
11374 else
11375 *access_addr -= (record_count * 4);
11377 return sec_pass;
11380 else
11382 /* Local symbols. */
11383 if (!elf32_nds32_allocate_local_sym_info (abfd))
11384 return false;
11385 isym = isymbuf + r_symndx;
11387 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11388 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11390 /* Forbid doing relaxation when hyper-relax is low. */
11391 if (table->hyper_relax == 0)
11392 return false;
11394 offset_to_gp = *access_addr - local_sda;
11395 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11396 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11397 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11398 < abs (offset_to_gp) - sda_offset)
11400 /* This may cause the error, so we reserve the
11401 safe enough size for relaxing. */
11402 if (*access_addr >= local_sda)
11403 *access_addr += (record_count * 4);
11404 else
11405 *access_addr -= (record_count * 4);
11407 return sec_pass;
11411 return true;
11414 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11416 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
11418 static bool
11419 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11420 asection *sec, Elf_Internal_Rela *irel,
11421 Elf_Internal_Rela *internal_relocs, int *insn_len,
11422 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11423 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11424 struct elf_nds32_link_hash_table *table)
11426 int eliminate_sethi = 0, range_type;
11427 unsigned int i;
11428 bfd_vma local_sda, laddr;
11429 int seq_len; /* Original length of instruction sequence. */
11430 uint32_t insn;
11431 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11432 bfd_vma access_addr = 0;
11433 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11434 struct elf_link_hash_entry *h = NULL;
11435 int indx;
11436 enum elf_nds32_reloc_type checked_types[] =
11437 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11438 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11439 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11440 R_NDS32_TLS_LE_HI20
11443 irelend = internal_relocs + sec->reloc_count;
11444 seq_len = GET_SEQ_LEN (irel->r_addend);
11445 laddr = irel->r_offset;
11446 *insn_len = seq_len;
11448 /* Get the high part relocation. */
11449 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11451 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11452 checked_types[i], laddr);
11453 if (hi_irelfn != irelend)
11454 break;
11457 if (hi_irelfn == irelend)
11459 /* Not R_NDS32_HI20_RELA. */
11460 if (i != 0)
11461 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11462 (uint64_t) irel->r_offset);
11463 return false;
11466 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11467 nds32_elf_final_sda_base (sec->output_section->owner,
11468 link_info, &local_sda, false);
11470 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11472 case R_NDS32_HI20_RELA:
11473 insn = bfd_getb32 (contents + laddr);
11474 access_addr =
11475 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11477 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11479 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11480 h = elf_sym_hashes (abfd)[indx];
11483 /* Try movi. */
11484 if (range_type == NDS32_LOADSTORE_IMM
11485 && access_addr < CONSERVATIVE_20BIT
11486 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11488 eliminate_sethi = 1;
11489 break;
11492 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11494 eliminate_sethi = 1;
11495 break;
11497 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11498 NULL, false, table, isymbuf, symtab_hdr))
11499 return false;
11501 if (!load_store_relax)
11502 return false;
11504 /* Case for set gp register. */
11505 if (N32_RT5 (insn) == REG_GP)
11506 return false;
11508 if (range_type == NDS32_LOADSTORE_FLOAT_S
11509 || range_type == NDS32_LOADSTORE_FLOAT_D)
11511 range_l = sdata_range[0][0];
11512 range_h = sdata_range[0][1];
11514 else
11516 range_l = sdata_range[1][0];
11517 range_h = sdata_range[1][1];
11519 break;
11521 default:
11522 return false;
11525 /* Delete sethi instruction. */
11526 if (eliminate_sethi == 1
11527 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11528 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11530 hi_irelfn->r_info =
11531 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11532 irel->r_info =
11533 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11534 *insn_len = 0;
11535 return true;
11538 return false;
11541 /* Relax LO12 relocation for nds32_elf_relax_section. */
11543 static void
11544 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11545 asection *sec, Elf_Internal_Rela *irel,
11546 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11547 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11548 struct elf_nds32_link_hash_table *table)
11550 uint32_t insn;
11551 bfd_vma local_sda, laddr;
11552 unsigned long reloc;
11553 bfd_vma access_addr;
11554 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11555 Elf_Internal_Rela *irelfn = NULL, *irelend;
11556 struct elf_link_hash_entry *h = NULL;
11557 int indx;
11559 /* For SDA base relative relaxation. */
11560 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11561 &local_sda, false);
11563 irelend = internal_relocs + sec->reloc_count;
11564 laddr = irel->r_offset;
11565 insn = bfd_getb32 (contents + laddr);
11567 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11568 return;
11570 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11572 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11574 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11575 h = elf_sym_hashes (abfd)[indx];
11578 /* Try movi. */
11579 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11580 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11582 reloc = R_NDS32_20_RELA;
11583 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11584 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11585 bfd_putb32 (insn, contents + laddr);
11587 else
11589 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11591 /* Fall through. */
11593 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11594 false, table, isymbuf, symtab_hdr))
11595 return;
11597 range_l = sdata_range[1][0];
11598 range_h = sdata_range[1][1];
11599 switch (ELF32_R_TYPE (irel->r_info))
11601 case R_NDS32_LO12S0_RELA:
11602 reloc = R_NDS32_SDA19S0_RELA;
11603 break;
11604 case R_NDS32_LO12S1_RELA:
11605 reloc = R_NDS32_SDA18S1_RELA;
11606 break;
11607 case R_NDS32_LO12S2_RELA:
11608 reloc = R_NDS32_SDA17S2_RELA;
11609 break;
11610 case R_NDS32_LO12S2_DP_RELA:
11611 range_l = sdata_range[0][0];
11612 range_h = sdata_range[0][1];
11613 reloc = R_NDS32_SDA12S2_DP_RELA;
11614 break;
11615 case R_NDS32_LO12S2_SP_RELA:
11616 range_l = sdata_range[0][0];
11617 range_h = sdata_range[0][1];
11618 reloc = R_NDS32_SDA12S2_SP_RELA;
11619 break;
11620 default:
11621 return;
11624 /* There are range_h and range_l because linker has to promise
11625 all sections move cross one page together. */
11626 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11627 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11628 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11630 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11632 /* Maybe we should add R_NDS32_INSN16 reloc type here
11633 or manually do some optimization. sethi can't be
11634 eliminated when updating $gp so the relative ori
11635 needs to be preserved. */
11636 return;
11638 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11639 &insn))
11640 return;
11641 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11642 bfd_putb32 (insn, contents + laddr);
11644 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11645 R_NDS32_INSN16);
11646 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11647 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11648 irelfn->r_info =
11649 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11653 return;
11656 /* Relax PTR relocation for nds32_elf_relax_section. */
11658 static bool
11659 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11660 Elf_Internal_Rela *internal_relocs, int *insn_len,
11661 int *seq_len, bfd_byte *contents)
11663 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11665 irelend = internal_relocs + sec->reloc_count;
11667 re_irel =
11668 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11669 R_NDS32_PTR_RESOLVED, irel->r_addend);
11671 if (re_irel == irelend)
11673 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11674 (uint64_t) irel->r_offset);
11675 return false;
11678 if (re_irel->r_addend != 1)
11679 return false;
11681 /* Pointed target is relaxed and no longer needs this void *,
11682 change the type to NONE. */
11683 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11685 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11686 not exist, it means only count 1 and remove it directly. */
11687 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11688 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11689 R_NDS32_PTR_COUNT);
11690 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11691 R_NDS32_PTR);
11692 if (count_irel != irelend)
11694 if (--count_irel->r_addend > 0)
11695 return false;
11698 if (ptr_irel != irelend)
11699 return false;
11701 /* If the PTR_COUNT is already 0, remove current instruction. */
11702 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11703 *insn_len = 0;
11704 return true;
11707 /* Relax LWC relocation for nds32_elf_relax_section. */
11709 static void
11710 nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11711 asection *sec, Elf_Internal_Rela *irel,
11712 Elf_Internal_Rela *internal_relocs,
11713 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11714 Elf_Internal_Shdr *symtab_hdr, bool *again)
11716 /* Pattern:
11717 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11718 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11719 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11720 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11721 ... */
11723 uint32_t insn;
11724 bfd_vma local_sda, laddr;
11725 unsigned long reloc;
11726 bfd_vma access_addr, flsi_offset;
11727 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11728 Elf_Internal_Rela *irelend, *re_irel;
11729 unsigned int opcode;
11731 irelend = internal_relocs + sec->reloc_count;
11732 laddr = irel->r_offset;
11733 insn = bfd_getb32 (contents + laddr);
11735 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11736 return;
11738 /* Can not do relaxation for bi format. */
11739 if ((insn & 0x1000))
11740 return;
11742 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11743 opcode = N32_OP6 (insn);
11744 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11745 reloc = R_NDS32_SDA12S2_SP_RELA;
11746 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11747 reloc = R_NDS32_SDA12S2_DP_RELA;
11748 else
11749 return;
11751 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11752 R_NDS32_PTR_RESOLVED);
11753 if (re_irel == irelend)
11755 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11756 (uint64_t) irel->r_offset);
11757 return;
11760 /* For SDA base relative relaxation. */
11761 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11762 &local_sda, false);
11763 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11764 flsi_offset = (insn & 0xfff) << 2;
11765 access_addr += flsi_offset;
11766 range_l = sdata_range[0][0];
11767 range_h = sdata_range[0][1];
11769 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11770 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11772 /* Turn flsi instruction into sda access format. */
11773 insn = (insn & 0x7ff07000) | (REG_GP << 15);
11775 /* Add relocation type to flsi. */
11776 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11777 irel->r_addend += flsi_offset;
11778 bfd_putb32 (insn, contents + re_irel->r_offset);
11780 re_irel->r_addend |= 1;
11781 *again = true;
11785 static bool
11786 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11787 Elf_Internal_Rela *internal_relocs,
11788 bfd_byte *contents,
11789 nds32_elf_blank_t **relax_blank_list,
11790 int optimize, int opt_size)
11792 /* This code block is used to adjust 4-byte alignment by relax a pair
11793 of instruction a time.
11795 It recognizes three types of relocations.
11796 1. R_NDS32_LABEL - a alignment.
11797 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11798 3. is_16bit_NOP () - remove a 16-bit instruction. */
11800 /* TODO: It seems currently implementation only support 4-byte alignment.
11801 We should handle any-alignment. */
11803 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11804 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11805 Elf_Internal_Rela rel_temp;
11806 Elf_Internal_Rela *irelend;
11807 bfd_vma address;
11808 uint16_t insn16;
11810 /* Checking for branch relaxation relies on the relocations to
11811 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11812 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11813 sizeof (Elf_Internal_Rela), compar_reloc);
11815 irelend = internal_relocs + sec->reloc_count;
11817 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11818 /* FIXME: Can we generate the right order in assembler?
11819 So we don't have to swapping them here. */
11821 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11822 label_rel < irelend; label_rel++)
11824 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11825 continue;
11827 /* Find the first reloc has the same offset with label_rel. */
11828 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11829 insn_rel++;
11831 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11832 insn_rel++)
11833 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11834 address. */
11835 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11836 break;
11838 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11839 && insn_rel < label_rel)
11841 /* Swap the two reloc if the R_NDS32_INSN16 is
11842 before R_NDS32_LABEL. */
11843 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11844 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11845 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11849 label_rel = NULL;
11850 insn_rel = NULL;
11851 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11852 or higher, remove other R_NDS32_LABEL with lower alignment.
11853 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11854 then the R_NDS32_LABEL sequence is broke. */
11855 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11857 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11859 if (label_rel == NULL)
11861 if (tmp_rel->r_addend < 2)
11862 label_rel = tmp_rel;
11863 continue;
11865 else if (tmp_rel->r_addend > 1)
11867 /* Remove all LABEL relocation from label_rel to tmp_rel
11868 including relocations with same offset as tmp_rel. */
11869 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
11871 if (tmp2_rel->r_offset == tmp_rel->r_offset)
11872 break;
11874 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11875 && tmp2_rel->r_addend < 2)
11876 tmp2_rel->r_info =
11877 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11878 R_NDS32_NONE);
11880 label_rel = NULL;
11883 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11885 /* A new INSN16 which can be converted, so clear label_rel. */
11886 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11887 irelend, &insn16)
11888 || is_16bit_NOP (abfd, sec, tmp_rel))
11889 label_rel = NULL;
11893 label_rel = NULL;
11894 insn_rel = NULL;
11895 /* Optimized for speed and nothing has not been relaxed.
11896 It's time to align labels.
11897 We may convert a 16-bit instruction right before a label to
11898 32-bit, in order to align the label if necessary
11899 all reloc entries has been sorted by r_offset. */
11900 for (irel = internal_relocs;
11901 irel < irelend && irel->r_offset < sec->size; irel++)
11903 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11904 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11905 continue;
11907 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11909 /* A new INSN16 found, resize the old one. */
11910 if (is_convert_32_to_16
11911 (abfd, sec, irel, internal_relocs, irelend, &insn16)
11912 || is_16bit_NOP (abfd, sec, irel))
11914 if (insn_rel)
11916 /* Previous INSN16 reloc exists, reduce its
11917 size to 16-bit. */
11918 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11919 irelend, &insn16))
11921 nds32_elf_write_16 (abfd, contents, insn_rel,
11922 internal_relocs, irelend, insn16);
11924 if (!insert_nds32_elf_blank_recalc_total
11925 (relax_blank_list, insn_rel->r_offset + 2, 2))
11926 return false;
11928 else if (is_16bit_NOP (abfd, sec, insn_rel))
11930 if (!insert_nds32_elf_blank_recalc_total
11931 (relax_blank_list, insn_rel->r_offset, 2))
11932 return false;
11934 insn_rel->r_info =
11935 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11937 /* Save the new one for later use. */
11938 insn_rel = irel;
11940 else
11941 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11942 R_NDS32_NONE);
11944 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11946 /* Search for label. */
11947 int force_relax = 0;
11949 /* Label on 16-bit instruction or optimization
11950 needless, just reset this reloc. */
11951 insn16 = bfd_getb16 (contents + irel->r_offset);
11952 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
11954 irel->r_info =
11955 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11956 continue;
11959 address =
11960 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
11961 irel->r_offset, 1);
11963 if (!insn_rel)
11965 /* Check if there is case which can not be aligned. */
11966 if (irel->r_addend == 2 && address & 0x2)
11967 return false;
11968 continue;
11971 /* Try to align this label. */
11973 if ((irel->r_addend & 0x1f) < 2)
11975 /* Check if there is a INSN16 at the same address.
11976 Label_rel always seats before insn_rel after
11977 our sort. */
11979 /* Search for INSN16 at LABEL location. If INSN16 is at
11980 same location and this LABEL alignment is lower than 2,
11981 the INSN16 can be converted to 2-byte. */
11982 for (tmp_rel = irel;
11983 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
11984 tmp_rel++)
11986 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
11987 && (is_convert_32_to_16
11988 (abfd, sec, tmp_rel, internal_relocs,
11989 irelend, &insn16)
11990 || is_16bit_NOP (abfd, sec, tmp_rel)))
11992 force_relax = 1;
11993 break;
11998 if (force_relax || irel->r_addend == 1 || address & 0x2)
12000 /* Label not aligned. */
12001 /* Previous reloc exists, reduce its size to 16-bit. */
12002 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12003 internal_relocs, irelend, &insn16))
12005 nds32_elf_write_16 (abfd, contents, insn_rel,
12006 internal_relocs, irelend, insn16);
12008 if (!insert_nds32_elf_blank_recalc_total
12009 (relax_blank_list, insn_rel->r_offset + 2, 2))
12010 return false;
12012 else if (is_16bit_NOP (abfd, sec, insn_rel))
12014 if (!insert_nds32_elf_blank_recalc_total
12015 (relax_blank_list, insn_rel->r_offset, 2))
12016 return false;
12020 /* INSN16 reloc is used. */
12021 insn_rel = NULL;
12025 address =
12026 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12027 if (insn_rel && (address & 0x2 || opt_size))
12029 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12030 irelend, &insn16))
12032 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12033 irelend, insn16);
12034 if (!insert_nds32_elf_blank_recalc_total
12035 (relax_blank_list, insn_rel->r_offset + 2, 2))
12036 return false;
12037 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12038 R_NDS32_NONE);
12040 else if (is_16bit_NOP (abfd, sec, insn_rel))
12042 if (!insert_nds32_elf_blank_recalc_total
12043 (relax_blank_list, insn_rel->r_offset, 2))
12044 return false;
12045 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12046 R_NDS32_NONE);
12049 insn_rel = NULL;
12050 return true;
12053 static bool
12054 nds32_elf_relax_section (bfd *abfd, asection *sec,
12055 struct bfd_link_info *link_info, bool *again)
12057 nds32_elf_blank_t *relax_blank_list = NULL;
12058 Elf_Internal_Shdr *symtab_hdr;
12059 Elf_Internal_Rela *internal_relocs;
12060 Elf_Internal_Rela *irel;
12061 Elf_Internal_Rela *irelend;
12062 Elf_Internal_Sym *isymbuf = NULL;
12063 bfd_byte *contents = NULL;
12064 bool result = true;
12065 int optimize = 0;
12066 int opt_size = 0;
12067 uint32_t insn;
12068 uint16_t insn16;
12070 /* Target dependnet option. */
12071 struct elf_nds32_link_hash_table *table;
12072 int load_store_relax;
12074 relax_blank_list = NULL;
12076 *again = false;
12078 /* Nothing to do for
12079 * relocatable link or
12080 * non-relocatable section or
12081 * non-code section or
12082 * empty content or
12083 * no reloc entry. */
12084 if (bfd_link_relocatable (link_info)
12085 || (sec->flags & SEC_RELOC) == 0
12086 || (sec->flags & SEC_EXCLUDE) != 0
12087 || (sec->flags & SEC_CODE) == 0
12088 || sec->size == 0
12089 || sec->reloc_count == 0)
12090 return true;
12092 /* 09.12.11 Workaround. */
12093 /* We have to adjust align for R_NDS32_LABEL if needed.
12094 The adjust approach only can fix 2-byte align once. */
12095 if (sec->alignment_power > 2)
12096 return true;
12098 /* Do TLS model conversion once at first. */
12099 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12101 /* The optimization type to do. */
12103 table = nds32_elf_hash_table (link_info);
12105 /* Save the first section for abs symbol relaxation.
12106 This is used for checking gp relaxation in the
12107 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12108 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, true,
12109 table, NULL, NULL);
12111 /* The begining of general relaxation. */
12113 if (is_SDA_BASE_set == 0)
12115 bfd_vma gp;
12116 is_SDA_BASE_set = 1;
12117 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12118 &gp, false);
12119 relax_range_measurement (abfd, link_info);
12122 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12123 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12124 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12125 true /* keep_memory */);
12126 if (internal_relocs == NULL)
12127 goto error_return;
12129 irelend = internal_relocs + sec->reloc_count;
12130 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12131 irelend, R_NDS32_RELAX_ENTRY);
12133 if (irel == irelend)
12134 return true;
12136 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12138 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12139 return true;
12141 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12142 optimize = 1;
12144 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12145 opt_size = 1;
12148 load_store_relax = table->load_store_relax;
12150 /* Get symbol table and section content. */
12151 contents = NULL;
12152 if (!nds32_get_section_contents (abfd, sec, &contents, true)
12153 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12154 goto error_return;
12156 /* Do relax loop only when finalize is not done.
12157 Take care of relaxable relocs except INSN16. */
12158 for (irel = internal_relocs; irel < irelend; irel++)
12160 int seq_len; /* Original length of instruction sequence. */
12161 int insn_len = 0; /* Final length of instruction sequence. */
12162 bool removed;
12164 insn = 0;
12165 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12166 && (irel->r_addend & 0x1f) >= 2)
12167 optimize = 1;
12169 /* Relocation Types
12170 R_NDS32_LONGCALL1 53
12171 R_NDS32_LONGCALL2 54
12172 R_NDS32_LONGCALL3 55
12173 R_NDS32_LONGJUMP1 56
12174 R_NDS32_LONGJUMP2 57
12175 R_NDS32_LONGJUMP3 58
12176 R_NDS32_LOADSTORE 59 */
12177 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12178 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12179 seq_len = GET_SEQ_LEN (irel->r_addend);
12181 /* Relocation Types
12182 R_NDS32_LONGCALL4 107
12183 R_NDS32_LONGCALL5 108
12184 R_NDS32_LONGCALL6 109
12185 R_NDS32_LONGJUMP4 110
12186 R_NDS32_LONGJUMP5 111
12187 R_NDS32_LONGJUMP6 112
12188 R_NDS32_LONGJUMP7 113 */
12189 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12190 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12191 seq_len = 4;
12193 /* Relocation Types
12194 R_NDS32_LO12S0_RELA 30
12195 R_NDS32_LO12S1_RELA 29
12196 R_NDS32_LO12S2_RELA 28
12197 R_NDS32_LO12S2_SP_RELA 71
12198 R_NDS32_LO12S2_DP_RELA 70
12199 R_NDS32_GOT_LO12 46
12200 R_NDS32_GOTOFF_LO12 50
12201 R_NDS32_PLTREL_LO12 65
12202 R_NDS32_PLT_GOTREL_LO12 67
12203 R_NDS32_17IFC_PCREL_RELA 96
12204 R_NDS32_GOT_SUFF 193
12205 R_NDS32_GOTOFF_SUFF 194
12206 R_NDS32_PLT_GOT_SUFF 195
12207 R_NDS32_MULCALL_SUFF 196
12208 R_NDS32_PTR 197 */
12209 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12210 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12211 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12212 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12213 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12214 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12215 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12216 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12217 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12218 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12219 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12220 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12221 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12222 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12223 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12224 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12225 seq_len = 0;
12226 else
12227 continue;
12229 insn_len = seq_len;
12230 removed = false;
12232 switch (ELF32_R_TYPE (irel->r_info))
12234 case R_NDS32_LONGCALL1:
12235 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12236 &insn_len, contents, isymbuf,
12237 symtab_hdr);
12238 break;
12239 case R_NDS32_LONGCALL2:
12240 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12241 &insn_len, contents, isymbuf,
12242 symtab_hdr);
12243 break;
12244 case R_NDS32_LONGCALL3:
12245 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12246 &insn_len, contents, isymbuf,
12247 symtab_hdr);
12248 break;
12249 case R_NDS32_LONGJUMP1:
12250 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12251 &insn_len, contents, isymbuf,
12252 symtab_hdr);
12253 break;
12254 case R_NDS32_LONGJUMP2:
12255 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12256 &insn_len, contents, isymbuf,
12257 symtab_hdr);
12258 break;
12259 case R_NDS32_LONGJUMP3:
12260 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12261 &insn_len, contents, isymbuf,
12262 symtab_hdr);
12263 break;
12264 case R_NDS32_LONGCALL4:
12265 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12266 &insn_len, contents, isymbuf,
12267 symtab_hdr);
12268 break;
12269 case R_NDS32_LONGCALL5:
12270 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12271 &insn_len, contents, isymbuf,
12272 symtab_hdr);
12273 break;
12274 case R_NDS32_LONGCALL6:
12275 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12276 &insn_len, contents, isymbuf,
12277 symtab_hdr);
12278 break;
12279 case R_NDS32_LONGJUMP4:
12280 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12281 &insn_len, contents, isymbuf,
12282 symtab_hdr);
12283 break;
12284 case R_NDS32_LONGJUMP5:
12285 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12286 &insn_len, &seq_len, contents,
12287 isymbuf, symtab_hdr);
12288 break;
12289 case R_NDS32_LONGJUMP6:
12290 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12291 &insn_len, &seq_len, contents,
12292 isymbuf, symtab_hdr);
12293 break;
12294 case R_NDS32_LONGJUMP7:
12295 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12296 &insn_len, &seq_len, contents,
12297 isymbuf, symtab_hdr);
12298 break;
12299 case R_NDS32_LOADSTORE:
12300 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12301 internal_relocs, &insn_len,
12302 contents, isymbuf, symtab_hdr,
12303 load_store_relax, table);
12304 break;
12305 case R_NDS32_LO12S0_RELA:
12306 case R_NDS32_LO12S1_RELA:
12307 case R_NDS32_LO12S2_RELA:
12308 case R_NDS32_LO12S2_DP_RELA:
12309 case R_NDS32_LO12S2_SP_RELA:
12310 /* Relax for low part. */
12311 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12312 contents, isymbuf, symtab_hdr, table);
12314 /* It is impossible to delete blank, so just continue. */
12315 continue;
12316 case R_NDS32_PTR:
12317 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12318 &insn_len, &seq_len, contents);
12319 break;
12320 case R_NDS32_LSI:
12321 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12322 contents, isymbuf, symtab_hdr, again);
12323 continue;
12324 case R_NDS32_GOT_LO12:
12325 case R_NDS32_GOTOFF_LO12:
12326 case R_NDS32_PLTREL_LO12:
12327 case R_NDS32_PLT_GOTREL_LO12:
12328 case R_NDS32_GOTPC_LO12:
12329 case R_NDS32_TLS_LE_LO12:
12330 case R_NDS32_TLS_LE_ADD:
12331 case R_NDS32_TLS_LE_LS:
12332 case R_NDS32_PLT_GOT_SUFF:
12333 case R_NDS32_GOT_SUFF:
12334 case R_NDS32_GOTOFF_SUFF:
12335 continue;
12336 default:
12337 continue;
12340 if (removed && seq_len - insn_len > 0)
12342 if (!insert_nds32_elf_blank
12343 (&relax_blank_list, irel->r_offset + insn_len,
12344 seq_len - insn_len))
12345 goto error_return;
12346 *again = true;
12350 calc_nds32_blank_total (relax_blank_list);
12352 if (table->relax_fp_as_gp)
12354 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12355 irelend, isymbuf))
12356 goto error_return;
12358 if (!*again)
12360 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12361 irelend))
12362 goto error_return;
12366 if (!*again)
12368 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12369 &relax_blank_list, optimize, opt_size))
12370 goto error_return;
12373 /* It doesn't matter optimize_for_space_no_align anymore.
12374 If object file is assembled with flag '-Os',
12375 the we don't adjust jump-destination on 4-byte boundary. */
12377 if (relax_blank_list)
12379 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12380 relax_blank_list = NULL;
12383 if (!*again)
12385 /* Closing the section, so we don't relax it anymore. */
12386 bfd_vma sec_size_align;
12387 Elf_Internal_Rela *tmp_rel;
12389 /* Pad to alignment boundary. Only handle current section alignment. */
12390 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12391 & ((-1U) << sec->alignment_power);
12392 if ((sec_size_align - sec->size) & 0x2)
12394 insn16 = NDS32_NOP16;
12395 bfd_putb16 (insn16, contents + sec->size);
12396 sec->size += 2;
12399 while (sec_size_align != sec->size)
12401 insn = NDS32_NOP32;
12402 bfd_putb32 (insn, contents + sec->size);
12403 sec->size += 4;
12406 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12407 irelend, R_NDS32_RELAX_ENTRY);
12408 if (tmp_rel != irelend)
12409 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12411 clean_nds32_elf_blank ();
12414 finish:
12415 if (elf_section_data (sec)->relocs != internal_relocs)
12416 free (internal_relocs);
12418 if (elf_section_data (sec)->this_hdr.contents != contents)
12419 free (contents);
12421 if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12422 free (isymbuf);
12424 return result;
12426 error_return:
12427 result = false;
12428 goto finish;
12431 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12433 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12434 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12435 {NULL, 0, 0, 0, 0}
12438 static bool
12439 nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12441 const char *name = hdr->bfd_section->name;
12443 if (startswith (name, ".sbss")
12444 || startswith (name, ".sdata"))
12445 hdr->bfd_section->flags |= SEC_SMALL_DATA;
12447 return true;
12450 static bool
12451 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12452 struct bfd_link_info *info,
12453 void *finfo ATTRIBUTE_UNUSED,
12454 int (*func) (void *, const char *,
12455 Elf_Internal_Sym *,
12456 asection *,
12457 struct elf_link_hash_entry *)
12458 ATTRIBUTE_UNUSED)
12460 FILE *sym_ld_script = NULL;
12461 struct elf_nds32_link_hash_table *table;
12463 table = nds32_elf_hash_table (info);
12464 sym_ld_script = table->sym_ld_script;
12466 if (check_start_export_sym)
12467 fprintf (sym_ld_script, "}\n");
12469 return true;
12472 static enum elf_reloc_type_class
12473 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12474 const asection *rel_sec ATTRIBUTE_UNUSED,
12475 const Elf_Internal_Rela *rela)
12477 switch ((int) ELF32_R_TYPE (rela->r_info))
12479 case R_NDS32_RELATIVE:
12480 return reloc_class_relative;
12481 case R_NDS32_JMP_SLOT:
12482 return reloc_class_plt;
12483 case R_NDS32_COPY:
12484 return reloc_class_copy;
12485 default:
12486 return reloc_class_normal;
12490 /* Put target dependent option into info hash table. */
12491 void
12492 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12493 int relax_fp_as_gp,
12494 int eliminate_gc_relocs,
12495 FILE * sym_ld_script,
12496 int hyper_relax,
12497 int tls_desc_trampoline,
12498 int load_store_relax)
12500 struct elf_nds32_link_hash_table *table;
12502 table = nds32_elf_hash_table (link_info);
12503 if (table == NULL)
12504 return;
12506 table->relax_fp_as_gp = relax_fp_as_gp;
12507 table->eliminate_gc_relocs = eliminate_gc_relocs;
12508 table->sym_ld_script = sym_ld_script;
12509 table->hyper_relax = hyper_relax;
12510 table->tls_desc_trampoline = tls_desc_trampoline;
12511 table ->load_store_relax = load_store_relax;
12515 /* These functions and data-structures are used for fp-as-gp
12516 optimization. */
12518 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12519 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12520 the read-only section and read-write section. */
12521 #define FAG_WINDOW (508 - 32)
12523 /* An nds32_fag represent a gp-relative access.
12524 We find best fp-base by using a sliding window
12525 to find a base address which can cover most gp-access. */
12526 struct nds32_fag
12528 struct nds32_fag *next; /* NULL-teminated linked list. */
12529 bfd_vma addr; /* The address of this fag. */
12530 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12531 It is used for applying FP7U2_FLAG. */
12532 int count; /* How many times this address is referred.
12533 There should be exactly `count' relocations
12534 in relas. */
12535 int relas_capcity; /* The buffer size of relas.
12536 We use an array instead of linked-list,
12537 and realloc is used to adjust buffer size. */
12540 static void
12541 nds32_fag_init (struct nds32_fag *head)
12543 memset (head, 0, sizeof (struct nds32_fag));
12546 static void
12547 nds32_fag_verify (struct nds32_fag *head)
12549 struct nds32_fag *iter;
12550 struct nds32_fag *prev;
12552 prev = NULL;
12553 iter = head->next;
12554 while (iter)
12556 if (prev && prev->addr >= iter->addr)
12557 puts ("Bug in fp-as-gp insertion.");
12558 prev = iter;
12559 iter = iter->next;
12563 /* Insert a fag in ascending order.
12564 If a fag of the same address already exists,
12565 they are chained by relas array. */
12567 static void
12568 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12569 Elf_Internal_Rela * rel)
12571 struct nds32_fag *iter;
12572 struct nds32_fag *new_fag;
12573 const int INIT_RELAS_CAP = 4;
12575 for (iter = head;
12576 iter->next && iter->next->addr <= addr;
12577 iter = iter->next)
12578 /* Find somewhere to insert. */ ;
12580 /* `iter' will be equal to `head' if the list is empty. */
12581 if (iter != head && iter->addr == addr)
12583 /* The address exists in the list.
12584 Insert `rel' into relocation list, relas. */
12586 /* Check whether relas is big enough. */
12587 if (iter->count >= iter->relas_capcity)
12589 iter->relas_capcity *= 2;
12590 iter->relas = bfd_realloc
12591 (iter->relas, iter->relas_capcity * sizeof (void *));
12593 iter->relas[iter->count++] = rel;
12594 return;
12597 /* This is a new address. Create a fag node for it. */
12598 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12599 memset (new_fag, 0, sizeof (*new_fag));
12600 new_fag->addr = addr;
12601 new_fag->count = 1;
12602 new_fag->next = iter->next;
12603 new_fag->relas_capcity = INIT_RELAS_CAP;
12604 new_fag->relas = (Elf_Internal_Rela **)
12605 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12606 new_fag->relas[0] = rel;
12607 iter->next = new_fag;
12609 nds32_fag_verify (head);
12612 static void
12613 nds32_fag_free_list (struct nds32_fag *head)
12615 struct nds32_fag *iter;
12617 iter = head->next;
12618 while (iter)
12620 struct nds32_fag *tmp = iter;
12621 iter = iter->next;
12622 free (tmp->relas);
12623 tmp->relas = NULL;
12624 free (tmp);
12628 /* Find the best fp-base address.
12629 The relocation associated with that address is returned,
12630 so we can track the symbol instead of a fixed address.
12632 When relaxation, the address of an datum may change,
12633 because a text section is shrinked, so the data section
12634 moves forward. If the aligments of text and data section
12635 are different, their distance may change too.
12636 Therefore, tracking a fixed address is not appriate. */
12638 static int
12639 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12641 struct nds32_fag *base; /* First fag in the window. */
12642 struct nds32_fag *last; /* First fag outside the window. */
12643 int accu = 0; /* Usage accumulation. */
12644 struct nds32_fag *best; /* Best fag. */
12645 int baccu = 0; /* Best accumulation. */
12647 /* Use first fag for initial, and find the last fag in the window.
12649 In each iteration, we could simply subtract previous fag
12650 and accumulate following fags which are inside the window,
12651 untill we each the end. */
12653 if (head->next == NULL)
12655 *bestpp = NULL;
12656 return 0;
12659 /* Initialize base. */
12660 base = head->next;
12661 best = base;
12662 for (last = base;
12663 last && last->addr < base->addr + FAG_WINDOW;
12664 last = last->next)
12665 accu += last->count;
12667 baccu = accu;
12669 /* Record the best base in each iteration. */
12670 while (base->next)
12672 accu -= base->count;
12673 base = base->next;
12674 /* Account fags in window. */
12675 for (/* Nothing. */;
12676 last && last->addr < base->addr + FAG_WINDOW;
12677 last = last->next)
12678 accu += last->count;
12680 /* A better fp-base? */
12681 if (accu > baccu)
12683 best = base;
12684 baccu = accu;
12688 if (bestpp)
12689 *bestpp = best;
12690 return baccu;
12693 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12694 so we can convert it fo fp-relative access later.
12695 `best_fag' is the best fp-base. Only those inside the window
12696 of best_fag is applied the flag. */
12698 static bool
12699 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12700 asection *sec, struct nds32_fag *best_fag,
12701 Elf_Internal_Rela *internal_relocs,
12702 Elf_Internal_Rela *irelend)
12704 struct nds32_fag *ifag;
12705 bfd_vma best_fpbase, gp;
12706 bfd *output_bfd;
12708 output_bfd = sec->output_section->owner;
12709 nds32_elf_final_sda_base (output_bfd, link_info, &gp, false);
12710 best_fpbase = best_fag->addr;
12712 if (best_fpbase > gp + sdata_range[1][1]
12713 || best_fpbase < gp - sdata_range[1][0])
12714 return false;
12716 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12717 so we know they can be converted to lwi37.fp. */
12718 for (ifag = best_fag;
12719 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12721 int i;
12723 for (i = 0; i < ifag->count; i++)
12725 Elf_Internal_Rela *insn16_rel;
12726 Elf_Internal_Rela *fag_rel;
12728 fag_rel = ifag->relas[i];
12730 /* Only if this is within the WINDOWS, FP7U2_FLAG
12731 is applied. */
12733 insn16_rel = find_relocs_at_address
12734 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12736 if (insn16_rel != irelend)
12737 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12740 return true;
12743 /* Reset INSN16 to clean fp as gp. */
12745 static void
12746 nds32_fag_unmark_relax (struct nds32_fag *fag,
12747 Elf_Internal_Rela *internal_relocs,
12748 Elf_Internal_Rela *irelend)
12750 struct nds32_fag *ifag;
12751 int i;
12752 Elf_Internal_Rela *insn16_rel;
12753 Elf_Internal_Rela *fag_rel;
12755 for (ifag = fag; ifag; ifag = ifag->next)
12757 for (i = 0; i < ifag->count; i++)
12759 fag_rel = ifag->relas[i];
12761 /* Restore the INSN16 relocation. */
12762 insn16_rel = find_relocs_at_address
12763 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12765 if (insn16_rel != irelend)
12766 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12771 /* This is the main function of fp-as-gp optimization.
12772 It should be called by relax_section. */
12774 static bool
12775 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12776 bfd *abfd, asection *sec,
12777 Elf_Internal_Rela *internal_relocs,
12778 Elf_Internal_Rela *irelend,
12779 Elf_Internal_Sym *isymbuf)
12781 Elf_Internal_Rela *begin_rel = NULL;
12782 Elf_Internal_Rela *irel;
12783 struct nds32_fag fag_head;
12784 Elf_Internal_Shdr *symtab_hdr;
12785 bfd_byte *contents;
12786 bool ifc_inside = false;
12788 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12790 /* Per-function fp-base selection.
12791 1. Create a list for all the gp-relative access.
12792 2. Base on those gp-relative address,
12793 find a fp-base which can cover most access.
12794 3. Use the fp-base for fp-as-gp relaxation.
12796 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12797 we should
12798 1. delete the `la $fp, _FP_BASE_' instruction and
12799 2. not convert lwi.gp to lwi37.fp.
12801 To delete the _FP_BASE_ instruction, we simply apply
12802 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12804 To suppress the conversion, we simply NOT to apply
12805 R_NDS32_INSN16_FP7U2_FLAG flag. */
12807 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12809 contents = NULL;
12810 if (!nds32_get_section_contents (abfd, sec, &contents, true)
12811 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12812 return false;
12814 /* Check whether it is worth for fp-as-gp optimization,
12815 i.e., at least 3 gp-load.
12817 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12818 apply this optimization. */
12820 for (irel = internal_relocs; irel < irelend; irel++)
12822 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12823 One we enter the begin of the region, we track all the LW/ST
12824 instructions, so when we leave the region, we try to find
12825 the best fp-base address for those LW/ST instructions. */
12827 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12828 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12830 /* Begin of the region. */
12831 if (begin_rel)
12832 /* xgettext:c-format */
12833 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12835 begin_rel = irel;
12836 nds32_fag_init (&fag_head);
12837 ifc_inside = false;
12839 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12840 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12842 int accu;
12843 struct nds32_fag *best_fag, *tmp_fag;
12844 int dist;
12846 /* End of the region.
12847 Check whether it is worth to do fp-as-gp. */
12849 if (begin_rel == NULL)
12851 /* xgettext:c-format */
12852 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12853 abfd, sec);
12854 continue;
12857 accu = nds32_fag_find_base (&fag_head, &best_fag);
12859 /* Clean FP7U2_FLAG because they may set ever. */
12860 tmp_fag = fag_head.next;
12861 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12863 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12864 if (accu < FAG_THRESHOLD
12865 || !nds32_fag_mark_relax (link_info, sec, best_fag,
12866 internal_relocs, irelend))
12868 /* Not worth to do fp-as-gp. */
12869 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12870 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12871 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12872 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12873 nds32_fag_free_list (&fag_head);
12874 begin_rel = NULL;
12875 continue;
12878 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12879 so we use it to record the distance to the reloction of best
12880 fp-base. */
12881 dist = best_fag->relas[0] - begin_rel;
12882 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12883 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12884 relocation. And get the base value when relocating. */
12885 begin_rel->r_addend &= (0x1 << 16) - 1;
12886 begin_rel->r_addend |= dist << 16;
12888 nds32_fag_free_list (&fag_head);
12889 begin_rel = NULL;
12892 if (begin_rel == NULL || ifc_inside)
12893 /* Skip if we are not in the region of fp-as-gp. */
12894 continue;
12896 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12897 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12899 bfd_vma addr;
12900 uint32_t insn;
12902 /* A gp-relative access is found. Insert it to the fag-list. */
12904 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
12905 insn = bfd_getb32 (contents + irel->r_offset);
12906 if (!N32_IS_RT3 (insn))
12907 continue;
12909 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12910 nds32_fag_insert (&fag_head, addr, irel);
12912 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12914 begin_rel = NULL;
12916 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12917 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12919 /* Suppress fp as gp when encounter ifc. */
12920 ifc_inside = true;
12924 return true;
12927 /* Remove unused `la $fp, _FD_BASE_' instruction. */
12929 static bool
12930 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12931 Elf_Internal_Rela *internal_relocs,
12932 Elf_Internal_Rela *irelend)
12934 Elf_Internal_Rela *irel;
12935 Elf_Internal_Shdr *symtab_hdr;
12936 bfd_byte *contents = NULL;
12937 nds32_elf_blank_t *relax_blank_list = NULL;
12938 bool result = true;
12939 bool unused_region = false;
12942 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
12943 R_NDS32_17IFC_PCREL_RELA
12944 R_NDS32_10IFCU_PCREL_RELA. */
12946 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12947 nds32_get_section_contents (abfd, sec, &contents, true);
12949 for (irel = internal_relocs; irel < irelend; irel++)
12951 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
12952 we marked to in previous pass.
12953 DO NOT scan relocations again, since we've alreadly decided it
12954 and set the flag. */
12955 const char *syname;
12956 int syndx;
12957 uint32_t insn;
12959 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12960 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12961 unused_region = true;
12962 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12963 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12964 unused_region = false;
12966 /* We're not in the region. */
12967 if (!unused_region)
12968 continue;
12970 /* _FP_BASE_ must be a GLOBAL symbol. */
12971 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
12972 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
12973 continue;
12975 /* The symbol name must be _FP_BASE_. */
12976 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
12977 if (strcmp (syname, FP_BASE_NAME) != 0)
12978 continue;
12980 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
12982 /* addi.gp $fp, -256 */
12983 insn = bfd_getb32 (contents + irel->r_offset);
12984 if (insn != INSN_ADDIGP_TO_FP)
12985 continue;
12987 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
12989 /* addi $fp, $gp, -256 */
12990 insn = bfd_getb32 (contents + irel->r_offset);
12991 if (insn != INSN_ADDI_GP_TO_FP)
12992 continue;
12994 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
12996 /* movi $fp, FP_BASE */
12997 insn = bfd_getb32 (contents + irel->r_offset);
12998 if (insn != INSN_MOVI_TO_FP)
12999 continue;
13001 else
13002 continue;
13004 /* We got here because a FP_BASE instruction is found. */
13005 if (!insert_nds32_elf_blank_recalc_total
13006 (&relax_blank_list, irel->r_offset, 4))
13007 goto error_return;
13010 finish:
13011 if (relax_blank_list)
13013 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13014 relax_blank_list = NULL;
13016 return result;
13018 error_return:
13019 result = false;
13020 goto finish;
13023 /* This is a version of bfd_generic_get_relocated_section_contents.
13024 We need this variety because relaxation will modify the dwarf
13025 infomation. When there is undefined symbol reference error mesage,
13026 linker need to dump line number where the symbol be used. However
13027 the address is be relaxed, it can not get the original dwarf contents.
13028 The variety only modify function call for reading in the section. */
13030 static bfd_byte *
13031 nds32_elf_get_relocated_section_contents (bfd *abfd,
13032 struct bfd_link_info *link_info,
13033 struct bfd_link_order *link_order,
13034 bfd_byte *data,
13035 bool relocatable,
13036 asymbol **symbols)
13038 bfd *input_bfd = link_order->u.indirect.section->owner;
13039 asection *input_section = link_order->u.indirect.section;
13040 long reloc_size;
13041 arelent **reloc_vector;
13042 long reloc_count;
13044 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13045 if (reloc_size < 0)
13046 return NULL;
13048 /* Read in the section. */
13049 if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
13050 return NULL;
13052 if (reloc_size == 0)
13053 return data;
13055 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13056 if (reloc_vector == NULL)
13057 return NULL;
13059 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13060 reloc_vector, symbols);
13061 if (reloc_count < 0)
13062 goto error_return;
13064 if (reloc_count > 0)
13066 arelent **parent;
13067 for (parent = reloc_vector; *parent != NULL; parent++)
13069 char *error_message = NULL;
13070 asymbol *symbol;
13071 bfd_reloc_status_type r;
13073 symbol = *(*parent)->sym_ptr_ptr;
13074 if (symbol->section && discarded_section (symbol->section))
13076 bfd_vma off;
13077 static reloc_howto_type none_howto
13078 = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13079 "unused", false, 0, 0, false);
13081 off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13082 input_section);
13083 _bfd_clear_contents ((*parent)->howto, input_bfd,
13084 input_section, data, off);
13085 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13086 (*parent)->addend = 0;
13087 (*parent)->howto = &none_howto;
13088 r = bfd_reloc_ok;
13090 else
13091 r = bfd_perform_relocation (input_bfd, *parent, data,
13092 input_section,
13093 relocatable ? abfd : NULL,
13094 &error_message);
13096 if (relocatable)
13098 asection *os = input_section->output_section;
13100 /* A partial link, so keep the relocs. */
13101 os->orelocation[os->reloc_count] = *parent;
13102 os->reloc_count++;
13105 if (r != bfd_reloc_ok)
13107 switch (r)
13109 case bfd_reloc_undefined:
13110 (*link_info->callbacks->undefined_symbol)
13111 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13112 input_bfd, input_section, (*parent)->address, true);
13113 break;
13114 case bfd_reloc_dangerous:
13115 BFD_ASSERT (error_message != NULL);
13116 (*link_info->callbacks->reloc_dangerous)
13117 (link_info, error_message,
13118 input_bfd, input_section, (*parent)->address);
13119 break;
13120 case bfd_reloc_overflow:
13121 (*link_info->callbacks->reloc_overflow)
13122 (link_info, NULL,
13123 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13124 (*parent)->howto->name, (*parent)->addend,
13125 input_bfd, input_section, (*parent)->address);
13126 break;
13127 case bfd_reloc_outofrange:
13128 /* PR ld/13730:
13129 This error can result when processing some partially
13130 complete binaries. Do not abort, but issue an error
13131 message instead. */
13132 link_info->callbacks->einfo
13133 /* xgettext:c-format */
13134 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13135 abfd, input_section, * parent);
13136 goto error_return;
13138 default:
13139 abort ();
13140 break;
13146 free (reloc_vector);
13147 return data;
13149 error_return:
13150 free (reloc_vector);
13151 return NULL;
13154 /* Check target symbol. */
13156 static bool
13157 nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13159 if (!sym || !sym->name || sym->name[0] != '$')
13160 return false;
13161 return true;
13164 /* nds32 find maybe function sym. Ignore target special symbol
13165 first, and then go the general function. */
13167 static bfd_size_type
13168 nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13169 bfd_vma *code_off)
13171 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13172 return 0;
13174 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13178 /* Do TLS model conversion. */
13180 typedef struct relax_group_list_t
13182 Elf_Internal_Rela *relo;
13183 struct relax_group_list_t *next;
13184 struct relax_group_list_t *next_sibling;
13185 int id;
13186 } relax_group_list_t;
13189 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13192 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13194 void
13195 dump_chain (relax_group_list_t *pHead);
13198 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13200 relax_group_list_t *pNext = pHead;
13202 /* Find place. */
13203 while (pNext->next)
13205 if (pNext->next->id > (int) pElem->r_addend)
13206 break;
13208 pNext = pNext->next;
13211 /* Insert node. */
13212 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13213 if (!pNew)
13214 return false;
13216 relax_group_list_t *tmp = pNext->next;
13217 pNext->next = pNew;
13219 pNew->id = pElem->r_addend;
13220 pNew->relo = pElem;
13221 pNew->next = tmp;
13222 pNew->next_sibling = NULL;
13224 return true;
13228 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13230 relax_group_list_t *pNext = pNode;
13232 /* Find place. */
13233 while (pNext->next_sibling)
13235 pNext = pNext->next_sibling;
13238 /* Insert node. */
13239 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13240 if (!pNew)
13241 return false;
13243 relax_group_list_t *tmp = pNext->next_sibling;
13244 pNext->next_sibling = pNew;
13246 pNew->id = -1;
13247 pNew->relo = pElem;
13248 pNew->next = NULL;
13249 pNew->next_sibling = tmp;
13251 return true;
13254 void
13255 dump_chain (relax_group_list_t *pHead)
13257 relax_group_list_t *pNext = pHead->next;
13258 while (pNext)
13260 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13261 relax_group_list_t *pNextSib = pNext->next_sibling;
13262 while (pNextSib)
13264 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13265 pNextSib = pNextSib->next_sibling;
13267 pNext = pNext->next;
13268 printf("\n");
13272 /* Check R_NDS32_RELAX_GROUP of each section.
13273 There might be multiple sections in one object file. */
13276 elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13278 elf32_nds32_relax_group_t *relax_group_ptr =
13279 elf32_nds32_relax_group_ptr (abfd);
13281 int min_id = relax_group_ptr->min_id;
13282 int max_id = relax_group_ptr->max_id;
13284 Elf_Internal_Rela *rel;
13285 Elf_Internal_Rela *relend;
13286 Elf_Internal_Rela *relocs;
13287 enum elf_nds32_reloc_type rtype;
13291 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13292 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13293 true /* keep_memory */);
13294 if (relocs == NULL)
13295 break;
13297 /* Check R_NDS32_RELAX_GROUP. */
13298 relend = relocs + asec->reloc_count;
13299 for (rel = relocs; rel < relend; rel++)
13301 int id;
13302 rtype = ELF32_R_TYPE (rel->r_info);
13303 if (rtype != R_NDS32_RELAX_GROUP)
13304 continue;
13306 id = rel->r_addend;
13307 if (id < min_id)
13308 min_id = id;
13309 else if (id > max_id)
13310 max_id = id;
13313 while (false);
13315 if (elf_section_data (asec)->relocs != relocs)
13316 free (relocs);
13318 if ((min_id != relax_group_ptr->min_id)
13319 || (max_id != relax_group_ptr->max_id))
13321 relax_group_ptr->count = max_id - min_id + 1;
13322 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13323 relax_group_ptr->min_id = min_id;
13324 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13325 relax_group_ptr->max_id = max_id;
13328 return relax_group_ptr->count;
13331 /* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13332 static struct section_id_list_t *relax_group_section_id_list = NULL;
13334 struct section_id_list_t *
13335 elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13337 struct section_id_list_t *result = NULL;
13338 struct section_id_list_t *lst = *lst_ptr;
13340 if (NULL == lst)
13342 result = (struct section_id_list_t *) calloc
13343 (1, sizeof (struct section_id_list_t));
13344 BFD_ASSERT (result); /* Feed me. */
13345 result->id = id;
13346 *lst_ptr = result;
13348 else
13350 struct section_id_list_t *cur = lst;
13351 struct section_id_list_t *prv = NULL;
13352 struct section_id_list_t *sec = NULL;
13354 while (cur)
13356 if (cur->id < id)
13358 prv = cur;
13359 cur = cur->next;
13360 continue;
13363 if (cur->id > id)
13365 cur = NULL; /* To insert after prv. */
13366 sec = cur; /* In case prv == NULL. */
13369 break;
13372 if (NULL == cur)
13374 /* Insert after prv. */
13375 result = (struct section_id_list_t *) calloc
13376 (1, sizeof (struct section_id_list_t));
13377 BFD_ASSERT (result); /* Feed me. */
13378 result->id = id;
13379 if (NULL != prv)
13381 result->next = prv->next;
13382 prv->next = result;
13384 else
13386 *lst_ptr = result;
13387 result->next = sec;
13392 return result;
13396 elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13398 static int next_relax_group_bias = 0;
13400 elf32_nds32_relax_group_t *relax_group_ptr =
13401 elf32_nds32_relax_group_ptr (abfd);
13403 bool result = true;
13404 Elf_Internal_Rela *rel;
13405 Elf_Internal_Rela *relend;
13406 Elf_Internal_Rela *relocs = NULL;
13407 enum elf_nds32_reloc_type rtype;
13408 struct section_id_list_t *node = NULL;
13409 int count = 0;
13413 if (0 == relax_group_ptr->count)
13414 break;
13416 /* Check if this section has been handled. */
13417 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13418 if (NULL == node)
13419 break; /* Hit, the section id has handled. */
13421 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13422 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13423 true /* keep_memory */);
13424 if (relocs == NULL)
13426 BFD_ASSERT (0); /* feed me */
13427 break;
13430 /* Allocate group id bias for this bfd! */
13431 if (0 == relax_group_ptr->init)
13433 relax_group_ptr->bias = next_relax_group_bias;
13434 next_relax_group_bias += relax_group_ptr->count;
13435 relax_group_ptr->init = 1;
13438 /* Reorder relax group groups. */
13439 relend = relocs + asec->reloc_count;
13440 for (rel = relocs; rel < relend; rel++)
13442 rtype = ELF32_R_TYPE(rel->r_info);
13443 if (rtype != R_NDS32_RELAX_GROUP)
13444 continue;
13446 /* Change it. */
13447 rel->r_addend += relax_group_ptr->bias;
13448 /* Debugging count. */
13449 count++;
13452 while (false);
13454 if (elf_section_data (asec)->relocs != relocs)
13455 free (relocs);
13457 return result;
13461 nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13462 struct bfd_link_info *lnkinfo)
13464 bool result = true;
13465 Elf_Internal_Rela *irel;
13466 Elf_Internal_Rela *irelend;
13467 Elf_Internal_Rela *internal_relocs;
13468 unsigned long r_symndx;
13469 enum elf_nds32_reloc_type r_type;
13471 Elf_Internal_Sym *local_syms = NULL;
13472 bfd_byte *contents = NULL;
13474 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13476 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13477 struct elf_link_hash_entry **sym_hashes;
13478 sym_hashes = elf_sym_hashes (inbfd);
13480 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13481 if (bfd_link_relocatable (lnkinfo))
13483 elf32_nds32_unify_relax_group (inbfd, insec);
13484 return result;
13487 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13488 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13489 true /* keep_memory */);
13490 if (internal_relocs == NULL)
13491 goto error_return;
13493 irelend = internal_relocs + insec->reloc_count;
13494 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13495 irelend, R_NDS32_RELAX_ENTRY);
13496 if (irel == irelend)
13497 goto finish;
13499 /* Chain/remove groups. */
13500 for (irel = internal_relocs; irel < irelend; irel++)
13502 r_symndx = ELF32_R_SYM (irel->r_info);
13503 r_type = ELF32_R_TYPE (irel->r_info);
13504 if (r_type != R_NDS32_RELAX_GROUP)
13505 continue;
13507 /* Remove it. */
13508 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13509 /* Chain it now. */
13510 if (!list_insert (&chain, irel))
13511 goto error_return;
13514 /* Collect group relocations. */
13515 /* Presume relocations are sorted. */
13516 relax_group_list_t *pNext = chain.next;
13517 while (pNext)
13519 for (irel = internal_relocs; irel < irelend; irel++)
13521 if (irel->r_offset == pNext->relo->r_offset)
13523 /* Ignore Non-TLS relocation types. */
13524 r_type = ELF32_R_TYPE (irel->r_info);
13525 if ((R_NDS32_TLS_LE_HI20 > r_type)
13526 || (R_NDS32_RELAX_ENTRY == r_type))
13527 continue;
13529 if (!list_insert_sibling (pNext, irel))
13530 goto error_return;
13532 else if (irel->r_offset > pNext->relo->r_offset)
13534 pNext = pNext->next;
13535 if (!pNext)
13536 break;
13538 bfd_vma current_offset = pNext->relo->r_offset;
13539 if (irel->r_offset > current_offset)
13540 irel = internal_relocs; /* restart from head */
13541 else
13542 --irel; /* Check current irel again. */
13543 continue;
13545 else
13547 /* This shouldn't be reached. */
13550 if (pNext)
13551 pNext = pNext->next;
13554 #ifdef DUBUG_VERBOSE
13555 dump_chain(&chain);
13556 #endif
13558 /* Get symbol table and section content. */
13559 if (incontents)
13560 contents = incontents;
13561 else if (!nds32_get_section_contents (inbfd, insec, &contents, true)
13562 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13563 goto error_return;
13565 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13567 /* Convert TLS model each group if necessary. */
13568 pNext = chain.next;
13570 int cur_grp_id = -1;
13571 int sethi_rt = -1;
13572 int add_rt = -1;
13573 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13575 tls_type = org_tls_type = eff_tls_type = 0;
13577 while (pNext)
13579 relax_group_list_t *pNextSig = pNext->next_sibling;
13580 while (pNextSig)
13582 struct elf_link_hash_entry *h = NULL;
13584 irel = pNextSig->relo;
13585 r_symndx = ELF32_R_SYM(irel->r_info);
13586 r_type = ELF32_R_TYPE(irel->r_info);
13588 if (pNext->id != cur_grp_id)
13590 cur_grp_id = pNext->id;
13591 org_tls_type = get_tls_type (r_type, NULL);
13592 if (r_symndx >= symtab_hdr->sh_info)
13594 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13595 while (h->root.type == bfd_link_hash_indirect
13596 || h->root.type == bfd_link_hash_warning)
13597 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13598 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13600 else
13602 tls_type = local_got_tls_type
13603 ? local_got_tls_type[r_symndx]
13604 : GOT_NORMAL;
13607 eff_tls_type = 1 << (fls (tls_type) - 1);
13608 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13611 if (eff_tls_type != org_tls_type)
13613 switch (org_tls_type)
13615 /* DESC to IEGP/IE/LE. */
13616 case GOT_TLS_DESC:
13617 switch (eff_tls_type)
13619 case GOT_TLS_IE:
13620 switch (r_type)
13622 case R_NDS32_TLS_DESC_HI20:
13623 irel->r_info = ELF32_R_INFO(r_symndx,
13624 R_NDS32_TLS_IE_HI20);
13625 break;
13626 case R_NDS32_TLS_DESC_LO12:
13627 irel->r_info = ELF32_R_INFO(r_symndx,
13628 R_NDS32_TLS_IE_LO12);
13629 break;
13630 case R_NDS32_TLS_DESC_ADD:
13632 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13633 add_rt = N32_RT5 (insn);
13634 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13635 bfd_putb32 (insn, contents + irel->r_offset);
13637 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13639 break;
13640 case R_NDS32_TLS_DESC_FUNC:
13641 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13642 irel->r_info = ELF32_R_INFO(r_symndx,
13643 R_NDS32_RELAX_REMOVE);
13644 break;
13645 case R_NDS32_TLS_DESC_CALL:
13647 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13648 REG_TP);
13649 bfd_putb32 (insn, contents + irel->r_offset);
13651 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13653 break;
13654 case R_NDS32_LOADSTORE:
13655 case R_NDS32_PTR:
13656 case R_NDS32_PTR_RESOLVED:
13657 case R_NDS32_NONE:
13658 case R_NDS32_LABEL:
13659 break;
13660 default:
13661 BFD_ASSERT(0);
13662 break;
13664 break;
13665 case GOT_TLS_IEGP:
13666 switch (r_type)
13668 case R_NDS32_TLS_DESC_HI20:
13669 irel->r_info = ELF32_R_INFO(r_symndx,
13670 R_NDS32_TLS_IEGP_HI20);
13671 break;
13672 case R_NDS32_TLS_DESC_LO12:
13673 irel->r_info = ELF32_R_INFO(r_symndx,
13674 R_NDS32_TLS_IEGP_LO12);
13675 break;
13676 case R_NDS32_TLS_DESC_ADD:
13678 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13679 add_rt = N32_RT5 (insn);
13680 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13681 bfd_putb32 (insn, contents + irel->r_offset);
13683 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13685 break;
13686 case R_NDS32_TLS_DESC_FUNC:
13687 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13688 irel->r_info = ELF32_R_INFO(r_symndx,
13689 R_NDS32_RELAX_REMOVE);
13690 break;
13691 case R_NDS32_TLS_DESC_CALL:
13693 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13694 REG_TP);
13695 bfd_putb32 (insn, contents + irel->r_offset);
13697 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13699 break;
13700 case R_NDS32_LOADSTORE:
13701 case R_NDS32_PTR:
13702 case R_NDS32_PTR_RESOLVED:
13703 case R_NDS32_NONE:
13704 case R_NDS32_LABEL:
13705 break;
13706 default:
13707 BFD_ASSERT(0);
13708 break;
13710 break;
13711 case GOT_TLS_LE:
13712 switch (r_type)
13714 case R_NDS32_TLS_DESC_HI20:
13715 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13716 break;
13717 case R_NDS32_TLS_DESC_LO12:
13718 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13719 break;
13720 case R_NDS32_TLS_DESC_ADD:
13722 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13724 add_rt = N32_RT5 (insn);
13725 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13726 bfd_putb32 (insn, contents + irel->r_offset);
13728 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13730 break;
13731 case R_NDS32_TLS_DESC_FUNC:
13732 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13733 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13734 break;
13735 case R_NDS32_TLS_DESC_CALL:
13736 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13737 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13738 break;
13739 case R_NDS32_LOADSTORE:
13740 case R_NDS32_PTR:
13741 case R_NDS32_PTR_RESOLVED:
13742 case R_NDS32_NONE:
13743 case R_NDS32_LABEL:
13744 break;
13745 default:
13746 BFD_ASSERT(0);
13747 break;
13749 break;
13750 default:
13751 break;
13753 break;
13754 /* IEGP to IE/LE. */
13755 case GOT_TLS_IEGP:
13756 switch (eff_tls_type)
13758 case GOT_TLS_IE:
13759 switch (r_type)
13761 case R_NDS32_TLS_IEGP_HI20:
13762 irel->r_info = ELF32_R_INFO(r_symndx,
13763 R_NDS32_TLS_IE_HI20);
13764 break;
13765 case R_NDS32_TLS_IEGP_LO12:
13766 irel->r_info = ELF32_R_INFO(r_symndx,
13767 R_NDS32_TLS_IE_LO12);
13768 break;
13769 case R_NDS32_PTR_RESOLVED:
13771 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13773 add_rt = N32_RT5 (insn);
13774 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13775 bfd_putb32 (insn, contents + irel->r_offset);
13777 break;
13778 case R_NDS32_TLS_IEGP_LW:
13779 break;
13780 case R_NDS32_LOADSTORE:
13781 case R_NDS32_PTR:
13782 case R_NDS32_NONE:
13783 case R_NDS32_LABEL:
13784 break;
13785 default:
13786 BFD_ASSERT(0);
13787 break;
13789 break;
13790 case GOT_TLS_LE:
13791 switch (r_type)
13793 case R_NDS32_TLS_IEGP_HI20:
13794 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13795 break;
13796 case R_NDS32_TLS_IEGP_LO12:
13797 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13798 break;
13799 case R_NDS32_TLS_IEGP_LW:
13800 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13801 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13802 break;
13803 case R_NDS32_LOADSTORE:
13804 case R_NDS32_PTR:
13805 case R_NDS32_NONE:
13806 case R_NDS32_LABEL:
13807 case R_NDS32_PTR_RESOLVED:
13808 break;
13809 default:
13810 BFD_ASSERT(0);
13811 break;
13813 break;
13814 default:
13815 break;
13817 break;
13818 /* IE to LE. */
13819 case GOT_TLS_IE:
13820 switch (eff_tls_type)
13822 case GOT_TLS_LE:
13823 switch (r_type)
13825 case R_NDS32_TLS_IE_HI20:
13826 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13827 break;
13828 case R_NDS32_TLS_IE_LO12S2:
13830 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13832 add_rt = N32_RT5 (insn);
13833 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13834 bfd_putb32 (insn, contents + irel->r_offset);
13836 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13838 break;
13839 case R_NDS32_LOADSTORE:
13840 case R_NDS32_PTR:
13841 case R_NDS32_NONE:
13842 case R_NDS32_LABEL:
13843 break;
13844 default:
13845 BFD_ASSERT(0);
13846 break;
13848 break;
13849 default:
13850 break;
13852 break;
13853 default:
13854 break;
13857 pNextSig = pNextSig->next_sibling;
13860 #if 1
13861 pNext = pNext->next;
13862 #else
13863 while (pNext)
13865 if (pNext->id != cur_grp_id)
13866 break;
13867 pNext = pNext->next;
13869 #endif
13872 finish:
13873 if (incontents)
13874 contents = NULL;
13876 if (elf_section_data (insec)->relocs != internal_relocs)
13877 free (internal_relocs);
13879 if (elf_section_data (insec)->this_hdr.contents != contents)
13880 free (contents);
13882 if (symtab_hdr->contents != (bfd_byte *) local_syms)
13883 free (local_syms);
13885 if (chain.next)
13887 pNext = chain.next;
13888 relax_group_list_t *pDel;
13889 while (pNext)
13891 pDel = pNext;
13892 pNext = pNext->next;
13893 free (pDel);
13897 return result;
13899 error_return:
13900 result = false;
13901 goto finish;
13904 /* End TLS model conversion. */
13906 #define ELF_ARCH bfd_arch_nds32
13907 #define ELF_MACHINE_CODE EM_NDS32
13908 #define ELF_MAXPAGESIZE 0x1000
13909 #define ELF_TARGET_ID NDS32_ELF_DATA
13911 #define TARGET_BIG_SYM nds32_elf32_be_vec
13912 #define TARGET_BIG_NAME "elf32-nds32be"
13913 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
13914 #define TARGET_LITTLE_NAME "elf32-nds32le"
13916 #define elf_info_to_howto nds32_info_to_howto
13917 #define elf_info_to_howto_rel nds32_info_to_howto_rel
13919 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
13920 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
13921 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
13922 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
13923 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
13925 #define bfd_elf32_mkobject nds32_elf_mkobject
13926 #define elf_backend_action_discarded nds32_elf_action_discarded
13927 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
13928 #define elf_backend_check_relocs nds32_elf_check_relocs
13929 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
13930 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
13931 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
13932 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
13933 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
13934 #define elf_backend_relocate_section nds32_elf_relocate_section
13935 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
13936 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
13937 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
13938 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
13939 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
13940 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
13941 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
13942 #define elf_backend_object_p nds32_elf_object_p
13943 #define elf_backend_final_write_processing nds32_elf_final_write_processing
13944 #define elf_backend_special_sections nds32_elf_special_sections
13945 #define elf_backend_section_flags nds32_elf_section_flags
13946 #define bfd_elf32_bfd_get_relocated_section_contents \
13947 nds32_elf_get_relocated_section_contents
13948 #define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
13949 #define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
13951 #define elf_backend_can_gc_sections 1
13952 #define elf_backend_can_refcount 1
13953 #define elf_backend_want_got_plt 1
13954 #define elf_backend_plt_readonly 1
13955 #define elf_backend_want_plt_sym 0
13956 #define elf_backend_got_header_size 12
13957 #define elf_backend_may_use_rel_p 1
13958 #define elf_backend_default_use_rela_p 1
13959 #define elf_backend_may_use_rela_p 1
13960 #define elf_backend_dtrel_excludes_plt 0
13962 #include "elf32-target.h"
13964 #undef ELF_MAXPAGESIZE
13965 #define ELF_MAXPAGESIZE 0x2000
13967 #undef TARGET_BIG_SYM
13968 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
13969 #undef TARGET_BIG_NAME
13970 #define TARGET_BIG_NAME "elf32-nds32be-linux"
13971 #undef TARGET_LITTLE_SYM
13972 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
13973 #undef TARGET_LITTLE_NAME
13974 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
13975 #undef elf32_bed
13976 #define elf32_bed elf32_nds32_lin_bed
13978 #include "elf32-target.h"