_bfd_clear_contents bounds checking
[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 static reloc_howto_type *
3169 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3170                                  const char *r_name)
3171 {
3172   unsigned int i;
3173
3174   for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3175     if (nds32_elf_howto_table[i].name != NULL
3176         && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3177       return &nds32_elf_howto_table[i];
3178
3179   for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3180     if (nds32_elf_relax_howto_table[i].name != NULL
3181         && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3182       return &nds32_elf_relax_howto_table[i];
3183
3184   return NULL;
3185 }
3186
3187 static reloc_howto_type *
3188 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3189 {
3190   if (code < R_NDS32_RELAX_ENTRY)
3191     {
3192       BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3193       return &nds32_elf_howto_table[code];
3194     }
3195   else
3196     {
3197       if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3198           >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3199         {
3200           int i = code;
3201           i += 1;
3202         }
3203
3204       BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3205                   < ARRAY_SIZE (nds32_elf_relax_howto_table));
3206       return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3207     }
3208 }
3209
3210 static reloc_howto_type *
3211 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3212                                  bfd_reloc_code_real_type code)
3213 {
3214   unsigned int i;
3215
3216   for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3217     {
3218       if (nds32_reloc_map[i].bfd_reloc_val == code)
3219         return bfd_elf32_bfd_reloc_type_table_lookup
3220           (nds32_reloc_map[i].elf_reloc_val);
3221     }
3222
3223   return NULL;
3224 }
3225
3226 /* Set the howto pointer for an NDS32 ELF reloc.  */
3227
3228 static bfd_boolean
3229 nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3230                          Elf_Internal_Rela *dst)
3231 {
3232   enum elf_nds32_reloc_type r_type;
3233
3234   r_type = ELF32_R_TYPE (dst->r_info);
3235   if (r_type > R_NDS32_GNU_VTENTRY)
3236     {
3237       /* xgettext:c-format */
3238       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3239                           abfd, r_type);
3240       bfd_set_error (bfd_error_bad_value);
3241       return FALSE;
3242     }
3243
3244   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
3245   cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3246   return TRUE;
3247 }
3248
3249 static bfd_boolean
3250 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3251                      Elf_Internal_Rela *dst)
3252 {
3253   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3254
3255   if ((r_type == R_NDS32_NONE)
3256       || ((r_type > R_NDS32_GNU_VTENTRY)
3257           && (r_type < R_NDS32_max)))
3258     {
3259       cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3260       return TRUE;
3261     }
3262
3263   /* xgettext:c-format */
3264   _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3265   bfd_set_error (bfd_error_bad_value);
3266   return FALSE;
3267 }
3268
3269 /* Support for core dump NOTE sections.
3270    Reference to include/linux/elfcore.h in Linux.  */
3271
3272 static bfd_boolean
3273 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3274 {
3275   int offset;
3276   size_t size;
3277
3278   switch (note->descsz)
3279     {
3280     case 0x114:
3281       /* Linux/NDS32 32-bit, ABI1.  */
3282
3283       /* pr_cursig */
3284       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3285
3286       /* pr_pid */
3287       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3288
3289       /* pr_reg */
3290       offset = 72;
3291       size = 200;
3292       break;
3293
3294     case 0xfc:
3295       /* Linux/NDS32 32-bit.  */
3296
3297       /* pr_cursig */
3298       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3299
3300       /* pr_pid */
3301       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3302
3303       /* pr_reg */
3304       offset = 72;
3305       size = 176;
3306       break;
3307
3308     default:
3309       return FALSE;
3310     }
3311
3312   /* Make a ".reg" section.  */
3313   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3314                                           size, note->descpos + offset);
3315 }
3316
3317 static bfd_boolean
3318 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3319 {
3320   switch (note->descsz)
3321     {
3322     case 124:
3323       /* Linux/NDS32.  */
3324
3325       /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
3326       elf_tdata (abfd)->core->program =
3327         _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3328       elf_tdata (abfd)->core->command =
3329         _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3330       break;
3331
3332     default:
3333       return FALSE;
3334     }
3335
3336   /* Note that for some reason, a spurious space is tacked
3337      onto the end of the args in some (at least one anyway)
3338      implementations, so strip it off if it exists.  */
3339   {
3340     char *command = elf_tdata (abfd)->core->command;
3341     int n = strlen (command);
3342
3343     if (0 < n && command[n - 1] == ' ')
3344       command[n - 1] = '\0';
3345   }
3346
3347   return TRUE;
3348 }
3349
3350 /* Hook called by the linker routine which adds symbols from an object
3351    file.  We must handle the special NDS32 section numbers here.
3352    We also keep watching for whether we need to create the sdata special
3353    linker sections.  */
3354
3355 static bfd_boolean
3356 nds32_elf_add_symbol_hook (bfd *abfd,
3357                            struct bfd_link_info *info ATTRIBUTE_UNUSED,
3358                            Elf_Internal_Sym *sym,
3359                            const char **namep ATTRIBUTE_UNUSED,
3360                            flagword *flagsp ATTRIBUTE_UNUSED,
3361                            asection **secp, bfd_vma *valp)
3362 {
3363   switch (sym->st_shndx)
3364     {
3365     case SHN_COMMON:
3366       /* Common symbols less than the GP size are automatically
3367          treated as SHN_MIPS_SCOMMON symbols.  */
3368       if (sym->st_size > elf_gp_size (abfd)
3369           || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3370         break;
3371
3372       /* st_value is the alignemnt constraint.
3373          That might be its actual size if it is an array or structure.  */
3374       switch (sym->st_value)
3375         {
3376         case 1:
3377           *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3378           break;
3379         case 2:
3380           *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3381           break;
3382         case 4:
3383           *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3384           break;
3385         case 8:
3386           *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3387           break;
3388         default:
3389           return TRUE;
3390         }
3391
3392       (*secp)->flags |= SEC_IS_COMMON;
3393       *valp = sym->st_size;
3394       break;
3395     }
3396
3397   return TRUE;
3398 }
3399
3400 /* This function can figure out the best location for a base register to access
3401    data relative to this base register
3402    INPUT:
3403    sda_d0: size of first DOUBLE WORD data section
3404    sda_w0: size of first WORD data section
3405    sda_h0: size of first HALF WORD data section
3406    sda_b : size of BYTE data section
3407    sda_hi: size of second HALF WORD data section
3408    sda_w1: size of second WORD data section
3409    sda_d1: size of second DOUBLE WORD data section
3410    OUTPUT:
3411    offset (always positive) from the beginning of sda_d0 if OK
3412    a negative error value if fail
3413    NOTE:
3414    these 7 sections have to be located back to back if exist
3415    a pass in 0 value for non-existing section   */
3416
3417 /* Due to the interpretation of simm15 field of load/store depending on
3418    data accessing size, the organization of base register relative data shall
3419    like the following figure
3420    -------------------------------------------
3421    |  DOUBLE WORD sized data (range +/- 128K)
3422    -------------------------------------------
3423    |  WORD sized data (range +/- 64K)
3424    -------------------------------------------
3425    |  HALF WORD sized data (range +/- 32K)
3426    -------------------------------------------
3427    |  BYTE sized data (range +/- 16K)
3428    -------------------------------------------
3429    |  HALF WORD sized data (range +/- 32K)
3430    -------------------------------------------
3431    |  WORD sized data (range +/- 64K)
3432    -------------------------------------------
3433    |  DOUBLE WORD sized data (range +/- 128K)
3434    -------------------------------------------
3435    Its base register shall be set to access these data freely.  */
3436
3437 /* We have to figure out the SDA_BASE value, so that we can adjust the
3438    symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3439    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3440    target data.  We don't need to adjust the symbol value for an
3441    external symbol if we are producing relocatable output.  */
3442
3443 static asection *sda_rela_sec = NULL;
3444
3445 #define SDA_SECTION_NUM 10
3446
3447 static bfd_reloc_status_type
3448 nds32_elf_final_sda_base (bfd *                   output_bfd,
3449                           struct bfd_link_info *  info,
3450                           bfd_vma *               psb,
3451                           bfd_boolean             add_symbol)
3452 {
3453   int relax_fp_as_gp;
3454   struct elf_nds32_link_hash_table *table;
3455   struct bfd_link_hash_entry *h, *h2;
3456   long unsigned int total = 0;
3457   asection *first = NULL, *final = NULL, *temp;
3458   bfd_vma sda_base = 0;
3459
3460   h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3461   if (!h || (h->type != bfd_link_hash_defined
3462              && h->type != bfd_link_hash_defweak))
3463     {
3464       /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3465          4 byte-aligned.  Therefore, it has to set the first section ".data"
3466          4 byte-aligned.  */
3467       static const char sec_name[SDA_SECTION_NUM][10] =
3468         {
3469           ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3470           ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3471         };
3472       size_t i = 0;
3473
3474       if (output_bfd->sections == NULL)
3475         {
3476           *psb = elf_gp (output_bfd);
3477           return bfd_reloc_ok;
3478         }
3479
3480       /* Get the first and final section.  */
3481       while (i < ARRAY_SIZE (sec_name))
3482         {
3483           temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3484           if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3485             first = temp;
3486           if (temp && (temp->size != 0 || temp->rawsize != 0))
3487             final = temp;
3488
3489           /* Summarize the sections in order to check if joining .bss.  */
3490           if (temp && temp->size != 0)
3491             total += temp->size;
3492           else if (temp && temp->rawsize != 0)
3493             total += temp->rawsize;
3494
3495           i++;
3496         }
3497
3498       /* Check .bss size.  */
3499       temp = bfd_get_section_by_name (output_bfd, ".bss");
3500       if (temp)
3501         {
3502           if (temp->size != 0)
3503             total += temp->size;
3504           else if (temp->rawsize != 0)
3505             total += temp->rawsize;
3506
3507           if (total < 0x80000)
3508             {
3509               if (!first && (temp->size != 0 || temp->rawsize != 0))
3510                 first = temp;
3511               if ((temp->size != 0 || temp->rawsize != 0))
3512                 final = temp;
3513             }
3514         }
3515
3516       if (first && final)
3517         {
3518           /* The middle of data region.  */
3519           sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3520
3521           /* Find the section sda_base located.  */
3522           i = 0;
3523           while (i < ARRAY_SIZE (sec_name))
3524             {
3525               final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3526               if (final && (final->size != 0 || final->rawsize != 0)
3527                   && sda_base >= final->vma)
3528                 {
3529                   first = final;
3530                   i++;
3531                 }
3532               else
3533                 break;
3534             }
3535         }
3536       else
3537         {
3538           /* If there is not any default data section in output bfd, try to find
3539              the first data section.  If no data section be found, just simplily
3540              choose the first output section.  */
3541           temp = output_bfd->sections;
3542           while (temp)
3543             {
3544               if (temp->flags & SEC_ALLOC
3545                   && (((temp->flags & SEC_DATA)
3546                        && ((temp->flags & SEC_READONLY) == 0))
3547                       || (temp->flags & SEC_LOAD) == 0)
3548                   && (temp->size != 0 || temp->rawsize != 0))
3549                 {
3550                   if (!first)
3551                     first = temp;
3552                   final = temp;
3553                 }
3554               temp = temp->next;
3555             }
3556
3557           /* There is no data or bss section.  */
3558           if (!first || (first->size == 0 && first->rawsize == 0))
3559             {
3560               first = output_bfd->sections;
3561               while (first && first->size == 0 && first->rawsize == 0)
3562                 first = first->next;
3563             }
3564
3565           /* There is no concrete section.  */
3566           if (!first)
3567             {
3568               *psb = elf_gp (output_bfd);
3569               return bfd_reloc_ok;
3570             }
3571
3572           if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3573             sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3574           else
3575             sda_base = first->vma + 0x2000;
3576         }
3577
3578       sda_base -= first->vma;
3579       sda_base = sda_base & (~7);
3580
3581       if (!_bfd_generic_link_add_one_symbol
3582           (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3583            (bfd_vma) sda_base, (const char *) NULL, FALSE,
3584            get_elf_backend_data (output_bfd)->collect, &h))
3585         return FALSE;
3586
3587       sda_rela_sec = first;
3588     }
3589
3590   /* Set _FP_BASE_ to _SDA_BASE_.  */
3591   table = nds32_elf_hash_table (info);
3592   relax_fp_as_gp = table->relax_fp_as_gp;
3593   h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3594   /* _SDA_BASE_ is difined in linker script.  */
3595   if (!first)
3596     {
3597       first = h->u.def.section;
3598       sda_base = h->u.def.value;
3599     }
3600
3601   if (relax_fp_as_gp && h2
3602       && (h2->type == bfd_link_hash_undefweak
3603           || h2->type == bfd_link_hash_undefined))
3604     {
3605       /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3606          And set FP equal to SDA_BASE to do relaxation for
3607          la $fp, _FP_BASE_.  */
3608       if (!_bfd_generic_link_add_one_symbol
3609           (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3610            first, sda_base, (const char *) NULL,
3611            FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3612         return FALSE;
3613     }
3614
3615   if (add_symbol)
3616     {
3617       if (h)
3618         {
3619           /* Now set gp.  */
3620           elf_gp (output_bfd) = (h->u.def.value
3621                                  + h->u.def.section->output_section->vma
3622                                  + h->u.def.section->output_offset);
3623         }
3624       else
3625         {
3626           _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3627           return bfd_reloc_dangerous;
3628         }
3629     }
3630
3631   *psb = h->u.def.value
3632     + h->u.def.section->output_section->vma
3633     + h->u.def.section->output_offset;
3634   return bfd_reloc_ok;
3635 }
3636 \f
3637
3638 /* Return size of a PLT entry.  */
3639 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3640
3641 /* Create an entry in an nds32 ELF linker hash table.  */
3642
3643 static struct bfd_hash_entry *
3644 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3645                              struct bfd_hash_table *table,
3646                              const char *string)
3647 {
3648   struct elf_nds32_link_hash_entry *ret;
3649
3650   ret = (struct elf_nds32_link_hash_entry *) entry;
3651
3652   /* Allocate the structure if it has not already been allocated by a
3653      subclass.  */
3654   if (ret == NULL)
3655     ret = (struct elf_nds32_link_hash_entry *)
3656        bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3657
3658   if (ret == NULL)
3659     return (struct bfd_hash_entry *) ret;
3660
3661   /* Call the allocation method of the superclass.  */
3662   ret = (struct elf_nds32_link_hash_entry *)
3663     _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3664
3665   if (ret != NULL)
3666     {
3667       struct elf_nds32_link_hash_entry *eh;
3668
3669       eh = (struct elf_nds32_link_hash_entry *) ret;
3670       eh->dyn_relocs = NULL;
3671       eh->tls_type = GOT_UNKNOWN;
3672       eh->offset_to_gp = 0;
3673     }
3674
3675   return (struct bfd_hash_entry *) ret;
3676 }
3677
3678 /* Create an nds32 ELF linker hash table.  */
3679
3680 static struct bfd_link_hash_table *
3681 nds32_elf_link_hash_table_create (bfd *abfd)
3682 {
3683   struct elf_nds32_link_hash_table *ret;
3684
3685   bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3686
3687   ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3688   if (ret == NULL)
3689     return NULL;
3690
3691   /* Patch tag.  */
3692   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3693                                       nds32_elf_link_hash_newfunc,
3694                                       sizeof (struct elf_nds32_link_hash_entry),
3695                                       NDS32_ELF_DATA))
3696     {
3697       free (ret);
3698       return NULL;
3699     }
3700
3701   ret->sdynbss = NULL;
3702   ret->srelbss = NULL;
3703   ret->sym_ld_script = NULL;
3704
3705   return &ret->root.root;
3706 }
3707
3708 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3709    shortcuts to them in our hash table.  */
3710
3711 static bfd_boolean
3712 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3713 {
3714   struct elf_link_hash_table *ehtab;
3715
3716   if (!_bfd_elf_create_got_section (dynobj, info))
3717     return FALSE;
3718
3719   ehtab = elf_hash_table (info);
3720   ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3721   ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3722   if (!ehtab->sgot || !ehtab->sgotplt)
3723     abort ();
3724
3725   /* _bfd_elf_create_got_section will create it for us.  */
3726   ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3727   if (ehtab->srelgot == NULL
3728       || !bfd_set_section_flags (dynobj, ehtab->srelgot,
3729                                  (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3730                                   | SEC_IN_MEMORY | SEC_LINKER_CREATED
3731                                   | SEC_READONLY))
3732       || !bfd_set_section_alignment (dynobj, ehtab->srelgot, 2))
3733     return FALSE;
3734
3735   return TRUE;
3736 }
3737
3738 /* Create dynamic sections when linking against a dynamic object.  */
3739
3740 static bfd_boolean
3741 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3742 {
3743   struct elf_link_hash_table *ehtab;
3744   struct elf_nds32_link_hash_table *htab;
3745   flagword flags, pltflags;
3746   register asection *s;
3747   const struct elf_backend_data *bed;
3748   int ptralign = 2;             /* 32-bit  */
3749   const char *secname;
3750   char *relname;
3751   flagword secflags;
3752   asection *sec;
3753
3754   bed = get_elf_backend_data (abfd);
3755   ehtab = elf_hash_table (info);
3756   htab = nds32_elf_hash_table (info);
3757
3758   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3759      .rel[a].bss sections.  */
3760
3761   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3762            | SEC_LINKER_CREATED);
3763
3764   pltflags = flags;
3765   pltflags |= SEC_CODE;
3766   if (bed->plt_not_loaded)
3767     pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3768   if (bed->plt_readonly)
3769     pltflags |= SEC_READONLY;
3770
3771   s = bfd_make_section (abfd, ".plt");
3772   ehtab->splt = s;
3773   if (s == NULL
3774       || !bfd_set_section_flags (abfd, s, pltflags)
3775       || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3776     return FALSE;
3777
3778   if (bed->want_plt_sym)
3779     {
3780       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3781          .plt section.  */
3782       struct bfd_link_hash_entry *bh = NULL;
3783       struct elf_link_hash_entry *h;
3784
3785       if (!(_bfd_generic_link_add_one_symbol
3786             (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3787              (bfd_vma) 0, (const char *) NULL, FALSE,
3788              get_elf_backend_data (abfd)->collect, &bh)))
3789         return FALSE;
3790
3791       h = (struct elf_link_hash_entry *) bh;
3792       h->def_regular = 1;
3793       h->type = STT_OBJECT;
3794
3795       if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3796         return FALSE;
3797     }
3798
3799   s = bfd_make_section (abfd,
3800                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3801   ehtab->srelplt = s;
3802   if (s == NULL
3803       || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3804       || !bfd_set_section_alignment (abfd, s, ptralign))
3805     return FALSE;
3806
3807   if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3808     return FALSE;
3809
3810   for (sec = abfd->sections; sec; sec = sec->next)
3811     {
3812       secflags = bfd_get_section_flags (abfd, sec);
3813       if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3814           || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3815         continue;
3816       secname = bfd_get_section_name (abfd, sec);
3817       relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3818       strcpy (relname, ".rela");
3819       strcat (relname, secname);
3820       if (bfd_get_section_by_name (abfd, secname))
3821         continue;
3822       s = bfd_make_section (abfd, relname);
3823       if (s == NULL
3824           || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3825           || !bfd_set_section_alignment (abfd, s, ptralign))
3826         return FALSE;
3827     }
3828
3829   if (bed->want_dynbss)
3830     {
3831       /* The .dynbss section is a place to put symbols which are defined
3832          by dynamic objects, are referenced by regular objects, and are
3833          not functions.  We must allocate space for them in the process
3834          image and use a R_*_COPY reloc to tell the dynamic linker to
3835          initialize them at run time.  The linker script puts the .dynbss
3836          section into the .bss section of the final image.  */
3837       s = bfd_make_section (abfd, ".dynbss");
3838       htab->sdynbss = s;
3839       if (s == NULL
3840           || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3841         return FALSE;
3842       /* The .rel[a].bss section holds copy relocs.  This section is not
3843          normally needed.  We need to create it here, though, so that the
3844          linker will map it to an output section.  We can't just create it
3845          only if we need it, because we will not know whether we need it
3846          until we have seen all the input files, and the first time the
3847          main linker code calls BFD after examining all the input files
3848          (size_dynamic_sections) the input sections have already been
3849          mapped to the output sections.  If the section turns out not to
3850          be needed, we can discard it later.  We will never need this
3851          section when generating a shared object, since they do not use
3852          copy relocs.  */
3853       if (!bfd_link_pic (info))
3854         {
3855           s = bfd_make_section (abfd, (bed->default_use_rela_p
3856                                        ? ".rela.bss" : ".rel.bss"));
3857           htab->srelbss = s;
3858           if (s == NULL
3859               || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3860               || !bfd_set_section_alignment (abfd, s, ptralign))
3861             return FALSE;
3862         }
3863     }
3864
3865   return TRUE;
3866 }
3867
3868 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3869 static void
3870 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3871                                 struct elf_link_hash_entry *dir,
3872                                 struct elf_link_hash_entry *ind)
3873 {
3874   struct elf_nds32_link_hash_entry *edir, *eind;
3875
3876   edir = (struct elf_nds32_link_hash_entry *) dir;
3877   eind = (struct elf_nds32_link_hash_entry *) ind;
3878
3879   if (eind->dyn_relocs != NULL)
3880     {
3881       if (edir->dyn_relocs != NULL)
3882         {
3883           struct elf_dyn_relocs **pp;
3884           struct elf_dyn_relocs *p;
3885
3886           if (ind->root.type == bfd_link_hash_indirect)
3887             abort ();
3888
3889           /* Add reloc counts against the weak sym to the strong sym
3890              list.  Merge any entries against the same section.  */
3891           for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3892             {
3893               struct elf_dyn_relocs *q;
3894
3895               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3896                 if (q->sec == p->sec)
3897                   {
3898                     q->pc_count += p->pc_count;
3899                     q->count += p->count;
3900                     *pp = p->next;
3901                     break;
3902                   }
3903               if (q == NULL)
3904                 pp = &p->next;
3905             }
3906           *pp = edir->dyn_relocs;
3907         }
3908
3909       edir->dyn_relocs = eind->dyn_relocs;
3910       eind->dyn_relocs = NULL;
3911     }
3912
3913   if (ind->root.type == bfd_link_hash_indirect)
3914     {
3915       if (dir->got.refcount <= 0)
3916         {
3917           edir->tls_type = eind->tls_type;
3918           eind->tls_type = GOT_UNKNOWN;
3919         }
3920     }
3921
3922   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3923 }
3924 \f
3925
3926 /* Find dynamic relocs for H that apply to read-only sections.  */
3927
3928 static asection *
3929 readonly_dynrelocs (struct elf_link_hash_entry *h)
3930 {
3931   struct elf_dyn_relocs *p;
3932
3933   for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3934     {
3935       asection *s = p->sec->output_section;
3936
3937       if (s != NULL && (s->flags & SEC_READONLY) != 0)
3938         return p->sec;
3939     }
3940   return NULL;
3941 }
3942
3943 /* Adjust a symbol defined by a dynamic object and referenced by a
3944    regular object.  The current definition is in some section of the
3945    dynamic object, but we're not including those sections.  We have to
3946    change the definition to something the rest of the link can
3947    understand.  */
3948
3949 static bfd_boolean
3950 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3951                                  struct elf_link_hash_entry *h)
3952 {
3953   struct elf_nds32_link_hash_table *htab;
3954   bfd *dynobj;
3955   asection *s;
3956   unsigned int power_of_two;
3957
3958   dynobj = elf_hash_table (info)->dynobj;
3959
3960   /* Make sure we know what is going on here.  */
3961   BFD_ASSERT (dynobj != NULL
3962               && (h->needs_plt
3963                   || h->is_weakalias
3964                   || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3965
3966
3967   /* If this is a function, put it in the procedure linkage table.  We
3968      will fill in the contents of the procedure linkage table later,
3969      when we know the address of the .got section.  */
3970   if (h->type == STT_FUNC || h->needs_plt)
3971     {
3972       if (!bfd_link_pic (info)
3973           && !h->def_dynamic
3974           && !h->ref_dynamic
3975           && h->root.type != bfd_link_hash_undefweak
3976           && h->root.type != bfd_link_hash_undefined)
3977         {
3978           /* This case can occur if we saw a PLT reloc in an input
3979              file, but the symbol was never referred to by a dynamic
3980              object.  In such a case, we don't actually need to build
3981              a procedure linkage table, and we can just do a PCREL
3982              reloc instead.  */
3983           h->plt.offset = (bfd_vma) - 1;
3984           h->needs_plt = 0;
3985         }
3986
3987       return TRUE;
3988     }
3989   else
3990     h->plt.offset = (bfd_vma) - 1;
3991
3992   /* If this is a weak symbol, and there is a real definition, the
3993      processor independent code will have arranged for us to see the
3994      real definition first, and we can just use the same value.  */
3995   if (h->is_weakalias)
3996     {
3997       struct elf_link_hash_entry *def = weakdef (h);
3998       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3999       h->root.u.def.section = def->root.u.def.section;
4000       h->root.u.def.value = def->root.u.def.value;
4001       return TRUE;
4002     }
4003
4004   /* This is a reference to a symbol defined by a dynamic object which
4005      is not a function.  */
4006
4007   /* If we are creating a shared library, we must presume that the
4008      only references to the symbol are via the global offset table.
4009      For such cases we need not do anything here; the relocations will
4010      be handled correctly by relocate_section.  */
4011   if (bfd_link_pic (info))
4012     return TRUE;
4013
4014   /* If there are no references to this symbol that do not use the
4015      GOT, we don't need to generate a copy reloc.  */
4016   if (!h->non_got_ref)
4017     return TRUE;
4018
4019   /* If -z nocopyreloc was given, we won't generate them either.  */
4020   if (0 && info->nocopyreloc)
4021     {
4022       h->non_got_ref = 0;
4023       return TRUE;
4024     }
4025
4026   /* If we don't find any dynamic relocs in read-only sections, then
4027      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4028   if (!readonly_dynrelocs (h))
4029     {
4030       h->non_got_ref = 0;
4031       return TRUE;
4032     }
4033
4034   /* We must allocate the symbol in our .dynbss section, which will
4035      become part of the .bss section of the executable.  There will be
4036      an entry for this symbol in the .dynsym section.  The dynamic
4037      object will contain position independent code, so all references
4038      from the dynamic object to this symbol will go through the global
4039      offset table.  The dynamic linker will use the .dynsym entry to
4040      determine the address it must put in the global offset table, so
4041      both the dynamic object and the regular object will refer to the
4042      same memory location for the variable.  */
4043
4044   htab = nds32_elf_hash_table (info);
4045   s = htab->sdynbss;
4046   BFD_ASSERT (s != NULL);
4047
4048   /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4049      to copy the initial value out of the dynamic object and into the
4050      runtime process image.  We need to remember the offset into the
4051      .rela.bss section we are going to use.  */
4052   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4053     {
4054       asection *srel;
4055
4056       srel = htab->srelbss;
4057       BFD_ASSERT (srel != NULL);
4058       srel->size += sizeof (Elf32_External_Rela);
4059       h->needs_copy = 1;
4060     }
4061
4062   /* We need to figure out the alignment required for this symbol.  I
4063      have no idea how ELF linkers handle this.  */
4064   power_of_two = bfd_log2 (h->size);
4065   if (power_of_two > 3)
4066     power_of_two = 3;
4067
4068   /* Apply the required alignment.  */
4069   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4070   if (power_of_two > bfd_get_section_alignment (dynobj, s))
4071     {
4072       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
4073         return FALSE;
4074     }
4075
4076   /* Define the symbol as being at this point in the section.  */
4077   h->root.u.def.section = s;
4078   h->root.u.def.value = s->size;
4079
4080   /* Increment the section size to make room for the symbol.  */
4081   s->size += h->size;
4082
4083   return TRUE;
4084 }
4085
4086 /* Allocate space in .plt, .got and associated reloc sections for
4087    dynamic relocs.  */
4088
4089 static bfd_boolean
4090 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4091 {
4092   struct bfd_link_info *info;
4093   struct elf_link_hash_table *ehtab;
4094   struct elf_nds32_link_hash_table *htab;
4095   struct elf_nds32_link_hash_entry *eh;
4096   struct elf_dyn_relocs *p;
4097
4098   if (h->root.type == bfd_link_hash_indirect)
4099     return TRUE;
4100
4101   /* When warning symbols are created, they **replace** the "real"
4102      entry in the hash table, thus we never get to see the real
4103      symbol in a hash traversal. So look at it now.  */
4104   if (h->root.type == bfd_link_hash_warning)
4105     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4106
4107   eh = (struct elf_nds32_link_hash_entry *) h;
4108
4109   info = (struct bfd_link_info *) inf;
4110   ehtab = elf_hash_table (info);
4111   htab = nds32_elf_hash_table (info);
4112   if (htab == NULL)
4113     return FALSE;
4114
4115   eh = (struct elf_nds32_link_hash_entry *) h;
4116
4117   if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4118       && h->plt.refcount > 0
4119       && !(bfd_link_pie (info) && h->def_regular))
4120     {
4121       /* Make sure this symbol is output as a dynamic symbol.
4122          Undefined weak syms won't yet be marked as dynamic.  */
4123       if (h->dynindx == -1 && !h->forced_local)
4124         {
4125           if (!bfd_elf_link_record_dynamic_symbol (info, h))
4126             return FALSE;
4127         }
4128
4129       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4130         {
4131           asection *s = ehtab->splt;
4132
4133           /* If this is the first .plt entry, make room for the special
4134              first entry.  */
4135           if (s->size == 0)
4136             s->size += PLT_ENTRY_SIZE;
4137
4138           h->plt.offset = s->size;
4139
4140           /* If this symbol is not defined in a regular file, and we are
4141              not generating a shared library, then set the symbol to this
4142              location in the .plt.  This is required to make function
4143              pointers compare as equal between the normal executable and
4144              the shared library.  */
4145           if (!bfd_link_pic (info) && !h->def_regular)
4146             {
4147               h->root.u.def.section = s;
4148               h->root.u.def.value = h->plt.offset;
4149             }
4150
4151           /* Make room for this entry.  */
4152           s->size += PLT_ENTRY_SIZE;
4153
4154           /* We also need to make an entry in the .got.plt section, which
4155              will be placed in the .got section by the linker script.  */
4156           ehtab->sgotplt->size += 4;
4157
4158           /* We also need to make an entry in the .rel.plt section.  */
4159           ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4160           if (htab->tls_desc_trampoline)
4161             htab->next_tls_desc_index++;
4162         }
4163       else
4164         {
4165           h->plt.offset = (bfd_vma) - 1;
4166           h->needs_plt = 0;
4167         }
4168     }
4169   else
4170     {
4171       h->plt.offset = (bfd_vma) - 1;
4172       h->needs_plt = 0;
4173     }
4174
4175   if (h->got.refcount > 0)
4176     {
4177       asection *sgot;
4178       bfd_boolean dyn;
4179       int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4180
4181       /* Make sure this symbol is output as a dynamic symbol.
4182          Undefined weak syms won't yet be marked as dynamic.  */
4183       if (h->dynindx == -1 && !h->forced_local)
4184         {
4185           if (!bfd_elf_link_record_dynamic_symbol (info, h))
4186             return FALSE;
4187         }
4188
4189       sgot = elf_hash_table (info)->sgot;
4190       h->got.offset = sgot->size;
4191
4192       if (tls_type == GOT_UNKNOWN)
4193         abort ();
4194
4195       /* Non-TLS symbols, and TLS_IE need one GOT slot.  */
4196       if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4197         sgot->size += 4;
4198       else
4199         {
4200           /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots.  */
4201           if (tls_type & GOT_TLS_DESC)
4202             sgot->size += 8;
4203         }
4204
4205       dyn = htab->root.dynamic_sections_created;
4206
4207       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4208         {
4209           if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4210             {
4211               /* TLS_DESC with trampoline needs a relocation slot
4212                  within .rela.plt.  */
4213               htab->num_tls_desc++;
4214               ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4215               htab->tls_trampoline = -1;
4216             }
4217           else
4218             {
4219               /* other relocations, including TLS_DESC without trampoline, need
4220                  a relocation slot within .rela.got.  */
4221               ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4222             }
4223         }
4224     }
4225   else
4226     h->got.offset = (bfd_vma)-1;
4227
4228   if (eh->dyn_relocs == NULL)
4229     return TRUE;
4230
4231   /* In the shared -Bsymbolic case, discard space allocated for
4232      dynamic pc-relative relocs against symbols which turn out to be
4233      defined in regular objects.  For the normal shared case, discard
4234      space for pc-relative relocs that have become local due to symbol
4235      visibility changes.  */
4236
4237   if (bfd_link_pic (info))
4238     {
4239       if (h->def_regular && (h->forced_local || info->symbolic))
4240         {
4241           struct elf_dyn_relocs **pp;
4242
4243           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
4244             {
4245               p->count -= p->pc_count;
4246               p->pc_count = 0;
4247               if (p->count == 0)
4248                 *pp = p->next;
4249               else
4250                 pp = &p->next;
4251             }
4252         }
4253     }
4254   else
4255     {
4256       /* For the non-shared case, discard space for relocs against
4257          symbols which turn out to need copy relocs or are not dynamic.  */
4258
4259       if (!h->non_got_ref
4260           && ((h->def_dynamic
4261                && !h->def_regular)
4262               || (htab->root.dynamic_sections_created
4263                   && (h->root.type == bfd_link_hash_undefweak
4264                       || h->root.type == bfd_link_hash_undefined))))
4265         {
4266           /* Make sure this symbol is output as a dynamic symbol.
4267              Undefined weak syms won't yet be marked as dynamic.  */
4268           if (h->dynindx == -1 && !h->forced_local)
4269             {
4270               if (!bfd_elf_link_record_dynamic_symbol (info, h))
4271                 return FALSE;
4272             }
4273
4274           /* If that succeeded, we know we'll be keeping all the
4275              relocs.  */
4276           if (h->dynindx != -1)
4277             goto keep;
4278         }
4279
4280       eh->dyn_relocs = NULL;
4281
4282 keep:;
4283     }
4284
4285   /* Finally, allocate space.  */
4286   for (p = eh->dyn_relocs; p != NULL; p = p->next)
4287     {
4288       asection *sreloc = elf_section_data (p->sec)->sreloc;
4289       sreloc->size += p->count * sizeof (Elf32_External_Rela);
4290     }
4291
4292   return TRUE;
4293 }
4294
4295 /* Add relocation REL to the end of relocation section SRELOC.  */
4296
4297 static void
4298 elf32_nds32_add_dynreloc (bfd *output_bfd,
4299                           struct bfd_link_info *info ATTRIBUTE_UNUSED,
4300                           asection *sreloc, Elf_Internal_Rela *rel)
4301 {
4302   bfd_byte *loc;
4303   if (sreloc == NULL)
4304     abort ();
4305
4306   loc = sreloc->contents;
4307   loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4308   if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4309     abort ();
4310
4311   bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4312 }
4313
4314 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
4315    read-only sections.  */
4316
4317 static bfd_boolean
4318 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
4319 {
4320   asection *sec;
4321
4322   if (h->root.type == bfd_link_hash_indirect)
4323     return TRUE;
4324
4325   sec = readonly_dynrelocs (h);
4326   if (sec != NULL)
4327     {
4328       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
4329
4330       info->flags |= DF_TEXTREL;
4331       info->callbacks->minfo
4332         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
4333          sec->owner, h->root.root.string, sec);
4334
4335       /* Not an error, just cut short the traversal.  */
4336       return FALSE;
4337     }
4338   return TRUE;
4339 }
4340
4341 /* Set the sizes of the dynamic sections.  */
4342
4343 static bfd_boolean
4344 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4345                                  struct bfd_link_info *info)
4346 {
4347   struct elf_nds32_link_hash_table *htab;
4348   bfd *dynobj;
4349   asection *s;
4350   bfd_boolean relocs;
4351   bfd_boolean plt;
4352   bfd *ibfd;
4353
4354   htab = nds32_elf_hash_table (info);
4355   if (htab == NULL)
4356     return FALSE;
4357
4358   dynobj = elf_hash_table (info)->dynobj;
4359   BFD_ASSERT (dynobj != NULL);
4360
4361   if (elf_hash_table (info)->dynamic_sections_created)
4362     {
4363       /* Set the contents of the .interp section to the interpreter.  */
4364       if (bfd_link_executable (info) && !info->nointerp)
4365         {
4366           s = bfd_get_section_by_name (dynobj, ".interp");
4367           BFD_ASSERT (s != NULL);
4368           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4369           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4370         }
4371     }
4372
4373   /* Set up .got offsets for local syms, and space for local dynamic
4374      relocs.  */
4375   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4376     {
4377       bfd_signed_vma *local_got;
4378       bfd_signed_vma *end_local_got;
4379       bfd_size_type locsymcount;
4380       Elf_Internal_Shdr *symtab_hdr;
4381       asection *sgot;
4382       char *local_tls_type;
4383       unsigned long symndx;
4384       bfd_vma *local_tlsdesc_gotent;
4385
4386       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4387         continue;
4388
4389       for (s = ibfd->sections; s != NULL; s = s->next)
4390         {
4391           struct elf_dyn_relocs *p;
4392
4393           for (p = ((struct elf_dyn_relocs *)
4394                     elf_section_data (s)->local_dynrel);
4395                p != NULL; p = p->next)
4396             {
4397               if (!bfd_is_abs_section (p->sec)
4398                   && bfd_is_abs_section (p->sec->output_section))
4399                 {
4400                   /* Input section has been discarded, either because
4401                      it is a copy of a linkonce section or due to
4402                      linker script /DISCARD/, so we'll be discarding
4403                      the relocs too.  */
4404                 }
4405               else if (p->count != 0)
4406                 {
4407                   asection *sreloc = elf_section_data (p->sec)->sreloc;
4408                   sreloc->size += p->count * sizeof (Elf32_External_Rela);
4409                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4410                     info->flags |= DF_TEXTREL;
4411                 }
4412             }
4413         }
4414
4415       local_got = elf_local_got_refcounts (ibfd);
4416       if (!local_got)
4417         continue;
4418
4419       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4420       locsymcount = symtab_hdr->sh_info;
4421       end_local_got = local_got + locsymcount;
4422       sgot = elf_hash_table (info)->sgot;
4423       local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4424       local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4425       for (symndx = 0; local_got < end_local_got;
4426            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4427         {
4428           if (*local_got > 0)
4429             {
4430               int num_of_got_entry_needed = 0;
4431               *local_got = sgot->size;
4432               *local_tlsdesc_gotent = sgot->size;
4433
4434               /* TLS_NORMAL, and TLS_IE need one slot in .got.  */
4435               if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4436                 num_of_got_entry_needed = 1;
4437               /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT.  */
4438               else if (*local_tls_type & GOT_TLS_DESC)
4439                 num_of_got_entry_needed = 2;
4440
4441               sgot->size += (num_of_got_entry_needed << 2);
4442
4443               /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4444                  others need a slot in .rela.got.  */
4445               if (*local_tls_type == GOT_TLS_DESC)
4446                 {
4447                   if (bfd_link_pic (info))
4448                     {
4449                       if (htab->tls_desc_trampoline)
4450                         {
4451                           htab->num_tls_desc++;
4452                           htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4453                           htab->tls_trampoline = -1;
4454                         }
4455                       else
4456                         htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4457                     }
4458                   else
4459                     {
4460                       /* TLS_DESC -> TLS_LE  */
4461                     }
4462                 }
4463               else
4464                 {
4465                   htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4466                 }
4467             }
4468           else
4469             {
4470               *local_got = (bfd_vma) -1;
4471               *local_tlsdesc_gotent = (bfd_vma) -1;
4472             }
4473         }
4474     }
4475
4476   /* Allocate global sym .plt and .got entries, and space for global
4477      sym dynamic relocs.  */
4478   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4479
4480   /* For every jump slot reserved in the sgotplt, reloc_count is
4481      incremented.  However, when we reserve space for TLS descriptors,
4482      it's not incremented, so in order to compute the space reserved
4483      for them, it suffices to multiply the reloc count by the jump
4484      slot size.  */
4485   if (htab->tls_desc_trampoline && htab->root.srelplt)
4486     htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4487
4488   if (htab->tls_trampoline)
4489     {
4490       htab->tls_trampoline = htab->root.splt->size;
4491
4492       /* If we're not using lazy TLS relocations, don't generate the
4493          PLT and GOT entries they require.  */
4494       if (!(info->flags & DF_BIND_NOW))
4495         {
4496           htab->dt_tlsdesc_got = htab->root.sgot->size;
4497           htab->root.sgot->size += 4;
4498
4499           htab->dt_tlsdesc_plt = htab->root.splt->size;
4500           htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4501         }
4502     }
4503
4504   /* We now have determined the sizes of the various dynamic sections.
4505      Allocate memory for them.  */
4506   /* The check_relocs and adjust_dynamic_symbol entry points have
4507      determined the sizes of the various dynamic sections.  Allocate
4508      memory for them.  */
4509   plt = FALSE;
4510   relocs = FALSE;
4511   for (s = dynobj->sections; s != NULL; s = s->next)
4512     {
4513       if ((s->flags & SEC_LINKER_CREATED) == 0)
4514         continue;
4515
4516       if (s == htab->root.splt)
4517         {
4518           /* Strip this section if we don't need it; see the
4519              comment below.  */
4520           plt = s->size != 0;
4521         }
4522       else if (s == elf_hash_table (info)->sgot)
4523         {
4524           got_size += s->size;
4525         }
4526       else if (s == elf_hash_table (info)->sgotplt)
4527         {
4528           got_size += s->size;
4529         }
4530       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4531         {
4532           if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4533             relocs = TRUE;
4534
4535           /* We use the reloc_count field as a counter if we need
4536              to copy relocs into the output file.  */
4537           s->reloc_count = 0;
4538         }
4539       else
4540         {
4541           /* It's not one of our sections, so don't allocate space.  */
4542           continue;
4543         }
4544
4545       if (s->size == 0)
4546         {
4547           /* If we don't need this section, strip it from the
4548              output file.  This is mostly to handle .rela.bss and
4549              .rela.plt.  We must create both sections in
4550              create_dynamic_sections, because they must be created
4551              before the linker maps input sections to output
4552              sections.  The linker does that before
4553              adjust_dynamic_symbol is called, and it is that
4554              function which decides whether anything needs to go
4555              into these sections.  */
4556           s->flags |= SEC_EXCLUDE;
4557           continue;
4558         }
4559
4560       /* Allocate memory for the section contents.  We use bfd_zalloc
4561          here in case unused entries are not reclaimed before the
4562          section's contents are written out.  This should not happen,
4563          but this way if it does, we get a R_NDS32_NONE reloc instead
4564          of garbage.  */
4565       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4566       if (s->contents == NULL)
4567         return FALSE;
4568     }
4569
4570
4571   if (htab->root.dynamic_sections_created)
4572     {
4573       /* Add some entries to the .dynamic section.  We fill in the
4574          values later, in nds32_elf_finish_dynamic_sections, but we
4575          must add the entries now so that we get the correct size for
4576          the .dynamic section.  The DT_DEBUG entry is filled in by the
4577          dynamic linker and used by the debugger.  */
4578 #define add_dynamic_entry(TAG, VAL) \
4579   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4580
4581       if (bfd_link_executable (info))
4582         {
4583           if (!add_dynamic_entry (DT_DEBUG, 0))
4584             return FALSE;
4585         }
4586
4587       if (elf_hash_table (info)->splt->size != 0)
4588         {
4589           if (!add_dynamic_entry (DT_PLTGOT, 0)
4590               || !add_dynamic_entry (DT_PLTRELSZ, 0)
4591               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4592               || !add_dynamic_entry (DT_JMPREL, 0))
4593             return FALSE;
4594         }
4595
4596       if (htab->tls_desc_trampoline && plt)
4597         {
4598           if (htab->dt_tlsdesc_plt
4599               && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4600                   || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4601             return FALSE;
4602         }
4603
4604       if (relocs)
4605         {
4606           if (!add_dynamic_entry (DT_RELA, 0)
4607               || !add_dynamic_entry (DT_RELASZ, 0)
4608               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4609             return FALSE;
4610
4611           /* If any dynamic relocs apply to a read-only section,
4612              then we need a DT_TEXTREL entry.  */
4613           if ((info->flags & DF_TEXTREL) == 0)
4614             elf_link_hash_traverse (&htab->root, maybe_set_textrel,
4615                                     (void *) info);
4616
4617           if ((info->flags & DF_TEXTREL) != 0)
4618             {
4619               if (!add_dynamic_entry (DT_TEXTREL, 0))
4620                 return FALSE;
4621             }
4622         }
4623     }
4624 #undef add_dynamic_entry
4625
4626   return TRUE;
4627 }
4628
4629 static bfd_reloc_status_type
4630 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4631                          bfd_vma relocation, bfd_byte *location)
4632 {
4633   int size;
4634   bfd_vma x = 0;
4635   bfd_reloc_status_type flag;
4636   unsigned int rightshift = howto->rightshift;
4637   unsigned int bitpos = howto->bitpos;
4638
4639   /* If the size is negative, negate RELOCATION.  This isn't very
4640      general.  */
4641   if (howto->size < 0)
4642     relocation = -relocation;
4643
4644   /* Get the value we are going to relocate.  */
4645   size = bfd_get_reloc_size (howto);
4646   switch (size)
4647     {
4648     default:
4649       abort ();
4650       break;
4651     case 0:
4652       return bfd_reloc_ok;
4653     case 2:
4654       x = bfd_getb16 (location);
4655       break;
4656     case 4:
4657       x = bfd_getb32 (location);
4658       break;
4659     }
4660
4661   /* Check for overflow.  FIXME: We may drop bits during the addition
4662      which we don't check for.  We must either check at every single
4663      operation, which would be tedious, or we must do the computations
4664      in a type larger than bfd_vma, which would be inefficient.  */
4665   flag = bfd_reloc_ok;
4666   if (howto->complain_on_overflow != complain_overflow_dont)
4667     {
4668       bfd_vma addrmask, fieldmask, signmask, ss;
4669       bfd_vma a, b, sum;
4670
4671       /* Get the values to be added together.  For signed and unsigned
4672          relocations, we assume that all values should be truncated to
4673          the size of an address.  For bitfields, all the bits matter.
4674          See also bfd_check_overflow.  */
4675       fieldmask = N_ONES (howto->bitsize);
4676       signmask = ~fieldmask;
4677       addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4678       a = (relocation & addrmask) >> rightshift;
4679       b = (x & howto->src_mask & addrmask) >> bitpos;
4680
4681       switch (howto->complain_on_overflow)
4682         {
4683         case complain_overflow_signed:
4684           /* If any sign bits are set, all sign bits must be set.
4685              That is, A must be a valid negative address after
4686              shifting.  */
4687           signmask = ~(fieldmask >> 1);
4688           /* Fall through.  */
4689
4690         case complain_overflow_bitfield:
4691           /* Much like the signed check, but for a field one bit
4692              wider.  We allow a bitfield to represent numbers in the
4693              range -2**n to 2**n-1, where n is the number of bits in the
4694              field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4695              can't overflow, which is exactly what we want.  */
4696           ss = a & signmask;
4697           if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4698             flag = bfd_reloc_overflow;
4699
4700           /* We only need this next bit of code if the sign bit of B
4701              is below the sign bit of A.  This would only happen if
4702              SRC_MASK had fewer bits than BITSIZE.  Note that if
4703              SRC_MASK has more bits than BITSIZE, we can get into
4704              trouble; we would need to verify that B is in range, as
4705              we do for A above.  */
4706           ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4707           ss >>= bitpos;
4708
4709           /* Set all the bits above the sign bit.  */
4710           b = (b ^ ss) - ss;
4711
4712           /* Now we can do the addition.  */
4713           sum = a + b;
4714
4715           /* See if the result has the correct sign.  Bits above the
4716              sign bit are junk now; ignore them.  If the sum is
4717              positive, make sure we did not have all negative inputs;
4718              if the sum is negative, make sure we did not have all
4719              positive inputs.  The test below looks only at the sign
4720              bits, and it really just
4721              SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4722
4723              We mask with addrmask here to explicitly allow an address
4724              wrap-around.  The Linux kernel relies on it, and it is
4725              the only way to write assembler code which can run when
4726              loaded at a location 0x80000000 away from the location at
4727              which it is linked.  */
4728           if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4729             flag = bfd_reloc_overflow;
4730
4731           break;
4732
4733         case complain_overflow_unsigned:
4734           /* Checking for an unsigned overflow is relatively easy:
4735              trim the addresses and add, and trim the result as well.
4736              Overflow is normally indicated when the result does not
4737              fit in the field.  However, we also need to consider the
4738              case when, e.g., fieldmask is 0x7fffffff or smaller, an
4739              input is 0x80000000, and bfd_vma is only 32 bits; then we
4740              will get sum == 0, but there is an overflow, since the
4741              inputs did not fit in the field.  Instead of doing a
4742              separate test, we can check for this by or-ing in the
4743              operands when testing for the sum overflowing its final
4744              field.  */
4745           sum = (a + b) & addrmask;
4746           if ((a | b | sum) & signmask)
4747             flag = bfd_reloc_overflow;
4748           break;
4749
4750         default:
4751           abort ();
4752         }
4753     }
4754
4755   /* Put RELOCATION in the right bits.  */
4756   relocation >>= (bfd_vma) rightshift;
4757   relocation <<= (bfd_vma) bitpos;
4758
4759   /* Add RELOCATION to the right bits of X.  */
4760   /* FIXME : 090616
4761      Because the relaxation may generate duplicate relocation at one address,
4762      an addition to immediate in the instruction may cause the relocation added
4763      several times.
4764      This bug should be fixed in assembler, but a check is also needed here.  */
4765   if (howto->partial_inplace)
4766     x = ((x & ~howto->dst_mask)
4767          | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4768   else
4769     x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4770
4771
4772   /* Put the relocated value back in the object file.  */
4773   switch (size)
4774     {
4775     default:
4776     case 0:
4777     case 1:
4778     case 8:
4779       abort ();
4780       break;
4781     case 2:
4782       bfd_putb16 (x, location);
4783       break;
4784     case 4:
4785       bfd_putb32 (x, location);
4786       break;
4787     }
4788
4789   return flag;
4790 }
4791
4792 static bfd_reloc_status_type
4793 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4794                                asection *input_section, bfd_byte *contents,
4795                                bfd_vma address, bfd_vma value, bfd_vma addend)
4796 {
4797   bfd_vma relocation;
4798
4799   /* Sanity check the address.  */
4800   if (address > bfd_get_section_limit (input_bfd, input_section))
4801     return bfd_reloc_outofrange;
4802
4803   /* This function assumes that we are dealing with a basic relocation
4804      against a symbol.  We want to compute the value of the symbol to
4805      relocate to.  This is just VALUE, the value of the symbol, plus
4806      ADDEND, any addend associated with the reloc.  */
4807   relocation = value + addend;
4808
4809   /* If the relocation is PC relative, we want to set RELOCATION to
4810      the distance between the symbol (currently in RELOCATION) and the
4811      location we are relocating.  If pcrel_offset is FALSE we do not
4812      need to subtract out the offset of the location within the
4813      section (which is just ADDRESS).  */
4814   if (howto->pc_relative)
4815     {
4816       relocation -= (input_section->output_section->vma
4817                      + input_section->output_offset);
4818       if (howto->pcrel_offset)
4819         relocation -= address;
4820     }
4821
4822   return nds32_relocate_contents (howto, input_bfd, relocation,
4823                                   contents + address);
4824 }
4825
4826 static bfd_boolean
4827 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4828                               const char *name,
4829                               Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4830                               asection *input_sec,
4831                               struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4832 {
4833   const char *source;
4834   FILE *sym_ld_script = NULL;
4835   struct elf_nds32_link_hash_table *table;
4836
4837   table = nds32_elf_hash_table (info);
4838   sym_ld_script = table->sym_ld_script;
4839   if (!sym_ld_script)
4840     return TRUE;
4841
4842   if (!h || !name || *name == '\0')
4843     return TRUE;
4844
4845   if (input_sec->flags & SEC_EXCLUDE)
4846     return TRUE;
4847
4848   if (!check_start_export_sym)
4849     {
4850       fprintf (sym_ld_script, "SECTIONS\n{\n");
4851       check_start_export_sym = 1;
4852     }
4853
4854   if (h->root.type == bfd_link_hash_defined
4855       || h->root.type == bfd_link_hash_defweak)
4856     {
4857       if (!h->root.u.def.section->output_section)
4858         return TRUE;
4859
4860       if (bfd_is_const_section (input_sec))
4861         source = input_sec->name;
4862       else
4863         source = input_sec->owner->filename;
4864
4865       fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4866                h->root.root.string,
4867                (long) (h->root.u.def.value
4868                 + h->root.u.def.section->output_section->vma
4869                 + h->root.u.def.section->output_offset), source);
4870     }
4871
4872   return TRUE;
4873 }
4874
4875 /* Relocate an NDS32/D ELF section.
4876    There is some attempt to make this function usable for many architectures,
4877    both for RELA and REL type relocs, if only to serve as a learning tool.
4878
4879    The RELOCATE_SECTION function is called by the new ELF backend linker
4880    to handle the relocations for a section.
4881
4882    The relocs are always passed as Rela structures; if the section
4883    actually uses Rel structures, the r_addend field will always be
4884    zero.
4885
4886    This function is responsible for adjust the section contents as
4887    necessary, and (if using Rela relocs and generating a
4888    relocatable output file) adjusting the reloc addend as
4889    necessary.
4890
4891    This function does not have to worry about setting the reloc
4892    address or the reloc symbol index.
4893
4894    LOCAL_SYMS is a pointer to the swapped in local symbols.
4895
4896    LOCAL_SECTIONS is an array giving the section in the input file
4897    corresponding to the st_shndx field of each local symbol.
4898
4899    The global hash table entry for the global symbols can be found
4900    via elf_sym_hashes (input_bfd).
4901
4902    When generating relocatable output, this function must handle
4903    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4904    going to be the section symbol corresponding to the output
4905    section, which means that the addend must be adjusted
4906    accordingly.  */
4907
4908 /* Return the base VMA address which should be subtracted from real addresses
4909    when resolving @dtpoff relocation.
4910    This is PT_TLS segment p_vaddr.  */
4911
4912 /* Return the relocation value for @tpoff relocation
4913    if STT_TLS virtual address is ADDRESS.  */
4914
4915 /* Return the relocation value for @gottpoff relocation
4916    if STT_TLS virtual address is ADDRESS.  */
4917
4918 static bfd_vma
4919 gottpoff (struct bfd_link_info *info, bfd_vma address)
4920 {
4921   bfd_vma tp_base;
4922   bfd_vma tp_offset;
4923
4924   /* If tls_sec is NULL, we should have signalled an error already.  */
4925   if (elf_hash_table (info)->tls_sec == NULL)
4926     return 0;
4927
4928   tp_base = elf_hash_table (info)->tls_sec->vma;
4929   tp_offset = address - tp_base;
4930
4931   return tp_offset;
4932 }
4933
4934 static bfd_boolean
4935 patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4936 {
4937   /* TLS_GD/TLS_LD model #1
4938      46 00 00 00 sethi $r0,#0x0
4939      58 00 00 00 ori $r0,$r0,#0x0
4940      40 00 74 00 add $r0,$r0,$gp
4941      04 10 00 00 lwi $r1,[$r0+#0x0]
4942      4b e0 04 01 jral $lp,$r1  */
4943
4944   /* TLS_GD/TLS_LD model #2
4945      46 00 00 00 sethi $r0,#0x0
4946      58 00 00 00 ori $r0,$r0,#0x0
4947      38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4948      40 00 74 00 add $r0,$r0,$gp
4949      4b e0 04 01 jral $lp,$r1  */
4950
4951   /* TLS_IE model (non-PIC)
4952      46 00 00 00 sethi $r0,#0x0
4953      04 00 00 00 lwi $r0,[$r0+#0x0]
4954      38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4955
4956   /* TLS_IE model (PIC)
4957      46 00 00 00 sethi $r0,#0x0
4958      58 00 00 00 ori $r0,$r0,#0x0
4959      38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4960      38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4961
4962   /* TLS_GD_TO_IE model
4963      46 00 00 00 sethi $r0,#0x0
4964      58 00 00 00 ori $r0,$r0,#0x0
4965      40 00 74 00 add $r0,$rM,$gp
4966      04 00 00 01 lwi $r0,[$r0+#0x4]
4967      40 00 64 00 add $r0,$r0,$r25  */
4968
4969   bfd_boolean rz = FALSE;
4970
4971   typedef struct
4972     {
4973       uint32_t opcode;
4974       uint32_t mask;
4975     } pat_t;
4976
4977   uint32_t patch[3] =
4978     {
4979       0x40007400, /* add $r0,$rM,$gp     */
4980       0x04000001, /* lwi $r0,[$r0+#0x4]  */
4981       0x40006400, /* add $r0,$r0,$r25    */
4982     };
4983
4984   pat_t mode0[3] =
4985     {
4986         { 0x40000000, 0xfe0003ff },
4987         { 0x04000000, 0xfe000000 },
4988         { 0x4be00001, 0xffff83ff },
4989     };
4990
4991   pat_t mode1[3] =
4992     {
4993         { 0x38007402, 0xfe007fff },
4994         { 0x40007400, 0xfe007fff },
4995         { 0x4be00001, 0xffff83ff },
4996     };
4997
4998   unsigned char *p = contents + rel->r_offset;
4999
5000   uint32_t insn;
5001   uint32_t regidx = 0;
5002   insn = bfd_getb32 (p);
5003   if (INSN_SETHI == (0xfe0fffffu & insn))
5004     {
5005       regidx = 0x1f & (insn >> 20);
5006       p += 4;
5007     }
5008
5009   insn = bfd_getb32 (p);
5010   if (INSN_ORI == (0xfe007fffu & insn))
5011     {
5012       regidx = 0x1f & (insn >> 20);
5013       p += 4;
5014     }
5015
5016   if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction.  */
5017     {
5018       /* already patched?  */
5019       if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
5020           (patch[1] == bfd_getb32 (p + 4)))
5021         rz = TRUE;
5022     }
5023   else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
5024     {
5025       if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
5026           (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
5027         {
5028           bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5029           bfd_putb32 (patch[1], p + 4);
5030           bfd_putb32 (patch[2], p + 8);
5031           rz = TRUE;
5032         }
5033     }
5034   else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
5035     {
5036       if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
5037           (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5038         {
5039           bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5040           bfd_putb32 (patch[1], p + 4);
5041           bfd_putb32 (patch[2], p + 8);
5042           rz = TRUE;
5043         }
5044     }
5045
5046   if (!rz)
5047     {
5048       printf ("%s: %s @ 0x%08x\n", __func__, ibfd->filename,
5049               (int) rel->r_offset);
5050       BFD_ASSERT(0); /* Unsupported pattern.  */
5051     }
5052
5053   return rz;
5054 }
5055
5056 static enum elf_nds32_tls_type
5057 get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5058
5059 static unsigned int
5060 ones32 (register unsigned int x)
5061 {
5062   /* 32-bit recursive reduction using SWAR...
5063      but first step is mapping 2-bit values
5064      into sum of 2 1-bit values in sneaky way.  */
5065   x -= ((x >> 1) & 0x55555555);
5066   x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5067   x = (((x >> 4) + x) & 0x0f0f0f0f);
5068   x += (x >> 8);
5069   x += (x >> 16);
5070   return (x & 0x0000003f);
5071 }
5072
5073 #if !HAVE_FLS
5074 static unsigned int
5075 fls (register unsigned int x)
5076 {
5077   return ffs (x & (-x));
5078 }
5079 #endif /* !HAVE_FLS */
5080
5081 #define nds32_elf_local_tlsdesc_gotent(bfd) \
5082   (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5083
5084 static bfd_boolean
5085 nds32_elf_relocate_section (bfd *                  output_bfd ATTRIBUTE_UNUSED,
5086                             struct bfd_link_info * info,
5087                             bfd *                  input_bfd,
5088                             asection *             input_section,
5089                             bfd_byte *             contents,
5090                             Elf_Internal_Rela *    relocs,
5091                             Elf_Internal_Sym *     local_syms,
5092                             asection **            local_sections)
5093 {
5094   Elf_Internal_Shdr *symtab_hdr;
5095   struct elf_link_hash_entry **sym_hashes;
5096   Elf_Internal_Rela *rel, *relend;
5097   bfd_boolean ret = TRUE;               /* Assume success.  */
5098   int align = 0;
5099   bfd_reloc_status_type r;
5100   const char *errmsg = NULL;
5101   bfd_vma gp;
5102   struct elf_link_hash_table *ehtab;
5103   struct elf_nds32_link_hash_table *htab;
5104   bfd *dynobj;
5105   bfd_vma *local_got_offsets;
5106   asection *sgot, *splt, *sreloc;
5107   bfd_vma high_address;
5108   struct elf_nds32_link_hash_table *table;
5109   int eliminate_gc_relocs;
5110   bfd_vma fpbase_addr;
5111
5112   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5113   sym_hashes = elf_sym_hashes (input_bfd);
5114   ehtab = elf_hash_table (info);
5115   htab = nds32_elf_hash_table (info);
5116   high_address = bfd_get_section_limit (input_bfd, input_section);
5117
5118   dynobj = htab->root.dynobj;
5119   local_got_offsets = elf_local_got_offsets (input_bfd);
5120
5121   sgot = ehtab->sgot;
5122   splt = ehtab->splt;
5123   sreloc = NULL;
5124
5125   rel = relocs;
5126   relend = relocs + input_section->reloc_count;
5127
5128   table = nds32_elf_hash_table (info);
5129   eliminate_gc_relocs = table->eliminate_gc_relocs;
5130
5131   /* By this time, we can adjust the value of _SDA_BASE_.  */
5132   /* Explain _SDA_BASE_  */
5133   if ((!bfd_link_relocatable (info)))
5134     {
5135       is_SDA_BASE_set = 1;
5136       r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5137       if (r != bfd_reloc_ok)
5138         return FALSE;
5139     }
5140
5141   /* Do TLS model conversion once at first.  */
5142   nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5143
5144   /* Use gp as fp to prevent truncated fit.  Because in relaxation time
5145      the fp value is set as gp, and it has be reverted for instruction
5146      setting fp.  */
5147   fpbase_addr = elf_gp (output_bfd);
5148
5149   /* Deal with (dynamic) relocations.  */
5150   for (rel = relocs; rel < relend; rel++)
5151     {
5152       enum elf_nds32_reloc_type r_type;
5153       reloc_howto_type *howto = NULL;
5154       unsigned long r_symndx;
5155       struct elf_link_hash_entry *h = NULL;
5156       Elf_Internal_Sym *sym = NULL;
5157       asection *sec;
5158       bfd_vma relocation;
5159       bfd_vma relocation_sym = 0xdeadbeef;
5160       Elf_Internal_Rela *lorel;
5161       bfd_vma off;
5162
5163       /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5164          ensure it's zero (we use REL relocs, not RELA).  Therefore this
5165          should be assigning zero to `addend', but for clarity we use
5166          `r_addend'.  */
5167
5168       bfd_vma addend = rel->r_addend;
5169       bfd_vma offset = rel->r_offset;
5170
5171       r_type = ELF32_R_TYPE (rel->r_info);
5172       if (r_type >= R_NDS32_max)
5173         {
5174           /* xgettext:c-format */
5175           _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5176                               input_bfd, r_type);
5177           bfd_set_error (bfd_error_bad_value);
5178           ret = FALSE;
5179           continue;
5180         }
5181
5182       if (r_type == R_NDS32_GNU_VTENTRY
5183           || r_type == R_NDS32_GNU_VTINHERIT
5184           || r_type == R_NDS32_NONE
5185           || r_type == R_NDS32_RELA_GNU_VTENTRY
5186           || r_type == R_NDS32_RELA_GNU_VTINHERIT
5187           || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5188           || r_type == R_NDS32_DATA
5189           || r_type == R_NDS32_TRAN)
5190         continue;
5191
5192       /* If we enter the fp-as-gp region.  Resolve the address
5193          of best fp-base.  */
5194       if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5195           && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5196         {
5197           int dist;
5198
5199           /* Distance to relocation of best fp-base is encoded in R_SYM.  */
5200           dist =  rel->r_addend >> 16;
5201           fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5202                                                   local_syms, symtab_hdr);
5203         }
5204       else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5205                && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5206         {
5207           fpbase_addr = elf_gp (output_bfd);
5208         }
5209
5210       /* Skip the relocations used for relaxation.  */
5211       /* We have to update LONGCALL and LONGJUMP
5212          relocations when generating the relocatable files.  */
5213       if (!bfd_link_relocatable (info)
5214           && (r_type >= R_NDS32_RELAX_ENTRY
5215               || (r_type >= R_NDS32_LONGCALL4
5216                   && r_type <= R_NDS32_LONGJUMP7)))
5217         continue;
5218
5219       howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5220       r_symndx = ELF32_R_SYM (rel->r_info);
5221
5222       /* This is a final link.  */
5223       sym = NULL;
5224       sec = NULL;
5225       h = NULL;
5226
5227       if (r_symndx < symtab_hdr->sh_info)
5228         {
5229           /* Local symbol.  */
5230           sym = local_syms + r_symndx;
5231           sec = local_sections[r_symndx];
5232
5233           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5234           addend = rel->r_addend;
5235
5236           /* keep symbol location for static TLS_IE GOT entry  */
5237           relocation_sym = relocation;
5238           if (bfd_link_relocatable (info))
5239             {
5240               /* This is a relocatable link.  We don't have to change
5241                  anything, unless the reloc is against a section symbol,
5242                  in which case we have to adjust according to where the
5243                  section symbol winds up in the output section.  */
5244               if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5245                 rel->r_addend += sec->output_offset + sym->st_value;
5246
5247               continue;
5248             }
5249         }
5250       else
5251         {
5252           /* External symbol.  */
5253           if (bfd_link_relocatable (info))
5254             continue;
5255           bfd_boolean warned, ignored, unresolved_reloc;
5256           int symndx = r_symndx - symtab_hdr->sh_info;
5257
5258           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5259                                    r_symndx, symtab_hdr, sym_hashes, h, sec,
5260                                    relocation, unresolved_reloc, warned,
5261                                    ignored);
5262
5263           /* keep symbol location for static TLS_IE GOT entry  */
5264           relocation_sym = relocation;
5265
5266           /* la $fp, _FP_BASE_ is per-function (region).
5267              Handle it specially.  */
5268           switch ((int) r_type)
5269             {
5270             case R_NDS32_HI20_RELA:
5271             case R_NDS32_LO12S0_RELA:
5272               if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5273                           FP_BASE_NAME) == 0)
5274                 {
5275                   if (!bfd_link_pie (info))
5276                     {
5277                       _bfd_error_handler
5278                         ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5279                          input_bfd);
5280                     }
5281                   relocation = fpbase_addr;
5282                 }
5283               break;
5284             case R_NDS32_SDA19S0_RELA:
5285             case R_NDS32_SDA15S0_RELA:
5286             case R_NDS32_20_RELA:
5287               if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5288                           FP_BASE_NAME) == 0)
5289                 {
5290                   relocation = fpbase_addr;
5291                   break;
5292                 }
5293             }
5294         }
5295
5296       /* Sanity check the address.  */
5297       if (offset > high_address)
5298         {
5299           r = bfd_reloc_outofrange;
5300           goto check_reloc;
5301         }
5302
5303       if (r_type >= R_NDS32_RELAX_ENTRY)
5304         continue;
5305
5306       switch ((int) r_type)
5307         {
5308         case R_NDS32_GOTOFF:
5309           /* Relocation is relative to the start of the global offset
5310              table (for ld24 rx, #uimm24), e.g. access at label+addend
5311
5312              ld24 rx. #label@GOTOFF + addend
5313              sub  rx, r12.  */
5314         case R_NDS32_GOTOFF_HI20:
5315         case R_NDS32_GOTOFF_LO12:
5316         case R_NDS32_GOTOFF_LO15:
5317         case R_NDS32_GOTOFF_LO19:
5318           BFD_ASSERT (sgot != NULL);
5319
5320           relocation -= elf_gp (output_bfd);
5321           break;
5322
5323         case R_NDS32_9_PLTREL:
5324         case R_NDS32_25_PLTREL:
5325           /* Relocation is to the entry for this symbol in the
5326              procedure linkage table.  */
5327
5328           /* The native assembler will generate a 25_PLTREL reloc
5329              for a local symbol if you assemble a call from one
5330              section to another when using -K pic.  */
5331           if (h == NULL)
5332             break;
5333
5334           if (h->forced_local)
5335             break;
5336
5337           /* We didn't make a PLT entry for this symbol.  This
5338              happens when statically linking PIC code, or when
5339              using -Bsymbolic.  */
5340           if (h->plt.offset == (bfd_vma) - 1)
5341             break;
5342
5343           relocation = (splt->output_section->vma
5344                         + splt->output_offset + h->plt.offset);
5345           break;
5346
5347         case R_NDS32_PLT_GOTREL_HI20:
5348         case R_NDS32_PLT_GOTREL_LO12:
5349         case R_NDS32_PLT_GOTREL_LO15:
5350         case R_NDS32_PLT_GOTREL_LO19:
5351         case R_NDS32_PLT_GOTREL_LO20:
5352           if (h == NULL
5353               || h->forced_local
5354               || h->plt.offset == (bfd_vma) -1
5355               || (bfd_link_pie (info) && h->def_regular))
5356             {
5357               /* Maybe we should find better checking to optimize
5358                  PIE PLT relocations.  */
5359               /* We didn't make a PLT entry for this symbol.  This
5360                  happens when statically linking PIC code, or when
5361                  using -Bsymbolic.  */
5362               if (h)
5363                 h->plt.offset = (bfd_vma) -1;   /* Cancel PLT trampoline.  */
5364               relocation -= elf_gp (output_bfd);
5365               break;
5366             }
5367
5368           relocation = (splt->output_section->vma
5369                         + splt->output_offset + h->plt.offset);
5370
5371           relocation -= elf_gp (output_bfd);
5372           break;
5373
5374         case R_NDS32_PLTREL_HI20:
5375         case R_NDS32_PLTREL_LO12:
5376
5377           /* Relocation is to the entry for this symbol in the
5378              procedure linkage table.  */
5379
5380           /* The native assembler will generate a 25_PLTREL reloc
5381              for a local symbol if you assemble a call from one
5382              section to another when using -K pic.  */
5383           if (h == NULL)
5384             break;
5385
5386           if (h->forced_local)
5387             break;
5388
5389           if (h->plt.offset == (bfd_vma) - 1)
5390             /* We didn't make a PLT entry for this symbol.  This
5391                happens when statically linking PIC code, or when
5392                using -Bsymbolic.  */
5393             break;
5394
5395           if (splt == NULL)
5396             break;
5397
5398           relocation = (splt->output_section->vma
5399                         + splt->output_offset
5400                         + h->plt.offset + 4)
5401                        - (input_section->output_section->vma
5402                           + input_section->output_offset
5403                           + rel->r_offset);
5404
5405           break;
5406
5407         case R_NDS32_GOTPC20:
5408           /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5409              ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
5410           relocation = elf_gp (output_bfd);
5411           break;
5412
5413         case R_NDS32_GOTPC_HI20:
5414         case R_NDS32_GOTPC_LO12:
5415           /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5416              bl .+4
5417              seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5418              or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5419              or
5420              bl .+4
5421              seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5422              add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)  */
5423           relocation = elf_gp (output_bfd);
5424           relocation -= (input_section->output_section->vma
5425                          + input_section->output_offset + rel->r_offset);
5426           break;
5427
5428         case R_NDS32_GOT20:
5429           /* Fall through.  */
5430         case R_NDS32_GOT_HI20:
5431         case R_NDS32_GOT_LO12:
5432         case R_NDS32_GOT_LO15:
5433         case R_NDS32_GOT_LO19:
5434           /* Relocation is to the entry for this symbol in the global
5435              offset table.  */
5436           BFD_ASSERT (sgot != NULL);
5437
5438           if (h != NULL)
5439             {
5440               /* External symbol  */
5441               bfd_boolean dyn;
5442
5443               off = h->got.offset;
5444               BFD_ASSERT (off != (bfd_vma) - 1);
5445               dyn = htab->root.dynamic_sections_created;
5446               if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5447                                                     bfd_link_pic (info),
5448                                                     h)
5449                   || (bfd_link_pic (info)
5450                       && (info->symbolic
5451                           || h->dynindx == -1
5452                           || h->forced_local) && h->def_regular))
5453                 {
5454                   /* This is actually a static link, or it is a
5455                      -Bsymbolic link and the symbol is defined
5456                      locally, or the symbol was forced to be local
5457                      because of a version file.  We must initialize
5458                      this entry in the global offset table.  Since the
5459                      offset must always be a multiple of 4, we use the
5460                      least significant bit to record whether we have
5461                      initialized it already.
5462
5463                      When doing a dynamic link, we create a .rela.got
5464                      relocation entry to initialize the value.  This
5465                      is done in the finish_dynamic_symbol routine.  */
5466                   if ((off & 1) != 0)   /* clear LSB  */
5467                     off &= ~1;
5468                   else
5469                     {
5470                       bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5471                       h->got.offset |= 1;
5472                     }
5473                 }
5474               relocation = sgot->output_section->vma + sgot->output_offset + off
5475                            - elf_gp (output_bfd);
5476             }
5477           else
5478             {
5479               /* Local symbol  */
5480               bfd_byte *loc;
5481
5482               BFD_ASSERT (local_got_offsets != NULL
5483                           && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5484
5485               off = local_got_offsets[r_symndx];
5486
5487               /* The offset must always be a multiple of 4.  We use
5488                  the least significant bit to record whether we have
5489                  already processed this entry.  */
5490               if ((off & 1) != 0)       /* clear LSB  */
5491                 off &= ~1;
5492               else
5493                 {
5494                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5495
5496                   if (bfd_link_pic (info))
5497                     {
5498                       asection *srelgot;
5499                       Elf_Internal_Rela outrel;
5500
5501                       /* We need to generate a R_NDS32_RELATIVE reloc
5502                          for the dynamic linker.  */
5503                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5504                       BFD_ASSERT (srelgot != NULL);
5505
5506                       outrel.r_offset = (elf_gp (output_bfd)
5507                                          + sgot->output_offset + off);
5508                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5509                       outrel.r_addend = relocation;
5510                       loc = srelgot->contents;
5511                       loc +=
5512                         srelgot->reloc_count * sizeof (Elf32_External_Rela);
5513                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5514                       ++srelgot->reloc_count;
5515                     }
5516                   local_got_offsets[r_symndx] |= 1;
5517                 }
5518               relocation = sgot->output_section->vma + sgot->output_offset + off
5519                            - elf_gp (output_bfd);
5520             }
5521
5522           break;
5523
5524         case R_NDS32_16_RELA:
5525         case R_NDS32_20_RELA:
5526         case R_NDS32_5_RELA:
5527         case R_NDS32_32_RELA:
5528         case R_NDS32_9_PCREL_RELA:
5529         case R_NDS32_WORD_9_PCREL_RELA:
5530         case R_NDS32_10_UPCREL_RELA:
5531         case R_NDS32_15_PCREL_RELA:
5532         case R_NDS32_17_PCREL_RELA:
5533         case R_NDS32_25_PCREL_RELA:
5534         case R_NDS32_HI20_RELA:
5535         case R_NDS32_LO12S3_RELA:
5536         case R_NDS32_LO12S2_RELA:
5537         case R_NDS32_LO12S2_DP_RELA:
5538         case R_NDS32_LO12S2_SP_RELA:
5539         case R_NDS32_LO12S1_RELA:
5540         case R_NDS32_LO12S0_RELA:
5541         case R_NDS32_LO12S0_ORI_RELA:
5542           if (bfd_link_pic (info) && r_symndx != 0
5543               && (input_section->flags & SEC_ALLOC) != 0
5544               && (eliminate_gc_relocs == 0
5545                   || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5546               && ((r_type != R_NDS32_9_PCREL_RELA
5547                    && r_type != R_NDS32_WORD_9_PCREL_RELA
5548                    && r_type != R_NDS32_10_UPCREL_RELA
5549                    && r_type != R_NDS32_15_PCREL_RELA
5550                    && r_type != R_NDS32_17_PCREL_RELA
5551                    && r_type != R_NDS32_25_PCREL_RELA
5552                    && !(r_type == R_NDS32_32_RELA
5553                         && strcmp (input_section->name, ".eh_frame") == 0))
5554                   || (h != NULL && h->dynindx != -1
5555                       && (!info->symbolic || !h->def_regular))))
5556             {
5557               Elf_Internal_Rela outrel;
5558               bfd_boolean skip, relocate;
5559               bfd_byte *loc;
5560
5561               /* When generating a shared object, these relocations
5562                  are copied into the output file to be resolved at run
5563                  time.  */
5564
5565               if (sreloc == NULL)
5566                 {
5567                   const char *name;
5568
5569                   name = bfd_elf_string_from_elf_section
5570                     (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5571                      elf_section_data (input_section)->rela.hdr->sh_name);
5572                   if (name == NULL)
5573                     return FALSE;
5574
5575                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5576                               && strcmp (bfd_get_section_name (input_bfd,
5577                                                                input_section),
5578                                          name + 5) == 0);
5579
5580                   sreloc = bfd_get_section_by_name (dynobj, name);
5581                   BFD_ASSERT (sreloc != NULL);
5582                 }
5583
5584               skip = FALSE;
5585               relocate = FALSE;
5586
5587               outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5588                                                          info,
5589                                                          input_section,
5590                                                          rel->r_offset);
5591               if (outrel.r_offset == (bfd_vma) - 1)
5592                 skip = TRUE;
5593               else if (outrel.r_offset == (bfd_vma) - 2)
5594                 skip = TRUE, relocate = TRUE;
5595               outrel.r_offset += (input_section->output_section->vma
5596                                   + input_section->output_offset);
5597
5598               if (skip)
5599                 memset (&outrel, 0, sizeof outrel);
5600               else if (r_type == R_NDS32_17_PCREL_RELA
5601                        || r_type == R_NDS32_15_PCREL_RELA
5602                        || r_type == R_NDS32_25_PCREL_RELA)
5603                 {
5604                   BFD_ASSERT (h != NULL && h->dynindx != -1);
5605                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5606                   outrel.r_addend = rel->r_addend;
5607                 }
5608               else
5609                 {
5610                   /* h->dynindx may be -1 if this symbol was marked to
5611                      become local.  */
5612                   if (h == NULL
5613                       || ((info->symbolic || h->dynindx == -1)
5614                           && h->def_regular)
5615                       || (bfd_link_pie (info) && h->def_regular))
5616                     {
5617                       relocate = TRUE;
5618                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5619                       outrel.r_addend = relocation + rel->r_addend;
5620
5621                       if (h)
5622                         {
5623                           h->plt.offset = (bfd_vma) -1;   /* cancel PLT trampoline.  */
5624
5625                           BFD_ASSERT (sgot != NULL);
5626                           /* If we did not allocate got entry for the symbol,
5627                              we can not fill the nonexistent got entry.  */
5628                           if (h->got.offset != (bfd_vma) -1
5629                               && (h->got.offset & 1) == 0)
5630                             {
5631                               bfd_put_32 (output_bfd, outrel.r_addend,
5632                                           sgot->contents + h->got.offset);
5633                             }
5634                         }
5635                     }
5636                   else
5637                     {
5638                       if (h->dynindx == -1)
5639                         {
5640                           _bfd_error_handler
5641                             (_("%pB: relocation %s against `%s' can not be used when"
5642                                "making a shared object; recompile with -fPIC"),
5643                              input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5644                           bfd_set_error (bfd_error_bad_value);
5645                           return FALSE;
5646                         }
5647
5648                       outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5649                       outrel.r_addend = rel->r_addend;
5650                     }
5651                 }
5652
5653               loc = sreloc->contents;
5654               loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5655               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5656               ++sreloc->reloc_count;
5657
5658               /* If this reloc is against an external symbol, we do
5659                  not want to fiddle with the addend.  Otherwise, we
5660                  need to include the symbol value so that it becomes
5661                  an addend for the dynamic reloc.  */
5662               if (!relocate)
5663                 continue;
5664             }
5665           break;
5666
5667         case R_NDS32_25_ABS_RELA:
5668           if (bfd_link_pic (info))
5669             {
5670               _bfd_error_handler
5671                 (_("%pB: warning: %s unsupported in shared mode"),
5672                  input_bfd, "R_NDS32_25_ABS_RELA");
5673               return FALSE;
5674             }
5675           break;
5676
5677         case R_NDS32_9_PCREL:
5678           r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5679                                           contents, offset,
5680                                           sec, relocation, addend);
5681           goto check_reloc;
5682
5683         case R_NDS32_HI20:
5684           /* We allow an arbitrary number of HI20 relocs before the
5685              LO12 reloc.  This permits gcc to emit the HI and LO relocs
5686              itself.  */
5687           for (lorel = rel + 1;
5688                (lorel < relend
5689                 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5690             continue;
5691           if (lorel < relend
5692               && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5693                   || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5694                   || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5695                   || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5696             {
5697               nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5698                                        contents, relocation + addend);
5699               r = bfd_reloc_ok;
5700             }
5701           else
5702             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5703                                           contents, offset, relocation,
5704                                           addend);
5705           goto check_reloc;
5706
5707         case R_NDS32_GOT17S2_RELA:
5708         case R_NDS32_GOT15S2_RELA:
5709           BFD_ASSERT (sgot != NULL);
5710
5711           if (h != NULL)
5712             {
5713               bfd_boolean dyn;
5714
5715               off = h->got.offset;
5716               BFD_ASSERT (off != (bfd_vma) - 1);
5717
5718               dyn = htab->root.dynamic_sections_created;
5719               if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5720                   (dyn, bfd_link_pic (info), h)
5721                   || (bfd_link_pic (info)
5722                       && (info->symbolic
5723                           || h->dynindx == -1
5724                           || h->forced_local)
5725                       && h->def_regular))
5726                 {
5727                   /* This is actually a static link, or it is a
5728                      -Bsymbolic link and the symbol is defined
5729                      locally, or the symbol was forced to be local
5730                      because of a version file.  We must initialize
5731                      this entry in the global offset table.  Since the
5732                      offset must always be a multiple of 4, we use the
5733                      least significant bit to record whether we have
5734                      initialized it already.
5735
5736                      When doing a dynamic link, we create a .rela.got
5737                      relocation entry to initialize the value.  This
5738                      is done in the finish_dynamic_symbol routine.  */
5739                   if ((off & 1) != 0)
5740                     off &= ~1;
5741                   else
5742                     {
5743                       bfd_put_32 (output_bfd, relocation,
5744                                   sgot->contents + off);
5745                       h->got.offset |= 1;
5746                     }
5747                 }
5748             }
5749           else
5750             {
5751               bfd_byte *loc;
5752
5753               BFD_ASSERT (local_got_offsets != NULL
5754                           && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5755
5756               off = local_got_offsets[r_symndx];
5757
5758               /* The offset must always be a multiple of 4.  We use
5759                  the least significant bit to record whether we have
5760                  already processed this entry.  */
5761               if ((off & 1) != 0)
5762                 off &= ~1;
5763               else
5764                 {
5765                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5766
5767                   if (bfd_link_pic (info))
5768                     {
5769                       asection *srelgot;
5770                       Elf_Internal_Rela outrel;
5771
5772                       /* We need to generate a R_NDS32_RELATIVE reloc
5773                          for the dynamic linker.  */
5774                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5775                       BFD_ASSERT (srelgot != NULL);
5776
5777                       outrel.r_offset = (elf_gp (output_bfd)
5778                                          + sgot->output_offset + off);
5779                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5780                       outrel.r_addend = relocation;
5781                       loc = srelgot->contents;
5782                       loc +=
5783                         srelgot->reloc_count * sizeof (Elf32_External_Rela);
5784                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5785                       ++srelgot->reloc_count;
5786                     }
5787                   local_got_offsets[r_symndx] |= 1;
5788                 }
5789             }
5790           relocation = sgot->output_section->vma + sgot->output_offset + off
5791             - elf_gp (output_bfd);
5792
5793           if (relocation & align)
5794             {
5795               /* Incorrect alignment.  */
5796               _bfd_error_handler
5797                 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5798               ret = FALSE;
5799               r = bfd_reloc_dangerous;
5800               goto check_reloc;
5801             }
5802           break;
5803
5804         case R_NDS32_SDA16S3_RELA:
5805         case R_NDS32_SDA15S3_RELA:
5806         case R_NDS32_SDA15S3:
5807           align = 0x7;
5808           goto handle_sda;
5809
5810         case R_NDS32_SDA17S2_RELA:
5811         case R_NDS32_SDA15S2_RELA:
5812         case R_NDS32_SDA12S2_SP_RELA:
5813         case R_NDS32_SDA12S2_DP_RELA:
5814         case R_NDS32_SDA15S2:
5815         case R_NDS32_SDA_FP7U2_RELA:
5816           align = 0x3;
5817           goto handle_sda;
5818
5819         case R_NDS32_SDA18S1_RELA:
5820         case R_NDS32_SDA15S1_RELA:
5821         case R_NDS32_SDA15S1:
5822           align = 0x1;
5823           goto handle_sda;
5824
5825         case R_NDS32_SDA19S0_RELA:
5826         case R_NDS32_SDA15S0_RELA:
5827         case R_NDS32_SDA15S0:
5828           align = 0x0;
5829 handle_sda:
5830           BFD_ASSERT (sec != NULL);
5831
5832           /* If the symbol is in the abs section, the out_bfd will be null.
5833              This happens when the relocation has a symbol@GOTOFF.  */
5834           r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5835           if (r != bfd_reloc_ok)
5836             {
5837               _bfd_error_handler
5838                 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5839               ret = FALSE;
5840               goto check_reloc;
5841             }
5842
5843           /* At this point `relocation' contains the object's
5844              address.  */
5845           if (r_type == R_NDS32_SDA_FP7U2_RELA)
5846             {
5847               relocation -= fpbase_addr;
5848             }
5849           else
5850             relocation -= gp;
5851           /* Now it contains the offset from _SDA_BASE_.  */
5852
5853           /* Make sure alignment is correct.  */
5854
5855           if (relocation & align)
5856             {
5857               /* Incorrect alignment.  */
5858               _bfd_error_handler
5859                 /* xgettext:c-format */
5860                 (_("%pB(%pA): warning: unaligned small data access"
5861                    " of type %d"),
5862                  input_bfd, input_section, r_type);
5863               ret = FALSE;
5864               goto check_reloc;
5865             }
5866           break;
5867
5868         case R_NDS32_17IFC_PCREL_RELA:
5869         case R_NDS32_10IFCU_PCREL_RELA:
5870           /* Do nothing.  */
5871           break;
5872
5873         case R_NDS32_TLS_LE_HI20:
5874         case R_NDS32_TLS_LE_LO12:
5875         case R_NDS32_TLS_LE_20:
5876         case R_NDS32_TLS_LE_15S0:
5877         case R_NDS32_TLS_LE_15S1:
5878         case R_NDS32_TLS_LE_15S2:
5879           /* We do not have garbage collection for got entries.
5880              Therefore, IE to LE may have one empty entry, and DESC to
5881              LE may have two.  */
5882           if (elf_hash_table (info)->tls_sec != NULL)
5883             relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5884           break;
5885
5886         case R_NDS32_TLS_IE_HI20:
5887         case R_NDS32_TLS_IE_LO12S2:
5888         case R_NDS32_TLS_DESC_HI20:
5889         case R_NDS32_TLS_DESC_LO12:
5890         case R_NDS32_TLS_IE_LO12:
5891         case R_NDS32_TLS_IEGP_HI20:
5892         case R_NDS32_TLS_IEGP_LO12:
5893         case R_NDS32_TLS_IEGP_LO12S2:
5894           {
5895             /* Relocation is to the entry for this symbol in the global
5896                offset table.  */
5897             enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5898             asection *srelgot;
5899             Elf_Internal_Rela outrel;
5900             bfd_byte *loc;
5901             int indx = 0;
5902
5903             eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5904
5905             BFD_ASSERT (sgot != NULL);
5906             if (h != NULL)
5907               {
5908                 bfd_boolean dyn;
5909
5910                 off = h->got.offset;
5911                 BFD_ASSERT (off != (bfd_vma) -1);
5912                 dyn = htab->root.dynamic_sections_created;
5913                 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5914                 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5915                     && (!bfd_link_pic (info)
5916                         || !SYMBOL_REFERENCES_LOCAL (info, h)))
5917                   indx = h->dynindx;
5918               }
5919             else
5920               {
5921                 BFD_ASSERT (local_got_offsets != NULL
5922                             && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5923                 off = local_got_offsets[r_symndx];
5924                 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5925               }
5926
5927             relocation = sgot->output_section->vma + sgot->output_offset + off;
5928
5929             if (1 < ones32 (tls_type))
5930               {
5931                 eff_tls_type = 1 << (fls (tls_type) - 1);
5932                 /* TLS model shall be handled in nds32_elf_unify_tls_model ().  */
5933
5934                 /* TLS model X -> LE is not implement yet!
5935                    workaround here!  */
5936                 if (eff_tls_type == GOT_TLS_LE)
5937                   {
5938                     eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5939                   }
5940               }
5941
5942             /* The offset must always be a multiple of 4.  We use
5943                the least significant bit to record whether we have
5944                already processed this entry.  */
5945             bfd_boolean need_relocs = FALSE;
5946             srelgot = ehtab->srelgot;
5947             if ((bfd_link_pic (info) || indx != 0)
5948                 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5949                     || h->root.type != bfd_link_hash_undefweak))
5950               {
5951                 need_relocs = TRUE;
5952                 BFD_ASSERT (srelgot != NULL);
5953               }
5954
5955             if (off & 1)
5956               {
5957                 off &= ~1;
5958                 relocation &= ~1;
5959
5960                 if (eff_tls_type & GOT_TLS_DESC)
5961                   {
5962                     relocation -= elf_gp (output_bfd);
5963                     if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5964                       {
5965                         /* TLS model shall be converted.  */
5966                         BFD_ASSERT(0);
5967                       }
5968                   }
5969                 else if (eff_tls_type & GOT_TLS_IEGP)
5970                   {
5971                     relocation -= elf_gp (output_bfd);
5972                   }
5973               }
5974             else
5975               {
5976                 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5977                   {
5978                     /* TLS model workaround shall be applied.  */
5979                     BFD_ASSERT(0);
5980                   }
5981                 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5982                   {
5983                     if (eff_tls_type & GOT_TLS_IEGP)
5984                       relocation -= elf_gp(output_bfd);
5985
5986                     if (need_relocs)
5987                       {
5988                         if (indx == 0)
5989                           outrel.r_addend = gottpoff (info, relocation_sym);
5990                         else
5991                           outrel.r_addend = 0;
5992                         outrel.r_offset = (sgot->output_section->vma
5993                                            + sgot->output_offset + off);
5994                         outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5995
5996                         elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5997                                                   &outrel);
5998                       }
5999                     else
6000                       {
6001                         bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6002                                     sgot->contents + off);
6003                       }
6004                   }
6005                 else if (eff_tls_type & GOT_TLS_DESC)
6006                   {
6007                     relocation -= elf_gp (output_bfd);
6008                     if (need_relocs)
6009                       {
6010                         if (indx == 0)
6011                           outrel.r_addend = gottpoff (info, relocation_sym);
6012                         else
6013                           outrel.r_addend = 0;
6014                         outrel.r_offset = (sgot->output_section->vma
6015                                            + sgot->output_offset + off);
6016                         outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
6017
6018                         if (htab->tls_desc_trampoline)
6019                           {
6020                             asection *srelplt;
6021                             srelplt = ehtab->srelplt;
6022                             loc = srelplt->contents;
6023                             loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
6024                             BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
6025                                         <= srelplt->contents + srelplt->size);
6026
6027                             bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6028                           }
6029                         else
6030                           {
6031                             loc = srelgot->contents;
6032                             loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
6033                             bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6034                             ++srelgot->reloc_count;
6035                           }
6036                       }
6037                     else
6038                       {
6039                         /* feed me!  */
6040                         bfd_put_32 (output_bfd, 0xdeadbeef,
6041                                     sgot->contents + off);
6042                         bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6043                                     sgot->contents + off + 4);
6044                         patch_tls_desc_to_ie (contents, rel, input_bfd);
6045                         BFD_ASSERT(0);
6046                       }
6047                   }
6048                 else
6049                   {
6050                     /* TLS model workaround shall be applied.  */
6051                     BFD_ASSERT(0);
6052                   }
6053
6054                 if (h != NULL)
6055                   h->got.offset |= 1;
6056                 else
6057                   local_got_offsets[r_symndx] |= 1;
6058               }
6059           }
6060         break;
6061           /* DON'T fall through.  */
6062
6063         default:
6064           /* OLD_NDS32_RELOC.  */
6065
6066           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6067                                         contents, offset, relocation, addend);
6068           goto check_reloc;
6069         }
6070
6071       switch ((int) r_type)
6072         {
6073         case R_NDS32_20_RELA:
6074         case R_NDS32_5_RELA:
6075         case R_NDS32_9_PCREL_RELA:
6076         case R_NDS32_WORD_9_PCREL_RELA:
6077         case R_NDS32_10_UPCREL_RELA:
6078         case R_NDS32_15_PCREL_RELA:
6079         case R_NDS32_17_PCREL_RELA:
6080         case R_NDS32_25_PCREL_RELA:
6081         case R_NDS32_25_ABS_RELA:
6082         case R_NDS32_HI20_RELA:
6083         case R_NDS32_LO12S3_RELA:
6084         case R_NDS32_LO12S2_RELA:
6085         case R_NDS32_LO12S2_DP_RELA:
6086         case R_NDS32_LO12S2_SP_RELA:
6087         case R_NDS32_LO12S1_RELA:
6088         case R_NDS32_LO12S0_RELA:
6089         case R_NDS32_LO12S0_ORI_RELA:
6090         case R_NDS32_SDA16S3_RELA:
6091         case R_NDS32_SDA17S2_RELA:
6092         case R_NDS32_SDA18S1_RELA:
6093         case R_NDS32_SDA19S0_RELA:
6094         case R_NDS32_SDA15S3_RELA:
6095         case R_NDS32_SDA15S2_RELA:
6096         case R_NDS32_SDA12S2_DP_RELA:
6097         case R_NDS32_SDA12S2_SP_RELA:
6098         case R_NDS32_SDA15S1_RELA:
6099         case R_NDS32_SDA15S0_RELA:
6100         case R_NDS32_SDA_FP7U2_RELA:
6101         case R_NDS32_9_PLTREL:
6102         case R_NDS32_25_PLTREL:
6103         case R_NDS32_GOT20:
6104         case R_NDS32_GOT_HI20:
6105         case R_NDS32_GOT_LO12:
6106         case R_NDS32_GOT_LO15:
6107         case R_NDS32_GOT_LO19:
6108         case R_NDS32_GOT15S2_RELA:
6109         case R_NDS32_GOT17S2_RELA:
6110         case R_NDS32_GOTPC20:
6111         case R_NDS32_GOTPC_HI20:
6112         case R_NDS32_GOTPC_LO12:
6113         case R_NDS32_GOTOFF:
6114         case R_NDS32_GOTOFF_HI20:
6115         case R_NDS32_GOTOFF_LO12:
6116         case R_NDS32_GOTOFF_LO15:
6117         case R_NDS32_GOTOFF_LO19:
6118         case R_NDS32_PLTREL_HI20:
6119         case R_NDS32_PLTREL_LO12:
6120         case R_NDS32_PLT_GOTREL_HI20:
6121         case R_NDS32_PLT_GOTREL_LO12:
6122         case R_NDS32_PLT_GOTREL_LO15:
6123         case R_NDS32_PLT_GOTREL_LO19:
6124         case R_NDS32_PLT_GOTREL_LO20:
6125         case R_NDS32_17IFC_PCREL_RELA:
6126         case R_NDS32_10IFCU_PCREL_RELA:
6127         case R_NDS32_TLS_LE_HI20:
6128         case R_NDS32_TLS_LE_LO12:
6129         case R_NDS32_TLS_IE_HI20:
6130         case R_NDS32_TLS_IE_LO12S2:
6131         case R_NDS32_TLS_LE_20:
6132         case R_NDS32_TLS_LE_15S0:
6133         case R_NDS32_TLS_LE_15S1:
6134         case R_NDS32_TLS_LE_15S2:
6135         case R_NDS32_TLS_DESC_HI20:
6136         case R_NDS32_TLS_DESC_LO12:
6137         case R_NDS32_TLS_IE_LO12:
6138         case R_NDS32_TLS_IEGP_HI20:
6139         case R_NDS32_TLS_IEGP_LO12:
6140         case R_NDS32_TLS_IEGP_LO12S2:
6141           /* Instruction related relocs must handle endian properly.  */
6142           /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
6143           r = nds32_elf_final_link_relocate (howto, input_bfd,
6144                                              input_section, contents,
6145                                              rel->r_offset, relocation,
6146                                              rel->r_addend);
6147           break;
6148
6149         default:
6150           /* All other relocs can use default handler.  */
6151           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6152                                         contents, rel->r_offset,
6153                                         relocation, rel->r_addend);
6154           break;
6155         }
6156
6157 check_reloc:
6158
6159       if (r != bfd_reloc_ok)
6160         {
6161           /* FIXME: This should be generic enough to go in a utility.  */
6162           const char *name;
6163
6164           if (h != NULL)
6165             name = h->root.root.string;
6166           else
6167             {
6168               name = bfd_elf_string_from_elf_section
6169                       (input_bfd, symtab_hdr->sh_link, sym->st_name);
6170               if (name == NULL || *name == '\0')
6171                 name = bfd_section_name (input_bfd, sec);
6172             }
6173
6174           if (errmsg != NULL)
6175             goto common_error;
6176
6177           switch (r)
6178             {
6179             case bfd_reloc_overflow:
6180               (*info->callbacks->reloc_overflow)
6181                 (info, (h ? &h->root : NULL), name, howto->name,
6182                  (bfd_vma) 0, input_bfd, input_section, offset);
6183               break;
6184
6185             case bfd_reloc_undefined:
6186               (*info->callbacks->undefined_symbol)
6187                 (info, name, input_bfd, input_section, offset, TRUE);
6188               break;
6189
6190             case bfd_reloc_outofrange:
6191               errmsg = _("internal error: out of range error");
6192               goto common_error;
6193
6194             case bfd_reloc_notsupported:
6195               errmsg = _("internal error: unsupported relocation error");
6196               goto common_error;
6197
6198             case bfd_reloc_dangerous:
6199               errmsg = _("internal error: dangerous error");
6200               goto common_error;
6201
6202             default:
6203               errmsg = _("internal error: unknown error");
6204               /* Fall through.  */
6205
6206 common_error:
6207               (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6208                                            input_section, offset);
6209               break;
6210             }
6211         }
6212     }
6213
6214   /* Resotre header size to avoid overflow load.  */
6215   if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6216     symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6217
6218   return ret;
6219 }
6220
6221 /* Finish up dynamic symbol handling.  We set the contents of various
6222    dynamic sections here.  */
6223
6224 static bfd_boolean
6225 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6226                                  struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6227 {
6228   struct elf_link_hash_table *ehtab;
6229   struct elf_nds32_link_hash_entry *hent;
6230   bfd_byte *loc;
6231
6232   ehtab = elf_hash_table (info);
6233   hent = (struct elf_nds32_link_hash_entry *) h;
6234
6235   if (h->plt.offset != (bfd_vma) - 1)
6236     {
6237       asection *splt;
6238       asection *sgot;
6239       asection *srela;
6240
6241       bfd_vma plt_index;
6242       bfd_vma got_offset;
6243       bfd_vma local_plt_offset;
6244       Elf_Internal_Rela rela;
6245
6246       /* This symbol has an entry in the procedure linkage table.  Set
6247          it up.  */
6248
6249       BFD_ASSERT (h->dynindx != -1);
6250
6251       splt = ehtab->splt;
6252       sgot = ehtab->sgotplt;
6253       srela = ehtab->srelplt;
6254       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6255
6256       /* Get the index in the procedure linkage table which
6257          corresponds to this symbol.  This is the index of this symbol
6258          in all the symbols for which we are making plt entries.  The
6259          first entry in the procedure linkage table is reserved.  */
6260       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6261
6262       /* Get the offset into the .got table of the entry that
6263          corresponds to this function.  Each .got entry is 4 bytes.
6264          The first three are reserved.  */
6265       got_offset = (plt_index + 3) * 4;
6266
6267       /* Fill in the entry in the procedure linkage table.  */
6268       if (!bfd_link_pic (info))
6269         {
6270           unsigned long insn;
6271
6272           insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6273                                       + sgot->output_offset + got_offset) >> 12)
6274                                     & 0xfffff);
6275           bfd_putb32 (insn, splt->contents + h->plt.offset);
6276
6277           insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6278                                       + sgot->output_offset + got_offset) & 0x0fff)
6279                                     >> 2);
6280           bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6281
6282           insn = PLT_ENTRY_WORD2;
6283           bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6284
6285           insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6286           bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6287
6288           insn = PLT_ENTRY_WORD4
6289                  + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6290           bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6291           local_plt_offset = 12;
6292         }
6293       else
6294         {
6295           /* sda_base must be set at this time.  */
6296           unsigned long insn;
6297           long offset;
6298
6299           offset = sgot->output_section->vma + sgot->output_offset + got_offset
6300                    - elf_gp (output_bfd);
6301           insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6302           bfd_putb32 (insn, splt->contents + h->plt.offset);
6303
6304           insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6305           bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6306
6307           insn = PLT_PIC_ENTRY_WORD2;
6308           bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6309
6310           insn = PLT_PIC_ENTRY_WORD3;
6311           bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6312
6313           insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6314           bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6315
6316           insn = PLT_PIC_ENTRY_WORD5
6317             + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6318           bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6319
6320           local_plt_offset = 16;
6321         }
6322
6323       /* Fill in the entry in the global offset table,
6324          so it will fall through to the next instruction for the first time.  */
6325       bfd_put_32 (output_bfd,
6326                   (splt->output_section->vma + splt->output_offset
6327                    + h->plt.offset + local_plt_offset),
6328                   sgot->contents + got_offset);
6329
6330       /* Fill in the entry in the .rela.plt section.  */
6331       rela.r_offset = (sgot->output_section->vma
6332                        + sgot->output_offset + got_offset);
6333       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6334       rela.r_addend = 0;
6335       loc = srela->contents;
6336       loc += plt_index * sizeof (Elf32_External_Rela);
6337       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6338
6339       if (!h->def_regular)
6340         {
6341           /* Mark the symbol as undefined, rather than as defined in
6342              the .plt section.  Leave the value alone.  */
6343           sym->st_shndx = SHN_UNDEF;
6344           if (!h->ref_regular_nonweak)
6345             sym->st_value = 0;
6346         }
6347     }
6348
6349   if (h->got.offset != (bfd_vma) - 1
6350       && hent->tls_type == GOT_NORMAL)
6351     {
6352       asection *sgot;
6353       asection *srelagot;
6354       Elf_Internal_Rela rela;
6355
6356       /* This symbol has an entry in the global offset table.
6357          Set it up.  */
6358
6359       sgot = ehtab->sgot;
6360       srelagot = ehtab->srelgot;
6361       BFD_ASSERT (sgot != NULL && srelagot != NULL);
6362
6363       rela.r_offset = (sgot->output_section->vma
6364                        + sgot->output_offset + (h->got.offset & ~1));
6365
6366       /* If this is a -Bsymbolic link, and the symbol is defined
6367          locally, we just want to emit a RELATIVE reloc.  Likewise if
6368          the symbol was forced to be local because of a version file.
6369          The entry in the global offset table will already have been
6370          initialized in the relocate_section function.  */
6371       if ((bfd_link_pic (info)
6372            && (info->symbolic || h->dynindx == -1 || h->forced_local)
6373            && h->def_regular)
6374           || (bfd_link_pie (info) && h->def_regular))
6375         {
6376           rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6377           rela.r_addend = (h->root.u.def.value
6378                            + h->root.u.def.section->output_section->vma
6379                            + h->root.u.def.section->output_offset);
6380
6381           if ((h->got.offset & 1) == 0)
6382             {
6383               bfd_put_32 (output_bfd, rela.r_addend,
6384                           sgot->contents + h->got.offset);
6385             }
6386         }
6387       else
6388         {
6389           BFD_ASSERT ((h->got.offset & 1) == 0);
6390           bfd_put_32 (output_bfd, (bfd_vma) 0,
6391                       sgot->contents + h->got.offset);
6392           rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6393           rela.r_addend = 0;
6394         }
6395
6396       loc = srelagot->contents;
6397       loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6398       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6399       ++srelagot->reloc_count;
6400       BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6401     }
6402
6403   if (h->needs_copy)
6404     {
6405       asection *s;
6406       Elf_Internal_Rela rela;
6407
6408       /* This symbols needs a copy reloc.  Set it up.  */
6409
6410       BFD_ASSERT (h->dynindx != -1
6411                   && (h->root.type == bfd_link_hash_defined
6412                       || h->root.type == bfd_link_hash_defweak));
6413
6414       s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6415       BFD_ASSERT (s != NULL);
6416
6417       rela.r_offset = (h->root.u.def.value
6418                        + h->root.u.def.section->output_section->vma
6419                        + h->root.u.def.section->output_offset);
6420       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6421       rela.r_addend = 0;
6422       loc = s->contents;
6423       loc += s->reloc_count * sizeof (Elf32_External_Rela);
6424       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6425       ++s->reloc_count;
6426     }
6427
6428   /* Mark some specially defined symbols as absolute.  */
6429   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6430       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6431     sym->st_shndx = SHN_ABS;
6432
6433   return TRUE;
6434 }
6435
6436
6437 /* Finish up the dynamic sections.  */
6438
6439 static bfd_boolean
6440 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6441 {
6442   bfd *dynobj;
6443   asection *sdyn;
6444   asection *sgotplt;
6445   struct elf_link_hash_table *ehtab;
6446   struct elf_nds32_link_hash_table *htab;
6447
6448   ehtab = elf_hash_table (info);
6449   htab = nds32_elf_hash_table (info);
6450   if (htab == NULL)
6451     return FALSE;
6452
6453   dynobj = elf_hash_table (info)->dynobj;
6454
6455   sgotplt = ehtab->sgotplt;
6456   /* A broken linker script might have discarded the dynamic sections.
6457      Catch this here so that we do not seg-fault later on.  */
6458   if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6459     return FALSE;
6460   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6461
6462   if (elf_hash_table (info)->dynamic_sections_created)
6463     {
6464       asection *splt;
6465       Elf32_External_Dyn *dyncon, *dynconend;
6466
6467       BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6468
6469       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6470       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6471
6472       for (; dyncon < dynconend; dyncon++)
6473         {
6474           Elf_Internal_Dyn dyn;
6475           asection *s;
6476
6477           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6478
6479           switch (dyn.d_tag)
6480             {
6481             default:
6482               break;
6483
6484             case DT_PLTGOT:
6485               /* name = ".got";  */
6486               s = ehtab->sgot->output_section;
6487               goto get_vma;
6488             case DT_JMPREL:
6489               s = ehtab->srelplt->output_section;
6490 get_vma:
6491               BFD_ASSERT (s != NULL);
6492               dyn.d_un.d_ptr = s->vma;
6493               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6494               break;
6495
6496             case DT_PLTRELSZ:
6497               s = ehtab->srelplt->output_section;
6498               BFD_ASSERT (s != NULL);
6499               dyn.d_un.d_val = s->size;
6500               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6501               break;
6502
6503             case DT_RELASZ:
6504               /* My reading of the SVR4 ABI indicates that the
6505                  procedure linkage table relocs (DT_JMPREL) should be
6506                  included in the overall relocs (DT_RELA).  This is
6507                  what Solaris does.  However, UnixWare can not handle
6508                  that case.  Therefore, we override the DT_RELASZ entry
6509                  here to make it not include the JMPREL relocs.  Since
6510                  the linker script arranges for .rela.plt to follow all
6511                  other relocation sections, we don't have to worry
6512                  about changing the DT_RELA entry.  */
6513               if (ehtab->srelplt != NULL)
6514                 {
6515                   s = ehtab->srelplt->output_section;
6516                   dyn.d_un.d_val -= s->size;
6517                 }
6518               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6519               break;
6520
6521             case DT_TLSDESC_PLT:
6522               s = htab->root.splt;
6523               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6524                                 + htab->dt_tlsdesc_plt);
6525               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6526               break;
6527
6528             case DT_TLSDESC_GOT:
6529               s = htab->root.sgot;
6530               dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6531                                 + htab->dt_tlsdesc_got);
6532               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6533               break;
6534             }
6535         }
6536
6537       /* Fill in the first entry in the procedure linkage table.  */
6538       splt = ehtab->splt;
6539       if (splt && splt->size > 0)
6540         {
6541           if (bfd_link_pic (info))
6542             {
6543               unsigned long insn;
6544               long offset;
6545
6546               offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6547                 - elf_gp (output_bfd);
6548               insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6549               bfd_putb32 (insn, splt->contents);
6550
6551               /* here has a typo?  */
6552               insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6553               bfd_putb32 (insn, splt->contents + 4);
6554
6555               insn = PLT0_PIC_ENTRY_WORD2;
6556               bfd_putb32 (insn, splt->contents + 8);
6557
6558               insn = PLT0_PIC_ENTRY_WORD3;
6559               bfd_putb32 (insn, splt->contents + 12);
6560
6561               insn = PLT0_PIC_ENTRY_WORD4;
6562               bfd_putb32 (insn, splt->contents + 16);
6563
6564               insn = PLT0_PIC_ENTRY_WORD5;
6565               bfd_putb32 (insn, splt->contents + 20);
6566             }
6567           else
6568             {
6569               unsigned long insn;
6570               unsigned long addr;
6571
6572               /* addr = .got + 4 */
6573               addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6574               insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6575               bfd_putb32 (insn, splt->contents);
6576
6577               insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6578               bfd_putb32 (insn, splt->contents + 4);
6579
6580               insn = PLT0_ENTRY_WORD2;
6581               bfd_putb32 (insn, splt->contents + 8);
6582
6583               insn = PLT0_ENTRY_WORD3;
6584               bfd_putb32 (insn, splt->contents + 12);
6585
6586               insn = PLT0_ENTRY_WORD4;
6587               bfd_putb32 (insn, splt->contents + 16);
6588             }
6589
6590           elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6591             PLT_ENTRY_SIZE;
6592         }
6593
6594       if (htab->dt_tlsdesc_plt)
6595         {
6596           /* Calculate addresses.  */
6597           asection *sgot = sgot = ehtab->sgot;
6598           bfd_vma pltgot = sgotplt->output_section->vma
6599             + sgotplt->output_offset;
6600           bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6601             + htab->dt_tlsdesc_got;
6602
6603           /* Get GP offset.  */
6604           pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1]  */
6605           tlsdesc_got -= elf_gp (output_bfd);
6606
6607           /* Do relocation.  */
6608           dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6609           dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6610           dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6611           dl_tlsdesc_lazy_trampoline[5] +=  0xfff & pltgot;
6612
6613           /* Insert .plt.  */
6614           nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6615                                 dl_tlsdesc_lazy_trampoline,
6616                                 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6617         }
6618     }
6619
6620   /* Fill in the first three entries in the global offset table.  */
6621   if (sgotplt && sgotplt->size > 0)
6622     {
6623       if (sdyn == NULL)
6624         bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6625       else
6626         bfd_put_32 (output_bfd,
6627                     sdyn->output_section->vma + sdyn->output_offset,
6628                     sgotplt->contents);
6629       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6630       bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6631
6632       elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6633     }
6634
6635   return TRUE;
6636 }
6637 \f
6638
6639 /* Set the right machine number.  */
6640
6641 static bfd_boolean
6642 nds32_elf_object_p (bfd *abfd)
6643 {
6644   static unsigned int cur_arch = 0;
6645
6646   if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6647     {
6648       /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
6649       cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6650     }
6651
6652   switch (cur_arch)
6653     {
6654     default:
6655     case E_N1_ARCH:
6656       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6657       break;
6658     case E_N1H_ARCH:
6659       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6660       break;
6661     case E_NDS_ARCH_STAR_V2_0:
6662       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6663       break;
6664     case E_NDS_ARCH_STAR_V3_0:
6665       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6666       break;
6667     case E_NDS_ARCH_STAR_V3_M:
6668       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6669       break;
6670     }
6671
6672   return TRUE;
6673 }
6674
6675 /* Store the machine number in the flags field.  */
6676
6677 static void
6678 nds32_elf_final_write_processing (bfd *abfd,
6679                                   bfd_boolean linker ATTRIBUTE_UNUSED)
6680 {
6681   unsigned long val;
6682   static unsigned int cur_mach = 0;
6683
6684   if (bfd_mach_n1 != bfd_get_mach (abfd))
6685     {
6686       cur_mach = bfd_get_mach (abfd);
6687     }
6688
6689   switch (cur_mach)
6690     {
6691     case bfd_mach_n1:
6692       /* Only happen when object is empty, since the case is abandon.  */
6693       val = E_N1_ARCH;
6694       val |= E_NDS_ABI_AABI;
6695       val |= E_NDS32_ELF_VER_1_4;
6696       break;
6697     case bfd_mach_n1h:
6698       val = E_N1H_ARCH;
6699       break;
6700     case bfd_mach_n1h_v2:
6701       val = E_NDS_ARCH_STAR_V2_0;
6702       break;
6703     case bfd_mach_n1h_v3:
6704       val = E_NDS_ARCH_STAR_V3_0;
6705       break;
6706     case bfd_mach_n1h_v3m:
6707       val = E_NDS_ARCH_STAR_V3_M;
6708       break;
6709     default:
6710       val = 0;
6711       break;
6712     }
6713
6714   elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6715   elf_elfheader (abfd)->e_flags |= val;
6716 }
6717
6718 /* Function to keep NDS32 specific file flags.  */
6719
6720 static bfd_boolean
6721 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6722 {
6723   BFD_ASSERT (!elf_flags_init (abfd)
6724               || elf_elfheader (abfd)->e_flags == flags);
6725
6726   elf_elfheader (abfd)->e_flags = flags;
6727   elf_flags_init (abfd) = TRUE;
6728   return TRUE;
6729 }
6730
6731 static unsigned int
6732 convert_e_flags (unsigned int e_flags, unsigned int arch)
6733 {
6734   if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6735     {
6736       /* From 0.9 to 1.0.  */
6737       e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6738
6739       /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6740       e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6741       if (arch == E_NDS_ARCH_STAR_V1_0)
6742         {
6743           /* Done.  */
6744           return e_flags;
6745         }
6746     }
6747
6748   /* From 1.0 to 2.0.  */
6749   e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6750
6751   /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
6752   e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6753
6754   /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6755   e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6756   return e_flags;
6757 }
6758
6759 static bfd_boolean
6760 nds32_check_vec_size (bfd *ibfd)
6761 {
6762   static unsigned int nds32_vec_size = 0;
6763
6764   asection *sec_t = NULL;
6765   bfd_byte *contents = NULL;
6766
6767   sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6768
6769   if (sec_t && sec_t->size >= 4)
6770     {
6771       /* Get vec_size in file.  */
6772       unsigned int flag_t;
6773
6774       nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
6775       flag_t = bfd_get_32 (ibfd, contents);
6776
6777       /* The value could only be 4 or 16.  */
6778
6779       if (!nds32_vec_size)
6780         /* Set if not set yet.  */
6781         nds32_vec_size = (flag_t & 0x3);
6782       else if (nds32_vec_size != (flag_t & 0x3))
6783         {
6784           _bfd_error_handler
6785             /* xgettext:c-format */
6786             (_("%pB: ISR vector size mismatch"
6787                " with previous modules, previous %u-byte, current %u-byte"),
6788              ibfd,
6789              nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6790              (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6791           return FALSE;
6792         }
6793       else
6794         /* Only keep the first vec_size section.  */
6795         sec_t->flags |= SEC_EXCLUDE;
6796     }
6797
6798   return TRUE;
6799 }
6800
6801 /* Merge backend specific data from an object file to the output
6802    object file when linking.  */
6803
6804 static bfd_boolean
6805 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6806 {
6807   bfd *obfd = info->output_bfd;
6808   flagword out_flags;
6809   flagword in_flags;
6810   flagword out_16regs;
6811   flagword in_no_mac;
6812   flagword out_no_mac;
6813   flagword in_16regs;
6814   flagword out_version;
6815   flagword in_version;
6816   flagword out_fpu_config;
6817   flagword in_fpu_config;
6818
6819   /* TODO: Revise to use object-attributes instead.  */
6820   if (!nds32_check_vec_size (ibfd))
6821     return FALSE;
6822
6823   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6824       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6825     return TRUE;
6826
6827   if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6828     {
6829       _bfd_error_handler
6830         (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6831
6832       bfd_set_error (bfd_error_bad_value);
6833       return FALSE;
6834     }
6835
6836   /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6837      treat as generic one without checking and merging.  */
6838   if (elf_elfheader (ibfd)->e_flags)
6839     {
6840       in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6841       if (in_version == E_NDS32_ELF_VER_1_2)
6842         {
6843           _bfd_error_handler
6844             (_("%pB: warning: older version of object file encountered, "
6845                "please recompile with current tool chain"), ibfd);
6846         }
6847
6848       /* We may need to merge V1 and V2 arch object files to V2.  */
6849       if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6850           != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6851         {
6852           /* Need to convert version.  */
6853           if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6854               == E_NDS_ARCH_STAR_RESERVED)
6855             {
6856               elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6857             }
6858           else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6859                    == E_NDS_ARCH_STAR_V3_M
6860                    && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6861                    == E_NDS_ARCH_STAR_V3_0)
6862             {
6863               elf_elfheader (ibfd)->e_flags =
6864                 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6865                 | E_NDS_ARCH_STAR_V3_0;
6866             }
6867           else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6868                    == E_NDS_ARCH_STAR_V0_9
6869                    || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6870                    > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6871             {
6872               elf_elfheader (obfd)->e_flags =
6873                 convert_e_flags (elf_elfheader (obfd)->e_flags,
6874                                  (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6875             }
6876           else
6877             {
6878               elf_elfheader (ibfd)->e_flags =
6879                 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6880                                  (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6881             }
6882         }
6883
6884       /* Extract some flags.  */
6885       in_flags = elf_elfheader (ibfd)->e_flags
6886         & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6887              | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6888
6889       /* The following flags need special treatment.  */
6890       in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6891       in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6892       in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6893
6894       /* Extract some flags.  */
6895       out_flags = elf_elfheader (obfd)->e_flags
6896         & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6897              | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6898
6899       /* The following flags need special treatment.  */
6900       out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6901       out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6902       out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6903       out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6904       if (!elf_flags_init (obfd))
6905         {
6906           /* If the input is the default architecture then do not
6907              bother setting the flags for the output architecture,
6908              instead allow future merges to do this.  If no future
6909              merges ever set these flags then they will retain their
6910              unitialised values, which surprise surprise, correspond
6911              to the default values.  */
6912           if (bfd_get_arch_info (ibfd)->the_default)
6913             return TRUE;
6914
6915           elf_flags_init (obfd) = TRUE;
6916           elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6917
6918           if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6919               && bfd_get_arch_info (obfd)->the_default)
6920             {
6921               return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6922                                         bfd_get_mach (ibfd));
6923             }
6924
6925           return TRUE;
6926         }
6927
6928       /* Check flag compatibility.  */
6929       if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6930         {
6931           _bfd_error_handler
6932             (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6933           bfd_set_error (bfd_error_bad_value);
6934           return FALSE;
6935         }
6936
6937       if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6938         {
6939           if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6940             {
6941               _bfd_error_handler
6942                 (_("%pB: error: instruction set mismatch with previous modules"),
6943                  ibfd);
6944
6945               bfd_set_error (bfd_error_bad_value);
6946               return FALSE;
6947             }
6948         }
6949
6950       /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6951          and perf ext1 and DIV are mergerd to perf ext1.  */
6952       if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6953         {
6954           elf_elfheader (obfd)->e_flags =
6955             (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6956             | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6957             | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6958                ?  E_NDS32_HAS_EXT_INST : 0)
6959             | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6960                ?  E_NDS32_HAS_EXT_INST : 0)
6961             | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6962             | ((in_version > out_version) ? out_version : in_version);
6963         }
6964       else
6965         {
6966           if (in_version != out_version)
6967             _bfd_error_handler
6968               /* xgettext:c-format */
6969               (_("%pB: warning: incompatible elf-versions %s and %s"),
6970                ibfd, nds32_elfver_strtab[out_version],
6971                nds32_elfver_strtab[in_version]);
6972
6973           elf_elfheader (obfd)->e_flags = in_flags | out_flags
6974             | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6975             | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6976             | (in_version > out_version ?  out_version : in_version);
6977         }
6978     }
6979
6980   return TRUE;
6981 }
6982
6983 /* Display the flags field.  */
6984
6985 static bfd_boolean
6986 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6987 {
6988   FILE *file = (FILE *) ptr;
6989
6990   BFD_ASSERT (abfd != NULL && ptr != NULL);
6991
6992   _bfd_elf_print_private_bfd_data (abfd, ptr);
6993
6994   fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6995
6996   switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6997     {
6998     default:
6999     case E_N1_ARCH:
7000       fprintf (file, _(": n1 instructions"));
7001       break;
7002     case E_N1H_ARCH:
7003       fprintf (file, _(": n1h instructions"));
7004       break;
7005     }
7006
7007   fputc ('\n', file);
7008
7009   return TRUE;
7010 }
7011
7012 static unsigned int
7013 nds32_elf_action_discarded (asection *sec)
7014 {
7015
7016   if (strncmp
7017       (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7018     return 0;
7019
7020   return _bfd_elf_default_action_discarded (sec);
7021 }
7022
7023 static asection *
7024 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7025                         Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7026                         Elf_Internal_Sym *sym)
7027 {
7028   if (h != NULL)
7029     switch (ELF32_R_TYPE (rel->r_info))
7030       {
7031       case R_NDS32_GNU_VTINHERIT:
7032       case R_NDS32_GNU_VTENTRY:
7033       case R_NDS32_RELA_GNU_VTINHERIT:
7034       case R_NDS32_RELA_GNU_VTENTRY:
7035         return NULL;
7036       }
7037
7038   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7039 }
7040
7041 static enum elf_nds32_tls_type
7042 get_tls_type (enum elf_nds32_reloc_type r_type,
7043               struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7044 {
7045   enum elf_nds32_tls_type tls_type;
7046
7047   switch (r_type)
7048     {
7049     case R_NDS32_TLS_LE_HI20:
7050     case R_NDS32_TLS_LE_LO12:
7051       tls_type = GOT_TLS_LE;
7052       break;
7053     case R_NDS32_TLS_IE_HI20:
7054     case R_NDS32_TLS_IE_LO12S2:
7055     case R_NDS32_TLS_IE_LO12:
7056       tls_type = GOT_TLS_IE;
7057       break;
7058     case R_NDS32_TLS_IEGP_HI20:
7059     case R_NDS32_TLS_IEGP_LO12:
7060     case R_NDS32_TLS_IEGP_LO12S2:
7061       tls_type = GOT_TLS_IEGP;
7062       break;
7063     case R_NDS32_TLS_DESC_HI20:
7064     case R_NDS32_TLS_DESC_LO12:
7065     case R_NDS32_TLS_DESC_ADD:
7066     case R_NDS32_TLS_DESC_FUNC:
7067     case R_NDS32_TLS_DESC_CALL:
7068       tls_type = GOT_TLS_DESC;
7069       break;
7070     default:
7071       tls_type = GOT_NORMAL;
7072       break;
7073     }
7074
7075   return tls_type;
7076 }
7077
7078 /* Ensure that we have allocated bookkeeping structures for ABFD's local
7079    symbols.  */
7080
7081 static bfd_boolean
7082 elf32_nds32_allocate_local_sym_info (bfd *abfd)
7083 {
7084   if (elf_local_got_refcounts (abfd) == NULL)
7085     {
7086       bfd_size_type num_syms;
7087       bfd_size_type size;
7088       char *data;
7089
7090       num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7091       /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7092          gp_offset.  The details can refer to struct elf_nds32_obj_tdata.  */
7093       size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7094                          + sizeof (bfd_vma) + sizeof (int)
7095                          + sizeof (bfd_boolean) + sizeof (bfd_vma));
7096       data = bfd_zalloc (abfd, size);
7097       if (data == NULL)
7098         return FALSE;
7099
7100       elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7101       data += num_syms * sizeof (bfd_signed_vma);
7102
7103       elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7104       data += num_syms * sizeof (char);
7105
7106       elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7107       data += num_syms * sizeof (bfd_vma);
7108
7109       elf32_nds32_local_gp_offset (abfd) = (int *) data;
7110       data += num_syms * sizeof (int);
7111     }
7112
7113   return TRUE;
7114 }
7115
7116 /* Look through the relocs for a section during the first phase.
7117    Since we don't do .gots or .plts, we just need to consider the
7118    virtual table relocs for gc.  */
7119
7120 static bfd_boolean
7121 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7122                         asection *sec, const Elf_Internal_Rela *relocs)
7123 {
7124   Elf_Internal_Shdr *symtab_hdr;
7125   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7126   const Elf_Internal_Rela *rel;
7127   const Elf_Internal_Rela *rel_end;
7128   struct elf_link_hash_table *ehtab;
7129   struct elf_nds32_link_hash_table *htab;
7130   bfd *dynobj;
7131   asection *sreloc = NULL;
7132
7133   /* No need for relocation if relocatable already.  */
7134   if (bfd_link_relocatable (info))
7135     {
7136       elf32_nds32_check_relax_group (abfd, sec);
7137       return TRUE;
7138     }
7139
7140   /* Don't do anything special with non-loaded, non-alloced sections.
7141      In particular, any relocs in such sections should not affect GOT
7142      and PLT reference counting (ie. we don't allow them to create GOT
7143      or PLT entries), there's no possibility or desire to optimize TLS
7144      relocs, and there's not much point in propagating relocs to shared
7145      libs that the dynamic linker won't relocate.  */
7146   if ((sec->flags & SEC_ALLOC) == 0)
7147     return TRUE;
7148
7149   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7150   sym_hashes = elf_sym_hashes (abfd);
7151   sym_hashes_end =
7152     sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7153   if (!elf_bad_symtab (abfd))
7154     sym_hashes_end -= symtab_hdr->sh_info;
7155
7156   ehtab = elf_hash_table (info);
7157   htab = nds32_elf_hash_table (info);
7158   dynobj = htab->root.dynobj;
7159
7160   rel_end = relocs + sec->reloc_count;
7161   for (rel = relocs; rel < rel_end; rel++)
7162     {
7163       enum elf_nds32_reloc_type r_type;
7164       struct elf_link_hash_entry *h;
7165       unsigned long r_symndx;
7166       enum elf_nds32_tls_type tls_type, old_tls_type;
7167
7168       r_symndx = ELF32_R_SYM (rel->r_info);
7169       r_type = ELF32_R_TYPE (rel->r_info);
7170       if (r_symndx < symtab_hdr->sh_info)
7171         h = NULL;
7172       else
7173         {
7174           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7175           while (h->root.type == bfd_link_hash_indirect
7176                  || h->root.type == bfd_link_hash_warning)
7177             h = (struct elf_link_hash_entry *) h->root.u.i.link;
7178         }
7179
7180       /* Create .got section if necessary.
7181          Some relocs require a global offset table.  We create
7182          got section here, since these relocation need a got section
7183          and if it is not created yet.  */
7184       if (ehtab->sgot == NULL)
7185         {
7186           switch (r_type)
7187             {
7188             case R_NDS32_GOT_HI20:
7189             case R_NDS32_GOT_LO12:
7190             case R_NDS32_GOT_LO15:
7191             case R_NDS32_GOT_LO19:
7192             case R_NDS32_GOT17S2_RELA:
7193             case R_NDS32_GOT15S2_RELA:
7194             case R_NDS32_GOTOFF:
7195             case R_NDS32_GOTOFF_HI20:
7196             case R_NDS32_GOTOFF_LO12:
7197             case R_NDS32_GOTOFF_LO15:
7198             case R_NDS32_GOTOFF_LO19:
7199             case R_NDS32_GOTPC20:
7200             case R_NDS32_GOTPC_HI20:
7201             case R_NDS32_GOTPC_LO12:
7202             case R_NDS32_GOT20:
7203             case R_NDS32_TLS_IE_HI20:
7204             case R_NDS32_TLS_IE_LO12:
7205             case R_NDS32_TLS_IE_LO12S2:
7206             case R_NDS32_TLS_IEGP_HI20:
7207             case R_NDS32_TLS_IEGP_LO12:
7208             case R_NDS32_TLS_IEGP_LO12S2:
7209             case R_NDS32_TLS_DESC_HI20:
7210             case R_NDS32_TLS_DESC_LO12:
7211               if (dynobj == NULL)
7212                 htab->root.dynobj = dynobj = abfd;
7213               if (!create_got_section (dynobj, info))
7214                 return FALSE;
7215               break;
7216
7217             default:
7218               break;
7219             }
7220         }
7221
7222       /* Check relocation type.  */
7223       switch ((int) r_type)
7224         {
7225         case R_NDS32_GOT_HI20:
7226         case R_NDS32_GOT_LO12:
7227         case R_NDS32_GOT_LO15:
7228         case R_NDS32_GOT_LO19:
7229         case R_NDS32_GOT20:
7230         case R_NDS32_TLS_LE_HI20:
7231         case R_NDS32_TLS_LE_LO12:
7232         case R_NDS32_TLS_IE_HI20:
7233         case R_NDS32_TLS_IE_LO12:
7234         case R_NDS32_TLS_IE_LO12S2:
7235         case R_NDS32_TLS_IEGP_HI20:
7236         case R_NDS32_TLS_IEGP_LO12:
7237         case R_NDS32_TLS_IEGP_LO12S2:
7238         case R_NDS32_TLS_DESC_HI20:
7239         case R_NDS32_TLS_DESC_LO12:
7240           tls_type = get_tls_type (r_type, h);
7241           if (h)
7242             {
7243               if (tls_type != GOT_TLS_LE)
7244                 h->got.refcount += 1;
7245               old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7246             }
7247           else
7248             {
7249               /* This is a global offset table entry for a local symbol.  */
7250               if (!elf32_nds32_allocate_local_sym_info (abfd))
7251                 return FALSE;
7252
7253               BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7254               if (tls_type != GOT_TLS_LE)
7255                 elf_local_got_refcounts (abfd)[r_symndx] += 1;
7256               old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7257             }
7258
7259           /* We would already have issued an error message if there
7260              is a TLS/non-TLS mismatch, based on the symbol
7261              type.  So just combine any TLS types needed.  */
7262           if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7263               && tls_type != GOT_NORMAL)
7264             tls_type |= old_tls_type;
7265
7266           /* DESC to IE/IEGP if link to executable.  */
7267           if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7268               && (bfd_link_executable (info)))
7269             tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7270
7271           if (old_tls_type != tls_type)
7272             {
7273               if (h != NULL)
7274                 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7275               else
7276                 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7277             }
7278           break;
7279         case R_NDS32_9_PLTREL:
7280         case R_NDS32_25_PLTREL:
7281         case R_NDS32_PLTREL_HI20:
7282         case R_NDS32_PLTREL_LO12:
7283         case R_NDS32_PLT_GOTREL_HI20:
7284         case R_NDS32_PLT_GOTREL_LO12:
7285         case R_NDS32_PLT_GOTREL_LO15:
7286         case R_NDS32_PLT_GOTREL_LO19:
7287         case R_NDS32_PLT_GOTREL_LO20:
7288
7289           /* This symbol requires a procedure linkage table entry.  We
7290              actually build the entry in adjust_dynamic_symbol,
7291              because this might be a case of linking PIC code without
7292              linking in any dynamic objects, in which case we don't
7293              need to generate a procedure linkage table after all.  */
7294
7295           /* If this is a local symbol, we resolve it directly without
7296              creating a procedure linkage table entry.  */
7297           if (h == NULL)
7298             continue;
7299
7300           if (h->forced_local
7301               || (bfd_link_pie (info) && h->def_regular))
7302             break;
7303
7304           elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7305           h->needs_plt = 1;
7306           h->plt.refcount += 1;
7307           break;
7308
7309         case R_NDS32_16_RELA:
7310         case R_NDS32_20_RELA:
7311         case R_NDS32_5_RELA:
7312         case R_NDS32_32_RELA:
7313         case R_NDS32_HI20_RELA:
7314         case R_NDS32_LO12S3_RELA:
7315         case R_NDS32_LO12S2_RELA:
7316         case R_NDS32_LO12S2_DP_RELA:
7317         case R_NDS32_LO12S2_SP_RELA:
7318         case R_NDS32_LO12S1_RELA:
7319         case R_NDS32_LO12S0_RELA:
7320         case R_NDS32_LO12S0_ORI_RELA:
7321         case R_NDS32_SDA16S3_RELA:
7322         case R_NDS32_SDA17S2_RELA:
7323         case R_NDS32_SDA18S1_RELA:
7324         case R_NDS32_SDA19S0_RELA:
7325         case R_NDS32_SDA15S3_RELA:
7326         case R_NDS32_SDA15S2_RELA:
7327         case R_NDS32_SDA12S2_DP_RELA:
7328         case R_NDS32_SDA12S2_SP_RELA:
7329         case R_NDS32_SDA15S1_RELA:
7330         case R_NDS32_SDA15S0_RELA:
7331         case R_NDS32_SDA_FP7U2_RELA:
7332         case R_NDS32_15_PCREL_RELA:
7333         case R_NDS32_17_PCREL_RELA:
7334         case R_NDS32_25_PCREL_RELA:
7335
7336           if (h != NULL && !bfd_link_pic (info))
7337             {
7338               h->non_got_ref = 1;
7339               h->plt.refcount += 1;
7340             }
7341
7342           /* If we are creating a shared library, and this is a reloc against
7343              a global symbol, or a non PC relative reloc against a local
7344              symbol, then we need to copy the reloc into the shared library.
7345              However, if we are linking with -Bsymbolic, we do not need to
7346              copy a reloc against a global symbol which is defined in an
7347              object we are including in the link (i.e., DEF_REGULAR is set).
7348              At this point we have not seen all the input files, so it is
7349              possible that DEF_REGULAR is not set now but will be set later
7350              (it is never cleared).  We account for that possibility below by
7351              storing information in the dyn_relocs field of the hash table
7352              entry.  A similar situation occurs when creating shared libraries
7353              and symbol visibility changes render the symbol local.
7354
7355              If on the other hand, we are creating an executable, we may need
7356              to keep relocations for symbols satisfied by a dynamic library
7357              if we manage to avoid copy relocs for the symbol.  */
7358           if ((bfd_link_pic (info)
7359                && (sec->flags & SEC_ALLOC) != 0
7360                && ((r_type != R_NDS32_25_PCREL_RELA
7361                     && r_type != R_NDS32_15_PCREL_RELA
7362                     && r_type != R_NDS32_17_PCREL_RELA
7363                     && !(r_type == R_NDS32_32_RELA
7364                          && strcmp (sec->name, ".eh_frame") == 0))
7365                    || (h != NULL
7366                        && (!info->symbolic
7367                            || h->root.type == bfd_link_hash_defweak
7368                            || !h->def_regular))))
7369               || (!bfd_link_pic (info)
7370                   && (sec->flags & SEC_ALLOC) != 0
7371                   && h != NULL
7372                   && (h->root.type == bfd_link_hash_defweak
7373                       || !h->def_regular)))
7374             {
7375               struct elf_dyn_relocs *p;
7376               struct elf_dyn_relocs **head;
7377
7378               if (dynobj == NULL)
7379                 htab->root.dynobj = dynobj = abfd;
7380
7381               /* When creating a shared object, we must copy these
7382                  relocs into the output file.  We create a reloc
7383                  section in dynobj and make room for the reloc.  */
7384               if (sreloc == NULL)
7385                 {
7386                   const char *name;
7387
7388                   name = bfd_elf_string_from_elf_section
7389                     (abfd, elf_elfheader (abfd)->e_shstrndx,
7390                      elf_section_data (sec)->rela.hdr->sh_name);
7391                   if (name == NULL)
7392                     return FALSE;
7393
7394                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
7395                               && strcmp (bfd_get_section_name (abfd, sec),
7396                                          name + 5) == 0);
7397
7398                   sreloc = bfd_get_section_by_name (dynobj, name);
7399                   if (sreloc == NULL)
7400                     {
7401                       flagword flags;
7402
7403                       sreloc = bfd_make_section (dynobj, name);
7404                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
7405                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7406                       if ((sec->flags & SEC_ALLOC) != 0)
7407                         flags |= SEC_ALLOC | SEC_LOAD;
7408                       if (sreloc == NULL
7409                           || !bfd_set_section_flags (dynobj, sreloc, flags)
7410                           || !bfd_set_section_alignment (dynobj, sreloc, 2))
7411                         return FALSE;
7412
7413                       elf_section_type (sreloc) = SHT_RELA;
7414                     }
7415                   elf_section_data (sec)->sreloc = sreloc;
7416                 }
7417
7418               /* If this is a global symbol, we count the number of
7419                  relocations we need for this symbol.  */
7420               if (h != NULL)
7421                 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
7422               else
7423                 {
7424                   asection *s;
7425                   void *vpp;
7426
7427                   Elf_Internal_Sym *isym;
7428                   isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7429                   if (isym == NULL)
7430                     return FALSE;
7431
7432                   /* Track dynamic relocs needed for local syms too.  */
7433                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7434                   if (s == NULL)
7435                     return FALSE;
7436
7437                   vpp = &elf_section_data (s)->local_dynrel;
7438                   head = (struct elf_dyn_relocs **) vpp;
7439                 }
7440
7441               p = *head;
7442               if (p == NULL || p->sec != sec)
7443                 {
7444                   bfd_size_type amt = sizeof (*p);
7445                   p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7446                   if (p == NULL)
7447                     return FALSE;
7448                   p->next = *head;
7449                   *head = p;
7450                   p->sec = sec;
7451                   p->count = 0;
7452                   p->pc_count = 0;
7453                 }
7454
7455               p->count += 1;
7456
7457               /* Since eh_frame is readonly, R_NDS32_32_RELA
7458                  reloc for eh_frame will cause shared library has
7459                  TEXTREL entry in the dynamic section. This lead glibc
7460                  testsuites to failure (bug-13092) and cause kernel fail
7461                  (bug-11819).  I think the best solution is to replace
7462                  absolute reloc with pc relative reloc in the eh_frame.
7463                  To do that, we need to support the following issues:
7464
7465                  === For GCC ===
7466                  * gcc/config/nds32/nds32.h: Define
7467                  ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7468                  and DW_EH_PE_sdata4 into DWARF exception header when
7469                  option have '-fpic'.
7470
7471                  === For binutils ===
7472                  * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7473                  * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7474                  may break our nds DIFF mechanism, therefore, we
7475                  must disable all linker relaxations to ensure
7476                  correctness.
7477                  * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7478                  R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7479                  do the necessary modification.
7480
7481                  Unfortunately, it still have some problems for nds32
7482                  to support pc relative reloc in the eh_frame. So I use
7483                  another solution to fix this issue.
7484
7485                  However, I find that ld always emit TEXTREL marker for
7486                  R_NDS32_NONE relocs in rel.dyn. These none relocs are
7487                  correspond to R_NDS32_32_RELA for .eh_frame section.
7488                  It means that we always reserve redundant entries of rel.dyn
7489                  for these relocs which actually do nothing in dynamic linker.
7490
7491                  Therefore, we regard these relocs as pc relative relocs
7492                  here and increase the pc_count.  */
7493               if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7494                   || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7495                   || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7496                   || (r_type == R_NDS32_32_RELA
7497                       && strcmp (sec->name, ".eh_frame") == 0))
7498                 p->pc_count += 1;
7499             }
7500           break;
7501
7502           /* This relocation describes the C++ object vtable hierarchy.
7503              Reconstruct it for later use during GC.  */
7504         case R_NDS32_RELA_GNU_VTINHERIT:
7505         case R_NDS32_GNU_VTINHERIT:
7506           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7507             return FALSE;
7508           break;
7509
7510           /* This relocation describes which C++ vtable entries are actually
7511              used.  Record for later use during GC.  */
7512         case R_NDS32_GNU_VTENTRY:
7513           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7514             return FALSE;
7515           break;
7516         case R_NDS32_RELA_GNU_VTENTRY:
7517           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7518             return FALSE;
7519           break;
7520         }
7521     }
7522
7523   return TRUE;
7524 }
7525
7526 /* Write VAL in uleb128 format to P, returning a pointer to the
7527    following byte.
7528    This code is copied from elf-attr.c.  */
7529
7530 static bfd_byte *
7531 write_uleb128 (bfd_byte *p, unsigned int val)
7532 {
7533   bfd_byte c;
7534   do
7535     {
7536       c = val & 0x7f;
7537       val >>= 7;
7538       if (val)
7539         c |= 0x80;
7540       *(p++) = c;
7541     }
7542   while (val);
7543   return p;
7544 }
7545
7546 static bfd_signed_vma
7547 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7548                   Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7549 {
7550   bfd_signed_vma foff;
7551   bfd_vma symval, addend;
7552   asection *sym_sec;
7553
7554   /* Get the value of the symbol referred to by the reloc.  */
7555   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7556     {
7557       Elf_Internal_Sym *isym;
7558
7559       /* A local symbol.  */
7560       isym = isymbuf + ELF32_R_SYM (irel->r_info);
7561
7562       if (isym->st_shndx == SHN_UNDEF)
7563         sym_sec = bfd_und_section_ptr;
7564       else if (isym->st_shndx == SHN_ABS)
7565         sym_sec = bfd_abs_section_ptr;
7566       else if (isym->st_shndx == SHN_COMMON)
7567         sym_sec = bfd_com_section_ptr;
7568       else
7569         sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7570       symval = isym->st_value + sym_sec->output_section->vma
7571                + sym_sec->output_offset;
7572     }
7573   else
7574     {
7575       unsigned long indx;
7576       struct elf_link_hash_entry *h;
7577
7578       /* An external symbol.  */
7579       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7580       h = elf_sym_hashes (abfd)[indx];
7581       BFD_ASSERT (h != NULL);
7582
7583       if (h->root.type != bfd_link_hash_defined
7584           && h->root.type != bfd_link_hash_defweak)
7585         /* This appears to be a reference to an undefined
7586            symbol.  Just ignore it--it will be caught by the
7587            regular reloc processing.  */
7588         return 0;
7589
7590       if (h->root.u.def.section->flags & SEC_MERGE)
7591         {
7592           sym_sec = h->root.u.def.section;
7593           symval = _bfd_merged_section_offset (abfd, &sym_sec,
7594                                                elf_section_data (sym_sec)->sec_info,
7595                                                h->root.u.def.value);
7596           symval = symval + sym_sec->output_section->vma
7597                    + sym_sec->output_offset;
7598         }
7599       else
7600         symval = (h->root.u.def.value
7601                   + h->root.u.def.section->output_section->vma
7602                   + h->root.u.def.section->output_offset);
7603     }
7604
7605   addend = irel->r_addend;
7606
7607   foff = (symval + addend
7608           - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7609   return foff;
7610 }
7611 \f
7612
7613 /* Convert a 32-bit instruction to 16-bit one.
7614    INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7615    instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
7616    type of INSN16.  Return 1 if successful.  */
7617
7618 static int
7619 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7620                              int *pinsn_type)
7621 {
7622   uint16_t insn16 = 0;
7623   int insn_type = 0;
7624   unsigned long mach = bfd_get_mach (abfd);
7625
7626   if (N32_SH5 (insn) != 0)
7627     return 0;
7628
7629   switch (N32_SUB5 (insn))
7630     {
7631     case N32_ALU1_ADD_SLLI:
7632     case N32_ALU1_ADD_SRLI:
7633       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7634         {
7635           insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7636                                 N32_RB5 (insn));
7637           insn_type = NDS32_INSN_ADD333;
7638         }
7639       else if (N32_IS_RT4 (insn))
7640         {
7641           if (N32_RT5 (insn) == N32_RA5 (insn))
7642             insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7643           else if (N32_RT5 (insn) == N32_RB5 (insn))
7644             insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7645           insn_type = NDS32_INSN_ADD45;
7646         }
7647       break;
7648
7649     case N32_ALU1_SUB_SLLI:
7650     case N32_ALU1_SUB_SRLI:
7651       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7652         {
7653           insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7654                                 N32_RB5 (insn));
7655           insn_type = NDS32_INSN_SUB333;
7656         }
7657       else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7658         {
7659           insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7660           insn_type = NDS32_INSN_SUB45;
7661         }
7662       break;
7663
7664     case N32_ALU1_AND_SLLI:
7665     case N32_ALU1_AND_SRLI:
7666       /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
7667       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7668           && N32_IS_RB3 (insn))
7669         {
7670           if (N32_RT5 (insn) == N32_RA5 (insn))
7671             insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7672           else if (N32_RT5 (insn) == N32_RB5 (insn))
7673             insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7674           if (insn16)
7675             insn_type = NDS32_INSN_AND33;
7676         }
7677       break;
7678
7679     case N32_ALU1_XOR_SLLI:
7680     case N32_ALU1_XOR_SRLI:
7681       /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
7682       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7683           && N32_IS_RB3 (insn))
7684         {
7685           if (N32_RT5 (insn) == N32_RA5 (insn))
7686             insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7687           else if (N32_RT5 (insn) == N32_RB5 (insn))
7688             insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7689           if (insn16)
7690             insn_type = NDS32_INSN_XOR33;
7691         }
7692       break;
7693
7694     case N32_ALU1_OR_SLLI:
7695     case N32_ALU1_OR_SRLI:
7696       /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
7697       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7698           && N32_IS_RB3 (insn))
7699         {
7700           if (N32_RT5 (insn) == N32_RA5 (insn))
7701             insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7702           else if (N32_RT5 (insn) == N32_RB5 (insn))
7703             insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7704           if (insn16)
7705             insn_type = NDS32_INSN_OR33;
7706         }
7707       break;
7708     case N32_ALU1_NOR:
7709       /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
7710       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7711           && N32_RA5 (insn) == N32_RB5 (insn))
7712         {
7713           insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7714           insn_type = NDS32_INSN_NOT33;
7715         }
7716       break;
7717     case N32_ALU1_SRAI:
7718       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7719         {
7720           insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7721           insn_type = NDS32_INSN_SRAI45;
7722         }
7723       break;
7724
7725     case N32_ALU1_SRLI:
7726       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7727         {
7728           insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7729           insn_type = NDS32_INSN_SRLI45;
7730         }
7731       break;
7732
7733     case N32_ALU1_SLLI:
7734       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7735         {
7736           insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7737                                 N32_UB5 (insn));
7738           insn_type = NDS32_INSN_SLLI333;
7739         }
7740       break;
7741
7742     case N32_ALU1_ZEH:
7743       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7744         {
7745           insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7746           insn_type = NDS32_INSN_ZEH33;
7747         }
7748       break;
7749
7750     case N32_ALU1_SEB:
7751       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7752         {
7753           insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7754           insn_type = NDS32_INSN_SEB33;
7755         }
7756       break;
7757
7758     case N32_ALU1_SEH:
7759       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7760         {
7761           insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7762           insn_type = NDS32_INSN_SEH33;
7763         }
7764       break;
7765
7766     case N32_ALU1_SLT:
7767       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7768         {
7769           /* Implicit r15.  */
7770           insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7771           insn_type = NDS32_INSN_SLT45;
7772         }
7773       break;
7774
7775     case N32_ALU1_SLTS:
7776       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7777         {
7778           /* Implicit r15.  */
7779           insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7780           insn_type = NDS32_INSN_SLTS45;
7781         }
7782       break;
7783     }
7784
7785   if ((insn16 & 0x8000) == 0)
7786     return 0;
7787
7788   if (pinsn16)
7789     *pinsn16 = insn16;
7790   if (pinsn_type)
7791     *pinsn_type = insn_type;
7792   return 1;
7793 }
7794
7795 static int
7796 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7797                              int *pinsn_type)
7798 {
7799   uint16_t insn16 = 0;
7800   int insn_type;
7801   unsigned long mach = bfd_get_mach (abfd);
7802
7803   /* TODO: bset, bclr, btgl, btst.  */
7804   if (__GF (insn, 6, 4) != 0)
7805     return 0;
7806
7807   switch (N32_IMMU (insn, 6))
7808     {
7809     case N32_ALU2_MUL:
7810       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7811           && N32_IS_RB3 (insn))
7812         {
7813           if (N32_RT5 (insn) == N32_RA5 (insn))
7814             insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7815           else if (N32_RT5 (insn) == N32_RB5 (insn))
7816             insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7817           if (insn16)
7818             insn_type = NDS32_INSN_MUL33;
7819         }
7820     }
7821
7822   if ((insn16 & 0x8000) == 0)
7823     return 0;
7824
7825   if (pinsn16)
7826     *pinsn16 = insn16;
7827   if (pinsn_type)
7828     *pinsn_type = insn_type;
7829   return 1;
7830 }
7831
7832 int
7833 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7834                         int *pinsn_type)
7835 {
7836   int op6;
7837   uint16_t insn16 = 0;
7838   int insn_type = 0;
7839   unsigned long mach = bfd_get_mach (abfd);
7840
7841   /* Decode 32-bit instruction.  */
7842   if (insn & 0x80000000)
7843     {
7844       /* Not 32-bit insn.  */
7845       return 0;
7846     }
7847
7848   op6 = N32_OP6 (insn);
7849
7850   /* Convert it to 16-bit instruction.  */
7851   switch (op6)
7852     {
7853     case N32_OP6_MOVI:
7854       if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7855         {
7856           insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7857           insn_type = NDS32_INSN_MOVI55;
7858         }
7859       else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7860                && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7861         {
7862           insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7863                                N32_IMM20S (insn) - 16);
7864           insn_type = NDS32_INSN_MOVPI45;
7865         }
7866       break;
7867
7868     case N32_OP6_ADDI:
7869       if (N32_IMM15S (insn) == 0)
7870         {
7871           /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7872              because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7873           if (mach <= MACH_V2
7874               || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7875             {
7876               insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7877               insn_type = NDS32_INSN_MOV55;
7878             }
7879         }
7880       else if (N32_IMM15S (insn) > 0)
7881         {
7882           if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7883             {
7884               insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7885                                     N32_IMM15S (insn));
7886               insn_type = NDS32_INSN_ADDI333;
7887             }
7888           else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7889                    && N32_IMM15S (insn) < 32)
7890             {
7891               insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7892               insn_type = NDS32_INSN_ADDI45;
7893             }
7894           else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7895                    && N32_RT5 (insn) == N32_RA5 (insn)
7896                    && N32_IMM15S (insn) < 512)
7897             {
7898               insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7899               insn_type = NDS32_INSN_ADDI10_SP;
7900             }
7901           else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7902                    && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7903                    && (N32_IMM15S (insn) % 4 == 0))
7904             {
7905               insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7906                                    N32_IMM15S (insn) >> 2);
7907               insn_type = NDS32_INSN_ADDRI36_SP;
7908             }
7909         }
7910       else
7911         {
7912           /* Less than 0.  */
7913           if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7914             {
7915               insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7916                                     0 - N32_IMM15S (insn));
7917               insn_type = NDS32_INSN_SUBI333;
7918             }
7919           else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7920                    && N32_IMM15S (insn) > -32)
7921             {
7922               insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7923                                    0 - N32_IMM15S (insn));
7924               insn_type = NDS32_INSN_SUBI45;
7925             }
7926           else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7927                    && N32_RT5 (insn) == N32_RA5 (insn)
7928                    && N32_IMM15S (insn) >= -512)
7929             {
7930               insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7931               insn_type = NDS32_INSN_ADDI10_SP;
7932             }
7933         }
7934       break;
7935
7936     case N32_OP6_ORI:
7937       if (N32_IMM15S (insn) == 0)
7938         {
7939           /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7940              because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7941           if (mach <= MACH_V2
7942               || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7943             {
7944               insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7945               insn_type = NDS32_INSN_MOV55;
7946             }
7947         }
7948       break;
7949
7950     case N32_OP6_SUBRI:
7951       if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7952           && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7953         {
7954           insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7955           insn_type = NDS32_INSN_NEG33;
7956         }
7957       break;
7958
7959     case N32_OP6_ANDI:
7960       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7961         {
7962           if (N32_IMM15U (insn) == 1)
7963             {
7964               insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7965               insn_type = NDS32_INSN_XLSB33;
7966             }
7967           else if (N32_IMM15U (insn) == 0x7ff)
7968             {
7969               insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7970               insn_type = NDS32_INSN_X11B33;
7971             }
7972           else if (N32_IMM15U (insn) == 0xff)
7973             {
7974               insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7975               insn_type = NDS32_INSN_ZEB33;
7976             }
7977           else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7978                    && N32_IMM15U (insn) < 256)
7979             {
7980               int imm15u = N32_IMM15U (insn);
7981
7982               if (__builtin_popcount (imm15u) == 1)
7983                 {
7984                   /* BMSKI33 */
7985                   int imm3u = __builtin_ctz (imm15u);
7986
7987                   insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7988                   insn_type = NDS32_INSN_BMSKI33;
7989                 }
7990               else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7991                 {
7992                   /* FEXTI33 */
7993                   int imm3u = __builtin_ctz (imm15u + 1) - 1;
7994
7995                   insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7996                   insn_type = NDS32_INSN_FEXTI33;
7997                 }
7998             }
7999         }
8000       break;
8001
8002     case N32_OP6_SLTI:
8003       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8004           && IS_WITHIN_U (N32_IMM15S (insn), 5))
8005         {
8006           insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8007           insn_type = NDS32_INSN_SLTI45;
8008         }
8009       break;
8010
8011     case N32_OP6_SLTSI:
8012       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8013           && IS_WITHIN_U (N32_IMM15S (insn), 5))
8014         {
8015           insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8016           insn_type = NDS32_INSN_SLTSI45;
8017         }
8018       break;
8019
8020     case N32_OP6_LWI:
8021       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8022         {
8023           insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8024           insn_type = NDS32_INSN_LWI450;
8025         }
8026       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8027                && IS_WITHIN_U (N32_IMM15S (insn), 3))
8028         {
8029           insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8030                                 N32_IMM15S (insn));
8031           insn_type = NDS32_INSN_LWI333;
8032         }
8033       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8034                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8035         {
8036           insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8037           insn_type = NDS32_INSN_LWI37;
8038         }
8039       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8040                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8041         {
8042           insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8043           insn_type = NDS32_INSN_LWI37_SP;
8044         }
8045       else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8046                && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8047         {
8048           insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8049                                N32_IMM15S (insn) + 32);
8050           insn_type = NDS32_INSN_LWI45_FE;
8051         }
8052       break;
8053
8054     case N32_OP6_SWI:
8055       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8056         {
8057           insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8058           insn_type = NDS32_INSN_SWI450;
8059         }
8060       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8061                && IS_WITHIN_U (N32_IMM15S (insn), 3))
8062         {
8063           insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8064                                 N32_IMM15S (insn));
8065           insn_type = NDS32_INSN_SWI333;
8066         }
8067       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8068                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8069         {
8070           insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8071           insn_type = NDS32_INSN_SWI37;
8072         }
8073       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8074                && IS_WITHIN_U (N32_IMM15S (insn), 7))
8075         {
8076           insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8077           insn_type = NDS32_INSN_SWI37_SP;
8078         }
8079       break;
8080
8081     case N32_OP6_LWI_BI:
8082       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8083           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8084         {
8085           insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8086                                 N32_IMM15S (insn));
8087           insn_type = NDS32_INSN_LWI333_BI;
8088         }
8089       break;
8090
8091     case N32_OP6_SWI_BI:
8092       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8093           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8094         {
8095           insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8096                                 N32_IMM15S (insn));
8097           insn_type = NDS32_INSN_SWI333_BI;
8098         }
8099       break;
8100
8101     case N32_OP6_LHI:
8102       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8103           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8104         {
8105           insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8106                                 N32_IMM15S (insn));
8107           insn_type = NDS32_INSN_LHI333;
8108         }
8109       break;
8110
8111     case N32_OP6_SHI:
8112       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8113           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8114         {
8115           insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8116                                 N32_IMM15S (insn));
8117           insn_type = NDS32_INSN_SHI333;
8118         }
8119       break;
8120
8121     case N32_OP6_LBI:
8122       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8123           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8124         {
8125           insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8126                                 N32_IMM15S (insn));
8127           insn_type = NDS32_INSN_LBI333;
8128         }
8129       break;
8130
8131     case N32_OP6_SBI:
8132       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8133           && IS_WITHIN_U (N32_IMM15S (insn), 3))
8134         {
8135           insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8136                                 N32_IMM15S (insn));
8137           insn_type = NDS32_INSN_SBI333;
8138         }
8139       break;
8140
8141     case N32_OP6_ALU1:
8142       return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8143
8144     case N32_OP6_ALU2:
8145       return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8146
8147     case N32_OP6_BR1:
8148       if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8149         goto done;
8150
8151       if ((insn & N32_BIT (14)) == 0)
8152         {
8153           /* N32_BR1_BEQ */
8154           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8155               && N32_RT5 (insn) != REG_R5)
8156             insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8157           else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8158                    && N32_RA5 (insn) != REG_R5)
8159             insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8160           insn_type = NDS32_INSN_BEQS38;
8161           break;
8162         }
8163       else
8164         {
8165           /* N32_BR1_BNE */
8166           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8167               && N32_RT5 (insn) != REG_R5)
8168             insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8169           else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8170                    && N32_RA5 (insn) != REG_R5)
8171             insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8172           insn_type = NDS32_INSN_BNES38;
8173           break;
8174         }
8175       break;
8176
8177     case N32_OP6_BR2:
8178       switch (N32_BR2_SUB (insn))
8179         {
8180         case N32_BR2_BEQZ:
8181           if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8182             {
8183               insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8184               insn_type = NDS32_INSN_BEQZ38;
8185             }
8186           else if (N32_RT5 (insn) == REG_R15
8187                    && IS_WITHIN_S (N32_IMM16S (insn), 8))
8188             {
8189               insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8190               insn_type = NDS32_INSN_BEQZS8;
8191             }
8192           break;
8193
8194         case N32_BR2_BNEZ:
8195           if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8196             {
8197               insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8198               insn_type = NDS32_INSN_BNEZ38;
8199             }
8200           else if (N32_RT5 (insn) == REG_R15
8201                    && IS_WITHIN_S (N32_IMM16S (insn), 8))
8202             {
8203               insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8204               insn_type = NDS32_INSN_BNEZS8;
8205             }
8206           break;
8207
8208         case N32_BR2_SOP0:
8209           if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8210             {
8211               insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8212               insn_type = NDS32_INSN_IFCALL9;
8213             }
8214           break;
8215         }
8216       break;
8217
8218     case N32_OP6_JI:
8219       if ((insn & N32_BIT (24)) == 0)
8220         {
8221           /* N32_JI_J */
8222           if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8223             {
8224               insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8225               insn_type = NDS32_INSN_J8;
8226             }
8227         }
8228       break;
8229
8230     case N32_OP6_JREG:
8231       if (__GF (insn, 8, 2) != 0)
8232         goto done;
8233
8234       switch (N32_IMMU (insn, 5))
8235         {
8236         case N32_JREG_JR:
8237           if (N32_JREG_HINT (insn) == 0)
8238             {
8239               /* jr */
8240               insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8241               insn_type = NDS32_INSN_JR5;
8242             }
8243           else if (N32_JREG_HINT (insn) == 1)
8244             {
8245               /* ret */
8246               insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8247               insn_type = NDS32_INSN_RET5;
8248             }
8249           else if (N32_JREG_HINT (insn) == 3)
8250             {
8251               /* ifret = mov55 $sp, $sp */
8252               insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8253               insn_type = NDS32_INSN_IFRET;
8254             }
8255           break;
8256
8257         case N32_JREG_JRAL:
8258           /* It's convertible when return rt5 is $lp and address
8259              translation is kept.  */
8260           if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8261             {
8262               insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8263               insn_type = NDS32_INSN_JRAL5;
8264             }
8265           break;
8266         }
8267       break;
8268
8269     case N32_OP6_MISC:
8270       if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8271         {
8272           /* For v3, swid above 31 are used for ex9.it.  */
8273           insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8274           insn_type = NDS32_INSN_BREAK16;
8275         }
8276       break;
8277
8278     default:
8279       /* This instruction has no 16-bit variant.  */
8280       goto done;
8281     }
8282
8283 done:
8284   /* Bit-15 of insn16 should be set for a valid instruction.  */
8285   if ((insn16 & 0x8000) == 0)
8286     return 0;
8287
8288   if (pinsn16)
8289     *pinsn16 = insn16;
8290   if (pinsn_type)
8291     *pinsn_type = insn_type;
8292   return 1;
8293 }
8294
8295 static int
8296 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8297                           Elf_Internal_Rela *reloc)
8298 {
8299   uint16_t insn16 = 0;
8300
8301   if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8302       || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8303     return 0;
8304
8305   if (!N32_IS_RT3 (insn))
8306     return 0;
8307
8308   switch (N32_OP6 (insn))
8309     {
8310     case N32_OP6_LWI:
8311       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8312         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8313       break;
8314     case N32_OP6_SWI:
8315       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8316         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8317       break;
8318     case N32_OP6_HWGP:
8319       if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8320         break;
8321
8322       if (__GF (insn, 17, 3) == 6)
8323         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8324       else if (__GF (insn, 17, 3) == 7)
8325         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8326       break;
8327     }
8328
8329   if ((insn16 & 0x8000) == 0)
8330     return 0;
8331
8332   *pinsn16 = insn16;
8333   return 1;
8334 }
8335
8336 /* Convert a 16-bit instruction to 32-bit one.
8337    INSN16 it the input and PINSN it the point to output.
8338    Return non-zero on successful.  Otherwise 0 is returned.  */
8339
8340 int
8341 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8342 {
8343   uint32_t insn = 0xffffffff;
8344   unsigned long mach = bfd_get_mach (abfd);
8345
8346   /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
8347
8348   switch (__GF (insn16, 9, 6))
8349     {
8350     case 0x4:                   /* add45 */
8351       insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8352                        N16_RA5 (insn16));
8353       goto done;
8354     case 0x5:                   /* sub45 */
8355       insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8356                        N16_RA5 (insn16));
8357       goto done;
8358     case 0x6:                   /* addi45 */
8359       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8360                         N16_IMM5U (insn16));
8361       goto done;
8362     case 0x7:                   /* subi45 */
8363       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8364                         -N16_IMM5U (insn16));
8365       goto done;
8366     case 0x8:                   /* srai45 */
8367       insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8368                        N16_IMM5U (insn16));
8369       goto done;
8370     case 0x9:                   /* srli45 */
8371       insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8372                        N16_IMM5U (insn16));
8373       goto done;
8374     case 0xa:                   /* slli333 */
8375       insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8376                        N16_IMM3U (insn16));
8377       goto done;
8378     case 0xc:                   /* add333 */
8379       insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8380                        N16_RB3 (insn16));
8381       goto done;
8382     case 0xd:                   /* sub333 */
8383       insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8384                        N16_RB3 (insn16));
8385       goto done;
8386     case 0xe:                   /* addi333 */
8387       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8388                         N16_IMM3U (insn16));
8389       goto done;
8390     case 0xf:                   /* subi333 */
8391       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8392                         -N16_IMM3U (insn16));
8393       goto done;
8394     case 0x10:                  /* lwi333 */
8395       insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8396                         N16_IMM3U (insn16));
8397       goto done;
8398     case 0x12:                  /* lhi333 */
8399       insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8400                         N16_IMM3U (insn16));
8401       goto done;
8402     case 0x13:                  /* lbi333 */
8403       insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8404                         N16_IMM3U (insn16));
8405       goto done;
8406     case 0x11:                  /* lwi333.bi */
8407       insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8408                         N16_IMM3U (insn16));
8409       goto done;
8410     case 0x14:                  /* swi333 */
8411       insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8412                         N16_IMM3U (insn16));
8413       goto done;
8414     case 0x16:                  /* shi333 */
8415       insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8416                         N16_IMM3U (insn16));
8417       goto done;
8418     case 0x17:                  /* sbi333 */
8419       insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8420                         N16_IMM3U (insn16));
8421       goto done;
8422     case 0x15:                  /* swi333.bi */
8423       insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8424                         N16_IMM3U (insn16));
8425       goto done;
8426     case 0x18:                  /* addri36.sp */
8427       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8428                         N16_IMM6U (insn16) << 2);
8429       goto done;
8430     case 0x19:                  /* lwi45.fe */
8431       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8432                         (N16_IMM5U (insn16) - 32));
8433       goto done;
8434     case 0x1a:                  /* lwi450 */
8435       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8436       goto done;
8437     case 0x1b:                  /* swi450 */
8438       insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8439       goto done;
8440
8441       /* These are r15 implied instructions.  */
8442     case 0x30:                  /* slts45 */
8443       insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8444       goto done;
8445     case 0x31:                  /* slt45 */
8446       insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8447       goto done;
8448     case 0x32:                  /* sltsi45 */
8449       insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8450       goto done;
8451     case 0x33:                  /* slti45 */
8452       insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8453       goto done;
8454     case 0x34:                  /* beqzs8, bnezs8 */
8455       if (insn16 & N32_BIT (8))
8456         insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8457       else
8458         insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8459       goto done;
8460
8461     case 0x35:                  /* break16, ex9.it */
8462       /* Only consider range of v3 break16.  */
8463       insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8464       goto done;
8465
8466     case 0x3c:                  /* ifcall9 */
8467       insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8468       goto done;
8469     case 0x3d:                  /* movpi45 */
8470       insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8471       goto done;
8472
8473     case 0x3f:                  /* MISC33 */
8474       switch (insn16 & 0x7)
8475         {
8476         case 2:                 /* neg33 */
8477           insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8478           break;
8479         case 3:                 /* not33 */
8480           insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8481                            N16_RA3 (insn16));
8482           break;
8483         case 4:                 /* mul33 */
8484           insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8485                            N16_RA3 (insn16));
8486           break;
8487         case 5:                 /* xor33 */
8488           insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8489                            N16_RA3 (insn16));
8490           break;
8491         case 6:                 /* and33 */
8492           insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8493                            N16_RA3 (insn16));
8494           break;
8495         case 7:                 /* or33 */
8496           insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8497                            N16_RA3 (insn16));
8498           break;
8499         }
8500       goto done;
8501
8502     case 0xb:
8503       switch (insn16 & 0x7)
8504         {
8505         case 0:                 /* zeb33 */
8506           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8507           break;
8508         case 1:                 /* zeh33 */
8509           insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8510           break;
8511         case 2:                 /* seb33 */
8512           insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8513           break;
8514         case 3:                 /* seh33 */
8515           insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8516           break;
8517         case 4:                 /* xlsb33 */
8518           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8519           break;
8520         case 5:                 /* x11b33 */
8521           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8522           break;
8523         case 6:                 /* bmski33 */
8524           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8525                             1 << __GF (insn16, 3, 3));
8526           break;
8527         case 7:                 /* fexti33 */
8528           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8529                             (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8530           break;
8531         }
8532       goto done;
8533     }
8534
8535   switch (__GF (insn16, 10, 5))
8536     {
8537     case 0x0:                   /* mov55 or ifret16 */
8538       if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8539           && N16_RT5 (insn16) == N16_RA5 (insn16))
8540         insn = N32_JREG (JR, 0, 0, 0, 3);
8541       else
8542         insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8543       goto done;
8544     case 0x1:                   /* movi55 */
8545       insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8546       goto done;
8547     case 0x1b:                  /* addi10s (V2) */
8548       insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8549       goto done;
8550     }
8551
8552   switch (__GF (insn16, 11, 4))
8553     {
8554     case 0x7:                   /* lwi37.fp/swi37.fp */
8555       if (insn16 & N32_BIT (7)) /* swi37.fp */
8556         insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8557       else                      /* lwi37.fp */
8558         insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8559       goto done;
8560     case 0x8:                   /* beqz38 */
8561       insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8562       goto done;
8563     case 0x9:                   /* bnez38 */
8564       insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8565       goto done;
8566     case 0xa:                   /* beqs38/j8, implied r5 */
8567       if (N16_RT38 (insn16) == 5)
8568         insn = N32_JI (J, N16_IMM8S (insn16));
8569       else
8570         insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8571       goto done;
8572     case 0xb:                   /* bnes38 and others.  */
8573       if (N16_RT38 (insn16) == 5)
8574         {
8575           switch (__GF (insn16, 5, 3))
8576             {
8577             case 0:             /* jr5 */
8578               insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8579               break;
8580             case 4:             /* ret5 */
8581               insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8582               break;
8583             case 1:             /* jral5 */
8584               insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8585               break;
8586             case 2:             /* ex9.it imm5 */
8587               /* ex9.it had no 32-bit variantl.  */
8588               break;
8589             case 5:             /* add5.pc */
8590               /* add5.pc had no 32-bit variantl.  */
8591               break;
8592             }
8593         }
8594       else                      /* bnes38 */
8595         insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8596       goto done;
8597     case 0xe:                   /* lwi37/swi37 */
8598       if (insn16 & (1 << 7))    /* swi37.sp */
8599         insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8600       else                      /* lwi37.sp */
8601         insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8602       goto done;
8603     }
8604
8605 done:
8606   if (insn & 0x80000000)
8607     return 0;
8608
8609   if (pinsn)
8610     *pinsn = insn;
8611   return 1;
8612 }
8613 \f
8614
8615 static bfd_boolean
8616 is_sda_access_insn (unsigned long insn)
8617 {
8618   switch (N32_OP6 (insn))
8619     {
8620     case N32_OP6_LWI:
8621     case N32_OP6_LHI:
8622     case N32_OP6_LHSI:
8623     case N32_OP6_LBI:
8624     case N32_OP6_LBSI:
8625     case N32_OP6_SWI:
8626     case N32_OP6_SHI:
8627     case N32_OP6_SBI:
8628     case N32_OP6_LWC:
8629     case N32_OP6_LDC:
8630     case N32_OP6_SWC:
8631     case N32_OP6_SDC:
8632       return TRUE;
8633     default:
8634       ;
8635     }
8636   return FALSE;
8637 }
8638
8639 static unsigned long
8640 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8641 {
8642   uint32_t oinsn = 0;
8643
8644   switch (type)
8645     {
8646     case R_NDS32_GOT_LO12:
8647     case R_NDS32_GOTOFF_LO12:
8648     case R_NDS32_PLTREL_LO12:
8649     case R_NDS32_PLT_GOTREL_LO12:
8650     case R_NDS32_LO12S0_RELA:
8651       switch (N32_OP6 (insn))
8652         {
8653         case N32_OP6_LBI:
8654           /* lbi.gp */
8655           oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8656           break;
8657         case N32_OP6_LBSI:
8658           /* lbsi.gp */
8659           oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8660           break;
8661         case N32_OP6_SBI:
8662           /* sbi.gp */
8663           oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8664           break;
8665         case N32_OP6_ORI:
8666           /* addi.gp */
8667           oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8668           break;
8669         }
8670       break;
8671
8672     case R_NDS32_LO12S1_RELA:
8673       switch (N32_OP6 (insn))
8674         {
8675         case N32_OP6_LHI:
8676           /* lhi.gp */
8677           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8678           break;
8679         case N32_OP6_LHSI:
8680           /* lhsi.gp */
8681           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8682           break;
8683         case N32_OP6_SHI:
8684           /* shi.gp */
8685           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8686           break;
8687         }
8688       break;
8689
8690     case R_NDS32_LO12S2_RELA:
8691       switch (N32_OP6 (insn))
8692         {
8693         case N32_OP6_LWI:
8694           /* lwi.gp */
8695           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8696           break;
8697         case N32_OP6_SWI:
8698           /* swi.gp */
8699           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8700           break;
8701         }
8702       break;
8703
8704     case R_NDS32_LO12S2_DP_RELA:
8705     case R_NDS32_LO12S2_SP_RELA:
8706       oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8707       break;
8708     }
8709
8710   if (oinsn)
8711     *pinsn = oinsn;
8712
8713   return oinsn != 0;
8714 }
8715
8716 /* Linker hasn't found the correct merge section for non-section symbol
8717    in relax time, this work is left to the function elf_link_input_bfd().
8718    So for non-section symbol, _bfd_merged_section_offset is also needed
8719    to find the correct symbol address.  */
8720
8721 static bfd_vma
8722 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8723                           asection **psec, Elf_Internal_Rela *rel)
8724 {
8725   asection *sec = *psec;
8726   bfd_vma relocation;
8727
8728   relocation = (sec->output_section->vma
8729                 + sec->output_offset + sym->st_value);
8730   if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8731     {
8732       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8733         rel->r_addend =
8734           _bfd_merged_section_offset (abfd, psec,
8735                                       elf_section_data (sec)->sec_info,
8736                                       sym->st_value + rel->r_addend);
8737       else
8738         rel->r_addend =
8739           _bfd_merged_section_offset (abfd, psec,
8740                                       elf_section_data (sec)->sec_info,
8741                                       sym->st_value) + rel->r_addend;
8742
8743       if (sec != *psec)
8744         {
8745           /* If we have changed the section, and our original section is
8746              marked with SEC_EXCLUDE, it means that the original
8747              SEC_MERGE section has been completely subsumed in some
8748              other SEC_MERGE section.  In this case, we need to leave
8749              some info around for --emit-relocs.  */
8750           if ((sec->flags & SEC_EXCLUDE) != 0)
8751             sec->kept_section = *psec;
8752           sec = *psec;
8753         }
8754       rel->r_addend -= relocation;
8755       rel->r_addend += sec->output_section->vma + sec->output_offset;
8756     }
8757   return relocation;
8758 }
8759
8760 static bfd_vma
8761 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8762                           Elf_Internal_Sym *isymbuf,
8763                           Elf_Internal_Shdr *symtab_hdr)
8764 {
8765   bfd_signed_vma foff;
8766   bfd_vma symval, addend;
8767   Elf_Internal_Rela irel_fn;
8768   Elf_Internal_Sym *isym;
8769   asection *sym_sec;
8770
8771   /* Get the value of the symbol referred to by the reloc.  */
8772   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8773     {
8774       /* A local symbol.  */
8775       isym = isymbuf + ELF32_R_SYM (irel->r_info);
8776
8777       if (isym->st_shndx == SHN_UNDEF)
8778         sym_sec = bfd_und_section_ptr;
8779       else if (isym->st_shndx == SHN_ABS)
8780         sym_sec = bfd_abs_section_ptr;
8781       else if (isym->st_shndx == SHN_COMMON)
8782         sym_sec = bfd_com_section_ptr;
8783       else
8784         sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8785       memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8786       symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8787       addend = irel_fn.r_addend;
8788     }
8789   else
8790     {
8791       unsigned long indx;
8792       struct elf_link_hash_entry *h;
8793
8794       /* An external symbol.  */
8795       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8796       h = elf_sym_hashes (abfd)[indx];
8797       BFD_ASSERT (h != NULL);
8798
8799       while (h->root.type == bfd_link_hash_indirect
8800              || h->root.type == bfd_link_hash_warning)
8801         h = (struct elf_link_hash_entry *) h->root.u.i.link;
8802
8803       if (h->root.type != bfd_link_hash_defined
8804           && h->root.type != bfd_link_hash_defweak)
8805         /* This appears to be a reference to an undefined
8806            symbol.  Just ignore it--it will be caught by the
8807            regular reloc processing.  */
8808         return 0;
8809
8810       if (h->root.u.def.section->flags & SEC_MERGE)
8811         {
8812           sym_sec = h->root.u.def.section;
8813           symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8814                                                (sym_sec)->sec_info, h->root.u.def.value);
8815           symval = symval + sym_sec->output_section->vma
8816                    + sym_sec->output_offset;
8817         }
8818       else
8819         symval = (h->root.u.def.value
8820                   + h->root.u.def.section->output_section->vma
8821                   + h->root.u.def.section->output_offset);
8822       addend = irel->r_addend;
8823     }
8824
8825   foff = symval + addend;
8826
8827   return foff;
8828 }
8829
8830 static int
8831 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8832               asection *sec, Elf_Internal_Rela *rel)
8833 {
8834   bfd_byte *contents;
8835   unsigned short insn16;
8836
8837   if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8838     return FALSE;
8839   contents = elf_section_data (sec)->this_hdr.contents;
8840   insn16 = bfd_getb16 (contents + rel->r_offset);
8841   if (insn16 == NDS32_NOP16)
8842     return TRUE;
8843   return FALSE;
8844 }
8845
8846 /* It checks whether the instruction could be converted to
8847    16-bit form and returns the converted one.
8848
8849    `internal_relocs' is supposed to be sorted.  */
8850
8851 static int
8852 is_convert_32_to_16 (bfd *abfd, asection *sec,
8853                      Elf_Internal_Rela *reloc,
8854                      Elf_Internal_Rela *internal_relocs,
8855                      Elf_Internal_Rela *irelend,
8856                      uint16_t *insn16)
8857 {
8858 #define NORMAL_32_TO_16 (1 << 0)
8859 #define SPECIAL_32_TO_16 (1 << 1)
8860   bfd_byte *contents = NULL;
8861   bfd_signed_vma off;
8862   bfd_vma mem_addr;
8863   uint32_t insn = 0;
8864   Elf_Internal_Rela *pc_rel;
8865   Elf_Internal_Shdr *symtab_hdr;
8866   Elf_Internal_Sym *isymbuf = NULL;
8867   int convert_type;
8868   bfd_vma offset;
8869
8870   if (reloc->r_offset + 4 > sec->size)
8871     return FALSE;
8872
8873   offset = reloc->r_offset;
8874
8875   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
8876     return FALSE;
8877   insn = bfd_getb32 (contents + offset);
8878
8879   if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8880     convert_type = NORMAL_32_TO_16;
8881   else if (special_convert_32_to_16 (insn, insn16, reloc))
8882     convert_type = SPECIAL_32_TO_16;
8883   else
8884     return FALSE;
8885
8886   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8887   if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8888     return FALSE;
8889
8890   /* Find the first relocation of the same relocation-type,
8891      so we iteratie them forward.  */
8892   pc_rel = reloc;
8893   while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8894     pc_rel--;
8895
8896   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8897     {
8898       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8899           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8900           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8901           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8902         {
8903           off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8904           if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8905               || off == 0)
8906             return FALSE;
8907           break;
8908         }
8909       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8910         {
8911           /* movi => movi55  */
8912           mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8913                                                symtab_hdr);
8914           /* mem_addr is unsigned, but the value should
8915              be between [-16, 15].  */
8916           if ((mem_addr + 0x10) >> 5)
8917             return FALSE;
8918           break;
8919         }
8920       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8921                || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8922         {
8923           /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8924              because it can be relaxed to addi for TLS_LE_ADD.  */
8925           return FALSE;
8926         }
8927       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8928                 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8929                && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8930                && convert_type == SPECIAL_32_TO_16)
8931         {
8932           /* fp-as-gp
8933              We've selected a best fp-base for this access, so we can
8934              always resolve it anyway.  Do nothing.  */
8935           break;
8936         }
8937       else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8938                 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8939                || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8940                    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8941                || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8942                    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8943         {
8944           /* Prevent unresolved addi instruction translate
8945              to addi45 or addi333.  */
8946           return FALSE;
8947         }
8948       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8949         {
8950           off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8951           if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8952             return FALSE;
8953           break;
8954         }
8955     }
8956
8957   return TRUE;
8958 }
8959
8960 static void
8961 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8962                     Elf_Internal_Rela *reloc,
8963                     Elf_Internal_Rela *internal_relocs,
8964                     Elf_Internal_Rela *irelend,
8965                     unsigned short insn16)
8966 {
8967   Elf_Internal_Rela *pc_rel;
8968   bfd_vma offset;
8969
8970   offset = reloc->r_offset;
8971   bfd_putb16 (insn16, contents + offset);
8972   /* Find the first relocation of the same relocation-type,
8973      so we iteratie them forward.  */
8974   pc_rel = reloc;
8975   while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8976     pc_rel--;
8977
8978   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8979     {
8980       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8981           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8982           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8983         {
8984           pc_rel->r_info =
8985             ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8986         }
8987       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8988         pc_rel->r_info =
8989           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8990       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8991         pc_rel->r_info =
8992           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8993       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8994                || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8995         pc_rel->r_info =
8996           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8997       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8998         pc_rel->r_info =
8999           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
9000     }
9001 }
9002
9003 /* Find a relocation of type specified by `reloc_type'
9004    of the same r_offset with reloc.
9005    If not found, return irelend.
9006
9007    Assuming relocations are sorted by r_offset,
9008    we find the relocation from `reloc' backward untill relocs,
9009    or find it from `reloc' forward untill irelend.  */
9010
9011 static Elf_Internal_Rela *
9012 find_relocs_at_address (Elf_Internal_Rela *reloc,
9013                         Elf_Internal_Rela *relocs,
9014                         Elf_Internal_Rela *irelend,
9015                         enum elf_nds32_reloc_type reloc_type)
9016 {
9017   Elf_Internal_Rela *rel_t;
9018
9019   /* Find backward.  */
9020   for (rel_t = reloc;
9021        rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9022        rel_t--)
9023     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9024       return rel_t;
9025
9026   /* We didn't find it backward.  Try find it forward.  */
9027   for (rel_t = reloc;
9028        rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9029        rel_t++)
9030     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9031       return rel_t;
9032
9033   return irelend;
9034 }
9035
9036 /* Find a relocation of specified type and offset.
9037    `reloc' is just a refence point to find a relocation at specified offset.
9038    If not found, return irelend.
9039
9040    Assuming relocations are sorted by r_offset,
9041    we find the relocation from `reloc' backward untill relocs,
9042    or find it from `reloc' forward untill irelend.  */
9043
9044 static Elf_Internal_Rela *
9045 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9046                              Elf_Internal_Rela *relocs,
9047                              Elf_Internal_Rela *irelend,
9048                              enum elf_nds32_reloc_type reloc_type,
9049                              bfd_vma offset_p)
9050 {
9051   Elf_Internal_Rela *rel_t = NULL;
9052
9053   /* First, we try to find a relocation of offset `offset_p',
9054      and then we use find_relocs_at_address to find specific type.  */
9055
9056   if (reloc->r_offset > offset_p)
9057     {
9058       /* Find backward.  */
9059       for (rel_t = reloc;
9060            rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9061         /* Do nothing.  */;
9062     }
9063   else if (reloc->r_offset < offset_p)
9064     {
9065       /* Find forward.  */
9066       for (rel_t = reloc;
9067            rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9068         /* Do nothing.  */;
9069     }
9070   else
9071     rel_t = reloc;
9072
9073   /* Not found?  */
9074   if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9075     return irelend;
9076
9077   return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9078 }
9079
9080 typedef struct nds32_elf_blank nds32_elf_blank_t;
9081 struct nds32_elf_blank
9082 {
9083   /* Where the blank begins.  */
9084   bfd_vma offset;
9085   /* The size of the blank.  */
9086   bfd_vma size;
9087   /* The accumulative size before this blank.  */
9088   bfd_vma total_size;
9089   nds32_elf_blank_t *next;
9090   nds32_elf_blank_t *prev;
9091 };
9092
9093 static nds32_elf_blank_t *blank_free_list = NULL;
9094
9095 static nds32_elf_blank_t *
9096 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9097 {
9098   nds32_elf_blank_t *blank_t;
9099
9100   if (blank_free_list)
9101     {
9102       blank_t = blank_free_list;
9103       blank_free_list = blank_free_list->next;
9104     }
9105   else
9106     blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9107
9108   if (blank_t == NULL)
9109     return NULL;
9110
9111   blank_t->offset = offset_p;
9112   blank_t->size = size_p;
9113   blank_t->total_size = 0;
9114   blank_t->next = NULL;
9115   blank_t->prev = NULL;
9116
9117   return blank_t;
9118 }
9119
9120 static void
9121 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9122 {
9123   if (blank_free_list)
9124     {
9125       blank_free_list->prev = blank_p;
9126       blank_p->next = blank_free_list;
9127     }
9128   else
9129     blank_p->next = NULL;
9130
9131   blank_p->prev = NULL;
9132   blank_free_list = blank_p;
9133 }
9134
9135 static void
9136 clean_nds32_elf_blank (void)
9137 {
9138   nds32_elf_blank_t *blank_t;
9139
9140   while (blank_free_list)
9141     {
9142       blank_t = blank_free_list;
9143       blank_free_list = blank_free_list->next;
9144       free (blank_t);
9145     }
9146 }
9147
9148 static nds32_elf_blank_t *
9149 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9150 {
9151   nds32_elf_blank_t *blank_t;
9152
9153   if (!blank_p)
9154     return NULL;
9155   blank_t = blank_p;
9156
9157   while (blank_t && addr < blank_t->offset)
9158     blank_t = blank_t->prev;
9159   while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9160     blank_t = blank_t->next;
9161
9162   return blank_t;
9163 }
9164
9165 static bfd_vma
9166 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9167                            int overwrite)
9168 {
9169   nds32_elf_blank_t *blank_t;
9170
9171   blank_t = search_nds32_elf_blank (*blank_p, addr);
9172   if (!blank_t)
9173     return 0;
9174
9175   if (overwrite)
9176     *blank_p = blank_t;
9177
9178   if (addr < blank_t->offset + blank_t->size)
9179     return blank_t->total_size + (addr - blank_t->offset);
9180   else
9181     return blank_t->total_size + blank_t->size;
9182 }
9183
9184 static bfd_boolean
9185 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9186 {
9187   nds32_elf_blank_t *blank_t, *blank_t2;
9188
9189   if (!*blank_p)
9190     {
9191       *blank_p = create_nds32_elf_blank (addr, len);
9192       return *blank_p ? TRUE : FALSE;
9193     }
9194
9195   blank_t = search_nds32_elf_blank (*blank_p, addr);
9196
9197   if (blank_t == NULL)
9198     {
9199       blank_t = create_nds32_elf_blank (addr, len);
9200       if (!blank_t)
9201         return FALSE;
9202       while ((*blank_p)->prev != NULL)
9203         *blank_p = (*blank_p)->prev;
9204       blank_t->next = *blank_p;
9205       (*blank_p)->prev = blank_t;
9206       (*blank_p) = blank_t;
9207       return TRUE;
9208     }
9209
9210   if (addr < blank_t->offset + blank_t->size)
9211     {
9212       /* Extend the origin blank.  */
9213       if (addr + len > blank_t->offset + blank_t->size)
9214         blank_t->size = addr + len - blank_t->offset;
9215     }
9216   else
9217     {
9218       blank_t2 = create_nds32_elf_blank (addr, len);
9219       if (!blank_t2)
9220         return FALSE;
9221       if (blank_t->next)
9222         {
9223           blank_t->next->prev = blank_t2;
9224           blank_t2->next = blank_t->next;
9225         }
9226       blank_t2->prev = blank_t;
9227       blank_t->next = blank_t2;
9228       *blank_p = blank_t2;
9229     }
9230
9231   return TRUE;
9232 }
9233
9234 static bfd_boolean
9235 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9236                                      bfd_vma len)
9237 {
9238   nds32_elf_blank_t *blank_t;
9239
9240   if (!insert_nds32_elf_blank (blank_p, addr, len))
9241     return FALSE;
9242
9243   blank_t = *blank_p;
9244
9245   if (!blank_t->prev)
9246     {
9247       blank_t->total_size = 0;
9248       blank_t = blank_t->next;
9249     }
9250
9251   while (blank_t)
9252     {
9253       blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9254       blank_t = blank_t->next;
9255     }
9256
9257   return TRUE;
9258 }
9259
9260 static void
9261 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9262 {
9263   nds32_elf_blank_t *blank_t;
9264   bfd_vma total_size = 0;
9265
9266   if (!blank_p)
9267     return;
9268
9269   blank_t = blank_p;
9270   while (blank_t->prev)
9271     blank_t = blank_t->prev;
9272   while (blank_t)
9273     {
9274       blank_t->total_size = total_size;
9275       total_size += blank_t->size;
9276       blank_t = blank_t->next;
9277     }
9278 }
9279
9280 static bfd_boolean
9281 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9282                                nds32_elf_blank_t *blank_p)
9283 {
9284   Elf_Internal_Shdr *symtab_hdr;        /* Symbol table header of this bfd.  */
9285   Elf_Internal_Sym *isym = NULL;        /* Symbol table of this bfd.  */
9286   Elf_Internal_Sym *isymend;            /* Symbol entry iterator.  */
9287   unsigned int sec_shndx;               /* The section the be relaxed.  */
9288   bfd_byte *contents;                   /* Contents data of iterating section.  */
9289   Elf_Internal_Rela *internal_relocs;
9290   Elf_Internal_Rela *irel;
9291   Elf_Internal_Rela *irelend;
9292   struct elf_link_hash_entry **sym_hashes;
9293   struct elf_link_hash_entry **end_hashes;
9294   unsigned int symcount;
9295   asection *sect;
9296   nds32_elf_blank_t *blank_t;
9297   nds32_elf_blank_t *blank_t2;
9298   nds32_elf_blank_t *blank_head;
9299
9300   blank_head = blank_t = blank_p;
9301   while (blank_head->prev != NULL)
9302     blank_head = blank_head->prev;
9303   while (blank_t->next != NULL)
9304     blank_t = blank_t->next;
9305
9306   if (blank_t->offset + blank_t->size <= sec->size)
9307     {
9308       blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9309       blank_t->next->prev = blank_t;
9310     }
9311   if (blank_head->offset > 0)
9312     {
9313       blank_head->prev = create_nds32_elf_blank (0, 0);
9314       blank_head->prev->next = blank_head;
9315       blank_head = blank_head->prev;
9316     }
9317
9318   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9319
9320   /* The deletion must stop at the next ALIGN reloc for an alignment
9321      power larger than the number of bytes we are deleting.  */
9322
9323   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9324   if (!nds32_get_local_syms (abfd, sec, &isym))
9325     return FALSE;
9326
9327   if (isym == NULL)
9328     {
9329       isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9330                                    symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9331       symtab_hdr->contents = (bfd_byte *) isym;
9332     }
9333
9334   if (isym == NULL || symtab_hdr->sh_info == 0)
9335     return FALSE;
9336
9337   blank_t = blank_head;
9338   calc_nds32_blank_total (blank_head);
9339
9340   for (sect = abfd->sections; sect != NULL; sect = sect->next)
9341     {
9342       /* Adjust all the relocs.  */
9343
9344       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
9345       internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9346                                                    TRUE /* keep_memory */);
9347       irelend = internal_relocs + sect->reloc_count;
9348
9349       blank_t = blank_head;
9350       blank_t2 = blank_head;
9351
9352       if (!(sect->flags & SEC_RELOC))
9353         continue;
9354
9355       nds32_get_section_contents (abfd, sect, &contents, TRUE);
9356
9357       for (irel = internal_relocs; irel < irelend; irel++)
9358         {
9359           bfd_vma raddr;
9360
9361           if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9362               && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9363               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9364             {
9365               unsigned long val = 0;
9366               unsigned long mask;
9367               long before, between;
9368               long offset = 0;
9369
9370               switch (ELF32_R_TYPE (irel->r_info))
9371                 {
9372                 case R_NDS32_DIFF8:
9373                   offset = bfd_get_8 (abfd, contents + irel->r_offset);
9374                   break;
9375                 case R_NDS32_DIFF16:
9376                   offset = bfd_get_16 (abfd, contents + irel->r_offset);
9377                   break;
9378                 case R_NDS32_DIFF32:
9379                   val = bfd_get_32 (abfd, contents + irel->r_offset);
9380                   /* Get the signed bit and mask for the high part.  The
9381                      gcc will alarm when right shift 32-bit since the
9382                      type size of long may be 32-bit.  */
9383                   mask = 0 - (val >> 31);
9384                   if (mask)
9385                     offset = (val | (mask - 0xffffffff));
9386                   else
9387                     offset = val;
9388                   break;
9389                 default:
9390                   BFD_ASSERT (0);
9391                 }
9392
9393               /*                  DIFF value
9394                 0            |encoded in location|
9395                 |------------|-------------------|---------
9396                             sym+off(addend)
9397                 -- before ---| *****************
9398                 --------------------- between ---|
9399
9400                 We only care how much data are relax between DIFF,
9401                 marked as ***.  */
9402
9403               before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9404               between = get_nds32_elf_blank_total (&blank_t,
9405                                                    irel->r_addend + offset, 0);
9406               if (between == before)
9407                 goto done_adjust_diff;
9408
9409               switch (ELF32_R_TYPE (irel->r_info))
9410                 {
9411                 case R_NDS32_DIFF8:
9412                   bfd_put_8 (abfd, offset - (between - before),
9413                              contents + irel->r_offset);
9414                   break;
9415                 case R_NDS32_DIFF16:
9416                   bfd_put_16 (abfd, offset - (between - before),
9417                               contents + irel->r_offset);
9418                   break;
9419                 case R_NDS32_DIFF32:
9420                   bfd_put_32 (abfd, offset - (between - before),
9421                               contents + irel->r_offset);
9422                   break;
9423                 }
9424             }
9425           else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9426               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9427             {
9428               bfd_vma val = 0;
9429               unsigned int len = 0;
9430               unsigned long before, between;
9431               bfd_byte *endp, *p;
9432
9433               val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9434                                                &len);
9435
9436               before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9437               between = get_nds32_elf_blank_total (&blank_t,
9438                                                    irel->r_addend + val, 0);
9439               if (between == before)
9440                 goto done_adjust_diff;
9441
9442               p = contents + irel->r_offset;
9443               endp = p + len -1;
9444               memset (p, 0x80, len);
9445               *(endp) = 0;
9446               p = write_uleb128 (p, val - (between - before)) - 1;
9447               if (p < endp)
9448                 *p |= 0x80;
9449             }
9450 done_adjust_diff:
9451
9452           if (sec == sect)
9453             {
9454               raddr = irel->r_offset;
9455               irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9456                                                            irel->r_offset, 1);
9457
9458               if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9459                 continue;
9460               if (blank_t2 && blank_t2->next
9461                   && (blank_t2->offset > raddr
9462                       || blank_t2->next->offset <= raddr))
9463                 _bfd_error_handler
9464                   (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9465                    abfd);
9466
9467               /* Mark reloc in deleted portion as NONE.
9468                  For some relocs like R_NDS32_LABEL that doesn't modify the
9469                  content in the section.  R_NDS32_LABEL doesn't belong to the
9470                  instruction in the section, so we should preserve it.  */
9471               if (raddr >= blank_t2->offset
9472                   && raddr < blank_t2->offset + blank_t2->size
9473                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9474                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9475                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9476                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9477                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9478                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9479                 {
9480                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9481                                                R_NDS32_NONE);
9482                   continue;
9483                 }
9484             }
9485
9486           if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9487               || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9488               || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9489             continue;
9490
9491           if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9492               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9493               && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9494             {
9495               if (irel->r_addend <= sec->size)
9496                 irel->r_addend -=
9497                   get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9498             }
9499         }
9500     }
9501
9502   /* Adjust the local symbols defined in this section.  */
9503   blank_t = blank_head;
9504   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9505     {
9506       if (isym->st_shndx == sec_shndx)
9507         {
9508           if (isym->st_value <= sec->size)
9509             {
9510               bfd_vma ahead;
9511               bfd_vma orig_addr = isym->st_value;
9512
9513               ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9514               isym->st_value -= ahead;
9515
9516               /* Adjust function size.  */
9517               if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9518                   && isym->st_size > 0)
9519                 isym->st_size -=
9520                   get_nds32_elf_blank_total
9521                   (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9522             }
9523         }
9524     }
9525
9526   /* Now adjust the global symbols defined in this section.  */
9527   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9528               - symtab_hdr->sh_info);
9529   sym_hashes = elf_sym_hashes (abfd);
9530   end_hashes = sym_hashes + symcount;
9531   blank_t = blank_head;
9532   for (; sym_hashes < end_hashes; sym_hashes++)
9533     {
9534       struct elf_link_hash_entry *sym_hash = *sym_hashes;
9535
9536       if ((sym_hash->root.type == bfd_link_hash_defined
9537            || sym_hash->root.type == bfd_link_hash_defweak)
9538           && sym_hash->root.u.def.section == sec)
9539         {
9540           if (sym_hash->root.u.def.value <= sec->size)
9541             {
9542               bfd_vma ahead;
9543               bfd_vma orig_addr = sym_hash->root.u.def.value;
9544
9545               ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9546               sym_hash->root.u.def.value -= ahead;
9547
9548               /* Adjust function size.  */
9549               if (sym_hash->type == STT_FUNC)
9550                 sym_hash->size -=
9551                   get_nds32_elf_blank_total
9552                   (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9553
9554             }
9555         }
9556     }
9557
9558   contents = elf_section_data (sec)->this_hdr.contents;
9559   blank_t = blank_head;
9560   while (blank_t->next)
9561     {
9562       /* Actually delete the bytes.  */
9563
9564       /* If current blank is the last blank overlap with current section,
9565          go to finish process.  */
9566       if (sec->size <= (blank_t->next->offset))
9567         break;
9568
9569       memmove (contents + blank_t->offset - blank_t->total_size,
9570                contents + blank_t->offset + blank_t->size,
9571                blank_t->next->offset - (blank_t->offset + blank_t->size));
9572
9573       blank_t = blank_t->next;
9574     }
9575
9576   if (sec->size > (blank_t->offset + blank_t->size))
9577     {
9578       /* There are remaining code between blank and section boundary.
9579          Move the remaining code to appropriate location.  */
9580       memmove (contents + blank_t->offset - blank_t->total_size,
9581                contents + blank_t->offset + blank_t->size,
9582                sec->size - (blank_t->offset + blank_t->size));
9583       sec->size -= blank_t->total_size + blank_t->size;
9584     }
9585   else
9586     /* This blank is not entirely included in the section,
9587        reduce the section size by only part of the blank size.  */
9588     sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9589
9590   while (blank_head)
9591     {
9592       blank_t = blank_head;
9593       blank_head = blank_head->next;
9594       remove_nds32_elf_blank (blank_t);
9595     }
9596
9597   return TRUE;
9598 }
9599
9600 /* Get the contents of a section.  */
9601
9602 static int
9603 nds32_get_section_contents (bfd *abfd, asection *sec,
9604                             bfd_byte **contents_p, bfd_boolean cache)
9605 {
9606   /* Get the section contents.  */
9607   if (elf_section_data (sec)->this_hdr.contents != NULL)
9608     *contents_p = elf_section_data (sec)->this_hdr.contents;
9609   else
9610     {
9611       if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9612         return FALSE;
9613       if (cache)
9614         elf_section_data (sec)->this_hdr.contents = *contents_p;
9615     }
9616
9617   return TRUE;
9618 }
9619
9620 /* Get the contents of the internal symbol of abfd.  */
9621
9622 static int
9623 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9624                       Elf_Internal_Sym **isymbuf_p)
9625 {
9626   Elf_Internal_Shdr *symtab_hdr;
9627   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9628
9629   /* Read this BFD's local symbols if we haven't done so already.  */
9630   if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9631     {
9632       *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9633       if (*isymbuf_p == NULL)
9634         {
9635           *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9636                                              symtab_hdr->sh_info, 0,
9637                                              NULL, NULL, NULL);
9638           if (*isymbuf_p == NULL)
9639             return FALSE;
9640         }
9641     }
9642   symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9643
9644   return TRUE;
9645 }
9646
9647 /* Range of small data.  */
9648 static bfd_vma sdata_range[2][2];
9649 static bfd_vma const sdata_init_range[2] =
9650 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
9651
9652 static int
9653 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9654                      bfd_byte *contents, bfd_vma addr)
9655 {
9656   unsigned long insn = bfd_getb32 (contents + addr);
9657
9658   if (insn & 0x80000000)
9659     return 2;
9660
9661   return 4;
9662 }
9663
9664 /* Set the gp relax range.  We have to measure the safe range
9665    to do gp relaxation.  */
9666
9667 static void
9668 relax_range_measurement (bfd *abfd)
9669 {
9670   asection *sec_f, *sec_b;
9671   /* For upper bound.   */
9672   bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9673   bfd_vma align;
9674   static int decide_relax_range = 0;
9675   int i;
9676   int range_number = ARRAY_SIZE (sdata_init_range);
9677
9678   if (decide_relax_range)
9679     return;
9680   decide_relax_range = 1;
9681
9682   if (sda_rela_sec == NULL)
9683     {
9684       /* Since there is no data sections, we assume the range is page size.  */
9685       for (i = 0; i < range_number; i++)
9686         {
9687           sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9688           sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9689         }
9690       return;
9691     }
9692
9693   /* Get the biggest alignment power after the gp located section.  */
9694   sec_f = sda_rela_sec->output_section;
9695   sec_b = sec_f->next;
9696   align = 0;
9697   while (sec_b != NULL)
9698     {
9699       if ((unsigned)(1 << sec_b->alignment_power) > align)
9700         align = (1 << sec_b->alignment_power);
9701       sec_b = sec_b->next;
9702     }
9703
9704   /* I guess we can not determine the section before
9705      gp located section, so we assume the align is max page size.  */
9706   for (i = 0; i < range_number; i++)
9707     {
9708       sdata_range[i][1] = sdata_init_range[i] - align;
9709       BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9710       sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9711       BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9712     }
9713 }
9714
9715 /* These are macros used to check flags encoded in r_addend.
9716    They are only used by nds32_elf_relax_section ().  */
9717 #define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
9718 #define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
9719 #define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
9720 #define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
9721
9722 static const char * unrecognized_reloc_msg =
9723   /* xgettext:c-format */
9724   N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9725
9726 /* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
9727
9728 static bfd_boolean
9729 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9730                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9731                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9732                            Elf_Internal_Shdr *symtab_hdr)
9733 {
9734   /* There are 3 variations for LONGCALL1
9735      case 4-4-2; 16-bit on, optimize off or optimize for space
9736      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
9737      ori   ta, ta, lo12(symbol) ; LO12S0
9738      jral5 ta                   ;
9739
9740      case 4-4-4; 16-bit off, optimize don't care
9741      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
9742      ori   ta, ta, lo12(symbol) ; LO12S0
9743      jral  ta                   ;
9744
9745      case 4-4-4; 16-bit on, optimize for speed
9746      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
9747      ori   ta, ta, lo12(symbol) ; LO12S0
9748      jral  ta                   ;
9749      Check code for -mlong-calls output.  */
9750
9751   /* Get the reloc for the address from which the register is
9752      being loaded.  This reloc will tell us which function is
9753      actually being called.  */
9754
9755   bfd_vma laddr;
9756   int seq_len;  /* Original length of instruction sequence.  */
9757   uint32_t insn;
9758   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9759   bfd_signed_vma foff;
9760   uint16_t insn16;
9761
9762   irelend = internal_relocs + sec->reloc_count;
9763   seq_len = GET_SEQ_LEN (irel->r_addend);
9764   laddr = irel->r_offset;
9765   *insn_len = seq_len;
9766
9767   hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9768                                            R_NDS32_HI20_RELA, laddr);
9769   lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9770                                            R_NDS32_LO12S0_ORI_RELA,
9771                                            laddr + 4);
9772
9773   if (hi_irelfn == irelend || lo_irelfn == irelend)
9774     {
9775       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9776                           (uint64_t) irel->r_offset);
9777       return FALSE;
9778     }
9779
9780   /* Get the value of the symbol referred to by the reloc.  */
9781   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9782
9783   /* This condition only happened when symbol is undefined.  */
9784   if (foff == 0
9785       || foff < -CONSERVATIVE_24BIT_S1
9786       || foff >= CONSERVATIVE_24BIT_S1)
9787     return FALSE;
9788
9789   /* Relax to: jal symbol; 25_PCREL.  */
9790   /* For simplicity of coding, we are going to modify the section
9791      contents, the section relocs, and the BFD symbol table.  We
9792      must tell the rest of the code not to free up this
9793      information.  It would be possible to instead create a table
9794      of changes which have to be made, as is done in coff-mips.c;
9795      that would be more work, but would require less memory when
9796      the linker is run.  */
9797
9798   /* Replace the long call with a jal.  */
9799   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9800                                R_NDS32_25_PCREL_RELA);
9801   irel->r_addend = hi_irelfn->r_addend;
9802
9803   /* We don't resolve this here but resolve it in relocate_section.  */
9804   insn = INSN_JAL;
9805   bfd_putb32 (insn, contents + irel->r_offset);
9806
9807   hi_irelfn->r_info =
9808     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9809   lo_irelfn->r_info =
9810     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9811   *insn_len = 4;
9812
9813   if (seq_len & 0x2)
9814     {
9815       insn16 = NDS32_NOP16;
9816       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9817       lo_irelfn->r_info =
9818         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9819       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9820       *insn_len += 2;
9821     }
9822   return TRUE;
9823 }
9824
9825 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9826 /* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
9827
9828 static bfd_boolean
9829 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9830                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9831                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9832                            Elf_Internal_Shdr *symtab_hdr)
9833 {
9834   /* bltz  rt, .L1   ; LONGCALL2
9835      jal   symbol   ; 25_PCREL
9836      .L1: */
9837
9838   /* Get the reloc for the address from which the register is
9839      being loaded.  This reloc will tell us which function is
9840      actually being called.  */
9841
9842   bfd_vma laddr;
9843   uint32_t insn;
9844   Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9845   bfd_signed_vma foff;
9846
9847   irelend = internal_relocs + sec->reloc_count;
9848   laddr = irel->r_offset;
9849   i1_irelfn =
9850     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9851                                  R_NDS32_25_PCREL_RELA, laddr + 4);
9852
9853   if (i1_irelfn == irelend)
9854     {
9855       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9856                           (uint64_t) irel->r_offset);
9857       return FALSE;
9858     }
9859
9860   insn = bfd_getb32 (contents + laddr);
9861
9862   /* Get the value of the symbol referred to by the reloc.  */
9863   foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9864
9865   if (foff == 0
9866       || foff < -CONSERVATIVE_16BIT_S1
9867       || foff >= CONSERVATIVE_16BIT_S1)
9868     return FALSE;
9869
9870   /* Relax to   bgezal   rt, label ; 17_PCREL
9871      or         bltzal   rt, label ; 17_PCREL */
9872
9873   /* Convert to complimentary conditional call.  */
9874   insn = CONVERT_CONDITION_CALL (insn);
9875
9876   /* For simplicity of coding, we are going to modify the section
9877      contents, the section relocs, and the BFD symbol table.  We
9878      must tell the rest of the code not to free up this
9879      information.  It would be possible to instead create a table
9880      of changes which have to be made, as is done in coff-mips.c;
9881      that would be more work, but would require less memory when
9882      the linker is run.  */
9883
9884   /* Clean unnessary relocations.  */
9885   i1_irelfn->r_info =
9886     ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9887   cond_irelfn =
9888     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9889                                  R_NDS32_17_PCREL_RELA, laddr);
9890   if (cond_irelfn != irelend)
9891     cond_irelfn->r_info =
9892       ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9893
9894   /* Replace the long call with a bgezal.  */
9895   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9896                                R_NDS32_17_PCREL_RELA);
9897   irel->r_addend = i1_irelfn->r_addend;
9898
9899   bfd_putb32 (insn, contents + irel->r_offset);
9900
9901   *insn_len = 4;
9902   return TRUE;
9903 }
9904
9905 /* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9906
9907 static bfd_boolean
9908 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9909                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9910                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9911                            Elf_Internal_Shdr *symtab_hdr)
9912 {
9913   /* There are 3 variations for LONGCALL3
9914      case 4-4-4-2; 16-bit on, optimize off or optimize for space
9915      bltz  rt,   $1                ; LONGCALL3
9916      sethi ta,   hi20(symbol)      ; HI20
9917      ori   ta, ta,  lo12(symbol)   ; LO12S0
9918      jral5 ta                      ;
9919      $1
9920
9921      case 4-4-4-4; 16-bit off, optimize don't care
9922      bltz  rt,   $1                ; LONGCALL3
9923      sethi ta,   hi20(symbol)      ; HI20
9924      ori   ta, ta,  lo12(symbol)   ; LO12S0
9925      jral  ta                      ;
9926      $1
9927
9928      case 4-4-4-4; 16-bit on, optimize for speed
9929      bltz  rt,   $1                ; LONGCALL3
9930      sethi ta,   hi20(symbol)      ; HI20
9931      ori   ta, ta,  lo12(symbol)   ; LO12S0
9932      jral  ta                      ;
9933      $1 */
9934
9935   /* Get the reloc for the address from which the register is
9936      being loaded.  This reloc will tell us which function is
9937      actually being called.  */
9938
9939   bfd_vma laddr;
9940   int seq_len;  /* Original length of instruction sequence.  */
9941   uint32_t insn;
9942   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9943   bfd_signed_vma foff;
9944   uint16_t insn16;
9945
9946   irelend = internal_relocs + sec->reloc_count;
9947   seq_len = GET_SEQ_LEN (irel->r_addend);
9948   laddr = irel->r_offset;
9949   *insn_len = seq_len;
9950
9951   hi_irelfn =
9952     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9953                                  R_NDS32_HI20_RELA, laddr + 4);
9954   lo_irelfn =
9955     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9956                                  R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9957
9958   if (hi_irelfn == irelend || lo_irelfn == irelend)
9959     {
9960       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9961                           (uint64_t) irel->r_offset);
9962       return FALSE;
9963     }
9964
9965   /* Get the value of the symbol referred to by the reloc.  */
9966   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9967
9968   if (foff == 0
9969       || foff < -CONSERVATIVE_24BIT_S1
9970       || foff >= CONSERVATIVE_24BIT_S1)
9971     return FALSE;
9972
9973   insn = bfd_getb32 (contents + laddr);
9974   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9975     {
9976       /* Relax to  bgezal   rt, label ; 17_PCREL
9977          or        bltzal   rt, label ; 17_PCREL */
9978
9979       /* Convert to complimentary conditional call.  */
9980       insn = CONVERT_CONDITION_CALL (insn);
9981       bfd_putb32 (insn, contents + irel->r_offset);
9982
9983       *insn_len = 4;
9984       irel->r_info =
9985         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9986       hi_irelfn->r_info =
9987         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9988       lo_irelfn->r_info =
9989         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9990
9991       cond_irelfn =
9992         find_relocs_at_address_addr (irel, internal_relocs, irelend,
9993                                      R_NDS32_17_PCREL_RELA, laddr);
9994       if (cond_irelfn != irelend)
9995         {
9996           cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9997                                               R_NDS32_17_PCREL_RELA);
9998           cond_irelfn->r_addend = hi_irelfn->r_addend;
9999         }
10000
10001       if (seq_len & 0x2)
10002         {
10003           insn16 = NDS32_NOP16;
10004           bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10005           hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10006                                             R_NDS32_INSN16);
10007           hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10008           insn_len += 2;
10009         }
10010     }
10011   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10012     {
10013       /* Relax to the following instruction sequence
10014          bltz  rt,   $1 ; LONGCALL2
10015          jal   symbol   ; 25_PCREL
10016          $1     */
10017       *insn_len = 8;
10018       insn = INSN_JAL;
10019       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10020
10021       hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10022                                         R_NDS32_25_PCREL_RELA);
10023       irel->r_info =
10024         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10025
10026       lo_irelfn->r_info =
10027         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10028
10029       if (seq_len & 0x2)
10030         {
10031           insn16 = NDS32_NOP16;
10032           bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10033           lo_irelfn->r_info =
10034             ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10035           lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10036           insn_len += 2;
10037         }
10038     }
10039   return TRUE;
10040 }
10041
10042 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
10043
10044 static bfd_boolean
10045 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10046                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10047                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10048                            Elf_Internal_Shdr *symtab_hdr)
10049 {
10050   /* There are 3 variations for LONGJUMP1
10051      case 4-4-2; 16-bit bit on, optimize off or optimize for space
10052      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
10053      ori   ta, ta, lo12(symbol)  ; LO12S0
10054      jr5   ta                    ;
10055
10056      case 4-4-4; 16-bit off, optimize don't care
10057      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
10058      ori   ta, ta, lo12(symbol)  ; LO12S0
10059      jr    ta                    ;
10060
10061      case 4-4-4; 16-bit on, optimize for speed
10062      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
10063      ori   ta, ta, lo12(symbol)  ; LO12S0
10064      jr    ta                    ;      */
10065
10066   /* Get the reloc for the address from which the register is
10067      being loaded.  This reloc will tell us which function is
10068      actually being called.  */
10069
10070   bfd_vma laddr;
10071   int seq_len;  /* Original length of instruction sequence.  */
10072   int insn16_on;        /* 16-bit on/off.  */
10073   uint32_t insn;
10074   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10075   bfd_signed_vma foff;
10076   uint16_t insn16;
10077   unsigned long reloc;
10078
10079   irelend = internal_relocs + sec->reloc_count;
10080   seq_len = GET_SEQ_LEN (irel->r_addend);
10081   laddr = irel->r_offset;
10082   *insn_len = seq_len;
10083   insn16_on = IS_16BIT_ON (irel->r_addend);
10084
10085   hi_irelfn =
10086     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10087                                  R_NDS32_HI20_RELA, laddr);
10088   lo_irelfn =
10089     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10090                                  R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10091   if (hi_irelfn == irelend || lo_irelfn == irelend)
10092     {
10093       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10094                           (uint64_t) irel->r_offset);
10095       return FALSE;
10096     }
10097
10098   /* Get the value of the symbol referred to by the reloc.  */
10099   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10100
10101   if (foff == 0
10102       || foff >= CONSERVATIVE_24BIT_S1
10103       || foff < -CONSERVATIVE_24BIT_S1)
10104     return FALSE;
10105
10106   if (insn16_on
10107       && foff >= -ACCURATE_8BIT_S1
10108       && foff < ACCURATE_8BIT_S1
10109       && (seq_len & 0x2))
10110     {
10111       /* j8     label */
10112       /* 16-bit on, but not optimized for speed.  */
10113       reloc = R_NDS32_9_PCREL_RELA;
10114       insn16 = INSN_J8;
10115       bfd_putb16 (insn16, contents + irel->r_offset);
10116       *insn_len = 2;
10117       irel->r_info =
10118         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10119     }
10120   else
10121     {
10122       /* j     label */
10123       reloc = R_NDS32_25_PCREL_RELA;
10124       insn = INSN_J;
10125       bfd_putb32 (insn, contents + irel->r_offset);
10126       *insn_len = 4;
10127       irel->r_info =
10128         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10129       irel->r_addend = 0;
10130     }
10131
10132   hi_irelfn->r_info =
10133     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10134   lo_irelfn->r_info =
10135     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10136
10137   if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10138     {
10139       insn16 = NDS32_NOP16;
10140       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10141       lo_irelfn->r_info =
10142         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10143                       R_NDS32_INSN16);
10144       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10145       *insn_len += 2;
10146     }
10147   return TRUE;
10148 }
10149
10150 /* Revert condition branch.  This function does not check if the input
10151    instruction is condition branch or not.  */
10152
10153 static void
10154 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10155                            uint16_t *re_insn16, uint32_t *re_insn)
10156 {
10157   uint32_t comp_insn = 0;
10158   uint16_t comp_insn16 = 0;
10159
10160   if (insn)
10161     {
10162       if (N32_OP6 (insn) == N32_OP6_BR1)
10163         {
10164           /* beqs label.  */
10165           comp_insn = (insn ^ 0x4000) & 0xffffc000;
10166           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10167             {
10168               /* Insn can be contracted to 16-bit implied r5.  */
10169               comp_insn16 =
10170                 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10171               comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10172             }
10173         }
10174       else if (N32_OP6 (insn) == N32_OP6_BR3)
10175         {
10176           /* bnec $ta, imm11, label.  */
10177           comp_insn = (insn ^ 0x80000) & 0xffffff00;
10178         }
10179       else
10180         {
10181           comp_insn = (insn ^ 0x10000) & 0xffffc000;
10182           if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10183               || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10184             {
10185               if (N32_IS_RT3 (insn))
10186                 {
10187                   /* Insn can be contracted to 16-bit.  */
10188                   comp_insn16 =
10189                     (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10190                   comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10191                 }
10192               else if (N32_RT5 (insn) == REG_R15)
10193                 {
10194                   /* Insn can be contracted to 16-bit.  */
10195                   comp_insn16 =
10196                     (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10197                 }
10198             }
10199         }
10200     }
10201   else
10202     {
10203       switch ((insn16 & 0xf000) >> 12)
10204         {
10205         case 0xc:
10206           /* beqz38 or bnez38 */
10207           comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10208           comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10209           comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10210           break;
10211
10212         case 0xd:
10213           /* beqs38 or bnes38 */
10214           comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10215           comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10216           comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10217             | (REG_R5 << 15);
10218           break;
10219
10220         case 0xe:
10221           /* beqzS8 or bnezS8 */
10222           comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10223           comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10224           comp_insn |= REG_R15 << 20;
10225           break;
10226
10227         default:
10228           break;
10229         }
10230     }
10231   if (comp_insn && re_insn)
10232     *re_insn = comp_insn;
10233   if (comp_insn16 && re_insn16)
10234     *re_insn16 = comp_insn16;
10235 }
10236
10237 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
10238
10239 static bfd_boolean
10240 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10241                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10242                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10243                            Elf_Internal_Shdr *symtab_hdr)
10244 {
10245   /* There are 3 variations for LONGJUMP2
10246      case 2-4;  1st insn convertible, 16-bit on,
10247      optimize off or optimize for space
10248      bnes38  rt, ra, $1 ; LONGJUMP2
10249      j       label      ; 25_PCREL
10250      $1:
10251
10252      case 4-4; 1st insn not convertible
10253      bne  rt, ra, $1 ; LONGJUMP2
10254      j    label      ; 25_PCREL
10255      $1:
10256
10257      case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10258      bne  rt, ra, $1 ; LONGJUMP2
10259      j    label      ; 25_PCREL
10260      $1: */
10261
10262   /* Get the reloc for the address from which the register is
10263      being loaded.  This reloc will tell us which function is
10264      actually being called.  */
10265
10266   bfd_vma laddr;
10267   int seq_len;  /* Original length of instruction sequence.  */
10268   Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10269   int first_size;
10270   unsigned int i;
10271   bfd_signed_vma foff;
10272   uint32_t insn, re_insn = 0;
10273   uint16_t insn16, re_insn16 = 0;
10274   unsigned long reloc, cond_reloc;
10275
10276   enum elf_nds32_reloc_type checked_types[] =
10277     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10278
10279   irelend = internal_relocs + sec->reloc_count;
10280   seq_len = GET_SEQ_LEN (irel->r_addend);
10281   laddr = irel->r_offset;
10282   *insn_len = seq_len;
10283   first_size = (seq_len == 6) ? 2 : 4;
10284
10285   i2_irelfn =
10286     find_relocs_at_address_addr (irel, internal_relocs,
10287                                  irelend, R_NDS32_25_PCREL_RELA,
10288                                  laddr + first_size);
10289
10290   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10291     {
10292       cond_irelfn =
10293         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10294                                      checked_types[i], laddr);
10295       if (cond_irelfn != irelend)
10296         break;
10297     }
10298
10299   if (i2_irelfn == irelend || cond_irelfn == irelend)
10300     {
10301       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10302                           (uint64_t) irel->r_offset);
10303       return FALSE;
10304     }
10305
10306   /* Get the value of the symbol referred to by the reloc.  */
10307   foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10308   if (foff == 0
10309       || foff < -CONSERVATIVE_16BIT_S1
10310       || foff >= CONSERVATIVE_16BIT_S1)
10311     return FALSE;
10312
10313   /* Get the all corresponding instructions.  */
10314   if (first_size == 4)
10315     {
10316       insn = bfd_getb32 (contents + laddr);
10317       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10318     }
10319   else
10320     {
10321       insn16 = bfd_getb16 (contents + laddr);
10322       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10323     }
10324
10325   if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10326       && foff < ACCURATE_8BIT_S1 - first_size)
10327     {
10328       if (first_size == 4)
10329         {
10330           /* Don't convert it to 16-bit now, keep this as relaxable for
10331              ``label reloc; INSN16''.  */
10332
10333           /* Save comp_insn32 to buffer.  */
10334           bfd_putb32 (re_insn, contents + irel->r_offset);
10335           *insn_len = 4;
10336           reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10337             R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10338           cond_reloc = R_NDS32_INSN16;
10339         }
10340       else
10341         {
10342           bfd_putb16 (re_insn16, contents + irel->r_offset);
10343           *insn_len = 2;
10344           reloc = R_NDS32_9_PCREL_RELA;
10345           cond_reloc = R_NDS32_NONE;
10346         }
10347     }
10348   else if (N32_OP6 (re_insn) == N32_OP6_BR1
10349            && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10350                && foff < ACCURATE_14BIT_S1 - first_size))
10351     {
10352       /* beqs     label    ; 15_PCREL */
10353       bfd_putb32 (re_insn, contents + irel->r_offset);
10354       *insn_len = 4;
10355       reloc = R_NDS32_15_PCREL_RELA;
10356       cond_reloc = R_NDS32_NONE;
10357     }
10358   else if (N32_OP6 (re_insn) == N32_OP6_BR2
10359            && foff >= -CONSERVATIVE_16BIT_S1
10360            && foff < CONSERVATIVE_16BIT_S1)
10361     {
10362       /* beqz     label ; 17_PCREL */
10363       bfd_putb32 (re_insn, contents + irel->r_offset);
10364       *insn_len = 4;
10365       reloc = R_NDS32_17_PCREL_RELA;
10366       cond_reloc = R_NDS32_NONE;
10367     }
10368   else
10369     return FALSE;
10370
10371   /* Set all relocations.  */
10372   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10373   irel->r_addend = i2_irelfn->r_addend;
10374
10375   cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10376                                       cond_reloc);
10377   cond_irelfn->r_addend = 0;
10378
10379   if ((seq_len ^ *insn_len ) & 0x2)
10380     {
10381       insn16 = NDS32_NOP16;
10382       bfd_putb16 (insn16, contents + irel->r_offset + 4);
10383       i2_irelfn->r_offset = 4;
10384       i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10385                                         R_NDS32_INSN16);
10386       i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10387       *insn_len += 2;
10388     }
10389   else
10390     i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10391                                       R_NDS32_NONE);
10392   return TRUE;
10393 }
10394
10395 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
10396
10397 static bfd_boolean
10398 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10399                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10400                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10401                            Elf_Internal_Shdr *symtab_hdr)
10402 {
10403   /* There are 5 variations for LONGJUMP3
10404      case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10405      optimize off or optimize for space
10406      bnes38   rt, ra, $1            ; LONGJUMP3
10407      sethi    ta, hi20(symbol)      ; HI20
10408      ori      ta, ta, lo12(symbol)  ; LO12S0
10409      jr5      ta                    ;
10410      $1:                            ;
10411
10412      case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10413      bnes38   rt, ra, $1           ; LONGJUMP3
10414      sethi    ta, hi20(symbol)     ; HI20
10415      ori      ta, ta, lo12(symbol) ; LO12S0
10416      jr5      ta                   ;
10417      $1:                           ; LABEL
10418
10419      case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10420      optimize off or optimize for space
10421      bne   rt, ra, $1           ; LONGJUMP3
10422      sethi ta, hi20(symbol)     ; HI20
10423      ori   ta, ta, lo12(symbol) ; LO12S0
10424      jr5   ta                   ;
10425      $1:                        ;
10426
10427      case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10428      16-bit off if no INSN16
10429      bne   rt, ra, $1           ; LONGJUMP3
10430      sethi ta, hi20(symbol)     ; HI20
10431      ori   ta, ta, lo12(symbol) ; LO12S0
10432      jr    ta                   ;
10433      $1:                        ;
10434
10435      case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10436      16-bit off if no INSN16
10437      bne   rt, ra, $1           ; LONGJUMP3
10438      sethi ta, hi20(symbol)     ; HI20
10439      ori   ta, ta, lo12(symbol) ; LO12S0
10440      jr    ta                   ;
10441      $1:                        ; LABEL */
10442
10443   /* Get the reloc for the address from which the register is
10444      being loaded.  This reloc will tell us which function is
10445      actually being called.  */
10446   enum elf_nds32_reloc_type checked_types[] =
10447     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10448
10449   int reloc_off = 0, cond_removed = 0, convertible;
10450   bfd_vma laddr;
10451   int seq_len;  /* Original length of instruction sequence.  */
10452   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10453   int first_size;
10454   unsigned int i;
10455   bfd_signed_vma foff;
10456   uint32_t insn, re_insn = 0;
10457   uint16_t insn16, re_insn16 = 0;
10458   unsigned long reloc, cond_reloc;
10459
10460   irelend = internal_relocs + sec->reloc_count;
10461   seq_len = GET_SEQ_LEN (irel->r_addend);
10462   laddr = irel->r_offset;
10463   *insn_len = seq_len;
10464
10465   convertible = IS_1ST_CONVERT (irel->r_addend);
10466
10467   if (convertible)
10468     first_size = 2;
10469   else
10470     first_size = 4;
10471
10472   /* Get all needed relocations.  */
10473   hi_irelfn =
10474     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10475                                  R_NDS32_HI20_RELA, laddr + first_size);
10476   lo_irelfn =
10477     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10478                                  R_NDS32_LO12S0_ORI_RELA,
10479                                  laddr + first_size + 4);
10480
10481   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10482     {
10483       cond_irelfn =
10484         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10485                                      checked_types[i], laddr);
10486       if (cond_irelfn != irelend)
10487         break;
10488     }
10489
10490   if (hi_irelfn == irelend
10491       || lo_irelfn == irelend
10492       || cond_irelfn == irelend)
10493     {
10494       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10495                           (uint64_t) irel->r_offset);
10496       return FALSE;
10497     }
10498
10499   /* Get the value of the symbol referred to by the reloc.  */
10500   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10501
10502   if (foff == 0
10503       || foff < -CONSERVATIVE_24BIT_S1
10504       || foff >= CONSERVATIVE_24BIT_S1)
10505     return FALSE;
10506
10507   /* Get the all corresponding instructions.  */
10508   if (first_size == 4)
10509     {
10510       insn = bfd_getb32 (contents + laddr);
10511       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10512     }
10513   else
10514     {
10515       insn16 = bfd_getb16 (contents + laddr);
10516       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10517     }
10518
10519   /* For simplicity of coding, we are going to modify the section
10520      contents, the section relocs, and the BFD symbol table.  We
10521      must tell the rest of the code not to free up this
10522      information.  It would be possible to instead create a table
10523      of changes which have to be made, as is done in coff-mips.c;
10524      that would be more work, but would require less memory when
10525      the linker is run.  */
10526
10527   if (re_insn16
10528       && foff >= -ACCURATE_8BIT_S1 - first_size
10529       && foff < ACCURATE_8BIT_S1 - first_size)
10530     {
10531       if (!(seq_len & 0x2))
10532         {
10533           /* Don't convert it to 16-bit now, keep this as relaxable
10534              for ``label reloc; INSN1a''6.  */
10535           /* Save comp_insn32 to buffer.  */
10536           bfd_putb32 (re_insn, contents + irel->r_offset);
10537           *insn_len = 4;
10538           reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10539             R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10540           cond_reloc = R_NDS32_INSN16;
10541         }
10542       else
10543         {
10544           /* Not optimize for speed; convert sequence to 16-bit.  */
10545           /* Save comp_insn16 to buffer.  */
10546           bfd_putb16 (re_insn16, contents + irel->r_offset);
10547           *insn_len = 2;
10548           reloc = R_NDS32_9_PCREL_RELA;
10549           cond_reloc = R_NDS32_NONE;
10550         }
10551       cond_removed = 1;
10552     }
10553   else if (N32_OP6 (re_insn) == N32_OP6_BR1
10554            && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10555                && foff < ACCURATE_14BIT_S1 - first_size))
10556     {
10557       /* beqs     label    ; 15_PCREL */
10558       bfd_putb32 (re_insn, contents + irel->r_offset);
10559       *insn_len = 4;
10560       reloc = R_NDS32_15_PCREL_RELA;
10561       cond_reloc = R_NDS32_NONE;
10562       cond_removed = 1;
10563     }
10564   else if (N32_OP6 (re_insn) == N32_OP6_BR2
10565            && foff >= -CONSERVATIVE_16BIT_S1
10566            && foff < CONSERVATIVE_16BIT_S1)
10567     {
10568       /* beqz     label ; 17_PCREL */
10569       bfd_putb32 (re_insn, contents + irel->r_offset);
10570       *insn_len = 4;
10571       reloc = R_NDS32_17_PCREL_RELA;
10572       cond_reloc = R_NDS32_NONE;
10573       cond_removed = 1;
10574     }
10575   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10576            && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10577     {
10578       /* Relax to one of the following 3 variations
10579
10580          case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10581          for space
10582          bnes38  rt, $1 ; LONGJUMP2
10583          j       label  ; 25_PCREL
10584          $1
10585
10586          case 4-4; 1st insn not convertible, others don't care
10587          bne   rt, ra, $1 ; LONGJUMP2
10588          j     label      ; 25_PCREL
10589          $1
10590
10591          case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10592          bne   rt, ra, $1 ; LONGJUMP2
10593          j     label      ; 25_PCREL
10594          $1 */
10595
10596       /* Offset for first instruction.  */
10597
10598       /* Use j label as second instruction.  */
10599       *insn_len = 4 + first_size;
10600       insn = INSN_J;
10601       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10602       reloc = R_NDS32_LONGJUMP2;
10603       cond_reloc = R_NDS32_25_PLTREL;
10604     }
10605     else
10606       return FALSE;
10607
10608     if (cond_removed == 1)
10609       {
10610         /* Set all relocations.  */
10611         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10612         irel->r_addend = hi_irelfn->r_addend;
10613
10614         cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10615                                             cond_reloc);
10616         cond_irelfn->r_addend = 0;
10617         hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10618                                           R_NDS32_NONE);
10619       }
10620     else
10621       {
10622         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10623         irel->r_addend = irel->r_addend;
10624         hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10625                                           cond_reloc);
10626       }
10627
10628   if ((seq_len ^ *insn_len ) & 0x2)
10629     {
10630       insn16 = NDS32_NOP16;
10631       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10632       lo_irelfn->r_offset = *insn_len;
10633       lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10634                                         R_NDS32_INSN16);
10635       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10636       *insn_len += 2;
10637     }
10638   else
10639     lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10640                                       R_NDS32_NONE);
10641   return TRUE;
10642 }
10643
10644 /* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
10645
10646 static bfd_boolean
10647 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10648                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10649                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10650                            Elf_Internal_Shdr *symtab_hdr)
10651 {
10652   /* The pattern for LONGCALL4.  Support for function cse.
10653      sethi ta, hi20(symbol)     ; LONGCALL4/HI20
10654      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10655      jral  ta                   ; PTR_RES/EMPTY/INSN16  */
10656
10657   bfd_vma laddr;
10658   uint32_t insn;
10659   Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10660   Elf_Internal_Rela *irelend;
10661   bfd_signed_vma foff;
10662
10663   irelend = internal_relocs + sec->reloc_count;
10664   laddr = irel->r_offset;
10665
10666   /* Get the reloc for the address from which the register is
10667      being loaded.  This reloc will tell us which function is
10668      actually being called.  */
10669   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10670                                          R_NDS32_HI20_RELA, laddr);
10671
10672   if (hi_irel == irelend)
10673     {
10674       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10675                           (uint64_t) irel->r_offset);
10676       return FALSE;
10677     }
10678
10679   /* Get the value of the symbol referred to by the reloc.  */
10680   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10681
10682   /* This condition only happened when symbol is undefined.  */
10683   if (foff == 0
10684       || foff < -CONSERVATIVE_24BIT_S1
10685       || foff >= CONSERVATIVE_24BIT_S1)
10686     return FALSE;
10687
10688   /* Relax to: jal symbol; 25_PCREL.  */
10689   /* For simplicity of coding, we are going to modify the section
10690      contents, the section relocs, and the BFD symbol table.  We
10691      must tell the rest of the code not to free up this
10692      information.  It would be possible to instead create a table
10693      of changes which have to be made, as is done in coff-mips.c;
10694      that would be more work, but would require less memory when
10695      the linker is run.  */
10696
10697   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10698                                           R_NDS32_PTR_RESOLVED, irel->r_addend);
10699   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10700                                           R_NDS32_EMPTY, irel->r_addend);
10701
10702   if (ptr_irel == irelend || em_irel == irelend)
10703     {
10704       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10705                           (uint64_t) irel->r_offset);
10706       return FALSE;
10707     }
10708   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10709   insn = bfd_getb32 (contents + irel->r_addend);
10710   if (insn & 0x80000000)
10711     return FALSE;
10712
10713   /* Replace the long call with a jal.  */
10714   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10715                                   R_NDS32_25_PCREL_RELA);
10716   ptr_irel->r_addend = 1;
10717
10718   /* We don't resolve this here but resolve it in relocate_section.  */
10719   insn = INSN_JAL;
10720   bfd_putb32 (insn, contents + em_irel->r_offset);
10721
10722   irel->r_info =
10723     ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10724
10725   /* If there is function cse, HI20 can not remove now.  */
10726   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10727                                            R_NDS32_LONGCALL4, laddr);
10728   if (call_irel == irelend)
10729     {
10730       *insn_len = 0;
10731       hi_irel->r_info =
10732         ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10733     }
10734
10735   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10736                                           R_NDS32_INSN16, irel->r_addend);
10737   if (insn_irel != irelend)
10738     insn_irel->r_info =
10739       ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10740
10741   return TRUE;
10742 }
10743
10744 /* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
10745
10746 static bfd_boolean
10747 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10748                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10749                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10750                            Elf_Internal_Shdr *symtab_hdr)
10751 {
10752   /* The pattern for LONGCALL5.
10753      bltz  rt, .L1      ; LONGCALL5/17_PCREL
10754      jal   symbol       ; 25_PCREL
10755      .L1:  */
10756
10757   bfd_vma laddr;
10758   uint32_t insn;
10759   Elf_Internal_Rela *cond_irel, *irelend;
10760   bfd_signed_vma foff;
10761
10762   irelend = internal_relocs + sec->reloc_count;
10763   laddr = irel->r_offset;
10764   insn = bfd_getb32 (contents + laddr);
10765
10766   /* Get the reloc for the address from which the register is
10767      being loaded.  This reloc will tell us which function is
10768      actually being called.  */
10769   cond_irel =
10770     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10771                                  R_NDS32_25_PCREL_RELA, irel->r_addend);
10772   if (cond_irel == irelend)
10773     {
10774       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10775                           (uint64_t) irel->r_offset);
10776       return FALSE;
10777     }
10778
10779   /* Get the value of the symbol referred to by the reloc.  */
10780   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10781
10782   if (foff == 0
10783       || foff < -CONSERVATIVE_16BIT_S1
10784       || foff >= CONSERVATIVE_16BIT_S1)
10785     return FALSE;
10786
10787   /* Relax to   bgezal   rt, label ; 17_PCREL
10788      or         bltzal   rt, label ; 17_PCREL.  */
10789
10790   /* Convert to complimentary conditional call.  */
10791   insn = CONVERT_CONDITION_CALL (insn);
10792
10793   /* For simplicity of coding, we are going to modify the section
10794      contents, the section relocs, and the BFD symbol table.  We
10795      must tell the rest of the code not to free up this
10796      information.  It would be possible to instead create a table
10797      of changes which have to be made, as is done in coff-mips.c;
10798      that would be more work, but would require less memory when
10799      the linker is run.  */
10800
10801   /* Modify relocation and contents.  */
10802   cond_irel->r_info =
10803     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10804
10805   /* Replace the long call with a bgezal.  */
10806   bfd_putb32 (insn, contents + cond_irel->r_offset);
10807   *insn_len = 0;
10808
10809   /* Clean unnessary relocations.  */
10810   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10811
10812   cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10813                                            R_NDS32_17_PCREL_RELA, laddr);
10814   cond_irel->r_info =
10815     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10816
10817   return TRUE;
10818 }
10819
10820 /* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
10821
10822 static bfd_boolean
10823 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10824                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10825                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10826                            Elf_Internal_Shdr *symtab_hdr)
10827 {
10828   /* The pattern for LONGCALL6.
10829      bltz  rt,   .L1                    ; LONGCALL6/17_PCREL
10830      sethi ta,   hi20(symbol)           ; HI20/PTR
10831      ori   ta, ta,  lo12(symbol)        ; LO12S0_ORI/PTR
10832      jral  ta                           ; PTR_RES/EMPTY/INSN16
10833      .L1  */
10834
10835   bfd_vma laddr;
10836   uint32_t insn;
10837   Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10838   bfd_signed_vma foff;
10839
10840   irelend = internal_relocs + sec->reloc_count;
10841   laddr = irel->r_offset;
10842
10843   /* Get the reloc for the address from which the register is
10844      being loaded.  This reloc will tell us which function is
10845      actually being called.  */
10846   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10847                                          R_NDS32_EMPTY, irel->r_addend);
10848
10849   if (em_irel == irelend)
10850     {
10851       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10852                           (uint64_t) irel->r_offset);
10853       return FALSE;
10854     }
10855
10856   /* Get the value of the symbol referred to by the reloc.  */
10857   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10858
10859   if (foff == 0
10860       || foff < -CONSERVATIVE_24BIT_S1
10861       || foff >= CONSERVATIVE_24BIT_S1)
10862     return FALSE;
10863
10864   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10865   insn = bfd_getb32 (contents + irel->r_addend);
10866   if (insn & 0x80000000)
10867     return FALSE;
10868
10869   insn = bfd_getb32 (contents + laddr);
10870   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10871     {
10872       /* Relax to  bgezal   rt, label ; 17_PCREL
10873          or        bltzal   rt, label ; 17_PCREL.  */
10874
10875       /* Convert to complimentary conditional call.  */
10876       *insn_len = 0;
10877       insn = CONVERT_CONDITION_CALL (insn);
10878       bfd_putb32 (insn, contents + em_irel->r_offset);
10879
10880       em_irel->r_info =
10881         ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10882
10883       /* Set resolved relocation.  */
10884       cond_irel =
10885         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10886                                      R_NDS32_PTR_RESOLVED, irel->r_addend);
10887       if (cond_irel == irelend)
10888         {
10889           _bfd_error_handler (unrecognized_reloc_msg, abfd,
10890                               "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10891           return FALSE;
10892         }
10893       cond_irel->r_addend = 1;
10894
10895       /* Clear relocations.  */
10896
10897       irel->r_info =
10898         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10899
10900       cond_irel =
10901         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10902                                      R_NDS32_17_PCREL_RELA, laddr);
10903       if (cond_irel != irelend)
10904         cond_irel->r_info =
10905           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10906
10907       cond_irel =
10908         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10909                                      R_NDS32_INSN16, irel->r_addend);
10910       if (cond_irel != irelend)
10911         cond_irel->r_info =
10912           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10913
10914     }
10915   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10916     {
10917       /* Relax to the following instruction sequence
10918          bltz  rt, .L1  ; LONGCALL2/17_PCREL
10919          jal   symbol   ; 25_PCREL/PTR_RES
10920          .L1  */
10921       *insn_len = 4;
10922       /* Convert instruction.  */
10923       insn = INSN_JAL;
10924       bfd_putb32 (insn, contents + em_irel->r_offset);
10925
10926       /* Convert relocations.  */
10927       em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10928                                       R_NDS32_25_PCREL_RELA);
10929       irel->r_info =
10930         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10931
10932       /* Set resolved relocation.  */
10933       cond_irel =
10934         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10935                                      R_NDS32_PTR_RESOLVED, irel->r_addend);
10936       if (cond_irel == irelend)
10937         {
10938           _bfd_error_handler (unrecognized_reloc_msg, abfd,
10939                               "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10940           return FALSE;
10941         }
10942       cond_irel->r_addend = 1;
10943
10944       cond_irel =
10945         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10946                                      R_NDS32_INSN16, irel->r_addend);
10947       if (cond_irel != irelend)
10948         cond_irel->r_info =
10949           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10950     }
10951   return TRUE;
10952 }
10953
10954 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10955
10956 static bfd_boolean
10957 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10958                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10959                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10960                            Elf_Internal_Shdr *symtab_hdr)
10961 {
10962   /* The pattern for LONGJUMP4.
10963      sethi ta, hi20(symbol)     ; LONGJUMP4/HI20
10964      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10965      jr    ta                   ; PTR_RES/INSN16/EMPTY  */
10966
10967   bfd_vma laddr;
10968   int seq_len;  /* Original length of instruction sequence.  */
10969   uint32_t insn;
10970   Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10971   bfd_signed_vma foff;
10972
10973   irelend = internal_relocs + sec->reloc_count;
10974   seq_len = GET_SEQ_LEN (irel->r_addend);
10975   laddr = irel->r_offset;
10976   *insn_len = seq_len;
10977
10978   /* Get the reloc for the address from which the register is
10979      being loaded.  This reloc will tell us which function is
10980      actually being called.  */
10981
10982   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10983                                          R_NDS32_HI20_RELA, laddr);
10984
10985   if (hi_irel == irelend)
10986     {
10987       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10988                           (uint64_t) irel->r_offset);
10989       return FALSE;
10990     }
10991
10992   /* Get the value of the symbol referred to by the reloc.  */
10993   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10994
10995   if (foff == 0
10996       || foff >= CONSERVATIVE_24BIT_S1
10997       || foff < -CONSERVATIVE_24BIT_S1)
10998     return FALSE;
10999
11000   /* Convert it to "j label", it may be converted to j8 in the final
11001      pass of relaxation.  Therefore, we do not consider this currently.  */
11002   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11003                                           R_NDS32_PTR_RESOLVED, irel->r_addend);
11004   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11005                                          R_NDS32_EMPTY, irel->r_addend);
11006
11007   if (ptr_irel == irelend || em_irel == irelend)
11008     {
11009       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
11010                           (uint64_t) irel->r_offset);
11011       return FALSE;
11012     }
11013
11014   em_irel->r_info =
11015     ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11016   ptr_irel->r_addend = 1;
11017
11018   /* Write instruction.  */
11019   insn = INSN_J;
11020   bfd_putb32 (insn, contents + em_irel->r_offset);
11021
11022   /* Clear relocations.  */
11023   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11024
11025   /* If there is function cse, HI20 can not remove now.  */
11026   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11027                                            R_NDS32_LONGJUMP4, laddr);
11028   if (call_irel == irelend)
11029     {
11030       *insn_len = 0;
11031       hi_irel->r_info =
11032         ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11033     }
11034
11035   return TRUE;
11036 }
11037
11038 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
11039
11040 static bfd_boolean
11041 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11042                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11043                            int *seq_len, bfd_byte *contents,
11044                            Elf_Internal_Sym *isymbuf,
11045                            Elf_Internal_Shdr *symtab_hdr)
11046 {
11047   /* There are 2 variations for LONGJUMP5
11048      case 2-4;  1st insn convertible, 16-bit on.
11049      bnes38  rt, ra, .L1        ; LONGJUMP5/9_PCREL/INSN16
11050      j       label              ; 25_PCREL/INSN16
11051      $1:
11052
11053      case 4-4; 1st insn not convertible
11054      bne  rt, ra, .L1   ; LONGJUMP5/15_PCREL/INSN16
11055      j    label         ; 25_PCREL/INSN16
11056      .L1:  */
11057
11058   bfd_vma laddr;
11059   Elf_Internal_Rela *cond_irel,  *irelend;
11060   unsigned int i;
11061   bfd_signed_vma foff;
11062   uint32_t insn, re_insn = 0;
11063   uint16_t insn16, re_insn16 = 0;
11064   unsigned long reloc;
11065
11066   enum elf_nds32_reloc_type checked_types[] =
11067     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11068       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11069
11070   irelend = internal_relocs + sec->reloc_count;
11071   laddr = irel->r_offset;
11072
11073   /* Get the reloc for the address from which the register is
11074      being loaded.  This reloc will tell us which function is
11075      actually being called.  */
11076
11077   cond_irel =
11078     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11079                                  R_NDS32_25_PCREL_RELA, irel->r_addend);
11080   if (cond_irel == irelend)
11081     {
11082       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11083                           (uint64_t) irel->r_offset);
11084       return FALSE;
11085     }
11086
11087   /* Get the value of the symbol referred to by the reloc.  */
11088   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11089
11090   if (foff == 0
11091       || foff < -CONSERVATIVE_16BIT_S1
11092       || foff >= CONSERVATIVE_16BIT_S1)
11093     return FALSE;
11094
11095   /* Get the all corresponding instructions.  */
11096   insn = bfd_getb32 (contents + laddr);
11097   /* Check instruction size.  */
11098   if (insn & 0x80000000)
11099     {
11100       *seq_len = 0;
11101       insn16 = insn >> 16;
11102       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11103     }
11104   else
11105     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11106
11107   if (N32_OP6 (re_insn) == N32_OP6_BR1
11108       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11109     {
11110       /* beqs label ; 15_PCREL.  */
11111       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11112       reloc = R_NDS32_15_PCREL_RELA;
11113     }
11114   else if (N32_OP6 (re_insn) == N32_OP6_BR2
11115            && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11116     {
11117       /* beqz label ; 17_PCREL.  */
11118       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11119       reloc = R_NDS32_17_PCREL_RELA;
11120     }
11121   else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11122            && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11123     {
11124       /* beqc label ; 9_PCREL.  */
11125       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11126       reloc = R_NDS32_WORD_9_PCREL_RELA;
11127     }
11128   else
11129     return FALSE;
11130
11131   /* Set all relocations.  */
11132   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11133
11134   /* Clean relocations.  */
11135   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11136   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11137     {
11138       cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11139                                                checked_types[i], laddr);
11140       if (cond_irel != irelend)
11141         {
11142           if (*seq_len == 0
11143               && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11144             {
11145               /* If the branch instruction is 2 byte, it cannot remove
11146                  directly.  Only convert it to nop16 and remove it after
11147                  checking alignment issue.  */
11148               insn16 = NDS32_NOP16;
11149               bfd_putb16 (insn16, contents + laddr);
11150               cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11151             }
11152           else
11153             cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11154                                               R_NDS32_NONE);
11155         }
11156     }
11157   *insn_len = 0;
11158
11159   return TRUE;
11160 }
11161
11162 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
11163
11164 static bfd_boolean
11165 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11166                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11167                            int *seq_len, bfd_byte *contents,
11168                            Elf_Internal_Sym *isymbuf,
11169                            Elf_Internal_Shdr *symtab_hdr)
11170 {
11171   /* There are 5 variations for LONGJUMP6
11172      case : 2-4-4-4; 1st insn convertible, 16-bit on.
11173      bnes38   rt, ra, .L1               ; LONGJUMP6/15_PCREL/INSN16
11174      sethi    ta, hi20(symbol)          ; HI20/PTR
11175      ori      ta, ta, lo12(symbol)      ; LO12S0_ORI/PTR
11176      jr       ta                        ; PTR_RES/INSN16/EMPTY
11177      .L1:
11178
11179      case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11180      bne   rt, ra, .L1          ; LONGJUMP6/15_PCREL/INSN16
11181      sethi ta, hi20(symbol)     ; HI20/PTR
11182      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11183      jr    ta                   ; PTR_RES/INSN16/EMPTY
11184      .L1:  */
11185
11186   enum elf_nds32_reloc_type checked_types[] =
11187     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11188       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11189
11190   int reloc_off = 0, cond_removed = 0;
11191   bfd_vma laddr;
11192   Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11193   unsigned int i;
11194   bfd_signed_vma foff;
11195   uint32_t insn, re_insn = 0;
11196   uint16_t insn16, re_insn16 = 0;
11197   unsigned long reloc;
11198
11199   irelend = internal_relocs + sec->reloc_count;
11200   laddr = irel->r_offset;
11201
11202   /* Get the reloc for the address from which the register is
11203      being loaded.  This reloc will tell us which function is
11204      actually being called.  */
11205   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11206                                          R_NDS32_EMPTY, irel->r_addend);
11207
11208   if (em_irel == irelend)
11209     {
11210       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11211                           (uint64_t) irel->r_offset);
11212       return FALSE;
11213     }
11214
11215   /* Get the value of the symbol referred to by the reloc.  */
11216   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11217
11218   if (foff == 0
11219       || foff < -CONSERVATIVE_24BIT_S1
11220       || foff >= CONSERVATIVE_24BIT_S1)
11221     return FALSE;
11222
11223   insn = bfd_getb32 (contents + laddr);
11224   /* Check instruction size.  */
11225   if (insn & 0x80000000)
11226     {
11227       *seq_len = 0;
11228       insn16 = insn >> 16;
11229       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11230     }
11231   else
11232     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11233
11234   /* For simplicity of coding, we are going to modify the section
11235      contents, the section relocs, and the BFD symbol table.  We
11236      must tell the rest of the code not to free up this
11237      information.  It would be possible to instead create a table
11238      of changes which have to be made, as is done in coff-mips.c;
11239      that would be more work, but would require less memory when
11240      the linker is run.  */
11241
11242   if (N32_OP6 (re_insn) == N32_OP6_BR1
11243       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11244     {
11245       /* beqs     label    ; 15_PCREL.  */
11246       bfd_putb32 (re_insn, contents + em_irel->r_offset);
11247       reloc = R_NDS32_15_PCREL_RELA;
11248       cond_removed = 1;
11249     }
11250   else if (N32_OP6 (re_insn) == N32_OP6_BR2
11251            && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11252     {
11253       /* beqz     label ; 17_PCREL.  */
11254       bfd_putb32 (re_insn, contents + em_irel->r_offset);
11255       reloc = R_NDS32_17_PCREL_RELA;
11256       cond_removed = 1;
11257     }
11258   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11259            && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11260     {
11261       /* Relax to one of the following 2 variations
11262
11263          case 2-4;  1st insn convertible, 16-bit on.
11264          bnes38  rt, ra, .L1    ; LONGJUMP5/9_PCREL/INSN16
11265          j       label          ; 25_PCREL/INSN16
11266          $1:
11267
11268          case 4-4; 1st insn not convertible
11269          bne  rt, ra, .L1       ; LONGJUMP5/15_PCREL/INSN16
11270          j    label             ; 25_PCREL/INSN16
11271          .L1:  */
11272
11273       /* Use j label as second instruction.  */
11274       insn = INSN_J;
11275       reloc = R_NDS32_25_PCREL_RELA;
11276       bfd_putb32 (insn, contents + em_irel->r_offset);
11277     }
11278   else
11279     return FALSE;
11280
11281   /* Set all relocations.  */
11282   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11283
11284   cond_irel =
11285     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11286                                  R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11287   cond_irel->r_addend = 1;
11288
11289   /* Use INSN16 of first branch instruction to distinguish if keeping
11290      INSN16 of final instruction or not.  */
11291   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11292                                            R_NDS32_INSN16, irel->r_offset);
11293   if (insn_irel == irelend)
11294     {
11295       /* Clean the final INSN16.  */
11296       insn_irel =
11297         find_relocs_at_address_addr (irel, internal_relocs, irelend,
11298                                      R_NDS32_INSN16, em_irel->r_offset);
11299       insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11300                                         R_NDS32_NONE);
11301     }
11302
11303   if (cond_removed == 1)
11304     {
11305       *insn_len = 0;
11306
11307       /* Clear relocations.  */
11308       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11309
11310       for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11311         {
11312           cond_irel =
11313             find_relocs_at_address_addr (irel, internal_relocs, irelend,
11314                                          checked_types[i], laddr);
11315           if (cond_irel != irelend)
11316             {
11317               if (*seq_len == 0
11318                   && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11319                 {
11320                   /* If the branch instruction is 2 byte, it cannot remove
11321                      directly.  Only convert it to nop16 and remove it after
11322                      checking alignment issue.  */
11323                   insn16 = NDS32_NOP16;
11324                   bfd_putb16 (insn16, contents + laddr);
11325                   cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11326                 }
11327               else
11328                 cond_irel->r_info =
11329                   ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11330             }
11331         }
11332     }
11333   else
11334     {
11335       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11336                                    R_NDS32_LONGJUMP5);
11337     }
11338
11339   return TRUE;
11340 }
11341
11342 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
11343
11344 static bfd_boolean
11345 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11346                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11347                            int *seq_len, bfd_byte *contents,
11348                            Elf_Internal_Sym *isymbuf,
11349                            Elf_Internal_Shdr *symtab_hdr)
11350 {
11351   /* There are 2 variations for LONGJUMP5
11352      case 2-4;  1st insn convertible, 16-bit on.
11353      movi55  ta, imm11          ; LONGJUMP7/INSN16
11354      beq     rt, ta, label      ; 15_PCREL
11355
11356      case 4-4; 1st insn not convertible
11357      movi55  ta, imm11          ; LONGJUMP7/INSN16
11358      beq     rt, ta, label      ; 15_PCREL  */
11359
11360   bfd_vma laddr;
11361   Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
11362   bfd_signed_vma foff;
11363   uint32_t insn, re_insn = 0;
11364   uint16_t insn16;
11365   uint32_t imm11;
11366
11367   irelend = internal_relocs + sec->reloc_count;
11368   laddr = irel->r_offset;
11369
11370   /* Get the reloc for the address from which the register is
11371      being loaded.  This reloc will tell us which function is
11372      actually being called.  */
11373
11374   cond_irel =
11375     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11376                                  R_NDS32_15_PCREL_RELA, irel->r_addend);
11377   if (cond_irel == irelend)
11378     {
11379       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11380                           (uint64_t) irel->r_offset);
11381       return FALSE;
11382     }
11383
11384   /* Get the value of the symbol referred to by the reloc.  */
11385   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11386
11387   if (foff == 0
11388       || foff < -CONSERVATIVE_8BIT_S1
11389       || foff >= CONSERVATIVE_8BIT_S1)
11390     return FALSE;
11391
11392   /* Get the first instruction for its size.  */
11393   insn = bfd_getb32 (contents + laddr);
11394   if (insn & 0x80000000)
11395     {
11396       *seq_len = 0;
11397       /* Get the immediate from movi55.  */
11398       imm11 = N16_IMM5S (insn >> 16);
11399     }
11400   else
11401     {
11402       /* Get the immediate from movi.  */
11403       imm11 = N32_IMM20S (insn);
11404     }
11405
11406   /* Get the branch instruction.  */
11407   insn = bfd_getb32 (contents + irel->r_addend);
11408   /* Convert instruction to BR3.  */
11409   if ((insn >> 14) & 0x1)
11410     re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11411   else
11412     re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11413
11414   bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11415
11416   /* Set all relocations.  */
11417   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11418                                     R_NDS32_WORD_9_PCREL_RELA);
11419
11420   /* Clean relocations.  */
11421   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11422   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11423                                            R_NDS32_INSN16, irel->r_offset);
11424   if (insn_irel != irelend)
11425     {
11426       if (*seq_len == 0)
11427         {
11428           /* If the first insntruction is 16bit, convert it to nop16.  */
11429           insn16 = NDS32_NOP16;
11430           bfd_putb16 (insn16, contents + laddr);
11431           insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11432         }
11433       else
11434         cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11435                                           R_NDS32_NONE);
11436     }
11437   *insn_len = 0;
11438
11439   return TRUE;
11440 }
11441
11442 /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11443    for each relax round. But the gp may changed dramatically and then cause
11444    the truncated to fit errors for the the converted gp instructions.
11445    Therefore, we must reserve the minimum but safe enough size to prevent it.  */
11446
11447 static bfd_boolean
11448 nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11449                        Elf_Internal_Rela *irel, bfd_boolean *again,
11450                        bfd_boolean init,
11451                        struct elf_nds32_link_hash_table *table,
11452                        Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11453
11454 {
11455   int offset_to_gp;
11456   static bfd_boolean sec_pass = FALSE;
11457   static asection *first_sec = NULL, *sym_sec;
11458   /* Record the number of instructions which may be removed.  */
11459   static int count = 0, record_count;
11460   Elf_Internal_Sym *isym;
11461   struct elf_link_hash_entry *h = NULL;
11462   int indx;
11463   unsigned long r_symndx;
11464   bfd *abfd = sec->owner;
11465   static bfd_vma record_sda = 0;
11466   int sda_offset = 0;
11467
11468   /* Force doing relaxation when hyper-relax is high.  */
11469   if (table->hyper_relax == 2)
11470     return TRUE;
11471
11472   /* Do not relax the load/store patterns for the first
11473      relax round.  */
11474   if (init)
11475     {
11476       if (!first_sec)
11477         first_sec = sec;
11478       else if (first_sec == sec)
11479         {
11480           record_count = count;
11481           count = 0;
11482           sec_pass = TRUE;
11483         }
11484
11485       if (!sec_pass)
11486         *again = TRUE;
11487
11488       return TRUE;
11489     }
11490
11491   /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11492      DATA_SEGMENT_ALIGN size in the linker script may make it
11493      get even bigger.  */
11494   if (record_sda == 0)
11495     record_sda = local_sda;
11496   else if (local_sda > record_sda)
11497     sda_offset = local_sda - record_sda;
11498
11499   /* Assume the instruction will be removed in the best case.  */
11500   count++;
11501
11502   /* We record the offset to gp for each symbol, and then check
11503      if it is changed dramatically after relaxing.
11504      (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11505      (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx].  */
11506   r_symndx = ELF32_R_SYM (irel->r_info);
11507   if (r_symndx >= symtab_hdr->sh_info)
11508     {
11509       /* Global symbols.  */
11510       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11511       h = elf_sym_hashes (abfd)[indx];
11512       sym_sec = h->root.u.def.section;
11513       if (NDS32_GUARD_SEC_P (sym_sec->flags)
11514           || bfd_is_abs_section (sym_sec))
11515         {
11516           /* Forbid doing relaxation when hyper-relax is low.  */
11517           if (table->hyper_relax == 0)
11518             return FALSE;
11519
11520           offset_to_gp = *access_addr - local_sda;
11521           if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11522             elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11523           else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11524                    < abs (offset_to_gp) - sda_offset)
11525             {
11526               /* This may cause the error, so we reserve the
11527                  safe enough size for relaxing.  */
11528               if (*access_addr >= local_sda)
11529                 *access_addr += (record_count * 4);
11530               else
11531                 *access_addr -= (record_count * 4);
11532             }
11533           return sec_pass;
11534         }
11535     }
11536   else
11537     {
11538       /* Local symbols.  */
11539       if (!elf32_nds32_allocate_local_sym_info (abfd))
11540         return FALSE;
11541       isym = isymbuf + r_symndx;
11542
11543       sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11544       if (NDS32_GUARD_SEC_P (sym_sec->flags))
11545         {
11546           /* Forbid doing relaxation when hyper-relax is low.  */
11547           if (table->hyper_relax == 0)
11548             return FALSE;
11549
11550           offset_to_gp = *access_addr - local_sda;
11551           if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11552             elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11553           else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11554                    < abs (offset_to_gp) - sda_offset)
11555             {
11556               /* This may cause the error, so we reserve the
11557                  safe enough size for relaxing.  */
11558               if (*access_addr >= local_sda)
11559                 *access_addr += (record_count * 4);
11560               else
11561                 *access_addr -= (record_count * 4);
11562             }
11563           return sec_pass;
11564         }
11565     }
11566
11567   return TRUE;
11568 }
11569
11570 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11571
11572 /* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
11573
11574 static bfd_boolean
11575 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11576                            asection *sec, Elf_Internal_Rela *irel,
11577                            Elf_Internal_Rela *internal_relocs, int *insn_len,
11578                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11579                            Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11580                            struct elf_nds32_link_hash_table *table)
11581 {
11582   int eliminate_sethi = 0, range_type;
11583   unsigned int i;
11584   bfd_vma local_sda, laddr;
11585   int seq_len;  /* Original length of instruction sequence.  */
11586   uint32_t insn;
11587   Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11588   bfd_vma access_addr = 0;
11589   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
11590   struct elf_link_hash_entry *h = NULL;
11591   int indx;
11592   enum elf_nds32_reloc_type checked_types[] =
11593     { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11594       R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11595       R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11596       R_NDS32_TLS_LE_HI20
11597     };
11598
11599   irelend = internal_relocs + sec->reloc_count;
11600   seq_len = GET_SEQ_LEN (irel->r_addend);
11601   laddr = irel->r_offset;
11602   *insn_len = seq_len;
11603
11604   /* Get the high part relocation.  */
11605   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11606     {
11607       hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11608                                                checked_types[i], laddr);
11609       if (hi_irelfn != irelend)
11610         break;
11611     }
11612
11613   if (hi_irelfn == irelend)
11614     {
11615       /* Not R_NDS32_HI20_RELA.  */
11616       if (i != 0)
11617         _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11618                             (uint64_t) irel->r_offset);
11619       return FALSE;
11620     }
11621
11622   range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11623   nds32_elf_final_sda_base (sec->output_section->owner,
11624                             link_info, &local_sda, FALSE);
11625
11626   switch (ELF32_R_TYPE (hi_irelfn->r_info))
11627     {
11628     case R_NDS32_HI20_RELA:
11629       insn = bfd_getb32 (contents + laddr);
11630       access_addr =
11631         calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11632
11633       if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11634         {
11635           indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11636           h = elf_sym_hashes (abfd)[indx];
11637         }
11638
11639       /* Try movi.  */
11640       if (range_type == NDS32_LOADSTORE_IMM
11641           && access_addr < CONSERVATIVE_20BIT
11642           && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11643         {
11644           eliminate_sethi = 1;
11645           break;
11646         }
11647
11648       if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11649         {
11650           eliminate_sethi = 1;
11651           break;
11652         }
11653       else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11654                                        NULL, FALSE, table, isymbuf, symtab_hdr))
11655         return FALSE;
11656
11657       if (!load_store_relax)
11658         return FALSE;
11659
11660       /* Case for set gp register.  */
11661       if (N32_RT5 (insn) == REG_GP)
11662         return FALSE;
11663
11664       if (range_type == NDS32_LOADSTORE_FLOAT_S
11665           || range_type == NDS32_LOADSTORE_FLOAT_D)
11666         {
11667           range_l = sdata_range[0][0];
11668           range_h = sdata_range[0][1];
11669         }
11670       else
11671         {
11672           range_l = sdata_range[1][0];
11673           range_h = sdata_range[1][1];
11674         }
11675       break;
11676
11677     default:
11678       return FALSE;
11679     }
11680
11681   /* Delete sethi instruction.  */
11682   if (eliminate_sethi == 1
11683       || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11684       || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11685     {
11686       hi_irelfn->r_info =
11687         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11688       irel->r_info =
11689         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11690       *insn_len = 0;
11691       return TRUE;
11692     }
11693
11694   return FALSE;
11695 }
11696
11697 /* Relax LO12 relocation for nds32_elf_relax_section.  */
11698
11699 static void
11700 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11701                       asection *sec, Elf_Internal_Rela *irel,
11702                       Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11703                       Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11704                       struct elf_nds32_link_hash_table *table)
11705 {
11706   uint32_t insn;
11707   bfd_vma local_sda, laddr;
11708   unsigned long reloc;
11709   bfd_vma access_addr;
11710   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
11711   Elf_Internal_Rela *irelfn = NULL, *irelend;
11712   struct elf_link_hash_entry *h = NULL;
11713   int indx;
11714
11715   /* For SDA base relative relaxation.  */
11716   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11717                             &local_sda, FALSE);
11718
11719   irelend = internal_relocs + sec->reloc_count;
11720   laddr = irel->r_offset;
11721   insn = bfd_getb32 (contents + laddr);
11722
11723   if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11724     return;
11725
11726   access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11727
11728   if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11729     {
11730       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11731       h = elf_sym_hashes (abfd)[indx];
11732     }
11733
11734   /* Try movi.  */
11735   if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11736       && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11737     {
11738       reloc = R_NDS32_20_RELA;
11739       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11740       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11741       bfd_putb32 (insn, contents + laddr);
11742     }
11743   else
11744     {
11745       if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11746         {
11747           /* Fall through.  */
11748         }
11749       else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11750                                        FALSE, table, isymbuf, symtab_hdr))
11751         return;
11752
11753       range_l = sdata_range[1][0];
11754       range_h = sdata_range[1][1];
11755       switch (ELF32_R_TYPE (irel->r_info))
11756         {
11757         case R_NDS32_LO12S0_RELA:
11758           reloc = R_NDS32_SDA19S0_RELA;
11759           break;
11760         case R_NDS32_LO12S1_RELA:
11761           reloc = R_NDS32_SDA18S1_RELA;
11762           break;
11763         case R_NDS32_LO12S2_RELA:
11764           reloc = R_NDS32_SDA17S2_RELA;
11765           break;
11766         case R_NDS32_LO12S2_DP_RELA:
11767           range_l = sdata_range[0][0];
11768           range_h = sdata_range[0][1];
11769           reloc = R_NDS32_SDA12S2_DP_RELA;
11770           break;
11771         case R_NDS32_LO12S2_SP_RELA:
11772           range_l = sdata_range[0][0];
11773           range_h = sdata_range[0][1];
11774           reloc = R_NDS32_SDA12S2_SP_RELA;
11775           break;
11776         default:
11777           return;
11778         }
11779
11780       /* There are range_h and range_l because linker has to promise
11781          all sections move cross one page together.  */
11782       if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11783           || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11784           || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11785         {
11786           if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11787             {
11788               /* Maybe we should add R_NDS32_INSN16 reloc type here
11789                  or manually do some optimization.  sethi can't be
11790                  eliminated when updating $gp so the relative ori
11791                  needs to be preserved.  */
11792               return;
11793             }
11794           if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11795                                         &insn))
11796             return;
11797           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11798           bfd_putb32 (insn, contents + laddr);
11799
11800           irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11801                                            R_NDS32_INSN16);
11802           /* SDA17 must keep INSN16 for converting fp_as_gp.  */
11803           if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11804             irelfn->r_info =
11805               ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11806
11807         }
11808     }
11809   return;
11810 }
11811
11812 /* Relax PTR relocation for nds32_elf_relax_section.  */
11813
11814 static bfd_boolean
11815 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11816                      Elf_Internal_Rela *internal_relocs, int *insn_len,
11817                      int *seq_len, bfd_byte *contents)
11818 {
11819   Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11820
11821   irelend = internal_relocs + sec->reloc_count;
11822
11823   re_irel =
11824     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11825                                  R_NDS32_PTR_RESOLVED, irel->r_addend);
11826
11827   if (re_irel == irelend)
11828     {
11829       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11830                           (uint64_t) irel->r_offset);
11831       return FALSE;
11832     }
11833
11834   if (re_irel->r_addend != 1)
11835     return FALSE;
11836
11837   /* Pointed target is relaxed and no longer needs this void *,
11838      change the type to NONE.  */
11839   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11840
11841   /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11842      not exist, it means only count 1 and remove it directly.  */
11843   /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11844   count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11845                                        R_NDS32_PTR_COUNT);
11846   ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11847                                      R_NDS32_PTR);
11848   if (count_irel != irelend)
11849     {
11850       if (--count_irel->r_addend > 0)
11851         return FALSE;
11852     }
11853
11854   if (ptr_irel != irelend)
11855     return FALSE;
11856
11857   /* If the PTR_COUNT is already 0, remove current instruction.  */
11858   *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11859   *insn_len = 0;
11860   return TRUE;
11861 }
11862
11863 /* Relax LWC relocation for nds32_elf_relax_section.  */
11864
11865 static void
11866 nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11867                       asection *sec, Elf_Internal_Rela *irel,
11868                       Elf_Internal_Rela *internal_relocs,
11869                       bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11870                       Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11871 {
11872   /* Pattern:
11873      sethi    ra, hi20(symbol)      ; HI20/LOADSTORE
11874      ori      ra, ra, lo12(symbol)  ; LO12S0/PTR/PTR/.../INSN16
11875      flsi     fsa, [ra + offset1]   ; LSI/PTR_RESOLVED/INSN16
11876      flsi     fsb, [ra + offset2]   ; LSI/PTR_RESOLVED/INSN16
11877      ...  */
11878
11879   uint32_t insn;
11880   bfd_vma local_sda, laddr;
11881   unsigned long reloc;
11882   bfd_vma access_addr, flsi_offset;
11883   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
11884   Elf_Internal_Rela *irelend, *re_irel;
11885   unsigned int opcode;
11886
11887   irelend = internal_relocs + sec->reloc_count;
11888   laddr = irel->r_offset;
11889   insn = bfd_getb32 (contents + laddr);
11890
11891   if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11892     return;
11893
11894   /* Can not do relaxation for bi format.  */
11895   if ((insn & 0x1000))
11896     return;
11897
11898   /* Only deal with flsi, fssi, fldi, fsdi, so far.  */
11899   opcode = N32_OP6 (insn);
11900   if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11901     reloc = R_NDS32_SDA12S2_SP_RELA;
11902   else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11903     reloc = R_NDS32_SDA12S2_DP_RELA;
11904   else
11905     return;
11906
11907   re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11908                                     R_NDS32_PTR_RESOLVED);
11909   if (re_irel == irelend)
11910     {
11911       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11912                           (uint64_t) irel->r_offset);
11913       return;
11914     }
11915
11916   /* For SDA base relative relaxation.  */
11917   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11918                             &local_sda, FALSE);
11919   access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11920   flsi_offset = (insn & 0xfff) << 2;
11921   access_addr += flsi_offset;
11922   range_l = sdata_range[0][0];
11923   range_h = sdata_range[0][1];
11924
11925   if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11926       || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11927     {
11928       /* Turn flsi instruction into sda access format.  */
11929       insn = (insn & 0x7ff07000) | (REG_GP << 15);
11930
11931       /* Add relocation type to flsi.  */
11932       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11933       irel->r_addend += flsi_offset;
11934       bfd_putb32 (insn, contents + re_irel->r_offset);
11935
11936       re_irel->r_addend |= 1;
11937       *again = TRUE;
11938     }
11939 }
11940
11941 static bfd_boolean
11942 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11943                           Elf_Internal_Rela *internal_relocs,
11944                           bfd_byte *contents,
11945                           nds32_elf_blank_t **relax_blank_list,
11946                           int optimize, int opt_size)
11947 {
11948   /* This code block is used to adjust 4-byte alignment by relax a pair
11949      of instruction a time.
11950
11951      It recognizes three types of relocations.
11952      1. R_NDS32_LABEL - a alignment.
11953      2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11954      3. is_16bit_NOP () - remove a 16-bit instruction.  */
11955
11956   /* TODO: It seems currently implementation only support 4-byte alignment.
11957      We should handle any-alignment.  */
11958
11959   Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11960   Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11961   Elf_Internal_Rela rel_temp;
11962   Elf_Internal_Rela *irelend;
11963   bfd_vma address;
11964   uint16_t insn16;
11965
11966   /* Checking for branch relaxation relies on the relocations to
11967      be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11968   nds32_insertion_sort (internal_relocs, sec->reloc_count,
11969                         sizeof (Elf_Internal_Rela), compar_reloc);
11970
11971   irelend = internal_relocs + sec->reloc_count;
11972
11973   /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11974   /* FIXME: Can we generate the right order in assembler?
11975      So we don't have to swapping them here.  */
11976
11977   for (label_rel = internal_relocs, insn_rel = internal_relocs;
11978        label_rel < irelend; label_rel++)
11979     {
11980       if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11981         continue;
11982
11983       /* Find the first reloc has the same offset with label_rel.  */
11984       while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11985         insn_rel++;
11986
11987       for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11988            insn_rel++)
11989         /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11990            address.  */
11991         if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11992           break;
11993
11994       if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11995           && insn_rel < label_rel)
11996         {
11997           /* Swap the two reloc if the R_NDS32_INSN16 is
11998              before R_NDS32_LABEL.  */
11999           memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
12000           memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
12001           memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
12002         }
12003     }
12004
12005   label_rel = NULL;
12006   insn_rel = NULL;
12007   /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12008      or higher, remove other R_NDS32_LABEL with lower alignment.
12009      If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12010      then the R_NDS32_LABEL sequence is broke.  */
12011   for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12012     {
12013       if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12014         {
12015           if (label_rel == NULL)
12016             {
12017               if (tmp_rel->r_addend < 2)
12018                 label_rel = tmp_rel;
12019               continue;
12020             }
12021           else if (tmp_rel->r_addend > 1)
12022             {
12023               /* Remove all LABEL relocation from label_rel to tmp_rel
12024                  including relocations with same offset as tmp_rel.  */
12025               for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
12026                 {
12027                   if (tmp2_rel->r_offset == tmp_rel->r_offset)
12028                     break;
12029
12030                   if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12031                       && tmp2_rel->r_addend < 2)
12032                     tmp2_rel->r_info =
12033                       ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12034                                     R_NDS32_NONE);
12035                 }
12036               label_rel = NULL;
12037             }
12038         }
12039       else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12040         {
12041           /* A new INSN16 which can be converted, so clear label_rel.  */
12042           if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12043                                    irelend, &insn16)
12044               || is_16bit_NOP (abfd, sec, tmp_rel))
12045             label_rel = NULL;
12046         }
12047     }
12048
12049   label_rel = NULL;
12050   insn_rel = NULL;
12051   /* Optimized for speed and nothing has not been relaxed.
12052      It's time to align labels.
12053      We may convert a 16-bit instruction right before a label to
12054      32-bit, in order to align the label if necessary
12055      all reloc entries has been sorted by r_offset.  */
12056   for (irel = internal_relocs;
12057        irel < irelend && irel->r_offset < sec->size; irel++)
12058     {
12059       if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12060           && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12061         continue;
12062
12063       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12064         {
12065           /* A new INSN16 found, resize the old one.  */
12066           if (is_convert_32_to_16
12067               (abfd, sec, irel, internal_relocs, irelend, &insn16)
12068               || is_16bit_NOP (abfd, sec, irel))
12069             {
12070               if (insn_rel)
12071                 {
12072                   /* Previous INSN16 reloc exists, reduce its
12073                      size to 16-bit.  */
12074                   if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12075                                            irelend, &insn16))
12076                     {
12077                       nds32_elf_write_16 (abfd, contents, insn_rel,
12078                                           internal_relocs, irelend, insn16);
12079
12080                       if (!insert_nds32_elf_blank_recalc_total
12081                           (relax_blank_list, insn_rel->r_offset + 2, 2))
12082                         return FALSE;
12083                     }
12084                   else if (is_16bit_NOP (abfd, sec, insn_rel))
12085                     {
12086                       if (!insert_nds32_elf_blank_recalc_total
12087                           (relax_blank_list, insn_rel->r_offset, 2))
12088                         return FALSE;
12089                     }
12090                   insn_rel->r_info =
12091                     ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12092                 }
12093               /* Save the new one for later use.  */
12094               insn_rel = irel;
12095             }
12096           else
12097             irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12098                                          R_NDS32_NONE);
12099         }
12100       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12101         {
12102           /* Search for label.  */
12103           int force_relax = 0;
12104
12105           /* Label on 16-bit instruction or optimization
12106              needless, just reset this reloc.  */
12107           insn16 = bfd_getb16 (contents + irel->r_offset);
12108           if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12109             {
12110               irel->r_info =
12111                 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12112               continue;
12113             }
12114
12115           address =
12116             irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12117                                                         irel->r_offset, 1);
12118
12119           if (!insn_rel)
12120             {
12121               /* Check if there is case which can not be aligned.  */
12122               if (irel->r_addend == 2 && address & 0x2)
12123                 return FALSE;
12124               continue;
12125             }
12126
12127           /* Try to align this label.  */
12128
12129           if ((irel->r_addend & 0x1f) < 2)
12130             {
12131               /* Check if there is a INSN16 at the same address.
12132                  Label_rel always seats before insn_rel after
12133                  our sort.  */
12134
12135               /* Search for INSN16 at LABEL location.  If INSN16 is at
12136                  same location and this LABEL alignment is lower than 2,
12137                  the INSN16 can be converted to 2-byte.  */
12138               for (tmp_rel = irel;
12139                    tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12140                    tmp_rel++)
12141                 {
12142                   if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12143                       && (is_convert_32_to_16
12144                           (abfd, sec, tmp_rel, internal_relocs,
12145                            irelend, &insn16)
12146                           || is_16bit_NOP (abfd, sec, tmp_rel)))
12147                     {
12148                       force_relax = 1;
12149                       break;
12150                     }
12151                 }
12152             }
12153
12154           if (force_relax || irel->r_addend == 1 || address & 0x2)
12155             {
12156               /* Label not aligned.  */
12157               /* Previous reloc exists, reduce its size to 16-bit.  */
12158               if (is_convert_32_to_16 (abfd, sec, insn_rel,
12159                                        internal_relocs, irelend, &insn16))
12160                 {
12161                   nds32_elf_write_16 (abfd, contents, insn_rel,
12162                                       internal_relocs, irelend, insn16);
12163
12164                   if (!insert_nds32_elf_blank_recalc_total
12165                       (relax_blank_list, insn_rel->r_offset + 2, 2))
12166                     return FALSE;
12167                 }
12168               else if (is_16bit_NOP (abfd, sec, insn_rel))
12169                 {
12170                   if (!insert_nds32_elf_blank_recalc_total
12171                       (relax_blank_list, insn_rel->r_offset, 2))
12172                     return FALSE;
12173                 }
12174
12175             }
12176           /* INSN16 reloc is used.  */
12177           insn_rel = NULL;
12178         }
12179     }
12180
12181   address =
12182     sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12183   if (insn_rel && (address & 0x2 || opt_size))
12184     {
12185       if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12186                                irelend, &insn16))
12187         {
12188           nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12189                               irelend, insn16);
12190           if (!insert_nds32_elf_blank_recalc_total
12191               (relax_blank_list, insn_rel->r_offset + 2, 2))
12192             return FALSE;
12193           insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12194                                            R_NDS32_NONE);
12195         }
12196       else if (is_16bit_NOP (abfd, sec, insn_rel))
12197         {
12198           if (!insert_nds32_elf_blank_recalc_total
12199               (relax_blank_list, insn_rel->r_offset, 2))
12200             return FALSE;
12201           insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12202                                            R_NDS32_NONE);
12203         }
12204     }
12205   insn_rel = NULL;
12206   return TRUE;
12207 }
12208
12209 static bfd_boolean
12210 nds32_elf_relax_section (bfd *abfd, asection *sec,
12211                          struct bfd_link_info *link_info, bfd_boolean *again)
12212 {
12213   nds32_elf_blank_t *relax_blank_list = NULL;
12214   Elf_Internal_Shdr *symtab_hdr;
12215   Elf_Internal_Rela *internal_relocs;
12216   Elf_Internal_Rela *irel;
12217   Elf_Internal_Rela *irelend;
12218   Elf_Internal_Sym *isymbuf = NULL;
12219   bfd_byte *contents = NULL;
12220   bfd_boolean result = TRUE;
12221   int optimize = 0;
12222   int opt_size = 0;
12223   uint32_t insn;
12224   uint16_t insn16;
12225
12226   /* Target dependnet option.  */
12227   struct elf_nds32_link_hash_table *table;
12228   int load_store_relax;
12229
12230   relax_blank_list = NULL;
12231
12232   *again = FALSE;
12233
12234   /* Nothing to do for
12235    * relocatable link or
12236    * non-relocatable section or
12237    * non-code section or
12238    * empty content or
12239    * no reloc entry.  */
12240   if (bfd_link_relocatable (link_info)
12241       || (sec->flags & SEC_RELOC) == 0
12242       || (sec->flags & SEC_EXCLUDE) != 0
12243       || (sec->flags & SEC_CODE) == 0
12244       || sec->size == 0
12245       || sec->reloc_count == 0)
12246     return TRUE;
12247
12248   /* 09.12.11 Workaround.  */
12249   /*  We have to adjust align for R_NDS32_LABEL if needed.
12250       The adjust approach only can fix 2-byte align once.  */
12251   if (sec->alignment_power > 2)
12252     return TRUE;
12253
12254   /* Do TLS model conversion once at first.  */
12255   nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12256
12257   /* The optimization type to do.  */
12258
12259   table = nds32_elf_hash_table (link_info);
12260
12261   /* Save the first section for abs symbol relaxation.
12262      This is used for checking gp relaxation in the
12263      nds32_elf_relax_loadstore and nds32_elf_relax_lo12.  */
12264   nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12265                          table, NULL, NULL);
12266
12267   /* The begining of general relaxation.  */
12268
12269   if (is_SDA_BASE_set == 0)
12270     {
12271       bfd_vma gp;
12272       is_SDA_BASE_set = 1;
12273       nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12274                                 &gp, FALSE);
12275       relax_range_measurement (abfd);
12276     }
12277
12278   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12279   /* Relocations MUST be kept in memory, because relaxation adjust them.  */
12280   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12281                                                TRUE /* keep_memory */);
12282   if (internal_relocs == NULL)
12283     goto error_return;
12284
12285   irelend = internal_relocs + sec->reloc_count;
12286   irel = find_relocs_at_address (internal_relocs, internal_relocs,
12287                                  irelend, R_NDS32_RELAX_ENTRY);
12288
12289   if (irel == irelend)
12290     return TRUE;
12291
12292   if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12293     {
12294       if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12295         return TRUE;
12296
12297       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12298         optimize = 1;
12299
12300       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12301         opt_size = 1;
12302     }
12303
12304   load_store_relax = table->load_store_relax;
12305
12306   /* Get symbol table and section content.  */
12307   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12308       || !nds32_get_local_syms (abfd, sec, &isymbuf))
12309     goto error_return;
12310
12311   /* Do relax loop only when finalize is not done.
12312      Take care of relaxable relocs except INSN16.  */
12313   for (irel = internal_relocs; irel < irelend; irel++)
12314     {
12315       int seq_len;              /* Original length of instruction sequence.  */
12316       int insn_len = 0;         /* Final length of instruction sequence.  */
12317       bfd_boolean removed;
12318
12319       insn = 0;
12320       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12321           && (irel->r_addend & 0x1f) >= 2)
12322         optimize = 1;
12323
12324       /* Relocation Types
12325          R_NDS32_LONGCALL1      53
12326          R_NDS32_LONGCALL2      54
12327          R_NDS32_LONGCALL3      55
12328          R_NDS32_LONGJUMP1      56
12329          R_NDS32_LONGJUMP2      57
12330          R_NDS32_LONGJUMP3      58
12331          R_NDS32_LOADSTORE      59  */
12332       if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12333           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12334         seq_len = GET_SEQ_LEN (irel->r_addend);
12335
12336       /* Relocation Types
12337          R_NDS32_LONGCALL4      107
12338          R_NDS32_LONGCALL5      108
12339          R_NDS32_LONGCALL6      109
12340          R_NDS32_LONGJUMP4      110
12341          R_NDS32_LONGJUMP5      111
12342          R_NDS32_LONGJUMP6      112
12343          R_NDS32_LONGJUMP7      113  */
12344       else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12345                && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12346         seq_len = 4;
12347
12348         /* Relocation Types
12349          R_NDS32_LO12S0_RELA            30
12350          R_NDS32_LO12S1_RELA            29
12351          R_NDS32_LO12S2_RELA            28
12352          R_NDS32_LO12S2_SP_RELA         71
12353          R_NDS32_LO12S2_DP_RELA         70
12354          R_NDS32_GOT_LO12               46
12355          R_NDS32_GOTOFF_LO12            50
12356          R_NDS32_PLTREL_LO12            65
12357          R_NDS32_PLT_GOTREL_LO12        67
12358          R_NDS32_17IFC_PCREL_RELA       96
12359          R_NDS32_GOT_SUFF               193
12360          R_NDS32_GOTOFF_SUFF            194
12361          R_NDS32_PLT_GOT_SUFF           195
12362          R_NDS32_MULCALL_SUFF           196
12363          R_NDS32_PTR                    197  */
12364       else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12365                 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12366                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12367                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12368                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12369                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12370                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12371                || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12372                || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12373                || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12374                    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12375                || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12376                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12377                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12378                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12379                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12380         seq_len = 0;
12381       else
12382         continue;
12383
12384       insn_len = seq_len;
12385       removed = FALSE;
12386
12387       switch (ELF32_R_TYPE (irel->r_info))
12388         {
12389         case R_NDS32_LONGCALL1:
12390           removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12391                                                &insn_len, contents, isymbuf,
12392                                                symtab_hdr);
12393           break;
12394         case R_NDS32_LONGCALL2:
12395           removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12396                                                &insn_len, contents, isymbuf,
12397                                                symtab_hdr);
12398           break;
12399         case R_NDS32_LONGCALL3:
12400           removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12401                                                &insn_len, contents, isymbuf,
12402                                                symtab_hdr);
12403           break;
12404         case R_NDS32_LONGJUMP1:
12405           removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12406                                                &insn_len, contents, isymbuf,
12407                                                symtab_hdr);
12408           break;
12409         case R_NDS32_LONGJUMP2:
12410           removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12411                                                &insn_len, contents, isymbuf,
12412                                                symtab_hdr);
12413           break;
12414         case R_NDS32_LONGJUMP3:
12415           removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12416                                                &insn_len, contents, isymbuf,
12417                                                symtab_hdr);
12418           break;
12419         case R_NDS32_LONGCALL4:
12420           removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12421                                                &insn_len, contents, isymbuf,
12422                                                symtab_hdr);
12423           break;
12424         case R_NDS32_LONGCALL5:
12425           removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12426                                                &insn_len, contents, isymbuf,
12427                                                symtab_hdr);
12428           break;
12429         case R_NDS32_LONGCALL6:
12430           removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12431                                                &insn_len, contents, isymbuf,
12432                                                symtab_hdr);
12433           break;
12434         case R_NDS32_LONGJUMP4:
12435           removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12436                                                &insn_len, contents, isymbuf,
12437                                                symtab_hdr);
12438           break;
12439         case R_NDS32_LONGJUMP5:
12440           removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12441                                                &insn_len, &seq_len, contents,
12442                                                isymbuf, symtab_hdr);
12443           break;
12444         case R_NDS32_LONGJUMP6:
12445           removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12446                                                &insn_len, &seq_len, contents,
12447                                                isymbuf, symtab_hdr);
12448           break;
12449         case R_NDS32_LONGJUMP7:
12450           removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12451                                                &insn_len, &seq_len, contents,
12452                                                isymbuf, symtab_hdr);
12453           break;
12454         case R_NDS32_LOADSTORE:
12455           removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12456                                                internal_relocs, &insn_len,
12457                                                contents, isymbuf, symtab_hdr,
12458                                                load_store_relax, table);
12459           break;
12460         case R_NDS32_LO12S0_RELA:
12461         case R_NDS32_LO12S1_RELA:
12462         case R_NDS32_LO12S2_RELA:
12463         case R_NDS32_LO12S2_DP_RELA:
12464         case R_NDS32_LO12S2_SP_RELA:
12465           /* Relax for low part.  */
12466           nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12467                                 contents, isymbuf, symtab_hdr, table);
12468
12469           /* It is impossible to delete blank, so just continue.  */
12470           continue;
12471         case R_NDS32_PTR:
12472           removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12473                                          &insn_len, &seq_len, contents);
12474           break;
12475         case R_NDS32_LSI:
12476           nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12477                                 contents, isymbuf, symtab_hdr, again);
12478           continue;
12479         case R_NDS32_GOT_LO12:
12480         case R_NDS32_GOTOFF_LO12:
12481         case R_NDS32_PLTREL_LO12:
12482         case R_NDS32_PLT_GOTREL_LO12:
12483         case R_NDS32_GOTPC_LO12:
12484         case R_NDS32_TLS_LE_LO12:
12485         case R_NDS32_TLS_LE_ADD:
12486         case R_NDS32_TLS_LE_LS:
12487         case R_NDS32_PLT_GOT_SUFF:
12488         case R_NDS32_GOT_SUFF:
12489         case R_NDS32_GOTOFF_SUFF:
12490           continue;
12491         default:
12492           continue;
12493         }
12494
12495       if (removed && seq_len - insn_len > 0)
12496         {
12497           if (!insert_nds32_elf_blank
12498               (&relax_blank_list, irel->r_offset + insn_len,
12499                seq_len - insn_len))
12500             goto error_return;
12501           *again = TRUE;
12502         }
12503     }
12504
12505   calc_nds32_blank_total (relax_blank_list);
12506
12507   if (table->relax_fp_as_gp)
12508     {
12509       if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12510                                  irelend, isymbuf))
12511         goto error_return;
12512
12513       if (!*again)
12514         {
12515           if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12516                                                irelend))
12517             goto error_return;
12518         }
12519     }
12520
12521   if (!*again)
12522     {
12523       if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12524                                      &relax_blank_list, optimize, opt_size))
12525         goto error_return;
12526     }
12527
12528   /* It doesn't matter optimize_for_space_no_align anymore.
12529        If object file is assembled with flag '-Os',
12530        the we don't adjust jump-destination on 4-byte boundary.  */
12531
12532   if (relax_blank_list)
12533     {
12534       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12535       relax_blank_list = NULL;
12536     }
12537
12538   if (!*again)
12539     {
12540       /* Closing the section, so we don't relax it anymore.  */
12541       bfd_vma sec_size_align;
12542       Elf_Internal_Rela *tmp_rel;
12543
12544       /* Pad to alignment boundary.  Only handle current section alignment.  */
12545       sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12546                        & ((-1U) << sec->alignment_power);
12547       if ((sec_size_align - sec->size) & 0x2)
12548         {
12549           insn16 = NDS32_NOP16;
12550           bfd_putb16 (insn16, contents + sec->size);
12551           sec->size += 2;
12552         }
12553
12554       while (sec_size_align != sec->size)
12555         {
12556           insn = NDS32_NOP32;
12557           bfd_putb32 (insn, contents + sec->size);
12558           sec->size += 4;
12559         }
12560
12561       tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12562                                         irelend, R_NDS32_RELAX_ENTRY);
12563       if (tmp_rel != irelend)
12564         tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12565
12566       clean_nds32_elf_blank ();
12567     }
12568
12569 finish:
12570   if (internal_relocs != NULL
12571       && elf_section_data (sec)->relocs != internal_relocs)
12572     free (internal_relocs);
12573
12574   if (contents != NULL
12575       && elf_section_data (sec)->this_hdr.contents != contents)
12576     free (contents);
12577
12578   if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12579     free (isymbuf);
12580
12581   return result;
12582
12583 error_return:
12584   result = FALSE;
12585   goto finish;
12586 }
12587
12588 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12589 {
12590   {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12591   {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12592   {NULL, 0, 0, 0, 0}
12593 };
12594
12595 static bfd_boolean
12596 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12597                             struct bfd_link_info *info,
12598                             void *finfo ATTRIBUTE_UNUSED,
12599                             bfd_boolean (*func) (void *, const char *,
12600                                                  Elf_Internal_Sym *,
12601                                                  asection *,
12602                                                  struct elf_link_hash_entry *)
12603                             ATTRIBUTE_UNUSED)
12604 {
12605   FILE *sym_ld_script = NULL;
12606   struct elf_nds32_link_hash_table *table;
12607
12608   table = nds32_elf_hash_table (info);
12609   sym_ld_script = table->sym_ld_script;
12610
12611   if (check_start_export_sym)
12612     fprintf (sym_ld_script, "}\n");
12613
12614   return TRUE;
12615 }
12616
12617 static enum elf_reloc_type_class
12618 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12619                             const asection *rel_sec ATTRIBUTE_UNUSED,
12620                             const Elf_Internal_Rela *rela)
12621 {
12622   switch ((int) ELF32_R_TYPE (rela->r_info))
12623     {
12624     case R_NDS32_RELATIVE:
12625       return reloc_class_relative;
12626     case R_NDS32_JMP_SLOT:
12627       return reloc_class_plt;
12628     case R_NDS32_COPY:
12629       return reloc_class_copy;
12630     default:
12631       return reloc_class_normal;
12632     }
12633 }
12634
12635 /* Put target dependent option into info hash table.  */
12636 void
12637 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12638                                    int relax_fp_as_gp,
12639                                    int eliminate_gc_relocs,
12640                                    FILE * sym_ld_script,
12641                                    int hyper_relax,
12642                                    int tls_desc_trampoline,
12643                                    int load_store_relax)
12644 {
12645   struct elf_nds32_link_hash_table *table;
12646
12647   table = nds32_elf_hash_table (link_info);
12648   if (table == NULL)
12649     return;
12650
12651   table->relax_fp_as_gp = relax_fp_as_gp;
12652   table->eliminate_gc_relocs = eliminate_gc_relocs;
12653   table->sym_ld_script = sym_ld_script;
12654   table->hyper_relax = hyper_relax;
12655   table->tls_desc_trampoline = tls_desc_trampoline;
12656   table ->load_store_relax = load_store_relax;
12657 }
12658 \f
12659
12660 /* These functions and data-structures are used for fp-as-gp
12661    optimization.  */
12662
12663 #define FAG_THRESHOLD   3       /* At least 3 gp-access.  */
12664 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12665    the read-only section and read-write section.  */
12666 #define FAG_WINDOW      (508 - 32)
12667
12668 /* An nds32_fag represent a gp-relative access.
12669    We find best fp-base by using a sliding window
12670    to find a base address which can cover most gp-access.  */
12671 struct nds32_fag
12672 {
12673   struct nds32_fag *next;       /* NULL-teminated linked list.  */
12674   bfd_vma addr;                 /* The address of this fag.  */
12675   Elf_Internal_Rela **relas;    /* The relocations associated with this fag.
12676                                    It is used for applying FP7U2_FLAG.  */
12677   int count;                    /* How many times this address is referred.
12678                                    There should be exactly `count' relocations
12679                                    in relas.  */
12680   int relas_capcity;            /* The buffer size of relas.
12681                                    We use an array instead of linked-list,
12682                                    and realloc is used to adjust buffer size.  */
12683 };
12684
12685 static void
12686 nds32_fag_init (struct nds32_fag *head)
12687 {
12688   memset (head, 0, sizeof (struct nds32_fag));
12689 }
12690
12691 static void
12692 nds32_fag_verify (struct nds32_fag *head)
12693 {
12694   struct nds32_fag *iter;
12695   struct nds32_fag *prev;
12696
12697   prev = NULL;
12698   iter = head->next;
12699   while (iter)
12700     {
12701       if (prev && prev->addr >= iter->addr)
12702         puts ("Bug in fp-as-gp insertion.");
12703       prev = iter;
12704       iter = iter->next;
12705     }
12706 }
12707
12708 /* Insert a fag in ascending order.
12709    If a fag of the same address already exists,
12710    they are chained by relas array.  */
12711
12712 static void
12713 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12714                   Elf_Internal_Rela * rel)
12715 {
12716   struct nds32_fag *iter;
12717   struct nds32_fag *new_fag;
12718   const int INIT_RELAS_CAP = 4;
12719
12720   for (iter = head;
12721        iter->next && iter->next->addr <= addr;
12722        iter = iter->next)
12723     /* Find somewhere to insert.  */ ;
12724
12725   /* `iter' will be equal to `head' if the list is empty.  */
12726   if (iter != head && iter->addr == addr)
12727     {
12728       /* The address exists in the list.
12729          Insert `rel' into relocation list, relas.  */
12730
12731       /* Check whether relas is big enough.  */
12732       if (iter->count >= iter->relas_capcity)
12733         {
12734           iter->relas_capcity *= 2;
12735           iter->relas = bfd_realloc
12736             (iter->relas, iter->relas_capcity * sizeof (void *));
12737         }
12738       iter->relas[iter->count++] = rel;
12739       return;
12740     }
12741
12742   /* This is a new address.  Create a fag node for it.  */
12743   new_fag = bfd_malloc (sizeof (struct nds32_fag));
12744   memset (new_fag, 0, sizeof (*new_fag));
12745   new_fag->addr = addr;
12746   new_fag->count = 1;
12747   new_fag->next = iter->next;
12748   new_fag->relas_capcity = INIT_RELAS_CAP;
12749   new_fag->relas = (Elf_Internal_Rela **)
12750     bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12751   new_fag->relas[0] = rel;
12752   iter->next = new_fag;
12753
12754   nds32_fag_verify (head);
12755 }
12756
12757 static void
12758 nds32_fag_free_list (struct nds32_fag *head)
12759 {
12760   struct nds32_fag *iter;
12761
12762   iter = head->next;
12763   while (iter)
12764     {
12765       struct nds32_fag *tmp = iter;
12766       iter = iter->next;
12767       free (tmp->relas);
12768       tmp->relas = NULL;
12769       free (tmp);
12770     }
12771 }
12772
12773 /* Find the best fp-base address.
12774    The relocation associated with that address is returned,
12775    so we can track the symbol instead of a fixed address.
12776
12777    When relaxation, the address of an datum may change,
12778    because a text section is shrinked, so the data section
12779    moves forward.  If the aligments of text and data section
12780    are different, their distance may change too.
12781    Therefore, tracking a fixed address is not appriate.  */
12782
12783 static int
12784 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12785 {
12786   struct nds32_fag *base;       /* First fag in the window.  */
12787   struct nds32_fag *last;       /* First fag outside the window.  */
12788   int accu = 0;                 /* Usage accumulation.  */
12789   struct nds32_fag *best;       /* Best fag.  */
12790   int baccu = 0;                /* Best accumulation.  */
12791
12792   /* Use first fag for initial, and find the last fag in the window.
12793
12794      In each iteration, we could simply subtract previous fag
12795      and accumulate following fags which are inside the window,
12796      untill we each the end.  */
12797
12798   if (head->next == NULL)
12799     {
12800       *bestpp = NULL;
12801       return 0;
12802     }
12803
12804   /* Initialize base.  */
12805   base = head->next;
12806   best = base;
12807   for (last = base;
12808        last && last->addr < base->addr + FAG_WINDOW;
12809        last = last->next)
12810     accu += last->count;
12811
12812   baccu = accu;
12813
12814   /* Record the best base in each iteration.  */
12815   while (base->next)
12816     {
12817       accu -= base->count;
12818       base = base->next;
12819       /* Account fags in window.  */
12820       for (/* Nothing.  */;
12821            last && last->addr < base->addr + FAG_WINDOW;
12822            last = last->next)
12823         accu += last->count;
12824
12825       /* A better fp-base?  */
12826       if (accu > baccu)
12827         {
12828           best = base;
12829           baccu = accu;
12830         }
12831     }
12832
12833   if (bestpp)
12834     *bestpp = best;
12835   return baccu;
12836 }
12837
12838 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12839    so we can convert it fo fp-relative access later.
12840    `best_fag' is the best fp-base.  Only those inside the window
12841    of best_fag is applied the flag.  */
12842
12843 static bfd_boolean
12844 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12845                       asection *sec, struct nds32_fag *best_fag,
12846                       Elf_Internal_Rela *internal_relocs,
12847                       Elf_Internal_Rela *irelend)
12848 {
12849   struct nds32_fag *ifag;
12850   bfd_vma best_fpbase, gp;
12851   bfd *output_bfd;
12852
12853   output_bfd = sec->output_section->owner;
12854   nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12855   best_fpbase = best_fag->addr;
12856
12857   if (best_fpbase > gp + sdata_range[1][1]
12858       || best_fpbase < gp - sdata_range[1][0])
12859     return FALSE;
12860
12861   /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12862      so we know they can be converted to lwi37.fp.   */
12863   for (ifag = best_fag;
12864        ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12865     {
12866       int i;
12867
12868       for (i = 0; i < ifag->count; i++)
12869         {
12870           Elf_Internal_Rela *insn16_rel;
12871           Elf_Internal_Rela *fag_rel;
12872
12873           fag_rel = ifag->relas[i];
12874
12875           /* Only if this is within the WINDOWS, FP7U2_FLAG
12876              is applied.  */
12877
12878           insn16_rel = find_relocs_at_address
12879             (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12880
12881           if (insn16_rel != irelend)
12882             insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12883         }
12884     }
12885   return TRUE;
12886 }
12887
12888 /* Reset INSN16 to clean fp as gp.  */
12889
12890 static void
12891 nds32_fag_unmark_relax (struct nds32_fag *fag,
12892                         Elf_Internal_Rela *internal_relocs,
12893                         Elf_Internal_Rela *irelend)
12894 {
12895   struct nds32_fag *ifag;
12896   int i;
12897   Elf_Internal_Rela *insn16_rel;
12898   Elf_Internal_Rela *fag_rel;
12899
12900   for (ifag = fag; ifag; ifag = ifag->next)
12901     {
12902       for (i = 0; i < ifag->count; i++)
12903         {
12904           fag_rel = ifag->relas[i];
12905
12906           /* Restore the INSN16 relocation.  */
12907           insn16_rel = find_relocs_at_address
12908             (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12909
12910           if (insn16_rel != irelend)
12911             insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12912         }
12913     }
12914 }
12915
12916 /* This is the main function of fp-as-gp optimization.
12917    It should be called by relax_section.  */
12918
12919 static bfd_boolean
12920 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12921                       bfd *abfd, asection *sec,
12922                       Elf_Internal_Rela *internal_relocs,
12923                       Elf_Internal_Rela *irelend,
12924                       Elf_Internal_Sym *isymbuf)
12925 {
12926   Elf_Internal_Rela *begin_rel = NULL;
12927   Elf_Internal_Rela *irel;
12928   struct nds32_fag fag_head;
12929   Elf_Internal_Shdr *symtab_hdr;
12930   bfd_byte *contents;
12931   bfd_boolean ifc_inside = FALSE;
12932
12933   /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12934
12935   /* Per-function fp-base selection.
12936      1. Create a list for all the gp-relative access.
12937      2. Base on those gp-relative address,
12938         find a fp-base which can cover most access.
12939      3. Use the fp-base for fp-as-gp relaxation.
12940
12941      NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12942      we should
12943      1. delete the `la $fp, _FP_BASE_' instruction and
12944      2. not convert lwi.gp to lwi37.fp.
12945
12946      To delete the _FP_BASE_ instruction, we simply apply
12947      R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12948
12949      To suppress the conversion, we simply NOT to apply
12950      R_NDS32_INSN16_FP7U2_FLAG flag.  */
12951
12952   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12953
12954   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12955       || !nds32_get_local_syms (abfd, sec, &isymbuf))
12956     return FALSE;
12957
12958   /* Check whether it is worth for fp-as-gp optimization,
12959      i.e., at least 3 gp-load.
12960
12961      Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12962      apply this optimization.  */
12963
12964   for (irel = internal_relocs; irel < irelend; irel++)
12965     {
12966       /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12967          One we enter the begin of the region, we track all the LW/ST
12968          instructions, so when we leave the region, we try to find
12969          the best fp-base address for those LW/ST instructions.  */
12970
12971       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12972           && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12973         {
12974           /* Begin of the region.  */
12975           if (begin_rel)
12976             /* xgettext:c-format */
12977             _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12978
12979           begin_rel = irel;
12980           nds32_fag_init (&fag_head);
12981           ifc_inside = FALSE;
12982         }
12983       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12984                && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12985         {
12986           int accu;
12987           struct nds32_fag *best_fag, *tmp_fag;
12988           int dist;
12989
12990           /* End of the region.
12991              Check whether it is worth to do fp-as-gp.  */
12992
12993           if (begin_rel == NULL)
12994             {
12995               /* xgettext:c-format */
12996               _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12997                                   abfd, sec);
12998               continue;
12999             }
13000
13001           accu = nds32_fag_find_base (&fag_head, &best_fag);
13002
13003           /* Clean FP7U2_FLAG because they may set ever.  */
13004           tmp_fag = fag_head.next;
13005           nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13006
13007           /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
13008           if (accu < FAG_THRESHOLD
13009               || !nds32_fag_mark_relax (link_info, sec, best_fag,
13010                                         internal_relocs, irelend))
13011             {
13012               /* Not worth to do fp-as-gp.  */
13013               begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13014               begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13015               irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13016               irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13017               nds32_fag_free_list (&fag_head);
13018               begin_rel = NULL;
13019               continue;
13020             }
13021
13022           /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13023              so we use it to record the distance to the reloction of best
13024              fp-base.  */
13025           dist = best_fag->relas[0] - begin_rel;
13026           BFD_ASSERT (dist > 0 && dist < 0xffffff);
13027           /* Use high 16 bits of addend to record the _FP_BASE_ matched
13028              relocation.  And get the base value when relocating.  */
13029           begin_rel->r_addend &= (0x1 << 16) - 1;
13030           begin_rel->r_addend |= dist << 16;
13031
13032           nds32_fag_free_list (&fag_head);
13033           begin_rel = NULL;
13034         }
13035
13036       if (begin_rel == NULL || ifc_inside)
13037         /* Skip if we are not in the region of fp-as-gp.  */
13038         continue;
13039
13040       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13041           || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13042         {
13043           bfd_vma addr;
13044           uint32_t insn;
13045
13046           /* A gp-relative access is found.  Insert it to the fag-list.  */
13047
13048           /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
13049           insn = bfd_getb32 (contents + irel->r_offset);
13050           if (!N32_IS_RT3 (insn))
13051             continue;
13052
13053           addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13054           nds32_fag_insert (&fag_head, addr, irel);
13055         }
13056       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13057         {
13058           begin_rel = NULL;
13059         }
13060       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13061                || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13062         {
13063           /* Suppress fp as gp when encounter ifc.  */
13064           ifc_inside = TRUE;
13065         }
13066     }
13067
13068   return TRUE;
13069 }
13070
13071 /* Remove unused `la $fp, _FD_BASE_' instruction.  */
13072
13073 static bfd_boolean
13074 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13075                                 Elf_Internal_Rela *internal_relocs,
13076                                 Elf_Internal_Rela *irelend)
13077 {
13078   Elf_Internal_Rela *irel;
13079   Elf_Internal_Shdr *symtab_hdr;
13080   bfd_byte *contents = NULL;
13081   nds32_elf_blank_t *relax_blank_list = NULL;
13082   bfd_boolean result = TRUE;
13083   bfd_boolean unused_region = FALSE;
13084
13085   /*
13086      NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13087        R_NDS32_17IFC_PCREL_RELA
13088        R_NDS32_10IFCU_PCREL_RELA.  */
13089
13090   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13091   nds32_get_section_contents (abfd, sec, &contents, TRUE);
13092
13093   for (irel = internal_relocs; irel < irelend; irel++)
13094     {
13095       /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13096          we marked to in previous pass.
13097          DO NOT scan relocations again, since we've alreadly decided it
13098          and set the flag.  */
13099       const char *syname;
13100       int syndx;
13101       uint32_t insn;
13102
13103       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13104           && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13105         unused_region = TRUE;
13106       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13107                && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13108         unused_region = FALSE;
13109
13110       /* We're not in the region.  */
13111       if (!unused_region)
13112         continue;
13113
13114       /* _FP_BASE_ must be a GLOBAL symbol.  */
13115       syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13116       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13117         continue;
13118
13119       /* The symbol name must be _FP_BASE_.  */
13120       syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13121       if (strcmp (syname, FP_BASE_NAME) != 0)
13122         continue;
13123
13124       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13125         {
13126           /* addi.gp  $fp, -256  */
13127           insn = bfd_getb32 (contents + irel->r_offset);
13128           if (insn != INSN_ADDIGP_TO_FP)
13129             continue;
13130         }
13131       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13132         {
13133           /* addi  $fp, $gp, -256  */
13134           insn = bfd_getb32 (contents + irel->r_offset);
13135           if (insn != INSN_ADDI_GP_TO_FP)
13136             continue;
13137         }
13138       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13139         {
13140           /* movi  $fp, FP_BASE  */
13141           insn = bfd_getb32 (contents + irel->r_offset);
13142           if (insn != INSN_MOVI_TO_FP)
13143             continue;
13144         }
13145       else
13146         continue;
13147
13148       /* We got here because a FP_BASE instruction is found.  */
13149       if (!insert_nds32_elf_blank_recalc_total
13150           (&relax_blank_list, irel->r_offset, 4))
13151         goto error_return;
13152     }
13153
13154 finish:
13155   if (relax_blank_list)
13156     {
13157       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13158       relax_blank_list = NULL;
13159     }
13160   return result;
13161
13162 error_return:
13163   result = FALSE;
13164   goto finish;
13165 }
13166
13167 /* This is a version of bfd_generic_get_relocated_section_contents.
13168    We need this variety because relaxation will modify the dwarf
13169    infomation.  When there is undefined symbol reference error mesage,
13170    linker need to dump line number where the symbol be used.  However
13171    the address is be relaxed, it can not get the original dwarf contents.
13172    The variety only modify function call for reading in the section.  */
13173
13174 static bfd_byte *
13175 nds32_elf_get_relocated_section_contents (bfd *abfd,
13176                                           struct bfd_link_info *link_info,
13177                                           struct bfd_link_order *link_order,
13178                                           bfd_byte *data,
13179                                           bfd_boolean relocatable,
13180                                           asymbol **symbols)
13181 {
13182   bfd *input_bfd = link_order->u.indirect.section->owner;
13183   asection *input_section = link_order->u.indirect.section;
13184   long reloc_size;
13185   arelent **reloc_vector;
13186   long reloc_count;
13187
13188   reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13189   if (reloc_size < 0)
13190     return NULL;
13191
13192   /* Read in the section.  */
13193   if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
13194     return NULL;
13195
13196   if (reloc_size == 0)
13197     return data;
13198
13199   reloc_vector = (arelent **) bfd_malloc (reloc_size);
13200   if (reloc_vector == NULL)
13201     return NULL;
13202
13203   reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13204                                         reloc_vector, symbols);
13205   if (reloc_count < 0)
13206     goto error_return;
13207
13208   if (reloc_count > 0)
13209     {
13210       arelent **parent;
13211       for (parent = reloc_vector; *parent != NULL; parent++)
13212         {
13213           char *error_message = NULL;
13214           asymbol *symbol;
13215           bfd_reloc_status_type r;
13216
13217           symbol = *(*parent)->sym_ptr_ptr;
13218           if (symbol->section && discarded_section (symbol->section))
13219             {
13220               bfd_vma off;
13221               static reloc_howto_type none_howto
13222                 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13223                          "unused", FALSE, 0, 0, FALSE);
13224
13225               off = (*parent)->address * bfd_octets_per_byte (input_bfd);
13226               _bfd_clear_contents ((*parent)->howto, input_bfd,
13227                                    input_section, data, off);
13228               (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13229               (*parent)->addend = 0;
13230               (*parent)->howto = &none_howto;
13231               r = bfd_reloc_ok;
13232             }
13233           else
13234             r = bfd_perform_relocation (input_bfd, *parent, data,
13235                                         input_section,
13236                                         relocatable ? abfd : NULL,
13237                                         &error_message);
13238
13239           if (relocatable)
13240             {
13241               asection *os = input_section->output_section;
13242
13243               /* A partial link, so keep the relocs.  */
13244               os->orelocation[os->reloc_count] = *parent;
13245               os->reloc_count++;
13246             }
13247
13248           if (r != bfd_reloc_ok)
13249             {
13250               switch (r)
13251                 {
13252                 case bfd_reloc_undefined:
13253                   (*link_info->callbacks->undefined_symbol)
13254                     (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13255                      input_bfd, input_section, (*parent)->address, TRUE);
13256                   break;
13257                 case bfd_reloc_dangerous:
13258                   BFD_ASSERT (error_message != NULL);
13259                   (*link_info->callbacks->reloc_dangerous)
13260                     (link_info, error_message,
13261                      input_bfd, input_section, (*parent)->address);
13262                   break;
13263                 case bfd_reloc_overflow:
13264                   (*link_info->callbacks->reloc_overflow)
13265                     (link_info, NULL,
13266                      bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13267                      (*parent)->howto->name, (*parent)->addend,
13268                      input_bfd, input_section, (*parent)->address);
13269                   break;
13270                 case bfd_reloc_outofrange:
13271                   /* PR ld/13730:
13272                      This error can result when processing some partially
13273                      complete binaries.  Do not abort, but issue an error
13274                      message instead.  */
13275                   link_info->callbacks->einfo
13276                     /* xgettext:c-format */
13277                     (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13278                      abfd, input_section, * parent);
13279                   goto error_return;
13280
13281                 default:
13282                   abort ();
13283                   break;
13284                 }
13285             }
13286         }
13287     }
13288
13289   free (reloc_vector);
13290   return data;
13291
13292 error_return:
13293   free (reloc_vector);
13294   return NULL;
13295 }
13296
13297 /* Check target symbol.  */
13298
13299 static bfd_boolean
13300 nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13301 {
13302   if (!sym || !sym->name || sym->name[0] != '$')
13303     return FALSE;
13304   return TRUE;
13305 }
13306
13307 /* nds32 find maybe function sym.  Ignore target special symbol
13308    first, and then go the general function.  */
13309
13310 static bfd_size_type
13311 nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13312                               bfd_vma *code_off)
13313 {
13314   if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13315     return 0;
13316
13317   return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13318 }
13319 \f
13320
13321 /* Do TLS model conversion.  */
13322
13323 typedef struct relax_group_list_t
13324 {
13325   Elf_Internal_Rela *relo;
13326   struct relax_group_list_t *next;
13327   struct relax_group_list_t *next_sibling;
13328   int id;
13329 } relax_group_list_t;
13330
13331 int
13332 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13333
13334 int
13335 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13336
13337 void
13338 dump_chain (relax_group_list_t *pHead);
13339
13340 int
13341 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13342 {
13343   relax_group_list_t *pNext = pHead;
13344
13345   /* Find place.  */
13346   while (pNext->next)
13347     {
13348       if (pNext->next->id > (int) pElem->r_addend)
13349         break;
13350
13351       pNext = pNext->next;
13352     }
13353
13354   /* Insert node.  */
13355   relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13356   if (!pNew)
13357     return FALSE;
13358
13359   relax_group_list_t *tmp = pNext->next;
13360   pNext->next = pNew;
13361
13362   pNew->id = pElem->r_addend;
13363   pNew->relo = pElem;
13364   pNew->next = tmp;
13365   pNew->next_sibling = NULL;
13366
13367   return TRUE;
13368 }
13369
13370 int
13371 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13372 {
13373   relax_group_list_t *pNext = pNode;
13374
13375   /* Find place.  */
13376   while (pNext->next_sibling)
13377     {
13378       pNext = pNext->next_sibling;
13379     }
13380
13381   /* Insert node.  */
13382   relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13383   if (!pNew)
13384     return FALSE;
13385
13386   relax_group_list_t *tmp = pNext->next_sibling;
13387   pNext->next_sibling = pNew;
13388
13389   pNew->id = -1;
13390   pNew->relo = pElem;
13391   pNew->next = NULL;
13392   pNew->next_sibling = tmp;
13393
13394   return TRUE;
13395 }
13396
13397 void
13398 dump_chain (relax_group_list_t *pHead)
13399 {
13400   relax_group_list_t *pNext = pHead->next;
13401   while (pNext)
13402     {
13403       printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13404       relax_group_list_t *pNextSib = pNext->next_sibling;
13405       while (pNextSib)
13406         {
13407           printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13408           pNextSib = pNextSib->next_sibling;
13409         }
13410       pNext = pNext->next;
13411       printf("\n");
13412     }
13413 }
13414
13415 /* Check R_NDS32_RELAX_GROUP of each section.
13416    There might be multiple sections in one object file.  */
13417
13418 int
13419 elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13420 {
13421   elf32_nds32_relax_group_t *relax_group_ptr =
13422     elf32_nds32_relax_group_ptr (abfd);
13423
13424   int min_id = relax_group_ptr->min_id;
13425   int max_id = relax_group_ptr->max_id;
13426
13427   Elf_Internal_Rela *rel;
13428   Elf_Internal_Rela *relend;
13429   Elf_Internal_Rela *relocs;
13430   enum elf_nds32_reloc_type rtype;
13431
13432   do
13433     {
13434       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13435       relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13436                                           TRUE /* keep_memory  */);
13437       if (relocs == NULL)
13438         break;
13439
13440       /* Check R_NDS32_RELAX_GROUP.  */
13441       relend = relocs + asec->reloc_count;
13442       for (rel = relocs; rel < relend; rel++)
13443         {
13444           int id;
13445           rtype = ELF32_R_TYPE (rel->r_info);
13446           if (rtype != R_NDS32_RELAX_GROUP)
13447             continue;
13448
13449           id = rel->r_addend;
13450           if (id < min_id)
13451             min_id = id;
13452           else if (id > max_id)
13453             max_id = id;
13454         }
13455     }
13456   while (FALSE);
13457
13458   if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs))
13459     free (relocs);
13460
13461   if ((min_id != relax_group_ptr->min_id)
13462       || (max_id != relax_group_ptr->max_id))
13463     {
13464       relax_group_ptr->count = max_id - min_id + 1;
13465       BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13466       relax_group_ptr->min_id = min_id;
13467       BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13468       relax_group_ptr->max_id = max_id;
13469     }
13470
13471   return relax_group_ptr->count;
13472 }
13473
13474 /* Reorder RELAX_GROUP ID when command line option '-r' is applied.  */
13475 struct section_id_list_t *relax_group_section_id_list = NULL;
13476
13477 struct section_id_list_t *
13478 elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13479 {
13480   struct section_id_list_t *result = NULL;
13481   struct section_id_list_t *lst = *lst_ptr;
13482
13483   if (NULL == lst)
13484     {
13485       result = (struct section_id_list_t *) calloc
13486         (1, sizeof (struct section_id_list_t));
13487       BFD_ASSERT (result); /* Feed me.  */
13488       result->id = id;
13489       *lst_ptr = result;
13490     }
13491   else
13492     {
13493       struct section_id_list_t *cur = lst;
13494       struct section_id_list_t *prv = NULL;
13495       struct section_id_list_t *sec = NULL;
13496
13497       while (cur)
13498         {
13499           if (cur->id < id)
13500             {
13501               prv = cur;
13502               cur = cur->next;
13503               continue;
13504             }
13505
13506           if (cur->id > id)
13507             {
13508               cur = NULL; /* To insert after prv.  */
13509               sec = cur;  /* In case prv == NULL.  */
13510             }
13511
13512           break;
13513         }
13514
13515       if (NULL == cur)
13516         {
13517           /* Insert after prv.  */
13518           result = (struct section_id_list_t *) calloc
13519             (1, sizeof (struct section_id_list_t));
13520           BFD_ASSERT (result); /* Feed me.  */
13521           result->id = id;
13522           if (NULL != prv)
13523             {
13524               result->next = prv->next;
13525               prv->next = result;
13526             }
13527           else
13528             {
13529               *lst_ptr = result;
13530               result->next = sec;
13531             }
13532         }
13533     }
13534
13535   return result;
13536 }
13537
13538 int
13539 elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13540 {
13541   static int next_relax_group_bias = 0;
13542
13543   elf32_nds32_relax_group_t *relax_group_ptr =
13544     elf32_nds32_relax_group_ptr (abfd);
13545
13546   bfd_boolean result = TRUE;
13547   Elf_Internal_Rela *rel;
13548   Elf_Internal_Rela *relend;
13549   Elf_Internal_Rela *relocs = NULL;
13550   enum elf_nds32_reloc_type rtype;
13551   struct section_id_list_t *node = NULL;
13552   int count = 0;
13553
13554   do
13555     {
13556       if (0 == relax_group_ptr->count)
13557         break;
13558
13559       /* Check if this section has been handled.  */
13560       node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13561       if (NULL == node)
13562         break; /* Hit, the section id has handled.  */
13563
13564       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13565       relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13566                                           TRUE /* keep_memory  */);
13567       if (relocs == NULL)
13568         {
13569           BFD_ASSERT (0); /* feed me */
13570           break;
13571         }
13572
13573       /* Allocate group id bias for this bfd!  */
13574       if (0 == relax_group_ptr->init)
13575         {
13576           relax_group_ptr->bias = next_relax_group_bias;
13577           next_relax_group_bias += relax_group_ptr->count;
13578           relax_group_ptr->init = 1;
13579         }
13580
13581       /* Reorder relax group groups.  */
13582       relend = relocs + asec->reloc_count;
13583       for (rel = relocs; rel < relend; rel++)
13584         {
13585           rtype = ELF32_R_TYPE(rel->r_info);
13586           if (rtype != R_NDS32_RELAX_GROUP)
13587             continue;
13588
13589           /* Change it.  */
13590           rel->r_addend += relax_group_ptr->bias;
13591           /* Debugging count.  */
13592           count++;
13593         }
13594     }
13595   while (FALSE);
13596
13597   if (relocs != NULL && elf_section_data (asec)->relocs != relocs)
13598     free (relocs);
13599
13600   return result;
13601 }
13602
13603 int
13604 nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13605                            struct bfd_link_info *lnkinfo)
13606 {
13607   bfd_boolean result = TRUE;
13608   Elf_Internal_Rela *irel;
13609   Elf_Internal_Rela *irelend;
13610   Elf_Internal_Rela *internal_relocs;
13611   unsigned long r_symndx;
13612   enum elf_nds32_reloc_type r_type;
13613
13614   Elf_Internal_Sym *local_syms = NULL;
13615   bfd_byte *contents = NULL;
13616
13617   relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13618
13619   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13620   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13621   sym_hashes = elf_sym_hashes (inbfd);
13622   sym_hashes_end =
13623     sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13624   if (!elf_bad_symtab (inbfd))
13625     sym_hashes_end -= symtab_hdr->sh_info;
13626
13627   /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
13628   if (bfd_link_relocatable (lnkinfo))
13629     {
13630       elf32_nds32_unify_relax_group (inbfd, insec);
13631       return result;
13632     }
13633
13634   /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13635   internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13636                                                TRUE /* keep_memory  */);
13637   if (internal_relocs == NULL)
13638     goto error_return;
13639
13640   irelend = internal_relocs + insec->reloc_count;
13641   irel = find_relocs_at_address (internal_relocs, internal_relocs,
13642                                  irelend, R_NDS32_RELAX_ENTRY);
13643   if (irel == irelend)
13644     goto finish;
13645
13646   /* Chain/remove groups.  */
13647   for (irel = internal_relocs; irel < irelend; irel++)
13648     {
13649       r_symndx = ELF32_R_SYM (irel->r_info);
13650       r_type = ELF32_R_TYPE (irel->r_info);
13651       if (r_type != R_NDS32_RELAX_GROUP)
13652         continue;
13653
13654       /* Remove it.  */
13655       irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13656       /* Chain it now.  */
13657       if (!list_insert (&chain, irel))
13658         goto error_return;
13659     }
13660
13661   /* Collect group relocations.  */
13662   /* Presume relocations are sorted.  */
13663   relax_group_list_t *pNext = chain.next;
13664   while (pNext)
13665     {
13666       for (irel = internal_relocs; irel < irelend; irel++)
13667         {
13668           if (irel->r_offset == pNext->relo->r_offset)
13669             {
13670               /* Ignore Non-TLS relocation types.  */
13671               r_type = ELF32_R_TYPE (irel->r_info);
13672               if ((R_NDS32_TLS_LE_HI20 > r_type)
13673                   || (R_NDS32_RELAX_ENTRY == r_type))
13674                 continue;
13675
13676               if (!list_insert_sibling (pNext, irel))
13677                 goto error_return;
13678             }
13679           else if (irel->r_offset > pNext->relo->r_offset)
13680             {
13681               pNext = pNext->next;
13682               if (!pNext)
13683                 break;
13684
13685               bfd_vma current_offset = pNext->relo->r_offset;
13686               if (irel->r_offset > current_offset)
13687                 irel = internal_relocs; /* restart from head */
13688               else
13689                 --irel; /* Check current irel again.  */
13690               continue;
13691             }
13692           else
13693             {
13694               /* This shouldn't be reached.  */
13695             }
13696         }
13697       if (pNext)
13698         pNext = pNext->next;
13699     }
13700
13701 #ifdef DUBUG_VERBOSE
13702   dump_chain(&chain);
13703 #endif
13704
13705   /* Get symbol table and section content.  */
13706   if (incontents)
13707     contents = incontents;
13708   else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13709            || !nds32_get_local_syms (inbfd, insec, &local_syms))
13710     goto error_return;
13711
13712   char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13713
13714   /* Convert TLS model each group if necessary.  */
13715   pNext = chain.next;
13716
13717   int cur_grp_id = -1;
13718   int sethi_rt = -1;
13719   int add_rt = -1;
13720   enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13721
13722   tls_type = org_tls_type = eff_tls_type = 0;
13723
13724   while (pNext)
13725     {
13726       relax_group_list_t *pNextSig = pNext->next_sibling;
13727       while (pNextSig)
13728         {
13729           struct elf_link_hash_entry *h = NULL;
13730
13731           irel = pNextSig->relo;
13732           r_symndx = ELF32_R_SYM(irel->r_info);
13733           r_type = ELF32_R_TYPE(irel->r_info);
13734
13735           if (pNext->id != cur_grp_id)
13736             {
13737               cur_grp_id = pNext->id;
13738               org_tls_type = get_tls_type (r_type, NULL);
13739               if (r_symndx >= symtab_hdr->sh_info)
13740                 {
13741                   h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13742                   while (h->root.type == bfd_link_hash_indirect
13743                          || h->root.type == bfd_link_hash_warning)
13744                     h = (struct elf_link_hash_entry *) h->root.u.i.link;
13745                   tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13746                 }
13747               else
13748                 {
13749                   tls_type = local_got_tls_type
13750                     ? local_got_tls_type[r_symndx]
13751                     : GOT_NORMAL;
13752                 }
13753
13754               eff_tls_type = 1 << (fls (tls_type) - 1);
13755               sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13756             }
13757
13758           if (eff_tls_type != org_tls_type)
13759             {
13760               switch (org_tls_type)
13761                 {
13762                   /* DESC to IEGP/IE/LE.  */
13763                 case GOT_TLS_DESC:
13764                   switch (eff_tls_type)
13765                     {
13766                     case GOT_TLS_IE:
13767                       switch (r_type)
13768                         {
13769                         case R_NDS32_TLS_DESC_HI20:
13770                           irel->r_info = ELF32_R_INFO(r_symndx,
13771                                                       R_NDS32_TLS_IE_HI20);
13772                           break;
13773                         case R_NDS32_TLS_DESC_LO12:
13774                           irel->r_info = ELF32_R_INFO(r_symndx,
13775                                                       R_NDS32_TLS_IE_LO12);
13776                           break;
13777                         case R_NDS32_TLS_DESC_ADD:
13778                           {
13779                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13780                             add_rt = N32_RT5 (insn);
13781                             insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13782                             bfd_putb32 (insn, contents + irel->r_offset);
13783
13784                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13785                           }
13786                           break;
13787                         case R_NDS32_TLS_DESC_FUNC:
13788                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13789                           irel->r_info = ELF32_R_INFO(r_symndx,
13790                                                       R_NDS32_RELAX_REMOVE);
13791                           break;
13792                         case R_NDS32_TLS_DESC_CALL:
13793                           {
13794                             uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13795                                                      REG_TP);
13796                             bfd_putb32 (insn, contents + irel->r_offset);
13797
13798                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13799                           }
13800                           break;
13801                         case R_NDS32_LOADSTORE:
13802                         case R_NDS32_PTR:
13803                         case R_NDS32_PTR_RESOLVED:
13804                         case R_NDS32_NONE:
13805                         case R_NDS32_LABEL:
13806                           break;
13807                         default:
13808                           BFD_ASSERT(0);
13809                           break;
13810                         }
13811                       break;
13812                     case GOT_TLS_IEGP:
13813                       switch (r_type)
13814                         {
13815                         case R_NDS32_TLS_DESC_HI20:
13816                           irel->r_info = ELF32_R_INFO(r_symndx,
13817                                                       R_NDS32_TLS_IEGP_HI20);
13818                           break;
13819                         case R_NDS32_TLS_DESC_LO12:
13820                           irel->r_info = ELF32_R_INFO(r_symndx,
13821                                                       R_NDS32_TLS_IEGP_LO12);
13822                           break;
13823                         case R_NDS32_TLS_DESC_ADD:
13824                           {
13825                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13826                             add_rt = N32_RT5 (insn);
13827                             insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13828                             bfd_putb32 (insn, contents + irel->r_offset);
13829
13830                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13831                           }
13832                           break;
13833                         case R_NDS32_TLS_DESC_FUNC:
13834                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13835                           irel->r_info = ELF32_R_INFO(r_symndx,
13836                                                       R_NDS32_RELAX_REMOVE);
13837                           break;
13838                         case R_NDS32_TLS_DESC_CALL:
13839                           {
13840                             uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13841                                                      REG_TP);
13842                             bfd_putb32 (insn, contents + irel->r_offset);
13843
13844                             irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13845                           }
13846                           break;
13847                         case R_NDS32_LOADSTORE:
13848                         case R_NDS32_PTR:
13849                         case R_NDS32_PTR_RESOLVED:
13850                         case R_NDS32_NONE:
13851                         case R_NDS32_LABEL:
13852                           break;
13853                         default:
13854                           BFD_ASSERT(0);
13855                           break;
13856                         }
13857                       break;
13858                     case GOT_TLS_LE:
13859                       switch (r_type)
13860                         {
13861                         case R_NDS32_TLS_DESC_HI20:
13862                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13863                           break;
13864                         case R_NDS32_TLS_DESC_LO12:
13865                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13866                           break;
13867                         case R_NDS32_TLS_DESC_ADD:
13868                           {
13869                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13870
13871                             add_rt = N32_RT5 (insn);
13872                             insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13873                             bfd_putb32 (insn, contents + irel->r_offset);
13874
13875                             irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13876                           }
13877                           break;
13878                         case R_NDS32_TLS_DESC_FUNC:
13879                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13880                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13881                           break;
13882                         case R_NDS32_TLS_DESC_CALL:
13883                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13884                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13885                           break;
13886                         case R_NDS32_LOADSTORE:
13887                         case R_NDS32_PTR:
13888                         case R_NDS32_PTR_RESOLVED:
13889                         case R_NDS32_NONE:
13890                         case R_NDS32_LABEL:
13891                           break;
13892                         default:
13893                           BFD_ASSERT(0);
13894                           break;
13895                         }
13896                       break;
13897                     default:
13898                       break;
13899                     }
13900                   break;
13901                   /* IEGP to IE/LE.  */
13902                 case GOT_TLS_IEGP:
13903                   switch (eff_tls_type)
13904                     {
13905                     case GOT_TLS_IE:
13906                       switch (r_type)
13907                         {
13908                         case R_NDS32_TLS_IEGP_HI20:
13909                           irel->r_info = ELF32_R_INFO(r_symndx,
13910                                                       R_NDS32_TLS_IE_HI20);
13911                           break;
13912                         case R_NDS32_TLS_IEGP_LO12:
13913                           irel->r_info = ELF32_R_INFO(r_symndx,
13914                                                       R_NDS32_TLS_IE_LO12);
13915                           break;
13916                         case R_NDS32_PTR_RESOLVED:
13917                           {
13918                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13919
13920                             add_rt = N32_RT5 (insn);
13921                             insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13922                             bfd_putb32 (insn, contents + irel->r_offset);
13923                           }
13924                           break;
13925                         case R_NDS32_TLS_IEGP_LW:
13926                           break;
13927                         case R_NDS32_LOADSTORE:
13928                         case R_NDS32_PTR:
13929                         case R_NDS32_NONE:
13930                         case R_NDS32_LABEL:
13931                           break;
13932                         default:
13933                           BFD_ASSERT(0);
13934                           break;
13935                         }
13936                       break;
13937                     case GOT_TLS_LE:
13938                       switch (r_type)
13939                         {
13940                         case R_NDS32_TLS_IEGP_HI20:
13941                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13942                           break;
13943                         case R_NDS32_TLS_IEGP_LO12:
13944                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13945                           break;
13946                         case R_NDS32_TLS_IEGP_LW:
13947                           bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13948                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13949                           break;
13950                         case R_NDS32_LOADSTORE:
13951                         case R_NDS32_PTR:
13952                         case R_NDS32_NONE:
13953                         case R_NDS32_LABEL:
13954                         case R_NDS32_PTR_RESOLVED:
13955                           break;
13956                         default:
13957                           BFD_ASSERT(0);
13958                           break;
13959                         }
13960                       break;
13961                     default:
13962                       break;
13963                     }
13964                   break;
13965                   /* IE to LE. */
13966                 case GOT_TLS_IE:
13967                   switch (eff_tls_type)
13968                     {
13969                     case GOT_TLS_LE:
13970                       switch (r_type)
13971                         {
13972                         case R_NDS32_TLS_IE_HI20:
13973                           irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13974                           break;
13975                         case R_NDS32_TLS_IE_LO12S2:
13976                           {
13977                             uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13978
13979                             add_rt = N32_RT5 (insn);
13980                             insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13981                             bfd_putb32 (insn, contents + irel->r_offset);
13982
13983                             irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13984                           }
13985                           break;
13986                         case R_NDS32_LOADSTORE:
13987                         case R_NDS32_PTR:
13988                         case R_NDS32_NONE:
13989                         case R_NDS32_LABEL:
13990                           break;
13991                         default:
13992                           BFD_ASSERT(0);
13993                           break;
13994                         }
13995                       break;
13996                     default:
13997                       break;
13998                     }
13999                   break;
14000                 default:
14001                   break;
14002                 }
14003             }
14004           pNextSig = pNextSig->next_sibling;
14005         }
14006
14007 #if 1
14008       pNext = pNext->next;
14009 #else
14010       while (pNext)
14011         {
14012           if (pNext->id != cur_grp_id)
14013             break;
14014           pNext = pNext->next;
14015         }
14016 #endif
14017     }
14018
14019 finish:
14020   if (incontents)
14021     contents = NULL;
14022
14023   if (internal_relocs != NULL
14024       && elf_section_data (insec)->relocs != internal_relocs)
14025     free (internal_relocs);
14026
14027   if (contents != NULL
14028       && elf_section_data (insec)->this_hdr.contents != contents)
14029     free (contents);
14030
14031   if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms)
14032     free (local_syms);
14033
14034   if (chain.next)
14035     {
14036       pNext = chain.next;
14037       relax_group_list_t *pDel;
14038       while (pNext)
14039         {
14040           pDel = pNext;
14041           pNext = pNext->next;
14042           free (pDel);
14043         }
14044     }
14045
14046   return result;
14047
14048 error_return:
14049   result = FALSE;
14050   goto finish;
14051 }
14052
14053 /* End TLS model conversion.  */
14054
14055 #define ELF_ARCH                                bfd_arch_nds32
14056 #define ELF_MACHINE_CODE                        EM_NDS32
14057 #define ELF_MAXPAGESIZE                         0x1000
14058 #define ELF_TARGET_ID                           NDS32_ELF_DATA
14059
14060 #define TARGET_BIG_SYM                          nds32_elf32_be_vec
14061 #define TARGET_BIG_NAME                         "elf32-nds32be"
14062 #define TARGET_LITTLE_SYM                       nds32_elf32_le_vec
14063 #define TARGET_LITTLE_NAME                      "elf32-nds32le"
14064
14065 #define elf_info_to_howto                       nds32_info_to_howto
14066 #define elf_info_to_howto_rel                   nds32_info_to_howto_rel
14067
14068 #define bfd_elf32_bfd_link_hash_table_create    nds32_elf_link_hash_table_create
14069 #define bfd_elf32_bfd_merge_private_bfd_data    nds32_elf_merge_private_bfd_data
14070 #define bfd_elf32_bfd_print_private_bfd_data    nds32_elf_print_private_bfd_data
14071 #define bfd_elf32_bfd_relax_section             nds32_elf_relax_section
14072 #define bfd_elf32_bfd_set_private_flags         nds32_elf_set_private_flags
14073
14074 #define bfd_elf32_mkobject                      nds32_elf_mkobject
14075 #define elf_backend_action_discarded            nds32_elf_action_discarded
14076 #define elf_backend_add_symbol_hook             nds32_elf_add_symbol_hook
14077 #define elf_backend_check_relocs                nds32_elf_check_relocs
14078 #define elf_backend_adjust_dynamic_symbol       nds32_elf_adjust_dynamic_symbol
14079 #define elf_backend_create_dynamic_sections     nds32_elf_create_dynamic_sections
14080 #define elf_backend_finish_dynamic_sections     nds32_elf_finish_dynamic_sections
14081 #define elf_backend_finish_dynamic_symbol       nds32_elf_finish_dynamic_symbol
14082 #define elf_backend_size_dynamic_sections       nds32_elf_size_dynamic_sections
14083 #define elf_backend_relocate_section            nds32_elf_relocate_section
14084 #define elf_backend_gc_mark_hook                nds32_elf_gc_mark_hook
14085 #define elf_backend_grok_prstatus               nds32_elf_grok_prstatus
14086 #define elf_backend_grok_psinfo                 nds32_elf_grok_psinfo
14087 #define elf_backend_reloc_type_class            nds32_elf_reloc_type_class
14088 #define elf_backend_copy_indirect_symbol        nds32_elf_copy_indirect_symbol
14089 #define elf_backend_link_output_symbol_hook     nds32_elf_output_symbol_hook
14090 #define elf_backend_output_arch_syms            nds32_elf_output_arch_syms
14091 #define elf_backend_object_p                    nds32_elf_object_p
14092 #define elf_backend_final_write_processing      nds32_elf_final_write_processing
14093 #define elf_backend_special_sections            nds32_elf_special_sections
14094 #define bfd_elf32_bfd_get_relocated_section_contents \
14095                                 nds32_elf_get_relocated_section_contents
14096 #define bfd_elf32_bfd_is_target_special_symbol  nds32_elf_is_target_special_symbol
14097 #define elf_backend_maybe_function_sym          nds32_elf_maybe_function_sym
14098
14099 #define elf_backend_can_gc_sections             1
14100 #define elf_backend_can_refcount                1
14101 #define elf_backend_want_got_plt                1
14102 #define elf_backend_plt_readonly                1
14103 #define elf_backend_want_plt_sym                0
14104 #define elf_backend_got_header_size             12
14105 #define elf_backend_may_use_rel_p               1
14106 #define elf_backend_default_use_rela_p          1
14107 #define elf_backend_may_use_rela_p              1
14108 #define elf_backend_dtrel_excludes_plt          0
14109
14110 #include "elf32-target.h"
14111
14112 #undef ELF_MAXPAGESIZE
14113 #define ELF_MAXPAGESIZE                         0x2000
14114
14115 #undef  TARGET_BIG_SYM
14116 #define TARGET_BIG_SYM                          nds32_elf32_linux_be_vec
14117 #undef  TARGET_BIG_NAME
14118 #define TARGET_BIG_NAME                         "elf32-nds32be-linux"
14119 #undef  TARGET_LITTLE_SYM
14120 #define TARGET_LITTLE_SYM                       nds32_elf32_linux_le_vec
14121 #undef  TARGET_LITTLE_NAME
14122 #define TARGET_LITTLE_NAME                      "elf32-nds32le-linux"
14123 #undef  elf32_bed
14124 #define elf32_bed                               elf32_nds32_lin_bed
14125
14126 #include "elf32-target.h"