elf32-nds32: Don't define fls if it is provided by the system
[external/binutils.git] / bfd / elf32-nds32.c
1 /* NDS32-specific support for 32-bit ELF.
2    Copyright (C) 2012-2018 Free Software Foundation, Inc.
3    Contributed by Andes Technology Corporation.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
21
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfd_stdint.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "libiberty.h"
30 #include "bfd_stdint.h"
31 #include "elf/nds32.h"
32 #include "opcode/nds32.h"
33 #include "elf32-nds32.h"
34 #include "opcode/cgen.h"
35 #include "../opcodes/nds32-opc.h"
36
37 /* Relocation HOWTO functions.  */
38 static bfd_reloc_status_type nds32_elf_ignore_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type nds32_elf_hi20_reloc
43   (bfd *, arelent *, asymbol *, void *,
44    asection *, bfd *, char **);
45 static bfd_reloc_status_type nds32_elf_lo12_reloc
46   (bfd *, arelent *, asymbol *, void *,
47    asection *, bfd *, char **);
48 static bfd_reloc_status_type nds32_elf_generic_reloc
49   (bfd *, arelent *, asymbol *, void *,
50    asection *, bfd *, char **);
51 static bfd_reloc_status_type nds32_elf_sda15_reloc
52   (bfd *, arelent *, asymbol *, void *,
53    asection *, bfd *, char **);
54
55 /* Helper functions for HOWTO.  */
56 static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
57   (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
58    asection *, bfd_vma, bfd_vma);
59
60 /* Nds32 helper functions.  */
61 static bfd_vma calculate_memory_address
62   (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
63 static int nds32_get_section_contents (bfd *, asection *,
64                                        bfd_byte **, bfd_boolean);
65 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
66                                  Elf_Internal_Sym **);
67 static bfd_boolean  nds32_relax_fp_as_gp
68   (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
69    Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
70    Elf_Internal_Sym *isymbuf);
71 static bfd_boolean nds32_fag_remove_unused_fpbase
72   (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
73    Elf_Internal_Rela *irelend);
74
75 enum
76 {
77   MACH_V1 = bfd_mach_n1h,
78   MACH_V2 = bfd_mach_n1h_v2,
79   MACH_V3 = bfd_mach_n1h_v3,
80   MACH_V3M = bfd_mach_n1h_v3m
81 };
82
83 #define MIN(a, b) ((a) > (b) ? (b) : (a))
84 #define MAX(a, b) ((a) > (b) ? (a) : (b))
85
86 /* The name of the dynamic interpreter.  This is put in the .interp
87    section.  */
88 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
89
90 #define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
91                                   && (flags) & SEC_LOAD \
92                                   && (flags) & SEC_READONLY)
93
94 /* The nop opcode we use.  */
95 #define NDS32_NOP32 0x40000009
96 #define NDS32_NOP16 0x9200
97
98 /* The size in bytes of an entry in the procedure linkage table.  */
99 #define PLT_ENTRY_SIZE 24
100 #define PLT_HEADER_SIZE 24
101
102 /* The first entry in a procedure linkage table are reserved,
103    and the initial contents are unimportant (we zero them out).
104    Subsequent entries look like this.  */
105 #define PLT0_ENTRY_WORD0  0x46f00000            /* sethi   r15, HI20(.got+4)      */
106 #define PLT0_ENTRY_WORD1  0x58f78000            /* ori     r15, r25, LO12(.got+4) */
107 #define PLT0_ENTRY_WORD2  0x05178000            /* lwi     r17, [r15+0]           */
108 #define PLT0_ENTRY_WORD3  0x04f78001            /* lwi     r15, [r15+4]           */
109 #define PLT0_ENTRY_WORD4  0x4a003c00            /* jr      r15                    */
110
111 /* $ta is change to $r15 (from $r25).  */
112 #define PLT0_PIC_ENTRY_WORD0  0x46f00000        /* sethi   r15, HI20(got[1]@GOT)  */
113 #define PLT0_PIC_ENTRY_WORD1  0x58f78000        /* ori     r15, r15, LO12(got[1]@GOT) */
114 #define PLT0_PIC_ENTRY_WORD2  0x40f7f400        /* add     r15, gp, r15           */
115 #define PLT0_PIC_ENTRY_WORD3  0x05178000        /* lwi     r17, [r15+0]           */
116 #define PLT0_PIC_ENTRY_WORD4  0x04f78001        /* lwi     r15, [r15+4]           */
117 #define PLT0_PIC_ENTRY_WORD5  0x4a003c00        /* jr      r15                    */
118
119 #define PLT_ENTRY_WORD0  0x46f00000             /* sethi   r15, HI20(&got[n+3])      */
120 #define PLT_ENTRY_WORD1  0x04f78000             /* lwi     r15, r15, LO12(&got[n+3]) */
121 #define PLT_ENTRY_WORD2  0x4a003c00             /* jr      r15                       */
122 #define PLT_ENTRY_WORD3  0x45000000             /* movi    r16, sizeof(RELA) * n     */
123 #define PLT_ENTRY_WORD4  0x48000000             /* j      .plt0.                     */
124
125 #define PLT_PIC_ENTRY_WORD0  0x46f00000         /* sethi  r15, HI20(got[n+3]@GOT)    */
126 #define PLT_PIC_ENTRY_WORD1  0x58f78000         /* ori    r15, r15,    LO12(got[n+3]@GOT) */
127 #define PLT_PIC_ENTRY_WORD2  0x38febc02         /* lw     r15, [gp+r15]              */
128 #define PLT_PIC_ENTRY_WORD3  0x4a003c00         /* jr     r15                        */
129 #define PLT_PIC_ENTRY_WORD4  0x45000000         /* movi   r16, sizeof(RELA) * n      */
130 #define PLT_PIC_ENTRY_WORD5  0x48000000         /* j      .plt0                      */
131
132 /* These are macros used to get the relocation accurate value.  */
133 #define ACCURATE_8BIT_S1        (0x100)
134 #define ACCURATE_U9BIT_S1       (0x400)
135 #define ACCURATE_12BIT_S1       (0x2000)
136 #define ACCURATE_14BIT_S1       (0x4000)
137 #define ACCURATE_19BIT          (0x40000)
138
139 /* These are macros used to get the relocation conservative value.  */
140 #define CONSERVATIVE_8BIT_S1    (0x100 - 4)
141 #define CONSERVATIVE_14BIT_S1   (0x4000 - 4)
142 #define CONSERVATIVE_16BIT_S1   (0x10000 - 4)
143 #define CONSERVATIVE_24BIT_S1   (0x1000000 - 4)
144 /* These must be more conservative because the address may be in
145    different segment.  */
146 #define CONSERVATIVE_15BIT      (0x4000 - 0x1000)
147 #define CONSERVATIVE_15BIT_S1   (0x8000 - 0x1000)
148 #define CONSERVATIVE_15BIT_S2   (0x10000 - 0x1000)
149 #define CONSERVATIVE_19BIT      (0x40000 - 0x1000)
150 #define CONSERVATIVE_20BIT      (0x80000 - 0x1000)
151
152 /* Size of small data/bss sections, used to calculate SDA_BASE.  */
153 static long got_size = 0;
154 static int is_SDA_BASE_set = 0;
155
156 /* Convert ELF-VER in eflags to string for debugging purpose.  */
157 static const char *const nds32_elfver_strtab[] =
158 {
159   "ELF-1.2",
160   "ELF-1.3",
161   "ELF-1.4",
162 };
163
164 /* The nds32 linker needs to keep track of the number of relocs that it
165    decides to copy in check_relocs for each symbol.  This is so that
166    it can discard PC relative relocs if it doesn't need them when
167    linking with -Bsymbolic.  We store the information in a field
168    extending the regular ELF linker hash table.  */
169
170 /* This structure keeps track of the number of PC relative relocs we
171    have copied for a given symbol.  */
172
173 struct elf_nds32_pcrel_relocs_copied
174 {
175   /* Next section.  */
176   struct elf_nds32_pcrel_relocs_copied *next;
177   /* A section in dynobj.  */
178   asection *section;
179   /* Number of relocs copied in this section.  */
180   bfd_size_type count;
181 };
182
183 enum elf_nds32_tls_type
184 {
185   GOT_UNKNOWN = (0),
186   GOT_NORMAL = (1 << 0),
187   GOT_TLS_LE = (1 << 1),
188   GOT_TLS_IE = (1 << 2),
189   GOT_TLS_IEGP = (1 << 3),
190   GOT_TLS_LD = (1 << 4),
191   GOT_TLS_GD = (1 << 5),
192   GOT_TLS_DESC = (1 << 6),
193 };
194
195 /* Nds32 ELF linker hash entry.  */
196
197 struct elf_nds32_link_hash_entry
198 {
199   struct elf_link_hash_entry root;
200
201   /* Track dynamic relocs copied for this symbol.  */
202   struct elf_dyn_relocs *dyn_relocs;
203
204   /* For checking relocation type.  */
205   enum elf_nds32_tls_type tls_type;
206
207   int offset_to_gp;
208 };
209
210 /* Get the nds32 ELF linker hash table from a link_info structure.  */
211
212 #define FP_BASE_NAME "_FP_BASE_"
213 static int check_start_export_sym = 0;
214
215 /* The offset for executable tls relaxation.  */
216 #define TP_OFFSET 0x0
217
218 typedef struct
219 {
220   int min_id;
221   int max_id;
222   int count;
223   int bias;
224   int init;
225 } elf32_nds32_relax_group_t;
226
227 struct elf_nds32_obj_tdata
228 {
229   struct elf_obj_tdata root;
230
231   /* tls_type for each local got entry.  */
232   char *local_got_tls_type;
233
234   /* GOTPLT entries for TLS descriptors.  */
235   bfd_vma *local_tlsdesc_gotent;
236
237   /* for R_NDS32_RELAX_GROUP handling.  */
238   elf32_nds32_relax_group_t relax_group;
239
240   unsigned int hdr_size;
241   int* offset_to_gp;
242 };
243
244 #define elf_nds32_tdata(bfd) \
245   ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
246
247 #define elf32_nds32_local_got_tls_type(bfd) \
248   (elf_nds32_tdata (bfd)->local_got_tls_type)
249
250 #define elf32_nds32_local_gp_offset(bfd) \
251   (elf_nds32_tdata (bfd)->offset_to_gp)
252
253 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
254
255 #define elf32_nds32_relax_group_ptr(bfd) \
256   &(elf_nds32_tdata (bfd)->relax_group)
257
258 static bfd_boolean
259 nds32_elf_mkobject (bfd *abfd)
260 {
261   return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
262                                   NDS32_ELF_DATA);
263 }
264
265 /* Relocations used for relocation.  */
266 /* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
267    initialize array nds32_elf_howto_table in any order. The benefit
268    is that we can add any new relocations with any numbers and don't
269    need to fill the gap by lots of EMPTY_HOWTO. */
270 #define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
271   [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
272
273 static reloc_howto_type nds32_elf_howto_table[] =
274 {
275   /* This reloc does nothing.  */
276   HOWTO2 (R_NDS32_NONE,         /* type  */
277          0,                     /* rightshift  */
278          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
279          32,                    /* bitsize  */
280          FALSE,                 /* pc_relative  */
281          0,                     /* bitpos  */
282          complain_overflow_bitfield,/* complain_on_overflow  */
283          bfd_elf_generic_reloc, /* special_function  */
284          "R_NDS32_NONE",        /* name  */
285          FALSE,                 /* partial_inplace  */
286          0,                     /* src_mask  */
287          0,                     /* dst_mask  */
288          FALSE),                /* pcrel_offset  */
289
290   /* A 16 bit absolute relocation.  */
291   HOWTO2 (R_NDS32_16,           /* type  */
292          0,                     /* rightshift  */
293          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
294          16,                    /* bitsize  */
295          FALSE,                 /* pc_relative  */
296          0,                     /* bitpos  */
297          complain_overflow_bitfield,/* complain_on_overflow  */
298          nds32_elf_generic_reloc,/* special_function  */
299          "R_NDS32_16",          /* name  */
300          FALSE,                 /* partial_inplace  */
301          0xffff,                /* src_mask  */
302          0xffff,                /* dst_mask  */
303          FALSE),                /* pcrel_offset  */
304
305   /* A 32 bit absolute relocation.  */
306   HOWTO2 (R_NDS32_32,           /* type  */
307          0,                     /* rightshift  */
308          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
309          32,                    /* bitsize  */
310          FALSE,                 /* pc_relative  */
311          0,                     /* bitpos  */
312          complain_overflow_bitfield,/* complain_on_overflow  */
313          nds32_elf_generic_reloc,/* special_function  */
314          "R_NDS32_32",          /* name  */
315          FALSE,                 /* partial_inplace  */
316          0xffffffff,            /* src_mask  */
317          0xffffffff,            /* dst_mask  */
318          FALSE),                /* pcrel_offset  */
319
320   /* A 20 bit address.  */
321   HOWTO2 (R_NDS32_20,           /* type  */
322          0,                     /* rightshift  */
323          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
324          20,                    /* bitsize  */
325          FALSE,                 /* pc_relative  */
326          0,                     /* bitpos  */
327          complain_overflow_unsigned,/* complain_on_overflow  */
328          nds32_elf_generic_reloc,/* special_function  */
329          "R_NDS32_20",          /* name  */
330          FALSE,                 /* partial_inplace  */
331          0xfffff,               /* src_mask  */
332          0xfffff,               /* dst_mask  */
333          FALSE),                /* pcrel_offset  */
334
335   /* An PC Relative 9-bit relocation, shifted by 2.
336      This reloc is complicated because relocations are relative to pc & -4.
337      i.e. branches in the right insn slot use the address of the left insn
338      slot for pc.  */
339   /* It's not clear whether this should have partial_inplace set or not.
340      Branch relaxing in the assembler can store the addend in the insn,
341      and if bfd_install_relocation gets called the addend may get added
342      again.  */
343   HOWTO2 (R_NDS32_9_PCREL,      /* type  */
344          1,                     /* rightshift  */
345          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
346          8,                     /* bitsize  */
347          TRUE,                  /* pc_relative  */
348          0,                     /* bitpos  */
349          complain_overflow_signed,/* complain_on_overflow  */
350          nds32_elf_9_pcrel_reloc,/* special_function  */
351          "R_NDS32_9_PCREL",     /* name  */
352          FALSE,                 /* partial_inplace  */
353          0xff,                  /* src_mask  */
354          0xff,                  /* dst_mask  */
355          TRUE),                 /* pcrel_offset  */
356
357   /* A relative 15 bit relocation, right shifted by 1.  */
358   HOWTO2 (R_NDS32_15_PCREL,     /* type  */
359          1,                     /* rightshift  */
360          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
361          14,                    /* bitsize  */
362          TRUE,                  /* pc_relative  */
363          0,                     /* bitpos  */
364          complain_overflow_signed,/* complain_on_overflow  */
365          bfd_elf_generic_reloc, /* special_function  */
366          "R_NDS32_15_PCREL",    /* name  */
367          FALSE,                 /* partial_inplace  */
368          0x3fff,                /* src_mask  */
369          0x3fff,                /* dst_mask  */
370          TRUE),                 /* pcrel_offset  */
371
372   /* A relative 17 bit relocation, right shifted by 1.  */
373   HOWTO2 (R_NDS32_17_PCREL,     /* type  */
374          1,                     /* rightshift  */
375          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
376          16,                    /* bitsize  */
377          TRUE,                  /* pc_relative  */
378          0,                     /* bitpos  */
379          complain_overflow_signed,/* complain_on_overflow  */
380          bfd_elf_generic_reloc, /* special_function  */
381          "R_NDS32_17_PCREL",    /* name  */
382          FALSE,                 /* partial_inplace  */
383          0xffff,                /* src_mask  */
384          0xffff,                /* dst_mask  */
385          TRUE),                 /* pcrel_offset  */
386
387   /* A relative 25 bit relocation, right shifted by 1.  */
388   /* It's not clear whether this should have partial_inplace set or not.
389      Branch relaxing in the assembler can store the addend in the insn,
390      and if bfd_install_relocation gets called the addend may get added
391      again.  */
392   HOWTO2 (R_NDS32_25_PCREL,     /* type  */
393          1,                     /* rightshift  */
394          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
395          24,                    /* bitsize  */
396          TRUE,                  /* pc_relative  */
397          0,                     /* bitpos  */
398          complain_overflow_signed,/* complain_on_overflow  */
399          bfd_elf_generic_reloc, /* special_function  */
400          "R_NDS32_25_PCREL",    /* name  */
401          FALSE,                 /* partial_inplace  */
402          0xffffff,              /* src_mask  */
403          0xffffff,              /* dst_mask  */
404          TRUE),                 /* pcrel_offset  */
405
406   /* High 20 bits of address when lower 12 is or'd in.  */
407   HOWTO2 (R_NDS32_HI20,         /* type  */
408          12,                    /* rightshift  */
409          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
410          20,                    /* bitsize  */
411          FALSE,                 /* pc_relative  */
412          0,                     /* bitpos  */
413          complain_overflow_dont,/* complain_on_overflow  */
414          nds32_elf_hi20_reloc,  /* special_function  */
415          "R_NDS32_HI20",        /* name  */
416          FALSE,                 /* partial_inplace  */
417          0x000fffff,            /* src_mask  */
418          0x000fffff,            /* dst_mask  */
419          FALSE),                /* pcrel_offset  */
420
421   /* Lower 12 bits of address.  */
422   HOWTO2 (R_NDS32_LO12S3,       /* type  */
423          3,                     /* rightshift  */
424          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
425          9,                     /* bitsize  */
426          FALSE,                 /* pc_relative  */
427          0,                     /* bitpos  */
428          complain_overflow_dont,/* complain_on_overflow  */
429          nds32_elf_lo12_reloc,  /* special_function  */
430          "R_NDS32_LO12S3",      /* name  */
431          FALSE,                 /* partial_inplace  */
432          0x000001ff,            /* src_mask  */
433          0x000001ff,            /* dst_mask  */
434          FALSE),                /* pcrel_offset  */
435
436   /* Lower 12 bits of address.  */
437   HOWTO2 (R_NDS32_LO12S2,       /* type  */
438          2,                     /* rightshift  */
439          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
440          10,                    /* bitsize  */
441          FALSE,                 /* pc_relative  */
442          0,                     /* bitpos  */
443          complain_overflow_dont,/* complain_on_overflow  */
444          nds32_elf_lo12_reloc,  /* special_function  */
445          "R_NDS32_LO12S2",      /* name  */
446          FALSE,                 /* partial_inplace  */
447          0x000003ff,            /* src_mask  */
448          0x000003ff,            /* dst_mask  */
449          FALSE),                /* pcrel_offset  */
450
451   /* Lower 12 bits of address.  */
452   HOWTO2 (R_NDS32_LO12S1,       /* type  */
453          1,                     /* rightshift  */
454          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
455          11,                    /* bitsize  */
456          FALSE,                 /* pc_relative  */
457          0,                     /* bitpos  */
458          complain_overflow_dont,/* complain_on_overflow  */
459          nds32_elf_lo12_reloc,  /* special_function  */
460          "R_NDS32_LO12S1",      /* name  */
461          FALSE,                 /* partial_inplace  */
462          0x000007ff,            /* src_mask  */
463          0x000007ff,            /* dst_mask  */
464          FALSE),                /* pcrel_offset  */
465
466   /* Lower 12 bits of address.  */
467   HOWTO2 (R_NDS32_LO12S0,       /* type  */
468          0,                     /* rightshift  */
469          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
470          12,                    /* bitsize  */
471          FALSE,                 /* pc_relative  */
472          0,                     /* bitpos  */
473          complain_overflow_dont,/* complain_on_overflow  */
474          nds32_elf_lo12_reloc,  /* special_function  */
475          "R_NDS32_LO12S0",      /* name  */
476          FALSE,                 /* partial_inplace  */
477          0x00000fff,            /* src_mask  */
478          0x00000fff,            /* dst_mask  */
479          FALSE),                /* pcrel_offset  */
480
481   /* Small data area 15 bits offset.  */
482   HOWTO2 (R_NDS32_SDA15S3,      /* type  */
483          3,                     /* rightshift  */
484          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
485          15,                    /* bitsize  */
486          FALSE,                 /* pc_relative  */
487          0,                     /* bitpos  */
488          complain_overflow_signed,/* complain_on_overflow  */
489          nds32_elf_sda15_reloc, /* special_function  */
490          "R_NDS32_SDA15S3",     /* name  */
491          FALSE,                 /* partial_inplace  */
492          0x00007fff,            /* src_mask  */
493          0x00007fff,            /* dst_mask  */
494          FALSE),                /* pcrel_offset  */
495
496   /* Small data area 15 bits offset.  */
497   HOWTO2 (R_NDS32_SDA15S2,      /* type  */
498          2,                     /* rightshift  */
499          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
500          15,                    /* bitsize  */
501          FALSE,                 /* pc_relative  */
502          0,                     /* bitpos  */
503          complain_overflow_signed,/* complain_on_overflow  */
504          nds32_elf_sda15_reloc, /* special_function  */
505          "R_NDS32_SDA15S2",     /* name  */
506          FALSE,                 /* partial_inplace  */
507          0x00007fff,            /* src_mask  */
508          0x00007fff,            /* dst_mask  */
509          FALSE),                /* pcrel_offset  */
510
511   /* Small data area 15 bits offset.  */
512   HOWTO2 (R_NDS32_SDA15S1,      /* type  */
513          1,                     /* rightshift  */
514          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
515          15,                    /* bitsize  */
516          FALSE,                 /* pc_relative  */
517          0,                     /* bitpos  */
518          complain_overflow_signed,/* complain_on_overflow  */
519          nds32_elf_sda15_reloc, /* special_function  */
520          "R_NDS32_SDA15S1",     /* name  */
521          FALSE,                 /* partial_inplace  */
522          0x00007fff,            /* src_mask  */
523          0x00007fff,            /* dst_mask  */
524          FALSE),                /* pcrel_offset  */
525
526   /* Small data area 15 bits offset.  */
527   HOWTO2 (R_NDS32_SDA15S0,      /* type  */
528          0,                     /* rightshift  */
529          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
530          15,                    /* bitsize  */
531          FALSE,                 /* pc_relative  */
532          0,                     /* bitpos  */
533          complain_overflow_signed,/* complain_on_overflow  */
534          nds32_elf_sda15_reloc, /* special_function  */
535          "R_NDS32_SDA15S0",     /* name  */
536          FALSE,                 /* partial_inplace  */
537          0x00007fff,            /* src_mask  */
538          0x00007fff,            /* dst_mask  */
539          FALSE),                /* pcrel_offset  */
540
541   /* GNU extension to record C++ vtable hierarchy  */
542   HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type  */
543          0,                     /* rightshift  */
544          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
545          0,                     /* bitsize  */
546          FALSE,                 /* pc_relative  */
547          0,                     /* bitpos  */
548          complain_overflow_dont,/* complain_on_overflow  */
549          NULL,                  /* special_function  */
550          "R_NDS32_GNU_VTINHERIT",/* name  */
551          FALSE,                 /* partial_inplace  */
552          0,                     /* src_mask  */
553          0,                     /* dst_mask  */
554          FALSE),                /* pcrel_offset  */
555
556   /* GNU extension to record C++ vtable member usage  */
557   HOWTO2 (R_NDS32_GNU_VTENTRY,  /* type  */
558          0,                     /* rightshift  */
559          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
560          0,                     /* bitsize  */
561          FALSE,                 /* pc_relative  */
562          0,                     /* bitpos  */
563          complain_overflow_dont,/* complain_on_overflow  */
564          _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
565          "R_NDS32_GNU_VTENTRY", /* name  */
566          FALSE,                 /* partial_inplace  */
567          0,                     /* src_mask  */
568          0,                     /* dst_mask  */
569          FALSE),                /* pcrel_offset  */
570
571   /* A 16 bit absolute relocation.  */
572   HOWTO2 (R_NDS32_16_RELA,      /* type  */
573          0,                     /* rightshift  */
574          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
575          16,                    /* bitsize  */
576          FALSE,                 /* pc_relative  */
577          0,                     /* bitpos  */
578          complain_overflow_bitfield,/* complain_on_overflow  */
579          bfd_elf_generic_reloc, /* special_function  */
580          "R_NDS32_16_RELA",     /* name  */
581          FALSE,                 /* partial_inplace  */
582          0xffff,                /* src_mask  */
583          0xffff,                /* dst_mask  */
584          FALSE),                /* pcrel_offset  */
585
586   /* A 32 bit absolute relocation.  */
587   HOWTO2 (R_NDS32_32_RELA,      /* type  */
588          0,                     /* rightshift  */
589          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
590          32,                    /* bitsize  */
591          FALSE,                 /* pc_relative  */
592          0,                     /* bitpos  */
593          complain_overflow_bitfield,/* complain_on_overflow  */
594          bfd_elf_generic_reloc, /* special_function  */
595          "R_NDS32_32_RELA",     /* name  */
596          FALSE,                 /* partial_inplace  */
597          0xffffffff,            /* src_mask  */
598          0xffffffff,            /* dst_mask  */
599          FALSE),                /* pcrel_offset  */
600
601   /* A 20 bit address.  */
602   HOWTO2 (R_NDS32_20_RELA,      /* type  */
603          0,                     /* rightshift  */
604          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
605          20,                    /* bitsize  */
606          FALSE,                 /* pc_relative  */
607          0,                     /* bitpos  */
608          complain_overflow_signed,/* complain_on_overflow  */
609          bfd_elf_generic_reloc, /* special_function  */
610          "R_NDS32_20_RELA",     /* name  */
611          FALSE,                 /* partial_inplace  */
612          0xfffff,               /* src_mask  */
613          0xfffff,               /* dst_mask  */
614          FALSE),                /* pcrel_offset  */
615
616   HOWTO2 (R_NDS32_9_PCREL_RELA, /* type  */
617          1,                     /* rightshift  */
618          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
619          8,                     /* bitsize  */
620          TRUE,                  /* pc_relative  */
621          0,                     /* bitpos  */
622          complain_overflow_signed,/* complain_on_overflow  */
623          bfd_elf_generic_reloc, /* special_function  */
624          "R_NDS32_9_PCREL_RELA",/* name  */
625          FALSE,                 /* partial_inplace  */
626          0xff,                  /* src_mask  */
627          0xff,                  /* dst_mask  */
628          TRUE),                 /* pcrel_offset  */
629
630   /* A relative 15 bit relocation, right shifted by 1.  */
631   HOWTO2 (R_NDS32_15_PCREL_RELA,/* type  */
632          1,                     /* rightshift  */
633          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
634          14,                    /* bitsize  */
635          TRUE,                  /* pc_relative  */
636          0,                     /* bitpos  */
637          complain_overflow_signed,/* complain_on_overflow  */
638          bfd_elf_generic_reloc, /* special_function  */
639          "R_NDS32_15_PCREL_RELA",/* name  */
640          FALSE,                 /* partial_inplace  */
641          0x3fff,                /* src_mask  */
642          0x3fff,                /* dst_mask  */
643          TRUE),                 /* pcrel_offset  */
644
645   /* A relative 17 bit relocation, right shifted by 1.  */
646   HOWTO2 (R_NDS32_17_PCREL_RELA,/* type  */
647          1,                     /* rightshift  */
648          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
649          16,                    /* bitsize  */
650          TRUE,                  /* pc_relative  */
651          0,                     /* bitpos  */
652          complain_overflow_signed,/* complain_on_overflow  */
653          bfd_elf_generic_reloc, /* special_function  */
654          "R_NDS32_17_PCREL_RELA",/* name  */
655          FALSE,                 /* partial_inplace  */
656          0xffff,                /* src_mask  */
657          0xffff,                /* dst_mask  */
658          TRUE),                 /* pcrel_offset  */
659
660   /* A relative 25 bit relocation, right shifted by 2.  */
661   HOWTO2 (R_NDS32_25_PCREL_RELA,/* type  */
662          1,                     /* rightshift  */
663          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
664          24,                    /* bitsize  */
665          TRUE,                  /* pc_relative  */
666          0,                     /* bitpos  */
667          complain_overflow_signed,/* complain_on_overflow  */
668          bfd_elf_generic_reloc, /* special_function  */
669          "R_NDS32_25_PCREL_RELA",/* name  */
670          FALSE,                 /* partial_inplace  */
671          0xffffff,              /* src_mask  */
672          0xffffff,              /* dst_mask  */
673          TRUE),                 /* pcrel_offset  */
674
675   /* High 20 bits of address when lower 16 is or'd in.  */
676   HOWTO2 (R_NDS32_HI20_RELA,    /* type  */
677          12,                    /* rightshift  */
678          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
679          20,                    /* bitsize  */
680          FALSE,                 /* pc_relative  */
681          0,                     /* bitpos  */
682          complain_overflow_dont,/* complain_on_overflow  */
683          bfd_elf_generic_reloc, /* special_function  */
684          "R_NDS32_HI20_RELA",   /* name  */
685          FALSE,                 /* partial_inplace  */
686          0x000fffff,            /* src_mask  */
687          0x000fffff,            /* dst_mask  */
688          FALSE),                /* pcrel_offset  */
689
690   /* Lower 12 bits of address.  */
691   HOWTO2 (R_NDS32_LO12S3_RELA,  /* type  */
692          3,                     /* rightshift  */
693          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
694          9,                     /* bitsize  */
695          FALSE,                 /* pc_relative  */
696          0,                     /* bitpos  */
697          complain_overflow_dont,/* complain_on_overflow  */
698          bfd_elf_generic_reloc, /* special_function  */
699          "R_NDS32_LO12S3_RELA", /* name  */
700          FALSE,                 /* partial_inplace  */
701          0x000001ff,            /* src_mask  */
702          0x000001ff,            /* dst_mask  */
703          FALSE),                /* pcrel_offset  */
704
705   /* Lower 12 bits of address.  */
706   HOWTO2 (R_NDS32_LO12S2_RELA,  /* type  */
707          2,                     /* rightshift  */
708          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
709          10,                    /* bitsize  */
710          FALSE,                 /* pc_relative  */
711          0,                     /* bitpos  */
712          complain_overflow_dont,/* complain_on_overflow  */
713          bfd_elf_generic_reloc, /* special_function  */
714          "R_NDS32_LO12S2_RELA", /* name  */
715          FALSE,                 /* partial_inplace  */
716          0x000003ff,            /* src_mask  */
717          0x000003ff,            /* dst_mask  */
718          FALSE),                /* pcrel_offset  */
719
720   /* Lower 12 bits of address.  */
721   HOWTO2 (R_NDS32_LO12S1_RELA,  /* type  */
722          1,                     /* rightshift  */
723          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
724          11,                    /* bitsize  */
725          FALSE,                 /* pc_relative  */
726          0,                     /* bitpos  */
727          complain_overflow_dont,/* complain_on_overflow  */
728          bfd_elf_generic_reloc, /* special_function  */
729          "R_NDS32_LO12S1_RELA", /* name  */
730          FALSE,                 /* partial_inplace  */
731          0x000007ff,            /* src_mask  */
732          0x000007ff,            /* dst_mask  */
733          FALSE),                /* pcrel_offset  */
734
735   /* Lower 12 bits of address.  */
736   HOWTO2 (R_NDS32_LO12S0_RELA,  /* type  */
737          0,                     /* rightshift  */
738          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
739          12,                    /* bitsize  */
740          FALSE,                 /* pc_relative  */
741          0,                     /* bitpos  */
742          complain_overflow_dont,/* complain_on_overflow  */
743          bfd_elf_generic_reloc, /* special_function  */
744          "R_NDS32_LO12S0_RELA", /* name  */
745          FALSE,                 /* partial_inplace  */
746          0x00000fff,            /* src_mask  */
747          0x00000fff,            /* dst_mask  */
748          FALSE),                /* pcrel_offset  */
749
750   /* Small data area 15 bits offset.  */
751   HOWTO2 (R_NDS32_SDA15S3_RELA, /* type  */
752          3,                     /* rightshift  */
753          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
754          15,                    /* bitsize  */
755          FALSE,                 /* pc_relative  */
756          0,                     /* bitpos  */
757          complain_overflow_signed,/* complain_on_overflow  */
758          bfd_elf_generic_reloc, /* special_function  */
759          "R_NDS32_SDA15S3_RELA",/* name  */
760          FALSE,                 /* partial_inplace  */
761          0x00007fff,            /* src_mask  */
762          0x00007fff,            /* dst_mask  */
763          FALSE),                /* pcrel_offset  */
764
765   /* Small data area 15 bits offset.  */
766   HOWTO2 (R_NDS32_SDA15S2_RELA, /* type  */
767          2,                     /* rightshift  */
768          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
769          15,                    /* bitsize  */
770          FALSE,                 /* pc_relative  */
771          0,                     /* bitpos  */
772          complain_overflow_signed,/* complain_on_overflow  */
773          bfd_elf_generic_reloc, /* special_function  */
774          "R_NDS32_SDA15S2_RELA",/* name  */
775          FALSE,                 /* partial_inplace  */
776          0x00007fff,            /* src_mask  */
777          0x00007fff,            /* dst_mask  */
778          FALSE),                /* pcrel_offset  */
779
780   HOWTO2 (R_NDS32_SDA15S1_RELA, /* type  */
781          1,                     /* rightshift  */
782          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
783          15,                    /* bitsize  */
784          FALSE,                 /* pc_relative  */
785          0,                     /* bitpos  */
786          complain_overflow_signed,/* complain_on_overflow  */
787          bfd_elf_generic_reloc, /* special_function  */
788          "R_NDS32_SDA15S1_RELA",/* name  */
789          FALSE,                 /* partial_inplace  */
790          0x00007fff,            /* src_mask  */
791          0x00007fff,            /* dst_mask  */
792          FALSE),                /* pcrel_offset  */
793
794   HOWTO2 (R_NDS32_SDA15S0_RELA, /* type  */
795          0,                     /* rightshift  */
796          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
797          15,                    /* bitsize  */
798          FALSE,                 /* pc_relative  */
799          0,                     /* bitpos  */
800          complain_overflow_signed,/* complain_on_overflow  */
801          bfd_elf_generic_reloc, /* special_function  */
802          "R_NDS32_SDA15S0_RELA",/* name  */
803          FALSE,                 /* partial_inplace  */
804          0x00007fff,            /* src_mask  */
805          0x00007fff,            /* dst_mask  */
806          FALSE),                /* pcrel_offset  */
807
808   /* GNU extension to record C++ vtable hierarchy  */
809   HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type  */
810          0,                     /* rightshift  */
811          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
812          0,                     /* bitsize  */
813          FALSE,                 /* pc_relative  */
814          0,                     /* bitpos  */
815          complain_overflow_dont,/* complain_on_overflow  */
816          NULL,                  /* special_function  */
817          "R_NDS32_RELA_GNU_VTINHERIT",/* name  */
818          FALSE,                 /* partial_inplace  */
819          0,                     /* src_mask  */
820          0,                     /* dst_mask  */
821          FALSE),                /* pcrel_offset  */
822
823   /* GNU extension to record C++ vtable member usage  */
824   HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type  */
825          0,                     /* rightshift  */
826          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
827          0,                     /* bitsize  */
828          FALSE,                 /* pc_relative  */
829          0,                     /* bitpos  */
830          complain_overflow_dont,/* complain_on_overflow  */
831          _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
832          "R_NDS32_RELA_GNU_VTENTRY",/* name  */
833          FALSE,                 /* partial_inplace  */
834          0,                     /* src_mask  */
835          0,                     /* dst_mask  */
836          FALSE),                /* pcrel_offset  */
837
838   /* Like R_NDS32_20, but referring to the GOT table entry for
839      the symbol.  */
840   HOWTO2 (R_NDS32_GOT20,        /* type  */
841          0,                     /* rightshift  */
842          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
843          20,                    /* bitsize  */
844          FALSE,                 /* pc_relative  */
845          0,                     /* bitpos  */
846          complain_overflow_signed,/* complain_on_overflow  */
847          bfd_elf_generic_reloc, /* special_function  */
848          "R_NDS32_GOT20",       /* name  */
849          FALSE,                 /* partial_inplace  */
850          0xfffff,               /* src_mask  */
851          0xfffff,               /* dst_mask  */
852          FALSE),                /* pcrel_offset  */
853
854   /* Like R_NDS32_PCREL, but referring to the procedure linkage table
855      entry for the symbol.  */
856   HOWTO2 (R_NDS32_25_PLTREL,    /* type  */
857          1,                     /* rightshift  */
858          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
859          24,                    /* bitsize  */
860          TRUE,                  /* pc_relative  */
861          0,                     /* bitpos  */
862          complain_overflow_signed,/* complain_on_overflow  */
863          bfd_elf_generic_reloc, /* special_function  */
864          "R_NDS32_25_PLTREL",   /* name  */
865          FALSE,                 /* partial_inplace  */
866          0xffffff,              /* src_mask  */
867          0xffffff,              /* dst_mask  */
868          TRUE),                 /* pcrel_offset  */
869
870   /* This is used only by the dynamic linker.  The symbol should exist
871      both in the object being run and in some shared library.  The
872      dynamic linker copies the data addressed by the symbol from the
873      shared library into the object, because the object being
874      run has to have the data at some particular address.  */
875   HOWTO2 (R_NDS32_COPY,         /* type  */
876          0,                     /* rightshift  */
877          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
878          32,                    /* bitsize  */
879          FALSE,                 /* pc_relative  */
880          0,                     /* bitpos  */
881          complain_overflow_bitfield,/* complain_on_overflow  */
882          bfd_elf_generic_reloc, /* special_function  */
883          "R_NDS32_COPY",        /* name  */
884          FALSE,                 /* partial_inplace  */
885          0xffffffff,            /* src_mask  */
886          0xffffffff,            /* dst_mask  */
887          FALSE),                /* pcrel_offset  */
888
889   /* Like R_NDS32_20, but used when setting global offset table
890      entries.  */
891   HOWTO2 (R_NDS32_GLOB_DAT,     /* type  */
892          0,                     /* rightshift  */
893          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
894          32,                    /* bitsize  */
895          FALSE,                 /* pc_relative  */
896          0,                     /* bitpos  */
897          complain_overflow_bitfield,/* complain_on_overflow  */
898          bfd_elf_generic_reloc, /* special_function  */
899          "R_NDS32_GLOB_DAT",    /* name  */
900          FALSE,                 /* partial_inplace  */
901          0xffffffff,            /* src_mask  */
902          0xffffffff,            /* dst_mask  */
903          FALSE),                /* pcrel_offset  */
904
905   /* Marks a procedure linkage table entry for a symbol.  */
906   HOWTO2 (R_NDS32_JMP_SLOT,     /* type  */
907          0,                     /* rightshift  */
908          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
909          32,                    /* bitsize  */
910          FALSE,                 /* pc_relative  */
911          0,                     /* bitpos  */
912          complain_overflow_bitfield,/* complain_on_overflow  */
913          bfd_elf_generic_reloc, /* special_function  */
914          "R_NDS32_JMP_SLOT",    /* name  */
915          FALSE,                 /* partial_inplace  */
916          0xffffffff,            /* src_mask  */
917          0xffffffff,            /* dst_mask  */
918          FALSE),                /* pcrel_offset  */
919
920   /* Used only by the dynamic linker.  When the object is run, this
921      longword is set to the load address of the object, plus the
922      addend.  */
923   HOWTO2 (R_NDS32_RELATIVE,     /* type  */
924          0,                     /* rightshift  */
925          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
926          32,                    /* bitsize  */
927          FALSE,                 /* pc_relative  */
928          0,                     /* bitpos  */
929          complain_overflow_bitfield,/* complain_on_overflow  */
930          bfd_elf_generic_reloc, /* special_function  */
931          "R_NDS32_RELATIVE",    /* name  */
932          FALSE,                 /* partial_inplace  */
933          0xffffffff,            /* src_mask  */
934          0xffffffff,            /* dst_mask  */
935          FALSE),                /* pcrel_offset  */
936
937   HOWTO2 (R_NDS32_GOTOFF,       /* type  */
938          0,                     /* rightshift  */
939          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
940          20,                    /* bitsize  */
941          FALSE,                 /* pc_relative  */
942          0,                     /* bitpos  */
943          complain_overflow_signed,/* complain_on_overflow  */
944          bfd_elf_generic_reloc, /* special_function  */
945          "R_NDS32_GOTOFF",      /* name  */
946          FALSE,                 /* partial_inplace  */
947          0xfffff,               /* src_mask  */
948          0xfffff,               /* dst_mask  */
949          FALSE),                /* pcrel_offset  */
950
951   /* An PC Relative 20-bit relocation used when setting PIC offset
952      table register.  */
953   HOWTO2 (R_NDS32_GOTPC20,      /* type  */
954          0,                     /* rightshift  */
955          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
956          20,                    /* bitsize  */
957          TRUE,                  /* pc_relative  */
958          0,                     /* bitpos  */
959          complain_overflow_signed,/* complain_on_overflow  */
960          bfd_elf_generic_reloc, /* special_function  */
961          "R_NDS32_GOTPC20",     /* name  */
962          FALSE,                 /* partial_inplace  */
963          0xfffff,               /* src_mask  */
964          0xfffff,               /* dst_mask  */
965          TRUE),                 /* pcrel_offset  */
966
967   /* Like R_NDS32_HI20, but referring to the GOT table entry for
968      the symbol.  */
969   HOWTO2 (R_NDS32_GOT_HI20,     /* type  */
970          12,                    /* rightshift  */
971          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
972          20,                    /* bitsize  */
973          FALSE,                 /* pc_relative  */
974          0,                     /* bitpos  */
975          complain_overflow_dont,/* complain_on_overflow  */
976          bfd_elf_generic_reloc, /* special_function  */
977          "R_NDS32_GOT_HI20",    /* name  */
978          FALSE,                 /* partial_inplace  */
979          0x000fffff,            /* src_mask  */
980          0x000fffff,            /* dst_mask  */
981          FALSE),                /* pcrel_offset  */
982   HOWTO2 (R_NDS32_GOT_LO12,     /* type  */
983          0,                     /* rightshift  */
984          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
985          12,                    /* bitsize  */
986          FALSE,                 /* pc_relative  */
987          0,                     /* bitpos  */
988          complain_overflow_dont,/* complain_on_overflow  */
989          bfd_elf_generic_reloc, /* special_function  */
990          "R_NDS32_GOT_LO12",    /* name  */
991          FALSE,                 /* partial_inplace  */
992          0x00000fff,            /* src_mask  */
993          0x00000fff,            /* dst_mask  */
994          FALSE),                /* pcrel_offset  */
995
996   /* An PC Relative relocation used when setting PIC offset table register.
997      Like R_NDS32_HI20, but referring to the GOT table entry for
998      the symbol.  */
999   HOWTO2 (R_NDS32_GOTPC_HI20,   /* type  */
1000          12,                    /* rightshift  */
1001          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1002          20,                    /* bitsize  */
1003          FALSE,                 /* pc_relative  */
1004          0,                     /* bitpos  */
1005          complain_overflow_dont,/* complain_on_overflow  */
1006          bfd_elf_generic_reloc, /* special_function  */
1007          "R_NDS32_GOTPC_HI20",  /* name  */
1008          FALSE,                 /* partial_inplace  */
1009          0x000fffff,            /* src_mask  */
1010          0x000fffff,            /* dst_mask  */
1011          TRUE),                 /* pcrel_offset  */
1012   HOWTO2 (R_NDS32_GOTPC_LO12,   /* type  */
1013          0,                     /* rightshift  */
1014          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1015          12,                    /* bitsize  */
1016          FALSE,                 /* pc_relative  */
1017          0,                     /* bitpos  */
1018          complain_overflow_dont,/* complain_on_overflow  */
1019          bfd_elf_generic_reloc, /* special_function  */
1020          "R_NDS32_GOTPC_LO12",  /* name  */
1021          FALSE,                 /* partial_inplace  */
1022          0x00000fff,            /* src_mask  */
1023          0x00000fff,            /* dst_mask  */
1024          TRUE),                 /* pcrel_offset  */
1025
1026   HOWTO2 (R_NDS32_GOTOFF_HI20,  /* type  */
1027          12,                    /* rightshift  */
1028          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1029          20,                    /* bitsize  */
1030          FALSE,                 /* pc_relative  */
1031          0,                     /* bitpos  */
1032          complain_overflow_dont,/* complain_on_overflow  */
1033          bfd_elf_generic_reloc, /* special_function  */
1034          "R_NDS32_GOTOFF_HI20", /* name  */
1035          FALSE,                 /* partial_inplace  */
1036          0x000fffff,            /* src_mask  */
1037          0x000fffff,            /* dst_mask  */
1038          FALSE),                /* pcrel_offset  */
1039   HOWTO2 (R_NDS32_GOTOFF_LO12,  /* type  */
1040          0,                     /* rightshift  */
1041          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1042          12,                    /* bitsize  */
1043          FALSE,                 /* pc_relative  */
1044          0,                     /* bitpos  */
1045          complain_overflow_dont,/* complain_on_overflow  */
1046          bfd_elf_generic_reloc, /* special_function  */
1047          "R_NDS32_GOTOFF_LO12", /* name  */
1048          FALSE,                 /* partial_inplace  */
1049          0x00000fff,            /* src_mask  */
1050          0x00000fff,            /* dst_mask  */
1051          FALSE),                /* pcrel_offset  */
1052
1053   /* Alignment hint for relaxable instruction.  This is used with
1054      R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
1055      in order to make next label aligned on word boundary.  */
1056   HOWTO2 (R_NDS32_INSN16,       /* type  */
1057          0,                     /* rightshift  */
1058          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1059          32,                    /* bitsize  */
1060          FALSE,                 /* pc_relative  */
1061          0,                     /* bitpos  */
1062          complain_overflow_dont,/* complain_on_overflow  */
1063          nds32_elf_ignore_reloc,/* special_function  */
1064          "R_NDS32_INSN16",      /* name  */
1065          FALSE,                 /* partial_inplace  */
1066          0x00000fff,            /* src_mask  */
1067          0x00000fff,            /* dst_mask  */
1068          FALSE),                /* pcrel_offset  */
1069
1070   /* Alignment hint for label.  */
1071   HOWTO2 (R_NDS32_LABEL,        /* type  */
1072          0,                     /* rightshift  */
1073          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1074          32,                    /* bitsize  */
1075          FALSE,                 /* pc_relative  */
1076          0,                     /* bitpos  */
1077          complain_overflow_dont,/* complain_on_overflow  */
1078          nds32_elf_ignore_reloc,/* special_function  */
1079          "R_NDS32_LABEL",       /* name  */
1080          FALSE,                 /* partial_inplace  */
1081          0xffffffff,            /* src_mask  */
1082          0xffffffff,            /* dst_mask  */
1083          FALSE),                /* pcrel_offset  */
1084
1085   /* Relax hint for unconditional call sequence  */
1086   HOWTO2 (R_NDS32_LONGCALL1,    /* type  */
1087          0,                     /* rightshift  */
1088          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1089          32,                    /* bitsize  */
1090          FALSE,                 /* pc_relative  */
1091          0,                     /* bitpos  */
1092          complain_overflow_dont,/* complain_on_overflow  */
1093          nds32_elf_ignore_reloc,/* special_function  */
1094          "R_NDS32_LONGCALL1",   /* name  */
1095          FALSE,                 /* partial_inplace  */
1096          0xffffffff,            /* src_mask  */
1097          0xffffffff,            /* dst_mask  */
1098          FALSE),                /* pcrel_offset  */
1099
1100   /* Relax hint for conditional call sequence.  */
1101   HOWTO2 (R_NDS32_LONGCALL2,    /* type  */
1102          0,                     /* rightshift  */
1103          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1104          32,                    /* bitsize  */
1105          FALSE,                 /* pc_relative  */
1106          0,                     /* bitpos  */
1107          complain_overflow_dont,/* complain_on_overflow  */
1108          nds32_elf_ignore_reloc,/* special_function  */
1109          "R_NDS32_LONGCALL2",   /* name  */
1110          FALSE,                 /* partial_inplace  */
1111          0xffffffff,            /* src_mask  */
1112          0xffffffff,            /* dst_mask  */
1113          FALSE),                /* pcrel_offset  */
1114
1115   /* Relax hint for conditional call sequence.  */
1116   HOWTO2 (R_NDS32_LONGCALL3,    /* type  */
1117          0,                     /* rightshift  */
1118          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1119          32,                    /* bitsize  */
1120          FALSE,                 /* pc_relative  */
1121          0,                     /* bitpos  */
1122          complain_overflow_dont,/* complain_on_overflow  */
1123          nds32_elf_ignore_reloc,/* special_function  */
1124          "R_NDS32_LONGCALL3",   /* name  */
1125          FALSE,                 /* partial_inplace  */
1126          0xffffffff,            /* src_mask  */
1127          0xffffffff,            /* dst_mask  */
1128          FALSE),                /* pcrel_offset  */
1129
1130   /* Relax hint for unconditional branch sequence.  */
1131   HOWTO2 (R_NDS32_LONGJUMP1,    /* type  */
1132          0,                     /* rightshift  */
1133          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1134          32,                    /* bitsize  */
1135          FALSE,                 /* pc_relative  */
1136          0,                     /* bitpos  */
1137          complain_overflow_dont,/* complain_on_overflow  */
1138          nds32_elf_ignore_reloc,/* special_function  */
1139          "R_NDS32_LONGJUMP1",   /* name  */
1140          FALSE,                 /* partial_inplace  */
1141          0xffffffff,            /* src_mask  */
1142          0xffffffff,            /* dst_mask  */
1143          FALSE),                /* pcrel_offset  */
1144
1145   /* Relax hint for conditional branch sequence.  */
1146   HOWTO2 (R_NDS32_LONGJUMP2,    /* type  */
1147          0,                     /* rightshift  */
1148          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1149          32,                    /* bitsize  */
1150          FALSE,                 /* pc_relative  */
1151          0,                     /* bitpos  */
1152          complain_overflow_dont,/* complain_on_overflow  */
1153          nds32_elf_ignore_reloc,/* special_function  */
1154          "R_NDS32_LONGJUMP2",   /* name  */
1155          FALSE,                 /* partial_inplace  */
1156          0xffffffff,            /* src_mask  */
1157          0xffffffff,            /* dst_mask  */
1158          FALSE),                /* pcrel_offset  */
1159
1160   /* Relax hint for conditional branch sequence.  */
1161   HOWTO2 (R_NDS32_LONGJUMP3,    /* type  */
1162          0,                     /* rightshift  */
1163          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1164          32,                    /* bitsize  */
1165          FALSE,                 /* pc_relative  */
1166          0,                     /* bitpos  */
1167          complain_overflow_dont,/* complain_on_overflow  */
1168          nds32_elf_ignore_reloc,/* special_function  */
1169          "R_NDS32_LONGJUMP3",   /* name  */
1170          FALSE,                 /* partial_inplace  */
1171          0xffffffff,            /* src_mask  */
1172          0xffffffff,            /* dst_mask  */
1173          FALSE),                /* pcrel_offset  */
1174
1175   /* Relax hint for load/store sequence.   */
1176   HOWTO2 (R_NDS32_LOADSTORE,    /* type  */
1177          0,                     /* rightshift  */
1178          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1179          32,                    /* bitsize  */
1180          FALSE,                 /* pc_relative  */
1181          0,                     /* bitpos  */
1182          complain_overflow_dont,/* complain_on_overflow  */
1183          nds32_elf_ignore_reloc,/* special_function  */
1184          "R_NDS32_LOADSTORE",   /* name  */
1185          FALSE,                 /* partial_inplace  */
1186          0xffffffff,            /* src_mask  */
1187          0xffffffff,            /* dst_mask  */
1188          FALSE),                /* pcrel_offset  */
1189
1190   /* Relax hint for load/store sequence.  */
1191   HOWTO2 (R_NDS32_9_FIXED_RELA, /* type  */
1192          0,                     /* rightshift  */
1193          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1194          16,                    /* bitsize  */
1195          FALSE,                 /* pc_relative  */
1196          0,                     /* bitpos  */
1197          complain_overflow_dont,/* complain_on_overflow  */
1198          nds32_elf_ignore_reloc,/* special_function  */
1199          "R_NDS32_9_FIXED_RELA",/* name  */
1200          FALSE,                 /* partial_inplace  */
1201          0x000000ff,            /* src_mask  */
1202          0x000000ff,            /* dst_mask  */
1203          FALSE),                /* pcrel_offset  */
1204
1205   /* Relax hint for load/store sequence.  */
1206   HOWTO2 (R_NDS32_15_FIXED_RELA,/* type  */
1207          0,                     /* rightshift  */
1208          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1209          32,                    /* bitsize  */
1210          FALSE,                 /* pc_relative  */
1211          0,                     /* bitpos  */
1212          complain_overflow_dont,/* complain_on_overflow  */
1213          nds32_elf_ignore_reloc,/* special_function  */
1214          "R_NDS32_15_FIXED_RELA",/* name  */
1215          FALSE,                 /* partial_inplace  */
1216          0x00003fff,            /* src_mask  */
1217          0x00003fff,            /* dst_mask  */
1218          FALSE),                /* pcrel_offset  */
1219
1220   /* Relax hint for load/store sequence.  */
1221   HOWTO2 (R_NDS32_17_FIXED_RELA,/* type  */
1222          0,                     /* rightshift  */
1223          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1224          32,                    /* bitsize  */
1225          FALSE,                 /* pc_relative  */
1226          0,                     /* bitpos  */
1227          complain_overflow_dont,/* complain_on_overflow  */
1228          nds32_elf_ignore_reloc,/* special_function  */
1229          "R_NDS32_17_FIXED_RELA",/* name  */
1230          FALSE,                 /* partial_inplace  */
1231          0x0000ffff,            /* src_mask  */
1232          0x0000ffff,            /* dst_mask  */
1233          FALSE),                /* pcrel_offset  */
1234
1235   /* Relax hint for load/store sequence.  */
1236   HOWTO2 (R_NDS32_25_FIXED_RELA,/* type  */
1237          0,                     /* rightshift  */
1238          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1239          32,                    /* bitsize  */
1240          FALSE,                 /* pc_relative  */
1241          0,                     /* bitpos  */
1242          complain_overflow_dont,/* complain_on_overflow  */
1243          nds32_elf_ignore_reloc,/* special_function  */
1244          "R_NDS32_25_FIXED_RELA",/* name  */
1245          FALSE,                 /* partial_inplace  */
1246          0x00ffffff,            /* src_mask  */
1247          0x00ffffff,            /* dst_mask  */
1248          FALSE),                /* pcrel_offset  */
1249
1250   /* High 20 bits of PLT symbol offset relative to PC.  */
1251   HOWTO2 (R_NDS32_PLTREL_HI20,  /* type  */
1252          12,                    /* rightshift  */
1253          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1254          20,                    /* bitsize  */
1255          FALSE,                 /* pc_relative  */
1256          0,                     /* bitpos  */
1257          complain_overflow_dont,/* complain_on_overflow  */
1258          bfd_elf_generic_reloc, /* special_function  */
1259          "R_NDS32_PLTREL_HI20", /* name  */
1260          FALSE,                 /* partial_inplace  */
1261          0x000fffff,            /* src_mask  */
1262          0x000fffff,            /* dst_mask  */
1263          FALSE),                /* pcrel_offset  */
1264
1265   /* Low 12 bits of PLT symbol offset relative to PC.  */
1266   HOWTO2 (R_NDS32_PLTREL_LO12,  /* type  */
1267          0,                     /* rightshift  */
1268          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1269          12,                    /* bitsize  */
1270          FALSE,                 /* pc_relative  */
1271          0,                     /* bitpos  */
1272          complain_overflow_dont,/* complain_on_overflow  */
1273          bfd_elf_generic_reloc, /* special_function  */
1274          "R_NDS32_PLTREL_LO12", /* name  */
1275          FALSE,                 /* partial_inplace  */
1276          0x00000fff,            /* src_mask  */
1277          0x00000fff,            /* dst_mask  */
1278          FALSE),                /* pcrel_offset  */
1279
1280   /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
1281   HOWTO2 (R_NDS32_PLT_GOTREL_HI20,      /* type  */
1282          12,                    /* rightshift  */
1283          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1284          20,                    /* bitsize  */
1285          FALSE,                 /* pc_relative  */
1286          0,                     /* bitpos  */
1287          complain_overflow_dont,/* complain_on_overflow  */
1288          bfd_elf_generic_reloc, /* special_function  */
1289          "R_NDS32_PLT_GOTREL_HI20",/* name  */
1290          FALSE,                 /* partial_inplace  */
1291          0x000fffff,            /* src_mask  */
1292          0x000fffff,            /* dst_mask  */
1293          FALSE),                /* pcrel_offset  */
1294
1295   /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
1296   HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type  */
1297          0,                     /* rightshift  */
1298          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1299          12,                    /* bitsize  */
1300          FALSE,                 /* pc_relative  */
1301          0,                     /* bitpos  */
1302          complain_overflow_dont,/* complain_on_overflow  */
1303          bfd_elf_generic_reloc, /* special_function  */
1304          "R_NDS32_PLT_GOTREL_LO12",/* name  */
1305          FALSE,                 /* partial_inplace  */
1306          0x00000fff,            /* src_mask  */
1307          0x00000fff,            /* dst_mask  */
1308          FALSE),                /* pcrel_offset  */
1309
1310   /* Small data area 12 bits offset.  */
1311   HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type  */
1312          2,                     /* rightshift  */
1313          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1314          12,                    /* bitsize  */
1315          FALSE,                 /* pc_relative  */
1316          0,                     /* bitpos  */
1317          complain_overflow_signed,/* complain_on_overflow  */
1318          bfd_elf_generic_reloc, /* special_function  */
1319          "R_NDS32_SDA12S2_DP_RELA",/* name  */
1320          FALSE,                 /* partial_inplace  */
1321          0x00000fff,            /* src_mask  */
1322          0x00000fff,            /* dst_mask  */
1323          FALSE),                /* pcrel_offset  */
1324
1325   /* Small data area 12 bits offset.  */
1326   HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type  */
1327          2,                     /* rightshift  */
1328          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1329          12,                    /* bitsize  */
1330          FALSE,                 /* pc_relative  */
1331          0,                     /* bitpos  */
1332          complain_overflow_signed,/* complain_on_overflow  */
1333          bfd_elf_generic_reloc, /* special_function  */
1334          "R_NDS32_SDA12S2_SP_RELA",/* name  */
1335          FALSE,                 /* partial_inplace  */
1336          0x00000fff,            /* src_mask  */
1337          0x00000fff,            /* dst_mask  */
1338          FALSE),                /* pcrel_offset  */
1339   /* Lower 12 bits of address.  */
1340
1341   HOWTO2 (R_NDS32_LO12S2_DP_RELA,       /* type  */
1342          2,                     /* rightshift  */
1343          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1344          10,                    /* bitsize  */
1345          FALSE,                 /* pc_relative  */
1346          0,                     /* bitpos  */
1347          complain_overflow_dont,/* complain_on_overflow  */
1348          bfd_elf_generic_reloc, /* special_function  */
1349          "R_NDS32_LO12S2_DP_RELA",/* name  */
1350          FALSE,                 /* partial_inplace  */
1351          0x000003ff,            /* src_mask  */
1352          0x000003ff,            /* dst_mask  */
1353          FALSE),                /* pcrel_offset  */
1354
1355   /* Lower 12 bits of address.  */
1356   HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type  */
1357          2,                     /* rightshift  */
1358          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1359          10,                    /* bitsize  */
1360          FALSE,                 /* pc_relative  */
1361          0,                     /* bitpos  */
1362          complain_overflow_dont,/* complain_on_overflow  */
1363          bfd_elf_generic_reloc, /* special_function  */
1364          "R_NDS32_LO12S2_SP_RELA",/* name  */
1365          FALSE,                 /* partial_inplace  */
1366          0x000003ff,            /* src_mask  */
1367          0x000003ff,            /* dst_mask  */
1368          FALSE),                /* pcrel_offset  */
1369   /* Lower 12 bits of address.  Special identity for or case.  */
1370   HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type  */
1371          0,                     /* rightshift  */
1372          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1373          12,                    /* bitsize  */
1374          FALSE,                 /* pc_relative  */
1375          0,                     /* bitpos  */
1376          complain_overflow_dont,/* complain_on_overflow  */
1377          bfd_elf_generic_reloc, /* special_function  */
1378          "R_NDS32_LO12S0_ORI_RELA",/* name  */
1379          FALSE,                 /* partial_inplace  */
1380          0x00000fff,            /* src_mask  */
1381          0x00000fff,            /* dst_mask  */
1382          FALSE),                /* pcrel_offset  */
1383   /* Small data area 19 bits offset.  */
1384   HOWTO2 (R_NDS32_SDA16S3_RELA, /* type  */
1385          3,                     /* rightshift  */
1386          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1387          16,                    /* bitsize  */
1388          FALSE,                 /* pc_relative  */
1389          0,                     /* bitpos  */
1390          complain_overflow_signed,/* complain_on_overflow  */
1391          bfd_elf_generic_reloc, /* special_function  */
1392          "R_NDS32_SDA16S3_RELA",/* name  */
1393          FALSE,                 /* partial_inplace  */
1394          0x0000ffff,            /* src_mask  */
1395          0x0000ffff,            /* dst_mask  */
1396          FALSE),                /* pcrel_offset  */
1397
1398   /* Small data area 15 bits offset.  */
1399   HOWTO2 (R_NDS32_SDA17S2_RELA, /* type  */
1400          2,                     /* rightshift  */
1401          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1402          17,                    /* bitsize  */
1403          FALSE,                 /* pc_relative  */
1404          0,                     /* bitpos  */
1405          complain_overflow_signed,/* complain_on_overflow  */
1406          bfd_elf_generic_reloc, /* special_function  */
1407          "R_NDS32_SDA17S2_RELA",/* name  */
1408          FALSE,                 /* partial_inplace  */
1409          0x0001ffff,            /* src_mask  */
1410          0x0001ffff,            /* dst_mask  */
1411          FALSE),                /* pcrel_offset  */
1412
1413   HOWTO2 (R_NDS32_SDA18S1_RELA, /* type  */
1414          1,                     /* rightshift  */
1415          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1416          18,                    /* bitsize  */
1417          FALSE,                 /* pc_relative  */
1418          0,                     /* bitpos  */
1419          complain_overflow_signed,/* complain_on_overflow  */
1420          bfd_elf_generic_reloc, /* special_function  */
1421          "R_NDS32_SDA18S1_RELA",/* name  */
1422          FALSE,                 /* partial_inplace  */
1423          0x0003ffff,            /* src_mask  */
1424          0x0003ffff,            /* dst_mask  */
1425          FALSE),                /* pcrel_offset  */
1426
1427   HOWTO2 (R_NDS32_SDA19S0_RELA, /* type  */
1428          0,                     /* rightshift  */
1429          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1430          19,                    /* bitsize  */
1431          FALSE,                 /* pc_relative  */
1432          0,                     /* bitpos  */
1433          complain_overflow_signed,/* complain_on_overflow  */
1434          bfd_elf_generic_reloc, /* special_function  */
1435          "R_NDS32_SDA19S0_RELA",/* name  */
1436          FALSE,                 /* partial_inplace  */
1437          0x0007ffff,            /* src_mask  */
1438          0x0007ffff,            /* dst_mask  */
1439          FALSE),                /* pcrel_offset  */
1440   HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type  */
1441          0,                     /* rightshift  */
1442          0,                     /* size (0 = byte, 1 = short, 2 = long)  */
1443          8,                     /* bitsize  */
1444          FALSE,                 /* pc_relative  */
1445          0,                     /* bitpos  */
1446          complain_overflow_dont,/* complain_on_overflow  */
1447          nds32_elf_ignore_reloc,/* special_function  */
1448          "R_NDS32_DWARF2_OP1_RELA",/* name  */
1449          FALSE,                 /* partial_inplace  */
1450          0xff,                  /* src_mask  */
1451          0xff,                  /* dst_mask  */
1452          FALSE),                /* pcrel_offset  */
1453   HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type  */
1454          0,                     /* rightshift  */
1455          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1456          16,                    /* bitsize  */
1457          FALSE,                 /* pc_relative  */
1458          0,                     /* bitpos  */
1459          complain_overflow_dont,/* complain_on_overflow  */
1460          nds32_elf_ignore_reloc,/* special_function  */
1461          "R_NDS32_DWARF2_OP2_RELA",/* name  */
1462          FALSE,                 /* partial_inplace  */
1463          0xffff,                /* src_mask  */
1464          0xffff,                /* dst_mask  */
1465          FALSE),                /* pcrel_offset  */
1466   HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type  */
1467          0,                     /* rightshift  */
1468          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1469          32,                    /* bitsize  */
1470          FALSE,                 /* pc_relative  */
1471          0,                     /* bitpos  */
1472          complain_overflow_dont,/* complain_on_overflow  */
1473          nds32_elf_ignore_reloc,/* special_function  */
1474          "R_NDS32_DWARF2_LEB_RELA",/* name  */
1475          FALSE,                 /* partial_inplace  */
1476          0xffffffff,            /* src_mask  */
1477          0xffffffff,            /* dst_mask  */
1478          FALSE),                /* pcrel_offset  */
1479   HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type  */
1480          0,                     /* rightshift  */
1481          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1482          16,                    /* bitsize  */
1483          FALSE,                 /* pc_relative  */
1484          0,                     /* bitpos  */
1485          complain_overflow_dont,/* complain_on_overflow  */
1486          nds32_elf_ignore_reloc,/* special_function  */
1487          "R_NDS32_UPDATE_TA_RELA",/* name  */
1488          FALSE,                 /* partial_inplace  */
1489          0xffff,                /* src_mask  */
1490          0xffff,                /* dst_mask  */
1491          FALSE),                /* pcrel_offset  */
1492   /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1493      entry for the symbol.  */
1494   HOWTO2 (R_NDS32_9_PLTREL,     /* type  */
1495          1,                     /* rightshift  */
1496          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1497          8,                     /* bitsize  */
1498          TRUE,                  /* pc_relative  */
1499          0,                     /* bitpos  */
1500          complain_overflow_signed,/* complain_on_overflow  */
1501          bfd_elf_generic_reloc, /* special_function  */
1502          "R_NDS32_9_PLTREL",    /* name  */
1503          FALSE,                 /* partial_inplace  */
1504          0xff,                  /* src_mask  */
1505          0xff,                  /* dst_mask  */
1506          TRUE),                 /* pcrel_offset  */
1507   /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
1508   HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type  */
1509          0,                     /* rightshift  */
1510          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1511          20,                    /* bitsize  */
1512          FALSE,                 /* pc_relative  */
1513          0,                     /* bitpos  */
1514          complain_overflow_dont,/* complain_on_overflow  */
1515          bfd_elf_generic_reloc, /* special_function  */
1516          "R_NDS32_PLT_GOTREL_LO20",/* name  */
1517          FALSE,                 /* partial_inplace  */
1518          0x000fffff,            /* src_mask  */
1519          0x000fffff,            /* dst_mask  */
1520          FALSE),                /* pcrel_offset  */
1521   /* low 15 bits of PLT symbol offset relative to GOT (GP)  */
1522   HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type  */
1523          0,                     /* rightshift  */
1524          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1525          15,                    /* bitsize  */
1526          FALSE,                 /* pc_relative  */
1527          0,                     /* bitpos  */
1528          complain_overflow_dont,/* complain_on_overflow  */
1529          bfd_elf_generic_reloc, /* special_function  */
1530          "R_NDS32_PLT_GOTREL_LO15",/* name  */
1531          FALSE,                 /* partial_inplace  */
1532          0x00007fff,            /* src_mask  */
1533          0x00007fff,            /* dst_mask  */
1534          FALSE),                /* pcrel_offset  */
1535   /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
1536   HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type  */
1537          0,                     /* rightshift  */
1538          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1539          19,                    /* bitsize  */
1540          FALSE,                 /* pc_relative  */
1541          0,                     /* bitpos  */
1542          complain_overflow_dont,/* complain_on_overflow  */
1543          bfd_elf_generic_reloc, /* special_function  */
1544          "R_NDS32_PLT_GOTREL_LO19",/* name  */
1545          FALSE,                 /* partial_inplace  */
1546          0x0007ffff,            /* src_mask  */
1547          0x0007ffff,            /* dst_mask  */
1548          FALSE),                /* pcrel_offset  */
1549   HOWTO2 (R_NDS32_GOT_LO15,     /* type  */
1550          0,                     /* rightshift  */
1551          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1552          15,                    /* bitsize  */
1553          FALSE,                 /* pc_relative  */
1554          0,                     /* bitpos  */
1555          complain_overflow_dont,/* complain_on_overflow  */
1556          bfd_elf_generic_reloc, /* special_function  */
1557          "R_NDS32_GOT_LO15",    /* name  */
1558          FALSE,                 /* partial_inplace  */
1559          0x00007fff,            /* src_mask  */
1560          0x00007fff,            /* dst_mask  */
1561          FALSE),                /* pcrel_offset  */
1562   HOWTO2 (R_NDS32_GOT_LO19,     /* type  */
1563          0,                     /* rightshift  */
1564          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1565          19,                    /* bitsize  */
1566          FALSE,                 /* pc_relative  */
1567          0,                     /* bitpos  */
1568          complain_overflow_dont,/* complain_on_overflow  */
1569          bfd_elf_generic_reloc, /* special_function  */
1570          "R_NDS32_GOT_LO19",    /* name  */
1571          FALSE,                 /* partial_inplace  */
1572          0x0007ffff,            /* src_mask  */
1573          0x0007ffff,            /* dst_mask  */
1574          FALSE),                /* pcrel_offset  */
1575   HOWTO2 (R_NDS32_GOTOFF_LO15,  /* type  */
1576          0,                     /* rightshift  */
1577          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1578          15,                    /* bitsize  */
1579          FALSE,                 /* pc_relative  */
1580          0,                     /* bitpos  */
1581          complain_overflow_dont,/* complain_on_overflow  */
1582          bfd_elf_generic_reloc, /* special_function  */
1583          "R_NDS32_GOTOFF_LO15", /* name  */
1584          FALSE,                 /* partial_inplace  */
1585          0x00007fff,            /* src_mask  */
1586          0x00007fff,            /* dst_mask  */
1587          FALSE),                /* pcrel_offset  */
1588   HOWTO2 (R_NDS32_GOTOFF_LO19,  /* type  */
1589          0,                     /* rightshift  */
1590          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1591          19,                    /* bitsize  */
1592          FALSE,                 /* pc_relative  */
1593          0,                     /* bitpos  */
1594          complain_overflow_dont,/* complain_on_overflow  */
1595          bfd_elf_generic_reloc, /* special_function  */
1596          "R_NDS32_GOTOFF_LO19", /* name  */
1597          FALSE,                 /* partial_inplace  */
1598          0x0007ffff,            /* src_mask  */
1599          0x0007ffff,            /* dst_mask  */
1600          FALSE),                /* pcrel_offset  */
1601   /* GOT 15 bits offset.  */
1602   HOWTO2 (R_NDS32_GOT15S2_RELA, /* type  */
1603          2,                     /* rightshift  */
1604          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1605          15,                    /* bitsize  */
1606          FALSE,                 /* pc_relative  */
1607          0,                     /* bitpos  */
1608          complain_overflow_signed,/* complain_on_overflow  */
1609          bfd_elf_generic_reloc, /* special_function  */
1610          "R_NDS32_GOT15S2_RELA",/* name  */
1611          FALSE,                 /* partial_inplace  */
1612          0x00007fff,            /* src_mask  */
1613          0x00007fff,            /* dst_mask  */
1614          FALSE),                /* pcrel_offset  */
1615   /* GOT 17 bits offset.  */
1616   HOWTO2 (R_NDS32_GOT17S2_RELA, /* type  */
1617          2,                     /* rightshift  */
1618          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1619          17,                    /* bitsize  */
1620          FALSE,                 /* pc_relative  */
1621          0,                     /* bitpos  */
1622          complain_overflow_signed,/* complain_on_overflow  */
1623          bfd_elf_generic_reloc, /* special_function  */
1624          "R_NDS32_GOT17S2_RELA",/* name  */
1625          FALSE,                 /* partial_inplace  */
1626          0x0001ffff,            /* src_mask  */
1627          0x0001ffff,            /* dst_mask  */
1628          FALSE),                /* pcrel_offset  */
1629   /* A 5 bit address.  */
1630   HOWTO2 (R_NDS32_5_RELA,       /* type  */
1631          0,                     /* rightshift  */
1632          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1633          5,                     /* bitsize  */
1634          FALSE,                 /* pc_relative  */
1635          0,                     /* bitpos  */
1636          complain_overflow_signed,/* complain_on_overflow  */
1637          bfd_elf_generic_reloc, /* special_function  */
1638          "R_NDS32_5_RELA",      /* name  */
1639          FALSE,                 /* partial_inplace  */
1640          0x1f,                  /* src_mask  */
1641          0x1f,                  /* dst_mask  */
1642          FALSE),                /* pcrel_offset  */
1643   HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type  */
1644          1,                     /* rightshift  */
1645          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1646          9,                     /* bitsize  */
1647          TRUE,                  /* pc_relative  */
1648          0,                     /* bitpos  */
1649          complain_overflow_unsigned,/* complain_on_overflow  */
1650          bfd_elf_generic_reloc, /* special_function  */
1651          "R_NDS32_10_UPCREL_RELA",/* name  */
1652          FALSE,                 /* partial_inplace  */
1653          0x1ff,                 /* src_mask  */
1654          0x1ff,                 /* dst_mask  */
1655          TRUE),                 /* pcrel_offset  */
1656   HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type  */
1657          2,                     /* rightshift  */
1658          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1659          7,                     /* bitsize  */
1660          FALSE,                 /* pc_relative  */
1661          0,                     /* bitpos  */
1662          complain_overflow_unsigned,/* complain_on_overflow  */
1663          bfd_elf_generic_reloc, /* special_function  */
1664          "R_NDS32_SDA_FP7U2_RELA",/* name  */
1665          FALSE,                 /* partial_inplace  */
1666          0x0000007f,            /* src_mask  */
1667          0x0000007f,            /* dst_mask  */
1668          FALSE),                /* pcrel_offset  */
1669   HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type  */
1670          1,                     /* rightshift  */
1671          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1672          8,                     /* bitsize  */
1673          TRUE,                  /* pc_relative  */
1674          0,                     /* bitpos  */
1675          complain_overflow_signed,/* complain_on_overflow  */
1676          bfd_elf_generic_reloc, /* special_function  */
1677          "R_NDS32_WORD_9_PCREL_RELA",/* name  */
1678          FALSE,                 /* partial_inplace  */
1679          0xff,                  /* src_mask  */
1680          0xff,                  /* dst_mask  */
1681          TRUE),                 /* pcrel_offset  */
1682   HOWTO2 (R_NDS32_25_ABS_RELA,  /* type  */
1683          1,                     /* rightshift  */
1684          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1685          24,                    /* bitsize  */
1686          FALSE,                 /* pc_relative  */
1687          0,                     /* bitpos  */
1688          complain_overflow_dont,/* complain_on_overflow  */
1689          bfd_elf_generic_reloc, /* special_function  */
1690          "R_NDS32_25_ABS_RELA", /* name  */
1691          FALSE,                 /* partial_inplace  */
1692          0xffffff,              /* src_mask  */
1693          0xffffff,              /* dst_mask  */
1694          FALSE),                /* pcrel_offset  */
1695
1696   /* A relative 17 bit relocation for ifc, right shifted by 1.  */
1697   HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type  */
1698          1,                     /* rightshift  */
1699          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1700          16,                    /* bitsize  */
1701          TRUE,                  /* pc_relative  */
1702          0,                     /* bitpos  */
1703          complain_overflow_signed,/* complain_on_overflow  */
1704          bfd_elf_generic_reloc, /* special_function  */
1705          "R_NDS32_17IFC_PCREL_RELA",/* name  */
1706          FALSE,                 /* partial_inplace  */
1707          0xffff,                /* src_mask  */
1708          0xffff,                /* dst_mask  */
1709          TRUE),                 /* pcrel_offset  */
1710
1711   /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
1712   HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type  */
1713          1,                     /* rightshift  */
1714          1,                     /* size (0 = byte, 1 = short, 2 = long)  */
1715          9,                     /* bitsize  */
1716          TRUE,                  /* pc_relative  */
1717          0,                     /* bitpos  */
1718          complain_overflow_unsigned,/* complain_on_overflow  */
1719          bfd_elf_generic_reloc, /* special_function  */
1720          "R_NDS32_10IFCU_PCREL_RELA",/* name  */
1721          FALSE,                 /* partial_inplace  */
1722          0x1ff,                 /* src_mask  */
1723          0x1ff,                 /* dst_mask  */
1724          TRUE),                 /* pcrel_offset  */
1725
1726   /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol.  */
1727   HOWTO2 (R_NDS32_TLS_LE_HI20,  /* type  */
1728          12,                    /* rightshift  */
1729          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1730          20,                    /* bitsize  */
1731          FALSE,                 /* pc_relative  */
1732          0,                     /* bitpos  */
1733          complain_overflow_dont,/* complain_on_overflow  */
1734          bfd_elf_generic_reloc, /* special_function  */
1735          "R_NDS32_TLS_LE_HI20", /* name  */
1736          FALSE,                 /* partial_inplace  */
1737          0x000fffff,            /* src_mask  */
1738          0x000fffff,            /* dst_mask  */
1739          FALSE),                /* pcrel_offset  */
1740
1741   HOWTO2 (R_NDS32_TLS_LE_LO12,  /* type  */
1742          0,                     /* rightshift  */
1743          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1744          12,                    /* bitsize  */
1745          FALSE,                 /* pc_relative  */
1746          0,                     /* bitpos  */
1747          complain_overflow_dont,/* complain_on_overflow  */
1748          bfd_elf_generic_reloc, /* special_function  */
1749          "R_NDS32_TLS_LE_LO12", /* name  */
1750          FALSE,                 /* partial_inplace  */
1751          0x00000fff,            /* src_mask  */
1752          0x00000fff,            /* dst_mask  */
1753          FALSE),                /* pcrel_offset  */
1754
1755   /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol.  */
1756   HOWTO2 (R_NDS32_TLS_IE_HI20,  /* type  */
1757          12,                    /* rightshift  */
1758          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1759          20,                    /* bitsize  */
1760          FALSE,                 /* pc_relative  */
1761          0,                     /* bitpos  */
1762          complain_overflow_dont,/* complain_on_overflow  */
1763          bfd_elf_generic_reloc, /* special_function  */
1764          "R_NDS32_TLS_IE_HI20", /* name  */
1765          FALSE,                 /* partial_inplace  */
1766          0x000fffff,            /* src_mask  */
1767          0x000fffff,            /* dst_mask  */
1768          FALSE),                /* pcrel_offset  */
1769
1770   HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type  */
1771          2,                     /* rightshift  */
1772          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1773          10,                    /* bitsize  */
1774          FALSE,                 /* pc_relative  */
1775          0,                     /* bitpos  */
1776          complain_overflow_dont,/* complain_on_overflow  */
1777          bfd_elf_generic_reloc, /* special_function  */
1778          "R_NDS32_TLS_IE_LO12S2",/* name  */
1779          FALSE,                 /* partial_inplace  */
1780          0x000003ff,            /* src_mask  */
1781          0x000003ff,            /* dst_mask  */
1782          FALSE),                /* pcrel_offset  */
1783
1784   /* TLS LE TP offset relocation  */
1785   HOWTO2 (R_NDS32_TLS_TPOFF,    /* type  */
1786          0,                     /* rightshift  */
1787          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1788          32,                    /* bitsize  */
1789          FALSE,                 /* pc_relative  */
1790          0,                     /* bitpos  */
1791          complain_overflow_bitfield,/* complain_on_overflow  */
1792          bfd_elf_generic_reloc, /* special_function  */
1793          "R_NDS32_TLS_TPOFF",   /* name  */
1794          FALSE,                 /* partial_inplace  */
1795          0xffffffff,            /* src_mask  */
1796          0xffffffff,            /* dst_mask  */
1797          FALSE),                /* pcrel_offset  */
1798
1799   /* A 20 bit address.  */
1800   HOWTO2 (R_NDS32_TLS_LE_20,    /* type  */
1801          0,                     /* rightshift  */
1802          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1803          20,                    /* bitsize  */
1804          FALSE,                 /* pc_relative  */
1805          0,                     /* bitpos  */
1806          complain_overflow_signed,/* complain_on_overflow  */
1807          bfd_elf_generic_reloc, /* special_function  */
1808          "R_NDS32_TLS_LE_20",   /* name  */
1809          FALSE,                 /* partial_inplace  */
1810          0xfffff,               /* src_mask  */
1811          0xfffff,               /* dst_mask  */
1812          FALSE),                /* pcrel_offset  */
1813
1814   HOWTO2 (R_NDS32_TLS_LE_15S0,  /* type  */
1815          0,                     /* rightshift  */
1816          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1817          15,                    /* bitsize  */
1818          FALSE,                 /* pc_relative  */
1819          0,                     /* bitpos  */
1820          complain_overflow_signed,/* complain_on_overflow  */
1821          bfd_elf_generic_reloc, /* special_function  */
1822          "R_NDS32_TLS_LE_15S0", /* name  */
1823          FALSE,                 /* partial_inplace  */
1824          0x7fff,                /* src_mask  */
1825          0x7fff,                /* dst_mask  */
1826          FALSE),                /* pcrel_offset  */
1827   HOWTO2 (R_NDS32_TLS_LE_15S1,  /* type  */
1828          1,                     /* rightshift  */
1829          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1830          15,                    /* bitsize  */
1831          FALSE,                 /* pc_relative  */
1832          0,                     /* bitpos  */
1833          complain_overflow_signed,/* complain_on_overflow  */
1834          bfd_elf_generic_reloc, /* special_function  */
1835          "R_NDS32_TLS_LE_15S1", /* name  */
1836          FALSE,                 /* partial_inplace  */
1837          0x7fff,                /* src_mask  */
1838          0x7fff,                /* dst_mask  */
1839          FALSE),                /* pcrel_offset  */
1840   HOWTO2 (R_NDS32_TLS_LE_15S2,  /* type  */
1841          2,                     /* rightshift  */
1842          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1843          15,                    /* bitsize  */
1844          FALSE,                 /* pc_relative  */
1845          0,                     /* bitpos  */
1846          complain_overflow_signed,/* complain_on_overflow  */
1847          bfd_elf_generic_reloc, /* special_function  */
1848          "R_NDS32_TLS_LE_15S2", /* name  */
1849          FALSE,                 /* partial_inplace  */
1850          0x7fff,                /* src_mask  */
1851          0x7fff,                /* dst_mask  */
1852          FALSE),                /* pcrel_offset  */
1853
1854   /* Relax hint for unconditional call sequence  */
1855   HOWTO2 (R_NDS32_LONGCALL4,    /* type  */
1856          0,                     /* rightshift  */
1857          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1858          32,                    /* bitsize  */
1859          FALSE,                 /* pc_relative  */
1860          0,                     /* bitpos  */
1861          complain_overflow_dont,/* complain_on_overflow  */
1862          nds32_elf_ignore_reloc,/* special_function  */
1863          "R_NDS32_LONGCALL4",   /* name  */
1864          FALSE,                 /* partial_inplace  */
1865          0xffffffff,            /* src_mask  */
1866          0xffffffff,            /* dst_mask  */
1867          FALSE),                /* pcrel_offset  */
1868
1869   /* Relax hint for conditional call sequence.  */
1870   HOWTO2 (R_NDS32_LONGCALL5,    /* type  */
1871          0,                     /* rightshift  */
1872          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1873          32,                    /* bitsize  */
1874          FALSE,                 /* pc_relative  */
1875          0,                     /* bitpos  */
1876          complain_overflow_dont,/* complain_on_overflow  */
1877          nds32_elf_ignore_reloc,/* special_function  */
1878          "R_NDS32_LONGCALL5",   /* name  */
1879          FALSE,                 /* partial_inplace  */
1880          0xffffffff,            /* src_mask  */
1881          0xffffffff,            /* dst_mask  */
1882          FALSE),                /* pcrel_offset  */
1883
1884   /* Relax hint for conditional call sequence.  */
1885   HOWTO2 (R_NDS32_LONGCALL6,    /* type  */
1886          0,                     /* rightshift  */
1887          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1888          32,                    /* bitsize  */
1889          FALSE,                 /* pc_relative  */
1890          0,                     /* bitpos  */
1891          complain_overflow_dont,/* complain_on_overflow  */
1892          nds32_elf_ignore_reloc,/* special_function  */
1893          "R_NDS32_LONGCALL6",   /* name  */
1894          FALSE,                 /* partial_inplace  */
1895          0xffffffff,            /* src_mask  */
1896          0xffffffff,            /* dst_mask  */
1897          FALSE),                /* pcrel_offset  */
1898
1899   /* Relax hint for unconditional branch sequence.  */
1900   HOWTO2 (R_NDS32_LONGJUMP4,    /* type  */
1901          0,                     /* rightshift  */
1902          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1903          32,                    /* bitsize  */
1904          FALSE,                 /* pc_relative  */
1905          0,                     /* bitpos  */
1906          complain_overflow_dont,/* complain_on_overflow  */
1907          nds32_elf_ignore_reloc,/* special_function  */
1908          "R_NDS32_LONGJUMP4",   /* name  */
1909          FALSE,                 /* partial_inplace  */
1910          0xffffffff,            /* src_mask  */
1911          0xffffffff,            /* dst_mask  */
1912          FALSE),                /* pcrel_offset  */
1913
1914   /* Relax hint for conditional branch sequence.  */
1915   HOWTO2 (R_NDS32_LONGJUMP5,    /* type  */
1916          0,                     /* rightshift  */
1917          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1918          32,                    /* bitsize  */
1919          FALSE,                 /* pc_relative  */
1920          0,                     /* bitpos  */
1921          complain_overflow_dont,/* complain_on_overflow  */
1922          nds32_elf_ignore_reloc,/* special_function  */
1923          "R_NDS32_LONGJUMP5",   /* name  */
1924          FALSE,                 /* partial_inplace  */
1925          0xffffffff,            /* src_mask  */
1926          0xffffffff,            /* dst_mask  */
1927          FALSE),                /* pcrel_offset  */
1928
1929   /* Relax hint for conditional branch sequence.  */
1930   HOWTO2 (R_NDS32_LONGJUMP6,    /* type  */
1931          0,                     /* rightshift  */
1932          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1933          32,                    /* bitsize  */
1934          FALSE,                 /* pc_relative  */
1935          0,                     /* bitpos  */
1936          complain_overflow_dont,/* complain_on_overflow  */
1937          nds32_elf_ignore_reloc,/* special_function  */
1938          "R_NDS32_LONGJUMP6",   /* name  */
1939          FALSE,                 /* partial_inplace  */
1940          0xffffffff,            /* src_mask  */
1941          0xffffffff,            /* dst_mask  */
1942          FALSE),                /* pcrel_offset  */
1943
1944   /* Relax hint for conditional branch sequence.  */
1945   HOWTO2 (R_NDS32_LONGJUMP7,    /* type  */
1946          0,                     /* rightshift  */
1947          2,                     /* size (0 = byte, 1 = short, 2 = long)  */
1948          32,                    /* bitsize  */
1949          FALSE,                 /* pc_relative  */
1950          0,                     /* bitpos  */
1951          complain_overflow_dont,/* complain_on_overflow  */
1952          nds32_elf_ignore_reloc,/* special_function  */
1953          "R_NDS32_LONGJUMP7",   /* name  */
1954          FALSE,                 /* partial_inplace  */
1955          0xffffffff,            /* src_mask  */
1956          0xffffffff,            /* dst_mask  */
1957          FALSE),                /* pcrel_offset  */
1958
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  */
1972
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  */
1988
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  */
2002
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  */
2016
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  */
2031
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  */
2046
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  */
2061
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  */
2076
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  */
2091 };
2092
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)
2096
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),
2495 };
2496
2497 static unsigned long dl_tlsdesc_lazy_trampoline[] =
2498 {
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              */
2507 };
2508
2509 static void
2510 nds32_put_trampoline (void *contents, const unsigned long *template,
2511                       unsigned count)
2512 {
2513   unsigned ix;
2514
2515   for (ix = 0; ix != count; ix++)
2516     {
2517       unsigned long insn = template[ix];
2518       bfd_putb32 (insn, (char *) contents + ix * 4);
2519     }
2520 }
2521
2522 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2523    This prototype is the same as qsort ().  */
2524
2525 void
2526 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2527                       int (*compar) (const void *lhs, const void *rhs))
2528 {
2529   char *ptr = (char *) base;
2530   int i, j;
2531   char *tmp = xmalloc (size);
2532
2533   /* If i is less than j, i is inserted before j.
2534
2535      |---- j ----- i --------------|
2536       \          / \              /
2537          sorted         unsorted
2538    */
2539
2540   for (i = 1; i < (int) nmemb; i++)
2541     {
2542       for (j = (i - 1); j >= 0; j--)
2543         if (compar (ptr + i * size, ptr + j * size) >= 0)
2544           break;
2545
2546       j++;
2547
2548       if (i == j)
2549         continue; /* i is in order.  */
2550
2551       memcpy (tmp, ptr + i * size, size);
2552       memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2553       memcpy (ptr + j * size, tmp, size);
2554     }
2555   free (tmp);
2556 }
2557
2558 /* Sort relocation by r_offset.
2559
2560    We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2561    algorithm.  Relocations at the same r_offset must keep their order.
2562    For example, RELAX_ENTRY must be the very first relocation entry.
2563
2564    Currently, this function implements insertion-sort.
2565
2566    FIXME: If we already sort them in assembler, why bother sort them
2567           here again?  */
2568
2569 static int
2570 compar_reloc (const void *lhs, const void *rhs)
2571 {
2572   const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2573   const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2574
2575   if (l->r_offset > r->r_offset)
2576     return 1;
2577   else if (l->r_offset == r->r_offset)
2578     return 0;
2579   else
2580     return -1;
2581 }
2582
2583 /* Functions listed below are only used for old relocs.
2584      nds32_elf_9_pcrel_reloc
2585      nds32_elf_do_9_pcrel_reloc
2586      nds32_elf_hi20_reloc
2587      nds32_elf_relocate_hi20
2588      nds32_elf_lo12_reloc
2589      nds32_elf_sda15_reloc
2590      nds32_elf_generic_reloc.  */
2591
2592 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
2593
2594 static bfd_reloc_status_type
2595 nds32_elf_9_pcrel_reloc (bfd *       abfd,
2596                          arelent *   reloc_entry,
2597                          asymbol *   symbol,
2598                          void *      data,
2599                          asection *  input_section,
2600                          bfd *       output_bfd,
2601                          char **     error_message ATTRIBUTE_UNUSED)
2602 {
2603   /* This part is from bfd_elf_generic_reloc.  */
2604   if (output_bfd != (bfd *) NULL
2605       && (symbol->flags & BSF_SECTION_SYM) == 0
2606       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2607     {
2608       reloc_entry->address += input_section->output_offset;
2609       return bfd_reloc_ok;
2610     }
2611
2612   if (output_bfd != NULL)
2613     {
2614       /* FIXME: See bfd_perform_relocation.  Is this right?  */
2615       return bfd_reloc_continue;
2616     }
2617
2618   return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2619                                      input_section,
2620                                      data, reloc_entry->address,
2621                                      symbol->section,
2622                                      (symbol->value
2623                                       + symbol->section->output_section->vma
2624                                       + symbol->section->output_offset),
2625                                      reloc_entry->addend);
2626 }
2627
2628 /* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
2629 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2630
2631 static bfd_reloc_status_type
2632 nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
2633                             reloc_howto_type *  howto,
2634                             asection *          input_section,
2635                             bfd_byte *          data,
2636                             bfd_vma             offset,
2637                             asection *          symbol_section ATTRIBUTE_UNUSED,
2638                             bfd_vma             symbol_value,
2639                             bfd_vma             addend)
2640 {
2641   bfd_signed_vma relocation;
2642   unsigned short x;
2643   bfd_reloc_status_type status;
2644
2645   /* Sanity check the address (offset in section).  */
2646   if (offset > bfd_get_section_limit (abfd, input_section))
2647     return bfd_reloc_outofrange;
2648
2649   relocation = symbol_value + addend;
2650   /* Make it pc relative.  */
2651   relocation -= (input_section->output_section->vma
2652                  + input_section->output_offset);
2653   /* These jumps mask off the lower two bits of the current address
2654      before doing pcrel calculations.  */
2655   relocation -= (offset & -(bfd_vma) 2);
2656
2657   if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2658     status = bfd_reloc_overflow;
2659   else
2660     status = bfd_reloc_ok;
2661
2662   x = bfd_getb16 (data + offset);
2663
2664   relocation >>= howto->rightshift;
2665   relocation <<= howto->bitpos;
2666   x = (x & ~howto->dst_mask)
2667       | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2668
2669   bfd_putb16 ((bfd_vma) x, data + offset);
2670
2671   return status;
2672 }
2673
2674 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2675    HI20_SLO is for the add3 and load/store with displacement instructions.
2676    HI20 is for the or3 instruction.
2677    For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2678    the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2679    we must add one to the high 16 bytes (which will get subtracted off when
2680    the low 16 bits are added).
2681    These relocs have to be done in combination with an R_NDS32_LO12 reloc
2682    because there is a carry from the LO12 to the HI20.  Here we just save
2683    the information we need; we do the actual relocation when we see the LO12.
2684    This code is copied from the elf32-mips.c.  We also support an arbitrary
2685    number of HI20 relocs to be associated with a single LO12 reloc.  The
2686    assembler sorts the relocs to ensure each HI20 immediately precedes its
2687    LO12.  However if there are multiple copies, the assembler may not find
2688    the real LO12 so it picks the first one it finds.  */
2689
2690 struct nds32_hi20
2691 {
2692   struct nds32_hi20 *next;
2693   bfd_byte *addr;
2694   bfd_vma addend;
2695 };
2696
2697 static struct nds32_hi20 *nds32_hi20_list;
2698
2699 static bfd_reloc_status_type
2700 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2701                       arelent *reloc_entry,
2702                       asymbol *symbol,
2703                       void *data,
2704                       asection *input_section,
2705                       bfd *output_bfd,
2706                       char **error_message ATTRIBUTE_UNUSED)
2707 {
2708   bfd_reloc_status_type ret;
2709   bfd_vma relocation;
2710   struct nds32_hi20 *n;
2711
2712   /* This part is from bfd_elf_generic_reloc.
2713      If we're relocating, and this an external symbol, we don't want
2714      to change anything.  */
2715   if (output_bfd != (bfd *) NULL
2716       && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2717     {
2718       reloc_entry->address += input_section->output_offset;
2719       return bfd_reloc_ok;
2720     }
2721
2722   /* Sanity check the address (offset in section).  */
2723   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2724     return bfd_reloc_outofrange;
2725
2726   ret = bfd_reloc_ok;
2727   if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2728     ret = bfd_reloc_undefined;
2729
2730   if (bfd_is_com_section (symbol->section))
2731     relocation = 0;
2732   else
2733     relocation = symbol->value;
2734
2735   relocation += symbol->section->output_section->vma;
2736   relocation += symbol->section->output_offset;
2737   relocation += reloc_entry->addend;
2738
2739   /* Save the information, and let LO12 do the actual relocation.  */
2740   n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2741   if (n == NULL)
2742     return bfd_reloc_outofrange;
2743
2744   n->addr = (bfd_byte *) data + reloc_entry->address;
2745   n->addend = relocation;
2746   n->next = nds32_hi20_list;
2747   nds32_hi20_list = n;
2748
2749   if (output_bfd != (bfd *) NULL)
2750     reloc_entry->address += input_section->output_offset;
2751
2752   return ret;
2753 }
2754
2755 /* Handle an NDS32 ELF HI20 reloc.  */
2756
2757 static void
2758 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2759                          int type ATTRIBUTE_UNUSED,
2760                          Elf_Internal_Rela *relhi,
2761                          Elf_Internal_Rela *rello,
2762                          bfd_byte *contents,
2763                          bfd_vma addend)
2764 {
2765   unsigned long insn;
2766   bfd_vma addlo;
2767
2768   insn = bfd_getb32 (contents + relhi->r_offset);
2769
2770   addlo = bfd_getb32 (contents + rello->r_offset);
2771   addlo &= 0xfff;
2772
2773   addend += ((insn & 0xfffff) << 20) + addlo;
2774
2775   insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2776   bfd_putb32 (insn, contents + relhi->r_offset);
2777 }
2778
2779 /* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
2780    inplace relocation; this function exists in order to do the
2781    R_NDS32_HI20_[SU]LO relocation described above.  */
2782
2783 static bfd_reloc_status_type
2784 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2785                       void *data, asection *input_section, bfd *output_bfd,
2786                       char **error_message)
2787 {
2788   /* This part is from bfd_elf_generic_reloc.
2789      If we're relocating, and this an external symbol, we don't want
2790      to change anything.  */
2791   if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2792       && reloc_entry->addend == 0)
2793     {
2794       reloc_entry->address += input_section->output_offset;
2795       return bfd_reloc_ok;
2796     }
2797
2798   if (nds32_hi20_list != NULL)
2799     {
2800       struct nds32_hi20 *l;
2801
2802       l = nds32_hi20_list;
2803       while (l != NULL)
2804         {
2805           unsigned long insn;
2806           unsigned long val;
2807           unsigned long vallo;
2808           struct nds32_hi20 *next;
2809
2810           /* Do the HI20 relocation.  Note that we actually don't need
2811              to know anything about the LO12 itself, except where to
2812              find the low 12 bits of the addend needed by the LO12.  */
2813           insn = bfd_getb32 (l->addr);
2814           vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2815           vallo &= 0xfff;
2816           switch (reloc_entry->howto->type)
2817             {
2818             case R_NDS32_LO12S3:
2819               vallo <<= 3;
2820               break;
2821
2822             case R_NDS32_LO12S2:
2823               vallo <<= 2;
2824               break;
2825
2826             case R_NDS32_LO12S1:
2827               vallo <<= 1;
2828               break;
2829
2830             case R_NDS32_LO12S0:
2831               vallo <<= 0;
2832               break;
2833             }
2834
2835           val = ((insn & 0xfffff) << 12) + vallo;
2836           val += l->addend;
2837
2838           insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2839           bfd_putb32 ((bfd_vma) insn, l->addr);
2840
2841           next = l->next;
2842           free (l);
2843           l = next;
2844         }
2845
2846       nds32_hi20_list = NULL;
2847     }
2848
2849   /* Now do the LO12 reloc in the usual way.
2850      ??? It would be nice to call bfd_elf_generic_reloc here,
2851      but we have partial_inplace set.  bfd_elf_generic_reloc will
2852      pass the handling back to bfd_install_relocation which will install
2853      a section relative addend which is wrong.  */
2854   return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2855                                   input_section, output_bfd, error_message);
2856 }
2857
2858 /* Do generic partial_inplace relocation.
2859    This is a local replacement for bfd_elf_generic_reloc.  */
2860
2861 static bfd_reloc_status_type
2862 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2863                          asymbol *symbol, void *data, asection *input_section,
2864                          bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2865 {
2866   bfd_reloc_status_type ret;
2867   bfd_vma relocation;
2868   bfd_byte *inplace_address;
2869
2870   /* This part is from bfd_elf_generic_reloc.
2871      If we're relocating, and this an external symbol, we don't want
2872      to change anything.  */
2873   if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2874       && reloc_entry->addend == 0)
2875     {
2876       reloc_entry->address += input_section->output_offset;
2877       return bfd_reloc_ok;
2878     }
2879
2880   /* Now do the reloc in the usual way.
2881      ??? It would be nice to call bfd_elf_generic_reloc here,
2882      but we have partial_inplace set.  bfd_elf_generic_reloc will
2883      pass the handling back to bfd_install_relocation which will install
2884      a section relative addend which is wrong.  */
2885
2886   /* Sanity check the address (offset in section).  */
2887   if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2888     return bfd_reloc_outofrange;
2889
2890   ret = bfd_reloc_ok;
2891   if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2892     ret = bfd_reloc_undefined;
2893
2894   if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2895     relocation = 0;
2896   else
2897     relocation = symbol->value;
2898
2899   /* Only do this for a final link.  */
2900   if (output_bfd == (bfd *) NULL)
2901     {
2902       relocation += symbol->section->output_section->vma;
2903       relocation += symbol->section->output_offset;
2904     }
2905
2906   relocation += reloc_entry->addend;
2907   switch (reloc_entry->howto->type)
2908     {
2909     case R_NDS32_LO12S3:
2910       relocation >>= 3;
2911       break;
2912
2913     case R_NDS32_LO12S2:
2914       relocation >>= 2;
2915       break;
2916
2917     case R_NDS32_LO12S1:
2918       relocation >>= 1;
2919       break;
2920
2921     case R_NDS32_LO12S0:
2922     default:
2923       relocation >>= 0;
2924       break;
2925     }
2926
2927   inplace_address = (bfd_byte *) data + reloc_entry->address;
2928
2929 #define DOIT(x)                                         \
2930   x = ((x & ~reloc_entry->howto->dst_mask) |            \
2931   (((x & reloc_entry->howto->src_mask) +  relocation) & \
2932   reloc_entry->howto->dst_mask))
2933
2934   switch (reloc_entry->howto->size)
2935     {
2936     case 1:
2937       {
2938         short x = bfd_getb16 (inplace_address);
2939
2940         DOIT (x);
2941         bfd_putb16 ((bfd_vma) x, inplace_address);
2942       }
2943       break;
2944     case 2:
2945       {
2946         unsigned long x = bfd_getb32 (inplace_address);
2947
2948         DOIT (x);
2949         bfd_putb32 ((bfd_vma) x, inplace_address);
2950       }
2951       break;
2952     default:
2953       BFD_ASSERT (0);
2954     }
2955
2956   if (output_bfd != (bfd *) NULL)
2957     reloc_entry->address += input_section->output_offset;
2958
2959   return ret;
2960 }
2961
2962 /* Handle the R_NDS32_SDA15 reloc.
2963    This reloc is used to compute the address of objects in the small data area
2964    and to perform loads and stores from that area.
2965    The lower 15 bits are sign extended and added to the register specified
2966    in the instruction, which is assumed to point to _SDA_BASE_.
2967
2968    Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2969    the access size, this must be taken care of.  */
2970
2971 static bfd_reloc_status_type
2972 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2973                        asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2974                        asection *input_section, bfd *output_bfd,
2975                        char **error_message ATTRIBUTE_UNUSED)
2976 {
2977   /* This part is from bfd_elf_generic_reloc.  */
2978   if (output_bfd != (bfd *) NULL
2979       && (symbol->flags & BSF_SECTION_SYM) == 0
2980       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2981     {
2982       reloc_entry->address += input_section->output_offset;
2983       return bfd_reloc_ok;
2984     }
2985
2986   if (output_bfd != NULL)
2987     {
2988       /* FIXME: See bfd_perform_relocation.  Is this right?  */
2989       return bfd_reloc_continue;
2990     }
2991
2992   /* FIXME: not sure what to do here yet.  But then again, the linker
2993      may never call us.  */
2994   abort ();
2995 }
2996
2997 /* nds32_elf_ignore_reloc is the special function for
2998    relocation types which don't need to be relocated
2999    like relaxation relocation types.
3000    This function simply return bfd_reloc_ok when it is
3001    invoked.  */
3002
3003 static bfd_reloc_status_type
3004 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3005                         asymbol *symbol ATTRIBUTE_UNUSED,
3006                         void *data ATTRIBUTE_UNUSED, asection *input_section,
3007                         bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3008 {
3009   if (output_bfd != NULL)
3010     reloc_entry->address += input_section->output_offset;
3011
3012   return bfd_reloc_ok;
3013 }
3014 \f
3015
3016 /* Map BFD reloc types to NDS32 ELF reloc types.  */
3017
3018 struct nds32_reloc_map_entry
3019 {
3020   bfd_reloc_code_real_type bfd_reloc_val;
3021   unsigned char elf_reloc_val;
3022 };
3023
3024 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3025 {
3026   {BFD_RELOC_NONE, R_NDS32_NONE},
3027   {BFD_RELOC_16, R_NDS32_16_RELA},
3028   {BFD_RELOC_32, R_NDS32_32_RELA},
3029   {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3030   {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3031
3032   {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3033   {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3034   {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3035   {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3036   {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3037   {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3038   {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3039   {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3040   {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3041   {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3042   {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3043   {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3044   {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3045   {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3046   {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3047   {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3048   {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3049   {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3050   {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3051   {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3052   {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3053   {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3054   {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3055   {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3056   {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3057   {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3058   {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3059   {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3060   {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3061   {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3062   {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3063   {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3064   {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3065   {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3066   {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3067   {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3068   {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3069   {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3070   {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3071   {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3072   {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3073   {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3074   {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3075   {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3076   {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3077   {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3078   {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3079   {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3080   {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3081   {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3082   {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3083   {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3084   {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3085   {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3086   {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3087   {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3088   {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3089   {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3090   {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3091   {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3092   {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3093   {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3094   {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3095   {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3096   {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3097   {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3098   {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3099   {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3100   {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3101   {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3102   {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3103   {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3104   {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3105   {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3106   {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3107   {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3108   {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3109   {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3110   {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3111   {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3112   {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3113   {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3114   {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3115   {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3116   {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3117   {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3118   {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3119   {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3120   {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3121   {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3122   {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3123   {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3124   {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3125   {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3126   {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3127   {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3128   {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3129   {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3130   {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3131   {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3132   {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3133   {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3134   /* Not sure.  */
3135   {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3136   /* Missing: BFD_RELOC_NDS32_GOTTPOFF.  */
3137   {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3138   {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3139   {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3140   {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3141   {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3142   {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3143   {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3144   {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3145   {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3146   {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3147   {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3148   {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3149   {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3150   {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3151   {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3152   {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3153   {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3154   {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3155   {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3156   {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3157   {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3158   {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3159   {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3160   {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3161   {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3162   {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3163   {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3164 };
3165
3166 /* Patch tag.  */
3167
3168 /* Reserve space for COUNT dynamic relocations in relocation selection
3169    SRELOC.  */
3170
3171 static inline void
3172 elf32_nds32_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
3173                                 bfd_size_type count)
3174 {
3175   BFD_ASSERT (elf_hash_table (info)->dynamic_sections_created);
3176   if (sreloc == NULL)
3177     abort ();
3178   sreloc->size += sizeof (Elf32_External_Rela) * count;
3179 }
3180
3181 static reloc_howto_type *
3182 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3183                                  const char *r_name)
3184 {
3185   unsigned int i;
3186
3187   for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3188     if (nds32_elf_howto_table[i].name != NULL
3189         && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3190       return &nds32_elf_howto_table[i];
3191
3192   for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3193     if (nds32_elf_relax_howto_table[i].name != NULL
3194         && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3195       return &nds32_elf_relax_howto_table[i];
3196
3197   return NULL;
3198 }
3199
3200 static reloc_howto_type *
3201 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3202 {
3203   if (code < R_NDS32_RELAX_ENTRY)
3204     {
3205       BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3206       return &nds32_elf_howto_table[code];
3207     }
3208   else
3209     {
3210       if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3211           >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3212         {
3213           int i = code;
3214           i += 1;
3215         }
3216
3217       BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3218                   < ARRAY_SIZE (nds32_elf_relax_howto_table));
3219       return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3220     }
3221 }
3222
3223 static reloc_howto_type *
3224 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3225                                  bfd_reloc_code_real_type code)
3226 {
3227   unsigned int i;
3228
3229   for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3230     {
3231       if (nds32_reloc_map[i].bfd_reloc_val == code)
3232         return bfd_elf32_bfd_reloc_type_table_lookup
3233           (nds32_reloc_map[i].elf_reloc_val);
3234     }
3235
3236   return NULL;
3237 }
3238
3239 /* Set the howto pointer for an NDS32 ELF reloc.  */
3240
3241 static bfd_boolean
3242 nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3243                          Elf_Internal_Rela *dst)
3244 {
3245   enum elf_nds32_reloc_type r_type;
3246
3247   r_type = ELF32_R_TYPE (dst->r_info);
3248   if (r_type > R_NDS32_GNU_VTENTRY)
3249     {
3250       /* xgettext:c-format */
3251       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3252                           abfd, r_type);
3253       bfd_set_error (bfd_error_bad_value);
3254       return FALSE;
3255     }
3256
3257   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
3258   cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3259   return TRUE;
3260 }
3261
3262 static bfd_boolean
3263 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3264                      Elf_Internal_Rela *dst)
3265 {
3266   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3267
3268   if ((r_type == R_NDS32_NONE)
3269       || ((r_type > R_NDS32_GNU_VTENTRY)
3270           && (r_type < R_NDS32_max)))
3271     {
3272       cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3273       return TRUE;
3274     }
3275
3276   /* xgettext:c-format */
3277   _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3278   bfd_set_error (bfd_error_bad_value);
3279   return FALSE;
3280 }
3281
3282 /* Support for core dump NOTE sections.
3283    Reference to include/linux/elfcore.h in Linux.  */
3284
3285 static bfd_boolean
3286 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3287 {
3288   int offset;
3289   size_t size;
3290
3291   switch (note->descsz)
3292     {
3293     case 0x114:
3294       /* Linux/NDS32 32-bit, ABI1.  */
3295
3296       /* pr_cursig */
3297       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3298
3299       /* pr_pid */
3300       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3301
3302       /* pr_reg */
3303       offset = 72;
3304       size = 200;
3305       break;
3306
3307     case 0xfc:
3308       /* Linux/NDS32 32-bit.  */
3309
3310       /* pr_cursig */
3311       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3312
3313       /* pr_pid */
3314       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3315
3316       /* pr_reg */
3317       offset = 72;
3318       size = 176;
3319       break;
3320
3321     default:
3322       return FALSE;
3323     }
3324
3325   /* Make a ".reg" section.  */
3326   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3327                                           size, note->descpos + offset);
3328 }
3329
3330 static bfd_boolean
3331 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3332 {
3333   switch (note->descsz)
3334     {
3335     case 124:
3336       /* Linux/NDS32.  */
3337
3338       /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
3339       elf_tdata (abfd)->core->program =
3340         _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3341       elf_tdata (abfd)->core->command =
3342         _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3343       break;
3344
3345     default:
3346       return FALSE;
3347     }
3348
3349   /* Note that for some reason, a spurious space is tacked
3350      onto the end of the args in some (at least one anyway)
3351      implementations, so strip it off if it exists.  */
3352   {
3353     char *command = elf_tdata (abfd)->core->command;
3354     int n = strlen (command);
3355
3356     if (0 < n && command[n - 1] == ' ')
3357       command[n - 1] = '\0';
3358   }
3359
3360   return TRUE;
3361 }
3362
3363 /* Hook called by the linker routine which adds symbols from an object
3364    file.  We must handle the special NDS32 section numbers here.
3365    We also keep watching for whether we need to create the sdata special
3366    linker sections.  */
3367
3368 static bfd_boolean
3369 nds32_elf_add_symbol_hook (bfd *abfd,
3370                            struct bfd_link_info *info ATTRIBUTE_UNUSED,
3371                            Elf_Internal_Sym *sym,
3372                            const char **namep ATTRIBUTE_UNUSED,
3373                            flagword *flagsp ATTRIBUTE_UNUSED,
3374                            asection **secp, bfd_vma *valp)
3375 {
3376   switch (sym->st_shndx)
3377     {
3378     case SHN_COMMON:
3379       /* Common symbols less than the GP size are automatically
3380          treated as SHN_MIPS_SCOMMON symbols.  */
3381       if (sym->st_size > elf_gp_size (abfd)
3382           || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3383         break;
3384
3385       /* st_value is the alignemnt constraint.
3386          That might be its actual size if it is an array or structure.  */
3387       switch (sym->st_value)
3388         {
3389         case 1:
3390           *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3391           break;
3392         case 2:
3393           *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3394           break;
3395         case 4:
3396           *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3397           break;
3398         case 8:
3399           *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3400           break;
3401         default:
3402           return TRUE;
3403         }
3404
3405       (*secp)->flags |= SEC_IS_COMMON;
3406       *valp = sym->st_size;
3407       break;
3408     }
3409
3410   return TRUE;
3411 }
3412
3413 /* This function can figure out the best location for a base register to access
3414    data relative to this base register
3415    INPUT:
3416    sda_d0: size of first DOUBLE WORD data section
3417    sda_w0: size of first WORD data section
3418    sda_h0: size of first HALF WORD data section
3419    sda_b : size of BYTE data section
3420    sda_hi: size of second HALF WORD data section
3421    sda_w1: size of second WORD data section
3422    sda_d1: size of second DOUBLE WORD data section
3423    OUTPUT:
3424    offset (always positive) from the beginning of sda_d0 if OK
3425    a negative error value if fail
3426    NOTE:
3427    these 7 sections have to be located back to back if exist
3428    a pass in 0 value for non-existing section   */
3429
3430 /* Due to the interpretation of simm15 field of load/store depending on
3431    data accessing size, the organization of base register relative data shall
3432    like the following figure
3433    -------------------------------------------
3434    |  DOUBLE WORD sized data (range +/- 128K)
3435    -------------------------------------------
3436    |  WORD sized data (range +/- 64K)
3437    -------------------------------------------
3438    |  HALF WORD sized data (range +/- 32K)
3439    -------------------------------------------
3440    |  BYTE sized data (range +/- 16K)
3441    -------------------------------------------
3442    |  HALF WORD sized data (range +/- 32K)
3443    -------------------------------------------
3444    |  WORD sized data (range +/- 64K)
3445    -------------------------------------------
3446    |  DOUBLE WORD sized data (range +/- 128K)
3447    -------------------------------------------
3448    Its base register shall be set to access these data freely.  */
3449
3450 /* We have to figure out the SDA_BASE value, so that we can adjust the
3451    symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3452    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3453    target data.  We don't need to adjust the symbol value for an
3454    external symbol if we are producing relocatable output.  */
3455
3456 static asection *sda_rela_sec = NULL;
3457
3458 #define SDA_SECTION_NUM 10
3459
3460 static bfd_reloc_status_type
3461 nds32_elf_final_sda_base (bfd *                   output_bfd,
3462                           struct bfd_link_info *  info,
3463                           bfd_vma *               psb,
3464                           bfd_boolean             add_symbol)
3465 {
3466   int relax_fp_as_gp;
3467   struct elf_nds32_link_hash_table *table;
3468   struct bfd_link_hash_entry *h, *h2;
3469   long unsigned int total = 0;
3470   asection *first = NULL, *final = NULL, *temp;
3471   bfd_vma sda_base = 0;
3472
3473   h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3474   if (!h || (h->type != bfd_link_hash_defined
3475              && h->type != bfd_link_hash_defweak))
3476     {
3477       /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3478          4 byte-aligned.  Therefore, it has to set the first section ".data"
3479          4 byte-aligned.  */
3480       static const char sec_name[SDA_SECTION_NUM][10] =
3481         {
3482           ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3483           ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3484         };
3485       size_t i = 0;
3486
3487       if (output_bfd->sections == NULL)
3488         {
3489           *psb = elf_gp (output_bfd);
3490           return bfd_reloc_ok;
3491         }
3492
3493       /* Get the first and final section.  */
3494       while (i < ARRAY_SIZE (sec_name))
3495         {
3496           temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3497           if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3498             first = temp;
3499           if (temp && (temp->size != 0 || temp->rawsize != 0))
3500             final = temp;
3501
3502           /* Summarize the sections in order to check if joining .bss.  */
3503           if (temp && temp->size != 0)
3504             total += temp->size;
3505           else if (temp && temp->rawsize != 0)
3506             total += temp->rawsize;
3507
3508           i++;
3509         }
3510
3511       /* Check .bss size.  */
3512       temp = bfd_get_section_by_name (output_bfd, ".bss");
3513       if (temp)
3514         {
3515           if (temp->size != 0)
3516             total += temp->size;
3517           else if (temp->rawsize != 0)
3518             total += temp->rawsize;
3519
3520           if (total < 0x80000)
3521             {
3522               if (!first && (temp->size != 0 || temp->rawsize != 0))
3523                 first = temp;
3524               if ((temp->size != 0 || temp->rawsize != 0))
3525                 final = temp;
3526             }
3527         }
3528
3529       if (first && final)
3530         {
3531           /* The middle of data region.  */
3532           sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3533
3534           /* Find the section sda_base located.  */
3535           i = 0;
3536           while (i < ARRAY_SIZE (sec_name))
3537             {
3538               final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3539               if (final && (final->size != 0 || final->rawsize != 0)
3540                   && sda_base >= final->vma)
3541                 {
3542                   first = final;
3543                   i++;
3544                 }
3545               else
3546                 break;
3547             }
3548         }
3549       else
3550         {
3551           /* If there is not any default data section in output bfd, try to find
3552              the first data section.  If no data section be found, just simplily
3553              choose the first output section.  */
3554           temp = output_bfd->sections;
3555           while (temp)
3556             {
3557               if (temp->flags & SEC_ALLOC
3558                   && (((temp->flags & SEC_DATA)
3559                        && ((temp->flags & SEC_READONLY) == 0))
3560                       || (temp->flags & SEC_LOAD) == 0)
3561                   && (temp->size != 0 || temp->rawsize != 0))
3562                 {
3563                   if (!first)
3564                     first = temp;
3565                   final = temp;
3566                 }
3567               temp = temp->next;
3568             }
3569
3570           /* There is no data or bss section.  */
3571           if (!first || (first->size == 0 && first->rawsize == 0))
3572             {
3573               first = output_bfd->sections;
3574               while (first && first->size == 0 && first->rawsize == 0)
3575                 first = first->next;
3576             }
3577
3578           /* There is no concrete section.  */
3579           if (!first)
3580             {
3581               *psb = elf_gp (output_bfd);
3582               return bfd_reloc_ok;
3583             }
3584
3585           if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3586             sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3587           else
3588             sda_base = first->vma + 0x2000;
3589         }
3590
3591       sda_base -= first->vma;
3592       sda_base = sda_base & (~7);
3593
3594       if (!_bfd_generic_link_add_one_symbol
3595           (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3596            (bfd_vma) sda_base, (const char *) NULL, FALSE,
3597            get_elf_backend_data (output_bfd)->collect, &h))
3598         return FALSE;
3599
3600       sda_rela_sec = first;
3601     }
3602
3603   /* Set _FP_BASE_ to _SDA_BASE_.  */
3604   table = nds32_elf_hash_table (info);
3605   relax_fp_as_gp = table->relax_fp_as_gp;
3606   h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3607   /* _SDA_BASE_ is difined in linker script.  */
3608   if (!first)
3609     {
3610       first = h->u.def.section;
3611       sda_base = h->u.def.value;
3612     }
3613
3614   if (relax_fp_as_gp && h2
3615       && (h2->type == bfd_link_hash_undefweak
3616           || h2->type == bfd_link_hash_undefined))
3617     {
3618       /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3619          And set FP equal to SDA_BASE to do relaxation for
3620          la $fp, _FP_BASE_.  */
3621       if (!_bfd_generic_link_add_one_symbol
3622           (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3623            first, sda_base, (const char *) NULL,
3624            FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3625         return FALSE;
3626     }
3627
3628   if (add_symbol)
3629     {
3630       if (h)
3631         {
3632           /* Now set gp.  */
3633           elf_gp (output_bfd) = (h->u.def.value
3634                                  + h->u.def.section->output_section->vma
3635                                  + h->u.def.section->output_offset);
3636         }
3637       else
3638         {
3639           _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3640           return bfd_reloc_dangerous;
3641         }
3642     }
3643
3644   *psb = h->u.def.value
3645     + h->u.def.section->output_section->vma
3646     + h->u.def.section->output_offset;
3647   return bfd_reloc_ok;
3648 }
3649 \f
3650
3651 /* Return size of a PLT entry.  */
3652 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3653
3654 /* Create an entry in an nds32 ELF linker hash table.  */
3655
3656 static struct bfd_hash_entry *
3657 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3658                              struct bfd_hash_table *table,
3659                              const char *string)
3660 {
3661   struct elf_nds32_link_hash_entry *ret;
3662
3663   ret = (struct elf_nds32_link_hash_entry *) entry;
3664
3665   /* Allocate the structure if it has not already been allocated by a
3666      subclass.  */
3667   if (ret == NULL)
3668     ret = (struct elf_nds32_link_hash_entry *)
3669        bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3670
3671   if (ret == NULL)
3672     return (struct bfd_hash_entry *) ret;
3673
3674   /* Call the allocation method of the superclass.  */
3675   ret = (struct elf_nds32_link_hash_entry *)
3676     _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3677
3678   if (ret != NULL)
3679     {
3680       struct elf_nds32_link_hash_entry *eh;
3681
3682       eh = (struct elf_nds32_link_hash_entry *) ret;
3683       eh->dyn_relocs = NULL;
3684       eh->tls_type = GOT_UNKNOWN;
3685       eh->offset_to_gp = 0;
3686     }
3687
3688   return (struct bfd_hash_entry *) ret;
3689 }
3690
3691 /* Create an nds32 ELF linker hash table.  */
3692
3693 static struct bfd_link_hash_table *
3694 nds32_elf_link_hash_table_create (bfd *abfd)
3695 {
3696   struct elf_nds32_link_hash_table *ret;
3697
3698   bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3699
3700   ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3701   if (ret == NULL)
3702     return NULL;
3703
3704   /* Patch tag.  */
3705   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3706                                       nds32_elf_link_hash_newfunc,
3707                                       sizeof (struct elf_nds32_link_hash_entry),
3708                                       NDS32_ELF_DATA))
3709     {
3710       free (ret);
3711       return NULL;
3712     }
3713
3714   ret->sdynbss = NULL;
3715   ret->srelbss = NULL;
3716   ret->sym_ld_script = NULL;
3717
3718   return &ret->root.root;
3719 }
3720
3721 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3722    shortcuts to them in our hash table.  */
3723
3724 static bfd_boolean
3725 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3726 {
3727   struct elf_link_hash_table *ehtab;
3728
3729   if (!_bfd_elf_create_got_section (dynobj, info))
3730     return FALSE;
3731
3732   ehtab = elf_hash_table (info);
3733   ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3734   ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3735   if (!ehtab->sgot || !ehtab->sgotplt)
3736     abort ();
3737
3738   /* _bfd_elf_create_got_section will create it for us.  */
3739   ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3740   if (ehtab->srelgot == NULL
3741       || !bfd_set_section_flags (dynobj, ehtab->srelgot,
3742                                  (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3743                                   | SEC_IN_MEMORY | SEC_LINKER_CREATED
3744                                   | SEC_READONLY))
3745       || !bfd_set_section_alignment (dynobj, ehtab->srelgot, 2))
3746     return FALSE;
3747
3748   return TRUE;
3749 }
3750
3751 /* Create dynamic sections when linking against a dynamic object.  */
3752
3753 static bfd_boolean
3754 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3755 {
3756   struct elf_link_hash_table *ehtab;
3757   struct elf_nds32_link_hash_table *htab;
3758   flagword flags, pltflags;
3759   register asection *s;
3760   const struct elf_backend_data *bed;
3761   int ptralign = 2;             /* 32-bit  */
3762   const char *secname;
3763   char *relname;
3764   flagword secflags;
3765   asection *sec;
3766
3767   bed = get_elf_backend_data (abfd);
3768   ehtab = elf_hash_table (info);
3769   htab = nds32_elf_hash_table (info);
3770
3771   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3772      .rel[a].bss sections.  */
3773
3774   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3775            | SEC_LINKER_CREATED);
3776
3777   pltflags = flags;
3778   pltflags |= SEC_CODE;
3779   if (bed->plt_not_loaded)
3780     pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3781   if (bed->plt_readonly)
3782     pltflags |= SEC_READONLY;
3783
3784   s = bfd_make_section (abfd, ".plt");
3785   ehtab->splt = s;
3786   if (s == NULL
3787       || !bfd_set_section_flags (abfd, s, pltflags)
3788       || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3789     return FALSE;
3790
3791   if (bed->want_plt_sym)
3792     {
3793       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3794          .plt section.  */
3795       struct bfd_link_hash_entry *bh = NULL;
3796       struct elf_link_hash_entry *h;
3797
3798       if (!(_bfd_generic_link_add_one_symbol
3799             (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3800              (bfd_vma) 0, (const char *) NULL, FALSE,
3801              get_elf_backend_data (abfd)->collect, &bh)))
3802         return FALSE;
3803
3804       h = (struct elf_link_hash_entry *) bh;
3805       h->def_regular = 1;
3806       h->type = STT_OBJECT;
3807
3808       if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3809         return FALSE;
3810     }
3811
3812   s = bfd_make_section (abfd,
3813                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3814   ehtab->srelplt = s;
3815   if (s == NULL
3816       || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3817       || !bfd_set_section_alignment (abfd, s, ptralign))
3818     return FALSE;
3819
3820   if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3821     return FALSE;
3822
3823   for (sec = abfd->sections; sec; sec = sec->next)
3824     {
3825       secflags = bfd_get_section_flags (abfd, sec);
3826       if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3827           || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3828         continue;
3829       secname = bfd_get_section_name (abfd, sec);
3830       relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3831       strcpy (relname, ".rela");
3832       strcat (relname, secname);
3833       if (bfd_get_section_by_name (abfd, secname))
3834         continue;
3835       s = bfd_make_section (abfd, relname);
3836       if (s == NULL
3837           || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3838           || !bfd_set_section_alignment (abfd, s, ptralign))
3839         return FALSE;
3840     }
3841
3842   if (bed->want_dynbss)
3843     {
3844       /* The .dynbss section is a place to put symbols which are defined
3845          by dynamic objects, are referenced by regular objects, and are
3846          not functions.  We must allocate space for them in the process
3847          image and use a R_*_COPY reloc to tell the dynamic linker to
3848          initialize them at run time.  The linker script puts the .dynbss
3849          section into the .bss section of the final image.  */
3850       s = bfd_make_section (abfd, ".dynbss");
3851       htab->sdynbss = s;
3852       if (s == NULL
3853           || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3854         return FALSE;
3855       /* The .rel[a].bss section holds copy relocs.  This section is not
3856          normally needed.  We need to create it here, though, so that the
3857          linker will map it to an output section.  We can't just create it
3858          only if we need it, because we will not know whether we need it
3859          until we have seen all the input files, and the first time the
3860          main linker code calls BFD after examining all the input files
3861          (size_dynamic_sections) the input sections have already been
3862          mapped to the output sections.  If the section turns out not to
3863          be needed, we can discard it later.  We will never need this
3864          section when generating a shared object, since they do not use
3865          copy relocs.  */
3866       if (!bfd_link_pic (info))
3867         {
3868           s = bfd_make_section (abfd, (bed->default_use_rela_p
3869                                        ? ".rela.bss" : ".rel.bss"));
3870           htab->srelbss = s;
3871           if (s == NULL
3872               || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3873               || !bfd_set_section_alignment (abfd, s, ptralign))
3874             return FALSE;
3875         }
3876     }
3877
3878   return TRUE;
3879 }
3880
3881 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3882 static void
3883 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3884                                 struct elf_link_hash_entry *dir,
3885                                 struct elf_link_hash_entry *ind)
3886 {
3887   struct elf_nds32_link_hash_entry *edir, *eind;
3888
3889   edir = (struct elf_nds32_link_hash_entry *) dir;
3890   eind = (struct elf_nds32_link_hash_entry *) ind;
3891
3892   if (eind->dyn_relocs != NULL)
3893     {
3894       if (edir->dyn_relocs != NULL)
3895         {
3896           struct elf_dyn_relocs **pp;
3897           struct elf_dyn_relocs *p;
3898
3899           if (ind->root.type == bfd_link_hash_indirect)
3900             abort ();
3901
3902           /* Add reloc counts against the weak sym to the strong sym
3903              list.  Merge any entries against the same section.  */
3904           for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3905             {
3906               struct elf_dyn_relocs *q;
3907
3908               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3909                 if (q->sec == p->sec)
3910                   {
3911                     q->pc_count += p->pc_count;
3912                     q->count += p->count;
3913                     *pp = p->next;
3914                     break;
3915                   }
3916               if (q == NULL)
3917                 pp = &p->next;
3918             }
3919           *pp = edir->dyn_relocs;
3920         }
3921
3922       edir->dyn_relocs = eind->dyn_relocs;
3923       eind->dyn_relocs = NULL;
3924     }
3925
3926   if (ind->root.type == bfd_link_hash_indirect)
3927     {
3928       if (dir->got.refcount <= 0)
3929         {
3930           edir->tls_type = eind->tls_type;
3931           eind->tls_type = GOT_UNKNOWN;
3932         }
3933     }
3934
3935   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3936 }
3937 \f
3938
3939 /* Find dynamic relocs for H that apply to read-only sections.  */
3940
3941 static asection *
3942 readonly_dynrelocs (struct elf_link_hash_entry *h)
3943 {
3944   struct elf_dyn_relocs *p;
3945
3946   for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3947     {
3948       asection *s = p->sec->output_section;
3949
3950       if (s != NULL && (s->flags & SEC_READONLY) != 0)
3951         return p->sec;
3952     }
3953   return NULL;
3954 }
3955
3956 /* Adjust a symbol defined by a dynamic object and referenced by a
3957    regular object.  The current definition is in some section of the
3958    dynamic object, but we're not including those sections.  We have to
3959    change the definition to something the rest of the link can
3960    understand.  */
3961
3962 static bfd_boolean
3963 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3964                                  struct elf_link_hash_entry *h)
3965 {
3966   struct elf_nds32_link_hash_table *htab;
3967   bfd *dynobj;
3968   asection *s;
3969   unsigned int power_of_two;
3970
3971   dynobj = elf_hash_table (info)->dynobj;
3972
3973   /* Make sure we know what is going on here.  */
3974   BFD_ASSERT (dynobj != NULL
3975               && (h->needs_plt
3976                   || h->is_weakalias
3977                   || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3978
3979
3980   /* If this is a function, put it in the procedure linkage table.  We
3981      will fill in the contents of the procedure linkage table later,
3982      when we know the address of the .got section.  */
3983   if (h->type == STT_FUNC || h->needs_plt)
3984     {
3985       if (!bfd_link_pic (info)
3986           && !h->def_dynamic
3987           && !h->ref_dynamic
3988           && h->root.type != bfd_link_hash_undefweak
3989           && h->root.type != bfd_link_hash_undefined)
3990         {
3991           /* This case can occur if we saw a PLT reloc in an input
3992              file, but the symbol was never referred to by a dynamic
3993              object.  In such a case, we don't actually need to build
3994              a procedure linkage table, and we can just do a PCREL
3995              reloc instead.  */
3996           h->plt.offset = (bfd_vma) - 1;
3997           h->needs_plt = 0;
3998         }
3999
4000       return TRUE;
4001     }
4002   else
4003     h->plt.offset = (bfd_vma) - 1;
4004
4005   /* If this is a weak symbol, and there is a real definition, the
4006      processor independent code will have arranged for us to see the
4007      real definition first, and we can just use the same value.  */
4008   if (h->is_weakalias)
4009     {
4010       struct elf_link_hash_entry *def = weakdef (h);
4011       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4012       h->root.u.def.section = def->root.u.def.section;
4013       h->root.u.def.value = def->root.u.def.value;
4014       return TRUE;
4015     }
4016
4017   /* This is a reference to a symbol defined by a dynamic object which
4018      is not a function.  */
4019
4020   /* If we are creating a shared library, we must presume that the
4021      only references to the symbol are via the global offset table.
4022      For such cases we need not do anything here; the relocations will
4023      be handled correctly by relocate_section.  */
4024   if (bfd_link_pic (info))
4025     return TRUE;
4026
4027   /* If there are no references to this symbol that do not use the
4028      GOT, we don't need to generate a copy reloc.  */
4029   if (!h->non_got_ref)
4030     return TRUE;
4031
4032   /* If -z nocopyreloc was given, we won't generate them either.  */
4033   if (0 && info->nocopyreloc)
4034     {
4035       h->non_got_ref = 0;
4036       return TRUE;
4037     }
4038
4039   /* If we don't find any dynamic relocs in read-only sections, then
4040      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4041   if (!readonly_dynrelocs (h))
4042     {
4043       h->non_got_ref = 0;
4044       return TRUE;
4045     }
4046
4047   /* We must allocate the symbol in our .dynbss section, which will
4048      become part of the .bss section of the executable.  There will be
4049      an entry for this symbol in the .dynsym section.  The dynamic
4050      object will contain position independent code, so all references
4051      from the dynamic object to this symbol will go through the global
4052      offset table.  The dynamic linker will use the .dynsym entry to
4053      determine the address it must put in the global offset table, so
4054      both the dynamic object and the regular object will refer to the
4055      same memory location for the variable.  */
4056
4057   htab = nds32_elf_hash_table (info);
4058   s = htab->sdynbss;
4059   BFD_ASSERT (s != NULL);
4060
4061   /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4062      to copy the initial value out of the dynamic object and into the
4063      runtime process image.  We need to remember the offset into the
4064      .rela.bss section we are going to use.  */
4065   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4066     {
4067       asection *srel;
4068
4069       srel = htab->srelbss;
4070       BFD_ASSERT (srel != NULL);
4071       srel->size += sizeof (Elf32_External_Rela);
4072       h->needs_copy = 1;
4073     }
4074
4075   /* We need to figure out the alignment required for this symbol.  I
4076      have no idea how ELF linkers handle this.  */
4077   power_of_two = bfd_log2 (h->size);
4078   if (power_of_two > 3)
4079     power_of_two = 3;
4080
4081   /* Apply the required alignment.  */
4082   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4083   if (power_of_two > bfd_get_section_alignment (dynobj, s))
4084     {
4085       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
4086         return FALSE;
4087     }
4088
4089   /* Define the symbol as being at this point in the section.  */
4090   h->root.u.def.section = s;
4091   h->root.u.def.value = s->size;
4092
4093   /* Increment the section size to make room for the symbol.  */
4094   s->size += h->size;
4095
4096   return TRUE;
4097 }
4098
4099 /* Allocate space in .plt, .got and associated reloc sections for
4100    dynamic relocs.  */
4101
4102 static bfd_boolean
4103 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4104 {
4105   struct bfd_link_info *info;
4106   struct elf_link_hash_table *ehtab;
4107   struct elf_nds32_link_hash_table *htab;
4108   struct elf_nds32_link_hash_entry *eh;
4109   struct elf_dyn_relocs *p;
4110
4111   if (h->root.type == bfd_link_hash_indirect)
4112     return TRUE;
4113
4114   /* When warning symbols are created, they **replace** the "real"
4115      entry in the hash table, thus we never get to see the real
4116      symbol in a hash traversal. So look at it now.  */
4117   if (h->root.type == bfd_link_hash_warning)
4118     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4119
4120   eh = (struct elf_nds32_link_hash_entry *) h;
4121
4122   info = (struct bfd_link_info *) inf;
4123   ehtab = elf_hash_table (info);
4124   htab = nds32_elf_hash_table (info);
4125   if (htab == NULL)
4126     return FALSE;
4127
4128   eh = (struct elf_nds32_link_hash_entry *) h;
4129
4130   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4131       && h->plt.refcount > 0
4132       && !(bfd_link_pie (info) && h->def_regular))
4133     {
4134       /* Make sure this symbol is output as a dynamic symbol.
4135          Undefined weak syms won't yet be marked as dynamic.  */
4136       if (h->dynindx == -1 && !h->forced_local)
4137         {
4138           if (!bfd_elf_link_record_dynamic_symbol (info, h))
4139             return FALSE;
4140         }
4141
4142       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4143         {
4144           asection *s = ehtab->splt;
4145
4146           /* If this is the first .plt entry, make room for the special
4147              first entry.  */
4148           if (s->size == 0)
4149             s->size += PLT_ENTRY_SIZE;
4150
4151           h->plt.offset = s->size;
4152
4153           /* If this symbol is not defined in a regular file, and we are
4154              not generating a shared library, then set the symbol to this
4155              location in the .plt.  This is required to make function
4156              pointers compare as equal between the normal executable and
4157              the shared library.  */
4158           if (!bfd_link_pic (info) && !h->def_regular)
4159             {
4160               h->root.u.def.section = s;
4161               h->root.u.def.value = h->plt.offset;
4162             }
4163
4164           /* Make room for this entry.  */
4165           s->size += PLT_ENTRY_SIZE;
4166
4167           /* We also need to make an entry in the .got.plt section, which
4168              will be placed in the .got section by the linker script.  */
4169           ehtab->sgotplt->size += 4;
4170
4171           /* We also need to make an entry in the .rel.plt section.  */
4172           ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4173           if (htab->tls_desc_trampoline)
4174             htab->next_tls_desc_index++;
4175         }
4176       else
4177         {
4178           h->plt.offset = (bfd_vma) - 1;
4179           h->needs_plt = 0;
4180         }
4181     }
4182   else
4183     {
4184       h->plt.offset = (bfd_vma) - 1;
4185       h->needs_plt = 0;
4186     }
4187
4188   if (h->got.refcount > 0)
4189     {
4190       asection *sgot;
4191       bfd_boolean dyn;
4192       int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4193
4194       /* Make sure this symbol is output as a dynamic symbol.
4195          Undefined weak syms won't yet be marked as dynamic.  */
4196       if (h->dynindx == -1 && !h->forced_local)
4197         {
4198           if (!bfd_elf_link_record_dynamic_symbol (info, h))
4199             return FALSE;
4200         }
4201
4202       sgot = elf_hash_table (info)->sgot;
4203       h->got.offset = sgot->size;
4204
4205       if (tls_type == GOT_UNKNOWN)
4206         abort ();
4207
4208       /* Non-TLS symbols, and TLS_IE need one GOT slot.  */
4209       if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4210         sgot->size += 4;
4211       else
4212         {
4213           /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots.  */
4214           if (tls_type & GOT_TLS_DESC)
4215             sgot->size += 8;
4216         }
4217
4218       dyn = htab->root.dynamic_sections_created;
4219
4220       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4221         {
4222           if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4223             {
4224               /* TLS_DESC with trampoline needs a relocation slot
4225                  within .rela.plt.  */
4226               htab->num_tls_desc++;
4227               ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4228               htab->tls_trampoline = -1;
4229             }
4230           else
4231             {
4232               /* other relocations, including TLS_DESC without trampoline, need
4233                  a relocation slot within .rela.got.  */
4234               ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4235             }
4236         }
4237     }
4238   else
4239     h->got.offset = (bfd_vma)-1;
4240
4241   if (eh->dyn_relocs == NULL)
4242     return TRUE;
4243
4244   /* In the shared -Bsymbolic case, discard space allocated for
4245      dynamic pc-relative relocs against symbols which turn out to be
4246      defined in regular objects.  For the normal shared case, discard
4247      space for pc-relative relocs that have become local due to symbol
4248      visibility changes.  */
4249
4250   if (bfd_link_pic (info))
4251     {
4252       if (h->def_regular && (h->forced_local || info->symbolic))
4253         {
4254           struct elf_dyn_relocs **pp;
4255
4256           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
4257             {
4258               p->count -= p->pc_count;
4259               p->pc_count = 0;
4260               if (p->count == 0)
4261                 *pp = p->next;
4262               else
4263                 pp = &p->next;
4264             }
4265         }
4266     }
4267   else
4268     {
4269       /* For the non-shared case, discard space for relocs against
4270          symbols which turn out to need copy relocs or are not dynamic.  */
4271
4272       if (!h->non_got_ref
4273           && ((h->def_dynamic
4274                && !h->def_regular)
4275               || (htab->root.dynamic_sections_created
4276                   && (h->root.type == bfd_link_hash_undefweak
4277                       || h->root.type == bfd_link_hash_undefined))))
4278         {
4279           /* Make sure this symbol is output as a dynamic symbol.
4280              Undefined weak syms won't yet be marked as dynamic.  */
4281           if (h->dynindx == -1 && !h->forced_local)
4282             {
4283               if (!bfd_elf_link_record_dynamic_symbol (info, h))
4284                 return FALSE;
4285             }
4286
4287           /* If that succeeded, we know we'll be keeping all the
4288              relocs.  */
4289           if (h->dynindx != -1)
4290             goto keep;
4291         }
4292
4293       eh->dyn_relocs = NULL;
4294
4295 keep:;
4296     }
4297
4298   /* Finally, allocate space.  */
4299   for (p = eh->dyn_relocs; p != NULL; p = p->next)
4300     {
4301       asection *sreloc = elf_section_data (p->sec)->sreloc;
4302       sreloc->size += p->count * sizeof (Elf32_External_Rela);
4303     }
4304
4305   return TRUE;
4306 }
4307
4308 /* Add relocation REL to the end of relocation section SRELOC.  */
4309
4310 static void
4311 elf32_nds32_add_dynreloc (bfd *output_bfd,
4312                           struct bfd_link_info *info ATTRIBUTE_UNUSED,
4313                           asection *sreloc, Elf_Internal_Rela *rel)
4314 {
4315   bfd_byte *loc;
4316   if (sreloc == NULL)
4317     abort ();
4318
4319   loc = sreloc->contents;
4320   loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4321   if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4322     abort ();
4323
4324   bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4325 }
4326
4327 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
4328    read-only sections.  */
4329
4330 static bfd_boolean
4331 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
4332 {
4333   asection *sec;
4334
4335   if (h->root.type == bfd_link_hash_indirect)
4336     return TRUE;
4337
4338   sec = readonly_dynrelocs (h);
4339   if (sec != NULL)
4340     {
4341       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
4342
4343       info->flags |= DF_TEXTREL;
4344       info->callbacks->minfo
4345         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
4346          sec->owner, h->root.root.string, sec);
4347
4348       /* Not an error, just cut short the traversal.  */
4349       return FALSE;
4350     }
4351   return TRUE;
4352 }
4353
4354 /* Set the sizes of the dynamic sections.  */
4355
4356 static bfd_boolean
4357 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4358                                  struct bfd_link_info *info)
4359 {
4360   struct elf_nds32_link_hash_table *htab;
4361   bfd *dynobj;
4362   asection *s;
4363   bfd_boolean relocs;
4364   bfd_boolean plt;
4365   bfd *ibfd;
4366
4367   htab = nds32_elf_hash_table (info);
4368   if (htab == NULL)
4369     return FALSE;
4370
4371   dynobj = elf_hash_table (info)->dynobj;
4372   BFD_ASSERT (dynobj != NULL);
4373
4374   if (elf_hash_table (info)->dynamic_sections_created)
4375     {
4376       /* Set the contents of the .interp section to the interpreter.  */
4377       if (bfd_link_executable (info) && !info->nointerp)
4378         {
4379           s = bfd_get_section_by_name (dynobj, ".interp");
4380           BFD_ASSERT (s != NULL);
4381           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4382           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4383         }
4384     }
4385
4386   /* Set up .got offsets for local syms, and space for local dynamic
4387      relocs.  */
4388   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4389     {
4390       bfd_signed_vma *local_got;
4391       bfd_signed_vma *end_local_got;
4392       bfd_size_type locsymcount;
4393       Elf_Internal_Shdr *symtab_hdr;
4394       asection *sgot;
4395       char *local_tls_type;
4396       unsigned long symndx;
4397       bfd_vma *local_tlsdesc_gotent;
4398
4399       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4400         continue;
4401
4402       for (s = ibfd->sections; s != NULL; s = s->next)
4403         {
4404           struct elf_dyn_relocs *p;
4405
4406           for (p = ((struct elf_dyn_relocs *)
4407                     elf_section_data (s)->local_dynrel);
4408                p != NULL; p = p->next)
4409             {
4410               if (!bfd_is_abs_section (p->sec)
4411                   && bfd_is_abs_section (p->sec->output_section))
4412                 {
4413                   /* Input section has been discarded, either because
4414                      it is a copy of a linkonce section or due to
4415                      linker script /DISCARD/, so we'll be discarding
4416                      the relocs too.  */
4417                 }
4418               else if (p->count != 0)
4419                 {
4420                   asection *sreloc = elf_section_data (p->sec)->sreloc;
4421                   sreloc->size += p->count * sizeof (Elf32_External_Rela);
4422                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4423                     info->flags |= DF_TEXTREL;
4424                 }
4425             }
4426         }
4427
4428       local_got = elf_local_got_refcounts (ibfd);
4429       if (!local_got)
4430         continue;
4431
4432       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4433       locsymcount = symtab_hdr->sh_info;
4434       end_local_got = local_got + locsymcount;
4435       sgot = elf_hash_table (info)->sgot;
4436       local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4437       local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4438       for (symndx = 0; local_got < end_local_got;
4439            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4440         {
4441           if (*local_got > 0)
4442             {
4443               int num_of_got_entry_needed = 0;
4444               *local_got = sgot->size;
4445               *local_tlsdesc_gotent = sgot->size;
4446
4447               /* TLS_NORMAL, and TLS_IE need one slot in .got.  */
4448               if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4449                 num_of_got_entry_needed = 1;
4450               /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT.  */
4451               else if (*local_tls_type & GOT_TLS_DESC)
4452                 num_of_got_entry_needed = 2;
4453
4454               sgot->size += (num_of_got_entry_needed << 2);
4455
4456               /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4457                  others need a slot in .rela.got.  */
4458               if (*local_tls_type == GOT_TLS_DESC)
4459                 {
4460                   if (bfd_link_pic (info))
4461                     {
4462                       if (htab->tls_desc_trampoline)
4463                         {
4464                           htab->num_tls_desc++;
4465                           htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4466                           htab->tls_trampoline = -1;
4467                         }
4468                       else
4469                         htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4470                     }
4471                   else
4472                     {
4473                       /* TLS_DESC -> TLS_LE  */
4474                     }
4475                 }
4476               else
4477                 {
4478                   htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4479                 }
4480             }
4481           else
4482             {
4483               *local_got = (bfd_vma) -1;
4484               *local_tlsdesc_gotent = (bfd_vma) -1;
4485             }
4486         }
4487     }
4488
4489   /* Allocate global sym .plt and .got entries, and space for global
4490      sym dynamic relocs.  */
4491   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4492
4493   /* For every jump slot reserved in the sgotplt, reloc_count is
4494      incremented.  However, when we reserve space for TLS descriptors,
4495      it's not incremented, so in order to compute the space reserved
4496      for them, it suffices to multiply the reloc count by the jump
4497      slot size.  */
4498   if (htab->tls_desc_trampoline && htab->root.srelplt)
4499     htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4500
4501   if (htab->tls_trampoline)
4502     {
4503       htab->tls_trampoline = htab->root.splt->size;
4504
4505       /* If we're not using lazy TLS relocations, don't generate the
4506          PLT and GOT entries they require.  */
4507       if (!(info->flags & DF_BIND_NOW))
4508         {
4509           htab->dt_tlsdesc_got = htab->root.sgot->size;
4510           htab->root.sgot->size += 4;
4511
4512           htab->dt_tlsdesc_plt = htab->root.splt->size;
4513           htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4514         }
4515     }
4516
4517   /* We now have determined the sizes of the various dynamic sections.
4518      Allocate memory for them.  */
4519   /* The check_relocs and adjust_dynamic_symbol entry points have
4520      determined the sizes of the various dynamic sections.  Allocate
4521      memory for them.  */
4522   plt = FALSE;
4523   relocs = FALSE;
4524   for (s = dynobj->sections; s != NULL; s = s->next)
4525     {
4526       if ((s->flags & SEC_LINKER_CREATED) == 0)
4527         continue;
4528
4529       if (s == htab->root.splt)
4530         {
4531           /* Strip this section if we don't need it; see the
4532              comment below.  */
4533           plt = s->size != 0;
4534         }
4535       else if (s == elf_hash_table (info)->sgot)
4536         {
4537           got_size += s->size;
4538         }
4539       else if (s == elf_hash_table (info)->sgotplt)
4540         {
4541           got_size += s->size;
4542         }
4543       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4544         {
4545           if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4546             relocs = TRUE;
4547
4548           /* We use the reloc_count field as a counter if we need
4549              to copy relocs into the output file.  */
4550           s->reloc_count = 0;
4551         }
4552       else
4553         {
4554           /* It's not one of our sections, so don't allocate space.  */
4555           continue;
4556         }
4557
4558       if (s->size == 0)
4559         {
4560           /* If we don't need this section, strip it from the
4561              output file.  This is mostly to handle .rela.bss and
4562              .rela.plt.  We must create both sections in
4563              create_dynamic_sections, because they must be created
4564              before the linker maps input sections to output
4565              sections.  The linker does that before
4566              adjust_dynamic_symbol is called, and it is that
4567              function which decides whether anything needs to go
4568              into these sections.  */
4569           s->flags |= SEC_EXCLUDE;
4570           continue;
4571         }
4572
4573       /* Allocate memory for the section contents.  We use bfd_zalloc
4574          here in case unused entries are not reclaimed before the
4575          section's contents are written out.  This should not happen,
4576          but this way if it does, we get a R_NDS32_NONE reloc instead
4577          of garbage.  */
4578       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4579       if (s->contents == NULL)
4580         return FALSE;
4581     }
4582
4583
4584   if (htab->root.dynamic_sections_created)
4585     {
4586       /* Add some entries to the .dynamic section.  We fill in the
4587          values later, in nds32_elf_finish_dynamic_sections, but we
4588          must add the entries now so that we get the correct size for
4589          the .dynamic section.  The DT_DEBUG entry is filled in by the
4590          dynamic linker and used by the debugger.  */
4591 #define add_dynamic_entry(TAG, VAL) \
4592   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4593
4594       if (bfd_link_executable (info))
4595         {
4596           if (!add_dynamic_entry (DT_DEBUG, 0))
4597             return FALSE;
4598         }
4599
4600       if (elf_hash_table (info)->splt->size != 0)
4601         {
4602           if (!add_dynamic_entry (DT_PLTGOT, 0)
4603               || !add_dynamic_entry (DT_PLTRELSZ, 0)
4604               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4605               || !add_dynamic_entry (DT_JMPREL, 0))
4606             return FALSE;
4607         }
4608
4609       if (htab->tls_desc_trampoline && plt)
4610         {
4611           if (htab->dt_tlsdesc_plt
4612               && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4613                   || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4614             return FALSE;
4615         }
4616
4617       if (relocs)
4618         {
4619           if (!add_dynamic_entry (DT_RELA, 0)
4620               || !add_dynamic_entry (DT_RELASZ, 0)
4621               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4622             return FALSE;
4623
4624           /* If any dynamic relocs apply to a read-only section,
4625              then we need a DT_TEXTREL entry.  */
4626           if ((info->flags & DF_TEXTREL) == 0)
4627             elf_link_hash_traverse (&htab->root, maybe_set_textrel,
4628                                     (void *) info);
4629
4630           if ((info->flags & DF_TEXTREL) != 0)
4631             {
4632               if (!add_dynamic_entry (DT_TEXTREL, 0))
4633                 return FALSE;
4634             }
4635         }
4636     }
4637 #undef add_dynamic_entry
4638
4639   return TRUE;
4640 }
4641
4642 static bfd_reloc_status_type
4643 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4644                          bfd_vma relocation, bfd_byte *location)
4645 {
4646   int size;
4647   bfd_vma x = 0;
4648   bfd_reloc_status_type flag;
4649   unsigned int rightshift = howto->rightshift;
4650   unsigned int bitpos = howto->bitpos;
4651
4652   /* If the size is negative, negate RELOCATION.  This isn't very
4653      general.  */
4654   if (howto->size < 0)
4655     relocation = -relocation;
4656
4657   /* Get the value we are going to relocate.  */
4658   size = bfd_get_reloc_size (howto);
4659   switch (size)
4660     {
4661     default:
4662       abort ();
4663       break;
4664     case 0:
4665       return bfd_reloc_ok;
4666     case 2:
4667       x = bfd_getb16 (location);
4668       break;
4669     case 4:
4670       x = bfd_getb32 (location);
4671       break;
4672     }
4673
4674   /* Check for overflow.  FIXME: We may drop bits during the addition
4675      which we don't check for.  We must either check at every single
4676      operation, which would be tedious, or we must do the computations
4677      in a type larger than bfd_vma, which would be inefficient.  */
4678   flag = bfd_reloc_ok;
4679   if (howto->complain_on_overflow != complain_overflow_dont)
4680     {
4681       bfd_vma addrmask, fieldmask, signmask, ss;
4682       bfd_vma a, b, sum;
4683
4684       /* Get the values to be added together.  For signed and unsigned
4685          relocations, we assume that all values should be truncated to
4686          the size of an address.  For bitfields, all the bits matter.
4687          See also bfd_check_overflow.  */
4688       fieldmask = N_ONES (howto->bitsize);
4689       signmask = ~fieldmask;
4690       addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4691       a = (relocation & addrmask) >> rightshift;
4692       b = (x & howto->src_mask & addrmask) >> bitpos;
4693
4694       switch (howto->complain_on_overflow)
4695         {
4696         case complain_overflow_signed:
4697           /* If any sign bits are set, all sign bits must be set.
4698              That is, A must be a valid negative address after
4699              shifting.  */
4700           signmask = ~(fieldmask >> 1);
4701           /* Fall through.  */
4702
4703         case complain_overflow_bitfield:
4704           /* Much like the signed check, but for a field one bit
4705              wider.  We allow a bitfield to represent numbers in the
4706              range -2**n to 2**n-1, where n is the number of bits in the
4707              field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4708              can't overflow, which is exactly what we want.  */
4709           ss = a & signmask;
4710           if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4711             flag = bfd_reloc_overflow;
4712
4713           /* We only need this next bit of code if the sign bit of B
4714              is below the sign bit of A.  This would only happen if
4715              SRC_MASK had fewer bits than BITSIZE.  Note that if
4716              SRC_MASK has more bits than BITSIZE, we can get into
4717              trouble; we would need to verify that B is in range, as
4718              we do for A above.  */
4719           ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4720           ss >>= bitpos;
4721
4722           /* Set all the bits above the sign bit.  */
4723           b = (b ^ ss) - ss;
4724
4725           /* Now we can do the addition.  */
4726           sum = a + b;
4727
4728           /* See if the result has the correct sign.  Bits above the
4729              sign bit are junk now; ignore them.  If the sum is
4730              positive, make sure we did not have all negative inputs;
4731              if the sum is negative, make sure we did not have all
4732              positive inputs.  The test below looks only at the sign
4733              bits, and it really just
4734              SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4735
4736              We mask with addrmask here to explicitly allow an address
4737              wrap-around.  The Linux kernel relies on it, and it is
4738              the only way to write assembler code which can run when
4739              loaded at a location 0x80000000 away from the location at
4740              which it is linked.  */
4741           if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4742             flag = bfd_reloc_overflow;
4743
4744           break;
4745
4746         case complain_overflow_unsigned:
4747           /* Checking for an unsigned overflow is relatively easy:
4748              trim the addresses and add, and trim the result as well.
4749              Overflow is normally indicated when the result does not
4750              fit in the field.  However, we also need to consider the
4751              case when, e.g., fieldmask is 0x7fffffff or smaller, an
4752              input is 0x80000000, and bfd_vma is only 32 bits; then we
4753              will get sum == 0, but there is an overflow, since the
4754              inputs did not fit in the field.  Instead of doing a
4755              separate test, we can check for this by or-ing in the
4756              operands when testing for the sum overflowing its final
4757              field.  */
4758           sum = (a + b) & addrmask;
4759           if ((a | b | sum) & signmask)
4760             flag = bfd_reloc_overflow;
4761           break;
4762
4763         default:
4764           abort ();
4765         }
4766     }
4767
4768   /* Put RELOCATION in the right bits.  */
4769   relocation >>= (bfd_vma) rightshift;
4770   relocation <<= (bfd_vma) bitpos;
4771
4772   /* Add RELOCATION to the right bits of X.  */
4773   /* FIXME : 090616
4774      Because the relaxation may generate duplicate relocation at one address,
4775      an addition to immediate in the instruction may cause the relocation added
4776      several times.
4777      This bug should be fixed in assembler, but a check is also needed here.  */
4778   if (howto->partial_inplace)
4779     x = ((x & ~howto->dst_mask)
4780          | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4781   else
4782     x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4783
4784
4785   /* Put the relocated value back in the object file.  */
4786   switch (size)
4787     {
4788     default:
4789     case 0:
4790     case 1:
4791     case 8:
4792       abort ();
4793       break;
4794     case 2:
4795       bfd_putb16 (x, location);
4796       break;
4797     case 4:
4798       bfd_putb32 (x, location);
4799       break;
4800     }
4801
4802   return flag;
4803 }
4804
4805 static bfd_reloc_status_type
4806 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4807                                asection *input_section, bfd_byte *contents,
4808                                bfd_vma address, bfd_vma value, bfd_vma addend)
4809 {
4810   bfd_vma relocation;
4811
4812   /* Sanity check the address.  */
4813   if (address > bfd_get_section_limit (input_bfd, input_section))
4814     return bfd_reloc_outofrange;
4815
4816   /* This function assumes that we are dealing with a basic relocation
4817      against a symbol.  We want to compute the value of the symbol to
4818      relocate to.  This is just VALUE, the value of the symbol, plus
4819      ADDEND, any addend associated with the reloc.  */
4820   relocation = value + addend;
4821
4822   /* If the relocation is PC relative, we want to set RELOCATION to
4823      the distance between the symbol (currently in RELOCATION) and the
4824      location we are relocating.  If pcrel_offset is FALSE we do not
4825      need to subtract out the offset of the location within the
4826      section (which is just ADDRESS).  */
4827   if (howto->pc_relative)
4828     {
4829       relocation -= (input_section->output_section->vma
4830                      + input_section->output_offset);
4831       if (howto->pcrel_offset)
4832         relocation -= address;
4833     }
4834
4835   return nds32_relocate_contents (howto, input_bfd, relocation,
4836                                   contents + address);
4837 }
4838
4839 static bfd_boolean
4840 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4841                               const char *name,
4842                               Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4843                               asection *input_sec,
4844                               struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4845 {
4846   const char *source;
4847   FILE *sym_ld_script = NULL;
4848   struct elf_nds32_link_hash_table *table;
4849
4850   table = nds32_elf_hash_table (info);
4851   sym_ld_script = table->sym_ld_script;
4852   if (!sym_ld_script)
4853     return TRUE;
4854
4855   if (!h || !name || *name == '\0')
4856     return TRUE;
4857
4858   if (input_sec->flags & SEC_EXCLUDE)
4859     return TRUE;
4860
4861   if (!check_start_export_sym)
4862     {
4863       fprintf (sym_ld_script, "SECTIONS\n{\n");
4864       check_start_export_sym = 1;
4865     }
4866
4867   if (h->root.type == bfd_link_hash_defined
4868       || h->root.type == bfd_link_hash_defweak)
4869     {
4870       if (!h->root.u.def.section->output_section)
4871         return TRUE;
4872
4873       if (bfd_is_const_section (input_sec))
4874         source = input_sec->name;
4875       else
4876         source = input_sec->owner->filename;
4877
4878       fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4879                h->root.root.string,
4880                (long) (h->root.u.def.value
4881                 + h->root.u.def.section->output_section->vma
4882                 + h->root.u.def.section->output_offset), source);
4883     }
4884
4885   return TRUE;
4886 }
4887
4888 /* Relocate an NDS32/D ELF section.
4889    There is some attempt to make this function usable for many architectures,
4890    both for RELA and REL type relocs, if only to serve as a learning tool.
4891
4892    The RELOCATE_SECTION function is called by the new ELF backend linker
4893    to handle the relocations for a section.
4894
4895    The relocs are always passed as Rela structures; if the section
4896    actually uses Rel structures, the r_addend field will always be
4897    zero.
4898
4899    This function is responsible for adjust the section contents as
4900    necessary, and (if using Rela relocs and generating a
4901    relocatable output file) adjusting the reloc addend as
4902    necessary.
4903
4904    This function does not have to worry about setting the reloc
4905    address or the reloc symbol index.
4906
4907    LOCAL_SYMS is a pointer to the swapped in local symbols.
4908
4909    LOCAL_SECTIONS is an array giving the section in the input file
4910    corresponding to the st_shndx field of each local symbol.
4911
4912    The global hash table entry for the global symbols can be found
4913    via elf_sym_hashes (input_bfd).
4914
4915    When generating relocatable output, this function must handle
4916    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4917    going to be the section symbol corresponding to the output
4918    section, which means that the addend must be adjusted
4919    accordingly.  */
4920
4921 /* Return the base VMA address which should be subtracted from real addresses
4922    when resolving @dtpoff relocation.
4923    This is PT_TLS segment p_vaddr.  */
4924
4925 /* Return the relocation value for @tpoff relocation
4926    if STT_TLS virtual address is ADDRESS.  */
4927
4928 /* Return the relocation value for @gottpoff relocation
4929    if STT_TLS virtual address is ADDRESS.  */
4930
4931 static bfd_vma
4932 gottpoff (struct bfd_link_info *info, bfd_vma address)
4933 {
4934   bfd_vma tp_base;
4935   bfd_vma tp_offset;
4936
4937   /* If tls_sec is NULL, we should have signalled an error already.  */
4938   if (elf_hash_table (info)->tls_sec == NULL)
4939     return 0;
4940
4941   tp_base = elf_hash_table (info)->tls_sec->vma;
4942   tp_offset = address - tp_base;
4943
4944   return tp_offset;
4945 }
4946
4947 static bfd_boolean
4948 patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4949 {
4950   /* TLS_GD/TLS_LD model #1
4951      46 00 00 00 sethi $r0,#0x0
4952      58 00 00 00 ori $r0,$r0,#0x0
4953      40 00 74 00 add $r0,$r0,$gp
4954      04 10 00 00 lwi $r1,[$r0+#0x0]
4955      4b e0 04 01 jral $lp,$r1  */
4956
4957   /* TLS_GD/TLS_LD model #2
4958      46 00 00 00 sethi $r0,#0x0
4959      58 00 00 00 ori $r0,$r0,#0x0
4960      38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4961      40 00 74 00 add $r0,$r0,$gp
4962      4b e0 04 01 jral $lp,$r1  */
4963
4964   /* TLS_IE model (non-PIC)
4965      46 00 00 00 sethi $r0,#0x0
4966      04 00 00 00 lwi $r0,[$r0+#0x0]
4967      38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4968
4969   /* TLS_IE model (PIC)
4970      46 00 00 00 sethi $r0,#0x0
4971      58 00 00 00 ori $r0,$r0,#0x0
4972      38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4973      38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4974
4975   /* TLS_GD_TO_IE model
4976      46 00 00 00 sethi $r0,#0x0
4977      58 00 00 00 ori $r0,$r0,#0x0
4978      40 00 74 00 add $r0,$rM,$gp
4979      04 00 00 01 lwi $r0,[$r0+#0x4]
4980      40 00 64 00 add $r0,$r0,$r25  */
4981
4982   bfd_boolean rz = FALSE;
4983
4984   typedef struct
4985     {
4986       uint32_t opcode;
4987       uint32_t mask;
4988     } pat_t;
4989
4990   uint32_t patch[3] =
4991     {
4992       0x40007400, /* add $r0,$rM,$gp     */
4993       0x04000001, /* lwi $r0,[$r0+#0x4]  */
4994       0x40006400, /* add $r0,$r0,$r25    */
4995     };
4996
4997   pat_t mode0[3] =
4998     {
4999         { 0x40000000, 0xfe0003ff },
5000         { 0x04000000, 0xfe000000 },
5001         { 0x4be00001, 0xffff83ff },
5002     };
5003
5004   pat_t mode1[3] =
5005     {
5006         { 0x38007402, 0xfe007fff },
5007         { 0x40007400, 0xfe007fff },
5008         { 0x4be00001, 0xffff83ff },
5009     };
5010
5011   unsigned char *p = contents + rel->r_offset;
5012
5013   uint32_t insn;
5014   uint32_t regidx = 0;
5015   insn = bfd_getb32 (p);
5016   if (INSN_SETHI == (0xfe0fffffu & insn))
5017     {
5018       regidx = 0x1f & (insn >> 20);
5019       p += 4;
5020     }
5021
5022   insn = bfd_getb32 (p);
5023   if (INSN_ORI == (0xfe007fffu & insn))
5024     {
5025       regidx = 0x1f & (insn >> 20);
5026       p += 4;
5027     }
5028
5029   if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction.  */
5030     {
5031       /* already patched?  */
5032       if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
5033           (patch[1] == bfd_getb32 (p + 4)))
5034         rz = TRUE;
5035     }
5036   else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
5037     {
5038       if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
5039           (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
5040         {
5041           bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5042           bfd_putb32 (patch[1], p + 4);
5043           bfd_putb32 (patch[2], p + 8);
5044           rz = TRUE;
5045         }
5046     }
5047   else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
5048     {
5049       if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
5050           (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5051         {
5052           bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5053           bfd_putb32 (patch[1], p + 4);
5054           bfd_putb32 (patch[2], p + 8);
5055           rz = TRUE;
5056         }
5057     }
5058
5059   if (!rz)
5060     {
5061       printf ("%s: %s @ 0x%08x\n", __func__, ibfd->filename,
5062               (int) rel->r_offset);
5063       BFD_ASSERT(0); /* Unsupported pattern.  */
5064     }
5065
5066   return rz;
5067 }
5068
5069 static enum elf_nds32_tls_type
5070 get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5071
5072 static unsigned int
5073 ones32 (register unsigned int x)
5074 {
5075   /* 32-bit recursive reduction using SWAR...
5076      but first step is mapping 2-bit values
5077      into sum of 2 1-bit values in sneaky way.  */
5078   x -= ((x >> 1) & 0x55555555);
5079   x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5080   x = (((x >> 4) + x) & 0x0f0f0f0f);
5081   x += (x >> 8);
5082   x += (x >> 16);
5083   return (x & 0x0000003f);
5084 }
5085
5086 #if !HAVE_FLS
5087 static unsigned int
5088 fls (register unsigned int x)
5089 {
5090   return ffs (x & (-x));
5091 }
5092 #endif /* !HAVE_FLS */
5093
5094 #define nds32_elf_local_tlsdesc_gotent(bfd) \
5095   (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5096
5097 static bfd_boolean
5098 nds32_elf_relocate_section (bfd *                  output_bfd ATTRIBUTE_UNUSED,
5099                             struct bfd_link_info * info,
5100                             bfd *                  input_bfd,
5101                             asection *             input_section,
5102                             bfd_byte *             contents,
5103                             Elf_Internal_Rela *    relocs,
5104                             Elf_Internal_Sym *     local_syms,
5105                             asection **            local_sections)
5106 {
5107   Elf_Internal_Shdr *symtab_hdr;
5108   struct elf_link_hash_entry **sym_hashes;
5109   Elf_Internal_Rela *rel, *relend;
5110   bfd_boolean ret = TRUE;               /* Assume success.  */
5111   int align = 0;
5112   bfd_reloc_status_type r;
5113   const char *errmsg = NULL;
5114   bfd_vma gp;
5115   struct elf_link_hash_table *ehtab;
5116   struct elf_nds32_link_hash_table *htab;
5117   bfd *dynobj;
5118   bfd_vma *local_got_offsets;
5119   asection *sgot, *splt, *sreloc;
5120   bfd_vma high_address;
5121   struct elf_nds32_link_hash_table *table;
5122   int eliminate_gc_relocs;
5123   bfd_vma fpbase_addr;
5124
5125   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5126   sym_hashes = elf_sym_hashes (input_bfd);
5127   ehtab = elf_hash_table (info);
5128   htab = nds32_elf_hash_table (info);
5129   high_address = bfd_get_section_limit (input_bfd, input_section);
5130
5131   dynobj = htab->root.dynobj;
5132   local_got_offsets = elf_local_got_offsets (input_bfd);
5133
5134   sgot = ehtab->sgot;
5135   splt = ehtab->splt;
5136   sreloc = NULL;
5137
5138   rel = relocs;
5139   relend = relocs + input_section->reloc_count;
5140
5141   table = nds32_elf_hash_table (info);
5142   eliminate_gc_relocs = table->eliminate_gc_relocs;
5143
5144   /* By this time, we can adjust the value of _SDA_BASE_.  */
5145   /* Explain _SDA_BASE_  */
5146   if ((!bfd_link_relocatable (info)))
5147     {
5148       is_SDA_BASE_set = 1;
5149       r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5150       if (r != bfd_reloc_ok)
5151         return FALSE;
5152     }
5153
5154   /* Do TLS model conversion once at first.  */
5155   nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5156
5157   /* Use gp as fp to prevent truncated fit.  Because in relaxation time
5158      the fp value is set as gp, and it has be reverted for instruction
5159      setting fp.  */
5160   fpbase_addr = elf_gp (output_bfd);
5161
5162   /* Deal with (dynamic) relocations.  */
5163   for (rel = relocs; rel < relend; rel++)
5164     {
5165       enum elf_nds32_reloc_type r_type;
5166       reloc_howto_type *howto = NULL;
5167       unsigned long r_symndx;
5168       struct elf_link_hash_entry *h = NULL;
5169       Elf_Internal_Sym *sym = NULL;
5170       asection *sec;
5171       bfd_vma relocation;
5172       bfd_vma relocation_sym = 0xdeadbeef;
5173       Elf_Internal_Rela *lorel;
5174       bfd_vma off;
5175
5176       /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5177          ensure it's zero (we use REL relocs, not RELA).  Therefore this
5178          should be assigning zero to `addend', but for clarity we use
5179          `r_addend'.  */
5180
5181       bfd_vma addend = rel->r_addend;
5182       bfd_vma offset = rel->r_offset;
5183
5184       r_type = ELF32_R_TYPE (rel->r_info);
5185       if (r_type >= R_NDS32_max)
5186         {
5187           /* xgettext:c-format */
5188           _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5189                               input_bfd, r_type);
5190           bfd_set_error (bfd_error_bad_value);
5191           ret = FALSE;
5192           continue;
5193         }
5194
5195       if (r_type == R_NDS32_GNU_VTENTRY
5196           || r_type == R_NDS32_GNU_VTINHERIT
5197           || r_type == R_NDS32_NONE
5198           || r_type == R_NDS32_RELA_GNU_VTENTRY
5199           || r_type == R_NDS32_RELA_GNU_VTINHERIT
5200           || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5201           || r_type == R_NDS32_DATA
5202           || r_type == R_NDS32_TRAN)
5203         continue;
5204
5205       /* If we enter the fp-as-gp region.  Resolve the address
5206          of best fp-base.  */
5207       if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5208           && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5209         {
5210           int dist;
5211
5212           /* Distance to relocation of best fp-base is encoded in R_SYM.  */
5213           dist =  rel->r_addend >> 16;
5214           fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5215                                                   local_syms, symtab_hdr);
5216         }
5217       else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5218                && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5219         {
5220           fpbase_addr = elf_gp (output_bfd);
5221         }
5222
5223       /* Skip the relocations used for relaxation.  */
5224       /* We have to update LONGCALL and LONGJUMP
5225          relocations when generating the relocatable files.  */
5226       if (!bfd_link_relocatable (info)
5227           && (r_type >= R_NDS32_RELAX_ENTRY
5228               || (r_type >= R_NDS32_LONGCALL4
5229                   && r_type <= R_NDS32_LONGJUMP7)))
5230         continue;
5231
5232       howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5233       r_symndx = ELF32_R_SYM (rel->r_info);
5234
5235       /* This is a final link.  */
5236       sym = NULL;
5237       sec = NULL;
5238       h = NULL;
5239
5240       if (r_symndx < symtab_hdr->sh_info)
5241         {
5242           /* Local symbol.  */
5243           sym = local_syms + r_symndx;
5244           sec = local_sections[r_symndx];
5245
5246           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5247           addend = rel->r_addend;
5248
5249           /* keep symbol location for static TLS_IE GOT entry  */
5250           relocation_sym = relocation;
5251           if (bfd_link_relocatable (info))
5252             {
5253               /* This is a relocatable link.  We don't have to change
5254                  anything, unless the reloc is against a section symbol,
5255                  in which case we have to adjust according to where the
5256                  section symbol winds up in the output section.  */
5257               if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5258                 rel->r_addend += sec->output_offset + sym->st_value;
5259
5260               continue;
5261             }
5262         }
5263       else
5264         {
5265           /* External symbol.  */
5266           if (bfd_link_relocatable (info))
5267             continue;
5268           bfd_boolean warned, ignored, unresolved_reloc;
5269           int symndx = r_symndx - symtab_hdr->sh_info;
5270
5271           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5272                                    r_symndx, symtab_hdr, sym_hashes, h, sec,
5273                                    relocation, unresolved_reloc, warned,
5274                                    ignored);
5275
5276           /* keep symbol location for static TLS_IE GOT entry  */
5277           relocation_sym = relocation;
5278
5279           /* la $fp, _FP_BASE_ is per-function (region).
5280              Handle it specially.  */
5281           switch ((int) r_type)
5282             {
5283             case R_NDS32_HI20_RELA:
5284             case R_NDS32_LO12S0_RELA:
5285               if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5286                           FP_BASE_NAME) == 0)
5287                 {
5288                   if (!bfd_link_pie (info))
5289                     {
5290                       _bfd_error_handler
5291                         ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5292                          input_bfd);
5293                     }
5294                   relocation = fpbase_addr;
5295                 }
5296               break;
5297             case R_NDS32_SDA19S0_RELA:
5298             case R_NDS32_SDA15S0_RELA:
5299             case R_NDS32_20_RELA:
5300               if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5301                           FP_BASE_NAME) == 0)
5302                 {
5303                   relocation = fpbase_addr;
5304                   break;
5305                 }
5306             }
5307         }
5308
5309       /* Sanity check the address.  */
5310       if (offset > high_address)
5311         {
5312           r = bfd_reloc_outofrange;
5313           goto check_reloc;
5314         }
5315
5316       if (r_type >= R_NDS32_RELAX_ENTRY)
5317         continue;
5318
5319       switch ((int) r_type)
5320         {
5321         case R_NDS32_GOTOFF:
5322           /* Relocation is relative to the start of the global offset
5323              table (for ld24 rx, #uimm24), e.g. access at label+addend
5324
5325              ld24 rx. #label@GOTOFF + addend
5326              sub  rx, r12.  */
5327         case R_NDS32_GOTOFF_HI20:
5328         case R_NDS32_GOTOFF_LO12:
5329         case R_NDS32_GOTOFF_LO15:
5330         case R_NDS32_GOTOFF_LO19:
5331           BFD_ASSERT (sgot != NULL);
5332
5333           relocation -= elf_gp (output_bfd);
5334           break;
5335
5336         case R_NDS32_9_PLTREL:
5337         case R_NDS32_25_PLTREL:
5338           /* Relocation is to the entry for this symbol in the
5339              procedure linkage table.  */
5340
5341           /* The native assembler will generate a 25_PLTREL reloc
5342              for a local symbol if you assemble a call from one
5343              section to another when using -K pic.  */
5344           if (h == NULL)
5345             break;
5346
5347           if (h->forced_local)
5348             break;
5349
5350           /* We didn't make a PLT entry for this symbol.  This
5351              happens when statically linking PIC code, or when
5352              using -Bsymbolic.  */
5353           if (h->plt.offset == (bfd_vma) - 1)
5354             break;
5355
5356           relocation = (splt->output_section->vma
5357                         + splt->output_offset + h->plt.offset);
5358           break;
5359
5360         case R_NDS32_PLT_GOTREL_HI20:
5361         case R_NDS32_PLT_GOTREL_LO12:
5362         case R_NDS32_PLT_GOTREL_LO15:
5363         case R_NDS32_PLT_GOTREL_LO19:
5364         case R_NDS32_PLT_GOTREL_LO20:
5365           if (h == NULL
5366               || h->forced_local
5367               || h->plt.offset == (bfd_vma) -1
5368               || (bfd_link_pie (info) && h->def_regular))
5369             {
5370               /* Maybe we should find better checking to optimize
5371                  PIE PLT relocations.  */
5372               /* We didn't make a PLT entry for this symbol.  This
5373                  happens when statically linking PIC code, or when
5374                  using -Bsymbolic.  */
5375               if (h)
5376                 h->plt.offset = (bfd_vma) -1;   /* Cancel PLT trampoline.  */
5377               relocation -= elf_gp (output_bfd);
5378               break;
5379             }
5380
5381           relocation = (splt->output_section->vma
5382                         + splt->output_offset + h->plt.offset);
5383
5384           relocation -= elf_gp (output_bfd);
5385           break;
5386
5387         case R_NDS32_PLTREL_HI20:
5388         case R_NDS32_PLTREL_LO12:
5389
5390           /* Relocation is to the entry for this symbol in the
5391              procedure linkage table.  */
5392
5393           /* The native assembler will generate a 25_PLTREL reloc
5394              for a local symbol if you assemble a call from one
5395              section to another when using -K pic.  */
5396           if (h == NULL)
5397             break;
5398
5399           if (h->forced_local)
5400             break;
5401
5402           if (h->plt.offset == (bfd_vma) - 1)
5403             /* We didn't make a PLT entry for this symbol.  This
5404                happens when statically linking PIC code, or when
5405                using -Bsymbolic.  */
5406             break;
5407
5408           if (splt == NULL)
5409             break;
5410
5411           relocation = (splt->output_section->vma
5412                         + splt->output_offset
5413                         + h->plt.offset + 4)
5414                        - (input_section->output_section->vma
5415                           + input_section->output_offset
5416                           + rel->r_offset);
5417
5418           break;
5419
5420         case R_NDS32_GOTPC20:
5421           /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5422              ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
5423           relocation = elf_gp (output_bfd);
5424           break;
5425
5426         case R_NDS32_GOTPC_HI20:
5427         case R_NDS32_GOTPC_LO12:
5428           /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5429              bl .+4
5430              seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5431              or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5432              or
5433              bl .+4
5434              seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5435              add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)  */
5436           relocation = elf_gp (output_bfd);
5437           relocation -= (input_section->output_section->vma
5438                          + input_section->output_offset + rel->r_offset);
5439           break;
5440
5441         case R_NDS32_GOT20:
5442           /* Fall through.  */
5443         case R_NDS32_GOT_HI20:
5444         case R_NDS32_GOT_LO12:
5445         case R_NDS32_GOT_LO15:
5446         case R_NDS32_GOT_LO19:
5447           /* Relocation is to the entry for this symbol in the global
5448              offset table.  */
5449           BFD_ASSERT (sgot != NULL);
5450
5451           if (h != NULL)
5452             {
5453               /* External symbol  */
5454               bfd_boolean dyn;
5455
5456               off = h->got.offset;
5457               BFD_ASSERT (off != (bfd_vma) - 1);
5458               dyn = htab->root.dynamic_sections_created;
5459               if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5460                                                     bfd_link_pic (info),
5461                                                     h)
5462                   || (bfd_link_pic (info)
5463                       && (info->symbolic
5464                           || h->dynindx == -1
5465                           || h->forced_local) && h->def_regular))
5466                 {
5467                   /* This is actually a static link, or it is a
5468                      -Bsymbolic link and the symbol is defined
5469                      locally, or the symbol was forced to be local
5470                      because of a version file.  We must initialize
5471                      this entry in the global offset table.  Since the
5472                      offset must always be a multiple of 4, we use the
5473                      least significant bit to record whether we have
5474                      initialized it already.
5475
5476                      When doing a dynamic link, we create a .rela.got
5477                      relocation entry to initialize the value.  This
5478                      is done in the finish_dynamic_symbol routine.  */
5479                   if ((off & 1) != 0)   /* clear LSB  */
5480                     off &= ~1;
5481                   else
5482                     {
5483                       bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5484                       h->got.offset |= 1;
5485                     }
5486                 }
5487               relocation = sgot->output_section->vma + sgot->output_offset + off
5488                            - elf_gp (output_bfd);
5489             }
5490           else
5491             {
5492               /* Local symbol  */
5493               bfd_byte *loc;
5494
5495               BFD_ASSERT (local_got_offsets != NULL
5496                           && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5497
5498               off = local_got_offsets[r_symndx];
5499
5500               /* The offset must always be a multiple of 4.  We use
5501                  the least significant bit to record whether we have
5502                  already processed this entry.  */
5503               if ((off & 1) != 0)       /* clear LSB  */
5504                 off &= ~1;
5505               else
5506                 {
5507                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5508
5509                   if (bfd_link_pic (info))
5510                     {
5511                       asection *srelgot;
5512                       Elf_Internal_Rela outrel;
5513
5514                       /* We need to generate a R_NDS32_RELATIVE reloc
5515                          for the dynamic linker.  */
5516                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5517                       BFD_ASSERT (srelgot != NULL);
5518
5519                       outrel.r_offset = (elf_gp (output_bfd)
5520                                          + sgot->output_offset + off);
5521                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5522                       outrel.r_addend = relocation;
5523                       loc = srelgot->contents;
5524                       loc +=
5525                         srelgot->reloc_count * sizeof (Elf32_External_Rela);
5526                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5527                       ++srelgot->reloc_count;
5528                     }
5529                   local_got_offsets[r_symndx] |= 1;
5530                 }
5531               relocation = sgot->output_section->vma + sgot->output_offset + off
5532                            - elf_gp (output_bfd);
5533             }
5534
5535           break;
5536
5537         case R_NDS32_16_RELA:
5538         case R_NDS32_20_RELA:
5539         case R_NDS32_5_RELA:
5540         case R_NDS32_32_RELA:
5541         case R_NDS32_9_PCREL_RELA:
5542         case R_NDS32_WORD_9_PCREL_RELA:
5543         case R_NDS32_10_UPCREL_RELA:
5544         case R_NDS32_15_PCREL_RELA:
5545         case R_NDS32_17_PCREL_RELA:
5546         case R_NDS32_25_PCREL_RELA:
5547         case R_NDS32_HI20_RELA:
5548         case R_NDS32_LO12S3_RELA:
5549         case R_NDS32_LO12S2_RELA:
5550         case R_NDS32_LO12S2_DP_RELA:
5551         case R_NDS32_LO12S2_SP_RELA:
5552         case R_NDS32_LO12S1_RELA:
5553         case R_NDS32_LO12S0_RELA:
5554         case R_NDS32_LO12S0_ORI_RELA:
5555           if (bfd_link_pic (info) && r_symndx != 0
5556               && (input_section->flags & SEC_ALLOC) != 0
5557               && (eliminate_gc_relocs == 0
5558                   || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5559               && ((r_type != R_NDS32_9_PCREL_RELA
5560                    && r_type != R_NDS32_WORD_9_PCREL_RELA
5561                    && r_type != R_NDS32_10_UPCREL_RELA
5562                    && r_type != R_NDS32_15_PCREL_RELA
5563                    && r_type != R_NDS32_17_PCREL_RELA
5564                    && r_type != R_NDS32_25_PCREL_RELA
5565                    && !(r_type == R_NDS32_32_RELA
5566                         && strcmp (input_section->name, ".eh_frame") == 0))
5567                   || (h != NULL && h->dynindx != -1
5568                       && (!info->symbolic || !h->def_regular))))
5569             {
5570               Elf_Internal_Rela outrel;
5571               bfd_boolean skip, relocate;
5572               bfd_byte *loc;
5573
5574               /* When generating a shared object, these relocations
5575                  are copied into the output file to be resolved at run
5576                  time.  */
5577
5578               if (sreloc == NULL)
5579                 {
5580                   const char *name;
5581
5582                   name = bfd_elf_string_from_elf_section
5583                     (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5584                      elf_section_data (input_section)->rela.hdr->sh_name);
5585                   if (name == NULL)
5586                     return FALSE;
5587
5588                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5589                               && strcmp (bfd_get_section_name (input_bfd,
5590                                                                input_section),
5591                                          name + 5) == 0);
5592
5593                   sreloc = bfd_get_section_by_name (dynobj, name);
5594                   BFD_ASSERT (sreloc != NULL);
5595                 }
5596
5597               skip = FALSE;
5598               relocate = FALSE;
5599
5600               outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5601                                                          info,
5602                                                          input_section,
5603                                                          rel->r_offset);
5604               if (outrel.r_offset == (bfd_vma) - 1)
5605                 skip = TRUE;
5606               else if (outrel.r_offset == (bfd_vma) - 2)
5607                 skip = TRUE, relocate = TRUE;
5608               outrel.r_offset += (input_section->output_section->vma
5609                                   + input_section->output_offset);
5610
5611               if (skip)
5612                 memset (&outrel, 0, sizeof outrel);
5613               else if (r_type == R_NDS32_17_PCREL_RELA
5614                        || r_type == R_NDS32_15_PCREL_RELA
5615                        || r_type == R_NDS32_25_PCREL_RELA)
5616                 {
5617                   BFD_ASSERT (h != NULL && h->dynindx != -1);
5618                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5619                   outrel.r_addend = rel->r_addend;
5620                 }
5621               else
5622                 {
5623                   /* h->dynindx may be -1 if this symbol was marked to
5624                      become local.  */
5625                   if (h == NULL
5626                       || ((info->symbolic || h->dynindx == -1)
5627                           && h->def_regular)
5628                       || (bfd_link_pie (info) && h->def_regular))
5629                     {
5630                       relocate = TRUE;
5631                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5632                       outrel.r_addend = relocation + rel->r_addend;
5633
5634                       if (h)
5635                         {
5636                           h->plt.offset = (bfd_vma) -1;   /* cancel PLT trampoline.  */
5637
5638                           BFD_ASSERT (sgot != NULL);
5639                           /* If we did not allocate got entry for the symbol,
5640                              we can not fill the nonexistent got entry.  */
5641                           if (h->got.offset != (bfd_vma) -1
5642                               && (h->got.offset & 1) == 0)
5643                             {
5644                               bfd_put_32 (output_bfd, outrel.r_addend,
5645                                           sgot->contents + h->got.offset);
5646                             }
5647                         }
5648                     }
5649                   else
5650                     {
5651                       if (h->dynindx == -1)
5652                         {
5653                           _bfd_error_handler
5654                             (_("%pB: relocation %s against `%s' can not be used when"
5655                                "making a shared object; recompile with -fPIC"),
5656                              input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5657                           bfd_set_error (bfd_error_bad_value);
5658                           return FALSE;
5659                         }
5660
5661                       outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5662                       outrel.r_addend = rel->r_addend;
5663                     }
5664                 }
5665
5666               loc = sreloc->contents;
5667               loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5668               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5669               ++sreloc->reloc_count;
5670
5671               /* If this reloc is against an external symbol, we do
5672                  not want to fiddle with the addend.  Otherwise, we
5673                  need to include the symbol value so that it becomes
5674                  an addend for the dynamic reloc.  */
5675               if (!relocate)
5676                 continue;
5677             }
5678           break;
5679
5680         case R_NDS32_25_ABS_RELA:
5681           if (bfd_link_pic (info))
5682             {
5683               _bfd_error_handler
5684                 (_("%pB: warning: %s unsupported in shared mode"),
5685                  input_bfd, "R_NDS32_25_ABS_RELA");
5686               return FALSE;
5687             }
5688           break;
5689
5690         case R_NDS32_9_PCREL:
5691           r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5692                                           contents, offset,
5693                                           sec, relocation, addend);
5694           goto check_reloc;
5695
5696         case R_NDS32_HI20:
5697           /* We allow an arbitrary number of HI20 relocs before the
5698              LO12 reloc.  This permits gcc to emit the HI and LO relocs
5699              itself.  */
5700           for (lorel = rel + 1;
5701                (lorel < relend
5702                 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5703             continue;
5704           if (lorel < relend
5705               && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5706                   || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5707                   || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5708                   || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5709             {
5710               nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5711                                        contents, relocation + addend);
5712               r = bfd_reloc_ok;
5713             }
5714           else
5715             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5716                                           contents, offset, relocation,
5717                                           addend);
5718           goto check_reloc;
5719
5720         case R_NDS32_GOT17S2_RELA:
5721         case R_NDS32_GOT15S2_RELA:
5722           BFD_ASSERT (sgot != NULL);
5723
5724           if (h != NULL)
5725             {
5726               bfd_boolean dyn;
5727
5728               off = h->got.offset;
5729               BFD_ASSERT (off != (bfd_vma) - 1);
5730
5731               dyn = htab->root.dynamic_sections_created;
5732               if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5733                   (dyn, bfd_link_pic (info), h)
5734                   || (bfd_link_pic (info)
5735                       && (info->symbolic
5736                           || h->dynindx == -1
5737                           || h->forced_local)
5738                       && h->def_regular))
5739                 {
5740                   /* This is actually a static link, or it is a
5741                      -Bsymbolic link and the symbol is defined
5742                      locally, or the symbol was forced to be local
5743                      because of a version file.  We must initialize
5744                      this entry in the global offset table.  Since the
5745                      offset must always be a multiple of 4, we use the
5746                      least significant bit to record whether we have
5747                      initialized it already.
5748
5749                      When doing a dynamic link, we create a .rela.got
5750                      relocation entry to initialize the value.  This
5751                      is done in the finish_dynamic_symbol routine.  */
5752                   if ((off & 1) != 0)
5753                     off &= ~1;
5754                   else
5755                     {
5756                       bfd_put_32 (output_bfd, relocation,
5757                                   sgot->contents + off);
5758                       h->got.offset |= 1;
5759                     }
5760                 }
5761             }
5762           else
5763             {
5764               bfd_byte *loc;
5765
5766               BFD_ASSERT (local_got_offsets != NULL
5767                           && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5768
5769               off = local_got_offsets[r_symndx];
5770
5771               /* The offset must always be a multiple of 4.  We use
5772                  the least significant bit to record whether we have
5773                  already processed this entry.  */
5774               if ((off & 1) != 0)
5775                 off &= ~1;
5776               else
5777                 {
5778                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5779
5780                   if (bfd_link_pic (info))
5781                     {
5782                       asection *srelgot;
5783                       Elf_Internal_Rela outrel;
5784
5785                       /* We need to generate a R_NDS32_RELATIVE reloc
5786                          for the dynamic linker.  */
5787                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5788                       BFD_ASSERT (srelgot != NULL);
5789
5790                       outrel.r_offset = (elf_gp (output_bfd)
5791                                          + sgot->output_offset + off);
5792                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5793                       outrel.r_addend = relocation;
5794                       loc = srelgot->contents;
5795                       loc +=
5796                         srelgot->reloc_count * sizeof (Elf32_External_Rela);
5797                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5798                       ++srelgot->reloc_count;
5799                     }
5800                   local_got_offsets[r_symndx] |= 1;
5801                 }
5802             }
5803           relocation = sgot->output_section->vma + sgot->output_offset + off
5804             - elf_gp (output_bfd);
5805
5806           if (relocation & align)
5807             {
5808               /* Incorrect alignment.  */
5809               _bfd_error_handler
5810                 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5811               ret = FALSE;
5812               r = bfd_reloc_dangerous;
5813               goto check_reloc;
5814             }
5815           break;
5816
5817         case R_NDS32_SDA16S3_RELA:
5818         case R_NDS32_SDA15S3_RELA:
5819         case R_NDS32_SDA15S3:
5820           align = 0x7;
5821           goto handle_sda;
5822
5823         case R_NDS32_SDA17S2_RELA:
5824         case R_NDS32_SDA15S2_RELA:
5825         case R_NDS32_SDA12S2_SP_RELA:
5826         case R_NDS32_SDA12S2_DP_RELA:
5827         case R_NDS32_SDA15S2:
5828         case R_NDS32_SDA_FP7U2_RELA:
5829           align = 0x3;
5830           goto handle_sda;
5831
5832         case R_NDS32_SDA18S1_RELA:
5833         case R_NDS32_SDA15S1_RELA:
5834         case R_NDS32_SDA15S1:
5835           align = 0x1;
5836           goto handle_sda;
5837
5838         case R_NDS32_SDA19S0_RELA:
5839         case R_NDS32_SDA15S0_RELA:
5840         case R_NDS32_SDA15S0:
5841           align = 0x0;
5842 handle_sda:
5843           BFD_ASSERT (sec != NULL);
5844
5845           /* If the symbol is in the abs section, the out_bfd will be null.
5846              This happens when the relocation has a symbol@GOTOFF.  */
5847           r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5848           if (r != bfd_reloc_ok)
5849             {
5850               _bfd_error_handler
5851                 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5852               ret = FALSE;
5853               goto check_reloc;
5854             }
5855
5856           /* At this point `relocation' contains the object's
5857              address.  */
5858           if (r_type == R_NDS32_SDA_FP7U2_RELA)
5859             {
5860               relocation -= fpbase_addr;
5861             }
5862           else
5863             relocation -= gp;
5864           /* Now it contains the offset from _SDA_BASE_.  */
5865
5866           /* Make sure alignment is correct.  */
5867
5868           if (relocation & align)
5869             {
5870               /* Incorrect alignment.  */
5871               _bfd_error_handler
5872                 /* xgettext:c-format */
5873                 (_("%pB(%pA): warning: unaligned small data access"
5874                    " of type %d"),
5875                  input_bfd, input_section, r_type);
5876               ret = FALSE;
5877               goto check_reloc;
5878             }
5879           break;
5880
5881         case R_NDS32_17IFC_PCREL_RELA:
5882         case R_NDS32_10IFCU_PCREL_RELA:
5883           /* Do nothing.  */
5884           break;
5885
5886         case R_NDS32_TLS_LE_HI20:
5887         case R_NDS32_TLS_LE_LO12:
5888         case R_NDS32_TLS_LE_20:
5889         case R_NDS32_TLS_LE_15S0:
5890         case R_NDS32_TLS_LE_15S1:
5891         case R_NDS32_TLS_LE_15S2:
5892           /* We do not have garbage collection for got entries.
5893              Therefore, IE to LE may have one empty entry, and DESC to
5894              LE may have two.  */
5895           if (elf_hash_table (info)->tls_sec != NULL)
5896             relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5897           break;
5898
5899         case R_NDS32_TLS_IE_HI20:
5900         case R_NDS32_TLS_IE_LO12S2:
5901         case R_NDS32_TLS_DESC_HI20:
5902         case R_NDS32_TLS_DESC_LO12:
5903         case R_NDS32_TLS_IE_LO12:
5904         case R_NDS32_TLS_IEGP_HI20:
5905         case R_NDS32_TLS_IEGP_LO12:
5906         case R_NDS32_TLS_IEGP_LO12S2:
5907           {
5908             /* Relocation is to the entry for this symbol in the global
5909                offset table.  */
5910             enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5911             asection *srelgot;
5912             Elf_Internal_Rela outrel;
5913             bfd_byte *loc;
5914             int indx = 0;
5915
5916             eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5917
5918             BFD_ASSERT (sgot != NULL);
5919             if (h != NULL)
5920               {
5921                 bfd_boolean dyn;
5922
5923                 off = h->got.offset;
5924                 BFD_ASSERT (off != (bfd_vma) -1);
5925                 dyn = htab->root.dynamic_sections_created;
5926                 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5927                 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5928                     && (!bfd_link_pic (info)
5929                         || !SYMBOL_REFERENCES_LOCAL (info, h)))
5930                   indx = h->dynindx;
5931               }
5932             else
5933               {
5934                 BFD_ASSERT (local_got_offsets != NULL
5935                             && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5936                 off = local_got_offsets[r_symndx];
5937                 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5938               }
5939
5940             relocation = sgot->output_section->vma + sgot->output_offset + off;
5941
5942             if (1 < ones32 (tls_type))
5943               {
5944                 eff_tls_type = 1 << (fls (tls_type) - 1);
5945                 /* TLS model shall be handled in nds32_elf_unify_tls_model ().  */
5946
5947                 /* TLS model X -> LE is not implement yet!
5948                    workaround here!  */
5949                 if (eff_tls_type == GOT_TLS_LE)
5950                   {
5951                     eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5952                   }
5953               }
5954
5955             /* The offset must always be a multiple of 4.  We use
5956                the least significant bit to record whether we have
5957                already processed this entry.  */
5958             bfd_boolean need_relocs = FALSE;
5959             srelgot = ehtab->srelgot;
5960             if ((bfd_link_pic (info) || indx != 0)
5961                 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5962                     || h->root.type != bfd_link_hash_undefweak))
5963               {
5964                 need_relocs = TRUE;
5965                 BFD_ASSERT (srelgot != NULL);
5966               }
5967
5968             if (off & 1)
5969               {
5970                 off &= ~1;
5971                 relocation &= ~1;
5972
5973                 if (eff_tls_type & GOT_TLS_DESC)
5974                   {
5975                     relocation -= elf_gp (output_bfd);
5976                     if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5977                       {
5978                         /* TLS model shall be converted.  */
5979                         BFD_ASSERT(0);
5980                       }
5981                   }
5982                 else if (eff_tls_type & GOT_TLS_IEGP)
5983                   {
5984                     relocation -= elf_gp (output_bfd);
5985                   }
5986               }
5987             else
5988               {
5989                 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5990                   {
5991                     /* TLS model workaround shall be applied.  */
5992                     BFD_ASSERT(0);
5993                   }
5994                 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5995                   {
5996                     if (eff_tls_type & GOT_TLS_IEGP)
5997                       relocation -= elf_gp(output_bfd);
5998
5999                     if (need_relocs)
6000                       {
6001                         if (indx == 0)
6002                           outrel.r_addend = gottpoff (info, relocation_sym);
6003                         else
6004                           outrel.r_addend = 0;
6005                         outrel.r_offset = (sgot->output_section->vma
6006                                            + sgot->output_offset + off);
6007                         outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
6008
6009                         elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
6010                                                   &outrel);
6011                       }
6012                     else
6013                       {
6014                         bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6015                                     sgot->contents + off);
6016                       }
6017                   }
6018                 else if (eff_tls_type & GOT_TLS_DESC)
6019                   {
6020                     relocation -= elf_gp (output_bfd);
6021                     if (need_relocs)
6022                       {
6023                         if (indx == 0)
6024                           outrel.r_addend = gottpoff (info, relocation_sym);
6025                         else
6026                           outrel.r_addend = 0;
6027                         outrel.r_offset = (sgot->output_section->vma
6028                                            + sgot->output_offset + off);
6029                         outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
6030
6031                         if (htab->tls_desc_trampoline)
6032                           {
6033                             asection *srelplt;
6034                             srelplt = ehtab->srelplt;
6035                             loc = srelplt->contents;
6036                             loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
6037                             BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
6038                                         <= srelplt->contents + srelplt->size);
6039
6040                             bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6041                           }
6042                         else
6043                           {
6044                             loc = srelgot->contents;
6045                             loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
6046                             bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6047                             ++srelgot->reloc_count;
6048                           }
6049                       }
6050                     else
6051                       {
6052                         /* feed me!  */
6053                         bfd_put_32 (output_bfd, 0xdeadbeef,
6054                                     sgot->contents + off);
6055                         bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6056                                     sgot->contents + off + 4);
6057                         patch_tls_desc_to_ie (contents, rel, input_bfd);
6058                         BFD_ASSERT(0);
6059                       }
6060                   }
6061                 else
6062                   {
6063                     /* TLS model workaround shall be applied.  */
6064                     BFD_ASSERT(0);
6065                   }
6066
6067                 if (h != NULL)
6068                   h->got.offset |= 1;
6069                 else
6070                   local_got_offsets[r_symndx] |= 1;
6071               }
6072           }
6073         break;
6074           /* DON'T fall through.  */
6075
6076         default:
6077           /* OLD_NDS32_RELOC.  */
6078
6079           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6080                                         contents, offset, relocation, addend);
6081           goto check_reloc;
6082         }
6083
6084       switch ((int) r_type)
6085         {
6086         case R_NDS32_20_RELA:
6087         case R_NDS32_5_RELA:
6088         case R_NDS32_9_PCREL_RELA:
6089         case R_NDS32_WORD_9_PCREL_RELA:
6090         case R_NDS32_10_UPCREL_RELA:
6091         case R_NDS32_15_PCREL_RELA:
6092         case R_NDS32_17_PCREL_RELA:
6093         case R_NDS32_25_PCREL_RELA:
6094         case R_NDS32_25_ABS_RELA:
6095         case R_NDS32_HI20_RELA:
6096         case R_NDS32_LO12S3_RELA:
6097         case R_NDS32_LO12S2_RELA:
6098         case R_NDS32_LO12S2_DP_RELA:
6099         case R_NDS32_LO12S2_SP_RELA:
6100         case R_NDS32_LO12S1_RELA:
6101         case R_NDS32_LO12S0_RELA:
6102         case R_NDS32_LO12S0_ORI_RELA:
6103         case R_NDS32_SDA16S3_RELA:
6104         case R_NDS32_SDA17S2_RELA:
6105         case R_NDS32_SDA18S1_RELA:
6106         case R_NDS32_SDA19S0_RELA:
6107         case R_NDS32_SDA15S3_RELA:
6108         case R_NDS32_SDA15S2_RELA:
6109         case R_NDS32_SDA12S2_DP_RELA:
6110         case R_NDS32_SDA12S2_SP_RELA:
6111         case R_NDS32_SDA15S1_RELA:
6112         case R_NDS32_SDA15S0_RELA:
6113         case R_NDS32_SDA_FP7U2_RELA:
6114         case R_NDS32_9_PLTREL:
6115         case R_NDS32_25_PLTREL:
6116         case R_NDS32_GOT20:
6117         case R_NDS32_GOT_HI20:
6118         case R_NDS32_GOT_LO12:
6119         case R_NDS32_GOT_LO15:
6120         case R_NDS32_GOT_LO19:
6121         case R_NDS32_GOT15S2_RELA:
6122         case R_NDS32_GOT17S2_RELA:
6123         case R_NDS32_GOTPC20:
6124         case R_NDS32_GOTPC_HI20:
6125         case R_NDS32_GOTPC_LO12:
6126         case R_NDS32_GOTOFF:
6127         case R_NDS32_GOTOFF_HI20:
6128         case R_NDS32_GOTOFF_LO12:
6129         case R_NDS32_GOTOFF_LO15:
6130         case R_NDS32_GOTOFF_LO19:
6131         case R_NDS32_PLTREL_HI20:
6132         case R_NDS32_PLTREL_LO12:
6133         case R_NDS32_PLT_GOTREL_HI20:
6134         case R_NDS32_PLT_GOTREL_LO12:
6135         case R_NDS32_PLT_GOTREL_LO15:
6136         case R_NDS32_PLT_GOTREL_LO19:
6137         case R_NDS32_PLT_GOTREL_LO20:
6138         case R_NDS32_17IFC_PCREL_RELA:
6139         case R_NDS32_10IFCU_PCREL_RELA:
6140         case R_NDS32_TLS_LE_HI20:
6141         case R_NDS32_TLS_LE_LO12:
6142         case R_NDS32_TLS_IE_HI20:
6143         case R_NDS32_TLS_IE_LO12S2:
6144         case R_NDS32_TLS_LE_20:
6145         case R_NDS32_TLS_LE_15S0:
6146         case R_NDS32_TLS_LE_15S1:
6147         case R_NDS32_TLS_LE_15S2:
6148         case R_NDS32_TLS_DESC_HI20:
6149         case R_NDS32_TLS_DESC_LO12:
6150         case R_NDS32_TLS_IE_LO12:
6151         case R_NDS32_TLS_IEGP_HI20:
6152         case R_NDS32_TLS_IEGP_LO12:
6153         case R_NDS32_TLS_IEGP_LO12S2:
6154           /* Instruction related relocs must handle endian properly.  */
6155           /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
6156           r = nds32_elf_final_link_relocate (howto, input_bfd,
6157                                              input_section, contents,
6158                                              rel->r_offset, relocation,
6159                                              rel->r_addend);
6160           break;
6161
6162         default:
6163           /* All other relocs can use default handler.  */
6164           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6165                                         contents, rel->r_offset,
6166                                         relocation, rel->r_addend);
6167           break;
6168         }
6169
6170 check_reloc:
6171
6172       if (r != bfd_reloc_ok)
6173         {
6174           /* FIXME: This should be generic enough to go in a utility.  */
6175           const char *name;
6176
6177           if (h != NULL)
6178             name = h->root.root.string;
6179           else
6180             {
6181               name = bfd_elf_string_from_elf_section
6182                       (input_bfd, symtab_hdr->sh_link, sym->st_name);
6183               if (name == NULL || *name == '\0')
6184                 name = bfd_section_name (input_bfd, sec);
6185             }
6186
6187           if (errmsg != NULL)
6188             goto common_error;
6189
6190           switch (r)
6191             {
6192             case bfd_reloc_overflow:
6193               (*info->callbacks->reloc_overflow)
6194                 (info, (h ? &h->root : NULL), name, howto->name,
6195                  (bfd_vma) 0, input_bfd, input_section, offset);
6196               break;
6197
6198             case bfd_reloc_undefined:
6199               (*info->callbacks->undefined_symbol)
6200                 (info, name, input_bfd, input_section, offset, TRUE);
6201               break;
6202
6203             case bfd_reloc_outofrange:
6204               errmsg = _("internal error: out of range error");
6205               goto common_error;
6206
6207             case bfd_reloc_notsupported:
6208               errmsg = _("internal error: unsupported relocation error");
6209               goto common_error;
6210
6211             case bfd_reloc_dangerous:
6212               errmsg = _("internal error: dangerous error");
6213               goto common_error;
6214
6215             default:
6216               errmsg = _("internal error: unknown error");
6217               /* Fall through.  */
6218
6219 common_error:
6220               (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6221                                            input_section, offset);
6222               break;
6223             }
6224         }
6225     }
6226
6227   /* Resotre header size to avoid overflow load.  */
6228   if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6229     symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6230
6231   return ret;
6232 }
6233
6234 /* Finish up dynamic symbol handling.  We set the contents of various
6235    dynamic sections here.  */
6236
6237 static bfd_boolean
6238 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6239                                  struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6240 {
6241   struct elf_link_hash_table *ehtab;
6242   struct elf_nds32_link_hash_entry *hent;
6243   bfd_byte *loc;
6244
6245   ehtab = elf_hash_table (info);
6246   hent = (struct elf_nds32_link_hash_entry *) h;
6247
6248   if (h->plt.offset != (bfd_vma) - 1)
6249     {
6250       asection *splt;
6251       asection *sgot;
6252       asection *srela;
6253
6254       bfd_vma plt_index;
6255       bfd_vma got_offset;
6256       bfd_vma local_plt_offset;
6257       Elf_Internal_Rela rela;
6258
6259       /* This symbol has an entry in the procedure linkage table.  Set
6260          it up.  */
6261
6262       BFD_ASSERT (h->dynindx != -1);
6263
6264       splt = ehtab->splt;
6265       sgot = ehtab->sgotplt;
6266       srela = ehtab->srelplt;
6267       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6268
6269       /* Get the index in the procedure linkage table which
6270          corresponds to this symbol.  This is the index of this symbol
6271          in all the symbols for which we are making plt entries.  The
6272          first entry in the procedure linkage table is reserved.  */
6273       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6274
6275       /* Get the offset into the .got table of the entry that
6276          corresponds to this function.  Each .got entry is 4 bytes.
6277          The first three are reserved.  */
6278       got_offset = (plt_index + 3) * 4;
6279
6280       /* Fill in the entry in the procedure linkage table.  */
6281       if (!bfd_link_pic (info))
6282         {
6283           unsigned long insn;
6284
6285           insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6286                                       + sgot->output_offset + got_offset) >> 12)
6287                                     & 0xfffff);
6288           bfd_putb32 (insn, splt->contents + h->plt.offset);
6289
6290           insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6291                                       + sgot->output_offset + got_offset) & 0x0fff)
6292                                     >> 2);
6293           bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6294
6295           insn = PLT_ENTRY_WORD2;
6296           bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6297
6298           insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6299           bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6300
6301           insn = PLT_ENTRY_WORD4
6302                  + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6303           bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6304           local_plt_offset = 12;
6305         }
6306       else
6307         {
6308           /* sda_base must be set at this time.  */
6309           unsigned long insn;
6310           long offset;
6311
6312           offset = sgot->output_section->vma + sgot->output_offset + got_offset
6313                    - elf_gp (output_bfd);
6314           insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6315           bfd_putb32 (insn, splt->contents + h->plt.offset);
6316
6317           insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6318           bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6319
6320           insn = PLT_PIC_ENTRY_WORD2;
6321           bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6322
6323           insn = PLT_PIC_ENTRY_WORD3;
6324           bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6325
6326           insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6327           bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6328
6329           insn = PLT_PIC_ENTRY_WORD5
6330             + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6331           bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6332
6333           local_plt_offset = 16;
6334         }
6335
6336       /* Fill in the entry in the global offset table,
6337          so it will fall through to the next instruction for the first time.  */
6338       bfd_put_32 (output_bfd,
6339                   (splt->output_section->vma + splt->output_offset
6340                    + h->plt.offset + local_plt_offset),
6341                   sgot->contents + got_offset);
6342
6343       /* Fill in the entry in the .rela.plt section.  */
6344       rela.r_offset = (sgot->output_section->vma
6345                        + sgot->output_offset + got_offset);
6346       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6347       rela.r_addend = 0;
6348       loc = srela->contents;
6349       loc += plt_index * sizeof (Elf32_External_Rela);
6350       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6351
6352       if (!h->def_regular)
6353         {
6354           /* Mark the symbol as undefined, rather than as defined in
6355              the .plt section.  Leave the value alone.  */
6356           sym->st_shndx = SHN_UNDEF;
6357           if (!h->ref_regular_nonweak)
6358             sym->st_value = 0;
6359         }
6360     }
6361
6362   if (h->got.offset != (bfd_vma) - 1
6363       && hent->tls_type == GOT_NORMAL)
6364     {
6365       asection *sgot;
6366       asection *srelagot;
6367       Elf_Internal_Rela rela;
6368
6369       /* This symbol has an entry in the global offset table.
6370          Set it up.  */
6371
6372       sgot = ehtab->sgot;
6373       srelagot = ehtab->srelgot;
6374       BFD_ASSERT (sgot != NULL && srelagot != NULL);
6375
6376       rela.r_offset = (sgot->output_section->vma
6377                        + sgot->output_offset + (h->got.offset & ~1));
6378
6379       /* If this is a -Bsymbolic link, and the symbol is defined
6380          locally, we just want to emit a RELATIVE reloc.  Likewise if
6381          the symbol was forced to be local because of a version file.
6382          The entry in the global offset table will already have been
6383          initialized in the relocate_section function.  */
6384       if ((bfd_link_pic (info)
6385            && (info->symbolic || h->dynindx == -1 || h->forced_local)
6386            && h->def_regular)
6387           || (bfd_link_pie (info) && h->def_regular))
6388         {
6389           rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6390           rela.r_addend = (h->root.u.def.value
6391                            + h->root.u.def.section->output_section->vma
6392                            + h->root.u.def.section->output_offset);
6393
6394           if ((h->got.offset & 1) == 0)
6395             {
6396               bfd_put_32 (output_bfd, rela.r_addend,
6397                           sgot->contents + h->got.offset);
6398             }
6399         }
6400       else
6401         {
6402           BFD_ASSERT ((h->got.offset & 1) == 0);
6403           bfd_put_32 (output_bfd, (bfd_vma) 0,
6404                       sgot->contents + h->got.offset);
6405           rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6406           rela.r_addend = 0;
6407         }
6408
6409       loc = srelagot->contents;
6410       loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6411       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6412       ++srelagot->reloc_count;
6413       BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6414     }
6415
6416   if (h->needs_copy)
6417     {
6418       asection *s;
6419       Elf_Internal_Rela rela;
6420
6421       /* This symbols needs a copy reloc.  Set it up.  */
6422
6423       BFD_ASSERT (h->dynindx != -1
6424                   && (h->root.type == bfd_link_hash_defined
6425                       || h->root.type == bfd_link_hash_defweak));
6426
6427       s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6428       BFD_ASSERT (s != NULL);
6429
6430       rela.r_offset = (h->root.u.def.value
6431                        + h->root.u.def.section->output_section->vma
6432                        + h->root.u.def.section->output_offset);
6433       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6434       rela.r_addend = 0;
6435       loc = s->contents;
6436       loc += s->reloc_count * sizeof (Elf32_External_Rela);
6437       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6438       ++s->reloc_count;
6439     }
6440
6441   /* Mark some specially defined symbols as absolute.  */
6442   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6443       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6444     sym->st_shndx = SHN_ABS;
6445
6446   return TRUE;
6447 }
6448
6449
6450 /* Finish up the dynamic sections.  */
6451
6452 static bfd_boolean
6453 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6454 {
6455   bfd *dynobj;
6456   asection *sdyn;
6457   asection *sgotplt;
6458   struct elf_link_hash_table *ehtab;
6459   struct elf_nds32_link_hash_table *htab;
6460
6461   ehtab = elf_hash_table (info);
6462   htab = nds32_elf_hash_table (info);
6463   if (htab == NULL)
6464     return FALSE;
6465
6466   dynobj = elf_hash_table (info)->dynobj;
6467
6468   sgotplt = ehtab->sgotplt;
6469   /* A broken linker script might have discarded the dynamic sections.
6470      Catch this here so that we do not seg-fault later on.  */
6471   if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6472     return FALSE;
6473   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6474
6475   if (elf_hash_table (info)->dynamic_sections_created)
6476     {
6477       asection *splt;
6478       Elf32_External_Dyn *dyncon, *dynconend;
6479
6480       BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6481
6482       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6483       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6484
6485       for (; dyncon < dynconend; dyncon++)
6486         {
6487           Elf_Internal_Dyn dyn;
6488           asection *s;
6489
6490           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6491
6492           switch (dyn.d_tag)
6493             {
6494             default:
6495               break;
6496
6497             case DT_PLTGOT:
6498               /* name = ".got";  */
6499               s = ehtab->sgot->output_section;
6500               goto get_vma;
6501             case DT_JMPREL:
6502               s = ehtab->srelplt->output_section;
6503 get_vma:
6504               BFD_ASSERT (s != NULL);
6505               dyn.d_un.d_ptr = s->vma;
6506               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6507               break;
6508
6509             case DT_PLTRELSZ:
6510               s = ehtab->srelplt->output_section;
6511               BFD_ASSERT (s != NULL);
6512               dyn.d_un.d_val = s->size;
6513               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6514               break;
6515
6516             case DT_RELASZ:
6517               /* My reading of the SVR4 ABI indicates that the
6518                  procedure linkage table relocs (DT_JMPREL) should be
6519                  included in the overall relocs (DT_RELA).  This is
6520                  what Solaris does.  However, UnixWare can not handle
6521                  that case.  Therefore, we override the DT_RELASZ entry
6522                  here to make it not include the JMPREL relocs.  Since
6523                  the linker script arranges for .rela.plt to follow all
6524                  other relocation sections, we don't have to worry
6525                  about changing the DT_RELA entry.  */
6526               if (ehtab->srelplt != NULL)
6527                 {
6528                   s = ehtab->srelplt->output_section;
6529                   dyn.d_un.d_val -= s->size;
6530                 }
6531               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6532               break;
6533
6534             case DT_TLSDESC_PLT:
6535               s = htab->root.splt;
6536               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6537                                 + htab->dt_tlsdesc_plt);
6538               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6539               break;
6540
6541             case DT_TLSDESC_GOT:
6542               s = htab->root.sgot;
6543               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6544                                 + htab->dt_tlsdesc_got);
6545               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6546               break;
6547             }
6548         }
6549
6550       /* Fill in the first entry in the procedure linkage table.  */
6551       splt = ehtab->splt;
6552       if (splt && splt->size > 0)
6553         {
6554           if (bfd_link_pic (info))
6555             {
6556               unsigned long insn;
6557               long offset;
6558
6559               offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6560                 - elf_gp (output_bfd);
6561               insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6562               bfd_putb32 (insn, splt->contents);
6563
6564               /* here has a typo?  */
6565               insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6566               bfd_putb32 (insn, splt->contents + 4);
6567
6568               insn = PLT0_PIC_ENTRY_WORD2;
6569               bfd_putb32 (insn, splt->contents + 8);
6570
6571               insn = PLT0_PIC_ENTRY_WORD3;
6572               bfd_putb32 (insn, splt->contents + 12);
6573
6574               insn = PLT0_PIC_ENTRY_WORD4;
6575               bfd_putb32 (insn, splt->contents + 16);
6576
6577               insn = PLT0_PIC_ENTRY_WORD5;
6578               bfd_putb32 (insn, splt->contents + 20);
6579             }
6580           else
6581             {
6582               unsigned long insn;
6583               unsigned long addr;
6584
6585               /* addr = .got + 4 */
6586               addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6587               insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6588               bfd_putb32 (insn, splt->contents);
6589
6590               insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6591               bfd_putb32 (insn, splt->contents + 4);
6592
6593               insn = PLT0_ENTRY_WORD2;
6594               bfd_putb32 (insn, splt->contents + 8);
6595
6596               insn = PLT0_ENTRY_WORD3;
6597               bfd_putb32 (insn, splt->contents + 12);
6598
6599               insn = PLT0_ENTRY_WORD4;
6600               bfd_putb32 (insn, splt->contents + 16);
6601             }
6602
6603           elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6604             PLT_ENTRY_SIZE;
6605         }
6606
6607       if (htab->dt_tlsdesc_plt)
6608         {
6609           /* Calculate addresses.  */
6610           asection *sgot = sgot = ehtab->sgot;
6611           bfd_vma pltgot = sgotplt->output_section->vma
6612             + sgotplt->output_offset;
6613           bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6614             + htab->dt_tlsdesc_got;
6615
6616           /* Get GP offset.  */
6617           pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1]  */
6618           tlsdesc_got -= elf_gp (output_bfd);
6619
6620           /* Do relocation.  */
6621           dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6622           dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6623           dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6624           dl_tlsdesc_lazy_trampoline[5] +=  0xfff & pltgot;
6625
6626           /* Insert .plt.  */
6627           nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6628                                 dl_tlsdesc_lazy_trampoline,
6629                                 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6630         }
6631     }
6632
6633   /* Fill in the first three entries in the global offset table.  */
6634   if (sgotplt && sgotplt->size > 0)
6635     {
6636       if (sdyn == NULL)
6637         bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6638       else
6639         bfd_put_32 (output_bfd,
6640                     sdyn->output_section->vma + sdyn->output_offset,
6641                     sgotplt->contents);
6642       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6643       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6644
6645       elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6646     }
6647
6648   return TRUE;
6649 }
6650 \f
6651
6652 /* Set the right machine number.  */
6653
6654 static bfd_boolean
6655 nds32_elf_object_p (bfd *abfd)
6656 {
6657   static unsigned int cur_arch = 0;
6658
6659   if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6660     {
6661       /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
6662       cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6663     }
6664
6665   switch (cur_arch)
6666     {
6667     default:
6668     case E_N1_ARCH:
6669       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6670       break;
6671     case E_N1H_ARCH:
6672       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6673       break;
6674     case E_NDS_ARCH_STAR_V2_0:
6675       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6676       break;
6677     case E_NDS_ARCH_STAR_V3_0:
6678       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6679       break;
6680     case E_NDS_ARCH_STAR_V3_M:
6681       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6682       break;
6683     }
6684
6685   return TRUE;
6686 }
6687
6688 /* Store the machine number in the flags field.  */
6689
6690 static void
6691 nds32_elf_final_write_processing (bfd *abfd,
6692                                   bfd_boolean linker ATTRIBUTE_UNUSED)
6693 {
6694   unsigned long val;
6695   static unsigned int cur_mach = 0;
6696
6697   if (bfd_mach_n1 != bfd_get_mach (abfd))
6698     {
6699       cur_mach = bfd_get_mach (abfd);
6700     }
6701
6702   switch (cur_mach)
6703     {
6704     case bfd_mach_n1:
6705       /* Only happen when object is empty, since the case is abandon.  */
6706       val = E_N1_ARCH;
6707       val |= E_NDS_ABI_AABI;
6708       val |= E_NDS32_ELF_VER_1_4;
6709       break;
6710     case bfd_mach_n1h:
6711       val = E_N1H_ARCH;
6712       break;
6713     case bfd_mach_n1h_v2:
6714       val = E_NDS_ARCH_STAR_V2_0;
6715       break;
6716     case bfd_mach_n1h_v3:
6717       val = E_NDS_ARCH_STAR_V3_0;
6718       break;
6719     case bfd_mach_n1h_v3m:
6720       val = E_NDS_ARCH_STAR_V3_M;
6721       break;
6722     default:
6723       val = 0;
6724       break;
6725     }
6726
6727   elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6728   elf_elfheader (abfd)->e_flags |= val;
6729 }
6730
6731 /* Function to keep NDS32 specific file flags.  */
6732
6733 static bfd_boolean
6734 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6735 {
6736   BFD_ASSERT (!elf_flags_init (abfd)
6737               || elf_elfheader (abfd)->e_flags == flags);
6738
6739   elf_elfheader (abfd)->e_flags = flags;
6740   elf_flags_init (abfd) = TRUE;
6741   return TRUE;
6742 }
6743
6744 static unsigned int
6745 convert_e_flags (unsigned int e_flags, unsigned int arch)
6746 {
6747   if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6748     {
6749       /* From 0.9 to 1.0.  */
6750       e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6751
6752       /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6753       e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6754       if (arch == E_NDS_ARCH_STAR_V1_0)
6755         {
6756           /* Done.  */
6757           return e_flags;
6758         }
6759     }
6760
6761   /* From 1.0 to 2.0.  */
6762   e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6763
6764   /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
6765   e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6766
6767   /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6768   e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6769   return e_flags;
6770 }
6771
6772 static bfd_boolean
6773 nds32_check_vec_size (bfd *ibfd)
6774 {
6775   static unsigned int nds32_vec_size = 0;
6776
6777   asection *sec_t = NULL;
6778   bfd_byte *contents = NULL;
6779
6780   sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6781
6782   if (sec_t && sec_t->size >= 4)
6783     {
6784       /* Get vec_size in file.  */
6785       unsigned int flag_t;
6786
6787       nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
6788       flag_t = bfd_get_32 (ibfd, contents);
6789
6790       /* The value could only be 4 or 16.  */
6791
6792       if (!nds32_vec_size)
6793         /* Set if not set yet.  */
6794         nds32_vec_size = (flag_t & 0x3);
6795       else if (nds32_vec_size != (flag_t & 0x3))
6796         {
6797           _bfd_error_handler
6798             /* xgettext:c-format */
6799             (_("%pB: ISR vector size mismatch"
6800                " with previous modules, previous %u-byte, current %u-byte"),
6801              ibfd,
6802              nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6803              (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6804           return FALSE;
6805         }
6806       else
6807         /* Only keep the first vec_size section.  */
6808         sec_t->flags |= SEC_EXCLUDE;
6809     }
6810
6811   return TRUE;
6812 }
6813
6814 /* Merge backend specific data from an object file to the output
6815    object file when linking.  */
6816
6817 static bfd_boolean
6818 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6819 {
6820   bfd *obfd = info->output_bfd;
6821   flagword out_flags;
6822   flagword in_flags;
6823   flagword out_16regs;
6824   flagword in_no_mac;
6825   flagword out_no_mac;
6826   flagword in_16regs;
6827   flagword out_version;
6828   flagword in_version;
6829   flagword out_fpu_config;
6830   flagword in_fpu_config;
6831
6832   /* TODO: Revise to use object-attributes instead.  */
6833   if (!nds32_check_vec_size (ibfd))
6834     return FALSE;
6835
6836   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6837       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6838     return TRUE;
6839
6840   if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6841     {
6842       _bfd_error_handler
6843         (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6844
6845       bfd_set_error (bfd_error_bad_value);
6846       return FALSE;
6847     }
6848
6849   /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6850      treat as generic one without checking and merging.  */
6851   if (elf_elfheader (ibfd)->e_flags)
6852     {
6853       in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6854       if (in_version == E_NDS32_ELF_VER_1_2)
6855         {
6856           _bfd_error_handler
6857             (_("%pB: warning: older version of object file encountered, "
6858                "please recompile with current tool chain"), ibfd);
6859         }
6860
6861       /* We may need to merge V1 and V2 arch object files to V2.  */
6862       if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6863           != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6864         {
6865           /* Need to convert version.  */
6866           if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6867               == E_NDS_ARCH_STAR_RESERVED)
6868             {
6869               elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6870             }
6871           else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6872                    == E_NDS_ARCH_STAR_V3_M
6873                    && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6874                    == E_NDS_ARCH_STAR_V3_0)
6875             {
6876               elf_elfheader (ibfd)->e_flags =
6877                 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6878                 | E_NDS_ARCH_STAR_V3_0;
6879             }
6880           else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6881                    == E_NDS_ARCH_STAR_V0_9
6882                    || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6883                    > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6884             {
6885               elf_elfheader (obfd)->e_flags =
6886                 convert_e_flags (elf_elfheader (obfd)->e_flags,
6887                                  (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6888             }
6889           else
6890             {
6891               elf_elfheader (ibfd)->e_flags =
6892                 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6893                                  (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6894             }
6895         }
6896
6897       /* Extract some flags.  */
6898       in_flags = elf_elfheader (ibfd)->e_flags
6899         & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6900              | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6901
6902       /* The following flags need special treatment.  */
6903       in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6904       in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6905       in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6906
6907       /* Extract some flags.  */
6908       out_flags = elf_elfheader (obfd)->e_flags
6909         & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6910              | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6911
6912       /* The following flags need special treatment.  */
6913       out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6914       out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6915       out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6916       out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6917       if (!elf_flags_init (obfd))
6918         {
6919           /* If the input is the default architecture then do not
6920              bother setting the flags for the output architecture,
6921              instead allow future merges to do this.  If no future
6922              merges ever set these flags then they will retain their
6923              unitialised values, which surprise surprise, correspond
6924              to the default values.  */
6925           if (bfd_get_arch_info (ibfd)->the_default)
6926             return TRUE;
6927
6928           elf_flags_init (obfd) = TRUE;
6929           elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6930
6931           if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6932               && bfd_get_arch_info (obfd)->the_default)
6933             {
6934               return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6935                                         bfd_get_mach (ibfd));
6936             }
6937
6938           return TRUE;
6939         }
6940
6941       /* Check flag compatibility.  */
6942       if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6943         {
6944           _bfd_error_handler
6945             (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6946           bfd_set_error (bfd_error_bad_value);
6947           return FALSE;
6948         }
6949
6950       if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6951         {
6952           if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6953             {
6954               _bfd_error_handler
6955                 (_("%pB: error: instruction set mismatch with previous modules"),
6956                  ibfd);
6957
6958               bfd_set_error (bfd_error_bad_value);
6959               return FALSE;
6960             }
6961         }
6962
6963       /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6964          and perf ext1 and DIV are mergerd to perf ext1.  */
6965       if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6966         {
6967           elf_elfheader (obfd)->e_flags =
6968             (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6969             | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6970             | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6971                ?  E_NDS32_HAS_EXT_INST : 0)
6972             | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6973                ?  E_NDS32_HAS_EXT_INST : 0)
6974             | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6975             | ((in_version > out_version) ? out_version : in_version);
6976         }
6977       else
6978         {
6979           if (in_version != out_version)
6980             _bfd_error_handler
6981               /* xgettext:c-format */
6982               (_("%pB: warning: incompatible elf-versions %s and %s"),
6983                ibfd, nds32_elfver_strtab[out_version],
6984                nds32_elfver_strtab[in_version]);
6985
6986           elf_elfheader (obfd)->e_flags = in_flags | out_flags
6987             | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6988             | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6989             | (in_version > out_version ?  out_version : in_version);
6990         }
6991     }
6992
6993   return TRUE;
6994 }
6995
6996 /* Display the flags field.  */
6997
6998 static bfd_boolean
6999 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
7000 {
7001   FILE *file = (FILE *) ptr;
7002
7003   BFD_ASSERT (abfd != NULL && ptr != NULL);
7004
7005   _bfd_elf_print_private_bfd_data (abfd, ptr);
7006
7007   fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
7008
7009   switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
7010     {
7011     default:
7012     case E_N1_ARCH:
7013       fprintf (file, _(": n1 instructions"));
7014       break;
7015     case E_N1H_ARCH:
7016       fprintf (file, _(": n1h instructions"));
7017       break;
7018     }
7019
7020   fputc ('\n', file);
7021
7022   return TRUE;
7023 }
7024
7025 static unsigned int
7026 nds32_elf_action_discarded (asection *sec)
7027 {
7028
7029   if (strncmp
7030       (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7031     return 0;
7032
7033   return _bfd_elf_default_action_discarded (sec);
7034 }
7035
7036 static asection *
7037 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7038                         Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7039                         Elf_Internal_Sym *sym)
7040 {
7041   if (h != NULL)
7042     switch (ELF32_R_TYPE (rel->r_info))
7043       {
7044       case R_NDS32_GNU_VTINHERIT:
7045       case R_NDS32_GNU_VTENTRY:
7046       case R_NDS32_RELA_GNU_VTINHERIT:
7047       case R_NDS32_RELA_GNU_VTENTRY:
7048         return NULL;
7049       }
7050
7051   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7052 }
7053
7054 static enum elf_nds32_tls_type
7055 get_tls_type (enum elf_nds32_reloc_type r_type,
7056               struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7057 {
7058   enum elf_nds32_tls_type tls_type;
7059
7060   switch (r_type)
7061     {
7062     case R_NDS32_TLS_LE_HI20:
7063     case R_NDS32_TLS_LE_LO12:
7064       tls_type = GOT_TLS_LE;
7065       break;
7066     case R_NDS32_TLS_IE_HI20:
7067     case R_NDS32_TLS_IE_LO12S2:
7068     case R_NDS32_TLS_IE_LO12:
7069       tls_type = GOT_TLS_IE;
7070       break;
7071     case R_NDS32_TLS_IEGP_HI20:
7072     case R_NDS32_TLS_IEGP_LO12:
7073     case R_NDS32_TLS_IEGP_LO12S2:
7074       tls_type = GOT_TLS_IEGP;
7075       break;
7076     case R_NDS32_TLS_DESC_HI20:
7077     case R_NDS32_TLS_DESC_LO12:
7078     case R_NDS32_TLS_DESC_ADD:
7079     case R_NDS32_TLS_DESC_FUNC:
7080     case R_NDS32_TLS_DESC_CALL:
7081       tls_type = GOT_TLS_DESC;
7082       break;
7083     default:
7084       tls_type = GOT_NORMAL;
7085       break;
7086     }
7087
7088   return tls_type;
7089 }
7090
7091 /* Ensure that we have allocated bookkeeping structures for ABFD's local
7092    symbols.  */
7093
7094 static bfd_boolean
7095 elf32_nds32_allocate_local_sym_info (bfd *abfd)
7096 {
7097   if (elf_local_got_refcounts (abfd) == NULL)
7098     {
7099       bfd_size_type num_syms;
7100       bfd_size_type size;
7101       char *data;
7102
7103       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7104       /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7105          gp_offset.  The details can refer to struct elf_nds32_obj_tdata.  */
7106       size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7107                          + sizeof (bfd_vma) + sizeof (int)
7108                          + sizeof (bfd_boolean) + sizeof (bfd_vma));
7109       data = bfd_zalloc (abfd, size);
7110       if (data == NULL)
7111         return FALSE;
7112
7113       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7114       data += num_syms * sizeof (bfd_signed_vma);
7115
7116       elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7117       data += num_syms * sizeof (char);
7118
7119       elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7120       data += num_syms * sizeof (bfd_vma);
7121
7122       elf32_nds32_local_gp_offset (abfd) = (int *) data;
7123       data += num_syms * sizeof (int);
7124     }
7125
7126   return TRUE;
7127 }
7128
7129 /* Look through the relocs for a section during the first phase.
7130    Since we don't do .gots or .plts, we just need to consider the
7131    virtual table relocs for gc.  */
7132
7133 static bfd_boolean
7134 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7135                         asection *sec, const Elf_Internal_Rela *relocs)
7136 {
7137   Elf_Internal_Shdr *symtab_hdr;
7138   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7139   const Elf_Internal_Rela *rel;
7140   const Elf_Internal_Rela *rel_end;
7141   struct elf_link_hash_table *ehtab;
7142   struct elf_nds32_link_hash_table *htab;
7143   bfd *dynobj;
7144   asection *sreloc = NULL;
7145
7146   /* No need for relocation if relocatable already.  */
7147   if (bfd_link_relocatable (info))
7148     {
7149       elf32_nds32_check_relax_group (abfd, sec);
7150       return TRUE;
7151     }
7152
7153   /* Don't do anything special with non-loaded, non-alloced sections.
7154      In particular, any relocs in such sections should not affect GOT
7155      and PLT reference counting (ie. we don't allow them to create GOT
7156      or PLT entries), there's no possibility or desire to optimize TLS
7157      relocs, and there's not much point in propagating relocs to shared
7158      libs that the dynamic linker won't relocate.  */
7159   if ((sec->flags & SEC_ALLOC) == 0)
7160     return TRUE;
7161
7162   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7163   sym_hashes = elf_sym_hashes (abfd);
7164   sym_hashes_end =
7165     sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7166   if (!elf_bad_symtab (abfd))
7167     sym_hashes_end -= symtab_hdr->sh_info;
7168
7169   ehtab = elf_hash_table (info);
7170   htab = nds32_elf_hash_table (info);
7171   dynobj = htab->root.dynobj;
7172
7173   rel_end = relocs + sec->reloc_count;
7174   for (rel = relocs; rel < rel_end; rel++)
7175     {
7176       enum elf_nds32_reloc_type r_type;
7177       struct elf_link_hash_entry *h;
7178       unsigned long r_symndx;
7179       enum elf_nds32_tls_type tls_type, old_tls_type;
7180
7181       r_symndx = ELF32_R_SYM (rel->r_info);
7182       r_type = ELF32_R_TYPE (rel->r_info);
7183       if (r_symndx < symtab_hdr->sh_info)
7184         h = NULL;
7185       else
7186         {
7187           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7188           while (h->root.type == bfd_link_hash_indirect
7189                  || h->root.type == bfd_link_hash_warning)
7190             h = (struct elf_link_hash_entry *) h->root.u.i.link;
7191         }
7192
7193       /* Create .got section if necessary.
7194          Some relocs require a global offset table.  We create
7195          got section here, since these relocation need a got section
7196          and if it is not created yet.  */
7197       if (ehtab->sgot == NULL)
7198         {
7199           switch (r_type)
7200             {
7201             case R_NDS32_GOT_HI20:
7202             case R_NDS32_GOT_LO12:
7203             case R_NDS32_GOT_LO15:
7204             case R_NDS32_GOT_LO19:
7205             case R_NDS32_GOT17S2_RELA:
7206             case R_NDS32_GOT15S2_RELA:
7207             case R_NDS32_GOTOFF:
7208             case R_NDS32_GOTOFF_HI20:
7209             case R_NDS32_GOTOFF_LO12:
7210             case R_NDS32_GOTOFF_LO15:
7211             case R_NDS32_GOTOFF_LO19:
7212             case R_NDS32_GOTPC20:
7213             case R_NDS32_GOTPC_HI20:
7214             case R_NDS32_GOTPC_LO12:
7215             case R_NDS32_GOT20:
7216             case R_NDS32_TLS_IE_HI20:
7217             case R_NDS32_TLS_IE_LO12:
7218             case R_NDS32_TLS_IE_LO12S2:
7219             case R_NDS32_TLS_IEGP_HI20:
7220             case R_NDS32_TLS_IEGP_LO12:
7221             case R_NDS32_TLS_IEGP_LO12S2:
7222             case R_NDS32_TLS_DESC_HI20:
7223             case R_NDS32_TLS_DESC_LO12:
7224               if (dynobj == NULL)
7225                 htab->root.dynobj = dynobj = abfd;
7226               if (!create_got_section (dynobj, info))
7227                 return FALSE;
7228               break;
7229
7230             default:
7231               break;
7232             }
7233         }
7234
7235       /* Check relocation type.  */
7236       switch ((int) r_type)
7237         {
7238         case R_NDS32_GOT_HI20:
7239         case R_NDS32_GOT_LO12:
7240         case R_NDS32_GOT_LO15:
7241         case R_NDS32_GOT_LO19:
7242         case R_NDS32_GOT20:
7243         case R_NDS32_TLS_LE_HI20:
7244         case R_NDS32_TLS_LE_LO12:
7245         case R_NDS32_TLS_IE_HI20:
7246         case R_NDS32_TLS_IE_LO12:
7247         case R_NDS32_TLS_IE_LO12S2:
7248         case R_NDS32_TLS_IEGP_HI20:
7249         case R_NDS32_TLS_IEGP_LO12:
7250         case R_NDS32_TLS_IEGP_LO12S2:
7251         case R_NDS32_TLS_DESC_HI20:
7252         case R_NDS32_TLS_DESC_LO12:
7253           tls_type = get_tls_type (r_type, h);
7254           if (h)
7255             {
7256               if (tls_type != GOT_TLS_LE)
7257                 h->got.refcount += 1;
7258               old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7259             }
7260           else
7261             {
7262               /* This is a global offset table entry for a local symbol.  */
7263               if (!elf32_nds32_allocate_local_sym_info (abfd))
7264                 return FALSE;
7265
7266               BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7267               if (tls_type != GOT_TLS_LE)
7268                 elf_local_got_refcounts (abfd)[r_symndx] += 1;
7269               old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7270             }
7271
7272           /* We would already have issued an error message if there
7273              is a TLS/non-TLS mismatch, based on the symbol
7274              type.  So just combine any TLS types needed.  */
7275           if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7276               && tls_type != GOT_NORMAL)
7277             tls_type |= old_tls_type;
7278
7279           /* DESC to IE/IEGP if link to executable.  */
7280           if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7281               && (bfd_link_executable (info)))
7282             tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7283
7284           if (old_tls_type != tls_type)
7285             {
7286               if (h != NULL)
7287                 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7288               else
7289                 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7290             }
7291           break;
7292         case R_NDS32_9_PLTREL:
7293         case R_NDS32_25_PLTREL:
7294         case R_NDS32_PLTREL_HI20:
7295         case R_NDS32_PLTREL_LO12:
7296         case R_NDS32_PLT_GOTREL_HI20:
7297         case R_NDS32_PLT_GOTREL_LO12:
7298         case R_NDS32_PLT_GOTREL_LO15:
7299         case R_NDS32_PLT_GOTREL_LO19:
7300         case R_NDS32_PLT_GOTREL_LO20:
7301
7302           /* This symbol requires a procedure linkage table entry.  We
7303              actually build the entry in adjust_dynamic_symbol,
7304              because this might be a case of linking PIC code without
7305              linking in any dynamic objects, in which case we don't
7306              need to generate a procedure linkage table after all.  */
7307
7308           /* If this is a local symbol, we resolve it directly without
7309              creating a procedure linkage table entry.  */
7310           if (h == NULL)
7311             continue;
7312
7313           if (h->forced_local
7314               || (bfd_link_pie (info) && h->def_regular))
7315             break;
7316
7317           elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7318           h->needs_plt = 1;
7319           h->plt.refcount += 1;
7320           break;
7321
7322         case R_NDS32_16_RELA:
7323         case R_NDS32_20_RELA:
7324         case R_NDS32_5_RELA:
7325         case R_NDS32_32_RELA:
7326         case R_NDS32_HI20_RELA:
7327         case R_NDS32_LO12S3_RELA:
7328         case R_NDS32_LO12S2_RELA:
7329         case R_NDS32_LO12S2_DP_RELA:
7330         case R_NDS32_LO12S2_SP_RELA:
7331         case R_NDS32_LO12S1_RELA:
7332         case R_NDS32_LO12S0_RELA:
7333         case R_NDS32_LO12S0_ORI_RELA:
7334         case R_NDS32_SDA16S3_RELA:
7335         case R_NDS32_SDA17S2_RELA:
7336         case R_NDS32_SDA18S1_RELA:
7337         case R_NDS32_SDA19S0_RELA:
7338         case R_NDS32_SDA15S3_RELA:
7339         case R_NDS32_SDA15S2_RELA:
7340         case R_NDS32_SDA12S2_DP_RELA:
7341         case R_NDS32_SDA12S2_SP_RELA:
7342         case R_NDS32_SDA15S1_RELA:
7343         case R_NDS32_SDA15S0_RELA:
7344         case R_NDS32_SDA_FP7U2_RELA:
7345         case R_NDS32_15_PCREL_RELA:
7346         case R_NDS32_17_PCREL_RELA:
7347         case R_NDS32_25_PCREL_RELA:
7348
7349           if (h != NULL && !bfd_link_pic (info))
7350             {
7351               h->non_got_ref = 1;
7352               h->plt.refcount += 1;
7353             }
7354
7355           /* If we are creating a shared library, and this is a reloc against
7356              a global symbol, or a non PC relative reloc against a local
7357              symbol, then we need to copy the reloc into the shared library.
7358              However, if we are linking with -Bsymbolic, we do not need to
7359              copy a reloc against a global symbol which is defined in an
7360              object we are including in the link (i.e., DEF_REGULAR is set).
7361              At this point we have not seen all the input files, so it is
7362              possible that DEF_REGULAR is not set now but will be set later
7363              (it is never cleared).  We account for that possibility below by
7364              storing information in the dyn_relocs field of the hash table
7365              entry.  A similar situation occurs when creating shared libraries
7366              and symbol visibility changes render the symbol local.
7367
7368              If on the other hand, we are creating an executable, we may need
7369              to keep relocations for symbols satisfied by a dynamic library
7370              if we manage to avoid copy relocs for the symbol.  */
7371           if ((bfd_link_pic (info)
7372                && (sec->flags & SEC_ALLOC) != 0
7373                && ((r_type != R_NDS32_25_PCREL_RELA
7374                     && r_type != R_NDS32_15_PCREL_RELA
7375                     && r_type != R_NDS32_17_PCREL_RELA
7376                     && !(r_type == R_NDS32_32_RELA
7377                          && strcmp (sec->name, ".eh_frame") == 0))
7378                    || (h != NULL
7379                        && (!info->symbolic
7380                            || h->root.type == bfd_link_hash_defweak
7381                            || !h->def_regular))))
7382               || (!bfd_link_pic (info)
7383                   && (sec->flags & SEC_ALLOC) != 0
7384                   && h != NULL
7385                   && (h->root.type == bfd_link_hash_defweak
7386                       || !h->def_regular)))
7387             {
7388               struct elf_dyn_relocs *p;
7389               struct elf_dyn_relocs **head;
7390
7391               if (dynobj == NULL)
7392                 htab->root.dynobj = dynobj = abfd;
7393
7394               /* When creating a shared object, we must copy these
7395                  relocs into the output file.  We create a reloc
7396                  section in dynobj and make room for the reloc.  */
7397               if (sreloc == NULL)
7398                 {
7399                   const char *name;
7400
7401                   name = bfd_elf_string_from_elf_section
7402                     (abfd, elf_elfheader (abfd)->e_shstrndx,
7403                      elf_section_data (sec)->rela.hdr->sh_name);
7404                   if (name == NULL)
7405                     return FALSE;
7406
7407                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
7408                               && strcmp (bfd_get_section_name (abfd, sec),
7409                                          name + 5) == 0);
7410
7411                   sreloc = bfd_get_section_by_name (dynobj, name);
7412                   if (sreloc == NULL)
7413                     {
7414                       flagword flags;
7415
7416                       sreloc = bfd_make_section (dynobj, name);
7417                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
7418                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7419                       if ((sec->flags & SEC_ALLOC) != 0)
7420                         flags |= SEC_ALLOC | SEC_LOAD;
7421                       if (sreloc == NULL
7422                           || !bfd_set_section_flags (dynobj, sreloc, flags)
7423                           || !bfd_set_section_alignment (dynobj, sreloc, 2))
7424                         return FALSE;
7425
7426                       elf_section_type (sreloc) = SHT_RELA;
7427                     }
7428                   elf_section_data (sec)->sreloc = sreloc;
7429                 }
7430
7431               /* If this is a global symbol, we count the number of
7432                  relocations we need for this symbol.  */
7433               if (h != NULL)
7434                 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
7435               else
7436                 {
7437                   asection *s;
7438                   void *vpp;
7439
7440                   Elf_Internal_Sym *isym;
7441                   isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7442                   if (isym == NULL)
7443                     return FALSE;
7444
7445                   /* Track dynamic relocs needed for local syms too.  */
7446                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7447                   if (s == NULL)
7448                     return FALSE;
7449
7450                   vpp = &elf_section_data (s)->local_dynrel;
7451                   head = (struct elf_dyn_relocs **) vpp;
7452                 }
7453
7454               p = *head;
7455               if (p == NULL || p->sec != sec)
7456                 {
7457                   bfd_size_type amt = sizeof (*p);
7458                   p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7459                   if (p == NULL)
7460                     return FALSE;
7461                   p->next = *head;
7462                   *head = p;
7463                   p->sec = sec;
7464                   p->count = 0;
7465                   p->pc_count = 0;
7466                 }
7467
7468               p->count += 1;
7469
7470               /* Since eh_frame is readonly, R_NDS32_32_RELA
7471                  reloc for eh_frame will cause shared library has
7472                  TEXTREL entry in the dynamic section. This lead glibc
7473                  testsuites to failure (bug-13092) and cause kernel fail
7474                  (bug-11819).  I think the best solution is to replace
7475                  absolute reloc with pc relative reloc in the eh_frame.
7476                  To do that, we need to support the following issues:
7477
7478                  === For GCC ===
7479                  * gcc/config/nds32/nds32.h: Define
7480                  ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7481                  and DW_EH_PE_sdata4 into DWARF exception header when
7482                  option have '-fpic'.
7483
7484                  === For binutils ===
7485                  * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7486                  * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7487                  may break our nds DIFF mechanism, therefore, we
7488                  must disable all linker relaxations to ensure
7489                  correctness.
7490                  * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7491                  R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7492                  do the necessary modification.
7493
7494                  Unfortunately, it still have some problems for nds32
7495                  to support pc relative reloc in the eh_frame. So I use
7496                  another solution to fix this issue.
7497
7498                  However, I find that ld always emit TEXTREL marker for
7499                  R_NDS32_NONE relocs in rel.dyn. These none relocs are
7500                  correspond to R_NDS32_32_RELA for .eh_frame section.
7501                  It means that we always reserve redundant entries of rel.dyn
7502                  for these relocs which actually do nothing in dynamic linker.
7503
7504                  Therefore, we regard these relocs as pc relative relocs
7505                  here and increase the pc_count.  */
7506               if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7507                   || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7508                   || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7509                   || (r_type == R_NDS32_32_RELA
7510                       && strcmp (sec->name, ".eh_frame") == 0))
7511                 p->pc_count += 1;
7512             }
7513           break;
7514
7515           /* This relocation describes the C++ object vtable hierarchy.
7516              Reconstruct it for later use during GC.  */
7517         case R_NDS32_RELA_GNU_VTINHERIT:
7518         case R_NDS32_GNU_VTINHERIT:
7519           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7520             return FALSE;
7521           break;
7522
7523           /* This relocation describes which C++ vtable entries are actually
7524              used.  Record for later use during GC.  */
7525         case R_NDS32_GNU_VTENTRY:
7526           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7527             return FALSE;
7528           break;
7529         case R_NDS32_RELA_GNU_VTENTRY:
7530           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7531             return FALSE;
7532           break;
7533         }
7534     }
7535
7536   return TRUE;
7537 }
7538
7539 /* Write VAL in uleb128 format to P, returning a pointer to the
7540    following byte.
7541    This code is copied from elf-attr.c.  */
7542
7543 static bfd_byte *
7544 write_uleb128 (bfd_byte *p, unsigned int val)
7545 {
7546   bfd_byte c;
7547   do
7548     {
7549       c = val & 0x7f;
7550       val >>= 7;
7551       if (val)
7552         c |= 0x80;
7553       *(p++) = c;
7554     }
7555   while (val);
7556   return p;
7557 }
7558
7559 static bfd_signed_vma
7560 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7561                   Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7562 {
7563   bfd_signed_vma foff;
7564   bfd_vma symval, addend;
7565   asection *sym_sec;
7566
7567   /* Get the value of the symbol referred to by the reloc.  */
7568   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7569     {
7570       Elf_Internal_Sym *isym;
7571
7572       /* A local symbol.  */
7573       isym = isymbuf + ELF32_R_SYM (irel->r_info);
7574
7575       if (isym->st_shndx == SHN_UNDEF)
7576         sym_sec = bfd_und_section_ptr;
7577       else if (isym->st_shndx == SHN_ABS)
7578         sym_sec = bfd_abs_section_ptr;
7579       else if (isym->st_shndx == SHN_COMMON)
7580         sym_sec = bfd_com_section_ptr;
7581       else
7582         sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7583       symval = isym->st_value + sym_sec->output_section->vma
7584                + sym_sec->output_offset;
7585     }
7586   else
7587     {
7588       unsigned long indx;
7589       struct elf_link_hash_entry *h;
7590
7591       /* An external symbol.  */
7592       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7593       h = elf_sym_hashes (abfd)[indx];
7594       BFD_ASSERT (h != NULL);
7595
7596       if (h->root.type != bfd_link_hash_defined
7597           && h->root.type != bfd_link_hash_defweak)
7598         /* This appears to be a reference to an undefined
7599            symbol.  Just ignore it--it will be caught by the
7600            regular reloc processing.  */
7601         return 0;
7602
7603       if (h->root.u.def.section->flags & SEC_MERGE)
7604         {
7605           sym_sec = h->root.u.def.section;
7606           symval = _bfd_merged_section_offset (abfd, &sym_sec,
7607                                                elf_section_data (sym_sec)->sec_info,
7608                                                h->root.u.def.value);
7609           symval = symval + sym_sec->output_section->vma
7610                    + sym_sec->output_offset;
7611         }
7612       else
7613         symval = (h->root.u.def.value
7614                   + h->root.u.def.section->output_section->vma
7615                   + h->root.u.def.section->output_offset);
7616     }
7617
7618   addend = irel->r_addend;
7619
7620   foff = (symval + addend
7621           - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7622   return foff;
7623 }
7624 \f
7625
7626 /* Convert a 32-bit instruction to 16-bit one.
7627    INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7628    instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
7629    type of INSN16.  Return 1 if successful.  */
7630
7631 static int
7632 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7633                              int *pinsn_type)
7634 {
7635   uint16_t insn16 = 0;
7636   int insn_type = 0;
7637   unsigned long mach = bfd_get_mach (abfd);
7638
7639   if (N32_SH5 (insn) != 0)
7640     return 0;
7641
7642   switch (N32_SUB5 (insn))
7643     {
7644     case N32_ALU1_ADD_SLLI:
7645     case N32_ALU1_ADD_SRLI:
7646       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7647         {
7648           insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7649                                 N32_RB5 (insn));
7650           insn_type = NDS32_INSN_ADD333;
7651         }
7652       else if (N32_IS_RT4 (insn))
7653         {
7654           if (N32_RT5 (insn) == N32_RA5 (insn))
7655             insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7656           else if (N32_RT5 (insn) == N32_RB5 (insn))
7657             insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7658           insn_type = NDS32_INSN_ADD45;
7659         }
7660       break;
7661
7662     case N32_ALU1_SUB_SLLI:
7663     case N32_ALU1_SUB_SRLI:
7664       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7665         {
7666           insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7667                                 N32_RB5 (insn));
7668           insn_type = NDS32_INSN_SUB333;
7669         }
7670       else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7671         {
7672           insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7673           insn_type = NDS32_INSN_SUB45;
7674         }
7675       break;
7676
7677     case N32_ALU1_AND_SLLI:
7678     case N32_ALU1_AND_SRLI:
7679       /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
7680       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7681           && N32_IS_RB3 (insn))
7682         {
7683           if (N32_RT5 (insn) == N32_RA5 (insn))
7684             insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7685           else if (N32_RT5 (insn) == N32_RB5 (insn))
7686             insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7687           if (insn16)
7688             insn_type = NDS32_INSN_AND33;
7689         }
7690       break;
7691
7692     case N32_ALU1_XOR_SLLI:
7693     case N32_ALU1_XOR_SRLI:
7694       /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
7695       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7696           && N32_IS_RB3 (insn))
7697         {
7698           if (N32_RT5 (insn) == N32_RA5 (insn))
7699             insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7700           else if (N32_RT5 (insn) == N32_RB5 (insn))
7701             insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7702           if (insn16)
7703             insn_type = NDS32_INSN_XOR33;
7704         }
7705       break;
7706
7707     case N32_ALU1_OR_SLLI:
7708     case N32_ALU1_OR_SRLI:
7709       /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
7710       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7711           && N32_IS_RB3 (insn))
7712         {
7713           if (N32_RT5 (insn) == N32_RA5 (insn))
7714             insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7715           else if (N32_RT5 (insn) == N32_RB5 (insn))
7716             insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7717           if (insn16)
7718             insn_type = NDS32_INSN_OR33;
7719         }
7720       break;
7721     case N32_ALU1_NOR:
7722       /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
7723       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7724           && N32_RA5 (insn) == N32_RB5 (insn))
7725         {
7726           insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7727           insn_type = NDS32_INSN_NOT33;
7728         }
7729       break;
7730     case N32_ALU1_SRAI:
7731       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7732         {
7733           insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7734           insn_type = NDS32_INSN_SRAI45;
7735         }
7736       break;
7737
7738     case N32_ALU1_SRLI:
7739       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7740         {
7741           insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7742           insn_type = NDS32_INSN_SRLI45;
7743         }
7744       break;
7745
7746     case N32_ALU1_SLLI:
7747       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7748         {
7749           insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7750                                 N32_UB5 (insn));
7751           insn_type = NDS32_INSN_SLLI333;
7752         }
7753       break;
7754
7755     case N32_ALU1_ZEH:
7756       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7757         {
7758           insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7759           insn_type = NDS32_INSN_ZEH33;
7760         }
7761       break;
7762
7763     case N32_ALU1_SEB:
7764       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7765         {
7766           insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7767           insn_type = NDS32_INSN_SEB33;
7768         }
7769       break;
7770
7771     case N32_ALU1_SEH:
7772       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7773         {
7774           insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7775           insn_type = NDS32_INSN_SEH33;
7776         }
7777       break;
7778
7779     case N32_ALU1_SLT:
7780       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7781         {
7782           /* Implicit r15.  */
7783           insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7784           insn_type = NDS32_INSN_SLT45;
7785         }
7786       break;
7787
7788     case N32_ALU1_SLTS:
7789       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7790         {
7791           /* Implicit r15.  */
7792           insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7793           insn_type = NDS32_INSN_SLTS45;
7794         }
7795       break;
7796     }
7797
7798   if ((insn16 & 0x8000) == 0)
7799     return 0;
7800
7801   if (pinsn16)
7802     *pinsn16 = insn16;
7803   if (pinsn_type)
7804     *pinsn_type = insn_type;
7805   return 1;
7806 }
7807
7808 static int
7809 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7810                              int *pinsn_type)
7811 {
7812   uint16_t insn16 = 0;
7813   int insn_type;
7814   unsigned long mach = bfd_get_mach (abfd);
7815
7816   /* TODO: bset, bclr, btgl, btst.  */
7817   if (__GF (insn, 6, 4) != 0)
7818     return 0;
7819
7820   switch (N32_IMMU (insn, 6))
7821     {
7822     case N32_ALU2_MUL:
7823       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7824           && N32_IS_RB3 (insn))
7825         {
7826           if (N32_RT5 (insn) == N32_RA5 (insn))
7827             insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7828           else if (N32_RT5 (insn) == N32_RB5 (insn))
7829             insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7830           if (insn16)
7831             insn_type = NDS32_INSN_MUL33;
7832         }
7833     }
7834
7835   if ((insn16 & 0x8000) == 0)
7836     return 0;
7837
7838   if (pinsn16)
7839     *pinsn16 = insn16;
7840   if (pinsn_type)
7841     *pinsn_type = insn_type;
7842   return 1;
7843 }
7844
7845 int
7846 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7847                         int *pinsn_type)
7848 {
7849   int op6;
7850   uint16_t insn16 = 0;
7851   int insn_type = 0;
7852   unsigned long mach = bfd_get_mach (abfd);
7853
7854   /* Decode 32-bit instruction.  */
7855   if (insn & 0x80000000)
7856     {
7857       /* Not 32-bit insn.  */
7858       return 0;
7859     }
7860
7861   op6 = N32_OP6 (insn);
7862
7863   /* Convert it to 16-bit instruction.  */
7864   switch (op6)
7865     {
7866     case N32_OP6_MOVI:
7867       if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7868         {
7869           insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7870           insn_type = NDS32_INSN_MOVI55;
7871         }
7872       else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7873                && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7874         {
7875           insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7876                                N32_IMM20S (insn) - 16);
7877           insn_type = NDS32_INSN_MOVPI45;
7878         }
7879       break;
7880
7881     case N32_OP6_ADDI:
7882       if (N32_IMM15S (insn) == 0)
7883         {
7884           /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7885              because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7886           if (mach <= MACH_V2
7887               || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7888             {
7889               insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7890               insn_type = NDS32_INSN_MOV55;
7891             }
7892         }
7893       else if (N32_IMM15S (insn) > 0)
7894         {
7895           if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7896             {
7897               insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7898                                     N32_IMM15S (insn));
7899               insn_type = NDS32_INSN_ADDI333;
7900             }
7901           else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7902                    && N32_IMM15S (insn) < 32)
7903             {
7904               insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7905               insn_type = NDS32_INSN_ADDI45;
7906             }
7907           else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7908                    && N32_RT5 (insn) == N32_RA5 (insn)
7909                    && N32_IMM15S (insn) < 512)
7910             {
7911               insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7912               insn_type = NDS32_INSN_ADDI10_SP;
7913             }
7914           else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7915                    && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7916                    && (N32_IMM15S (insn) % 4 == 0))
7917             {
7918               insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7919                                    N32_IMM15S (insn) >> 2);
7920               insn_type = NDS32_INSN_ADDRI36_SP;
7921             }
7922         }
7923       else
7924         {
7925           /* Less than 0.  */
7926           if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7927             {
7928               insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7929                                     0 - N32_IMM15S (insn));
7930               insn_type = NDS32_INSN_SUBI333;
7931             }
7932           else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7933                    && N32_IMM15S (insn) > -32)
7934             {
7935               insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7936                                    0 - N32_IMM15S (insn));
7937               insn_type = NDS32_INSN_SUBI45;
7938             }
7939           else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7940                    && N32_RT5 (insn) == N32_RA5 (insn)
7941                    && N32_IMM15S (insn) >= -512)
7942             {
7943               insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7944               insn_type = NDS32_INSN_ADDI10_SP;
7945             }
7946         }
7947       break;
7948
7949     case N32_OP6_ORI:
7950       if (N32_IMM15S (insn) == 0)
7951         {
7952           /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7953              because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7954           if (mach <= MACH_V2
7955               || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7956             {
7957               insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7958               insn_type = NDS32_INSN_MOV55;
7959             }
7960         }
7961       break;
7962
7963     case N32_OP6_SUBRI:
7964       if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7965           && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7966         {
7967           insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7968           insn_type = NDS32_INSN_NEG33;
7969         }
7970       break;
7971
7972     case N32_OP6_ANDI:
7973       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7974         {
7975           if (N32_IMM15U (insn) == 1)
7976             {
7977               insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7978               insn_type = NDS32_INSN_XLSB33;
7979             }
7980           else if (N32_IMM15U (insn) == 0x7ff)
7981             {
7982               insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7983               insn_type = NDS32_INSN_X11B33;
7984             }
7985           else if (N32_IMM15U (insn) == 0xff)
7986             {
7987               insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7988               insn_type = NDS32_INSN_ZEB33;
7989             }
7990           else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7991                    && N32_IMM15U (insn) < 256)
7992             {
7993               int imm15u = N32_IMM15U (insn);
7994
7995               if (__builtin_popcount (imm15u) == 1)
7996                 {
7997                   /* BMSKI33 */
7998                   int imm3u = __builtin_ctz (imm15u);
7999
8000                   insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
8001                   insn_type = NDS32_INSN_BMSKI33;
8002                 }
8003               else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
8004                 {
8005                   /* FEXTI33 */
8006                   int imm3u = __builtin_ctz (imm15u + 1) - 1;
8007
8008                   insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
8009                   insn_type = NDS32_INSN_FEXTI33;
8010                 }
8011             }
8012         }
8013       break;
8014
8015     case N32_OP6_SLTI:
8016       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8017           && IS_WITHIN_U (N32_IMM15S (insn), 5))
8018         {
8019           insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8020           insn_type = NDS32_INSN_SLTI45;
8021         }
8022       break;
8023
8024     case N32_OP6_SLTSI:
8025       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8026           && IS_WITHIN_U (N32_IMM15S (insn), 5))
8027         {
8028           insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8029           insn_type = NDS32_INSN_SLTSI45;
8030         }
8031       break;
8032
8033     case N32_OP6_LWI:
8034       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8035         {
8036           insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8037           insn_type = NDS32_INSN_LWI450;
8038         }
8039       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8040                && IS_WITHIN_U (N32_IMM15S (insn), 3))
8041         {
8042           insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8043                                 N32_IMM15S (insn));
8044           insn_type = NDS32_INSN_LWI333;
8045         }
8046       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8047                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8048         {
8049           insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8050           insn_type = NDS32_INSN_LWI37;
8051         }
8052       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8053                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8054         {
8055           insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8056           insn_type = NDS32_INSN_LWI37_SP;
8057         }
8058       else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8059                && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8060         {
8061           insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8062                                N32_IMM15S (insn) + 32);
8063           insn_type = NDS32_INSN_LWI45_FE;
8064         }
8065       break;
8066
8067     case N32_OP6_SWI:
8068       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8069         {
8070           insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8071           insn_type = NDS32_INSN_SWI450;
8072         }
8073       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8074                && IS_WITHIN_U (N32_IMM15S (insn), 3))
8075         {
8076           insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8077                                 N32_IMM15S (insn));
8078           insn_type = NDS32_INSN_SWI333;
8079         }
8080       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8081                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8082         {
8083           insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8084           insn_type = NDS32_INSN_SWI37;
8085         }
8086       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8087                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8088         {
8089           insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8090           insn_type = NDS32_INSN_SWI37_SP;
8091         }
8092       break;
8093
8094     case N32_OP6_LWI_BI:
8095       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8096           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8097         {
8098           insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8099                                 N32_IMM15S (insn));
8100           insn_type = NDS32_INSN_LWI333_BI;
8101         }
8102       break;
8103
8104     case N32_OP6_SWI_BI:
8105       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8106           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8107         {
8108           insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8109                                 N32_IMM15S (insn));
8110           insn_type = NDS32_INSN_SWI333_BI;
8111         }
8112       break;
8113
8114     case N32_OP6_LHI:
8115       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8116           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8117         {
8118           insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8119                                 N32_IMM15S (insn));
8120           insn_type = NDS32_INSN_LHI333;
8121         }
8122       break;
8123
8124     case N32_OP6_SHI:
8125       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8126           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8127         {
8128           insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8129                                 N32_IMM15S (insn));
8130           insn_type = NDS32_INSN_SHI333;
8131         }
8132       break;
8133
8134     case N32_OP6_LBI:
8135       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8136           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8137         {
8138           insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8139                                 N32_IMM15S (insn));
8140           insn_type = NDS32_INSN_LBI333;
8141         }
8142       break;
8143
8144     case N32_OP6_SBI:
8145       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8146           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8147         {
8148           insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8149                                 N32_IMM15S (insn));
8150           insn_type = NDS32_INSN_SBI333;
8151         }
8152       break;
8153
8154     case N32_OP6_ALU1:
8155       return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8156
8157     case N32_OP6_ALU2:
8158       return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8159
8160     case N32_OP6_BR1:
8161       if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8162         goto done;
8163
8164       if ((insn & N32_BIT (14)) == 0)
8165         {
8166           /* N32_BR1_BEQ */
8167           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8168               && N32_RT5 (insn) != REG_R5)
8169             insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8170           else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8171                    && N32_RA5 (insn) != REG_R5)
8172             insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8173           insn_type = NDS32_INSN_BEQS38;
8174           break;
8175         }
8176       else
8177         {
8178           /* N32_BR1_BNE */
8179           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8180               && N32_RT5 (insn) != REG_R5)
8181             insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8182           else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8183                    && N32_RA5 (insn) != REG_R5)
8184             insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8185           insn_type = NDS32_INSN_BNES38;
8186           break;
8187         }
8188       break;
8189
8190     case N32_OP6_BR2:
8191       switch (N32_BR2_SUB (insn))
8192         {
8193         case N32_BR2_BEQZ:
8194           if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8195             {
8196               insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8197               insn_type = NDS32_INSN_BEQZ38;
8198             }
8199           else if (N32_RT5 (insn) == REG_R15
8200                    && IS_WITHIN_S (N32_IMM16S (insn), 8))
8201             {
8202               insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8203               insn_type = NDS32_INSN_BEQZS8;
8204             }
8205           break;
8206
8207         case N32_BR2_BNEZ:
8208           if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8209             {
8210               insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8211               insn_type = NDS32_INSN_BNEZ38;
8212             }
8213           else if (N32_RT5 (insn) == REG_R15
8214                    && IS_WITHIN_S (N32_IMM16S (insn), 8))
8215             {
8216               insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8217               insn_type = NDS32_INSN_BNEZS8;
8218             }
8219           break;
8220
8221         case N32_BR2_SOP0:
8222           if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8223             {
8224               insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8225               insn_type = NDS32_INSN_IFCALL9;
8226             }
8227           break;
8228         }
8229       break;
8230
8231     case N32_OP6_JI:
8232       if ((insn & N32_BIT (24)) == 0)
8233         {
8234           /* N32_JI_J */
8235           if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8236             {
8237               insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8238               insn_type = NDS32_INSN_J8;
8239             }
8240         }
8241       break;
8242
8243     case N32_OP6_JREG:
8244       if (__GF (insn, 8, 2) != 0)
8245         goto done;
8246
8247       switch (N32_IMMU (insn, 5))
8248         {
8249         case N32_JREG_JR:
8250           if (N32_JREG_HINT (insn) == 0)
8251             {
8252               /* jr */
8253               insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8254               insn_type = NDS32_INSN_JR5;
8255             }
8256           else if (N32_JREG_HINT (insn) == 1)
8257             {
8258               /* ret */
8259               insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8260               insn_type = NDS32_INSN_RET5;
8261             }
8262           else if (N32_JREG_HINT (insn) == 3)
8263             {
8264               /* ifret = mov55 $sp, $sp */
8265               insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8266               insn_type = NDS32_INSN_IFRET;
8267             }
8268           break;
8269
8270         case N32_JREG_JRAL:
8271           /* It's convertible when return rt5 is $lp and address
8272              translation is kept.  */
8273           if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8274             {
8275               insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8276               insn_type = NDS32_INSN_JRAL5;
8277             }
8278           break;
8279         }
8280       break;
8281
8282     case N32_OP6_MISC:
8283       if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8284         {
8285           /* For v3, swid above 31 are used for ex9.it.  */
8286           insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8287           insn_type = NDS32_INSN_BREAK16;
8288         }
8289       break;
8290
8291     default:
8292       /* This instruction has no 16-bit variant.  */
8293       goto done;
8294     }
8295
8296 done:
8297   /* Bit-15 of insn16 should be set for a valid instruction.  */
8298   if ((insn16 & 0x8000) == 0)
8299     return 0;
8300
8301   if (pinsn16)
8302     *pinsn16 = insn16;
8303   if (pinsn_type)
8304     *pinsn_type = insn_type;
8305   return 1;
8306 }
8307
8308 static int
8309 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8310                           Elf_Internal_Rela *reloc)
8311 {
8312   uint16_t insn16 = 0;
8313
8314   if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8315       || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8316     return 0;
8317
8318   if (!N32_IS_RT3 (insn))
8319     return 0;
8320
8321   switch (N32_OP6 (insn))
8322     {
8323     case N32_OP6_LWI:
8324       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8325         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8326       break;
8327     case N32_OP6_SWI:
8328       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8329         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8330       break;
8331     case N32_OP6_HWGP:
8332       if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8333         break;
8334
8335       if (__GF (insn, 17, 3) == 6)
8336         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8337       else if (__GF (insn, 17, 3) == 7)
8338         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8339       break;
8340     }
8341
8342   if ((insn16 & 0x8000) == 0)
8343     return 0;
8344
8345   *pinsn16 = insn16;
8346   return 1;
8347 }
8348
8349 /* Convert a 16-bit instruction to 32-bit one.
8350    INSN16 it the input and PINSN it the point to output.
8351    Return non-zero on successful.  Otherwise 0 is returned.  */
8352
8353 int
8354 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8355 {
8356   uint32_t insn = 0xffffffff;
8357   unsigned long mach = bfd_get_mach (abfd);
8358
8359   /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
8360
8361   switch (__GF (insn16, 9, 6))
8362     {
8363     case 0x4:                   /* add45 */
8364       insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8365                        N16_RA5 (insn16));
8366       goto done;
8367     case 0x5:                   /* sub45 */
8368       insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8369                        N16_RA5 (insn16));
8370       goto done;
8371     case 0x6:                   /* addi45 */
8372       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8373                         N16_IMM5U (insn16));
8374       goto done;
8375     case 0x7:                   /* subi45 */
8376       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8377                         -N16_IMM5U (insn16));
8378       goto done;
8379     case 0x8:                   /* srai45 */
8380       insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8381                        N16_IMM5U (insn16));
8382       goto done;
8383     case 0x9:                   /* srli45 */
8384       insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8385                        N16_IMM5U (insn16));
8386       goto done;
8387     case 0xa:                   /* slli333 */
8388       insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8389                        N16_IMM3U (insn16));
8390       goto done;
8391     case 0xc:                   /* add333 */
8392       insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8393                        N16_RB3 (insn16));
8394       goto done;
8395     case 0xd:                   /* sub333 */
8396       insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8397                        N16_RB3 (insn16));
8398       goto done;
8399     case 0xe:                   /* addi333 */
8400       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8401                         N16_IMM3U (insn16));
8402       goto done;
8403     case 0xf:                   /* subi333 */
8404       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8405                         -N16_IMM3U (insn16));
8406       goto done;
8407     case 0x10:                  /* lwi333 */
8408       insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8409                         N16_IMM3U (insn16));
8410       goto done;
8411     case 0x12:                  /* lhi333 */
8412       insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8413                         N16_IMM3U (insn16));
8414       goto done;
8415     case 0x13:                  /* lbi333 */
8416       insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8417                         N16_IMM3U (insn16));
8418       goto done;
8419     case 0x11:                  /* lwi333.bi */
8420       insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8421                         N16_IMM3U (insn16));
8422       goto done;
8423     case 0x14:                  /* swi333 */
8424       insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8425                         N16_IMM3U (insn16));
8426       goto done;
8427     case 0x16:                  /* shi333 */
8428       insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8429                         N16_IMM3U (insn16));
8430       goto done;
8431     case 0x17:                  /* sbi333 */
8432       insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8433                         N16_IMM3U (insn16));
8434       goto done;
8435     case 0x15:                  /* swi333.bi */
8436       insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8437                         N16_IMM3U (insn16));
8438       goto done;
8439     case 0x18:                  /* addri36.sp */
8440       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8441                         N16_IMM6U (insn16) << 2);
8442       goto done;
8443     case 0x19:                  /* lwi45.fe */
8444       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8445                         (N16_IMM5U (insn16) - 32));
8446       goto done;
8447     case 0x1a:                  /* lwi450 */
8448       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8449       goto done;
8450     case 0x1b:                  /* swi450 */
8451       insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8452       goto done;
8453
8454       /* These are r15 implied instructions.  */
8455     case 0x30:                  /* slts45 */
8456       insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8457       goto done;
8458     case 0x31:                  /* slt45 */
8459       insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8460       goto done;
8461     case 0x32:                  /* sltsi45 */
8462       insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8463       goto done;
8464     case 0x33:                  /* slti45 */
8465       insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8466       goto done;
8467     case 0x34:                  /* beqzs8, bnezs8 */
8468       if (insn16 & N32_BIT (8))
8469         insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8470       else
8471         insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8472       goto done;
8473
8474     case 0x35:                  /* break16, ex9.it */
8475       /* Only consider range of v3 break16.  */
8476       insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8477       goto done;
8478
8479     case 0x3c:                  /* ifcall9 */
8480       insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8481       goto done;
8482     case 0x3d:                  /* movpi45 */
8483       insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8484       goto done;
8485
8486     case 0x3f:                  /* MISC33 */
8487       switch (insn16 & 0x7)
8488         {
8489         case 2:                 /* neg33 */
8490           insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8491           break;
8492         case 3:                 /* not33 */
8493           insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8494                            N16_RA3 (insn16));
8495           break;
8496         case 4:                 /* mul33 */
8497           insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8498                            N16_RA3 (insn16));
8499           break;
8500         case 5:                 /* xor33 */
8501           insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8502                            N16_RA3 (insn16));
8503           break;
8504         case 6:                 /* and33 */
8505           insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8506                            N16_RA3 (insn16));
8507           break;
8508         case 7:                 /* or33 */
8509           insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8510                            N16_RA3 (insn16));
8511           break;
8512         }
8513       goto done;
8514
8515     case 0xb:
8516       switch (insn16 & 0x7)
8517         {
8518         case 0:                 /* zeb33 */
8519           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8520           break;
8521         case 1:                 /* zeh33 */
8522           insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8523           break;
8524         case 2:                 /* seb33 */
8525           insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8526           break;
8527         case 3:                 /* seh33 */
8528           insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8529           break;
8530         case 4:                 /* xlsb33 */
8531           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8532           break;
8533         case 5:                 /* x11b33 */
8534           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8535           break;
8536         case 6:                 /* bmski33 */
8537           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8538                             1 << __GF (insn16, 3, 3));
8539           break;
8540         case 7:                 /* fexti33 */
8541           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8542                             (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8543           break;
8544         }
8545       goto done;
8546     }
8547
8548   switch (__GF (insn16, 10, 5))
8549     {
8550     case 0x0:                   /* mov55 or ifret16 */
8551       if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8552           && N16_RT5 (insn16) == N16_RA5 (insn16))
8553         insn = N32_JREG (JR, 0, 0, 0, 3);
8554       else
8555         insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8556       goto done;
8557     case 0x1:                   /* movi55 */
8558       insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8559       goto done;
8560     case 0x1b:                  /* addi10s (V2) */
8561       insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8562       goto done;
8563     }
8564
8565   switch (__GF (insn16, 11, 4))
8566     {
8567     case 0x7:                   /* lwi37.fp/swi37.fp */
8568       if (insn16 & N32_BIT (7)) /* swi37.fp */
8569         insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8570       else                      /* lwi37.fp */
8571         insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8572       goto done;
8573     case 0x8:                   /* beqz38 */
8574       insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8575       goto done;
8576     case 0x9:                   /* bnez38 */
8577       insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8578       goto done;
8579     case 0xa:                   /* beqs38/j8, implied r5 */
8580       if (N16_RT38 (insn16) == 5)
8581         insn = N32_JI (J, N16_IMM8S (insn16));
8582       else
8583         insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8584       goto done;
8585     case 0xb:                   /* bnes38 and others.  */
8586       if (N16_RT38 (insn16) == 5)
8587         {
8588           switch (__GF (insn16, 5, 3))
8589             {
8590             case 0:             /* jr5 */
8591               insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8592               break;
8593             case 4:             /* ret5 */
8594               insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8595               break;
8596             case 1:             /* jral5 */
8597               insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8598               break;
8599             case 2:             /* ex9.it imm5 */
8600               /* ex9.it had no 32-bit variantl.  */
8601               break;
8602             case 5:             /* add5.pc */
8603               /* add5.pc had no 32-bit variantl.  */
8604               break;
8605             }
8606         }
8607       else                      /* bnes38 */
8608         insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8609       goto done;
8610     case 0xe:                   /* lwi37/swi37 */
8611       if (insn16 & (1 << 7))    /* swi37.sp */
8612         insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8613       else                      /* lwi37.sp */
8614         insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8615       goto done;
8616     }
8617
8618 done:
8619   if (insn & 0x80000000)
8620     return 0;
8621
8622   if (pinsn)
8623     *pinsn = insn;
8624   return 1;
8625 }
8626 \f
8627
8628 static bfd_boolean
8629 is_sda_access_insn (unsigned long insn)
8630 {
8631   switch (N32_OP6 (insn))
8632     {
8633     case N32_OP6_LWI:
8634     case N32_OP6_LHI:
8635     case N32_OP6_LHSI:
8636     case N32_OP6_LBI:
8637     case N32_OP6_LBSI:
8638     case N32_OP6_SWI:
8639     case N32_OP6_SHI:
8640     case N32_OP6_SBI:
8641     case N32_OP6_LWC:
8642     case N32_OP6_LDC:
8643     case N32_OP6_SWC:
8644     case N32_OP6_SDC:
8645       return TRUE;
8646     default:
8647       ;
8648     }
8649   return FALSE;
8650 }
8651
8652 static unsigned long
8653 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8654 {
8655   uint32_t oinsn = 0;
8656
8657   switch (type)
8658     {
8659     case R_NDS32_GOT_LO12:
8660     case R_NDS32_GOTOFF_LO12:
8661     case R_NDS32_PLTREL_LO12:
8662     case R_NDS32_PLT_GOTREL_LO12:
8663     case R_NDS32_LO12S0_RELA:
8664       switch (N32_OP6 (insn))
8665         {
8666         case N32_OP6_LBI:
8667           /* lbi.gp */
8668           oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8669           break;
8670         case N32_OP6_LBSI:
8671           /* lbsi.gp */
8672           oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8673           break;
8674         case N32_OP6_SBI:
8675           /* sbi.gp */
8676           oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8677           break;
8678         case N32_OP6_ORI:
8679           /* addi.gp */
8680           oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8681           break;
8682         }
8683       break;
8684
8685     case R_NDS32_LO12S1_RELA:
8686       switch (N32_OP6 (insn))
8687         {
8688         case N32_OP6_LHI:
8689           /* lhi.gp */
8690           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8691           break;
8692         case N32_OP6_LHSI:
8693           /* lhsi.gp */
8694           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8695           break;
8696         case N32_OP6_SHI:
8697           /* shi.gp */
8698           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8699           break;
8700         }
8701       break;
8702
8703     case R_NDS32_LO12S2_RELA:
8704       switch (N32_OP6 (insn))
8705         {
8706         case N32_OP6_LWI:
8707           /* lwi.gp */
8708           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8709           break;
8710         case N32_OP6_SWI:
8711           /* swi.gp */
8712           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8713           break;
8714         }
8715       break;
8716
8717     case R_NDS32_LO12S2_DP_RELA:
8718     case R_NDS32_LO12S2_SP_RELA:
8719       oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8720       break;
8721     }
8722
8723   if (oinsn)
8724     *pinsn = oinsn;
8725
8726   return oinsn != 0;
8727 }
8728
8729 /* Linker hasn't found the correct merge section for non-section symbol
8730    in relax time, this work is left to the function elf_link_input_bfd().
8731    So for non-section symbol, _bfd_merged_section_offset is also needed
8732    to find the correct symbol address.  */
8733
8734 static bfd_vma
8735 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8736                           asection **psec, Elf_Internal_Rela *rel)
8737 {
8738   asection *sec = *psec;
8739   bfd_vma relocation;
8740
8741   relocation = (sec->output_section->vma
8742                 + sec->output_offset + sym->st_value);
8743   if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8744     {
8745       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8746         rel->r_addend =
8747           _bfd_merged_section_offset (abfd, psec,
8748                                       elf_section_data (sec)->sec_info,
8749                                       sym->st_value + rel->r_addend);
8750       else
8751         rel->r_addend =
8752           _bfd_merged_section_offset (abfd, psec,
8753                                       elf_section_data (sec)->sec_info,
8754                                       sym->st_value) + rel->r_addend;
8755
8756       if (sec != *psec)
8757         {
8758           /* If we have changed the section, and our original section is
8759              marked with SEC_EXCLUDE, it means that the original
8760              SEC_MERGE section has been completely subsumed in some
8761              other SEC_MERGE section.  In this case, we need to leave
8762              some info around for --emit-relocs.  */
8763           if ((sec->flags & SEC_EXCLUDE) != 0)
8764             sec->kept_section = *psec;
8765           sec = *psec;
8766         }
8767       rel->r_addend -= relocation;
8768       rel->r_addend += sec->output_section->vma + sec->output_offset;
8769     }
8770   return relocation;
8771 }
8772
8773 static bfd_vma
8774 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8775                           Elf_Internal_Sym *isymbuf,
8776                           Elf_Internal_Shdr *symtab_hdr)
8777 {
8778   bfd_signed_vma foff;
8779   bfd_vma symval, addend;
8780   Elf_Internal_Rela irel_fn;
8781   Elf_Internal_Sym *isym;
8782   asection *sym_sec;
8783
8784   /* Get the value of the symbol referred to by the reloc.  */
8785   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8786     {
8787       /* A local symbol.  */
8788       isym = isymbuf + ELF32_R_SYM (irel->r_info);
8789
8790       if (isym->st_shndx == SHN_UNDEF)
8791         sym_sec = bfd_und_section_ptr;
8792       else if (isym->st_shndx == SHN_ABS)
8793         sym_sec = bfd_abs_section_ptr;
8794       else if (isym->st_shndx == SHN_COMMON)
8795         sym_sec = bfd_com_section_ptr;
8796       else
8797         sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8798       memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8799       symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8800       addend = irel_fn.r_addend;
8801     }
8802   else
8803     {
8804       unsigned long indx;
8805       struct elf_link_hash_entry *h;
8806
8807       /* An external symbol.  */
8808       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8809       h = elf_sym_hashes (abfd)[indx];
8810       BFD_ASSERT (h != NULL);
8811
8812       while (h->root.type == bfd_link_hash_indirect
8813              || h->root.type == bfd_link_hash_warning)
8814         h = (struct elf_link_hash_entry *) h->root.u.i.link;
8815
8816       if (h->root.type != bfd_link_hash_defined
8817           && h->root.type != bfd_link_hash_defweak)
8818         /* This appears to be a reference to an undefined
8819            symbol.  Just ignore it--it will be caught by the
8820            regular reloc processing.  */
8821         return 0;
8822
8823       if (h->root.u.def.section->flags & SEC_MERGE)
8824         {
8825           sym_sec = h->root.u.def.section;
8826           symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8827                                                (sym_sec)->sec_info, h->root.u.def.value);
8828           symval = symval + sym_sec->output_section->vma
8829                    + sym_sec->output_offset;
8830         }
8831       else
8832         symval = (h->root.u.def.value
8833                   + h->root.u.def.section->output_section->vma
8834                   + h->root.u.def.section->output_offset);
8835       addend = irel->r_addend;
8836     }
8837
8838   foff = symval + addend;
8839
8840   return foff;
8841 }
8842
8843 static int
8844 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8845               asection *sec, Elf_Internal_Rela *rel)
8846 {
8847   bfd_byte *contents;
8848   unsigned short insn16;
8849
8850   if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8851     return FALSE;
8852   contents = elf_section_data (sec)->this_hdr.contents;
8853   insn16 = bfd_getb16 (contents + rel->r_offset);
8854   if (insn16 == NDS32_NOP16)
8855     return TRUE;
8856   return FALSE;
8857 }
8858
8859 /* It checks whether the instruction could be converted to
8860    16-bit form and returns the converted one.
8861
8862    `internal_relocs' is supposed to be sorted.  */
8863
8864 static int
8865 is_convert_32_to_16 (bfd *abfd, asection *sec,
8866                      Elf_Internal_Rela *reloc,
8867                      Elf_Internal_Rela *internal_relocs,
8868                      Elf_Internal_Rela *irelend,
8869                      uint16_t *insn16)
8870 {
8871 #define NORMAL_32_TO_16 (1 << 0)
8872 #define SPECIAL_32_TO_16 (1 << 1)
8873   bfd_byte *contents = NULL;
8874   bfd_signed_vma off;
8875   bfd_vma mem_addr;
8876   uint32_t insn = 0;
8877   Elf_Internal_Rela *pc_rel;
8878   Elf_Internal_Shdr *symtab_hdr;
8879   Elf_Internal_Sym *isymbuf = NULL;
8880   int convert_type;
8881   bfd_vma offset;
8882
8883   if (reloc->r_offset + 4 > sec->size)
8884     return FALSE;
8885
8886   offset = reloc->r_offset;
8887
8888   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
8889     return FALSE;
8890   insn = bfd_getb32 (contents + offset);
8891
8892   if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8893     convert_type = NORMAL_32_TO_16;
8894   else if (special_convert_32_to_16 (insn, insn16, reloc))
8895     convert_type = SPECIAL_32_TO_16;
8896   else
8897     return FALSE;
8898
8899   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8900   if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8901     return FALSE;
8902
8903   /* Find the first relocation of the same relocation-type,
8904      so we iteratie them forward.  */
8905   pc_rel = reloc;
8906   while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8907     pc_rel--;
8908
8909   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8910     {
8911       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8912           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8913           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8914           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8915         {
8916           off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8917           if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8918               || off == 0)
8919             return FALSE;
8920           break;
8921         }
8922       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8923         {
8924           /* movi => movi55  */
8925           mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8926                                                symtab_hdr);
8927           /* mem_addr is unsigned, but the value should
8928              be between [-16, 15].  */
8929           if ((mem_addr + 0x10) >> 5)
8930             return FALSE;
8931           break;
8932         }
8933       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8934                || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8935         {
8936           /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8937              because it can be relaxed to addi for TLS_LE_ADD.  */
8938           return FALSE;
8939         }
8940       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8941                 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8942                && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8943                && convert_type == SPECIAL_32_TO_16)
8944         {
8945           /* fp-as-gp
8946              We've selected a best fp-base for this access, so we can
8947              always resolve it anyway.  Do nothing.  */
8948           break;
8949         }
8950       else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8951                 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8952                || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8953                    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8954                || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8955                    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8956         {
8957           /* Prevent unresolved addi instruction translate
8958              to addi45 or addi333.  */
8959           return FALSE;
8960         }
8961       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8962         {
8963           off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8964           if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8965             return FALSE;
8966           break;
8967         }
8968     }
8969
8970   return TRUE;
8971 }
8972
8973 static void
8974 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8975                     Elf_Internal_Rela *reloc,
8976                     Elf_Internal_Rela *internal_relocs,
8977                     Elf_Internal_Rela *irelend,
8978                     unsigned short insn16)
8979 {
8980   Elf_Internal_Rela *pc_rel;
8981   bfd_vma offset;
8982
8983   offset = reloc->r_offset;
8984   bfd_putb16 (insn16, contents + offset);
8985   /* Find the first relocation of the same relocation-type,
8986      so we iteratie them forward.  */
8987   pc_rel = reloc;
8988   while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8989     pc_rel--;
8990
8991   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8992     {
8993       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8994           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8995           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8996         {
8997           pc_rel->r_info =
8998             ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8999         }
9000       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
9001         pc_rel->r_info =
9002           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
9003       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
9004         pc_rel->r_info =
9005           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
9006       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
9007                || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
9008         pc_rel->r_info =
9009           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
9010       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
9011         pc_rel->r_info =
9012           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
9013     }
9014 }
9015
9016 /* Find a relocation of type specified by `reloc_type'
9017    of the same r_offset with reloc.
9018    If not found, return irelend.
9019
9020    Assuming relocations are sorted by r_offset,
9021    we find the relocation from `reloc' backward untill relocs,
9022    or find it from `reloc' forward untill irelend.  */
9023
9024 static Elf_Internal_Rela *
9025 find_relocs_at_address (Elf_Internal_Rela *reloc,
9026                         Elf_Internal_Rela *relocs,
9027                         Elf_Internal_Rela *irelend,
9028                         enum elf_nds32_reloc_type reloc_type)
9029 {
9030   Elf_Internal_Rela *rel_t;
9031
9032   /* Find backward.  */
9033   for (rel_t = reloc;
9034        rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9035        rel_t--)
9036     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9037       return rel_t;
9038
9039   /* We didn't find it backward.  Try find it forward.  */
9040   for (rel_t = reloc;
9041        rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9042        rel_t++)
9043     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9044       return rel_t;
9045
9046   return irelend;
9047 }
9048
9049 /* Find a relocation of specified type and offset.
9050    `reloc' is just a refence point to find a relocation at specified offset.
9051    If not found, return irelend.
9052
9053    Assuming relocations are sorted by r_offset,
9054    we find the relocation from `reloc' backward untill relocs,
9055    or find it from `reloc' forward untill irelend.  */
9056
9057 static Elf_Internal_Rela *
9058 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9059                              Elf_Internal_Rela *relocs,
9060                              Elf_Internal_Rela *irelend,
9061                              enum elf_nds32_reloc_type reloc_type,
9062                              bfd_vma offset_p)
9063 {
9064   Elf_Internal_Rela *rel_t = NULL;
9065
9066   /* First, we try to find a relocation of offset `offset_p',
9067      and then we use find_relocs_at_address to find specific type.  */
9068
9069   if (reloc->r_offset > offset_p)
9070     {
9071       /* Find backward.  */
9072       for (rel_t = reloc;
9073            rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9074         /* Do nothing.  */;
9075     }
9076   else if (reloc->r_offset < offset_p)
9077     {
9078       /* Find forward.  */
9079       for (rel_t = reloc;
9080            rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9081         /* Do nothing.  */;
9082     }
9083   else
9084     rel_t = reloc;
9085
9086   /* Not found?  */
9087   if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9088     return irelend;
9089
9090   return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9091 }
9092
9093 typedef struct nds32_elf_blank nds32_elf_blank_t;
9094 struct nds32_elf_blank
9095 {
9096   /* Where the blank begins.  */
9097   bfd_vma offset;
9098   /* The size of the blank.  */
9099   bfd_vma size;
9100   /* The accumulative size before this blank.  */
9101   bfd_vma total_size;
9102   nds32_elf_blank_t *next;
9103   nds32_elf_blank_t *prev;
9104 };
9105
9106 static nds32_elf_blank_t *blank_free_list = NULL;
9107
9108 static nds32_elf_blank_t *
9109 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9110 {
9111   nds32_elf_blank_t *blank_t;
9112
9113   if (blank_free_list)
9114     {
9115       blank_t = blank_free_list;
9116       blank_free_list = blank_free_list->next;
9117     }
9118   else
9119     blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9120
9121   if (blank_t == NULL)
9122     return NULL;
9123
9124   blank_t->offset = offset_p;
9125   blank_t->size = size_p;
9126   blank_t->total_size = 0;
9127   blank_t->next = NULL;
9128   blank_t->prev = NULL;
9129
9130   return blank_t;
9131 }
9132
9133 static void
9134 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9135 {
9136   if (blank_free_list)
9137     {
9138       blank_free_list->prev = blank_p;
9139       blank_p->next = blank_free_list;
9140     }
9141   else
9142     blank_p->next = NULL;
9143
9144   blank_p->prev = NULL;
9145   blank_free_list = blank_p;
9146 }
9147
9148 static void
9149 clean_nds32_elf_blank (void)
9150 {
9151   nds32_elf_blank_t *blank_t;
9152
9153   while (blank_free_list)
9154     {
9155       blank_t = blank_free_list;
9156       blank_free_list = blank_free_list->next;
9157       free (blank_t);
9158     }
9159 }
9160
9161 static nds32_elf_blank_t *
9162 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9163 {
9164   nds32_elf_blank_t *blank_t;
9165
9166   if (!blank_p)
9167     return NULL;
9168   blank_t = blank_p;
9169
9170   while (blank_t && addr < blank_t->offset)
9171     blank_t = blank_t->prev;
9172   while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9173     blank_t = blank_t->next;
9174
9175   return blank_t;
9176 }
9177
9178 static bfd_vma
9179 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9180                            int overwrite)
9181 {
9182   nds32_elf_blank_t *blank_t;
9183
9184   blank_t = search_nds32_elf_blank (*blank_p, addr);
9185   if (!blank_t)
9186     return 0;
9187
9188   if (overwrite)
9189     *blank_p = blank_t;
9190
9191   if (addr < blank_t->offset + blank_t->size)
9192     return blank_t->total_size + (addr - blank_t->offset);
9193   else
9194     return blank_t->total_size + blank_t->size;
9195 }
9196
9197 static bfd_boolean
9198 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9199 {
9200   nds32_elf_blank_t *blank_t, *blank_t2;
9201
9202   if (!*blank_p)
9203     {
9204       *blank_p = create_nds32_elf_blank (addr, len);
9205       return *blank_p ? TRUE : FALSE;
9206     }
9207
9208   blank_t = search_nds32_elf_blank (*blank_p, addr);
9209
9210   if (blank_t == NULL)
9211     {
9212       blank_t = create_nds32_elf_blank (addr, len);
9213       if (!blank_t)
9214         return FALSE;
9215       while ((*blank_p)->prev != NULL)
9216         *blank_p = (*blank_p)->prev;
9217       blank_t->next = *blank_p;
9218       (*blank_p)->prev = blank_t;
9219       (*blank_p) = blank_t;
9220       return TRUE;
9221     }
9222
9223   if (addr < blank_t->offset + blank_t->size)
9224     {
9225       /* Extend the origin blank.  */
9226       if (addr + len > blank_t->offset + blank_t->size)
9227         blank_t->size = addr + len - blank_t->offset;
9228     }
9229   else
9230     {
9231       blank_t2 = create_nds32_elf_blank (addr, len);
9232       if (!blank_t2)
9233         return FALSE;
9234       if (blank_t->next)
9235         {
9236           blank_t->next->prev = blank_t2;
9237           blank_t2->next = blank_t->next;
9238         }
9239       blank_t2->prev = blank_t;
9240       blank_t->next = blank_t2;
9241       *blank_p = blank_t2;
9242     }
9243
9244   return TRUE;
9245 }
9246
9247 static bfd_boolean
9248 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9249                                      bfd_vma len)
9250 {
9251   nds32_elf_blank_t *blank_t;
9252
9253   if (!insert_nds32_elf_blank (blank_p, addr, len))
9254     return FALSE;
9255
9256   blank_t = *blank_p;
9257
9258   if (!blank_t->prev)
9259     {
9260       blank_t->total_size = 0;
9261       blank_t = blank_t->next;
9262     }
9263
9264   while (blank_t)
9265     {
9266       blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9267       blank_t = blank_t->next;
9268     }
9269
9270   return TRUE;
9271 }
9272
9273 static void
9274 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9275 {
9276   nds32_elf_blank_t *blank_t;
9277   bfd_vma total_size = 0;
9278
9279   if (!blank_p)
9280     return;
9281
9282   blank_t = blank_p;
9283   while (blank_t->prev)
9284     blank_t = blank_t->prev;
9285   while (blank_t)
9286     {
9287       blank_t->total_size = total_size;
9288       total_size += blank_t->size;
9289       blank_t = blank_t->next;
9290     }
9291 }
9292
9293 static bfd_boolean
9294 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9295                                nds32_elf_blank_t *blank_p)
9296 {
9297   Elf_Internal_Shdr *symtab_hdr;        /* Symbol table header of this bfd.  */
9298   Elf_Internal_Sym *isym = NULL;        /* Symbol table of this bfd.  */
9299   Elf_Internal_Sym *isymend;            /* Symbol entry iterator.  */
9300   unsigned int sec_shndx;               /* The section the be relaxed.  */
9301   bfd_byte *contents;                   /* Contents data of iterating section.  */
9302   Elf_Internal_Rela *internal_relocs;
9303   Elf_Internal_Rela *irel;
9304   Elf_Internal_Rela *irelend;
9305   struct elf_link_hash_entry **sym_hashes;
9306   struct elf_link_hash_entry **end_hashes;
9307   unsigned int symcount;
9308   asection *sect;
9309   nds32_elf_blank_t *blank_t;
9310   nds32_elf_blank_t *blank_t2;
9311   nds32_elf_blank_t *blank_head;
9312
9313   blank_head = blank_t = blank_p;
9314   while (blank_head->prev != NULL)
9315     blank_head = blank_head->prev;
9316   while (blank_t->next != NULL)
9317     blank_t = blank_t->next;
9318
9319   if (blank_t->offset + blank_t->size <= sec->size)
9320     {
9321       blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9322       blank_t->next->prev = blank_t;
9323     }
9324   if (blank_head->offset > 0)
9325     {
9326       blank_head->prev = create_nds32_elf_blank (0, 0);
9327       blank_head->prev->next = blank_head;
9328       blank_head = blank_head->prev;
9329     }
9330
9331   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9332
9333   /* The deletion must stop at the next ALIGN reloc for an alignment
9334      power larger than the number of bytes we are deleting.  */
9335
9336   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9337   if (!nds32_get_local_syms (abfd, sec, &isym))
9338     return FALSE;
9339
9340   if (isym == NULL)
9341     {
9342       isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9343                                    symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9344       symtab_hdr->contents = (bfd_byte *) isym;
9345     }
9346
9347   if (isym == NULL || symtab_hdr->sh_info == 0)
9348     return FALSE;
9349
9350   blank_t = blank_head;
9351   calc_nds32_blank_total (blank_head);
9352
9353   for (sect = abfd->sections; sect != NULL; sect = sect->next)
9354     {
9355       /* Adjust all the relocs.  */
9356
9357       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
9358       internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9359                                                    TRUE /* keep_memory */);
9360       irelend = internal_relocs + sect->reloc_count;
9361
9362       blank_t = blank_head;
9363       blank_t2 = blank_head;
9364
9365       if (!(sect->flags & SEC_RELOC))
9366         continue;
9367
9368       nds32_get_section_contents (abfd, sect, &contents, TRUE);
9369
9370       for (irel = internal_relocs; irel < irelend; irel++)
9371         {
9372           bfd_vma raddr;
9373
9374           if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9375               && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9376               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9377             {
9378               unsigned long val = 0;
9379               unsigned long mask;
9380               long before, between;
9381               long offset = 0;
9382
9383               switch (ELF32_R_TYPE (irel->r_info))
9384                 {
9385                 case R_NDS32_DIFF8:
9386                   offset = bfd_get_8 (abfd, contents + irel->r_offset);
9387                   break;
9388                 case R_NDS32_DIFF16:
9389                   offset = bfd_get_16 (abfd, contents + irel->r_offset);
9390                   break;
9391                 case R_NDS32_DIFF32:
9392                   val = bfd_get_32 (abfd, contents + irel->r_offset);
9393                   /* Get the signed bit and mask for the high part.  The
9394                      gcc will alarm when right shift 32-bit since the
9395                      type size of long may be 32-bit.  */
9396                   mask = 0 - (val >> 31);
9397                   if (mask)
9398                     offset = (val | (mask - 0xffffffff));
9399                   else
9400                     offset = val;
9401                   break;
9402                 default:
9403                   BFD_ASSERT (0);
9404                 }
9405
9406               /*                  DIFF value
9407                 0            |encoded in location|
9408                 |------------|-------------------|---------
9409                             sym+off(addend)
9410                 -- before ---| *****************
9411                 --------------------- between ---|
9412
9413                 We only care how much data are relax between DIFF,
9414                 marked as ***.  */
9415
9416               before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9417               between = get_nds32_elf_blank_total (&blank_t,
9418                                                    irel->r_addend + offset, 0);
9419               if (between == before)
9420                 goto done_adjust_diff;
9421
9422               switch (ELF32_R_TYPE (irel->r_info))
9423                 {
9424                 case R_NDS32_DIFF8:
9425                   bfd_put_8 (abfd, offset - (between - before),
9426                              contents + irel->r_offset);
9427                   break;
9428                 case R_NDS32_DIFF16:
9429                   bfd_put_16 (abfd, offset - (between - before),
9430                               contents + irel->r_offset);
9431                   break;
9432                 case R_NDS32_DIFF32:
9433                   bfd_put_32 (abfd, offset - (between - before),
9434                               contents + irel->r_offset);
9435                   break;
9436                 }
9437             }
9438           else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9439               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9440             {
9441               bfd_vma val = 0;
9442               unsigned int len = 0;
9443               unsigned long before, between;
9444               bfd_byte *endp, *p;
9445
9446               val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9447                                                &len);
9448
9449               before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9450               between = get_nds32_elf_blank_total (&blank_t,
9451                                                    irel->r_addend + val, 0);
9452               if (between == before)
9453                 goto done_adjust_diff;
9454
9455               p = contents + irel->r_offset;
9456               endp = p + len -1;
9457               memset (p, 0x80, len);
9458               *(endp) = 0;
9459               p = write_uleb128 (p, val - (between - before)) - 1;
9460               if (p < endp)
9461                 *p |= 0x80;
9462             }
9463 done_adjust_diff:
9464
9465           if (sec == sect)
9466             {
9467               raddr = irel->r_offset;
9468               irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9469                                                            irel->r_offset, 1);
9470
9471               if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9472                 continue;
9473               if (blank_t2 && blank_t2->next
9474                   && (blank_t2->offset > raddr
9475                       || blank_t2->next->offset <= raddr))
9476                 _bfd_error_handler
9477                   (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9478                    abfd);
9479
9480               /* Mark reloc in deleted portion as NONE.
9481                  For some relocs like R_NDS32_LABEL that doesn't modify the
9482                  content in the section.  R_NDS32_LABEL doesn't belong to the
9483                  instruction in the section, so we should preserve it.  */
9484               if (raddr >= blank_t2->offset
9485                   && raddr < blank_t2->offset + blank_t2->size
9486                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9487                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9488                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9489                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9490                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9491                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9492                 {
9493                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9494                                                R_NDS32_NONE);
9495                   continue;
9496                 }
9497             }
9498
9499           if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9500               || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9501               || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9502             continue;
9503
9504           if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9505               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9506               && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9507             {
9508               if (irel->r_addend <= sec->size)
9509                 irel->r_addend -=
9510                   get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9511             }
9512         }
9513     }
9514
9515   /* Adjust the local symbols defined in this section.  */
9516   blank_t = blank_head;
9517   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9518     {
9519       if (isym->st_shndx == sec_shndx)
9520         {
9521           if (isym->st_value <= sec->size)
9522             {
9523               bfd_vma ahead;
9524               bfd_vma orig_addr = isym->st_value;
9525
9526               ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9527               isym->st_value -= ahead;
9528
9529               /* Adjust function size.  */
9530               if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9531                   && isym->st_size > 0)
9532                 isym->st_size -=
9533                   get_nds32_elf_blank_total
9534                   (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9535             }
9536         }
9537     }
9538
9539   /* Now adjust the global symbols defined in this section.  */
9540   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9541               - symtab_hdr->sh_info);
9542   sym_hashes = elf_sym_hashes (abfd);
9543   end_hashes = sym_hashes + symcount;
9544   blank_t = blank_head;
9545   for (; sym_hashes < end_hashes; sym_hashes++)
9546     {
9547       struct elf_link_hash_entry *sym_hash = *sym_hashes;
9548
9549       if ((sym_hash->root.type == bfd_link_hash_defined
9550            || sym_hash->root.type == bfd_link_hash_defweak)
9551           && sym_hash->root.u.def.section == sec)
9552         {
9553           if (sym_hash->root.u.def.value <= sec->size)
9554             {
9555               bfd_vma ahead;
9556               bfd_vma orig_addr = sym_hash->root.u.def.value;
9557
9558               ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9559               sym_hash->root.u.def.value -= ahead;
9560
9561               /* Adjust function size.  */
9562               if (sym_hash->type == STT_FUNC)
9563                 sym_hash->size -=
9564                   get_nds32_elf_blank_total
9565                   (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9566
9567             }
9568         }
9569     }
9570
9571   contents = elf_section_data (sec)->this_hdr.contents;
9572   blank_t = blank_head;
9573   while (blank_t->next)
9574     {
9575       /* Actually delete the bytes.  */
9576
9577       /* If current blank is the last blank overlap with current section,
9578          go to finish process.  */
9579       if (sec->size <= (blank_t->next->offset))
9580         break;
9581
9582       memmove (contents + blank_t->offset - blank_t->total_size,
9583                contents + blank_t->offset + blank_t->size,
9584                blank_t->next->offset - (blank_t->offset + blank_t->size));
9585
9586       blank_t = blank_t->next;
9587     }
9588
9589   if (sec->size > (blank_t->offset + blank_t->size))
9590     {
9591       /* There are remaining code between blank and section boundary.
9592          Move the remaining code to appropriate location.  */
9593       memmove (contents + blank_t->offset - blank_t->total_size,
9594                contents + blank_t->offset + blank_t->size,
9595                sec->size - (blank_t->offset + blank_t->size));
9596       sec->size -= blank_t->total_size + blank_t->size;
9597     }
9598   else
9599     /* This blank is not entirely included in the section,
9600        reduce the section size by only part of the blank size.  */
9601     sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9602
9603   while (blank_head)
9604     {
9605       blank_t = blank_head;
9606       blank_head = blank_head->next;
9607       remove_nds32_elf_blank (blank_t);
9608     }
9609
9610   return TRUE;
9611 }
9612
9613 /* Get the contents of a section.  */
9614
9615 static int
9616 nds32_get_section_contents (bfd *abfd, asection *sec,
9617                             bfd_byte **contents_p, bfd_boolean cache)
9618 {
9619   /* Get the section contents.  */
9620   if (elf_section_data (sec)->this_hdr.contents != NULL)
9621     *contents_p = elf_section_data (sec)->this_hdr.contents;
9622   else
9623     {
9624       if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9625         return FALSE;
9626       if (cache)
9627         elf_section_data (sec)->this_hdr.contents = *contents_p;
9628     }
9629
9630   return TRUE;
9631 }
9632
9633 /* Get the contents of the internal symbol of abfd.  */
9634
9635 static int
9636 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9637                       Elf_Internal_Sym **isymbuf_p)
9638 {
9639   Elf_Internal_Shdr *symtab_hdr;
9640   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9641
9642   /* Read this BFD's local symbols if we haven't done so already.  */
9643   if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9644     {
9645       *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9646       if (*isymbuf_p == NULL)
9647         {
9648           *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9649                                              symtab_hdr->sh_info, 0,
9650                                              NULL, NULL, NULL);
9651           if (*isymbuf_p == NULL)
9652             return FALSE;
9653         }
9654     }
9655   symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9656
9657   return TRUE;
9658 }
9659
9660 /* Range of small data.  */
9661 static bfd_vma sdata_range[2][2];
9662 static bfd_vma const sdata_init_range[2] =
9663 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
9664
9665 static int
9666 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9667                      bfd_byte *contents, bfd_vma addr)
9668 {
9669   unsigned long insn = bfd_getb32 (contents + addr);
9670
9671   if (insn & 0x80000000)
9672     return 2;
9673
9674   return 4;
9675 }
9676
9677 /* Set the gp relax range.  We have to measure the safe range
9678    to do gp relaxation.  */
9679
9680 static void
9681 relax_range_measurement (bfd *abfd)
9682 {
9683   asection *sec_f, *sec_b;
9684   /* For upper bound.   */
9685   bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9686   bfd_vma align;
9687   static int decide_relax_range = 0;
9688   int i;
9689   int range_number = ARRAY_SIZE (sdata_init_range);
9690
9691   if (decide_relax_range)
9692     return;
9693   decide_relax_range = 1;
9694
9695   if (sda_rela_sec == NULL)
9696     {
9697       /* Since there is no data sections, we assume the range is page size.  */
9698       for (i = 0; i < range_number; i++)
9699         {
9700           sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9701           sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9702         }
9703       return;
9704     }
9705
9706   /* Get the biggest alignment power after the gp located section.  */
9707   sec_f = sda_rela_sec->output_section;
9708   sec_b = sec_f->next;
9709   align = 0;
9710   while (sec_b != NULL)
9711     {
9712       if ((unsigned)(1 << sec_b->alignment_power) > align)
9713         align = (1 << sec_b->alignment_power);
9714       sec_b = sec_b->next;
9715     }
9716
9717   /* I guess we can not determine the section before
9718      gp located section, so we assume the align is max page size.  */
9719   for (i = 0; i < range_number; i++)
9720     {
9721       sdata_range[i][1] = sdata_init_range[i] - align;
9722       BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9723       sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9724       BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9725     }
9726 }
9727
9728 /* These are macros used to check flags encoded in r_addend.
9729    They are only used by nds32_elf_relax_section ().  */
9730 #define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
9731 #define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
9732 #define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
9733 #define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
9734
9735 static const char * unrecognized_reloc_msg =
9736   /* xgettext:c-format */
9737   N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9738
9739 /* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
9740
9741 static bfd_boolean
9742 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9743                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9744                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9745                            Elf_Internal_Shdr *symtab_hdr)
9746 {
9747   /* There are 3 variations for LONGCALL1
9748      case 4-4-2; 16-bit on, optimize off or optimize for space
9749      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
9750      ori   ta, ta, lo12(symbol) ; LO12S0
9751      jral5 ta                   ;
9752
9753      case 4-4-4; 16-bit off, optimize don't care
9754      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
9755      ori   ta, ta, lo12(symbol) ; LO12S0
9756      jral  ta                   ;
9757
9758      case 4-4-4; 16-bit on, optimize for speed
9759      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
9760      ori   ta, ta, lo12(symbol) ; LO12S0
9761      jral  ta                   ;
9762      Check code for -mlong-calls output.  */
9763
9764   /* Get the reloc for the address from which the register is
9765      being loaded.  This reloc will tell us which function is
9766      actually being called.  */
9767
9768   bfd_vma laddr;
9769   int seq_len;  /* Original length of instruction sequence.  */
9770   uint32_t insn;
9771   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9772   bfd_signed_vma foff;
9773   uint16_t insn16;
9774
9775   irelend = internal_relocs + sec->reloc_count;
9776   seq_len = GET_SEQ_LEN (irel->r_addend);
9777   laddr = irel->r_offset;
9778   *insn_len = seq_len;
9779
9780   hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9781                                            R_NDS32_HI20_RELA, laddr);
9782   lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9783                                            R_NDS32_LO12S0_ORI_RELA,
9784                                            laddr + 4);
9785
9786   if (hi_irelfn == irelend || lo_irelfn == irelend)
9787     {
9788       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9789                           (uint64_t) irel->r_offset);
9790       return FALSE;
9791     }
9792
9793   /* Get the value of the symbol referred to by the reloc.  */
9794   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9795
9796   /* This condition only happened when symbol is undefined.  */
9797   if (foff == 0
9798       || foff < -CONSERVATIVE_24BIT_S1
9799       || foff >= CONSERVATIVE_24BIT_S1)
9800     return FALSE;
9801
9802   /* Relax to: jal symbol; 25_PCREL.  */
9803   /* For simplicity of coding, we are going to modify the section
9804      contents, the section relocs, and the BFD symbol table.  We
9805      must tell the rest of the code not to free up this
9806      information.  It would be possible to instead create a table
9807      of changes which have to be made, as is done in coff-mips.c;
9808      that would be more work, but would require less memory when
9809      the linker is run.  */
9810
9811   /* Replace the long call with a jal.  */
9812   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9813                                R_NDS32_25_PCREL_RELA);
9814   irel->r_addend = hi_irelfn->r_addend;
9815
9816   /* We don't resolve this here but resolve it in relocate_section.  */
9817   insn = INSN_JAL;
9818   bfd_putb32 (insn, contents + irel->r_offset);
9819
9820   hi_irelfn->r_info =
9821     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9822   lo_irelfn->r_info =
9823     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9824   *insn_len = 4;
9825
9826   if (seq_len & 0x2)
9827     {
9828       insn16 = NDS32_NOP16;
9829       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9830       lo_irelfn->r_info =
9831         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9832       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9833       *insn_len += 2;
9834     }
9835   return TRUE;
9836 }
9837
9838 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9839 /* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
9840
9841 static bfd_boolean
9842 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9843                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9844                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9845                            Elf_Internal_Shdr *symtab_hdr)
9846 {
9847   /* bltz  rt, .L1   ; LONGCALL2
9848      jal   symbol   ; 25_PCREL
9849      .L1: */
9850
9851   /* Get the reloc for the address from which the register is
9852      being loaded.  This reloc will tell us which function is
9853      actually being called.  */
9854
9855   bfd_vma laddr;
9856   uint32_t insn;
9857   Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9858   bfd_signed_vma foff;
9859
9860   irelend = internal_relocs + sec->reloc_count;
9861   laddr = irel->r_offset;
9862   i1_irelfn =
9863     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9864                                  R_NDS32_25_PCREL_RELA, laddr + 4);
9865
9866   if (i1_irelfn == irelend)
9867     {
9868       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9869                           (uint64_t) irel->r_offset);
9870       return FALSE;
9871     }
9872
9873   insn = bfd_getb32 (contents + laddr);
9874
9875   /* Get the value of the symbol referred to by the reloc.  */
9876   foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9877
9878   if (foff == 0
9879       || foff < -CONSERVATIVE_16BIT_S1
9880       || foff >= CONSERVATIVE_16BIT_S1)
9881     return FALSE;
9882
9883   /* Relax to   bgezal   rt, label ; 17_PCREL
9884      or         bltzal   rt, label ; 17_PCREL */
9885
9886   /* Convert to complimentary conditional call.  */
9887   insn = CONVERT_CONDITION_CALL (insn);
9888
9889   /* For simplicity of coding, we are going to modify the section
9890      contents, the section relocs, and the BFD symbol table.  We
9891      must tell the rest of the code not to free up this
9892      information.  It would be possible to instead create a table
9893      of changes which have to be made, as is done in coff-mips.c;
9894      that would be more work, but would require less memory when
9895      the linker is run.  */
9896
9897   /* Clean unnessary relocations.  */
9898   i1_irelfn->r_info =
9899     ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9900   cond_irelfn =
9901     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9902                                  R_NDS32_17_PCREL_RELA, laddr);
9903   if (cond_irelfn != irelend)
9904     cond_irelfn->r_info =
9905       ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9906
9907   /* Replace the long call with a bgezal.  */
9908   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9909                                R_NDS32_17_PCREL_RELA);
9910   irel->r_addend = i1_irelfn->r_addend;
9911
9912   bfd_putb32 (insn, contents + irel->r_offset);
9913
9914   *insn_len = 4;
9915   return TRUE;
9916 }
9917
9918 /* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9919
9920 static bfd_boolean
9921 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9922                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9923                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9924                            Elf_Internal_Shdr *symtab_hdr)
9925 {
9926   /* There are 3 variations for LONGCALL3
9927      case 4-4-4-2; 16-bit on, optimize off or optimize for space
9928      bltz  rt,   $1                ; LONGCALL3
9929      sethi ta,   hi20(symbol)      ; HI20
9930      ori   ta, ta,  lo12(symbol)   ; LO12S0
9931      jral5 ta                      ;
9932      $1
9933
9934      case 4-4-4-4; 16-bit off, optimize don't care
9935      bltz  rt,   $1                ; LONGCALL3
9936      sethi ta,   hi20(symbol)      ; HI20
9937      ori   ta, ta,  lo12(symbol)   ; LO12S0
9938      jral  ta                      ;
9939      $1
9940
9941      case 4-4-4-4; 16-bit on, optimize for speed
9942      bltz  rt,   $1                ; LONGCALL3
9943      sethi ta,   hi20(symbol)      ; HI20
9944      ori   ta, ta,  lo12(symbol)   ; LO12S0
9945      jral  ta                      ;
9946      $1 */
9947
9948   /* Get the reloc for the address from which the register is
9949      being loaded.  This reloc will tell us which function is
9950      actually being called.  */
9951
9952   bfd_vma laddr;
9953   int seq_len;  /* Original length of instruction sequence.  */
9954   uint32_t insn;
9955   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9956   bfd_signed_vma foff;
9957   uint16_t insn16;
9958
9959   irelend = internal_relocs + sec->reloc_count;
9960   seq_len = GET_SEQ_LEN (irel->r_addend);
9961   laddr = irel->r_offset;
9962   *insn_len = seq_len;
9963
9964   hi_irelfn =
9965     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9966                                  R_NDS32_HI20_RELA, laddr + 4);
9967   lo_irelfn =
9968     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9969                                  R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9970
9971   if (hi_irelfn == irelend || lo_irelfn == irelend)
9972     {
9973       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9974                           (uint64_t) irel->r_offset);
9975       return FALSE;
9976     }
9977
9978   /* Get the value of the symbol referred to by the reloc.  */
9979   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9980
9981   if (foff == 0
9982       || foff < -CONSERVATIVE_24BIT_S1
9983       || foff >= CONSERVATIVE_24BIT_S1)
9984     return FALSE;
9985
9986   insn = bfd_getb32 (contents + laddr);
9987   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9988     {
9989       /* Relax to  bgezal   rt, label ; 17_PCREL
9990          or        bltzal   rt, label ; 17_PCREL */
9991
9992       /* Convert to complimentary conditional call.  */
9993       insn = CONVERT_CONDITION_CALL (insn);
9994       bfd_putb32 (insn, contents + irel->r_offset);
9995
9996       *insn_len = 4;
9997       irel->r_info =
9998         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9999       hi_irelfn->r_info =
10000         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
10001       lo_irelfn->r_info =
10002         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10003
10004       cond_irelfn =
10005         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10006                                      R_NDS32_17_PCREL_RELA, laddr);
10007       if (cond_irelfn != irelend)
10008         {
10009           cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10010                                               R_NDS32_17_PCREL_RELA);
10011           cond_irelfn->r_addend = hi_irelfn->r_addend;
10012         }
10013
10014       if (seq_len & 0x2)
10015         {
10016           insn16 = NDS32_NOP16;
10017           bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10018           hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10019                                             R_NDS32_INSN16);
10020           hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10021           insn_len += 2;
10022         }
10023     }
10024   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10025     {
10026       /* Relax to the following instruction sequence
10027          bltz  rt,   $1 ; LONGCALL2
10028          jal   symbol   ; 25_PCREL
10029          $1     */
10030       *insn_len = 8;
10031       insn = INSN_JAL;
10032       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10033
10034       hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10035                                         R_NDS32_25_PCREL_RELA);
10036       irel->r_info =
10037         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10038
10039       lo_irelfn->r_info =
10040         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10041
10042       if (seq_len & 0x2)
10043         {
10044           insn16 = NDS32_NOP16;
10045           bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10046           lo_irelfn->r_info =
10047             ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10048           lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10049           insn_len += 2;
10050         }
10051     }
10052   return TRUE;
10053 }
10054
10055 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
10056
10057 static bfd_boolean
10058 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10059                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10060                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10061                            Elf_Internal_Shdr *symtab_hdr)
10062 {
10063   /* There are 3 variations for LONGJUMP1
10064      case 4-4-2; 16-bit bit on, optimize off or optimize for space
10065      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
10066      ori   ta, ta, lo12(symbol)  ; LO12S0
10067      jr5   ta                    ;
10068
10069      case 4-4-4; 16-bit off, optimize don't care
10070      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
10071      ori   ta, ta, lo12(symbol)  ; LO12S0
10072      jr    ta                    ;
10073
10074      case 4-4-4; 16-bit on, optimize for speed
10075      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
10076      ori   ta, ta, lo12(symbol)  ; LO12S0
10077      jr    ta                    ;      */
10078
10079   /* Get the reloc for the address from which the register is
10080      being loaded.  This reloc will tell us which function is
10081      actually being called.  */
10082
10083   bfd_vma laddr;
10084   int seq_len;  /* Original length of instruction sequence.  */
10085   int insn16_on;        /* 16-bit on/off.  */
10086   uint32_t insn;
10087   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10088   bfd_signed_vma foff;
10089   uint16_t insn16;
10090   unsigned long reloc;
10091
10092   irelend = internal_relocs + sec->reloc_count;
10093   seq_len = GET_SEQ_LEN (irel->r_addend);
10094   laddr = irel->r_offset;
10095   *insn_len = seq_len;
10096   insn16_on = IS_16BIT_ON (irel->r_addend);
10097
10098   hi_irelfn =
10099     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10100                                  R_NDS32_HI20_RELA, laddr);
10101   lo_irelfn =
10102     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10103                                  R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10104   if (hi_irelfn == irelend || lo_irelfn == irelend)
10105     {
10106       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10107                           (uint64_t) irel->r_offset);
10108       return FALSE;
10109     }
10110
10111   /* Get the value of the symbol referred to by the reloc.  */
10112   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10113
10114   if (foff == 0
10115       || foff >= CONSERVATIVE_24BIT_S1
10116       || foff < -CONSERVATIVE_24BIT_S1)
10117     return FALSE;
10118
10119   if (insn16_on
10120       && foff >= -ACCURATE_8BIT_S1
10121       && foff < ACCURATE_8BIT_S1
10122       && (seq_len & 0x2))
10123     {
10124       /* j8     label */
10125       /* 16-bit on, but not optimized for speed.  */
10126       reloc = R_NDS32_9_PCREL_RELA;
10127       insn16 = INSN_J8;
10128       bfd_putb16 (insn16, contents + irel->r_offset);
10129       *insn_len = 2;
10130       irel->r_info =
10131         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10132     }
10133   else
10134     {
10135       /* j     label */
10136       reloc = R_NDS32_25_PCREL_RELA;
10137       insn = INSN_J;
10138       bfd_putb32 (insn, contents + irel->r_offset);
10139       *insn_len = 4;
10140       irel->r_info =
10141         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10142       irel->r_addend = 0;
10143     }
10144
10145   hi_irelfn->r_info =
10146     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10147   lo_irelfn->r_info =
10148     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10149
10150   if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10151     {
10152       insn16 = NDS32_NOP16;
10153       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10154       lo_irelfn->r_info =
10155         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10156                       R_NDS32_INSN16);
10157       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10158       *insn_len += 2;
10159     }
10160   return TRUE;
10161 }
10162
10163 /* Revert condition branch.  This function does not check if the input
10164    instruction is condition branch or not.  */
10165
10166 static void
10167 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10168                            uint16_t *re_insn16, uint32_t *re_insn)
10169 {
10170   uint32_t comp_insn = 0;
10171   uint16_t comp_insn16 = 0;
10172
10173   if (insn)
10174     {
10175       if (N32_OP6 (insn) == N32_OP6_BR1)
10176         {
10177           /* beqs label.  */
10178           comp_insn = (insn ^ 0x4000) & 0xffffc000;
10179           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10180             {
10181               /* Insn can be contracted to 16-bit implied r5.  */
10182               comp_insn16 =
10183                 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10184               comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10185             }
10186         }
10187       else if (N32_OP6 (insn) == N32_OP6_BR3)
10188         {
10189           /* bnec $ta, imm11, label.  */
10190           comp_insn = (insn ^ 0x80000) & 0xffffff00;
10191         }
10192       else
10193         {
10194           comp_insn = (insn ^ 0x10000) & 0xffffc000;
10195           if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10196               || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10197             {
10198               if (N32_IS_RT3 (insn))
10199                 {
10200                   /* Insn can be contracted to 16-bit.  */
10201                   comp_insn16 =
10202                     (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10203                   comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10204                 }
10205               else if (N32_RT5 (insn) == REG_R15)
10206                 {
10207                   /* Insn can be contracted to 16-bit.  */
10208                   comp_insn16 =
10209                     (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10210                 }
10211             }
10212         }
10213     }
10214   else
10215     {
10216       switch ((insn16 & 0xf000) >> 12)
10217         {
10218         case 0xc:
10219           /* beqz38 or bnez38 */
10220           comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10221           comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10222           comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10223           break;
10224
10225         case 0xd:
10226           /* beqs38 or bnes38 */
10227           comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10228           comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10229           comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10230             | (REG_R5 << 15);
10231           break;
10232
10233         case 0xe:
10234           /* beqzS8 or bnezS8 */
10235           comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10236           comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10237           comp_insn |= REG_R15 << 20;
10238           break;
10239
10240         default:
10241           break;
10242         }
10243     }
10244   if (comp_insn && re_insn)
10245     *re_insn = comp_insn;
10246   if (comp_insn16 && re_insn16)
10247     *re_insn16 = comp_insn16;
10248 }
10249
10250 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
10251
10252 static bfd_boolean
10253 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10254                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10255                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10256                            Elf_Internal_Shdr *symtab_hdr)
10257 {
10258   /* There are 3 variations for LONGJUMP2
10259      case 2-4;  1st insn convertible, 16-bit on,
10260      optimize off or optimize for space
10261      bnes38  rt, ra, $1 ; LONGJUMP2
10262      j       label      ; 25_PCREL
10263      $1:
10264
10265      case 4-4; 1st insn not convertible
10266      bne  rt, ra, $1 ; LONGJUMP2
10267      j    label      ; 25_PCREL
10268      $1:
10269
10270      case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10271      bne  rt, ra, $1 ; LONGJUMP2
10272      j    label      ; 25_PCREL
10273      $1: */
10274
10275   /* Get the reloc for the address from which the register is
10276      being loaded.  This reloc will tell us which function is
10277      actually being called.  */
10278
10279   bfd_vma laddr;
10280   int seq_len;  /* Original length of instruction sequence.  */
10281   Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10282   int first_size;
10283   unsigned int i;
10284   bfd_signed_vma foff;
10285   uint32_t insn, re_insn = 0;
10286   uint16_t insn16, re_insn16 = 0;
10287   unsigned long reloc, cond_reloc;
10288
10289   enum elf_nds32_reloc_type checked_types[] =
10290     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10291
10292   irelend = internal_relocs + sec->reloc_count;
10293   seq_len = GET_SEQ_LEN (irel->r_addend);
10294   laddr = irel->r_offset;
10295   *insn_len = seq_len;
10296   first_size = (seq_len == 6) ? 2 : 4;
10297
10298   i2_irelfn =
10299     find_relocs_at_address_addr (irel, internal_relocs,
10300                                  irelend, R_NDS32_25_PCREL_RELA,
10301                                  laddr + first_size);
10302
10303   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10304     {
10305       cond_irelfn =
10306         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10307                                      checked_types[i], laddr);
10308       if (cond_irelfn != irelend)
10309         break;
10310     }
10311
10312   if (i2_irelfn == irelend || cond_irelfn == irelend)
10313     {
10314       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10315                           (uint64_t) irel->r_offset);
10316       return FALSE;
10317     }
10318
10319   /* Get the value of the symbol referred to by the reloc.  */
10320   foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10321   if (foff == 0
10322       || foff < -CONSERVATIVE_16BIT_S1
10323       || foff >= CONSERVATIVE_16BIT_S1)
10324     return FALSE;
10325
10326   /* Get the all corresponding instructions.  */
10327   if (first_size == 4)
10328     {
10329       insn = bfd_getb32 (contents + laddr);
10330       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10331     }
10332   else
10333     {
10334       insn16 = bfd_getb16 (contents + laddr);
10335       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10336     }
10337
10338   if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10339       && foff < ACCURATE_8BIT_S1 - first_size)
10340     {
10341       if (first_size == 4)
10342         {
10343           /* Don't convert it to 16-bit now, keep this as relaxable for
10344              ``label reloc; INSN16''.  */
10345
10346           /* Save comp_insn32 to buffer.  */
10347           bfd_putb32 (re_insn, contents + irel->r_offset);
10348           *insn_len = 4;
10349           reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10350             R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10351           cond_reloc = R_NDS32_INSN16;
10352         }
10353       else
10354         {
10355           bfd_putb16 (re_insn16, contents + irel->r_offset);
10356           *insn_len = 2;
10357           reloc = R_NDS32_9_PCREL_RELA;
10358           cond_reloc = R_NDS32_NONE;
10359         }
10360     }
10361   else if (N32_OP6 (re_insn) == N32_OP6_BR1
10362            && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10363                && foff < ACCURATE_14BIT_S1 - first_size))
10364     {
10365       /* beqs     label    ; 15_PCREL */
10366       bfd_putb32 (re_insn, contents + irel->r_offset);
10367       *insn_len = 4;
10368       reloc = R_NDS32_15_PCREL_RELA;
10369       cond_reloc = R_NDS32_NONE;
10370     }
10371   else if (N32_OP6 (re_insn) == N32_OP6_BR2
10372            && foff >= -CONSERVATIVE_16BIT_S1
10373            && foff < CONSERVATIVE_16BIT_S1)
10374     {
10375       /* beqz     label ; 17_PCREL */
10376       bfd_putb32 (re_insn, contents + irel->r_offset);
10377       *insn_len = 4;
10378       reloc = R_NDS32_17_PCREL_RELA;
10379       cond_reloc = R_NDS32_NONE;
10380     }
10381   else
10382     return FALSE;
10383
10384   /* Set all relocations.  */
10385   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10386   irel->r_addend = i2_irelfn->r_addend;
10387
10388   cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10389                                       cond_reloc);
10390   cond_irelfn->r_addend = 0;
10391
10392   if ((seq_len ^ *insn_len ) & 0x2)
10393     {
10394       insn16 = NDS32_NOP16;
10395       bfd_putb16 (insn16, contents + irel->r_offset + 4);
10396       i2_irelfn->r_offset = 4;
10397       i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10398                                         R_NDS32_INSN16);
10399       i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10400       *insn_len += 2;
10401     }
10402   else
10403     i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10404                                       R_NDS32_NONE);
10405   return TRUE;
10406 }
10407
10408 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
10409
10410 static bfd_boolean
10411 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10412                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10413                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10414                            Elf_Internal_Shdr *symtab_hdr)
10415 {
10416   /* There are 5 variations for LONGJUMP3
10417      case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10418      optimize off or optimize for space
10419      bnes38   rt, ra, $1            ; LONGJUMP3
10420      sethi    ta, hi20(symbol)      ; HI20
10421      ori      ta, ta, lo12(symbol)  ; LO12S0
10422      jr5      ta                    ;
10423      $1:                            ;
10424
10425      case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10426      bnes38   rt, ra, $1           ; LONGJUMP3
10427      sethi    ta, hi20(symbol)     ; HI20
10428      ori      ta, ta, lo12(symbol) ; LO12S0
10429      jr5      ta                   ;
10430      $1:                           ; LABEL
10431
10432      case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10433      optimize off or optimize for space
10434      bne   rt, ra, $1           ; LONGJUMP3
10435      sethi ta, hi20(symbol)     ; HI20
10436      ori   ta, ta, lo12(symbol) ; LO12S0
10437      jr5   ta                   ;
10438      $1:                        ;
10439
10440      case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10441      16-bit off if no INSN16
10442      bne   rt, ra, $1           ; LONGJUMP3
10443      sethi ta, hi20(symbol)     ; HI20
10444      ori   ta, ta, lo12(symbol) ; LO12S0
10445      jr    ta                   ;
10446      $1:                        ;
10447
10448      case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10449      16-bit off if no INSN16
10450      bne   rt, ra, $1           ; LONGJUMP3
10451      sethi ta, hi20(symbol)     ; HI20
10452      ori   ta, ta, lo12(symbol) ; LO12S0
10453      jr    ta                   ;
10454      $1:                        ; LABEL */
10455
10456   /* Get the reloc for the address from which the register is
10457      being loaded.  This reloc will tell us which function is
10458      actually being called.  */
10459   enum elf_nds32_reloc_type checked_types[] =
10460     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10461
10462   int reloc_off = 0, cond_removed = 0, convertible;
10463   bfd_vma laddr;
10464   int seq_len;  /* Original length of instruction sequence.  */
10465   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10466   int first_size;
10467   unsigned int i;
10468   bfd_signed_vma foff;
10469   uint32_t insn, re_insn = 0;
10470   uint16_t insn16, re_insn16 = 0;
10471   unsigned long reloc, cond_reloc;
10472
10473   irelend = internal_relocs + sec->reloc_count;
10474   seq_len = GET_SEQ_LEN (irel->r_addend);
10475   laddr = irel->r_offset;
10476   *insn_len = seq_len;
10477
10478   convertible = IS_1ST_CONVERT (irel->r_addend);
10479
10480   if (convertible)
10481     first_size = 2;
10482   else
10483     first_size = 4;
10484
10485   /* Get all needed relocations.  */
10486   hi_irelfn =
10487     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10488                                  R_NDS32_HI20_RELA, laddr + first_size);
10489   lo_irelfn =
10490     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10491                                  R_NDS32_LO12S0_ORI_RELA,
10492                                  laddr + first_size + 4);
10493
10494   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10495     {
10496       cond_irelfn =
10497         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10498                                      checked_types[i], laddr);
10499       if (cond_irelfn != irelend)
10500         break;
10501     }
10502
10503   if (hi_irelfn == irelend
10504       || lo_irelfn == irelend
10505       || cond_irelfn == irelend)
10506     {
10507       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10508                           (uint64_t) irel->r_offset);
10509       return FALSE;
10510     }
10511
10512   /* Get the value of the symbol referred to by the reloc.  */
10513   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10514
10515   if (foff == 0
10516       || foff < -CONSERVATIVE_24BIT_S1
10517       || foff >= CONSERVATIVE_24BIT_S1)
10518     return FALSE;
10519
10520   /* Get the all corresponding instructions.  */
10521   if (first_size == 4)
10522     {
10523       insn = bfd_getb32 (contents + laddr);
10524       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10525     }
10526   else
10527     {
10528       insn16 = bfd_getb16 (contents + laddr);
10529       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10530     }
10531
10532   /* For simplicity of coding, we are going to modify the section
10533      contents, the section relocs, and the BFD symbol table.  We
10534      must tell the rest of the code not to free up this
10535      information.  It would be possible to instead create a table
10536      of changes which have to be made, as is done in coff-mips.c;
10537      that would be more work, but would require less memory when
10538      the linker is run.  */
10539
10540   if (re_insn16
10541       && foff >= -ACCURATE_8BIT_S1 - first_size
10542       && foff < ACCURATE_8BIT_S1 - first_size)
10543     {
10544       if (!(seq_len & 0x2))
10545         {
10546           /* Don't convert it to 16-bit now, keep this as relaxable
10547              for ``label reloc; INSN1a''6.  */
10548           /* Save comp_insn32 to buffer.  */
10549           bfd_putb32 (re_insn, contents + irel->r_offset);
10550           *insn_len = 4;
10551           reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10552             R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10553           cond_reloc = R_NDS32_INSN16;
10554         }
10555       else
10556         {
10557           /* Not optimize for speed; convert sequence to 16-bit.  */
10558           /* Save comp_insn16 to buffer.  */
10559           bfd_putb16 (re_insn16, contents + irel->r_offset);
10560           *insn_len = 2;
10561           reloc = R_NDS32_9_PCREL_RELA;
10562           cond_reloc = R_NDS32_NONE;
10563         }
10564       cond_removed = 1;
10565     }
10566   else if (N32_OP6 (re_insn) == N32_OP6_BR1
10567            && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10568                && foff < ACCURATE_14BIT_S1 - first_size))
10569     {
10570       /* beqs     label    ; 15_PCREL */
10571       bfd_putb32 (re_insn, contents + irel->r_offset);
10572       *insn_len = 4;
10573       reloc = R_NDS32_15_PCREL_RELA;
10574       cond_reloc = R_NDS32_NONE;
10575       cond_removed = 1;
10576     }
10577   else if (N32_OP6 (re_insn) == N32_OP6_BR2
10578            && foff >= -CONSERVATIVE_16BIT_S1
10579            && foff < CONSERVATIVE_16BIT_S1)
10580     {
10581       /* beqz     label ; 17_PCREL */
10582       bfd_putb32 (re_insn, contents + irel->r_offset);
10583       *insn_len = 4;
10584       reloc = R_NDS32_17_PCREL_RELA;
10585       cond_reloc = R_NDS32_NONE;
10586       cond_removed = 1;
10587     }
10588   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10589            && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10590     {
10591       /* Relax to one of the following 3 variations
10592
10593          case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10594          for space
10595          bnes38  rt, $1 ; LONGJUMP2
10596          j       label  ; 25_PCREL
10597          $1
10598
10599          case 4-4; 1st insn not convertible, others don't care
10600          bne   rt, ra, $1 ; LONGJUMP2
10601          j     label      ; 25_PCREL
10602          $1
10603
10604          case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10605          bne   rt, ra, $1 ; LONGJUMP2
10606          j     label      ; 25_PCREL
10607          $1 */
10608
10609       /* Offset for first instruction.  */
10610
10611       /* Use j label as second instruction.  */
10612       *insn_len = 4 + first_size;
10613       insn = INSN_J;
10614       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10615       reloc = R_NDS32_LONGJUMP2;
10616       cond_reloc = R_NDS32_25_PLTREL;
10617     }
10618     else
10619       return FALSE;
10620
10621     if (cond_removed == 1)
10622       {
10623         /* Set all relocations.  */
10624         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10625         irel->r_addend = hi_irelfn->r_addend;
10626
10627         cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10628                                             cond_reloc);
10629         cond_irelfn->r_addend = 0;
10630         hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10631                                           R_NDS32_NONE);
10632       }
10633     else
10634       {
10635         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10636         irel->r_addend = irel->r_addend;
10637         hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10638                                           cond_reloc);
10639       }
10640
10641   if ((seq_len ^ *insn_len ) & 0x2)
10642     {
10643       insn16 = NDS32_NOP16;
10644       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10645       lo_irelfn->r_offset = *insn_len;
10646       lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10647                                         R_NDS32_INSN16);
10648       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10649       *insn_len += 2;
10650     }
10651   else
10652     lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10653                                       R_NDS32_NONE);
10654   return TRUE;
10655 }
10656
10657 /* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
10658
10659 static bfd_boolean
10660 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10661                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10662                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10663                            Elf_Internal_Shdr *symtab_hdr)
10664 {
10665   /* The pattern for LONGCALL4.  Support for function cse.
10666      sethi ta, hi20(symbol)     ; LONGCALL4/HI20
10667      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10668      jral  ta                   ; PTR_RES/EMPTY/INSN16  */
10669
10670   bfd_vma laddr;
10671   uint32_t insn;
10672   Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10673   Elf_Internal_Rela *irelend;
10674   bfd_signed_vma foff;
10675
10676   irelend = internal_relocs + sec->reloc_count;
10677   laddr = irel->r_offset;
10678
10679   /* Get the reloc for the address from which the register is
10680      being loaded.  This reloc will tell us which function is
10681      actually being called.  */
10682   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10683                                          R_NDS32_HI20_RELA, laddr);
10684
10685   if (hi_irel == irelend)
10686     {
10687       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10688                           (uint64_t) irel->r_offset);
10689       return FALSE;
10690     }
10691
10692   /* Get the value of the symbol referred to by the reloc.  */
10693   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10694
10695   /* This condition only happened when symbol is undefined.  */
10696   if (foff == 0
10697       || foff < -CONSERVATIVE_24BIT_S1
10698       || foff >= CONSERVATIVE_24BIT_S1)
10699     return FALSE;
10700
10701   /* Relax to: jal symbol; 25_PCREL.  */
10702   /* For simplicity of coding, we are going to modify the section
10703      contents, the section relocs, and the BFD symbol table.  We
10704      must tell the rest of the code not to free up this
10705      information.  It would be possible to instead create a table
10706      of changes which have to be made, as is done in coff-mips.c;
10707      that would be more work, but would require less memory when
10708      the linker is run.  */
10709
10710   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10711                                           R_NDS32_PTR_RESOLVED, irel->r_addend);
10712   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10713                                           R_NDS32_EMPTY, irel->r_addend);
10714
10715   if (ptr_irel == irelend || em_irel == irelend)
10716     {
10717       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10718                           (uint64_t) irel->r_offset);
10719       return FALSE;
10720     }
10721   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10722   insn = bfd_getb32 (contents + irel->r_addend);
10723   if (insn & 0x80000000)
10724     return FALSE;
10725
10726   /* Replace the long call with a jal.  */
10727   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10728                                   R_NDS32_25_PCREL_RELA);
10729   ptr_irel->r_addend = 1;
10730
10731   /* We don't resolve this here but resolve it in relocate_section.  */
10732   insn = INSN_JAL;
10733   bfd_putb32 (insn, contents + em_irel->r_offset);
10734
10735   irel->r_info =
10736     ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10737
10738   /* If there is function cse, HI20 can not remove now.  */
10739   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10740                                            R_NDS32_LONGCALL4, laddr);
10741   if (call_irel == irelend)
10742     {
10743       *insn_len = 0;
10744       hi_irel->r_info =
10745         ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10746     }
10747
10748   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10749                                           R_NDS32_INSN16, irel->r_addend);
10750   if (insn_irel != irelend)
10751     insn_irel->r_info =
10752       ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10753
10754   return TRUE;
10755 }
10756
10757 /* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
10758
10759 static bfd_boolean
10760 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10761                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10762                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10763                            Elf_Internal_Shdr *symtab_hdr)
10764 {
10765   /* The pattern for LONGCALL5.
10766      bltz  rt, .L1      ; LONGCALL5/17_PCREL
10767      jal   symbol       ; 25_PCREL
10768      .L1:  */
10769
10770   bfd_vma laddr;
10771   uint32_t insn;
10772   Elf_Internal_Rela *cond_irel, *irelend;
10773   bfd_signed_vma foff;
10774
10775   irelend = internal_relocs + sec->reloc_count;
10776   laddr = irel->r_offset;
10777   insn = bfd_getb32 (contents + laddr);
10778
10779   /* Get the reloc for the address from which the register is
10780      being loaded.  This reloc will tell us which function is
10781      actually being called.  */
10782   cond_irel =
10783     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10784                                  R_NDS32_25_PCREL_RELA, irel->r_addend);
10785   if (cond_irel == irelend)
10786     {
10787       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10788                           (uint64_t) irel->r_offset);
10789       return FALSE;
10790     }
10791
10792   /* Get the value of the symbol referred to by the reloc.  */
10793   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10794
10795   if (foff == 0
10796       || foff < -CONSERVATIVE_16BIT_S1
10797       || foff >= CONSERVATIVE_16BIT_S1)
10798     return FALSE;
10799
10800   /* Relax to   bgezal   rt, label ; 17_PCREL
10801      or         bltzal   rt, label ; 17_PCREL.  */
10802
10803   /* Convert to complimentary conditional call.  */
10804   insn = CONVERT_CONDITION_CALL (insn);
10805
10806   /* For simplicity of coding, we are going to modify the section
10807      contents, the section relocs, and the BFD symbol table.  We
10808      must tell the rest of the code not to free up this
10809      information.  It would be possible to instead create a table
10810      of changes which have to be made, as is done in coff-mips.c;
10811      that would be more work, but would require less memory when
10812      the linker is run.  */
10813
10814   /* Modify relocation and contents.  */
10815   cond_irel->r_info =
10816     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10817
10818   /* Replace the long call with a bgezal.  */
10819   bfd_putb32 (insn, contents + cond_irel->r_offset);
10820   *insn_len = 0;
10821
10822   /* Clean unnessary relocations.  */
10823   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10824
10825   cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10826                                            R_NDS32_17_PCREL_RELA, laddr);
10827   cond_irel->r_info =
10828     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10829
10830   return TRUE;
10831 }
10832
10833 /* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
10834
10835 static bfd_boolean
10836 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10837                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10838                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10839                            Elf_Internal_Shdr *symtab_hdr)
10840 {
10841   /* The pattern for LONGCALL6.
10842      bltz  rt,   .L1                    ; LONGCALL6/17_PCREL
10843      sethi ta,   hi20(symbol)           ; HI20/PTR
10844      ori   ta, ta,  lo12(symbol)        ; LO12S0_ORI/PTR
10845      jral  ta                           ; PTR_RES/EMPTY/INSN16
10846      .L1  */
10847
10848   bfd_vma laddr;
10849   uint32_t insn;
10850   Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10851   bfd_signed_vma foff;
10852
10853   irelend = internal_relocs + sec->reloc_count;
10854   laddr = irel->r_offset;
10855
10856   /* Get the reloc for the address from which the register is
10857      being loaded.  This reloc will tell us which function is
10858      actually being called.  */
10859   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10860                                          R_NDS32_EMPTY, irel->r_addend);
10861
10862   if (em_irel == irelend)
10863     {
10864       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10865                           (uint64_t) irel->r_offset);
10866       return FALSE;
10867     }
10868
10869   /* Get the value of the symbol referred to by the reloc.  */
10870   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10871
10872   if (foff == 0
10873       || foff < -CONSERVATIVE_24BIT_S1
10874       || foff >= CONSERVATIVE_24BIT_S1)
10875     return FALSE;
10876
10877   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10878   insn = bfd_getb32 (contents + irel->r_addend);
10879   if (insn & 0x80000000)
10880     return FALSE;
10881
10882   insn = bfd_getb32 (contents + laddr);
10883   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10884     {
10885       /* Relax to  bgezal   rt, label ; 17_PCREL
10886          or        bltzal   rt, label ; 17_PCREL.  */
10887
10888       /* Convert to complimentary conditional call.  */
10889       *insn_len = 0;
10890       insn = CONVERT_CONDITION_CALL (insn);
10891       bfd_putb32 (insn, contents + em_irel->r_offset);
10892
10893       em_irel->r_info =
10894         ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10895
10896       /* Set resolved relocation.  */
10897       cond_irel =
10898         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10899                                      R_NDS32_PTR_RESOLVED, irel->r_addend);
10900       if (cond_irel == irelend)
10901         {
10902           _bfd_error_handler (unrecognized_reloc_msg, abfd,
10903                               "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10904           return FALSE;
10905         }
10906       cond_irel->r_addend = 1;
10907
10908       /* Clear relocations.  */
10909
10910       irel->r_info =
10911         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10912
10913       cond_irel =
10914         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10915                                      R_NDS32_17_PCREL_RELA, laddr);
10916       if (cond_irel != irelend)
10917         cond_irel->r_info =
10918           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10919
10920       cond_irel =
10921         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10922                                      R_NDS32_INSN16, irel->r_addend);
10923       if (cond_irel != irelend)
10924         cond_irel->r_info =
10925           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10926
10927     }
10928   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10929     {
10930       /* Relax to the following instruction sequence
10931          bltz  rt, .L1  ; LONGCALL2/17_PCREL
10932          jal   symbol   ; 25_PCREL/PTR_RES
10933          .L1  */
10934       *insn_len = 4;
10935       /* Convert instruction.  */
10936       insn = INSN_JAL;
10937       bfd_putb32 (insn, contents + em_irel->r_offset);
10938
10939       /* Convert relocations.  */
10940       em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10941                                       R_NDS32_25_PCREL_RELA);
10942       irel->r_info =
10943         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10944
10945       /* Set resolved relocation.  */
10946       cond_irel =
10947         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10948                                      R_NDS32_PTR_RESOLVED, irel->r_addend);
10949       if (cond_irel == irelend)
10950         {
10951           _bfd_error_handler (unrecognized_reloc_msg, abfd,
10952                               "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10953           return FALSE;
10954         }
10955       cond_irel->r_addend = 1;
10956
10957       cond_irel =
10958         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10959                                      R_NDS32_INSN16, irel->r_addend);
10960       if (cond_irel != irelend)
10961         cond_irel->r_info =
10962           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10963     }
10964   return TRUE;
10965 }
10966
10967 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10968
10969 static bfd_boolean
10970 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10971                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10972                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10973                            Elf_Internal_Shdr *symtab_hdr)
10974 {
10975   /* The pattern for LONGJUMP4.
10976      sethi ta, hi20(symbol)     ; LONGJUMP4/HI20
10977      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10978      jr    ta                   ; PTR_RES/INSN16/EMPTY  */
10979
10980   bfd_vma laddr;
10981   int seq_len;  /* Original length of instruction sequence.  */
10982   uint32_t insn;
10983   Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10984   bfd_signed_vma foff;
10985
10986   irelend = internal_relocs + sec->reloc_count;
10987   seq_len = GET_SEQ_LEN (irel->r_addend);
10988   laddr = irel->r_offset;
10989   *insn_len = seq_len;
10990
10991   /* Get the reloc for the address from which the register is
10992      being loaded.  This reloc will tell us which function is
10993      actually being called.  */
10994
10995   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10996                                          R_NDS32_HI20_RELA, laddr);
10997
10998   if (hi_irel == irelend)
10999     {
11000       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
11001                           (uint64_t) irel->r_offset);
11002       return FALSE;
11003     }
11004
11005   /* Get the value of the symbol referred to by the reloc.  */
11006   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
11007
11008   if (foff == 0
11009       || foff >= CONSERVATIVE_24BIT_S1
11010       || foff < -CONSERVATIVE_24BIT_S1)
11011     return FALSE;
11012
11013   /* Convert it to "j label", it may be converted to j8 in the final
11014      pass of relaxation.  Therefore, we do not consider this currently.  */
11015   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11016                                           R_NDS32_PTR_RESOLVED, irel->r_addend);
11017   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11018                                          R_NDS32_EMPTY, irel->r_addend);
11019
11020   if (ptr_irel == irelend || em_irel == irelend)
11021     {
11022       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
11023                           (uint64_t) irel->r_offset);
11024       return FALSE;
11025     }
11026
11027   em_irel->r_info =
11028     ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11029   ptr_irel->r_addend = 1;
11030
11031   /* Write instruction.  */
11032   insn = INSN_J;
11033   bfd_putb32 (insn, contents + em_irel->r_offset);
11034
11035   /* Clear relocations.  */
11036   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11037
11038   /* If there is function cse, HI20 can not remove now.  */
11039   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11040                                            R_NDS32_LONGJUMP4, laddr);
11041   if (call_irel == irelend)
11042     {
11043       *insn_len = 0;
11044       hi_irel->r_info =
11045         ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11046     }
11047
11048   return TRUE;
11049 }
11050
11051 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
11052
11053 static bfd_boolean
11054 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11055                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11056                            int *seq_len, bfd_byte *contents,
11057                            Elf_Internal_Sym *isymbuf,
11058                            Elf_Internal_Shdr *symtab_hdr)
11059 {
11060   /* There are 2 variations for LONGJUMP5
11061      case 2-4;  1st insn convertible, 16-bit on.
11062      bnes38  rt, ra, .L1        ; LONGJUMP5/9_PCREL/INSN16
11063      j       label              ; 25_PCREL/INSN16
11064      $1:
11065
11066      case 4-4; 1st insn not convertible
11067      bne  rt, ra, .L1   ; LONGJUMP5/15_PCREL/INSN16
11068      j    label         ; 25_PCREL/INSN16
11069      .L1:  */
11070
11071   bfd_vma laddr;
11072   Elf_Internal_Rela *cond_irel,  *irelend;
11073   unsigned int i;
11074   bfd_signed_vma foff;
11075   uint32_t insn, re_insn = 0;
11076   uint16_t insn16, re_insn16 = 0;
11077   unsigned long reloc;
11078
11079   enum elf_nds32_reloc_type checked_types[] =
11080     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11081       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11082
11083   irelend = internal_relocs + sec->reloc_count;
11084   laddr = irel->r_offset;
11085
11086   /* Get the reloc for the address from which the register is
11087      being loaded.  This reloc will tell us which function is
11088      actually being called.  */
11089
11090   cond_irel =
11091     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11092                                  R_NDS32_25_PCREL_RELA, irel->r_addend);
11093   if (cond_irel == irelend)
11094     {
11095       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11096                           (uint64_t) irel->r_offset);
11097       return FALSE;
11098     }
11099
11100   /* Get the value of the symbol referred to by the reloc.  */
11101   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11102
11103   if (foff == 0
11104       || foff < -CONSERVATIVE_16BIT_S1
11105       || foff >= CONSERVATIVE_16BIT_S1)
11106     return FALSE;
11107
11108   /* Get the all corresponding instructions.  */
11109   insn = bfd_getb32 (contents + laddr);
11110   /* Check instruction size.  */
11111   if (insn & 0x80000000)
11112     {
11113       *seq_len = 0;
11114       insn16 = insn >> 16;
11115       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11116     }
11117   else
11118     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11119
11120   if (N32_OP6 (re_insn) == N32_OP6_BR1
11121       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11122     {
11123       /* beqs label ; 15_PCREL.  */
11124       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11125       reloc = R_NDS32_15_PCREL_RELA;
11126     }
11127   else if (N32_OP6 (re_insn) == N32_OP6_BR2
11128            && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11129     {
11130       /* beqz label ; 17_PCREL.  */
11131       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11132       reloc = R_NDS32_17_PCREL_RELA;
11133     }
11134   else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11135            && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11136     {
11137       /* beqc label ; 9_PCREL.  */
11138       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11139       reloc = R_NDS32_WORD_9_PCREL_RELA;
11140     }
11141   else
11142     return FALSE;
11143
11144   /* Set all relocations.  */
11145   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11146
11147   /* Clean relocations.  */
11148   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11149   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11150     {
11151       cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11152                                                checked_types[i], laddr);
11153       if (cond_irel != irelend)
11154         {
11155           if (*seq_len == 0
11156               && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11157             {
11158               /* If the branch instruction is 2 byte, it cannot remove
11159                  directly.  Only convert it to nop16 and remove it after
11160                  checking alignment issue.  */
11161               insn16 = NDS32_NOP16;
11162               bfd_putb16 (insn16, contents + laddr);
11163               cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11164             }
11165           else
11166             cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11167                                               R_NDS32_NONE);
11168         }
11169     }
11170   *insn_len = 0;
11171
11172   return TRUE;
11173 }
11174
11175 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
11176
11177 static bfd_boolean
11178 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11179                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11180                            int *seq_len, bfd_byte *contents,
11181                            Elf_Internal_Sym *isymbuf,
11182                            Elf_Internal_Shdr *symtab_hdr)
11183 {
11184   /* There are 5 variations for LONGJUMP6
11185      case : 2-4-4-4; 1st insn convertible, 16-bit on.
11186      bnes38   rt, ra, .L1               ; LONGJUMP6/15_PCREL/INSN16
11187      sethi    ta, hi20(symbol)          ; HI20/PTR
11188      ori      ta, ta, lo12(symbol)      ; LO12S0_ORI/PTR
11189      jr       ta                        ; PTR_RES/INSN16/EMPTY
11190      .L1:
11191
11192      case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11193      bne   rt, ra, .L1          ; LONGJUMP6/15_PCREL/INSN16
11194      sethi ta, hi20(symbol)     ; HI20/PTR
11195      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11196      jr    ta                   ; PTR_RES/INSN16/EMPTY
11197      .L1:  */
11198
11199   enum elf_nds32_reloc_type checked_types[] =
11200     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11201       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11202
11203   int reloc_off = 0, cond_removed = 0;
11204   bfd_vma laddr;
11205   Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11206   unsigned int i;
11207   bfd_signed_vma foff;
11208   uint32_t insn, re_insn = 0;
11209   uint16_t insn16, re_insn16 = 0;
11210   unsigned long reloc;
11211
11212   irelend = internal_relocs + sec->reloc_count;
11213   laddr = irel->r_offset;
11214
11215   /* Get the reloc for the address from which the register is
11216      being loaded.  This reloc will tell us which function is
11217      actually being called.  */
11218   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11219                                          R_NDS32_EMPTY, irel->r_addend);
11220
11221   if (em_irel == irelend)
11222     {
11223       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11224                           (uint64_t) irel->r_offset);
11225       return FALSE;
11226     }
11227
11228   /* Get the value of the symbol referred to by the reloc.  */
11229   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11230
11231   if (foff == 0
11232       || foff < -CONSERVATIVE_24BIT_S1
11233       || foff >= CONSERVATIVE_24BIT_S1)
11234     return FALSE;
11235
11236   insn = bfd_getb32 (contents + laddr);
11237   /* Check instruction size.  */
11238   if (insn & 0x80000000)
11239     {
11240       *seq_len = 0;
11241       insn16 = insn >> 16;
11242       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11243     }
11244   else
11245     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11246
11247   /* For simplicity of coding, we are going to modify the section
11248      contents, the section relocs, and the BFD symbol table.  We
11249      must tell the rest of the code not to free up this
11250      information.  It would be possible to instead create a table
11251      of changes which have to be made, as is done in coff-mips.c;
11252      that would be more work, but would require less memory when
11253      the linker is run.  */
11254
11255   if (N32_OP6 (re_insn) == N32_OP6_BR1
11256       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11257     {
11258       /* beqs     label    ; 15_PCREL.  */
11259       bfd_putb32 (re_insn, contents + em_irel->r_offset);
11260       reloc = R_NDS32_15_PCREL_RELA;
11261       cond_removed = 1;
11262     }
11263   else if (N32_OP6 (re_insn) == N32_OP6_BR2
11264            && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11265     {
11266       /* beqz     label ; 17_PCREL.  */
11267       bfd_putb32 (re_insn, contents + em_irel->r_offset);
11268       reloc = R_NDS32_17_PCREL_RELA;
11269       cond_removed = 1;
11270     }
11271   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11272            && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11273     {
11274       /* Relax to one of the following 2 variations
11275
11276          case 2-4;  1st insn convertible, 16-bit on.
11277          bnes38  rt, ra, .L1    ; LONGJUMP5/9_PCREL/INSN16
11278          j       label          ; 25_PCREL/INSN16
11279          $1:
11280
11281          case 4-4; 1st insn not convertible
11282          bne  rt, ra, .L1       ; LONGJUMP5/15_PCREL/INSN16
11283          j    label             ; 25_PCREL/INSN16
11284          .L1:  */
11285
11286       /* Use j label as second instruction.  */
11287       insn = INSN_J;
11288       reloc = R_NDS32_25_PCREL_RELA;
11289       bfd_putb32 (insn, contents + em_irel->r_offset);
11290     }
11291   else
11292     return FALSE;
11293
11294   /* Set all relocations.  */
11295   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11296
11297   cond_irel =
11298     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11299                                  R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11300   cond_irel->r_addend = 1;
11301
11302   /* Use INSN16 of first branch instruction to distinguish if keeping
11303      INSN16 of final instruction or not.  */
11304   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11305                                            R_NDS32_INSN16, irel->r_offset);
11306   if (insn_irel == irelend)
11307     {
11308       /* Clean the final INSN16.  */
11309       insn_irel =
11310         find_relocs_at_address_addr (irel, internal_relocs, irelend,
11311                                      R_NDS32_INSN16, em_irel->r_offset);
11312       insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11313                                         R_NDS32_NONE);
11314     }
11315
11316   if (cond_removed == 1)
11317     {
11318       *insn_len = 0;
11319
11320       /* Clear relocations.  */
11321       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11322
11323       for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11324         {
11325           cond_irel =
11326             find_relocs_at_address_addr (irel, internal_relocs, irelend,
11327                                          checked_types[i], laddr);
11328           if (cond_irel != irelend)
11329             {
11330               if (*seq_len == 0
11331                   && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11332                 {
11333                   /* If the branch instruction is 2 byte, it cannot remove
11334                      directly.  Only convert it to nop16 and remove it after
11335                      checking alignment issue.  */
11336                   insn16 = NDS32_NOP16;
11337                   bfd_putb16 (insn16, contents + laddr);
11338                   cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11339                 }
11340               else
11341                 cond_irel->r_info =
11342                   ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11343             }
11344         }
11345     }
11346   else
11347     {
11348       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11349                                    R_NDS32_LONGJUMP5);
11350     }
11351
11352   return TRUE;
11353 }
11354
11355 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
11356
11357 static bfd_boolean
11358 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11359                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11360                            int *seq_len, bfd_byte *contents,
11361                            Elf_Internal_Sym *isymbuf,
11362                            Elf_Internal_Shdr *symtab_hdr)
11363 {
11364   /* There are 2 variations for LONGJUMP5
11365      case 2-4;  1st insn convertible, 16-bit on.
11366      movi55  ta, imm11          ; LONGJUMP7/INSN16
11367      beq     rt, ta, label      ; 15_PCREL
11368
11369      case 4-4; 1st insn not convertible
11370      movi55  ta, imm11          ; LONGJUMP7/INSN16
11371      beq     rt, ta, label      ; 15_PCREL  */
11372
11373   bfd_vma laddr;
11374   Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
11375   bfd_signed_vma foff;
11376   uint32_t insn, re_insn = 0;
11377   uint16_t insn16;
11378   uint32_t imm11;
11379
11380   irelend = internal_relocs + sec->reloc_count;
11381   laddr = irel->r_offset;
11382
11383   /* Get the reloc for the address from which the register is
11384      being loaded.  This reloc will tell us which function is
11385      actually being called.  */
11386
11387   cond_irel =
11388     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11389                                  R_NDS32_15_PCREL_RELA, irel->r_addend);
11390   if (cond_irel == irelend)
11391     {
11392       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11393                           (uint64_t) irel->r_offset);
11394       return FALSE;
11395     }
11396
11397   /* Get the value of the symbol referred to by the reloc.  */
11398   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11399
11400   if (foff == 0
11401       || foff < -CONSERVATIVE_8BIT_S1
11402       || foff >= CONSERVATIVE_8BIT_S1)
11403     return FALSE;
11404
11405   /* Get the first instruction for its size.  */
11406   insn = bfd_getb32 (contents + laddr);
11407   if (insn & 0x80000000)
11408     {
11409       *seq_len = 0;
11410       /* Get the immediate from movi55.  */
11411       imm11 = N16_IMM5S (insn >> 16);
11412     }
11413   else
11414     {
11415       /* Get the immediate from movi.  */
11416       imm11 = N32_IMM20S (insn);
11417     }
11418
11419   /* Get the branch instruction.  */
11420   insn = bfd_getb32 (contents + irel->r_addend);
11421   /* Convert instruction to BR3.  */
11422   if ((insn >> 14) & 0x1)
11423     re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11424   else
11425     re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11426
11427   bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11428
11429   /* Set all relocations.  */
11430   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11431                                     R_NDS32_WORD_9_PCREL_RELA);
11432
11433   /* Clean relocations.  */
11434   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11435   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11436                                            R_NDS32_INSN16, irel->r_offset);
11437   if (insn_irel != irelend)
11438     {
11439       if (*seq_len == 0)
11440         {
11441           /* If the first insntruction is 16bit, convert it to nop16.  */
11442           insn16 = NDS32_NOP16;
11443           bfd_putb16 (insn16, contents + laddr);
11444           insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11445         }
11446       else
11447         cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11448                                           R_NDS32_NONE);
11449     }
11450   *insn_len = 0;
11451
11452   return TRUE;
11453 }
11454
11455 /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11456    for each relax round. But the gp may changed dramatically and then cause
11457    the truncated to fit errors for the the converted gp instructions.
11458    Therefore, we must reserve the minimum but safe enough size to prevent it.  */
11459
11460 static bfd_boolean
11461 nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11462                        Elf_Internal_Rela *irel, bfd_boolean *again,
11463                        bfd_boolean init,
11464                        struct elf_nds32_link_hash_table *table,
11465                        Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11466
11467 {
11468   int offset_to_gp;
11469   static bfd_boolean sec_pass = FALSE;
11470   static asection *first_sec = NULL, *sym_sec;
11471   /* Record the number of instructions which may be removed.  */
11472   static int count = 0, record_count;
11473   Elf_Internal_Sym *isym;
11474   struct elf_link_hash_entry *h = NULL;
11475   int indx;
11476   unsigned long r_symndx;
11477   bfd *abfd = sec->owner;
11478   static bfd_vma record_sda = 0;
11479   int sda_offset = 0;
11480
11481   /* Force doing relaxation when hyper-relax is high.  */
11482   if (table->hyper_relax == 2)
11483     return TRUE;
11484
11485   /* Do not relax the load/store patterns for the first
11486      relax round.  */
11487   if (init)
11488     {
11489       if (!first_sec)
11490         first_sec = sec;
11491       else if (first_sec == sec)
11492         {
11493           record_count = count;
11494           count = 0;
11495           sec_pass = TRUE;
11496         }
11497
11498       if (!sec_pass)
11499         *again = TRUE;
11500
11501       return TRUE;
11502     }
11503
11504   /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11505      DATA_SEGMENT_ALIGN size in the linker script may make it
11506      get even bigger.  */
11507   if (record_sda == 0)
11508     record_sda = local_sda;
11509   else if (local_sda > record_sda)
11510     sda_offset = local_sda - record_sda;
11511
11512   /* Assume the instruction will be removed in the best case.  */
11513   count++;
11514
11515   /* We record the offset to gp for each symbol, and then check
11516      if it is changed dramatically after relaxing.
11517      (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11518      (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx].  */
11519   r_symndx = ELF32_R_SYM (irel->r_info);
11520   if (r_symndx >= symtab_hdr->sh_info)
11521     {
11522       /* Global symbols.  */
11523       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11524       h = elf_sym_hashes (abfd)[indx];
11525       sym_sec = h->root.u.def.section;
11526       if (NDS32_GUARD_SEC_P (sym_sec->flags)
11527           || bfd_is_abs_section (sym_sec))
11528         {
11529           /* Forbid doing relaxation when hyper-relax is low.  */
11530           if (table->hyper_relax == 0)
11531             return FALSE;
11532
11533           offset_to_gp = *access_addr - local_sda;
11534           if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11535             elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11536           else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11537                    < abs (offset_to_gp) - sda_offset)
11538             {
11539               /* This may cause the error, so we reserve the
11540                  safe enough size for relaxing.  */
11541               if (*access_addr >= local_sda)
11542                 *access_addr += (record_count * 4);
11543               else
11544                 *access_addr -= (record_count * 4);
11545             }
11546           return sec_pass;
11547         }
11548     }
11549   else
11550     {
11551       /* Local symbols.  */
11552       if (!elf32_nds32_allocate_local_sym_info (abfd))
11553         return FALSE;
11554       isym = isymbuf + r_symndx;
11555
11556       sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11557       if (NDS32_GUARD_SEC_P (sym_sec->flags))
11558         {
11559           /* Forbid doing relaxation when hyper-relax is low.  */
11560           if (table->hyper_relax == 0)
11561             return FALSE;
11562
11563           offset_to_gp = *access_addr - local_sda;
11564           if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11565             elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11566           else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11567                    < abs (offset_to_gp) - sda_offset)
11568             {
11569               /* This may cause the error, so we reserve the
11570                  safe enough size for relaxing.  */
11571               if (*access_addr >= local_sda)
11572                 *access_addr += (record_count * 4);
11573               else
11574                 *access_addr -= (record_count * 4);
11575             }
11576           return sec_pass;
11577         }
11578     }
11579
11580   return TRUE;
11581 }
11582
11583 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11584
11585 /* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
11586
11587 static bfd_boolean
11588 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11589                            asection *sec, Elf_Internal_Rela *irel,
11590                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11591                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11592                            Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11593                            struct elf_nds32_link_hash_table *table)
11594 {
11595   int eliminate_sethi = 0, range_type;
11596   unsigned int i;
11597   bfd_vma local_sda, laddr;
11598   int seq_len;  /* Original length of instruction sequence.  */
11599   uint32_t insn;
11600   Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11601   bfd_vma access_addr = 0;
11602   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
11603   struct elf_link_hash_entry *h = NULL;
11604   int indx;
11605   enum elf_nds32_reloc_type checked_types[] =
11606     { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11607       R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11608       R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11609       R_NDS32_TLS_LE_HI20
11610     };
11611
11612   irelend = internal_relocs + sec->reloc_count;
11613   seq_len = GET_SEQ_LEN (irel->r_addend);
11614   laddr = irel->r_offset;
11615   *insn_len = seq_len;
11616
11617   /* Get the high part relocation.  */
11618   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11619     {
11620       hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11621                                                checked_types[i], laddr);
11622       if (hi_irelfn != irelend)
11623         break;
11624     }
11625
11626   if (hi_irelfn == irelend)
11627     {
11628       /* Not R_NDS32_HI20_RELA.  */
11629       if (i != 0)
11630         _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11631                             (uint64_t) irel->r_offset);
11632       return FALSE;
11633     }
11634
11635   range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11636   nds32_elf_final_sda_base (sec->output_section->owner,
11637                             link_info, &local_sda, FALSE);
11638
11639   switch (ELF32_R_TYPE (hi_irelfn->r_info))
11640     {
11641     case R_NDS32_HI20_RELA:
11642       insn = bfd_getb32 (contents + laddr);
11643       access_addr =
11644         calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11645
11646       if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11647         {
11648           indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11649           h = elf_sym_hashes (abfd)[indx];
11650         }
11651
11652       /* Try movi.  */
11653       if (range_type == NDS32_LOADSTORE_IMM
11654           && access_addr < CONSERVATIVE_20BIT
11655           && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11656         {
11657           eliminate_sethi = 1;
11658           break;
11659         }
11660
11661       if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11662         {
11663           eliminate_sethi = 1;
11664           break;
11665         }
11666       else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11667                                        NULL, FALSE, table, isymbuf, symtab_hdr))
11668         return FALSE;
11669
11670       if (!load_store_relax)
11671         return FALSE;
11672
11673       /* Case for set gp register.  */
11674       if (N32_RT5 (insn) == REG_GP)
11675         return FALSE;
11676
11677       if (range_type == NDS32_LOADSTORE_FLOAT_S
11678           || range_type == NDS32_LOADSTORE_FLOAT_D)
11679         {
11680           range_l = sdata_range[0][0];
11681           range_h = sdata_range[0][1];
11682         }
11683       else
11684         {
11685           range_l = sdata_range[1][0];
11686           range_h = sdata_range[1][1];
11687         }
11688       break;
11689
11690     default:
11691       return FALSE;
11692     }
11693
11694   /* Delete sethi instruction.  */
11695   if (eliminate_sethi == 1
11696       || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11697       || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11698     {
11699       hi_irelfn->r_info =
11700         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11701       irel->r_info =
11702         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11703       *insn_len = 0;
11704       return TRUE;
11705     }
11706
11707   return FALSE;
11708 }
11709
11710 /* Relax LO12 relocation for nds32_elf_relax_section.  */
11711
11712 static void
11713 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11714                       asection *sec, Elf_Internal_Rela *irel,
11715                       Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11716                       Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11717                       struct elf_nds32_link_hash_table *table)
11718 {
11719   uint32_t insn;
11720   bfd_vma local_sda, laddr;
11721   unsigned long reloc;
11722   bfd_vma access_addr;
11723   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
11724   Elf_Internal_Rela *irelfn = NULL, *irelend;
11725   struct elf_link_hash_entry *h = NULL;
11726   int indx;
11727
11728   /* For SDA base relative relaxation.  */
11729   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11730                             &local_sda, FALSE);
11731
11732   irelend = internal_relocs + sec->reloc_count;
11733   laddr = irel->r_offset;
11734   insn = bfd_getb32 (contents + laddr);
11735
11736   if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11737     return;
11738
11739   access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11740
11741   if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11742     {
11743       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11744       h = elf_sym_hashes (abfd)[indx];
11745     }
11746
11747   /* Try movi.  */
11748   if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11749       && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11750     {
11751       reloc = R_NDS32_20_RELA;
11752       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11753       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11754       bfd_putb32 (insn, contents + laddr);
11755     }
11756   else
11757     {
11758       if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11759         {
11760           /* Fall through.  */
11761         }
11762       else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11763                                        FALSE, table, isymbuf, symtab_hdr))
11764         return;
11765
11766       range_l = sdata_range[1][0];
11767       range_h = sdata_range[1][1];
11768       switch (ELF32_R_TYPE (irel->r_info))
11769         {
11770         case R_NDS32_LO12S0_RELA:
11771           reloc = R_NDS32_SDA19S0_RELA;
11772           break;
11773         case R_NDS32_LO12S1_RELA:
11774           reloc = R_NDS32_SDA18S1_RELA;
11775           break;
11776         case R_NDS32_LO12S2_RELA:
11777           reloc = R_NDS32_SDA17S2_RELA;
11778           break;
11779         case R_NDS32_LO12S2_DP_RELA:
11780           range_l = sdata_range[0][0];
11781           range_h = sdata_range[0][1];
11782           reloc = R_NDS32_SDA12S2_DP_RELA;
11783           break;
11784         case R_NDS32_LO12S2_SP_RELA:
11785           range_l = sdata_range[0][0];
11786           range_h = sdata_range[0][1];
11787           reloc = R_NDS32_SDA12S2_SP_RELA;
11788           break;
11789         default:
11790           return;
11791         }
11792
11793       /* There are range_h and range_l because linker has to promise
11794          all sections move cross one page together.  */
11795       if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11796           || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11797           || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11798         {
11799           if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11800             {
11801               /* Maybe we should add R_NDS32_INSN16 reloc type here
11802                  or manually do some optimization.  sethi can't be
11803                  eliminated when updating $gp so the relative ori
11804                  needs to be preserved.  */
11805               return;
11806             }
11807           if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11808                                         &insn))
11809             return;
11810           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11811           bfd_putb32 (insn, contents + laddr);
11812
11813           irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11814                                            R_NDS32_INSN16);
11815           /* SDA17 must keep INSN16 for converting fp_as_gp.  */
11816           if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11817             irelfn->r_info =
11818               ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11819
11820         }
11821     }
11822   return;
11823 }
11824
11825 /* Relax PTR relocation for nds32_elf_relax_section.  */
11826
11827 static bfd_boolean
11828 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11829                      Elf_Internal_Rela *internal_relocs, int *insn_len,
11830                      int *seq_len, bfd_byte *contents)
11831 {
11832   Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11833
11834   irelend = internal_relocs + sec->reloc_count;
11835
11836   re_irel =
11837     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11838                                  R_NDS32_PTR_RESOLVED, irel->r_addend);
11839
11840   if (re_irel == irelend)
11841     {
11842       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11843                           (uint64_t) irel->r_offset);
11844       return FALSE;
11845     }
11846
11847   if (re_irel->r_addend != 1)
11848     return FALSE;
11849
11850   /* Pointed target is relaxed and no longer needs this void *,
11851      change the type to NONE.  */
11852   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11853
11854   /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11855      not exist, it means only count 1 and remove it directly.  */
11856   /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11857   count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11858                                        R_NDS32_PTR_COUNT);
11859   ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11860                                      R_NDS32_PTR);
11861   if (count_irel != irelend)
11862     {
11863       if (--count_irel->r_addend > 0)
11864         return FALSE;
11865     }
11866
11867   if (ptr_irel != irelend)
11868     return FALSE;
11869
11870   /* If the PTR_COUNT is already 0, remove current instruction.  */
11871   *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11872   *insn_len = 0;
11873   return TRUE;
11874 }
11875
11876 /* Relax LWC relocation for nds32_elf_relax_section.  */
11877
11878 static void
11879 nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11880                       asection *sec, Elf_Internal_Rela *irel,
11881                       Elf_Internal_Rela *internal_relocs,
11882                       bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11883                       Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11884 {
11885   /* Pattern:
11886      sethi    ra, hi20(symbol)      ; HI20/LOADSTORE
11887      ori      ra, ra, lo12(symbol)  ; LO12S0/PTR/PTR/.../INSN16
11888      flsi     fsa, [ra + offset1]   ; LSI/PTR_RESOLVED/INSN16
11889      flsi     fsb, [ra + offset2]   ; LSI/PTR_RESOLVED/INSN16
11890      ...  */
11891
11892   uint32_t insn;
11893   bfd_vma local_sda, laddr;
11894   unsigned long reloc;
11895   bfd_vma access_addr, flsi_offset;
11896   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
11897   Elf_Internal_Rela *irelend, *re_irel;
11898   unsigned int opcode;
11899
11900   irelend = internal_relocs + sec->reloc_count;
11901   laddr = irel->r_offset;
11902   insn = bfd_getb32 (contents + laddr);
11903
11904   if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11905     return;
11906
11907   /* Can not do relaxation for bi format.  */
11908   if ((insn & 0x1000))
11909     return;
11910
11911   /* Only deal with flsi, fssi, fldi, fsdi, so far.  */
11912   opcode = N32_OP6 (insn);
11913   if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11914     reloc = R_NDS32_SDA12S2_SP_RELA;
11915   else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11916     reloc = R_NDS32_SDA12S2_DP_RELA;
11917   else
11918     return;
11919
11920   re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11921                                     R_NDS32_PTR_RESOLVED);
11922   if (re_irel == irelend)
11923     {
11924       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11925                           (uint64_t) irel->r_offset);
11926       return;
11927     }
11928
11929   /* For SDA base relative relaxation.  */
11930   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11931                             &local_sda, FALSE);
11932   access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11933   flsi_offset = (insn & 0xfff) << 2;
11934   access_addr += flsi_offset;
11935   range_l = sdata_range[0][0];
11936   range_h = sdata_range[0][1];
11937
11938   if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11939       || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11940     {
11941       /* Turn flsi instruction into sda access format.  */
11942       insn = (insn & 0x7ff07000) | (REG_GP << 15);
11943
11944       /* Add relocation type to flsi.  */
11945       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11946       irel->r_addend += flsi_offset;
11947       bfd_putb32 (insn, contents + re_irel->r_offset);
11948
11949       re_irel->r_addend |= 1;
11950       *again = TRUE;
11951     }
11952 }
11953
11954 static bfd_boolean
11955 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11956                           Elf_Internal_Rela *internal_relocs,
11957                           bfd_byte *contents,
11958                           nds32_elf_blank_t **relax_blank_list,
11959                           int optimize, int opt_size)
11960 {
11961   /* This code block is used to adjust 4-byte alignment by relax a pair
11962      of instruction a time.
11963
11964      It recognizes three types of relocations.
11965      1. R_NDS32_LABEL - a alignment.
11966      2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11967      3. is_16bit_NOP () - remove a 16-bit instruction.  */
11968
11969   /* TODO: It seems currently implementation only support 4-byte alignment.
11970      We should handle any-alignment.  */
11971
11972   Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11973   Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11974   Elf_Internal_Rela rel_temp;
11975   Elf_Internal_Rela *irelend;
11976   bfd_vma address;
11977   uint16_t insn16;
11978
11979   /* Checking for branch relaxation relies on the relocations to
11980      be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11981   nds32_insertion_sort (internal_relocs, sec->reloc_count,
11982                         sizeof (Elf_Internal_Rela), compar_reloc);
11983
11984   irelend = internal_relocs + sec->reloc_count;
11985
11986   /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11987   /* FIXME: Can we generate the right order in assembler?
11988      So we don't have to swapping them here.  */
11989
11990   for (label_rel = internal_relocs, insn_rel = internal_relocs;
11991        label_rel < irelend; label_rel++)
11992     {
11993       if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11994         continue;
11995
11996       /* Find the first reloc has the same offset with label_rel.  */
11997       while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11998         insn_rel++;
11999
12000       for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
12001            insn_rel++)
12002         /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
12003            address.  */
12004         if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
12005           break;
12006
12007       if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
12008           && insn_rel < label_rel)
12009         {
12010           /* Swap the two reloc if the R_NDS32_INSN16 is
12011              before R_NDS32_LABEL.  */
12012           memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
12013           memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
12014           memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
12015         }
12016     }
12017
12018   label_rel = NULL;
12019   insn_rel = NULL;
12020   /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12021      or higher, remove other R_NDS32_LABEL with lower alignment.
12022      If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12023      then the R_NDS32_LABEL sequence is broke.  */
12024   for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12025     {
12026       if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12027         {
12028           if (label_rel == NULL)
12029             {
12030               if (tmp_rel->r_addend < 2)
12031                 label_rel = tmp_rel;
12032               continue;
12033             }
12034           else if (tmp_rel->r_addend > 1)
12035             {
12036               /* Remove all LABEL relocation from label_rel to tmp_rel
12037                  including relocations with same offset as tmp_rel.  */
12038               for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
12039                 {
12040                   if (tmp2_rel->r_offset == tmp_rel->r_offset)
12041                     break;
12042
12043                   if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12044                       && tmp2_rel->r_addend < 2)
12045                     tmp2_rel->r_info =
12046                       ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12047                                     R_NDS32_NONE);
12048                 }
12049               label_rel = NULL;
12050             }
12051         }
12052       else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12053         {
12054           /* A new INSN16 which can be converted, so clear label_rel.  */
12055           if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12056                                    irelend, &insn16)
12057               || is_16bit_NOP (abfd, sec, tmp_rel))
12058             label_rel = NULL;
12059         }
12060     }
12061
12062   label_rel = NULL;
12063   insn_rel = NULL;
12064   /* Optimized for speed and nothing has not been relaxed.
12065      It's time to align labels.
12066      We may convert a 16-bit instruction right before a label to
12067      32-bit, in order to align the label if necessary
12068      all reloc entries has been sorted by r_offset.  */
12069   for (irel = internal_relocs;
12070        irel < irelend && irel->r_offset < sec->size; irel++)
12071     {
12072       if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12073           && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12074         continue;
12075
12076       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12077         {
12078           /* A new INSN16 found, resize the old one.  */
12079           if (is_convert_32_to_16
12080               (abfd, sec, irel, internal_relocs, irelend, &insn16)
12081               || is_16bit_NOP (abfd, sec, irel))
12082             {
12083               if (insn_rel)
12084                 {
12085                   /* Previous INSN16 reloc exists, reduce its
12086                      size to 16-bit.  */
12087                   if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12088                                            irelend, &insn16))
12089                     {
12090                       nds32_elf_write_16 (abfd, contents, insn_rel,
12091                                           internal_relocs, irelend, insn16);
12092
12093                       if (!insert_nds32_elf_blank_recalc_total
12094                           (relax_blank_list, insn_rel->r_offset + 2, 2))
12095                         return FALSE;
12096                     }
12097                   else if (is_16bit_NOP (abfd, sec, insn_rel))
12098                     {
12099                       if (!insert_nds32_elf_blank_recalc_total
12100                           (relax_blank_list, insn_rel->r_offset, 2))
12101                         return FALSE;
12102                     }
12103                   insn_rel->r_info =
12104                     ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12105                 }
12106               /* Save the new one for later use.  */
12107               insn_rel = irel;
12108             }
12109           else
12110             irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12111                                          R_NDS32_NONE);
12112         }
12113       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12114         {
12115           /* Search for label.  */
12116           int force_relax = 0;
12117
12118           /* Label on 16-bit instruction or optimization
12119              needless, just reset this reloc.  */
12120           insn16 = bfd_getb16 (contents + irel->r_offset);
12121           if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12122             {
12123               irel->r_info =
12124                 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12125               continue;
12126             }
12127
12128           address =
12129             irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12130                                                         irel->r_offset, 1);
12131
12132           if (!insn_rel)
12133             {
12134               /* Check if there is case which can not be aligned.  */
12135               if (irel->r_addend == 2 && address & 0x2)
12136                 return FALSE;
12137               continue;
12138             }
12139
12140           /* Try to align this label.  */
12141
12142           if ((irel->r_addend & 0x1f) < 2)
12143             {
12144               /* Check if there is a INSN16 at the same address.
12145                  Label_rel always seats before insn_rel after
12146                  our sort.  */
12147
12148               /* Search for INSN16 at LABEL location.  If INSN16 is at
12149                  same location and this LABEL alignment is lower than 2,
12150                  the INSN16 can be converted to 2-byte.  */
12151               for (tmp_rel = irel;
12152                    tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12153                    tmp_rel++)
12154                 {
12155                   if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12156                       && (is_convert_32_to_16
12157                           (abfd, sec, tmp_rel, internal_relocs,
12158                            irelend, &insn16)
12159                           || is_16bit_NOP (abfd, sec, tmp_rel)))
12160                     {
12161                       force_relax = 1;
12162                       break;
12163                     }
12164                 }
12165             }
12166
12167           if (force_relax || irel->r_addend == 1 || address & 0x2)
12168             {
12169               /* Label not aligned.  */
12170               /* Previous reloc exists, reduce its size to 16-bit.  */
12171               if (is_convert_32_to_16 (abfd, sec, insn_rel,
12172                                        internal_relocs, irelend, &insn16))
12173                 {
12174                   nds32_elf_write_16 (abfd, contents, insn_rel,
12175                                       internal_relocs, irelend, insn16);
12176
12177                   if (!insert_nds32_elf_blank_recalc_total
12178                       (relax_blank_list, insn_rel->r_offset + 2, 2))
12179                     return FALSE;
12180                 }
12181               else if (is_16bit_NOP (abfd, sec, insn_rel))
12182                 {
12183                   if (!insert_nds32_elf_blank_recalc_total
12184                       (relax_blank_list, insn_rel->r_offset, 2))
12185                     return FALSE;
12186                 }
12187
12188             }
12189           /* INSN16 reloc is used.  */
12190           insn_rel = NULL;
12191         }
12192     }
12193
12194   address =
12195     sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12196   if (insn_rel && (address & 0x2 || opt_size))
12197     {
12198       if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12199                                irelend, &insn16))
12200         {
12201           nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12202                               irelend, insn16);
12203           if (!insert_nds32_elf_blank_recalc_total
12204               (relax_blank_list, insn_rel->r_offset + 2, 2))
12205             return FALSE;
12206           insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12207                                            R_NDS32_NONE);
12208         }
12209       else if (is_16bit_NOP (abfd, sec, insn_rel))
12210         {
12211           if (!insert_nds32_elf_blank_recalc_total
12212               (relax_blank_list, insn_rel->r_offset, 2))
12213             return FALSE;
12214           insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12215                                            R_NDS32_NONE);
12216         }
12217     }
12218   insn_rel = NULL;
12219   return TRUE;
12220 }
12221
12222 static bfd_boolean
12223 nds32_elf_relax_section (bfd *abfd, asection *sec,
12224                          struct bfd_link_info *link_info, bfd_boolean *again)
12225 {
12226   nds32_elf_blank_t *relax_blank_list = NULL;
12227   Elf_Internal_Shdr *symtab_hdr;
12228   Elf_Internal_Rela *internal_relocs;
12229   Elf_Internal_Rela *irel;
12230   Elf_Internal_Rela *irelend;
12231   Elf_Internal_Sym *isymbuf = NULL;
12232   bfd_byte *contents = NULL;
12233   bfd_boolean result = TRUE;
12234   int optimize = 0;
12235   int opt_size = 0;
12236   uint32_t insn;
12237   uint16_t insn16;
12238
12239   /* Target dependnet option.  */
12240   struct elf_nds32_link_hash_table *table;
12241   int load_store_relax;
12242
12243   relax_blank_list = NULL;
12244
12245   *again = FALSE;
12246
12247   /* Nothing to do for
12248    * relocatable link or
12249    * non-relocatable section or
12250    * non-code section or
12251    * empty content or
12252    * no reloc entry.  */
12253   if (bfd_link_relocatable (link_info)
12254       || (sec->flags & SEC_RELOC) == 0
12255       || (sec->flags & SEC_EXCLUDE) != 0
12256       || (sec->flags & SEC_CODE) == 0
12257       || sec->size == 0
12258       || sec->reloc_count == 0)
12259     return TRUE;
12260
12261   /* 09.12.11 Workaround.  */
12262   /*  We have to adjust align for R_NDS32_LABEL if needed.
12263       The adjust approach only can fix 2-byte align once.  */
12264   if (sec->alignment_power > 2)
12265     return TRUE;
12266
12267   /* Do TLS model conversion once at first.  */
12268   nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12269
12270   /* The optimization type to do.  */
12271
12272   table = nds32_elf_hash_table (link_info);
12273
12274   /* Save the first section for abs symbol relaxation.
12275      This is used for checking gp relaxation in the
12276      nds32_elf_relax_loadstore and nds32_elf_relax_lo12.  */
12277   nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12278                          table, NULL, NULL);
12279
12280   /* The begining of general relaxation.  */
12281
12282   if (is_SDA_BASE_set == 0)
12283     {
12284       bfd_vma gp;
12285       is_SDA_BASE_set = 1;
12286       nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12287                                 &gp, FALSE);
12288       relax_range_measurement (abfd);
12289     }
12290
12291   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12292   /* Relocations MUST be kept in memory, because relaxation adjust them.  */
12293   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12294                                                TRUE /* keep_memory */);
12295   if (internal_relocs == NULL)
12296     goto error_return;
12297
12298   irelend = internal_relocs + sec->reloc_count;
12299   irel = find_relocs_at_address (internal_relocs, internal_relocs,
12300                                  irelend, R_NDS32_RELAX_ENTRY);
12301
12302   if (irel == irelend)
12303     return TRUE;
12304
12305   if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12306     {
12307       if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12308         return TRUE;
12309
12310       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12311         optimize = 1;
12312
12313       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12314         opt_size = 1;
12315     }
12316
12317   load_store_relax = table->load_store_relax;
12318
12319   /* Get symbol table and section content.  */
12320   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12321       || !nds32_get_local_syms (abfd, sec, &isymbuf))
12322     goto error_return;
12323
12324   /* Do relax loop only when finalize is not done.
12325      Take care of relaxable relocs except INSN16.  */
12326   for (irel = internal_relocs; irel < irelend; irel++)
12327     {
12328       int seq_len;              /* Original length of instruction sequence.  */
12329       int insn_len = 0;         /* Final length of instruction sequence.  */
12330       bfd_boolean removed;
12331
12332       insn = 0;
12333       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12334           && (irel->r_addend & 0x1f) >= 2)
12335         optimize = 1;
12336
12337       /* Relocation Types
12338          R_NDS32_LONGCALL1      53
12339          R_NDS32_LONGCALL2      54
12340          R_NDS32_LONGCALL3      55
12341          R_NDS32_LONGJUMP1      56
12342          R_NDS32_LONGJUMP2      57
12343          R_NDS32_LONGJUMP3      58
12344          R_NDS32_LOADSTORE      59  */
12345       if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12346           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12347         seq_len = GET_SEQ_LEN (irel->r_addend);
12348
12349       /* Relocation Types
12350          R_NDS32_LONGCALL4      107
12351          R_NDS32_LONGCALL5      108
12352          R_NDS32_LONGCALL6      109
12353          R_NDS32_LONGJUMP4      110
12354          R_NDS32_LONGJUMP5      111
12355          R_NDS32_LONGJUMP6      112
12356          R_NDS32_LONGJUMP7      113  */
12357       else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12358                && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12359         seq_len = 4;
12360
12361         /* Relocation Types
12362          R_NDS32_LO12S0_RELA            30
12363          R_NDS32_LO12S1_RELA            29
12364          R_NDS32_LO12S2_RELA            28
12365          R_NDS32_LO12S2_SP_RELA         71
12366          R_NDS32_LO12S2_DP_RELA         70
12367          R_NDS32_GOT_LO12               46
12368          R_NDS32_GOTOFF_LO12            50
12369          R_NDS32_PLTREL_LO12            65
12370          R_NDS32_PLT_GOTREL_LO12        67
12371          R_NDS32_17IFC_PCREL_RELA       96
12372          R_NDS32_GOT_SUFF               193
12373          R_NDS32_GOTOFF_SUFF            194
12374          R_NDS32_PLT_GOT_SUFF           195
12375          R_NDS32_MULCALL_SUFF           196
12376          R_NDS32_PTR                    197  */
12377       else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12378                 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12379                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12380                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12381                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12382                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12383                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12384                || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12385                || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12386                || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12387                    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12388                || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12389                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12390                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12391                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12392                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12393         seq_len = 0;
12394       else
12395         continue;
12396
12397       insn_len = seq_len;
12398       removed = FALSE;
12399
12400       switch (ELF32_R_TYPE (irel->r_info))
12401         {
12402         case R_NDS32_LONGCALL1:
12403           removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12404                                                &insn_len, contents, isymbuf,
12405                                                symtab_hdr);
12406           break;
12407         case R_NDS32_LONGCALL2:
12408           removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12409                                                &insn_len, contents, isymbuf,
12410                                                symtab_hdr);
12411           break;
12412         case R_NDS32_LONGCALL3:
12413           removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12414                                                &insn_len, contents, isymbuf,
12415                                                symtab_hdr);
12416           break;
12417         case R_NDS32_LONGJUMP1:
12418           removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12419                                                &insn_len, contents, isymbuf,
12420                                                symtab_hdr);
12421           break;
12422         case R_NDS32_LONGJUMP2:
12423           removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12424                                                &insn_len, contents, isymbuf,
12425                                                symtab_hdr);
12426           break;
12427         case R_NDS32_LONGJUMP3:
12428           removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12429                                                &insn_len, contents, isymbuf,
12430                                                symtab_hdr);
12431           break;
12432         case R_NDS32_LONGCALL4:
12433           removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12434                                                &insn_len, contents, isymbuf,
12435                                                symtab_hdr);
12436           break;
12437         case R_NDS32_LONGCALL5:
12438           removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12439                                                &insn_len, contents, isymbuf,
12440                                                symtab_hdr);
12441           break;
12442         case R_NDS32_LONGCALL6:
12443           removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12444                                                &insn_len, contents, isymbuf,
12445                                                symtab_hdr);
12446           break;
12447         case R_NDS32_LONGJUMP4:
12448           removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12449                                                &insn_len, contents, isymbuf,
12450                                                symtab_hdr);
12451           break;
12452         case R_NDS32_LONGJUMP5:
12453           removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12454                                                &insn_len, &seq_len, contents,
12455                                                isymbuf, symtab_hdr);
12456           break;
12457         case R_NDS32_LONGJUMP6:
12458           removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12459                                                &insn_len, &seq_len, contents,
12460                                                isymbuf, symtab_hdr);
12461           break;
12462         case R_NDS32_LONGJUMP7:
12463           removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12464                                                &insn_len, &seq_len, contents,
12465                                                isymbuf, symtab_hdr);
12466           break;
12467         case R_NDS32_LOADSTORE:
12468           removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12469                                                internal_relocs, &insn_len,
12470                                                contents, isymbuf, symtab_hdr,
12471                                                load_store_relax, table);
12472           break;
12473         case R_NDS32_LO12S0_RELA:
12474         case R_NDS32_LO12S1_RELA:
12475         case R_NDS32_LO12S2_RELA:
12476         case R_NDS32_LO12S2_DP_RELA:
12477         case R_NDS32_LO12S2_SP_RELA:
12478           /* Relax for low part.  */
12479           nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12480                                 contents, isymbuf, symtab_hdr, table);
12481
12482           /* It is impossible to delete blank, so just continue.  */
12483           continue;
12484         case R_NDS32_PTR:
12485           removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12486                                          &insn_len, &seq_len, contents);
12487           break;
12488         case R_NDS32_LSI:
12489           nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12490                                 contents, isymbuf, symtab_hdr, again);
12491           continue;
12492         case R_NDS32_GOT_LO12:
12493         case R_NDS32_GOTOFF_LO12:
12494         case R_NDS32_PLTREL_LO12:
12495         case R_NDS32_PLT_GOTREL_LO12:
12496         case R_NDS32_GOTPC_LO12:
12497         case R_NDS32_TLS_LE_LO12:
12498         case R_NDS32_TLS_LE_ADD:
12499         case R_NDS32_TLS_LE_LS:
12500         case R_NDS32_PLT_GOT_SUFF:
12501         case R_NDS32_GOT_SUFF:
12502         case R_NDS32_GOTOFF_SUFF:
12503           continue;
12504         default:
12505           continue;
12506         }
12507
12508       if (removed && seq_len - insn_len > 0)
12509         {
12510           if (!insert_nds32_elf_blank
12511               (&relax_blank_list, irel->r_offset + insn_len,
12512                seq_len - insn_len))
12513             goto error_return;
12514           *again = TRUE;
12515         }
12516     }
12517
12518   calc_nds32_blank_total (relax_blank_list);
12519
12520   if (table->relax_fp_as_gp)
12521     {
12522       if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12523                                  irelend, isymbuf))
12524         goto error_return;
12525
12526       if (!*again)
12527         {
12528           if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12529                                                irelend))
12530             goto error_return;
12531         }
12532     }
12533
12534   if (!*again)
12535     {
12536       if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12537                                      &relax_blank_list, optimize, opt_size))
12538         goto error_return;
12539     }
12540
12541   /* It doesn't matter optimize_for_space_no_align anymore.
12542        If object file is assembled with flag '-Os',
12543        the we don't adjust jump-destination on 4-byte boundary.  */
12544
12545   if (relax_blank_list)
12546     {
12547       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12548       relax_blank_list = NULL;
12549     }
12550
12551   if (!*again)
12552     {
12553       /* Closing the section, so we don't relax it anymore.  */
12554       bfd_vma sec_size_align;
12555       Elf_Internal_Rela *tmp_rel;
12556
12557       /* Pad to alignment boundary.  Only handle current section alignment.  */
12558       sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12559                        & ((-1U) << sec->alignment_power);
12560       if ((sec_size_align - sec->size) & 0x2)
12561         {
12562           insn16 = NDS32_NOP16;
12563           bfd_putb16 (insn16, contents + sec->size);
12564           sec->size += 2;
12565         }
12566
12567       while (sec_size_align != sec->size)
12568         {
12569           insn = NDS32_NOP32;
12570           bfd_putb32 (insn, contents + sec->size);
12571           sec->size += 4;
12572         }
12573
12574       tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12575                                         irelend, R_NDS32_RELAX_ENTRY);
12576       if (tmp_rel != irelend)
12577         tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12578
12579       clean_nds32_elf_blank ();
12580     }
12581
12582 finish:
12583   if (internal_relocs != NULL
12584       && elf_section_data (sec)->relocs != internal_relocs)
12585     free (internal_relocs);
12586
12587   if (contents != NULL
12588       && elf_section_data (sec)->this_hdr.contents != contents)
12589     free (contents);
12590
12591   if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12592     free (isymbuf);
12593
12594   return result;
12595
12596 error_return:
12597   result = FALSE;
12598   goto finish;
12599 }
12600
12601 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12602 {
12603   {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12604   {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12605   {NULL, 0, 0, 0, 0}
12606 };
12607
12608 static bfd_boolean
12609 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12610                             struct bfd_link_info *info,
12611                             void *finfo ATTRIBUTE_UNUSED,
12612                             bfd_boolean (*func) (void *, const char *,
12613                                                  Elf_Internal_Sym *,
12614                                                  asection *,
12615                                                  struct elf_link_hash_entry *)
12616                             ATTRIBUTE_UNUSED)
12617 {
12618   FILE *sym_ld_script = NULL;
12619   struct elf_nds32_link_hash_table *table;
12620
12621   table = nds32_elf_hash_table (info);
12622   sym_ld_script = table->sym_ld_script;
12623
12624   if (check_start_export_sym)
12625     fprintf (sym_ld_script, "}\n");
12626
12627   return TRUE;
12628 }
12629
12630 static enum elf_reloc_type_class
12631 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12632                             const asection *rel_sec ATTRIBUTE_UNUSED,
12633                             const Elf_Internal_Rela *rela)
12634 {
12635   switch ((int) ELF32_R_TYPE (rela->r_info))
12636     {
12637     case R_NDS32_RELATIVE:
12638       return reloc_class_relative;
12639     case R_NDS32_JMP_SLOT:
12640       return reloc_class_plt;
12641     case R_NDS32_COPY:
12642       return reloc_class_copy;
12643     default:
12644       return reloc_class_normal;
12645     }
12646 }
12647
12648 /* Put target dependent option into info hash table.  */
12649 void
12650 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12651                                    int relax_fp_as_gp,
12652                                    int eliminate_gc_relocs,
12653                                    FILE * sym_ld_script,
12654                                    int hyper_relax,
12655                                    int tls_desc_trampoline,
12656                                    int load_store_relax)
12657 {
12658   struct elf_nds32_link_hash_table *table;
12659
12660   table = nds32_elf_hash_table (link_info);
12661   if (table == NULL)
12662     return;
12663
12664   table->relax_fp_as_gp = relax_fp_as_gp;
12665   table->eliminate_gc_relocs = eliminate_gc_relocs;
12666   table->sym_ld_script = sym_ld_script;
12667   table->hyper_relax = hyper_relax;
12668   table->tls_desc_trampoline = tls_desc_trampoline;
12669   table ->load_store_relax = load_store_relax;
12670 }
12671 \f
12672
12673 /* These functions and data-structures are used for fp-as-gp
12674    optimization.  */
12675
12676 #define FAG_THRESHOLD   3       /* At least 3 gp-access.  */
12677 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12678    the read-only section and read-write section.  */
12679 #define FAG_WINDOW      (508 - 32)
12680
12681 /* An nds32_fag represent a gp-relative access.
12682    We find best fp-base by using a sliding window
12683    to find a base address which can cover most gp-access.  */
12684 struct nds32_fag
12685 {
12686   struct nds32_fag *next;       /* NULL-teminated linked list.  */
12687   bfd_vma addr;                 /* The address of this fag.  */
12688   Elf_Internal_Rela **relas;    /* The relocations associated with this fag.
12689                                    It is used for applying FP7U2_FLAG.  */
12690   int count;                    /* How many times this address is referred.
12691                                    There should be exactly `count' relocations
12692                                    in relas.  */
12693   int relas_capcity;            /* The buffer size of relas.
12694                                    We use an array instead of linked-list,
12695                                    and realloc is used to adjust buffer size.  */
12696 };
12697
12698 static void
12699 nds32_fag_init (struct nds32_fag *head)
12700 {
12701   memset (head, 0, sizeof (struct nds32_fag));
12702 }
12703
12704 static void
12705 nds32_fag_verify (struct nds32_fag *head)
12706 {
12707   struct nds32_fag *iter;
12708   struct nds32_fag *prev;
12709
12710   prev = NULL;
12711   iter = head->next;
12712   while (iter)
12713     {
12714       if (prev && prev->addr >= iter->addr)
12715         puts ("Bug in fp-as-gp insertion.");
12716       prev = iter;
12717       iter = iter->next;
12718     }
12719 }
12720
12721 /* Insert a fag in ascending order.
12722    If a fag of the same address already exists,
12723    they are chained by relas array.  */
12724
12725 static void
12726 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12727                   Elf_Internal_Rela * rel)
12728 {
12729   struct nds32_fag *iter;
12730   struct nds32_fag *new_fag;
12731   const int INIT_RELAS_CAP = 4;
12732
12733   for (iter = head;
12734        iter->next && iter->next->addr <= addr;
12735        iter = iter->next)
12736     /* Find somewhere to insert.  */ ;
12737
12738   /* `iter' will be equal to `head' if the list is empty.  */
12739   if (iter != head && iter->addr == addr)
12740     {
12741       /* The address exists in the list.
12742          Insert `rel' into relocation list, relas.  */
12743
12744       /* Check whether relas is big enough.  */
12745       if (iter->count >= iter->relas_capcity)
12746         {
12747           iter->relas_capcity *= 2;
12748           iter->relas = bfd_realloc
12749             (iter->relas, iter->relas_capcity * sizeof (void *));
12750         }
12751       iter->relas[iter->count++] = rel;
12752       return;
12753     }
12754
12755   /* This is a new address.  Create a fag node for it.  */
12756   new_fag = bfd_malloc (sizeof (struct nds32_fag));
12757   memset (new_fag, 0, sizeof (*new_fag));
12758   new_fag->addr = addr;
12759   new_fag->count = 1;
12760   new_fag->next = iter->next;
12761   new_fag->relas_capcity = INIT_RELAS_CAP;
12762   new_fag->relas = (Elf_Internal_Rela **)
12763     bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12764   new_fag->relas[0] = rel;
12765   iter->next = new_fag;
12766
12767   nds32_fag_verify (head);
12768 }
12769
12770 static void
12771 nds32_fag_free_list (struct nds32_fag *head)
12772 {
12773   struct nds32_fag *iter;
12774
12775   iter = head->next;
12776   while (iter)
12777     {
12778       struct nds32_fag *tmp = iter;
12779       iter = iter->next;
12780       free (tmp->relas);
12781       tmp->relas = NULL;
12782       free (tmp);
12783     }
12784 }
12785
12786 /* Find the best fp-base address.
12787    The relocation associated with that address is returned,
12788    so we can track the symbol instead of a fixed address.
12789
12790    When relaxation, the address of an datum may change,
12791    because a text section is shrinked, so the data section
12792    moves forward.  If the aligments of text and data section
12793    are different, their distance may change too.
12794    Therefore, tracking a fixed address is not appriate.  */
12795
12796 static int
12797 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12798 {
12799   struct nds32_fag *base;       /* First fag in the window.  */
12800   struct nds32_fag *last;       /* First fag outside the window.  */
12801   int accu = 0;                 /* Usage accumulation.  */
12802   struct nds32_fag *best;       /* Best fag.  */
12803   int baccu = 0;                /* Best accumulation.  */
12804
12805   /* Use first fag for initial, and find the last fag in the window.
12806
12807      In each iteration, we could simply subtract previous fag
12808      and accumulate following fags which are inside the window,
12809      untill we each the end.  */
12810
12811   if (head->next == NULL)
12812     {
12813       *bestpp = NULL;
12814       return 0;
12815     }
12816
12817   /* Initialize base.  */
12818   base = head->next;
12819   best = base;
12820   for (last = base;
12821        last && last->addr < base->addr + FAG_WINDOW;
12822        last = last->next)
12823     accu += last->count;
12824
12825   baccu = accu;
12826
12827   /* Record the best base in each iteration.  */
12828   while (base->next)
12829     {
12830       accu -= base->count;
12831       base = base->next;
12832       /* Account fags in window.  */
12833       for (/* Nothing.  */;
12834            last && last->addr < base->addr + FAG_WINDOW;
12835            last = last->next)
12836         accu += last->count;
12837
12838       /* A better fp-base?  */
12839       if (accu > baccu)
12840         {
12841           best = base;
12842           baccu = accu;
12843         }
12844     }
12845
12846   if (bestpp)
12847     *bestpp = best;
12848   return baccu;
12849 }
12850
12851 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12852    so we can convert it fo fp-relative access later.
12853    `best_fag' is the best fp-base.  Only those inside the window
12854    of best_fag is applied the flag.  */
12855
12856 static bfd_boolean
12857 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12858                       asection *sec, struct nds32_fag *best_fag,
12859                       Elf_Internal_Rela *internal_relocs,
12860                       Elf_Internal_Rela *irelend)
12861 {
12862   struct nds32_fag *ifag;
12863   bfd_vma best_fpbase, gp;
12864   bfd *output_bfd;
12865
12866   output_bfd = sec->output_section->owner;
12867   nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12868   best_fpbase = best_fag->addr;
12869
12870   if (best_fpbase > gp + sdata_range[1][1]
12871       || best_fpbase < gp - sdata_range[1][0])
12872     return FALSE;
12873
12874   /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12875      so we know they can be converted to lwi37.fp.   */
12876   for (ifag = best_fag;
12877        ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12878     {
12879       int i;
12880
12881       for (i = 0; i < ifag->count; i++)
12882         {
12883           Elf_Internal_Rela *insn16_rel;
12884           Elf_Internal_Rela *fag_rel;
12885
12886           fag_rel = ifag->relas[i];
12887
12888           /* Only if this is within the WINDOWS, FP7U2_FLAG
12889              is applied.  */
12890
12891           insn16_rel = find_relocs_at_address
12892             (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12893
12894           if (insn16_rel != irelend)
12895             insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12896         }
12897     }
12898   return TRUE;
12899 }
12900
12901 /* Reset INSN16 to clean fp as gp.  */
12902
12903 static void
12904 nds32_fag_unmark_relax (struct nds32_fag *fag,
12905                         Elf_Internal_Rela *internal_relocs,
12906                         Elf_Internal_Rela *irelend)
12907 {
12908   struct nds32_fag *ifag;
12909   int i;
12910   Elf_Internal_Rela *insn16_rel;
12911   Elf_Internal_Rela *fag_rel;
12912
12913   for (ifag = fag; ifag; ifag = ifag->next)
12914     {
12915       for (i = 0; i < ifag->count; i++)
12916         {
12917           fag_rel = ifag->relas[i];
12918
12919           /* Restore the INSN16 relocation.  */
12920           insn16_rel = find_relocs_at_address
12921             (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12922
12923           if (insn16_rel != irelend)
12924             insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12925         }
12926     }
12927 }
12928
12929 /* This is the main function of fp-as-gp optimization.
12930    It should be called by relax_section.  */
12931
12932 static bfd_boolean
12933 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12934                       bfd *abfd, asection *sec,
12935                       Elf_Internal_Rela *internal_relocs,
12936                       Elf_Internal_Rela *irelend,
12937                       Elf_Internal_Sym *isymbuf)
12938 {
12939   Elf_Internal_Rela *begin_rel = NULL;
12940   Elf_Internal_Rela *irel;
12941   struct nds32_fag fag_head;
12942   Elf_Internal_Shdr *symtab_hdr;
12943   bfd_byte *contents;
12944   bfd_boolean ifc_inside = FALSE;
12945
12946   /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12947
12948   /* Per-function fp-base selection.
12949      1. Create a list for all the gp-relative access.
12950      2. Base on those gp-relative address,
12951         find a fp-base which can cover most access.
12952      3. Use the fp-base for fp-as-gp relaxation.
12953
12954      NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12955      we should
12956      1. delete the `la $fp, _FP_BASE_' instruction and
12957      2. not convert lwi.gp to lwi37.fp.
12958
12959      To delete the _FP_BASE_ instruction, we simply apply
12960      R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12961
12962      To suppress the conversion, we simply NOT to apply
12963      R_NDS32_INSN16_FP7U2_FLAG flag.  */
12964
12965   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12966
12967   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12968       || !nds32_get_local_syms (abfd, sec, &isymbuf))
12969     return FALSE;
12970
12971   /* Check whether it is worth for fp-as-gp optimization,
12972      i.e., at least 3 gp-load.
12973
12974      Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12975      apply this optimization.  */
12976
12977   for (irel = internal_relocs; irel < irelend; irel++)
12978     {
12979       /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12980          One we enter the begin of the region, we track all the LW/ST
12981          instructions, so when we leave the region, we try to find
12982          the best fp-base address for those LW/ST instructions.  */
12983
12984       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12985           && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12986         {
12987           /* Begin of the region.  */
12988           if (begin_rel)
12989             /* xgettext:c-format */
12990             _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12991
12992           begin_rel = irel;
12993           nds32_fag_init (&fag_head);
12994           ifc_inside = FALSE;
12995         }
12996       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12997                && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12998         {
12999           int accu;
13000           struct nds32_fag *best_fag, *tmp_fag;
13001           int dist;
13002
13003           /* End of the region.
13004              Check whether it is worth to do fp-as-gp.  */
13005
13006           if (begin_rel == NULL)
13007             {
13008               /* xgettext:c-format */
13009               _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
13010                                   abfd, sec);
13011               continue;
13012             }
13013
13014           accu = nds32_fag_find_base (&fag_head, &best_fag);
13015
13016           /* Clean FP7U2_FLAG because they may set ever.  */
13017           tmp_fag = fag_head.next;
13018           nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13019
13020           /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
13021           if (accu < FAG_THRESHOLD
13022               || !nds32_fag_mark_relax (link_info, sec, best_fag,
13023                                         internal_relocs, irelend))
13024             {
13025               /* Not worth to do fp-as-gp.  */
13026               begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13027               begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13028               irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13029               irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13030               nds32_fag_free_list (&fag_head);
13031               begin_rel = NULL;
13032               continue;
13033             }
13034
13035           /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13036              so we use it to record the distance to the reloction of best
13037              fp-base.  */
13038           dist = best_fag->relas[0] - begin_rel;
13039           BFD_ASSERT (dist > 0 && dist < 0xffffff);
13040           /* Use high 16 bits of addend to record the _FP_BASE_ matched
13041              relocation.  And get the base value when relocating.  */
13042           begin_rel->r_addend &= (0x1 << 16) - 1;
13043           begin_rel->r_addend |= dist << 16;
13044
13045           nds32_fag_free_list (&fag_head);
13046           begin_rel = NULL;
13047         }
13048
13049       if (begin_rel == NULL || ifc_inside)
13050         /* Skip if we are not in the region of fp-as-gp.  */
13051         continue;
13052
13053       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13054           || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13055         {
13056           bfd_vma addr;
13057           uint32_t insn;
13058
13059           /* A gp-relative access is found.  Insert it to the fag-list.  */
13060
13061           /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
13062           insn = bfd_getb32 (contents + irel->r_offset);
13063           if (!N32_IS_RT3 (insn))
13064             continue;
13065
13066           addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13067           nds32_fag_insert (&fag_head, addr, irel);
13068         }
13069       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13070         {
13071           begin_rel = NULL;
13072         }
13073       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13074                || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13075         {
13076           /* Suppress fp as gp when encounter ifc.  */
13077           ifc_inside = TRUE;
13078         }
13079     }
13080
13081   return TRUE;
13082 }
13083
13084 /* Remove unused `la $fp, _FD_BASE_' instruction.  */
13085
13086 static bfd_boolean
13087 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13088                                 Elf_Internal_Rela *internal_relocs,
13089                                 Elf_Internal_Rela *irelend)
13090 {
13091   Elf_Internal_Rela *irel;
13092   Elf_Internal_Shdr *symtab_hdr;
13093   bfd_byte *contents = NULL;
13094   nds32_elf_blank_t *relax_blank_list = NULL;
13095   bfd_boolean result = TRUE;
13096   bfd_boolean unused_region = FALSE;
13097
13098   /*
13099      NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13100        R_NDS32_17IFC_PCREL_RELA
13101        R_NDS32_10IFCU_PCREL_RELA.  */
13102
13103   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13104   nds32_get_section_contents (abfd, sec, &contents, TRUE);
13105
13106   for (irel = internal_relocs; irel < irelend; irel++)
13107     {
13108       /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13109          we marked to in previous pass.
13110          DO NOT scan relocations again, since we've alreadly decided it
13111          and set the flag.  */
13112       const char *syname;
13113       int syndx;
13114       uint32_t insn;
13115
13116       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13117           && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13118         unused_region = TRUE;
13119       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13120                && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13121         unused_region = FALSE;
13122
13123       /* We're not in the region.  */
13124       if (!unused_region)
13125         continue;
13126
13127       /* _FP_BASE_ must be a GLOBAL symbol.  */
13128       syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13129       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13130         continue;
13131
13132       /* The symbol name must be _FP_BASE_.  */
13133       syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13134       if (strcmp (syname, FP_BASE_NAME) != 0)
13135         continue;
13136
13137       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13138         {
13139           /* addi.gp  $fp, -256  */
13140           insn = bfd_getb32 (contents + irel->r_offset);
13141           if (insn != INSN_ADDIGP_TO_FP)
13142             continue;
13143         }
13144       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13145         {
13146           /* addi  $fp, $gp, -256  */
13147           insn = bfd_getb32 (contents + irel->r_offset);
13148           if (insn != INSN_ADDI_GP_TO_FP)
13149             continue;
13150         }
13151       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13152         {
13153           /* movi  $fp, FP_BASE  */
13154           insn = bfd_getb32 (contents + irel->r_offset);
13155           if (insn != INSN_MOVI_TO_FP)
13156             continue;
13157         }
13158       else
13159         continue;
13160
13161       /* We got here because a FP_BASE instruction is found.  */
13162       if (!insert_nds32_elf_blank_recalc_total
13163           (&relax_blank_list, irel->r_offset, 4))
13164         goto error_return;
13165     }
13166
13167 finish:
13168   if (relax_blank_list)
13169     {
13170       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13171       relax_blank_list = NULL;
13172     }
13173   return result;
13174
13175 error_return:
13176   result = FALSE;
13177   goto finish;
13178 }
13179
13180 /* This is a version of bfd_generic_get_relocated_section_contents.
13181    We need this variety because relaxation will modify the dwarf
13182    infomation.  When there is undefined symbol reference error mesage,
13183    linker need to dump line number where the symbol be used.  However
13184    the address is be relaxed, it can not get the original dwarf contents.
13185    The variety only modify function call for reading in the section.  */
13186
13187 static bfd_byte *
13188 nds32_elf_get_relocated_section_contents (bfd *abfd,
13189                                           struct bfd_link_info *link_info,
13190                                           struct bfd_link_order *link_order,
13191                                           bfd_byte *data,
13192                                           bfd_boolean relocatable,
13193                                           asymbol **symbols)
13194 {
13195   bfd *input_bfd = link_order->u.indirect.section->owner;
13196   asection *input_section = link_order->u.indirect.section;
13197   long reloc_size;
13198   arelent **reloc_vector;
13199   long reloc_count;
13200
13201   reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13202   if (reloc_size < 0)
13203     return NULL;
13204
13205   /* Read in the section.  */
13206   if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
13207     return NULL;
13208
13209   if (reloc_size == 0)
13210     return data;
13211
13212   reloc_vector = (arelent **) bfd_malloc (reloc_size);
13213   if (reloc_vector == NULL)
13214     return NULL;
13215
13216   reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13217                                         reloc_vector, symbols);
13218   if (reloc_count < 0)
13219     goto error_return;
13220
13221   if (reloc_count > 0)
13222     {
13223       arelent **parent;
13224       for (parent = reloc_vector; *parent != NULL; parent++)
13225         {
13226           char *error_message = NULL;
13227           asymbol *symbol;
13228           bfd_reloc_status_type r;
13229
13230           symbol = *(*parent)->sym_ptr_ptr;
13231           if (symbol->section && discarded_section (symbol->section))
13232             {
13233               bfd_byte *p;
13234               static reloc_howto_type none_howto
13235                 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13236                          "unused", FALSE, 0, 0, FALSE);
13237
13238               p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
13239               _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
13240                                    p);
13241               (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13242               (*parent)->addend = 0;
13243               (*parent)->howto = &none_howto;
13244               r = bfd_reloc_ok;
13245             }
13246           else
13247             r = bfd_perform_relocation (input_bfd, *parent, data,
13248                                         input_section,
13249                                         relocatable ? abfd : NULL,
13250                                         &error_message);
13251
13252           if (relocatable)
13253             {
13254               asection *os = input_section->output_section;
13255
13256               /* A partial link, so keep the relocs.  */
13257               os->orelocation[os->reloc_count] = *parent;
13258               os->reloc_count++;
13259             }
13260
13261           if (r != bfd_reloc_ok)
13262             {
13263               switch (r)
13264                 {
13265                 case bfd_reloc_undefined:
13266                   (*link_info->callbacks->undefined_symbol)
13267                     (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13268                      input_bfd, input_section, (*parent)->address, TRUE);
13269                   break;
13270                 case bfd_reloc_dangerous:
13271                   BFD_ASSERT (error_message != NULL);
13272                   (*link_info->callbacks->reloc_dangerous)
13273                     (link_info, error_message,
13274                      input_bfd, input_section, (*parent)->address);
13275                   break;
13276                 case bfd_reloc_overflow:
13277                   (*link_info->callbacks->reloc_overflow)
13278                     (link_info, NULL,
13279                      bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13280                      (*parent)->howto->name, (*parent)->addend,
13281                      input_bfd, input_section, (*parent)->address);
13282                   break;
13283                 case bfd_reloc_outofrange:
13284                   /* PR ld/13730:
13285                      This error can result when processing some partially
13286                      complete binaries.  Do not abort, but issue an error
13287                      message instead.  */
13288                   link_info->callbacks->einfo
13289                     /* xgettext:c-format */
13290                     (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13291                      abfd, input_section, * parent);
13292                   goto error_return;
13293
13294                 default:
13295                   abort ();
13296                   break;
13297                 }
13298             }
13299         }
13300     }
13301
13302   free (reloc_vector);
13303   return data;
13304
13305 error_return:
13306   free (reloc_vector);
13307   return NULL;
13308 }
13309
13310 /* Check target symbol.  */
13311
13312 static bfd_boolean
13313 nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13314 {
13315   if (!sym || !sym->name || sym->name[0] != '$')
13316     return FALSE;
13317   return TRUE;
13318 }
13319
13320 /* nds32 find maybe function sym.  Ignore target special symbol
13321    first, and then go the general function.  */
13322
13323 static bfd_size_type
13324 nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13325                               bfd_vma *code_off)
13326 {
13327   if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13328     return 0;
13329
13330   return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13331 }
13332 \f
13333
13334 /* Do TLS model conversion.  */
13335
13336 typedef struct relax_group_list_t
13337 {
13338   Elf_Internal_Rela *relo;
13339   struct relax_group_list_t *next;
13340   struct relax_group_list_t *next_sibling;
13341   int id;
13342 } relax_group_list_t;
13343
13344 int
13345 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13346
13347 int
13348 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13349
13350 void
13351 dump_chain (relax_group_list_t *pHead);
13352
13353 int
13354 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13355 {
13356   relax_group_list_t *pNext = pHead;
13357
13358   /* Find place.  */
13359   while (pNext->next)
13360     {
13361       if (pNext->next->id > (int) pElem->r_addend)
13362         break;
13363
13364       pNext = pNext->next;
13365     }
13366
13367   /* Insert node.  */
13368   relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13369   if (!pNew)
13370     return FALSE;
13371
13372   relax_group_list_t *tmp = pNext->next;
13373   pNext->next = pNew;
13374
13375   pNew->id = pElem->r_addend;
13376   pNew->relo = pElem;
13377   pNew->next = tmp;
13378   pNew->next_sibling = NULL;
13379
13380   return TRUE;
13381 }
13382
13383 int
13384 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13385 {
13386   relax_group_list_t *pNext = pNode;
13387
13388   /* Find place.  */
13389   while (pNext->next_sibling)
13390     {
13391       pNext = pNext->next_sibling;
13392     }
13393
13394   /* Insert node.  */
13395   relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13396   if (!pNew)
13397     return FALSE;
13398
13399   relax_group_list_t *tmp = pNext->next_sibling;
13400   pNext->next_sibling = pNew;
13401
13402   pNew->id = -1;
13403   pNew->relo = pElem;
13404   pNew->next = NULL;
13405   pNew->next_sibling = tmp;
13406
13407   return TRUE;
13408 }
13409
13410 void
13411 dump_chain (relax_group_list_t *pHead)
13412 {
13413   relax_group_list_t *pNext = pHead->next;
13414   while (pNext)
13415     {
13416       printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13417       relax_group_list_t *pNextSib = pNext->next_sibling;
13418       while (pNextSib)
13419         {
13420           printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13421           pNextSib = pNextSib->next_sibling;
13422         }
13423       pNext = pNext->next;
13424       printf("\n");
13425     }
13426 }
13427
13428 /* Check R_NDS32_RELAX_GROUP of each section.
13429    There might be multiple sections in one object file.  */
13430
13431 int
13432 elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13433 {
13434   elf32_nds32_relax_group_t *relax_group_ptr =
13435     elf32_nds32_relax_group_ptr (abfd);
13436
13437   int min_id = relax_group_ptr->min_id;
13438   int max_id = relax_group_ptr->max_id;
13439
13440   Elf_Internal_Rela *rel;
13441   Elf_Internal_Rela *relend;
13442   Elf_Internal_Rela *relocs;
13443   enum elf_nds32_reloc_type rtype;
13444
13445   do
13446     {
13447       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13448       relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13449                                           TRUE /* keep_memory  */);
13450       if (relocs == NULL)
13451         break;
13452
13453       /* Check R_NDS32_RELAX_GROUP.  */
13454       relend = relocs + asec->reloc_count;
13455       for (rel = relocs; rel < relend; rel++)
13456         {
13457           int id;
13458           rtype = ELF32_R_TYPE (rel->r_info);
13459           if (rtype != R_NDS32_RELAX_GROUP)
13460             continue;
13461
13462           id = rel->r_addend;
13463           if (id < min_id)
13464             min_id = id;
13465           else if (id > max_id)
13466             max_id = id;
13467         }
13468     }
13469   while (FALSE);
13470
13471   if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs))
13472     free (relocs);
13473
13474   if ((min_id != relax_group_ptr->min_id)
13475       || (max_id != relax_group_ptr->max_id))
13476     {
13477       relax_group_ptr->count = max_id - min_id + 1;
13478       BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13479       relax_group_ptr->min_id = min_id;
13480       BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13481       relax_group_ptr->max_id = max_id;
13482     }
13483
13484   return relax_group_ptr->count;
13485 }
13486
13487 /* Reorder RELAX_GROUP ID when command line option '-r' is applied.  */
13488 struct section_id_list_t *relax_group_section_id_list = NULL;
13489
13490 struct section_id_list_t *
13491 elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13492 {
13493   struct section_id_list_t *result = NULL;
13494   struct section_id_list_t *lst = *lst_ptr;
13495
13496   if (NULL == lst)
13497     {
13498       result = (struct section_id_list_t *) calloc
13499         (1, sizeof (struct section_id_list_t));
13500       BFD_ASSERT (result); /* Feed me.  */
13501       result->id = id;
13502       *lst_ptr = result;
13503     }
13504   else
13505     {
13506       struct section_id_list_t *cur = lst;
13507       struct section_id_list_t *prv = NULL;
13508       struct section_id_list_t *sec = NULL;
13509
13510       while (cur)
13511         {
13512           if (cur->id < id)
13513             {
13514               prv = cur;
13515               cur = cur->next;
13516               continue;
13517             }
13518
13519           if (cur->id > id)
13520             {
13521               cur = NULL; /* To insert after prv.  */
13522               sec = cur;  /* In case prv == NULL.  */
13523             }
13524
13525           break;
13526         }
13527
13528       if (NULL == cur)
13529         {
13530           /* Insert after prv.  */
13531           result = (struct section_id_list_t *) calloc
13532             (1, sizeof (struct section_id_list_t));
13533           BFD_ASSERT (result); /* Feed me.  */
13534           result->id = id;
13535           if (NULL != prv)
13536             {
13537               result->next = prv->next;
13538               prv->next = result;
13539             }
13540           else
13541             {
13542               *lst_ptr = result;
13543               result->next = sec;
13544             }
13545         }
13546     }
13547
13548   return result;
13549 }
13550
13551 int
13552 elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13553 {
13554   static int next_relax_group_bias = 0;
13555
13556   elf32_nds32_relax_group_t *relax_group_ptr =
13557     elf32_nds32_relax_group_ptr (abfd);
13558
13559   bfd_boolean result = TRUE;
13560   Elf_Internal_Rela *rel;
13561   Elf_Internal_Rela *relend;
13562   Elf_Internal_Rela *relocs = NULL;
13563   enum elf_nds32_reloc_type rtype;
13564   struct section_id_list_t *node = NULL;
13565   int count = 0;
13566
13567   do
13568     {
13569       if (0 == relax_group_ptr->count)
13570         break;
13571
13572       /* Check if this section has been handled.  */
13573       node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13574       if (NULL == node)
13575         break; /* Hit, the section id has handled.  */
13576
13577       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13578       relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13579                                           TRUE /* keep_memory  */);
13580       if (relocs == NULL)
13581         {
13582           BFD_ASSERT (0); /* feed me */
13583           break;
13584         }
13585
13586       /* Allocate group id bias for this bfd!  */
13587       if (0 == relax_group_ptr->init)
13588         {
13589           relax_group_ptr->bias = next_relax_group_bias;
13590           next_relax_group_bias += relax_group_ptr->count;
13591           relax_group_ptr->init = 1;
13592         }
13593
13594       /* Reorder relax group groups.  */
13595       relend = relocs + asec->reloc_count;
13596       for (rel = relocs; rel < relend; rel++)
13597         {
13598           rtype = ELF32_R_TYPE(rel->r_info);
13599           if (rtype != R_NDS32_RELAX_GROUP)
13600             continue;
13601
13602           /* Change it.  */
13603           rel->r_addend += relax_group_ptr->bias;
13604           /* Debugging count.  */
13605           count++;
13606         }
13607     }
13608   while (FALSE);
13609
13610   if (relocs != NULL && elf_section_data (asec)->relocs != relocs)
13611     free (relocs);
13612
13613   return result;
13614 }
13615
13616 int
13617 nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13618                            struct bfd_link_info *lnkinfo)
13619 {
13620   bfd_boolean result = TRUE;
13621   Elf_Internal_Rela *irel;
13622   Elf_Internal_Rela *irelend;
13623   Elf_Internal_Rela *internal_relocs;
13624   unsigned long r_symndx;
13625   enum elf_nds32_reloc_type r_type;
13626
13627   Elf_Internal_Sym *local_syms = NULL;
13628   bfd_byte *contents = NULL;
13629
13630   relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13631
13632   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13633   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13634   sym_hashes = elf_sym_hashes (inbfd);
13635   sym_hashes_end =
13636     sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13637   if (!elf_bad_symtab (inbfd))
13638     sym_hashes_end -= symtab_hdr->sh_info;
13639
13640   /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
13641   if (bfd_link_relocatable (lnkinfo))
13642     {
13643       elf32_nds32_unify_relax_group (inbfd, insec);
13644       return result;
13645     }
13646
13647   /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13648   internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13649                                                TRUE /* keep_memory  */);
13650   if (internal_relocs == NULL)
13651     goto error_return;
13652
13653   irelend = internal_relocs + insec->reloc_count;
13654   irel = find_relocs_at_address (internal_relocs, internal_relocs,
13655                                  irelend, R_NDS32_RELAX_ENTRY);
13656   if (irel == irelend)
13657     goto finish;
13658
13659   /* Chain/remove groups.  */
13660   for (irel = internal_relocs; irel < irelend; irel++)
13661     {
13662       r_symndx = ELF32_R_SYM (irel->r_info);
13663       r_type = ELF32_R_TYPE (irel->r_info);
13664       if (r_type != R_NDS32_RELAX_GROUP)
13665         continue;
13666
13667       /* Remove it.  */
13668       irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13669       /* Chain it now.  */
13670       if (!list_insert (&chain, irel))
13671         goto error_return;
13672     }
13673
13674   /* Collect group relocations.  */
13675   /* Presume relocations are sorted.  */
13676   relax_group_list_t *pNext = chain.next;
13677   while (pNext)
13678     {
13679       for (irel = internal_relocs; irel < irelend; irel++)
13680         {
13681           if (irel->r_offset == pNext->relo->r_offset)
13682             {
13683               /* Ignore Non-TLS relocation types.  */
13684               r_type = ELF32_R_TYPE (irel->r_info);
13685               if ((R_NDS32_TLS_LE_HI20 > r_type)
13686                   || (R_NDS32_RELAX_ENTRY == r_type))
13687                 continue;
13688
13689               if (!list_insert_sibling (pNext, irel))
13690                 goto error_return;
13691             }
13692           else if (irel->r_offset > pNext->relo->r_offset)
13693             {
13694               pNext = pNext->next;
13695               if (!pNext)
13696                 break;
13697
13698               bfd_vma current_offset = pNext->relo->r_offset;
13699               if (irel->r_offset > current_offset)
13700                 irel = internal_relocs; /* restart from head */
13701               else
13702                 --irel; /* Check current irel again.  */
13703               continue;
13704             }
13705           else
13706             {
13707               /* This shouldn't be reached.  */
13708             }
13709         }
13710       if (pNext)
13711         pNext = pNext->next;
13712     }
13713
13714 #ifdef DUBUG_VERBOSE
13715   dump_chain(&chain);
13716 #endif
13717
13718   /* Get symbol table and section content.  */
13719   if (incontents)
13720     contents = incontents;
13721   else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13722            || !nds32_get_local_syms (inbfd, insec, &local_syms))
13723     goto error_return;
13724
13725   char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13726
13727   /* Convert TLS model each group if necessary.  */
13728   pNext = chain.next;
13729
13730   int cur_grp_id = -1;
13731   int sethi_rt = -1;
13732   int add_rt = -1;
13733   enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13734
13735   tls_type = org_tls_type = eff_tls_type = 0;
13736
13737   while (pNext)
13738     {
13739       relax_group_list_t *pNextSig = pNext->next_sibling;
13740       while (pNextSig)
13741         {
13742           struct elf_link_hash_entry *h = NULL;
13743
13744           irel = pNextSig->relo;
13745           r_symndx = ELF32_R_SYM(irel->r_info);
13746           r_type = ELF32_R_TYPE(irel->r_info);
13747
13748           if (pNext->id != cur_grp_id)
13749             {
13750               cur_grp_id = pNext->id;
13751               org_tls_type = get_tls_type (r_type, NULL);
13752               if (r_symndx >= symtab_hdr->sh_info)
13753                 {
13754                   h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13755                   while (h->root.type == bfd_link_hash_indirect
13756                          || h->root.type == bfd_link_hash_warning)
13757                     h = (struct elf_link_hash_entry *) h->root.u.i.link;
13758                   tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13759                 }
13760               else
13761                 {
13762                   tls_type = local_got_tls_type
13763                     ? local_got_tls_type[r_symndx]
13764                     : GOT_NORMAL;
13765                 }
13766
13767               eff_tls_type = 1 << (fls (tls_type) - 1);
13768               sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13769             }
13770
13771           if (eff_tls_type != org_tls_type)
13772             {
13773               switch (org_tls_type)
13774                 {
13775                   /* DESC to IEGP/IE/LE.  */
13776                 case GOT_TLS_DESC:
13777                   switch (eff_tls_type)
13778                     {
13779                     case GOT_TLS_IE:
13780                       switch (r_type)
13781                         {
13782                         case R_NDS32_TLS_DESC_HI20:
13783                           irel->r_info = ELF32_R_INFO(r_symndx,
13784                                                       R_NDS32_TLS_IE_HI20);
13785                           break;
13786                         case R_NDS32_TLS_DESC_LO12:
13787                           irel->r_info = ELF32_R_INFO(r_symndx,
13788                                                       R_NDS32_TLS_IE_LO12);
13789                           break;
13790                         case R_NDS32_TLS_DESC_ADD:
13791                           {
13792                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13793                             add_rt = N32_RT5 (insn);
13794                             insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13795                             bfd_putb32 (insn, contents + irel->r_offset);
13796
13797                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13798                           }
13799                           break;
13800                         case R_NDS32_TLS_DESC_FUNC:
13801                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13802                           irel->r_info = ELF32_R_INFO(r_symndx,
13803                                                       R_NDS32_RELAX_REMOVE);
13804                           break;
13805                         case R_NDS32_TLS_DESC_CALL:
13806                           {
13807                             uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13808                                                      REG_TP);
13809                             bfd_putb32 (insn, contents + irel->r_offset);
13810
13811                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13812                           }
13813                           break;
13814                         case R_NDS32_LOADSTORE:
13815                         case R_NDS32_PTR:
13816                         case R_NDS32_PTR_RESOLVED:
13817                         case R_NDS32_NONE:
13818                         case R_NDS32_LABEL:
13819                           break;
13820                         default:
13821                           BFD_ASSERT(0);
13822                           break;
13823                         }
13824                       break;
13825                     case GOT_TLS_IEGP:
13826                       switch (r_type)
13827                         {
13828                         case R_NDS32_TLS_DESC_HI20:
13829                           irel->r_info = ELF32_R_INFO(r_symndx,
13830                                                       R_NDS32_TLS_IEGP_HI20);
13831                           break;
13832                         case R_NDS32_TLS_DESC_LO12:
13833                           irel->r_info = ELF32_R_INFO(r_symndx,
13834                                                       R_NDS32_TLS_IEGP_LO12);
13835                           break;
13836                         case R_NDS32_TLS_DESC_ADD:
13837                           {
13838                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13839                             add_rt = N32_RT5 (insn);
13840                             insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13841                             bfd_putb32 (insn, contents + irel->r_offset);
13842
13843                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13844                           }
13845                           break;
13846                         case R_NDS32_TLS_DESC_FUNC:
13847                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13848                           irel->r_info = ELF32_R_INFO(r_symndx,
13849                                                       R_NDS32_RELAX_REMOVE);
13850                           break;
13851                         case R_NDS32_TLS_DESC_CALL:
13852                           {
13853                             uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13854                                                      REG_TP);
13855                             bfd_putb32 (insn, contents + irel->r_offset);
13856
13857                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13858                           }
13859                           break;
13860                         case R_NDS32_LOADSTORE:
13861                         case R_NDS32_PTR:
13862                         case R_NDS32_PTR_RESOLVED:
13863                         case R_NDS32_NONE:
13864                         case R_NDS32_LABEL:
13865                           break;
13866                         default:
13867                           BFD_ASSERT(0);
13868                           break;
13869                         }
13870                       break;
13871                     case GOT_TLS_LE:
13872                       switch (r_type)
13873                         {
13874                         case R_NDS32_TLS_DESC_HI20:
13875                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13876                           break;
13877                         case R_NDS32_TLS_DESC_LO12:
13878                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13879                           break;
13880                         case R_NDS32_TLS_DESC_ADD:
13881                           {
13882                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13883
13884                             add_rt = N32_RT5 (insn);
13885                             insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13886                             bfd_putb32 (insn, contents + irel->r_offset);
13887
13888                             irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13889                           }
13890                           break;
13891                         case R_NDS32_TLS_DESC_FUNC:
13892                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13893                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13894                           break;
13895                         case R_NDS32_TLS_DESC_CALL:
13896                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13897                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13898                           break;
13899                         case R_NDS32_LOADSTORE:
13900                         case R_NDS32_PTR:
13901                         case R_NDS32_PTR_RESOLVED:
13902                         case R_NDS32_NONE:
13903                         case R_NDS32_LABEL:
13904                           break;
13905                         default:
13906                           BFD_ASSERT(0);
13907                           break;
13908                         }
13909                       break;
13910                     default:
13911                       break;
13912                     }
13913                   break;
13914                   /* IEGP to IE/LE.  */
13915                 case GOT_TLS_IEGP:
13916                   switch (eff_tls_type)
13917                     {
13918                     case GOT_TLS_IE:
13919                       switch (r_type)
13920                         {
13921                         case R_NDS32_TLS_IEGP_HI20:
13922                           irel->r_info = ELF32_R_INFO(r_symndx,
13923                                                       R_NDS32_TLS_IE_HI20);
13924                           break;
13925                         case R_NDS32_TLS_IEGP_LO12:
13926                           irel->r_info = ELF32_R_INFO(r_symndx,
13927                                                       R_NDS32_TLS_IE_LO12);
13928                           break;
13929                         case R_NDS32_PTR_RESOLVED:
13930                           {
13931                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13932
13933                             add_rt = N32_RT5 (insn);
13934                             insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13935                             bfd_putb32 (insn, contents + irel->r_offset);
13936                           }
13937                           break;
13938                         case R_NDS32_TLS_IEGP_LW:
13939                           break;
13940                         case R_NDS32_LOADSTORE:
13941                         case R_NDS32_PTR:
13942                         case R_NDS32_NONE:
13943                         case R_NDS32_LABEL:
13944                           break;
13945                         default:
13946                           BFD_ASSERT(0);
13947                           break;
13948                         }
13949                       break;
13950                     case GOT_TLS_LE:
13951                       switch (r_type)
13952                         {
13953                         case R_NDS32_TLS_IEGP_HI20:
13954                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13955                           break;
13956                         case R_NDS32_TLS_IEGP_LO12:
13957                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13958                           break;
13959                         case R_NDS32_TLS_IEGP_LW:
13960                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13961                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13962                           break;
13963                         case R_NDS32_LOADSTORE:
13964                         case R_NDS32_PTR:
13965                         case R_NDS32_NONE:
13966                         case R_NDS32_LABEL:
13967                         case R_NDS32_PTR_RESOLVED:
13968                           break;
13969                         default:
13970                           BFD_ASSERT(0);
13971                           break;
13972                         }
13973                       break;
13974                     default:
13975                       break;
13976                     }
13977                   break;
13978                   /* IE to LE. */
13979                 case GOT_TLS_IE:
13980                   switch (eff_tls_type)
13981                     {
13982                     case GOT_TLS_LE:
13983                       switch (r_type)
13984                         {
13985                         case R_NDS32_TLS_IE_HI20:
13986                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13987                           break;
13988                         case R_NDS32_TLS_IE_LO12S2:
13989                           {
13990                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13991
13992                             add_rt = N32_RT5 (insn);
13993                             insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13994                             bfd_putb32 (insn, contents + irel->r_offset);
13995
13996                             irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13997                           }
13998                           break;
13999                         case R_NDS32_LOADSTORE:
14000                         case R_NDS32_PTR:
14001                         case R_NDS32_NONE:
14002                         case R_NDS32_LABEL:
14003                           break;
14004                         default:
14005                           BFD_ASSERT(0);
14006                           break;
14007                         }
14008                       break;
14009                     default:
14010                       break;
14011                     }
14012                   break;
14013                 default:
14014                   break;
14015                 }
14016             }
14017           pNextSig = pNextSig->next_sibling;
14018         }
14019
14020 #if 1
14021       pNext = pNext->next;
14022 #else
14023       while (pNext)
14024         {
14025           if (pNext->id != cur_grp_id)
14026             break;
14027           pNext = pNext->next;
14028         }
14029 #endif
14030     }
14031
14032 finish:
14033   if (incontents)
14034     contents = NULL;
14035
14036   if (internal_relocs != NULL
14037       && elf_section_data (insec)->relocs != internal_relocs)
14038     free (internal_relocs);
14039
14040   if (contents != NULL
14041       && elf_section_data (insec)->this_hdr.contents != contents)
14042     free (contents);
14043
14044   if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms)
14045     free (local_syms);
14046
14047   if (chain.next)
14048     {
14049       pNext = chain.next;
14050       relax_group_list_t *pDel;
14051       while (pNext)
14052         {
14053           pDel = pNext;
14054           pNext = pNext->next;
14055           free (pDel);
14056         }
14057     }
14058
14059   return result;
14060
14061 error_return:
14062   result = FALSE;
14063   goto finish;
14064 }
14065
14066 /* End TLS model conversion.  */
14067
14068 #define ELF_ARCH                                bfd_arch_nds32
14069 #define ELF_MACHINE_CODE                        EM_NDS32
14070 #define ELF_MAXPAGESIZE                         0x1000
14071 #define ELF_TARGET_ID                           NDS32_ELF_DATA
14072
14073 #define TARGET_BIG_SYM                          nds32_elf32_be_vec
14074 #define TARGET_BIG_NAME                         "elf32-nds32be"
14075 #define TARGET_LITTLE_SYM                       nds32_elf32_le_vec
14076 #define TARGET_LITTLE_NAME                      "elf32-nds32le"
14077
14078 #define elf_info_to_howto                       nds32_info_to_howto
14079 #define elf_info_to_howto_rel                   nds32_info_to_howto_rel
14080
14081 #define bfd_elf32_bfd_link_hash_table_create    nds32_elf_link_hash_table_create
14082 #define bfd_elf32_bfd_merge_private_bfd_data    nds32_elf_merge_private_bfd_data
14083 #define bfd_elf32_bfd_print_private_bfd_data    nds32_elf_print_private_bfd_data
14084 #define bfd_elf32_bfd_relax_section             nds32_elf_relax_section
14085 #define bfd_elf32_bfd_set_private_flags         nds32_elf_set_private_flags
14086
14087 #define bfd_elf32_mkobject                      nds32_elf_mkobject
14088 #define elf_backend_action_discarded            nds32_elf_action_discarded
14089 #define elf_backend_add_symbol_hook             nds32_elf_add_symbol_hook
14090 #define elf_backend_check_relocs                nds32_elf_check_relocs
14091 #define elf_backend_adjust_dynamic_symbol       nds32_elf_adjust_dynamic_symbol
14092 #define elf_backend_create_dynamic_sections     nds32_elf_create_dynamic_sections
14093 #define elf_backend_finish_dynamic_sections     nds32_elf_finish_dynamic_sections
14094 #define elf_backend_finish_dynamic_symbol       nds32_elf_finish_dynamic_symbol
14095 #define elf_backend_size_dynamic_sections       nds32_elf_size_dynamic_sections
14096 #define elf_backend_relocate_section            nds32_elf_relocate_section
14097 #define elf_backend_gc_mark_hook                nds32_elf_gc_mark_hook
14098 #define elf_backend_grok_prstatus               nds32_elf_grok_prstatus
14099 #define elf_backend_grok_psinfo                 nds32_elf_grok_psinfo
14100 #define elf_backend_reloc_type_class            nds32_elf_reloc_type_class
14101 #define elf_backend_copy_indirect_symbol        nds32_elf_copy_indirect_symbol
14102 #define elf_backend_link_output_symbol_hook     nds32_elf_output_symbol_hook
14103 #define elf_backend_output_arch_syms            nds32_elf_output_arch_syms
14104 #define elf_backend_object_p                    nds32_elf_object_p
14105 #define elf_backend_final_write_processing      nds32_elf_final_write_processing
14106 #define elf_backend_special_sections            nds32_elf_special_sections
14107 #define bfd_elf32_bfd_get_relocated_section_contents \
14108                                 nds32_elf_get_relocated_section_contents
14109 #define bfd_elf32_bfd_is_target_special_symbol  nds32_elf_is_target_special_symbol
14110 #define elf_backend_maybe_function_sym          nds32_elf_maybe_function_sym
14111
14112 #define elf_backend_can_gc_sections             1
14113 #define elf_backend_can_refcount                1
14114 #define elf_backend_want_got_plt                1
14115 #define elf_backend_plt_readonly                1
14116 #define elf_backend_want_plt_sym                0
14117 #define elf_backend_got_header_size             12
14118 #define elf_backend_may_use_rel_p               1
14119 #define elf_backend_default_use_rela_p          1
14120 #define elf_backend_may_use_rela_p              1
14121 #define elf_backend_dtrel_excludes_plt          0
14122
14123 #include "elf32-target.h"
14124
14125 #undef ELF_MAXPAGESIZE
14126 #define ELF_MAXPAGESIZE                         0x2000
14127
14128 #undef  TARGET_BIG_SYM
14129 #define TARGET_BIG_SYM                          nds32_elf32_linux_be_vec
14130 #undef  TARGET_BIG_NAME
14131 #define TARGET_BIG_NAME                         "elf32-nds32be-linux"
14132 #undef  TARGET_LITTLE_SYM
14133 #define TARGET_LITTLE_SYM                       nds32_elf32_linux_le_vec
14134 #undef  TARGET_LITTLE_NAME
14135 #define TARGET_LITTLE_NAME                      "elf32-nds32le-linux"
14136 #undef  elf32_bed
14137 #define elf32_bed                               elf32_nds32_lin_bed
14138
14139 #include "elf32-target.h"